Animation: Scale Average slider for Graph Editor

This patch has been originally authored by Ares Deveaux #106526

Scale the selected key segments to their average
Unlike just scaling using the transform tools, this scales to the average of each individual F-Curve segment.

Co-authored-by: Ares Deveaux <aresdevo@gmail.com>
Pull Request: https://projects.blender.org/blender/blender/pulls/111744
This commit is contained in:
Christoph Lendenfeld
2023-09-08 13:07:30 +02:00
committed by Christoph Lendenfeld
parent e39cc78313
commit 1a73039499
6 changed files with 111 additions and 0 deletions

View File

@@ -325,6 +325,7 @@ class GRAPH_MT_key_blending(Menu):
layout.operator("graph.blend_to_ease", text="Blend to Ease")
layout.operator("graph.match_slope", text="Match Slope")
layout.operator("graph.shear", text="Shear Keys")
layout.operator("graph.scale_average", text="Scale Average")
class GRAPH_MT_key_smoothing(Menu):

View File

@@ -390,6 +390,25 @@ void blend_to_default_fcurve(PointerRNA *id_ptr, FCurve *fcu, const float factor
/* ---------------- */
void scale_average_fcurve_segment(FCurve *fcu, FCurveSegment *segment, const float factor)
{
float y = 0;
/* Find first the average of the y values to then use it in the final calculation. */
for (int i = segment->start_index; i < segment->start_index + segment->length; i++) {
y += fcu->bezt[i].vec[1][1];
}
const float y_average = y / segment->length;
for (int i = segment->start_index; i < segment->start_index + segment->length; i++) {
const float key_y_value = interpf(y_average, fcu->bezt[i].vec[1][1], 1 - factor);
BKE_fcurve_keyframe_move_value_with_handles(&fcu->bezt[i], key_y_value);
}
}
/* ---------------- */
struct ButterworthCoefficients {
double *A, *d1, *d2;
int filter_order;

View File

@@ -430,6 +430,7 @@ ListBase find_fcurve_segments(FCurve *fcu);
void clean_fcurve(bAnimContext *ac, bAnimListElem *ale, float thresh, bool cleardefault);
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);
/**
* Get a 1D gauss kernel. Since the kernel is symmetrical, only calculates the positive side.

View File

@@ -123,6 +123,7 @@ void GRAPH_OT_blend_offset(struct wmOperatorType *ot);
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_decimate(struct wmOperatorType *ot);
void GRAPH_OT_blend_to_default(struct wmOperatorType *ot);
void GRAPH_OT_butterworth_smooth(struct wmOperatorType *ot);

View File

@@ -470,6 +470,7 @@ void graphedit_operatortypes()
WM_operatortype_append(GRAPH_OT_breakdown);
WM_operatortype_append(GRAPH_OT_ease);
WM_operatortype_append(GRAPH_OT_shear);
WM_operatortype_append(GRAPH_OT_scale_average);
WM_operatortype_append(GRAPH_OT_blend_offset);
WM_operatortype_append(GRAPH_OT_blend_to_ease);
WM_operatortype_append(GRAPH_OT_match_slope);

View File

@@ -1457,6 +1457,94 @@ void GRAPH_OT_shear(wmOperatorType *ot)
"Which end of the segment to use as a reference to shear from");
}
/* -------------------------------------------------------------------- */
/** \name Scale Average Operator
* \{ */
static void scale_average_graph_keys(bAnimContext *ac, const float factor)
{
apply_fcu_segment_function(ac, factor, scale_average_fcurve_segment);
}
static void scale_average_modal_update(bContext *C, wmOperator *op)
{
tGraphSliderOp *gso = static_cast<tGraphSliderOp *>(op->customdata);
common_draw_status_header(C, gso, "Scale to Average");
/* Reset keyframes to the state at invoke. */
reset_bezts(gso);
const float factor = slider_factor_get_and_remember(op);
scale_average_graph_keys(&gso->ac, factor);
WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME | NA_EDITED, NULL);
}
static int scale_average_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 = scale_average_modal_update;
gso->factor_prop = RNA_struct_find_property(op->ptr, "factor");
common_draw_status_header(C, gso, "Scale to Average");
ED_slider_factor_bounds_set(gso->slider, 0, 2);
ED_slider_factor_set(gso->slider, 1.0f);
return invoke_result;
}
static int scale_average_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");
scale_average_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_scale_average(wmOperatorType *ot)
{
/* Identifiers. */
ot->name = "Scale Average Keyframes";
ot->idname = "GRAPH_OT_scale_average";
ot->description =
"Increase or decrease the value of selected keys \n\
in relationship to their average";
/* API callbacks. */
ot->invoke = scale_average_invoke;
ot->modal = graph_slider_modal;
ot->exec = scale_average_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,
"Scale Factor",
"The scale factor applied to the curve segments",
0.0f,
2.0f);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Gauss Smooth Operator