diff --git a/scripts/startup/bl_ui/space_graph.py b/scripts/startup/bl_ui/space_graph.py index 5bdc303e4ec..6a8d73e45ba 100644 --- a/scripts/startup/bl_ui/space_graph.py +++ b/scripts/startup/bl_ui/space_graph.py @@ -300,6 +300,7 @@ class GRAPH_MT_key_blending(Menu): 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") + layout.operator("graph.blend_to_ease", text="Blend to Ease") 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 5d5a42ea459..ae954691cb8 100644 --- a/source/blender/editors/animation/keyframes_general.cc +++ b/source/blender/editors/animation/keyframes_general.cc @@ -704,6 +704,75 @@ void blend_offset_fcurve_segment(FCurve *fcu, FCurveSegment *segment, const floa /* ---------------- */ +static float s_curve(float x, float slope, float width, float height, float xshift, float yshift) +{ + /* Formula for 'S' curve we use for the "ease" sliders. The shift values move the curve verticaly + * or horizontaly. The range of the curve used is from 0 to 1 on "x" and "y" so we can scale it + * (width and height) and move it (xshift and y yshift) to crop the part of the curve we need. + * Slope determins how curvy the shape is. */ + float y = height * pow((x - xshift), slope) / + (pow((x - xshift), slope) + pow((width - (x - xshift)), slope)) + + yshift; + + /* The curve doesn't do what we want beyond our margins so we clamp the values. */ + if (x > xshift + width) { + y = height + yshift; + } + else if (x < xshift) { + y = yshift; + } + return y; +} + +void blend_to_ease_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); + + const float key_x_range = right_key->vec[1][0] - left_key->vec[1][0]; + const float key_y_range = right_key->vec[1][1] - left_key->vec[1][1]; + + /* Happens if there is only 1 key on the FCurve. Needs to be skipped because it + * would be a divide by 0. */ + if (IS_EQF(key_x_range, 0.0f)) { + return; + } + + const float slope = 3.0; + /* By doubling the size of the "S" curve we just one side of it, a "C" shape. */ + const float width = 2.0; + const float height = 2.0; + float xy_shift; + + /* Shifting the x and y values we can decide what side of the "S" shape to use. */ + if (factor > 0) { + xy_shift = -1.0; + } + else { + xy_shift = 0.0; + } + + for (int i = segment->start_index; i < segment->start_index + segment->length; i++) { + + const float x = (fcu->bezt[i].vec[1][0] - left_key->vec[1][0]) / key_x_range; + const float ease = s_curve(x, slope, width, height, xy_shift, xy_shift); + const float base = left_key->vec[1][1] + key_y_range * ease; + + float y_delta; + if (factor > 0) { + y_delta = base - fcu->bezt[i].vec[1][1]; + } + else { + y_delta = fcu->bezt[i].vec[1][1] - base; + } + + const float key_y_value = fcu->bezt[i].vec[1][1] + y_delta * factor; + 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 ad0bc260b72..276a54e9cfd 100644 --- a/source/blender/editors/include/ED_keyframes_edit.hh +++ b/source/blender/editors/include/ED_keyframes_edit.hh @@ -466,6 +466,7 @@ void ease_fcurve_segment(FCurve *fcu, FCurveSegment *segment, float factor); * segment is aligned with the key before or after the segment. */ void blend_offset_fcurve_segment(FCurve *fcu, FCurveSegment *segment, float factor); +void blend_to_ease_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 ff1a5018020..0f719d39109 100644 --- a/source/blender/editors/space_graph/graph_intern.h +++ b/source/blender/editors/space_graph/graph_intern.h @@ -120,6 +120,7 @@ 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_blend_to_ease(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 547f874d98d..15ee31d7557 100644 --- a/source/blender/editors/space_graph/graph_ops.cc +++ b/source/blender/editors/space_graph/graph_ops.cc @@ -470,6 +470,7 @@ void graphedit_operatortypes() 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_ease); 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 fa79e55418b..8409efe73db 100644 --- a/source/blender/editors/space_graph/graph_slider_ops.cc +++ b/source/blender/editors/space_graph/graph_slider_ops.cc @@ -1074,6 +1074,98 @@ void GRAPH_OT_blend_offset(wmOperatorType *ot) 1.0f); } +/* -------------------------------------------------------------------- */ +/** \name Blend to Ease Operator + * \{ */ + +static void blend_to_ease_graph_keys(bAnimContext *ac, const float factor) +{ + apply_fcu_segment_function(ac, factor, blend_to_ease_fcurve_segment); +} + +static void blend_to_ease_draw_status_header(bContext *C, tGraphSliderOp *gso) +{ + common_draw_status_header(C, gso, "Blend to Ease Keys"); +} + +static void blend_to_ease_modal_update(bContext *C, wmOperator *op) +{ + tGraphSliderOp *gso = static_cast(op->customdata); + + blend_to_ease_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_to_ease_graph_keys(&gso->ac, factor); + WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME | NA_EDITED, NULL); +} + +static int blend_to_ease_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_to_ease_modal_update; + gso->factor_prop = RNA_struct_find_property(op->ptr, "factor"); + blend_to_ease_draw_status_header(C, gso); + ED_slider_allow_overshoot_set(gso->slider, false, false); + ED_slider_factor_bounds_set(gso->slider, -1, 1); + ED_slider_factor_set(gso->slider, 0.0f); + + return invoke_result; +} + +static int blend_to_ease_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_to_ease_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_to_ease(wmOperatorType *ot) +{ + /* Identifiers. */ + ot->name = "Blend to Ease Keyframes"; + ot->idname = "GRAPH_OT_blend_to_ease"; + ot->description = "Blends keyframes from current state to an ease-in or ease-out curve"; + + /* API callbacks. */ + ot->invoke = blend_to_ease_invoke; + ot->modal = graph_slider_modal; + ot->exec = blend_to_ease_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