From 87ef870c48f0f0f30d6389c7a2bb1d0b674a9fd1 Mon Sep 17 00:00:00 2001 From: Christoph Lendenfeld Date: Fri, 5 Jan 2024 16:48:38 +0100 Subject: [PATCH 1/3] add unit tests --- tests/python/bl_animation_keyframing.py | 46 +++++++++++++++++++++---- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/tests/python/bl_animation_keyframing.py b/tests/python/bl_animation_keyframing.py index 47cb8459e52..5885104944a 100644 --- a/tests/python/bl_animation_keyframing.py +++ b/tests/python/bl_animation_keyframing.py @@ -204,11 +204,14 @@ class VisualKeyingTest(AbstractKeyframingTest, unittest.TestCase): class CycleAwareKeyingTest(AbstractKeyframingTest, unittest.TestCase): """ Check if cycle aware keying remaps the keyframes correctly and adds fcurve modifiers. """ - def test_insert_location_cycle_aware(self): + def setUp(self): + super().setUp() + bpy.context.scene.tool_settings.use_keyframe_cycle_aware = True + + def test_insert_by_name(self): # In order to make cycle aware keying work, the action needs to be created and have the # frame_range set plus the use_frame_range flag set to True. keyed_object = _create_animation_object() - bpy.context.scene.tool_settings.use_keyframe_cycle_aware = True with bpy.context.temp_override(**_get_view3d_context()): bpy.ops.anim.keyframe_insert_by_name(type="Location") @@ -216,18 +219,16 @@ class CycleAwareKeyingTest(AbstractKeyframingTest, unittest.TestCase): action = keyed_object.animation_data.action action.use_cyclic = True action.use_frame_range = True - cyclic_range_end = 20 - action.frame_range = [1, cyclic_range_end] + action.frame_range = [1, 20] with bpy.context.temp_override(**_get_view3d_context()): bpy.context.scene.frame_set(5) bpy.ops.anim.keyframe_insert_by_name(type="Location") # Will be mapped to frame 3. - # This will insert the key based on the user preference settings. bpy.context.preferences.edit.key_insert_channels = {"LOCATION"} bpy.context.scene.frame_set(22) - bpy.ops.anim.keyframe_insert() + bpy.ops.anim.keyframe_insert_by_name(type="Location") # Will be mapped to frame 9. bpy.context.scene.frame_set(-10) @@ -248,6 +249,39 @@ class CycleAwareKeyingTest(AbstractKeyframingTest, unittest.TestCase): # All fcurves should have a cycles modifier. self.assertTrue(fcurve.modifiers[0].type == "CYCLES") + def test_insert_key(self): + keyed_object = _create_animation_object() + + bpy.context.preferences.edit.key_insert_channels = {'LOCATION'} + + with bpy.context.temp_override(**_get_view3d_context()): + bpy.ops.anim.keyframe_insert() + + action = keyed_object.animation_data.action + action.use_cyclic = True + action.use_frame_range = True + action.frame_range = [1, 20] + + with bpy.context.temp_override(**_get_view3d_context()): + bpy.context.scene.frame_set(5) + bpy.ops.anim.keyframe_insert() + + # Will be mapped to frame 3. + bpy.context.scene.frame_set(22) + bpy.ops.anim.keyframe_insert() + + expected_keys = [1, 3, 5, 20] + + for fcurve in action.fcurves: + self.assertEqual(len(fcurve.keyframe_points), len(expected_keys)) + key_index = 0 + for key in fcurve.keyframe_points: + self.assertEqual(key.co.x, expected_keys[key_index]) + key_index += 1 + + # All fcurves should have a cycles modifier. + self.assertTrue(fcurve.modifiers[0].type == "CYCLES") + class AutoKeyframingTest(AbstractKeyframingTest, unittest.TestCase): -- 2.30.2 From b9bf84a48fa349043199f329491f026eb4fc5d0b Mon Sep 17 00:00:00 2001 From: Christoph Lendenfeld Date: Tue, 9 Jan 2024 14:59:18 +0100 Subject: [PATCH 2/3] wip --- source/blender/animrig/intern/keyframing.cc | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/source/blender/animrig/intern/keyframing.cc b/source/blender/animrig/intern/keyframing.cc index e7b8389895c..441cd6ae60b 100644 --- a/source/blender/animrig/intern/keyframing.cc +++ b/source/blender/animrig/intern/keyframing.cc @@ -483,7 +483,7 @@ static bool insert_keyframe_fcurve_value(Main *bmain, const bool success = insert_keyframe_value(fcu, cfra, curval, keytype, flag); - if (!success) { + if (!success && reports != nullptr) { BKE_reportf(reports, RPT_ERROR, "Failed to insert keys on F-Curve with path '%s[%d]', ensure that it is not " @@ -896,10 +896,18 @@ int insert_key_action(Main *bmain, int property_array_index = 0; int inserted_keys = 0; for (float value : values) { - FCurve *fcurve = action_fcurve_ensure( - bmain, action, group.c_str(), ptr, rna_path.c_str(), property_array_index); - const bool inserted_key = insert_keyframe_value( - fcurve, frame, value, key_type, insert_key_flag); + const bool inserted_key = insert_keyframe_fcurve_value(bmain, + nullptr, + ptr, + nullptr, + action, + group.c_str(), + rna_path.c_str(), + property_array_index, + nullptr, + value, + key_type, + insert_key_flag); if (inserted_key) { inserted_keys++; } -- 2.30.2 From 95bf299c8cc99730bd49aa034176b28bae8f8a8b Mon Sep 17 00:00:00 2001 From: Christoph Lendenfeld Date: Thu, 18 Jan 2024 17:59:12 +0100 Subject: [PATCH 3/3] the fix --- source/blender/animrig/ANIM_keyframing.hh | 1 + source/blender/animrig/intern/keyframing.cc | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/source/blender/animrig/ANIM_keyframing.hh b/source/blender/animrig/ANIM_keyframing.hh index 17441b77d2e..d2e8b2bdf44 100644 --- a/source/blender/animrig/ANIM_keyframing.hh +++ b/source/blender/animrig/ANIM_keyframing.hh @@ -197,6 +197,7 @@ bool autokeyframe_property(bContext *C, int insert_key_action(Main *bmain, bAction *action, PointerRNA *ptr, + PropertyRNA *prop, const std::string &rna_path, float frame, Span values, diff --git a/source/blender/animrig/intern/keyframing.cc b/source/blender/animrig/intern/keyframing.cc index ac228ce0e42..016f3e9e134 100644 --- a/source/blender/animrig/intern/keyframing.cc +++ b/source/blender/animrig/intern/keyframing.cc @@ -848,6 +848,7 @@ int clear_keyframe(Main *bmain, int insert_key_action(Main *bmain, bAction *action, PointerRNA *ptr, + PropertyRNA *prop, const std::string &rna_path, const float frame, const Span values, @@ -872,12 +873,12 @@ int insert_key_action(Main *bmain, const bool inserted_key = insert_keyframe_fcurve_value(bmain, nullptr, ptr, - nullptr, + prop, action, group.c_str(), rna_path.c_str(), property_array_index, - nullptr, + frame, value, key_type, insert_key_flag); @@ -952,6 +953,7 @@ void insert_key_rna(PointerRNA *rna_pointer, insert_key_count += insert_key_action(bmain, action, rna_pointer, + prop, rna_path_id_to_prop, nla_frame, rna_values.as_span(), -- 2.30.2