From 5d123d652a06706864262a3ead7150d48a2ce0f0 Mon Sep 17 00:00:00 2001 From: Christoph Lendenfeld Date: Thu, 21 Dec 2023 12:09:57 +0100 Subject: [PATCH 1/3] unit tests --- tests/python/bl_animation_keyframing.py | 71 +++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/tests/python/bl_animation_keyframing.py b/tests/python/bl_animation_keyframing.py index bfb6a8c200c..13a3eb775df 100644 --- a/tests/python/bl_animation_keyframing.py +++ b/tests/python/bl_animation_keyframing.py @@ -395,6 +395,77 @@ class InsertAvailableTest(AbstractKeyframingTest, unittest.TestCase): bpy.data.objects.remove(keyed_object, do_unlink=True) +class InsertNeededTest(AbstractKeyframingTest, unittest.TestCase): + + def test_insert_needed_object(self): + keyed_object = _create_animation_object() + + bpy.context.scene.tool_settings.use_keyframe_insert_auto = True + bpy.context.preferences.edit.use_keyframe_insert_needed = True + bpy.context.preferences.edit.use_keyframe_insert_available = False + + with bpy.context.temp_override(**_get_view3d_context()): + bpy.context.scene.frame_set(1) + bpy.ops.transform.translate(value=(-1, 0, 0)) + bpy.context.scene.frame_set(5) + bpy.ops.transform.translate(value=(1, 0, 0)) + + action = keyed_object.animation_data.action + self.assertTrue(_fcurve_paths_match(action.fcurves, ["location"])) + + # With "Insert Needed" enabled it has to key all location channels first, + # before it can add keys only to the channels where values have actually + # changed. + expected_keys = { + "location": (2, 1, 1) + } + + for fcurve in action.fcurves: + if fcurve.data_path not in expected_keys: + raise AssertionError(f"Did not expect a key on {fcurve.data_path}") + self.assertEqual(expected_keys[fcurve.data_path][fcurve.array_index], len(fcurve.keyframe_points)) + + bpy.data.objects.remove(keyed_object, do_unlink=True) + bpy.context.scene.tool_settings.use_keyframe_insert_auto = False + bpy.context.preferences.edit.use_keyframe_insert_needed = False + + def test_insert_needed_bone(self): + armature_obj = _create_armature() + + bpy.context.scene.tool_settings.use_keyframe_insert_auto = True + bpy.context.preferences.edit.use_keyframe_insert_needed = True + bpy.context.preferences.edit.use_keyframe_insert_available = False + + bpy.ops.object.mode_set(mode='POSE') + with bpy.context.temp_override(**_get_view3d_context()): + bpy.context.scene.frame_set(1) + bpy.ops.transform.translate(value=(-1, 0, 0), orient_type='LOCAL') + bpy.context.scene.frame_set(5) + bpy.ops.transform.translate(value=(1, 0, 0), orient_type='LOCAL') + + bpy.ops.object.mode_set(mode='OBJECT') + + action = armature_obj.animation_data.action + bone_path = f"pose.bones[\"{_BONE_NAME}\"]" + self.assertTrue(_fcurve_paths_match(action.fcurves, [f"{bone_path}.location"])) + + # With "Insert Needed" enabled it has to key all location channels first, + # before it can add keys only to the channels where values have actually + # changed. + expected_keys = { + f"{bone_path}.location": (2, 1, 1) + } + + for fcurve in action.fcurves: + if fcurve.data_path not in expected_keys: + raise AssertionError(f"Did not expect a key on {fcurve.data_path}") + self.assertEqual(expected_keys[fcurve.data_path][fcurve.array_index], len(fcurve.keyframe_points)) + + bpy.data.objects.remove(armature_obj, do_unlink=True) + bpy.context.scene.tool_settings.use_keyframe_insert_auto = False + bpy.context.preferences.edit.use_keyframe_insert_needed = False + + def main(): global args import argparse -- 2.30.2 From 28c39cf08c437253a0a36de50a9ca269958a75fd Mon Sep 17 00:00:00 2001 From: Christoph Lendenfeld Date: Thu, 21 Dec 2023 12:35:52 +0100 Subject: [PATCH 2/3] review comments --- tests/python/bl_animation_keyframing.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/tests/python/bl_animation_keyframing.py b/tests/python/bl_animation_keyframing.py index 13a3eb775df..9fb36db40bb 100644 --- a/tests/python/bl_animation_keyframing.py +++ b/tests/python/bl_animation_keyframing.py @@ -397,13 +397,19 @@ class InsertAvailableTest(AbstractKeyframingTest, unittest.TestCase): class InsertNeededTest(AbstractKeyframingTest, unittest.TestCase): - def test_insert_needed_object(self): - keyed_object = _create_animation_object() - + def setUp(self): + super().setUp() bpy.context.scene.tool_settings.use_keyframe_insert_auto = True bpy.context.preferences.edit.use_keyframe_insert_needed = True bpy.context.preferences.edit.use_keyframe_insert_available = False + def tearDown(self): + bpy.context.scene.tool_settings.use_keyframe_insert_auto = False + bpy.context.preferences.edit.use_keyframe_insert_needed = False + + def test_insert_needed_object(self): + keyed_object = _create_animation_object() + with bpy.context.temp_override(**_get_view3d_context()): bpy.context.scene.frame_set(1) bpy.ops.transform.translate(value=(-1, 0, 0)) @@ -420,22 +426,18 @@ class InsertNeededTest(AbstractKeyframingTest, unittest.TestCase): "location": (2, 1, 1) } + self.assertEqual(len(action.fcurves), 3) + for fcurve in action.fcurves: if fcurve.data_path not in expected_keys: raise AssertionError(f"Did not expect a key on {fcurve.data_path}") self.assertEqual(expected_keys[fcurve.data_path][fcurve.array_index], len(fcurve.keyframe_points)) bpy.data.objects.remove(keyed_object, do_unlink=True) - bpy.context.scene.tool_settings.use_keyframe_insert_auto = False - bpy.context.preferences.edit.use_keyframe_insert_needed = False def test_insert_needed_bone(self): armature_obj = _create_armature() - bpy.context.scene.tool_settings.use_keyframe_insert_auto = True - bpy.context.preferences.edit.use_keyframe_insert_needed = True - bpy.context.preferences.edit.use_keyframe_insert_available = False - bpy.ops.object.mode_set(mode='POSE') with bpy.context.temp_override(**_get_view3d_context()): bpy.context.scene.frame_set(1) @@ -456,14 +458,14 @@ class InsertNeededTest(AbstractKeyframingTest, unittest.TestCase): f"{bone_path}.location": (2, 1, 1) } + self.assertEqual(len(action.fcurves), 3) + for fcurve in action.fcurves: if fcurve.data_path not in expected_keys: raise AssertionError(f"Did not expect a key on {fcurve.data_path}") self.assertEqual(expected_keys[fcurve.data_path][fcurve.array_index], len(fcurve.keyframe_points)) bpy.data.objects.remove(armature_obj, do_unlink=True) - bpy.context.scene.tool_settings.use_keyframe_insert_auto = False - bpy.context.preferences.edit.use_keyframe_insert_needed = False def main(): -- 2.30.2 From a936d5653cad1050bf145eded694954a42b50d49 Mon Sep 17 00:00:00 2001 From: Christoph Lendenfeld Date: Thu, 21 Dec 2023 16:22:45 +0100 Subject: [PATCH 3/3] remove object deletion --- tests/python/bl_animation_keyframing.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/python/bl_animation_keyframing.py b/tests/python/bl_animation_keyframing.py index 9fb36db40bb..6f1d606ba9d 100644 --- a/tests/python/bl_animation_keyframing.py +++ b/tests/python/bl_animation_keyframing.py @@ -433,8 +433,6 @@ class InsertNeededTest(AbstractKeyframingTest, unittest.TestCase): raise AssertionError(f"Did not expect a key on {fcurve.data_path}") self.assertEqual(expected_keys[fcurve.data_path][fcurve.array_index], len(fcurve.keyframe_points)) - bpy.data.objects.remove(keyed_object, do_unlink=True) - def test_insert_needed_bone(self): armature_obj = _create_armature() @@ -465,8 +463,6 @@ class InsertNeededTest(AbstractKeyframingTest, unittest.TestCase): raise AssertionError(f"Did not expect a key on {fcurve.data_path}") self.assertEqual(expected_keys[fcurve.data_path][fcurve.array_index], len(fcurve.keyframe_points)) - bpy.data.objects.remove(armature_obj, do_unlink=True) - def main(): global args -- 2.30.2