From 06bcbc644ceccff8868c3d1eac28a13412094661 Mon Sep 17 00:00:00 2001 From: Christoph Lendenfeld Date: Fri, 10 Nov 2023 12:22:45 +0100 Subject: [PATCH] Refactor: extract function from insert_keyframe_value No functional changes. Extract code from the function `insert_keyframe_value` to make it easier to use in other places. I created a new function `remap_driver_frame` that removes 3 parameters from the function. And I removed the requirement for a `ReportList` by just returning false and letting the caller create a message. Pull Request: https://projects.blender.org/blender/blender/pulls/114700 --- source/blender/animrig/intern/keyframing.cc | 85 ++++++++++++--------- 1 file changed, 51 insertions(+), 34 deletions(-) diff --git a/source/blender/animrig/intern/keyframing.cc b/source/blender/animrig/intern/keyframing.cc index 51f21080676..ed473b881eb 100644 --- a/source/blender/animrig/intern/keyframing.cc +++ b/source/blender/animrig/intern/keyframing.cc @@ -366,39 +366,29 @@ static AnimationEvalContext nla_time_remap(const AnimationEvalContext *anim_eval return *anim_eval_context; } -/* Insert the specified keyframe value into a single F-Curve. */ -static bool insert_keyframe_value(ReportList *reports, - PointerRNA *ptr, - PropertyRNA *prop, - FCurve *fcu, - const AnimationEvalContext *anim_eval_context, - float curval, - eBezTriple_KeyframeType keytype, - eInsertKeyFlags flag) +/* Adjust frame on which to add keyframe, to make it easier to add corrective drivers. */ +static float remap_driver_frame(const AnimationEvalContext *anim_eval_context, + PointerRNA *ptr, + PropertyRNA *prop, + const FCurve *fcu) { - if (BKE_fcurve_is_keyframable(fcu) == 0) { - BKE_reportf( - reports, - RPT_ERROR, - "F-Curve with path '%s[%d]' cannot be keyframed, ensure that it is not locked or sampled, " - "and try removing F-Modifiers", - fcu->rna_path, - fcu->array_index); - return false; - } - float cfra = anim_eval_context->eval_time; + PathResolvedRNA anim_rna; + if (RNA_path_resolved_create(ptr, prop, fcu->array_index, &anim_rna)) { + cfra = evaluate_driver(&anim_rna, fcu->driver, fcu->driver, anim_eval_context); + } + else { + cfra = 0.0f; + } + return cfra; +} - /* Adjust frame on which to add keyframe, to make it easier to add corrective drivers. */ - if ((flag & INSERTKEY_DRIVER) && (fcu->driver)) { - PathResolvedRNA anim_rna; - - if (RNA_path_resolved_create(ptr, prop, fcu->array_index, &anim_rna)) { - cfra = evaluate_driver(&anim_rna, fcu->driver, fcu->driver, anim_eval_context); - } - else { - cfra = 0.0f; - } +/* Insert the specified keyframe value into a single F-Curve. */ +static bool insert_keyframe_value( + FCurve *fcu, float cfra, float curval, eBezTriple_KeyframeType keytype, eInsertKeyFlags flag) +{ + if (!BKE_fcurve_is_keyframable(fcu)) { + return false; } /* Adjust coordinates for cycle aware insertion. */ @@ -507,8 +497,22 @@ bool insert_keyframe_direct(ReportList *reports, return false; } - return insert_keyframe_value( - reports, &ptr, prop, fcu, anim_eval_context, current_value, keytype, flag); + float cfra = anim_eval_context->eval_time; + if ((flag & INSERTKEY_DRIVER) && (fcu->driver)) { + cfra = remap_driver_frame(anim_eval_context, &ptr, prop, fcu); + } + + const bool success = insert_keyframe_value(fcu, cfra, current_value, keytype, flag); + + if (!success) { + BKE_reportf(reports, + RPT_ERROR, + "Failed to insert keys on F-Curve with path '%s[%d]', ensure that it is not " + "locked or sampled, and try removing F-Modifiers", + fcu->rna_path, + fcu->array_index); + } + return success; } /** Find or create the FCurve based on the given path, and insert the specified value into it. */ @@ -565,8 +569,21 @@ static bool insert_keyframe_fcurve_value(Main *bmain, /* Update F-Curve flags to ensure proper behavior for property type. */ update_autoflags_fcurve_direct(fcu, prop); - const bool success = insert_keyframe_value( - reports, ptr, prop, fcu, anim_eval_context, curval, keytype, flag); + float cfra = anim_eval_context->eval_time; + if ((flag & INSERTKEY_DRIVER) && (fcu->driver)) { + cfra = remap_driver_frame(anim_eval_context, ptr, prop, fcu); + } + + const bool success = insert_keyframe_value(fcu, cfra, curval, keytype, flag); + + if (!success) { + BKE_reportf(reports, + RPT_ERROR, + "Failed to insert keys on F-Curve with path '%s[%d]', ensure that it is not " + "locked or sampled, and try removing F-Modifiers", + fcu->rna_path, + fcu->array_index); + } /* If the curve is new, make it cyclic if appropriate. */ if (is_cyclic_action && is_new_curve) {