From a0aa5480b14f417412d881ebce81977dbba59c14 Mon Sep 17 00:00:00 2001 From: Christoph Lendenfeld Date: Thu, 10 Aug 2023 16:09:39 +0200 Subject: [PATCH] Animation: Blend Offset Slider MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds an operator to the Graph Editor that moves the selected segment up/down so it aligns with the keys before/after the segment. Co-authored-by: Ares Deveaux Co-authored-by: Sybren A. Stüvel Pull Request: https://projects.blender.org/blender/blender/pulls/110544 --- scripts/startup/bl_ui/space_graph.py | 1 + .../editors/animation/keyframes_general.cc | 25 +++++ .../editors/include/ED_keyframes_edit.hh | 8 ++ .../editors/space_graph/graph_intern.h | 1 + .../blender/editors/space_graph/graph_ops.cc | 1 + .../editors/space_graph/graph_slider_ops.cc | 91 +++++++++++++++++++ 6 files changed, 127 insertions(+) diff --git a/scripts/startup/bl_ui/space_graph.py b/scripts/startup/bl_ui/space_graph.py index 8d458b99864..5bdc303e4ec 100644 --- a/scripts/startup/bl_ui/space_graph.py +++ b/scripts/startup/bl_ui/space_graph.py @@ -299,6 +299,7 @@ class GRAPH_MT_key_blending(Menu): layout.operator("graph.blend_to_neighbor", text="Blend to Neighbor") layout.operator("graph.blend_to_default", text="Blend to Default Value") layout.operator("graph.ease", text="Ease") + layout.operator("graph.blend_offset", text="Blend Offset") class GRAPH_MT_key_smoothing(Menu): diff --git a/source/blender/editors/animation/keyframes_general.cc b/source/blender/editors/animation/keyframes_general.cc index 5b20ea6fbe0..5d5a42ea459 100644 --- a/source/blender/editors/animation/keyframes_general.cc +++ b/source/blender/editors/animation/keyframes_general.cc @@ -679,6 +679,31 @@ void ease_fcurve_segment(FCurve *fcu, FCurveSegment *segment, const float factor /* ---------------- */ +void blend_offset_fcurve_segment(FCurve *fcu, FCurveSegment *segment, const float factor) +{ + const BezTriple *left_key = fcurve_segment_start_get(fcu, segment->start_index); + const BezTriple *right_key = fcurve_segment_end_get(fcu, segment->start_index + segment->length); + + float y_delta; + + if (factor > 0) { + const BezTriple segment_last_key = fcu->bezt[segment->start_index + segment->length - 1]; + y_delta = right_key->vec[1][1] - segment_last_key.vec[1][1]; + } + else { + const BezTriple segment_first_key = fcu->bezt[segment->start_index]; + y_delta = left_key->vec[1][1] - segment_first_key.vec[1][1]; + } + + const float offset_value = y_delta * fabs(factor); + for (int i = segment->start_index; i < segment->start_index + segment->length; i++) { + const float key_y_value = fcu->bezt[i].vec[1][1] + offset_value; + BKE_fcurve_keyframe_move_value_with_handles(&fcu->bezt[i], key_y_value); + } +} + +/* ---------------- */ + void breakdown_fcurve_segment(FCurve *fcu, FCurveSegment *segment, const float factor) { const BezTriple *left_bezt = fcurve_segment_start_get(fcu, segment->start_index); diff --git a/source/blender/editors/include/ED_keyframes_edit.hh b/source/blender/editors/include/ED_keyframes_edit.hh index 34eaa6526a4..ad0bc260b72 100644 --- a/source/blender/editors/include/ED_keyframes_edit.hh +++ b/source/blender/editors/include/ED_keyframes_edit.hh @@ -458,6 +458,14 @@ void smooth_fcurve_segment(FCurve *fcu, int kernel_size, double *kernel); void ease_fcurve_segment(FCurve *fcu, FCurveSegment *segment, float factor); +/** + * Shift the FCurve segment up/down so that it aligns with the key before/after + * the segment. + * + * \param factor blend factor from -1.0 to 1.0. The sign determines whether the + * segment is aligned with the key before or after the segment. + */ +void blend_offset_fcurve_segment(FCurve *fcu, FCurveSegment *segment, float factor); bool decimate_fcurve(bAnimListElem *ale, float remove_ratio, float error_sq_max); /** diff --git a/source/blender/editors/space_graph/graph_intern.h b/source/blender/editors/space_graph/graph_intern.h index 3d1eabd4df9..ff1a5018020 100644 --- a/source/blender/editors/space_graph/graph_intern.h +++ b/source/blender/editors/space_graph/graph_intern.h @@ -119,6 +119,7 @@ void GRAPH_OT_clean(struct wmOperatorType *ot); void GRAPH_OT_blend_to_neighbor(struct wmOperatorType *ot); void GRAPH_OT_breakdown(struct wmOperatorType *ot); void GRAPH_OT_ease(struct wmOperatorType *ot); +void GRAPH_OT_blend_offset(struct wmOperatorType *ot); void GRAPH_OT_decimate(struct wmOperatorType *ot); void GRAPH_OT_blend_to_default(struct wmOperatorType *ot); void GRAPH_OT_butterworth_smooth(struct wmOperatorType *ot); diff --git a/source/blender/editors/space_graph/graph_ops.cc b/source/blender/editors/space_graph/graph_ops.cc index a21e6883c83..547f874d98d 100644 --- a/source/blender/editors/space_graph/graph_ops.cc +++ b/source/blender/editors/space_graph/graph_ops.cc @@ -469,6 +469,7 @@ void graphedit_operatortypes() WM_operatortype_append(GRAPH_OT_blend_to_neighbor); WM_operatortype_append(GRAPH_OT_breakdown); WM_operatortype_append(GRAPH_OT_ease); + WM_operatortype_append(GRAPH_OT_blend_offset); WM_operatortype_append(GRAPH_OT_blend_to_default); WM_operatortype_append(GRAPH_OT_gaussian_smooth); WM_operatortype_append(GRAPH_OT_butterworth_smooth); diff --git a/source/blender/editors/space_graph/graph_slider_ops.cc b/source/blender/editors/space_graph/graph_slider_ops.cc index d094a5c22d7..5ba0c5641cb 100644 --- a/source/blender/editors/space_graph/graph_slider_ops.cc +++ b/source/blender/editors/space_graph/graph_slider_ops.cc @@ -983,6 +983,97 @@ void GRAPH_OT_ease(wmOperatorType *ot) 1.0f); } +/* -------------------------------------------------------------------- */ +/** \name Blend Offset Operator + * \{ */ + +static void blend_offset_graph_keys(bAnimContext *ac, const float factor) +{ + apply_fcu_segment_function(ac, factor, blend_offset_fcurve_segment); +} + +static void blend_offset_draw_status_header(bContext *C, tGraphSliderOp *gso) +{ + common_draw_status_header(C, gso, "Blend Offset Keys"); +} + +static void blend_offset_modal_update(bContext *C, wmOperator *op) +{ + tGraphSliderOp *gso = static_cast(op->customdata); + + blend_offset_draw_status_header(C, gso); + + /* Reset keyframes to the state at invoke. */ + reset_bezts(gso); + const float factor = slider_factor_get_and_remember(op); + blend_offset_graph_keys(&gso->ac, factor); + WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME | NA_EDITED, NULL); +} + +static int blend_offset_invoke(bContext *C, wmOperator *op, const wmEvent *event) +{ + const int invoke_result = graph_slider_invoke(C, op, event); + + if (invoke_result == OPERATOR_CANCELLED) { + return invoke_result; + } + + tGraphSliderOp *gso = static_cast(op->customdata); + gso->modal_update = blend_offset_modal_update; + gso->factor_prop = RNA_struct_find_property(op->ptr, "factor"); + blend_offset_draw_status_header(C, gso); + ED_slider_factor_bounds_set(gso->slider, -1, 1); + ED_slider_factor_set(gso->slider, 0.0f); + + return invoke_result; +} + +static int blend_offset_exec(bContext *C, wmOperator *op) +{ + bAnimContext ac; + + /* Get editor data. */ + if (ANIM_animdata_get_context(C, &ac) == 0) { + return OPERATOR_CANCELLED; + } + + const float factor = RNA_float_get(op->ptr, "factor"); + + blend_offset_graph_keys(&ac, factor); + + /* Set notifier that keyframes have changed. */ + WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME | NA_EDITED, NULL); + + return OPERATOR_FINISHED; +} + +void GRAPH_OT_blend_offset(wmOperatorType *ot) +{ + /* Identifiers. */ + ot->name = "Blend Offset Keyframes"; + ot->idname = "GRAPH_OT_blend_offset"; + ot->description = "Shift selected keys to the value of the neighboring keys as a block"; + + /* API callbacks. */ + ot->invoke = blend_offset_invoke; + ot->modal = graph_slider_modal; + ot->exec = blend_offset_exec; + ot->poll = graphop_editable_keyframes_poll; + + /* Flags. */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + RNA_def_float_factor(ot->srna, + "factor", + 0.0f, + -FLT_MAX, + FLT_MAX, + "Curve Bend", + "Control the bend of the curve", + -1.0f, + 1.0f); +} + /** \} */ /* -------------------------------------------------------------------- */ /** \name Gauss Smooth Operator