From bbc6f1cf3348a8ba0944c67d171ef6e03a32784a Mon Sep 17 00:00:00 2001 From: Ares Deveaux Date: Sun, 2 Apr 2023 22:48:19 -0700 Subject: [PATCH 1/7] Added functions --- scripts/startup/bl_ui/space_graph.py | 1 + .../editors/animation/keyframes_general.c | 68 ++++++++++ .../editors/include/ED_keyframes_edit.h | 1 + .../editors/space_graph/graph_intern.h | 1 + .../blender/editors/space_graph/graph_ops.c | 1 + .../editors/space_graph/graph_slider_ops.c | 124 ++++++++++++++++++ 6 files changed, 196 insertions(+) diff --git a/scripts/startup/bl_ui/space_graph.py b/scripts/startup/bl_ui/space_graph.py index 999a9e3cef7..5b5e1464d57 100644 --- a/scripts/startup/bl_ui/space_graph.py +++ b/scripts/startup/bl_ui/space_graph.py @@ -330,6 +330,7 @@ class GRAPH_MT_slider(Menu): layout.operator("graph.blend_to_neighbor", text="Blend to Neighbor") layout.operator("graph.blend_to_default", text="Blend to Default Value") layout.operator("graph.ease", text="Ease") + layout.operator("graph.time_offset", text="Time Offset") layout.operator("graph.gaussian_smooth", text="Smooth") diff --git a/source/blender/editors/animation/keyframes_general.c b/source/blender/editors/animation/keyframes_general.c index f1848e4cc42..94bfcf2900a 100644 --- a/source/blender/editors/animation/keyframes_general.c +++ b/source/blender/editors/animation/keyframes_general.c @@ -491,6 +491,74 @@ void ease_fcurve_segment(FCurve *fcu, FCurveSegment *segment, const float factor /* ---------------- */ +void time_offset_fcurve_segment(FCurve *fcu, FCurveSegment *segment, const float factor) +{ + const BezTriple *left_key = fcurve_segment_start_get(fcu, segment->start_index); + const float left_x = left_key->vec[1][0]; + + 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_x; + + /* 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; + } + + /* The factor goes from 0 to 1, but for this tool it needs to go from -1 to 1. */ + const float long_factor = factor * 2 - 1; + + /* 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 float last_key_x = last_key->vec[1][0]; + const float last_key_y = last_key->vec[1][1]; + + const BezTriple *first_key = &fcu->bezt[0]; + const float first_key_x = first_key->vec[1][0]; + const float first_key_y = first_key->vec[1][1]; + + const int fcu_x_range = last_key_x - first_key_x; + float delta_y; + + /* 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[segment->length]; + + for (int i = segment->start_index; i < segment->start_index + segment->length; i++) { + + /* This simulates the fcu curve moving in time. */ + float time = fcu->bezt[i].vec[1][0] + fcu_x_range * long_factor; + + /* The values need to go back to the ones at the other end of the fcurve + * every time we get to the last or the first key. */ + if(time > last_key_x){ + int offset_frame = fcu->bezt[i].vec[1][0] - fcu_x_range; + time = offset_frame + fcu_x_range * long_factor; + delta_y = last_key_y - first_key_y; + } + else if(time < first_key_x){ + int offset_frame = fcu->bezt[i].vec[1][0] + fcu_x_range; + time = offset_frame + fcu_x_range * long_factor; + delta_y = first_key_y - last_key_y; + } + else{ + delta_y = 0; + } + + const float key_y_value = evaluate_fcurve(fcu, time) + delta_y; + const int index_from_zero = i-segment->start_index; + y_values[index_from_zero] = key_y_value; + } + + for (int i = segment->start_index; i < segment->start_index + segment->length; i++){ + const int index_from_zero = i-segment->start_index; + move_key(&fcu->bezt[i], y_values[index_from_zero]); + } +} + +/* ---------------- */ + 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.h b/source/blender/editors/include/ED_keyframes_edit.h index 69f103c6cba..224b4aee3eb 100644 --- a/source/blender/editors/include/ED_keyframes_edit.h +++ b/source/blender/editors/include/ED_keyframes_edit.h @@ -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 time_offset_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); /** diff --git a/source/blender/editors/space_graph/graph_intern.h b/source/blender/editors/space_graph/graph_intern.h index 7128f805a23..b7302247255 100644 --- a/source/blender/editors/space_graph/graph_intern.h +++ b/source/blender/editors/space_graph/graph_intern.h @@ -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_time_offset(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); diff --git a/source/blender/editors/space_graph/graph_ops.c b/source/blender/editors/space_graph/graph_ops.c index b64884da06c..c991f3c2fb3 100644 --- a/source/blender/editors/space_graph/graph_ops.c +++ b/source/blender/editors/space_graph/graph_ops.c @@ -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_time_offset); WM_operatortype_append(GRAPH_OT_blend_to_default); WM_operatortype_append(GRAPH_OT_gaussian_smooth); WM_operatortype_append(GRAPH_OT_euler_filter); diff --git a/source/blender/editors/space_graph/graph_slider_ops.c b/source/blender/editors/space_graph/graph_slider_ops.c index 184bd8329e8..c957358971f 100644 --- a/source/blender/editors/space_graph/graph_slider_ops.c +++ b/source/blender/editors/space_graph/graph_slider_ops.c @@ -1060,6 +1060,130 @@ void GRAPH_OT_ease(wmOperatorType *ot) 1.0f); } +/* -------------------------------------------------------------------- */ +/** \name Time Offset + * \{ */ + +static void time_offset_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) { + time_offset_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 time_offset_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_("Time Offset 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 time_offset_modal_update(bContext *C, wmOperator *op) +{ + tGraphSliderOp *gso = 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 = op->customdata; + gso->modal_update = time_offset_modal_update; + gso->factor_prop = RNA_struct_find_property(op->ptr, "factor"); + time_offset_draw_status_header(C, gso); + + 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, "factor"); + + 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; + + RNA_def_float_factor(ot->srna, + "factor", + 0.5f, + -FLT_MAX, + FLT_MAX, + "Curve Bend", + "Control the bend of the curve", + 0.0f, + 1.0f); +} + /** \} */ /* -------------------------------------------------------------------- */ /** \name Gauss Smooth Operator -- 2.30.2 From ac8d62f74295025f08144704b6658113faf1d1e3 Mon Sep 17 00:00:00 2001 From: Ares Deveaux Date: Mon, 3 Apr 2023 10:55:51 -0700 Subject: [PATCH 2/7] Fixed some formating --- .../editors/animation/keyframes_general.c | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/source/blender/editors/animation/keyframes_general.c b/source/blender/editors/animation/keyframes_general.c index 94bfcf2900a..b92b2478e65 100644 --- a/source/blender/editors/animation/keyframes_general.c +++ b/source/blender/editors/animation/keyframes_general.c @@ -521,38 +521,38 @@ void time_offset_fcurve_segment(FCurve *fcu, FCurveSegment *segment, const float const int fcu_x_range = last_key_x - first_key_x; float delta_y; - /* If we operate directly on the fcurve there will be a feedback loop + /* 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[segment->length]; - + for (int i = segment->start_index; i < segment->start_index + segment->length; i++) { - + /* This simulates the fcu curve moving in time. */ float time = fcu->bezt[i].vec[1][0] + fcu_x_range * long_factor; - /* The values need to go back to the ones at the other end of the fcurve + /* The values need to go back to the ones at the other end of the fcurve * every time we get to the last or the first key. */ - if(time > last_key_x){ + if (time > last_key_x) { int offset_frame = fcu->bezt[i].vec[1][0] - fcu_x_range; time = offset_frame + fcu_x_range * long_factor; delta_y = last_key_y - first_key_y; } - else if(time < first_key_x){ + else if (time < first_key_x) { int offset_frame = fcu->bezt[i].vec[1][0] + fcu_x_range; time = offset_frame + fcu_x_range * long_factor; delta_y = first_key_y - last_key_y; } - else{ + else { delta_y = 0; } const float key_y_value = evaluate_fcurve(fcu, time) + delta_y; - const int index_from_zero = i-segment->start_index; + const int index_from_zero = i - segment->start_index; y_values[index_from_zero] = key_y_value; } - for (int i = segment->start_index; i < segment->start_index + segment->length; i++){ - const int index_from_zero = i-segment->start_index; + for (int i = segment->start_index; i < segment->start_index + segment->length; i++) { + const int index_from_zero = i - segment->start_index; move_key(&fcu->bezt[i], y_values[index_from_zero]); } } -- 2.30.2 From 688a03971103f8e75ec4227687a8e2b92c3fbc04 Mon Sep 17 00:00:00 2001 From: Ares Deveaux Date: Mon, 10 Apr 2023 16:24:53 -0700 Subject: [PATCH 3/7] simplified the function a bit --- .../editors/animation/keyframes_general.c | 28 ++++--------------- 1 file changed, 5 insertions(+), 23 deletions(-) diff --git a/source/blender/editors/animation/keyframes_general.c b/source/blender/editors/animation/keyframes_general.c index b92b2478e65..98efc86190e 100644 --- a/source/blender/editors/animation/keyframes_general.c +++ b/source/blender/editors/animation/keyframes_general.c @@ -493,32 +493,14 @@ void ease_fcurve_segment(FCurve *fcu, FCurveSegment *segment, const float factor void time_offset_fcurve_segment(FCurve *fcu, FCurveSegment *segment, const float factor) { - const BezTriple *left_key = fcurve_segment_start_get(fcu, segment->start_index); - const float left_x = left_key->vec[1][0]; - - 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_x; - - /* 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; - } - /* The factor goes from 0 to 1, but for this tool it needs to go from -1 to 1. */ const float long_factor = factor * 2 - 1; /* 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 float last_key_x = last_key->vec[1][0]; - const float last_key_y = last_key->vec[1][1]; - const BezTriple *first_key = &fcu->bezt[0]; - const float first_key_x = first_key->vec[1][0]; - const float first_key_y = first_key->vec[1][1]; - const int fcu_x_range = last_key_x - first_key_x; + const int fcu_x_range = last_key->vec[1][0] - first_key->vec[1][0]; float delta_y; /* If we operate directly on the fcurve there will be a feedback loop @@ -532,15 +514,15 @@ void time_offset_fcurve_segment(FCurve *fcu, FCurveSegment *segment, const float /* The values need to go back to the ones at the other end of the fcurve * every time we get to the last or the first key. */ - if (time > last_key_x) { + if (time > last_key->vec[1][0]) { int offset_frame = fcu->bezt[i].vec[1][0] - fcu_x_range; time = offset_frame + fcu_x_range * long_factor; - delta_y = last_key_y - first_key_y; + delta_y = last_key->vec[1][1] - first_key->vec[1][1]; } - else if (time < first_key_x) { + else if (time < first_key->vec[1][0]) { int offset_frame = fcu->bezt[i].vec[1][0] + fcu_x_range; time = offset_frame + fcu_x_range * long_factor; - delta_y = first_key_y - last_key_y; + delta_y = first_key->vec[1][1] - last_key->vec[1][1]; } else { delta_y = 0; -- 2.30.2 From 264301a1b27c77e3fef0fa8f99a5322ed2d74fab Mon Sep 17 00:00:00 2001 From: Ares Deveaux Date: Fri, 14 Apr 2023 12:32:12 -0700 Subject: [PATCH 4/7] fix minor error in comment --- source/blender/editors/animation/keyframes_general.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/blender/editors/animation/keyframes_general.c b/source/blender/editors/animation/keyframes_general.c index 98efc86190e..3dae82783fc 100644 --- a/source/blender/editors/animation/keyframes_general.c +++ b/source/blender/editors/animation/keyframes_general.c @@ -504,7 +504,7 @@ void time_offset_fcurve_segment(FCurve *fcu, FCurveSegment *segment, const float float delta_y; /* 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*/ + * so we need to capture the "y" values on an array to then apply them on a second loop. */ float y_values[segment->length]; for (int i = segment->start_index; i < segment->start_index + segment->length; i++) { -- 2.30.2 From 8ec0fa3976a7c700700df7b185065b58b31a6991 Mon Sep 17 00:00:00 2001 From: Ares Deveaux Date: Mon, 17 Apr 2023 19:58:48 -0700 Subject: [PATCH 5/7] added lines to avoid memory leaks --- source/blender/editors/animation/keyframes_general.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/source/blender/editors/animation/keyframes_general.c b/source/blender/editors/animation/keyframes_general.c index 3dae82783fc..8c4b2fc6ba1 100644 --- a/source/blender/editors/animation/keyframes_general.c +++ b/source/blender/editors/animation/keyframes_general.c @@ -505,7 +505,8 @@ void time_offset_fcurve_segment(FCurve *fcu, FCurveSegment *segment, const float /* 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[segment->length]; + float *y_values = MEM_callocN(sizeof(float) * segment->length, "Time Offset Samples"); + y_values[segment->length]; for (int i = segment->start_index; i < segment->start_index + segment->length; i++) { @@ -537,6 +538,7 @@ void time_offset_fcurve_segment(FCurve *fcu, FCurveSegment *segment, const float const int index_from_zero = i - segment->start_index; move_key(&fcu->bezt[i], y_values[index_from_zero]); } + MEM_freeN(y_values); } /* ---------------- */ -- 2.30.2 From f4750d0e484fca6ff3938bddc060ed905aefd493 Mon Sep 17 00:00:00 2001 From: Ares Deveaux Date: Mon, 17 Apr 2023 22:17:01 -0700 Subject: [PATCH 6/7] converted the slider to bidirectional --- source/blender/editors/animation/keyframes_general.c | 9 +++------ source/blender/editors/space_graph/graph_slider_ops.c | 2 ++ 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/source/blender/editors/animation/keyframes_general.c b/source/blender/editors/animation/keyframes_general.c index 8c4b2fc6ba1..7ee69d2b36a 100644 --- a/source/blender/editors/animation/keyframes_general.c +++ b/source/blender/editors/animation/keyframes_general.c @@ -493,9 +493,6 @@ void ease_fcurve_segment(FCurve *fcu, FCurveSegment *segment, const float factor void time_offset_fcurve_segment(FCurve *fcu, FCurveSegment *segment, const float factor) { - /* The factor goes from 0 to 1, but for this tool it needs to go from -1 to 1. */ - const float long_factor = factor * 2 - 1; - /* 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]; @@ -511,18 +508,18 @@ void time_offset_fcurve_segment(FCurve *fcu, FCurveSegment *segment, const float for (int i = segment->start_index; i < segment->start_index + segment->length; i++) { /* This simulates the fcu curve moving in time. */ - float time = fcu->bezt[i].vec[1][0] + fcu_x_range * long_factor; + float time = fcu->bezt[i].vec[1][0] + fcu_x_range * factor; /* The values need to go back to the ones at the other end of the fcurve * every time we get to the last or the first key. */ if (time > last_key->vec[1][0]) { int offset_frame = fcu->bezt[i].vec[1][0] - fcu_x_range; - time = offset_frame + fcu_x_range * long_factor; + time = offset_frame + fcu_x_range * factor; delta_y = last_key->vec[1][1] - first_key->vec[1][1]; } else if (time < first_key->vec[1][0]) { int offset_frame = fcu->bezt[i].vec[1][0] + fcu_x_range; - time = offset_frame + fcu_x_range * long_factor; + time = offset_frame + fcu_x_range * factor; delta_y = first_key->vec[1][1] - last_key->vec[1][1]; } else { diff --git a/source/blender/editors/space_graph/graph_slider_ops.c b/source/blender/editors/space_graph/graph_slider_ops.c index c957358971f..27c9e108265 100644 --- a/source/blender/editors/space_graph/graph_slider_ops.c +++ b/source/blender/editors/space_graph/graph_slider_ops.c @@ -1134,6 +1134,8 @@ static int time_offset_invoke(bContext *C, wmOperator *op, const wmEvent *event) gso->modal_update = time_offset_modal_update; gso->factor_prop = RNA_struct_find_property(op->ptr, "factor"); time_offset_draw_status_header(C, gso); + ED_slider_is_bidirectional_set(gso->slider, true); + ED_slider_factor_set(gso->slider, 0.0f); return invoke_result; } -- 2.30.2 From 65b92186e999a348497b96b72eba8bbd0de145cf Mon Sep 17 00:00:00 2001 From: Ares Deveaux Date: Thu, 20 Apr 2023 19:56:50 -0700 Subject: [PATCH 7/7] Used Christoph's lines of code --- .../editors/animation/keyframes_general.c | 33 ++++++++----------- .../editors/space_graph/graph_slider_ops.c | 4 +-- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/source/blender/editors/animation/keyframes_general.c b/source/blender/editors/animation/keyframes_general.c index 7ee69d2b36a..ef8525ef563 100644 --- a/source/blender/editors/animation/keyframes_general.c +++ b/source/blender/editors/animation/keyframes_general.c @@ -498,35 +498,30 @@ void time_offset_fcurve_segment(FCurve *fcu, FCurveSegment *segment, const float const BezTriple *first_key = &fcu->bezt[0]; const int fcu_x_range = last_key->vec[1][0] - first_key->vec[1][0]; - float delta_y; + 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 = MEM_callocN(sizeof(float) * segment->length, "Time Offset Samples"); - y_values[segment->length]; for (int i = segment->start_index; i < segment->start_index + segment->length; i++) { /* This simulates the fcu curve moving in time. */ - float time = fcu->bezt[i].vec[1][0] + fcu_x_range * factor; - - /* The values need to go back to the ones at the other end of the fcurve - * every time we get to the last or the first key. */ - if (time > last_key->vec[1][0]) { - int offset_frame = fcu->bezt[i].vec[1][0] - fcu_x_range; - time = offset_frame + fcu_x_range * factor; - delta_y = last_key->vec[1][1] - first_key->vec[1][1]; - } - else if (time < first_key->vec[1][0]) { - int offset_frame = fcu->bezt[i].vec[1][0] + fcu_x_range; - time = offset_frame + fcu_x_range * factor; - delta_y = first_key->vec[1][1] - last_key->vec[1][1]; - } - else { - delta_y = 0; + const float time = fcu->bezt[i].vec[1][0] + fcu_x_range * factor; + /* Need to normalize time to first_key to specify that as the wrapping point. */ + float wrapped_time = fmod(time - first_key_x, fcu_x_range) + first_key_x; + float delta_y = fcu_y_range * (int)((time - first_key_x) / fcu_x_range); + if (time - first_key_x < 0) { + /* Offset negative values since the modulo at fmod(-0.1, 1) would produce -0.1 instead of + * 0.9, as we'd need it to. */ + wrapped_time += fcu_x_range; + /* Similarly, (int)-0.1 produces 0 while we need it to be -1. */ + delta_y -= fcu_y_range; } - const float key_y_value = evaluate_fcurve(fcu, time) + delta_y; + const float key_y_value = evaluate_fcurve(fcu, wrapped_time) + delta_y; const int index_from_zero = i - segment->start_index; y_values[index_from_zero] = key_y_value; } diff --git a/source/blender/editors/space_graph/graph_slider_ops.c b/source/blender/editors/space_graph/graph_slider_ops.c index 27c9e108265..970f8e5900e 100644 --- a/source/blender/editors/space_graph/graph_slider_ops.c +++ b/source/blender/editors/space_graph/graph_slider_ops.c @@ -1177,12 +1177,12 @@ void GRAPH_OT_time_offset(wmOperatorType *ot) RNA_def_float_factor(ot->srna, "factor", - 0.5f, + 0.0f, -FLT_MAX, FLT_MAX, "Curve Bend", "Control the bend of the curve", - 0.0f, + -1.0f, 1.0f); } -- 2.30.2