From 69ee6c986657bf0d6bf631277751d24e72d76bac Mon Sep 17 00:00:00 2001 From: Joshua Leung Date: Fri, 15 Apr 2016 20:04:07 +1200 Subject: Drivers Editing: Added "Copy/Paste" buttons beside "Add Variable" for copying all variables from one driver to another This was a feature request from a few years back (IIRC from ZanQdo?) to make it easier to reuse one set of driver variables across several different drivers. Dev Notes: * Finally it's done! All that trouble for two little buttons. * Grr... cmake... grrr! --- source/blender/editors/space_graph/graph_buttons.c | 30 +++-- source/blender/editors/space_graph/graph_edit.c | 128 +++++++++++++++++++++ source/blender/editors/space_graph/graph_intern.h | 5 + source/blender/editors/space_graph/graph_ops.c | 4 + 4 files changed, 158 insertions(+), 9 deletions(-) (limited to 'source/blender/editors/space_graph') diff --git a/source/blender/editors/space_graph/graph_buttons.c b/source/blender/editors/space_graph/graph_buttons.c index f8b081eda20..5846a439b3c 100644 --- a/source/blender/editors/space_graph/graph_buttons.c +++ b/source/blender/editors/space_graph/graph_buttons.c @@ -516,7 +516,7 @@ static void driver_delete_var_cb(bContext *UNUSED(C), void *driver_v, void *dvar DriverVar *dvar = (DriverVar *)dvar_v; /* remove the active variable */ - driver_free_variable(driver, dvar); + driver_free_variable_ex(driver, dvar); } /* callback to report why a driver variable is invalid */ @@ -825,14 +825,26 @@ static void graph_panel_drivers(const bContext *C, Panel *pa) uiItemL(row, valBuf, ICON_NONE); } - /* add driver variables */ - col = uiLayoutColumn(pa->layout, false); - block = uiLayoutGetBlock(col); - but = uiDefIconTextBut(block, UI_BTYPE_BUT, B_IPO_DEPCHANGE, ICON_ZOOMIN, IFACE_("Add Variable"), - 0, 0, 10 * UI_UNIT_X, UI_UNIT_Y, - NULL, 0.0, 0.0, 0, 0, - TIP_("Driver variables ensure that all dependencies will be accounted for and that drivers will update correctly")); - UI_but_func_set(but, driver_add_var_cb, driver, NULL); + /* add/copy/paste driver variables */ + { + uiLayout *row; + + /* add driver variable */ + row = uiLayoutRow(pa->layout, false); + block = uiLayoutGetBlock(row); + but = uiDefIconTextBut(block, UI_BTYPE_BUT, B_IPO_DEPCHANGE, ICON_ZOOMIN, IFACE_("Add Variable"), + 0, 0, 10 * UI_UNIT_X, UI_UNIT_Y, + NULL, 0.0, 0.0, 0, 0, + TIP_("Driver variables ensure that all dependencies will be accounted for and that drivers will update correctly")); + UI_but_func_set(but, driver_add_var_cb, driver, NULL); + + /* copy/paste (as sub-row) */ + row = uiLayoutRow(row, true); + block = uiLayoutGetBlock(row); + + uiItemO(row, "", ICON_COPYDOWN, "GRAPH_OT_driver_variables_copy"); + uiItemO(row, "", ICON_PASTEDOWN, "GRAPH_OT_driver_variables_paste"); + } /* loop over targets, drawing them */ for (dvar = driver->variables.first; dvar; dvar = dvar->next) { diff --git a/source/blender/editors/space_graph/graph_edit.c b/source/blender/editors/space_graph/graph_edit.c index dd2ec2401a5..a54b7e141f3 100644 --- a/source/blender/editors/space_graph/graph_edit.c +++ b/source/blender/editors/space_graph/graph_edit.c @@ -54,6 +54,7 @@ #include "BLT_translation.h" +#include "BKE_depsgraph.h" #include "BKE_fcurve.h" #include "BKE_global.h" #include "BKE_nla.h" @@ -2610,3 +2611,130 @@ void GRAPH_OT_fmodifier_paste(wmOperatorType *ot) } /* ************************************************************************** */ +/* Drivers */ + +/* ******************** Copy Driver Vars Operator *********************** */ + +static int graph_driver_vars_copy_exec(bContext *C, wmOperator *op) +{ + bAnimContext ac; + bAnimListElem *ale; + bool ok = false; + + /* get editor data */ + if (ANIM_animdata_get_context(C, &ac) == 0) + return OPERATOR_CANCELLED; + + /* clear buffer first */ + ANIM_driver_vars_copybuf_free(); + + /* get the active F-Curve */ + ale = get_active_fcurve_channel(&ac); + + /* if this exists, call the copy driver vars API function */ + if (ale && ale->data) { + FCurve *fcu = (FCurve *)ale->data; + + ok = ANIM_driver_vars_copy(op->reports, fcu); + + /* free temp data now */ + MEM_freeN(ale); + } + + /* successful or not? */ + if (ok == 0) + return OPERATOR_CANCELLED; + else + return OPERATOR_FINISHED; +} + +void GRAPH_OT_driver_variables_copy(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Copy Driver Variables"; + ot->idname = "GRAPH_OT_driver_variables_copy"; + ot->description = "Copy the driver variables of the active F-Curve"; + + /* api callbacks */ + ot->exec = graph_driver_vars_copy_exec; + ot->poll = graphop_active_fcurve_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; +} + +/* ******************** Paste Driver Vars Operator *********************** */ + +static int graph_driver_vars_paste_exec(bContext *C, wmOperator *op) +{ + bAnimContext ac; + + ListBase anim_data = {NULL, NULL}; + bAnimListElem *ale; + int filter; + + const bool replace = RNA_boolean_get(op->ptr, "replace"); + bool ok = false; + + /* get editor data */ + if (ANIM_animdata_get_context(C, &ac) == 0) + return OPERATOR_CANCELLED; + + /* filter data */ + filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_ACTIVE | ANIMFILTER_FOREDIT | ANIMFILTER_NODUPLIS); + ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype); + + /* paste modifiers */ + for (ale = anim_data.first; ale; ale = ale->next) { + FCurve *fcu = (FCurve *)ale->data; + bool paste_ok; + + printf("pasting vars to %p -> %p\n", fcu, fcu->driver); + paste_ok = ANIM_driver_vars_paste(op->reports, fcu, replace); + + if (paste_ok) { + //ale->update |= ANIM_UPDATE_DEPS; + printf("now we have: %d vars\n", BLI_listbase_count(&fcu->driver->variables)); + ok = true; + } + } + + // XXX: something causes a crash after adding the copies (to empty list), if we do an update immediately + if (ok) { + DAG_relations_tag_update(CTX_data_main(C)); + //ANIM_animdata_update(&ac, &anim_data); + } + ANIM_animdata_freelist(&anim_data); + + /* successful or not? */ + if (ok) { + /* set notifier that keyframes have changed */ + WM_event_add_notifier(C, NC_SCENE | ND_FRAME, CTX_data_scene(C)); + return OPERATOR_FINISHED; + } + else { + return OPERATOR_CANCELLED; + } +} + +void GRAPH_OT_driver_variables_paste(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Paste Driver Variables"; + ot->idname = "GRAPH_OT_driver_variables_paste"; + ot->description = "Add copied driver variables to the active driver"; + + /* api callbacks */ + ot->exec = graph_driver_vars_paste_exec; + ot->poll = graphop_active_fcurve_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + /* properties */ + RNA_def_boolean(ot->srna, "only_active", true, "Only Active", "Only paste F-Modifiers on active F-Curve"); + RNA_def_boolean(ot->srna, "replace", false, "Replace Existing", + "Replace existing F-Modifiers, instead of just appending to the end of the existing list"); +} + +/* ************************************************************************** */ diff --git a/source/blender/editors/space_graph/graph_intern.h b/source/blender/editors/space_graph/graph_intern.h index a478a86a5e2..b6b711e129f 100644 --- a/source/blender/editors/space_graph/graph_intern.h +++ b/source/blender/editors/space_graph/graph_intern.h @@ -148,6 +148,11 @@ void GRAPH_OT_fmodifier_paste(struct wmOperatorType *ot); /* ----------- */ +void GRAPH_OT_driver_variables_copy(struct wmOperatorType *ot); +void GRAPH_OT_driver_variables_paste(struct wmOperatorType *ot); + +/* ----------- */ + void GRAPH_OT_ghost_curves_create(struct wmOperatorType *ot); void GRAPH_OT_ghost_curves_clear(struct wmOperatorType *ot); diff --git a/source/blender/editors/space_graph/graph_ops.c b/source/blender/editors/space_graph/graph_ops.c index 59215531ac0..7ffa8250067 100644 --- a/source/blender/editors/space_graph/graph_ops.c +++ b/source/blender/editors/space_graph/graph_ops.c @@ -458,6 +458,10 @@ void graphedit_operatortypes(void) WM_operatortype_append(GRAPH_OT_fmodifier_add); WM_operatortype_append(GRAPH_OT_fmodifier_copy); WM_operatortype_append(GRAPH_OT_fmodifier_paste); + + /* Drivers */ + WM_operatortype_append(GRAPH_OT_driver_variables_copy); + WM_operatortype_append(GRAPH_OT_driver_variables_paste); } void ED_operatormacros_graph(void) -- cgit v1.2.3