[Bf-blender-cvs] [6fbeb6e2e05] master: Add operator to copy a modifier to all selected objects

Erik Abrahamsson noreply at git.blender.org
Mon Dec 28 18:20:00 CET 2020


Commit: 6fbeb6e2e05408af448e9409f8e7e11470f82db6
Author: Erik Abrahamsson
Date:   Mon Dec 28 11:17:49 2020 -0600
Branches: master
https://developer.blender.org/rB6fbeb6e2e05408af448e9409f8e7e11470f82db6

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

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

M	source/blender/blenkernel/BKE_object.h
M	source/blender/blenkernel/intern/object.c
M	source/blender/blenkernel/intern/particle.c
M	source/blender/editors/object/object_gpencil_modifier.c
M	source/blender/editors/object/object_intern.h
M	source/blender/editors/object/object_modifier.c
M	source/blender/editors/object/object_ops.c
M	source/blender/gpencil_modifiers/intern/MOD_gpencil_ui_common.c
M	source/blender/modifiers/intern/MOD_ui_common.c

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

diff --git a/source/blender/blenkernel/BKE_object.h b/source/blender/blenkernel/BKE_object.h
index a620d9af946..cd253ca4bbc 100644
--- a/source/blender/blenkernel/BKE_object.h
+++ b/source/blender/blenkernel/BKE_object.h
@@ -75,6 +75,7 @@ bool BKE_object_modifier_gpencil_use_time(struct Object *ob, struct GpencilModif
 
 bool BKE_object_shaderfx_use_time(struct Object *ob, struct ShaderFxData *md);
 
+bool BKE_object_supports_modifiers(const struct Object *ob);
 bool BKE_object_support_modifier_type_check(const struct Object *ob, int modifier_type);
 
 /* Active modifier. */
diff --git a/source/blender/blenkernel/intern/object.c b/source/blender/blenkernel/intern/object.c
index 7ed906e2cea..163e7d51235 100644
--- a/source/blender/blenkernel/intern/object.c
+++ b/source/blender/blenkernel/intern/object.c
@@ -1315,6 +1315,15 @@ ModifierData *BKE_object_active_modifier(const Object *ob)
   return NULL;
 }
 
+/**
+ * \return True if the object's type supports regular modifiers (not grease pencil modifiers).
+ */
+bool BKE_object_supports_modifiers(const Object *ob)
+{
+  return (
+      ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_LATTICE, OB_POINTCLOUD, OB_VOLUME));
+}
+
 bool BKE_object_support_modifier_type_check(const Object *ob, int modifier_type)
 {
   const ModifierTypeInfo *mti = BKE_modifier_get_info(modifier_type);
@@ -1377,6 +1386,7 @@ bool BKE_object_copy_modifier(struct Object *ob_dst, const struct Object *ob_src
   BKE_modifier_copydata(md, nmd);
   BLI_addtail(&ob_dst->modifiers, nmd);
   BKE_modifier_unique_name(&ob_dst->modifiers, nmd);
+  BKE_object_modifier_set_active(ob_dst, nmd);
 
   return true;
 }
diff --git a/source/blender/blenkernel/intern/particle.c b/source/blender/blenkernel/intern/particle.c
index dce45f44583..335913c9b8e 100644
--- a/source/blender/blenkernel/intern/particle.c
+++ b/source/blender/blenkernel/intern/particle.c
@@ -75,6 +75,7 @@
 #include "BKE_material.h"
 #include "BKE_mesh.h"
 #include "BKE_modifier.h"
+#include "BKE_object.h"
 #include "BKE_particle.h"
 #include "BKE_pointcache.h"
 #include "BKE_scene.h"
@@ -3948,6 +3949,7 @@ static ModifierData *object_add_or_copy_particle_system(
   psmd = (ParticleSystemModifierData *)md;
   psmd->psys = psys;
   BLI_addtail(&ob->modifiers, md);
+  BKE_object_modifier_set_active(ob, md);
 
   psys->totpart = 0;
   psys->flag = PSYS_CURRENT;
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);
+}
diff --git a/source/blender/editors/object/object_intern.h b/source/blender/editors/object/object_intern.h
index 89ade5cc49d..00426e96a81 100644
--- a/source/blender/editors/object/object_intern.h
+++ b/source/blender/editors/object/object_intern.h
@@ -175,6 +175,7 @@ void OBJECT_OT_modifier_apply(struct wmOperatorType *ot);
 void OBJECT_OT_modifier_apply_as_shapekey(wmOperatorType *ot);
 void OBJECT_OT_modifier_convert(struct wmOperatorType *ot);
 void OBJECT_OT_modifier_copy(struct wmOperatorType *ot);
+void OBJECT_OT_modifier_copy_to_selected(struct wmOperatorType *ot);
 void OBJECT_OT_modifier_set_active(struct wmOperatorType *ot);
 void OBJECT_OT_multires_subdivide(struct wmOperatorType *ot);
 void OBJECT_OT_multires_reshape(struct wmOperatorType *ot);
@@ -203,6 +204,7 @@ void OBJECT_OT_gpencil_modifier_move_down(struct wmOperatorType *ot);
 void OBJECT_OT_gpencil_modifier_move_to_index(struct wmOperatorType *ot);
 void OBJECT_OT_gpencil_modifier_apply(struct wmOperatorType *ot);
 void OBJECT_OT_gpencil_modifier_copy(struct wmOperatorType *ot);
+void OBJECT_OT_gpencil_modifier_copy_to_selected(struct wmOperatorType *ot);
 
 /* object_shader_fx.c */
 void OBJECT_OT_shaderfx_add(struct wmOperatorType *ot);
diff --git a/source/blender/editors/object/object_modifier.c b/source/blender/editors/object/object_modifier.c
index 3111003703f..e9c84353b80 100644
--- a/source/blender/editors/object/object_modifier.c
+++ b/source/blender/editors/object/object_modifier.c
@@ -30,6 +30,8 @@
 #include "DNA_anim_types.h"
 #include "DNA_armature_types.h"
 #include "DNA_curve_types.h"
+#include "DNA_dynamicpaint_types.h"
+#include "DNA_fluid_types.h"
 #include "DNA_key_types.h"
 #include "DNA_lattice_types.h"
 #include "DNA_mesh_types.h"
@@ -1681,6 +1683,229 @@ void OBJECT_OT_modifier_set_active(wmOperatorType *ot)
   edit_modifier_properties(ot);
 }
 
+/** \} */
+/** \name Copy Modifier To Selected Operator
+ * \{ */
+
+/* If the modifier uses particles, copy particle system to destination object
+ * or reuse existing if it has the same ParticleSettings */
+static void copy_or_reuse_particle_system(bContext *C, Object *ob, ModifierData *md)
+{
+  ParticleSystem *psys_on_modifier = NULL;
+
+  if (md->type == eModifierType_DynamicPaint) {
+    DynamicPaintModifierData *pmd = (DynamicPaintModifierData *)md;
+    if (pmd->brush && pmd->brush->psys) {
+      psys_on_modifier = pmd->brush->psys;
+    }
+  }
+  else if (md->type == eModifierType_Fluid) {
+    FluidModifierData *fmd = (FluidModifierData *)md;
+    if (fmd->type == MOD_FLUID_TYPE_FLOW) {
+      if (fmd->flow && fmd->flow->psys) {
+        psys_on_modifier = fmd->flow->psys;
+      }
+    }
+  }
+
+  if (!psys_on_modifier) {
+    return;
+  }
+
+  ParticleSystem *psys_on_new_modifier = NULL;
+
+  /* Check if a particle system with the same particle settings
+   * already exists on the destination object. */
+  LISTBASE_FOREACH (ParticleSystem *, psys, &ob->particlesystem) {
+    if (psys_on_modifier->part == psys->part) {
+      psys_on_new_modifier = psys;
+      break;
+    }
+  }
+
+  /* If it does not exist, copy the particle system to the destination object. */
+  if (!psys_on_new_modifier) {
+    Main *bmain = CTX_data_main(C);
+    Scene *scene = CTX_data_scene(C);
+    object_copy_particle_system(bmain, scene, ob, psys_on_modifier);
+
+    LISTBASE_FOREACH (ParticleSystem *, psys, &ob->particlesystem) {
+      if (psys_on_modifier->part == psys->part) {
+        psys_on_new_modifier = psys;
+      }
+    }
+  }
+
+  /* Update the modifier to point to the new/existing particle system. */
+  LISTBASE_FOREACH (ModifierData *, new_md, &ob->modifiers) {
+    if (new_md->type == eModifierType_DynamicPaint) {
+      DynamicPaintModifierData *new_pmd = (DynamicPaintModifierData *)new_md;
+
+      if (psys_on_modifier == new_pmd->brush->psys) {
+        new_pmd->brush->psys = psys_on_new_modifier;
+      }
+    }
+    else if (new_md->type == eModifierType_Fluid) {
+      FluidModifierData *new_fmd = (FluidModifierData *)new_md;
+
+      if (psys_on_modifier == new_fmd->flow->psys) {
+        new_fmd->flow->psys = psys_on_new_modifier;
+      }
+    }
+  }
+}
+
+static int modifier_copy_to_selected_exec(bContext *C, wmOperator *op)
+{
+  Main *bmain = CTX_data_main(C);
+  Scene *scene = CTX_data_scene(C);
+  Object *obact = ED_object_active_context(C);
+  ModifierData *md = edit_modifier_property_get(op, obact, 0);

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list