[Bf-blender-cvs] [76a68649c1c] master: Animation: Graph Editor Ease operator

Christoph Lendenfeld noreply at git.blender.org
Thu Jan 5 10:11:32 CET 2023


Commit: 76a68649c1c1f3db28b069397c71daf10f1c4ea4
Author: Christoph Lendenfeld
Date:   Thu Jan 5 10:11:24 2023 +0100
Branches: master
https://developer.blender.org/rB76a68649c1c1f3db28b069397c71daf10f1c4ea4

Animation: Graph Editor Ease operator

Added a new operator that aligns selected keys on an exponential curve

Revied by Reviewed by: Sybren A. Stüvel
Differential Revision: https://developer.blender.org/D9479
Ref: D9479

===================================================================

M	release/scripts/startup/bl_ui/space_graph.py
M	source/blender/editors/animation/keyframes_general.c
M	source/blender/editors/include/ED_keyframes_edit.h
M	source/blender/editors/space_graph/graph_intern.h
M	source/blender/editors/space_graph/graph_ops.c
M	source/blender/editors/space_graph/graph_slider_ops.c

===================================================================

diff --git a/release/scripts/startup/bl_ui/space_graph.py b/release/scripts/startup/bl_ui/space_graph.py
index a5ad13ad5d8..b2c8822a75d 100644
--- a/release/scripts/startup/bl_ui/space_graph.py
+++ b/release/scripts/startup/bl_ui/space_graph.py
@@ -332,6 +332,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.ease", text="Ease")
 
 
 class GRAPH_MT_view_pie(Menu):
diff --git a/source/blender/editors/animation/keyframes_general.c b/source/blender/editors/animation/keyframes_general.c
index fbe65d6917e..f6b09e60e3c 100644
--- a/source/blender/editors/animation/keyframes_general.c
+++ b/source/blender/editors/animation/keyframes_general.c
@@ -377,6 +377,40 @@ void blend_to_default_fcurve(PointerRNA *id_ptr, FCurve *fcu, const float factor
 
 /* ---------------- */
 
+void ease_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 float left_y = left_key.vec[1][1];
+
+  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;
+  const float key_y_range = right_key.vec[1][1] - left_y;
+
+  /* In order to have a curve that favors the right key, the curve needs to be mirrored in x and y.
+   * Having an exponent that is a fraction of 1 would produce a similar but inferior result. */
+  const bool inverted = factor > 0.5;
+  const float exponent = 1 + fabs(factor * 2 - 1) * 4;
+
+  for (int i = segment->start_index; i < segment->start_index + segment->length; i++) {
+    /* For easy calculation of the curve, the  values are normalized. */
+    const float normalized_x = (fcu->bezt[i].vec[1][0] - left_x) / key_x_range;
+
+    float normalized_y = 0;
+    if (inverted) {
+      normalized_y = 1 - pow(1 - normalized_x, exponent);
+    }
+    else {
+      normalized_y = pow(normalized_x, exponent);
+    }
+
+    fcu->bezt[i].vec[1][1] = left_y + normalized_y * key_y_range;
+  }
+}
+
+/* ---------------- */
+
 void breakdown_fcurve_segment(FCurve *fcu, FCurveSegment *segment, const float factor)
 {
   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 e5bcdcdd282..f027a46b470 100644
--- a/source/blender/editors/include/ED_keyframes_edit.h
+++ b/source/blender/editors/include/ED_keyframes_edit.h
@@ -411,6 +411,7 @@ void blend_to_neighbor_fcurve_segment(struct FCurve *fcu,
                                       struct FCurveSegment *segment,
                                       float factor);
 void breakdown_fcurve_segment(struct FCurve *fcu, struct FCurveSegment *segment, float factor);
+void 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);
 /**
diff --git a/source/blender/editors/space_graph/graph_intern.h b/source/blender/editors/space_graph/graph_intern.h
index e6b4b9827d2..a685216db31 100644
--- a/source/blender/editors/space_graph/graph_intern.h
+++ b/source/blender/editors/space_graph/graph_intern.h
@@ -113,6 +113,7 @@ void GRAPH_OT_delete(struct wmOperatorType *ot);
 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_decimate(struct wmOperatorType *ot);
 void GRAPH_OT_blend_to_default(struct wmOperatorType *ot);
 void GRAPH_OT_sample(struct wmOperatorType *ot);
diff --git a/source/blender/editors/space_graph/graph_ops.c b/source/blender/editors/space_graph/graph_ops.c
index 8deea21318c..b178a3d3430 100644
--- a/source/blender/editors/space_graph/graph_ops.c
+++ b/source/blender/editors/space_graph/graph_ops.c
@@ -462,6 +462,7 @@ void graphedit_operatortypes(void)
   WM_operatortype_append(GRAPH_OT_decimate);
   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_default);
   WM_operatortype_append(GRAPH_OT_euler_filter);
   WM_operatortype_append(GRAPH_OT_delete);
diff --git a/source/blender/editors/space_graph/graph_slider_ops.c b/source/blender/editors/space_graph/graph_slider_ops.c
index 62aecf930d3..1bce22959ee 100644
--- a/source/blender/editors/space_graph/graph_slider_ops.c
+++ b/source/blender/editors/space_graph/graph_slider_ops.c
@@ -927,3 +927,129 @@ void GRAPH_OT_blend_to_default(wmOperatorType *ot)
                        1.0f);
 }
 /** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Ease Operator
+ * \{ */
+
+static void 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) {
+      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 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_("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 ease_modal_update(bContext *C, wmOperator *op)
+{
+  tGraphSliderOp *gso = op->customdata;
+
+  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);
+  ease_graph_keys(&gso->ac, factor);
+  WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME | NA_EDITED, NULL);
+}
+
+static int 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 = ease_modal_update;
+  gso->factor_prop = RNA_struct_find_property(op->ptr, "factor");
+  ease_draw_status_header(C, gso);
+
+  return invoke_result;
+}
+
+static int 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");
+
+  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_ease(wmOperatorType *ot)
+{
+  /* Identifiers. */
+  ot->name = "Ease Keyframes";
+  ot->idname = "GRAPH_OT_ease";
+  ot->description = "Align keyframes on a ease-in or ease-out curve";
+
+  /* API callbacks. */
+  ot->invoke = ease_invoke;
+  ot->modal = graph_slider_modal;
+  ot->exec = ease_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);
+}
+
+/** \} */



More information about the Bf-blender-cvs mailing list