Anim: Graph Editor Push/Pull operator #112388

Merged
Christoph Lendenfeld merged 8 commits from ChrisLend/blender:ge_push_pull into main 2023-09-21 12:46:34 +02:00
6 changed files with 120 additions and 1 deletions

View File

@ -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")

View File

@ -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. */
nathanvegdahl marked this conversation as resolved Outdated

Very minor typo: a double space between "the" and "values".

Very minor typo: a double space between "the" and "values".
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);

View File

@ -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.

View File

@ -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);

View File

@ -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);

View File

@ -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<tGraphSliderOp *>(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<tGraphSliderOp *>(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";
nathanvegdahl marked this conversation as resolved Outdated

Spelling: exaggerates.

Also keep the verb form consistent: either "Exaggerates or minimizes" or "Exaggerate or minimize". I think the latter is standard in Blender?

Spelling: exaggerates. Also keep the verb form consistent: either "Exaggerates or minimizes" or "Exaggerate or minimize". I think the latter is standard in Blender?

Good catch, thanks!

Good catch, thanks!
/* 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);
}
/** \} */