diff --git a/scripts/startup/bl_ui/space_graph.py b/scripts/startup/bl_ui/space_graph.py index 73acde0b5fc..f18d4ae7e88 100644 --- a/scripts/startup/bl_ui/space_graph.py +++ b/scripts/startup/bl_ui/space_graph.py @@ -324,6 +324,7 @@ class GRAPH_MT_key_blending(Menu): layout.operator("graph.blend_offset", text="Blend Offset") layout.operator("graph.blend_to_ease", text="Blend to Ease") layout.operator("graph.match_slope", text="Match Slope") + layout.operator("graph.push_pull", text="Push Pull") layout.operator("graph.shear", text="Shear Keys") layout.operator("graph.scale_average", text="Scale Average") diff --git a/source/blender/editors/animation/keyframes_general.cc b/source/blender/editors/animation/keyframes_general.cc index e5a5d7c5aa9..5e60f2ce7ed 100644 --- a/source/blender/editors/animation/keyframes_general.cc +++ b/source/blender/editors/animation/keyframes_general.cc @@ -881,6 +881,35 @@ void shear_fcurve_segment(FCurve *fcu, /* ---------------- */ +void push_pull_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; + } + + for (int i = segment->start_index; i < segment->start_index + segment->length; i++) { + /* For easy calculation of the curve, the values are normalized. */ + const float normalized_x = (fcu->bezt[i].vec[1][0] - left_key->vec[1][0]) / key_x_range; + + const float linear = left_key->vec[1][1] + key_y_range * normalized_x; + + const float delta = fcu->bezt[i].vec[1][1] - linear; + + const float key_y_value = linear + 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 b0aca9b86cd..e32fd9dc07e 100644 --- a/source/blender/editors/include/ED_keyframes_edit.hh +++ b/source/blender/editors/include/ED_keyframes_edit.hh @@ -431,7 +431,7 @@ void clean_fcurve(bAnimContext *ac, bAnimListElem *ale, float thresh, bool clear void blend_to_neighbor_fcurve_segment(FCurve *fcu, FCurveSegment *segment, float factor); void breakdown_fcurve_segment(FCurve *fcu, FCurveSegment *segment, float factor); void scale_average_fcurve_segment(struct FCurve *fcu, struct FCurveSegment *segment, float factor); - +void push_pull_fcurve_segment(struct FCurve *fcu, struct FCurveSegment *segment, float factor); /** * Get a 1D gauss kernel. Since the kernel is symmetrical, only calculates the positive side. * \param sigma: The shape of the gauss distribution. diff --git a/source/blender/editors/space_graph/graph_intern.h b/source/blender/editors/space_graph/graph_intern.h index e744a46346e..7639e3ffbdc 100644 --- a/source/blender/editors/space_graph/graph_intern.h +++ b/source/blender/editors/space_graph/graph_intern.h @@ -124,6 +124,7 @@ void GRAPH_OT_blend_to_ease(struct wmOperatorType *ot); void GRAPH_OT_match_slope(struct wmOperatorType *ot); void GRAPH_OT_shear(struct wmOperatorType *ot); void GRAPH_OT_scale_average(struct wmOperatorType *ot); +void GRAPH_OT_push_pull(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 3df2f1c3496..dc0187a39e3 100644 --- a/source/blender/editors/space_graph/graph_ops.cc +++ b/source/blender/editors/space_graph/graph_ops.cc @@ -475,6 +475,7 @@ void graphedit_operatortypes() WM_operatortype_append(GRAPH_OT_blend_to_ease); WM_operatortype_append(GRAPH_OT_match_slope); WM_operatortype_append(GRAPH_OT_blend_to_default); + WM_operatortype_append(GRAPH_OT_push_pull); WM_operatortype_append(GRAPH_OT_gaussian_smooth); WM_operatortype_append(GRAPH_OT_butterworth_smooth); WM_operatortype_append(GRAPH_OT_euler_filter); diff --git a/source/blender/editors/space_graph/graph_slider_ops.cc b/source/blender/editors/space_graph/graph_slider_ops.cc index 5dcafaa6113..86cacf584b6 100644 --- a/source/blender/editors/space_graph/graph_slider_ops.cc +++ b/source/blender/editors/space_graph/graph_slider_ops.cc @@ -2075,3 +2075,90 @@ void GRAPH_OT_butterworth_smooth(wmOperatorType *ot) 128); } /** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Push-Pull Operator + * \{ */ + +static void push_pull_graph_keys(bAnimContext *ac, const float factor) +{ + apply_fcu_segment_function(ac, factor, push_pull_fcurve_segment); +} + +static void push_pull_modal_update(bContext *C, wmOperator *op) +{ + tGraphSliderOp *gso = static_cast(op->customdata); + + common_draw_status_header(C, gso, "Push Pull Keys"); + + /* Reset keyframes to the state at invoke. */ + reset_bezts(gso); + const float factor = slider_factor_get_and_remember(op); + push_pull_graph_keys(&gso->ac, factor); + WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME | NA_EDITED, NULL); +} + +static int push_pull_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 = push_pull_modal_update; + gso->factor_prop = RNA_struct_find_property(op->ptr, "factor"); + ED_slider_factor_bounds_set(gso->slider, 0, 2); + ED_slider_factor_set(gso->slider, 1); + common_draw_status_header(C, gso, "Push Pull Keys"); + + return invoke_result; +} + +static int push_pull_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"); + + push_pull_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_push_pull(wmOperatorType *ot) +{ + /* Identifiers. */ + ot->name = "Push Pull Keyframes"; + ot->idname = "GRAPH_OT_push_pull"; + ot->description = "Exaggerate or minimize the value of the selected keys"; + + /* API callbacks. */ + ot->invoke = push_pull_invoke; + ot->modal = graph_slider_modal; + ot->exec = push_pull_exec; + ot->poll = graphop_editable_keyframes_poll; + + /* Flags. */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_BLOCKING | OPTYPE_GRAB_CURSOR_X; + + RNA_def_float_factor(ot->srna, + "factor", + 1.0f, + -FLT_MAX, + FLT_MAX, + "Factor", + "Control how far to push or pull the keys", + 0.0f, + 2.0f); +} +/** \} */ \ No newline at end of file