Animation: blend to ease slider #106519

Closed
AresDeveaux wants to merge 11 commits from AresDeveaux/blender:blend_to_ease_slider into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
6 changed files with 200 additions and 0 deletions

View File

@ -329,6 +329,7 @@ class GRAPH_MT_slider(Menu):
layout.operator("graph.breakdown", text="Breakdown")
layout.operator("graph.blend_to_neighbor", text="Blend to Neighbor")
layout.operator("graph.blend_to_default", text="Blend to Default Value")
layout.operator("graph.blend_to_ease", text="Blend to Ease")
layout.operator("graph.ease", text="Ease")
layout.operator("graph.gaussian_smooth", text="Smooth")

View File

@ -491,6 +491,75 @@ void ease_fcurve_segment(FCurve *fcu, FCurveSegment *segment, const float factor
/* ---------------- */
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) /

this could be renamed to y or curve_y
after all it's only a single point and not a whole curve

this could be renamed to `y` or `curve_y` after all it's only a single point and not a whole curve
(pow((x - xshift), slope) + pow((width - (x - xshift)), slope)) +
yshift;
/* The curve has some noise 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;

you can also set the slider itself to go from -1/1 using ED_slider_is_bidirectional_set

you can also set the slider itself to go from -1/1 using `ED_slider_is_bidirectional_set`
float y_delta;
/* 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;
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;
move_key(&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

@ -437,6 +437,7 @@ void smooth_fcurve_segment(struct FCurve *fcu,
int kernel_size,
double *kernel);
void ease_fcurve_segment(struct FCurve *fcu, struct FCurveSegment *segment, float factor);
void blend_to_ease_fcurve_segment(struct FCurve *fcu, struct FCurveSegment *segment, float factor);
bool decimate_fcurve(struct bAnimListElem *ale, float remove_ratio, float error_sq_max);
void blend_to_default_fcurve(struct PointerRNA *id_ptr, struct FCurve *fcu, float factor);
/**

View File

@ -114,6 +114,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_to_ease(struct wmOperatorType *ot);
void GRAPH_OT_decimate(struct wmOperatorType *ot);
void GRAPH_OT_blend_to_default(struct wmOperatorType *ot);
void GRAPH_OT_gaussian_smooth(struct wmOperatorType *ot);

View File

@ -463,6 +463,7 @@ void graphedit_operatortypes(void)
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_to_ease);
WM_operatortype_append(GRAPH_OT_blend_to_default);
WM_operatortype_append(GRAPH_OT_gaussian_smooth);
WM_operatortype_append(GRAPH_OT_euler_filter);

View File

@ -1060,6 +1060,133 @@ void GRAPH_OT_ease(wmOperatorType *ot)
1.0f);
}
/* -------------------------------------------------------------------- */
/** \name Blend to Ease Operator
* \{ */
static void blend_to_ease_graph_keys(bAnimContext *ac, const float factor)
{
ListBase anim_data = {NULL, NULL};
ANIM_animdata_filter(ac, &anim_data, OPERATOR_DATA_FILTER, ac->data, ac->datatype);
LISTBASE_FOREACH (bAnimListElem *, ale, &anim_data) {
FCurve *fcu = (FCurve *)ale->key_data;
ListBase segments = find_fcurve_segments(fcu);
LISTBASE_FOREACH (FCurveSegment *, segment, &segments) {
blend_to_ease_fcurve_segment(fcu, segment, factor);
}
ale->update |= ANIM_UPDATE_DEFAULT;
BLI_freelistN(&segments);
}
ANIM_animdata_update(ac, &anim_data);
ANIM_animdata_freelist(&anim_data);
}
static void blend_to_ease_draw_status_header(bContext *C, tGraphSliderOp *gso)
{
char status_str[UI_MAX_DRAW_STR];
char mode_str[32];
char slider_string[UI_MAX_DRAW_STR];
ED_slider_status_string_get(gso->slider, slider_string, UI_MAX_DRAW_STR);
strcpy(mode_str, TIP_("Blend to Ease Keys"));
if (hasNumInput(&gso->num)) {
char str_ofs[NUM_STR_REP_LEN];
outputNumInput(&gso->num, str_ofs, &gso->scene->unit);
BLI_snprintf(status_str, sizeof(status_str), "%s: %s", mode_str, str_ofs);
}
else {
BLI_snprintf(status_str, sizeof(status_str), "%s: %s", mode_str, slider_string);
}
ED_workspace_status_text(C, status_str);
}
static void blend_to_ease_modal_update(bContext *C, wmOperator *op)
{
tGraphSliderOp *gso = 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 = 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);
ED_slider_is_bidirectional_set(gso->slider, true);
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