From 37b93b5df85c9e7acac989c86cf658bf8a0bc1e5 Mon Sep 17 00:00:00 2001 From: Christoph Lendenfeld Date: Fri, 18 Mar 2022 20:07:16 +0100 Subject: Animation: Blend To Default Implementation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a new operator to the Graph Editor that blends selected keyframes to their default value. The operator can be accessed from Key>Slider Operators>Blend To Default Value Reviewed by: Sybren A. Stüvel Differential Revision: https://developer.blender.org/D9376 Ref: D9367 --- release/scripts/startup/bl_ui/space_graph.py | 1 + .../blender/editors/animation/keyframes_general.c | 62 ++++++++++ source/blender/editors/include/ED_keyframes_edit.h | 2 + source/blender/editors/space_graph/graph_intern.h | 1 + source/blender/editors/space_graph/graph_ops.c | 1 + .../blender/editors/space_graph/graph_slider_ops.c | 128 +++++++++++++++++++++ 6 files changed, 195 insertions(+) diff --git a/release/scripts/startup/bl_ui/space_graph.py b/release/scripts/startup/bl_ui/space_graph.py index 6f9ef12c3b7..86d9771b638 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") 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 24ba62fc0f7..aec2b0f769a 100644 --- a/source/blender/editors/animation/keyframes_general.c +++ b/source/blender/editors/animation/keyframes_general.c @@ -382,6 +382,68 @@ void blend_to_neighbor_fcurve_segment(FCurve *fcu, FCurveSegment *segment, const /* ---------------- */ +float get_default_rna_value(FCurve *fcu, PropertyRNA *prop, PointerRNA *ptr) +{ + const int len = RNA_property_array_length(ptr, prop); + + float default_value = 0; + /* Find the default value of that property. */ + switch (RNA_property_type(prop)) { + case PROP_BOOLEAN: + if (len) { + default_value = RNA_property_boolean_get_default_index(ptr, prop, fcu->array_index); + } + else { + default_value = RNA_property_boolean_get_default(ptr, prop); + } + break; + case PROP_INT: + if (len) { + default_value = RNA_property_int_get_default_index(ptr, prop, fcu->array_index); + } + else { + default_value = RNA_property_int_get_default(ptr, prop); + } + break; + case PROP_FLOAT: + if (len) { + default_value = RNA_property_float_get_default_index(ptr, prop, fcu->array_index); + } + else { + default_value = RNA_property_float_get_default(ptr, prop); + } + break; + + default: + break; + } + return default_value; +} + +/* This function blends the selected keyframes to the default value of the property the fcurve + * drives. */ +void blend_to_default_fcurve(PointerRNA *id_ptr, FCurve *fcu, const float factor) +{ + PointerRNA ptr; + PropertyRNA *prop; + + /* Check if path is valid. */ + if (!RNA_path_resolve_property(id_ptr, fcu->rna_path, &ptr, &prop)) { + return; + } + + const float default_value = get_default_rna_value(fcu, prop, &ptr); + + /* Blend selected keys to default */ + for (int i = 0; i < fcu->totvert; i++) { + if (fcu->bezt[i].f2 & SELECT) { + fcu->bezt[i].vec[1][1] = interpf(default_value, fcu->bezt[i].vec[1][1], factor); + } + } +} + +/* ---------------- */ + 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 90e8dbad272..3a0bb9738ae 100644 --- a/source/blender/editors/include/ED_keyframes_edit.h +++ b/source/blender/editors/include/ED_keyframes_edit.h @@ -382,6 +382,7 @@ void delete_fcurve_key(struct FCurve *fcu, int index, bool do_recalc); bool delete_fcurve_keys(struct FCurve *fcu); void clear_fcurve_keys(struct FCurve *fcu); void duplicate_fcurve_keys(struct FCurve *fcu); +float get_default_rna_value(struct FCurve *fcu, struct PropertyRNA *prop, struct PointerRNA *ptr); typedef struct FCurveSegment { struct FCurveSegment *next, *prev; @@ -404,6 +405,7 @@ void blend_to_neighbor_fcurve_segment(struct FCurve *fcu, float factor); void breakdown_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); /** * Use a weighted moving-means method to reduce intensity of fluctuations. */ diff --git a/source/blender/editors/space_graph/graph_intern.h b/source/blender/editors/space_graph/graph_intern.h index fe8005892cf..2eb64e5b115 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_clean(struct wmOperatorType *ot); void GRAPH_OT_blend_to_neighbor(struct wmOperatorType *ot); void GRAPH_OT_breakdown(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); void GRAPH_OT_bake(struct wmOperatorType *ot); void GRAPH_OT_unbake(struct wmOperatorType *ot); diff --git a/source/blender/editors/space_graph/graph_ops.c b/source/blender/editors/space_graph/graph_ops.c index b00e069470d..128925d4591 100644 --- a/source/blender/editors/space_graph/graph_ops.c +++ b/source/blender/editors/space_graph/graph_ops.c @@ -457,6 +457,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_blend_to_default); WM_operatortype_append(GRAPH_OT_euler_filter); WM_operatortype_append(GRAPH_OT_delete); WM_operatortype_append(GRAPH_OT_duplicate); diff --git a/source/blender/editors/space_graph/graph_slider_ops.c b/source/blender/editors/space_graph/graph_slider_ops.c index 1a3355b0139..6166ad128de 100644 --- a/source/blender/editors/space_graph/graph_slider_ops.c +++ b/source/blender/editors/space_graph/graph_slider_ops.c @@ -802,3 +802,131 @@ void GRAPH_OT_breakdown(wmOperatorType *ot) } /** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Blend To Default Value Operator + * \{ */ + +static void blend_to_default_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; + + /* Check if the curves actually have any points. */ + if (fcu == NULL || fcu->bezt == NULL || fcu->totvert == 0) { + continue; + } + + PointerRNA id_ptr; + RNA_id_pointer_create(ale->id, &id_ptr); + + blend_to_default_fcurve(&id_ptr, fcu, factor); + ale->update |= ANIM_UPDATE_DEFAULT; + } + + ANIM_animdata_update(ac, &anim_data); + ANIM_animdata_freelist(&anim_data); +} + +static void blend_to_default_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 Default Value")); + + 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_default_modal_update(bContext *C, wmOperator *op) +{ + tGraphSliderOp *gso = op->customdata; + + blend_to_default_draw_status_header(C, gso); + + /* Set notifier that keyframes have changed. */ + reset_bezts(gso); + const float factor = ED_slider_factor_get(gso->slider); + RNA_property_float_set(op->ptr, gso->factor_prop, factor); + blend_to_default_graph_keys(&gso->ac, factor); + WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME | NA_EDITED, NULL); +} + +static int blend_to_default_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_default_modal_update; + gso->factor_prop = RNA_struct_find_property(op->ptr, "factor"); + blend_to_default_draw_status_header(C, gso); + + return invoke_result; +} + +static int blend_to_default_exec(bContext *C, wmOperator *op) +{ + bAnimContext ac; + + if (ANIM_animdata_get_context(C, &ac) == 0) { + return OPERATOR_CANCELLED; + } + + const float factor = RNA_float_get(op->ptr, "factor"); + + blend_to_default_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_default(wmOperatorType *ot) +{ + /* Identifiers. */ + ot->name = "Blend To Default Value"; + ot->idname = "GRAPH_OT_blend_to_default"; + ot->description = "Blend selected keys to their default value from their current position"; + + /* API callbacks. */ + ot->invoke = blend_to_default_invoke; + ot->modal = graph_slider_modal; + ot->exec = blend_to_default_exec; + ot->poll = graphop_editable_keyframes_poll; + + /* Flags. */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + RNA_def_float_factor(ot->srna, + "factor", + 1.0f / 3.0f, + -FLT_MAX, + FLT_MAX, + "Factor", + "How much to blend to the default value", + 0.0f, + 1.0f); +} +/** \} */ -- cgit v1.2.3