diff options
author | Erik Abrahamsson <erik85> | 2020-12-28 20:17:49 +0300 |
---|---|---|
committer | Hans Goudey <h.goudey@me.com> | 2020-12-28 20:18:52 +0300 |
commit | 6fbeb6e2e05408af448e9409f8e7e11470f82db6 (patch) | |
tree | af7a763798177bb54e61b164bc215ed610983b0c /source/blender/editors/object/object_gpencil_modifier.c | |
parent | 7f3601e70dd094b6f9162bb03a8a3440d50de47e (diff) |
Add operator to copy a modifier to all selected objects
These two operators (one for grease pencil, one for other objects)
copy a single modifier from the active object to all selected objects.
The operators are exposed in the dropdown menus in modifier headers.
Note that It's currently possible to drag and drop modifiers between
objects in the outliner, but that only works for dragging to one object
at a time. Modifiers can also be copied with the "Make Links" operator,
but that copies *all* modifiers rather than just one. The placement
and scope of these new operators allow for more useful poll messages
and error messages as well.
Every object type that supports modifiers is supported. Although hook
and collision modifiers aren't supported because of an unexplained
comment in `BKE_object_copy_modifier`, other than that, every modifier
type is supported, including particle systems, nodes modifiers, etc.
The new modifiers are set active, which required two small tweaks to
`object.c` and `particle.c`.
Reviewed By: Hans Goudey (with additional edits)
Differential Revision: https://developer.blender.org/D9537
Diffstat (limited to 'source/blender/editors/object/object_gpencil_modifier.c')
-rw-r--r-- | source/blender/editors/object/object_gpencil_modifier.c | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/source/blender/editors/object/object_gpencil_modifier.c b/source/blender/editors/object/object_gpencil_modifier.c index e5feb74df26..eb3ccf52c6f 100644 --- a/source/blender/editors/object/object_gpencil_modifier.c +++ b/source/blender/editors/object/object_gpencil_modifier.c @@ -835,3 +835,99 @@ void OBJECT_OT_gpencil_modifier_copy(wmOperatorType *ot) ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL; gpencil_edit_modifier_properties(ot); } + +/************************ Copy Modifier to Selected Operator *********************/ + +static int gpencil_modifier_copy_to_selected_exec(bContext *C, wmOperator *op) +{ + Object *obact = ED_object_active_context(C); + GpencilModifierData *md = gpencil_edit_modifier_property_get(op, obact, 0); + + if (!md) { + return OPERATOR_CANCELLED; + } + + if (obact->type != OB_GPENCIL) { + BKE_reportf(op->reports, + RPT_ERROR, + "Source object '%s' is not a grease pencil object", + obact->id.name + 2); + return OPERATOR_CANCELLED; + } + + CTX_DATA_BEGIN (C, Object *, ob, selected_objects) { + if (ob == obact) { + continue; + } + + if (ob->type != OB_GPENCIL) { + BKE_reportf(op->reports, + RPT_WARNING, + "Destination object '%s' is not a grease pencil object", + ob->id.name + 2); + continue; + } + + /* This always returns true right now. */ + BKE_object_copy_gpencil_modifier(ob, md); + + WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob); + DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION); + } + CTX_DATA_END; + + return OPERATOR_FINISHED; +} + +static int gpencil_modifier_copy_to_selected_invoke(bContext *C, + wmOperator *op, + const wmEvent *event) +{ + int retval; + if (gpencil_edit_modifier_invoke_properties(C, op, event, &retval)) { + return gpencil_modifier_copy_to_selected_exec(C, op); + } + return retval; +} + +static bool gpencil_modifier_copy_to_selected_poll(bContext *C) +{ + Object *obact = ED_object_active_context(C); + + /* This could have a performance impact in the worst case, where there are many objects selected + * and none of them pass the check. But that should be uncommon, and this operator is only + * exposed in a drop-down menu anyway. */ + bool found_supported_objects = false; + CTX_DATA_BEGIN (C, Object *, ob, selected_objects) { + if (ob == obact) { + continue; + } + + if (ob->type == OB_GPENCIL) { + found_supported_objects = true; + break; + } + } + CTX_DATA_END; + + if (!found_supported_objects) { + CTX_wm_operator_poll_msg_set(C, "No supported objects were selected"); + return false; + } + return true; +} + +void OBJECT_OT_gpencil_modifier_copy_to_selected(wmOperatorType *ot) +{ + ot->name = "Copy Modifier to Selected"; + ot->description = "Copy the modifier from the active object to all selected objects"; + ot->idname = "OBJECT_OT_gpencil_modifier_copy_to_selected"; + + ot->invoke = gpencil_modifier_copy_to_selected_invoke; + ot->exec = gpencil_modifier_copy_to_selected_exec; + ot->poll = gpencil_modifier_copy_to_selected_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL; + gpencil_edit_modifier_properties(ot); +} |