Anim: Time Offset Slider #110540

Merged
Christoph Lendenfeld merged 21 commits from ChrisLend/blender:time_offset_slider into main 2023-09-21 15:11:25 +02:00
6 changed files with 132 additions and 0 deletions

View File

@ -327,6 +327,7 @@ class GRAPH_MT_key_blending(Menu):
layout.operator("graph.push_pull", text="Push Pull")
layout.operator("graph.shear", text="Shear Keys")
layout.operator("graph.scale_average", text="Scale Average")
layout.operator("graph.time_offset", text="Time Offset")
class GRAPH_MT_key_smoothing(Menu):

View File

@ -910,6 +910,41 @@ void push_pull_fcurve_segment(FCurve *fcu, FCurveSegment *segment, const float f
/* ---------------- */
void time_offset_fcurve_segment(FCurve *fcu, FCurveSegment *segment, const float frame_offset)
{
/* Two bookend keys of the fcurve are needed to be able to cycle the values. */
const BezTriple *last_key = &fcu->bezt[fcu->totvert - 1];
const BezTriple *first_key = &fcu->bezt[0];
const float fcu_x_range = last_key->vec[1][0] - first_key->vec[1][0];
const float fcu_y_range = last_key->vec[1][1] - first_key->vec[1][1];
const float first_key_x = first_key->vec[1][0];
/* If we operate directly on the fcurve there will be a feedback loop
* so we need to capture the "y" values on an array to then apply them on a second loop. */
float *y_values = static_cast<float *>(
MEM_callocN(sizeof(float) * segment->length, "Time Offset Samples"));
for (int i = 0; i < segment->length; i++) {
/* This simulates the fcu curve moving in time. */
const float time = fcu->bezt[segment->start_index + i].vec[1][0] + frame_offset;
/* Need to normalize time to first_key to specify that as the wrapping point. */
const float wrapped_time = mod_f_positive(time - first_key_x, fcu_x_range) + first_key_x;
const float delta_y = fcu_y_range * floorf((time - first_key_x) / fcu_x_range);
const float key_y_value = evaluate_fcurve(fcu, wrapped_time) + delta_y;
y_values[i] = key_y_value;
}
for (int i = 0; i < segment->length; i++) {
BKE_fcurve_keyframe_move_value_with_handles(&fcu->bezt[segment->start_index + i], y_values[i]);
}
MEM_freeN(y_values);
}
/* ---------------- */
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

@ -476,6 +476,7 @@ void shear_fcurve_segment(struct FCurve *fcu,
*/
void blend_offset_fcurve_segment(FCurve *fcu, FCurveSegment *segment, float factor);
void blend_to_ease_fcurve_segment(FCurve *fcu, FCurveSegment *segment, float factor);
void time_offset_fcurve_segment(FCurve *fcu, FCurveSegment *segment, float frame_offset);
bool decimate_fcurve(bAnimListElem *ale, float remove_ratio, float error_sq_max);
bool match_slope_fcurve_segment(FCurve *fcu, FCurveSegment *segment, float factor);

View File

@ -125,6 +125,7 @@ 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_time_offset(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

@ -474,6 +474,7 @@ void graphedit_operatortypes()
WM_operatortype_append(GRAPH_OT_blend_offset);
WM_operatortype_append(GRAPH_OT_blend_to_ease);
WM_operatortype_append(GRAPH_OT_match_slope);
WM_operatortype_append(GRAPH_OT_time_offset);
WM_operatortype_append(GRAPH_OT_blend_to_default);
WM_operatortype_append(GRAPH_OT_push_pull);
WM_operatortype_append(GRAPH_OT_gaussian_smooth);

View File

@ -1289,6 +1289,99 @@ void GRAPH_OT_match_slope(wmOperatorType *ot)
1.0f);
}
/* -------------------------------------------------------------------- */
/** \name Time Offset
* \{ */
static void time_offset_graph_keys(bAnimContext *ac, const float factor)
{
apply_fcu_segment_function(ac, factor, time_offset_fcurve_segment);
}
static void time_offset_draw_status_header(bContext *C, tGraphSliderOp *gso)
{
common_draw_status_header(C, gso, "Time Offset Keys");
}
static void time_offset_modal_update(bContext *C, wmOperator *op)
{
tGraphSliderOp *gso = static_cast<tGraphSliderOp *>(op->customdata);
time_offset_draw_status_header(C, gso);
/* Reset keyframes to the state at invoke. */
reset_bezts(gso);
const float factor = slider_factor_get_and_remember(op);
time_offset_graph_keys(&gso->ac, factor);
WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME | NA_EDITED, NULL);
}
static int time_offset_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 = time_offset_modal_update;
gso->factor_prop = RNA_struct_find_property(op->ptr, "frame_offset");
time_offset_draw_status_header(C, gso);
ED_slider_factor_bounds_set(gso->slider, -10, 10);
ED_slider_factor_set(gso->slider, 0.0f);
ED_slider_mode_set(gso->slider, SLIDER_MODE_FLOAT);
ED_slider_unit_set(gso->slider, "Frames");
return invoke_result;
}
static int time_offset_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, "frame_offset");
time_offset_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_time_offset(wmOperatorType *ot)
{
/* Identifiers. */
ot->name = "Time Offset Keyframes";
ot->idname = "GRAPH_OT_time_offset";
ot->description = "Shifts the value of selected keys in time";
/* API callbacks. */
ot->invoke = time_offset_invoke;
ot->modal = graph_slider_modal;
ot->exec = time_offset_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,
"frame_offset",
0.0f,
-FLT_MAX,
FLT_MAX,
"Frame Offset",
"How far in frames to offset the animation",
-10.0f,
10.0f);
}
/** \} */
/* -------------------------------------------------------------------- */