[Bf-blender-cvs] [bc95c249a76] master: Refactor modifier copying code.

Bastien Montagne noreply at git.blender.org
Tue Jan 19 18:50:37 CET 2021


Commit: bc95c249a76563bffd8ce9242f7de6849cebe801
Author: Bastien Montagne
Date:   Tue Jan 19 16:02:25 2021 +0100
Branches: master
https://developer.blender.org/rBbc95c249a76563bffd8ce9242f7de6849cebe801

Refactor modifier copying code.

Things like pointers to particle systems, or softbody data being stored
outside of its modifier, make it impossible for internal modifier copy
data code to be self-contained currently. It requires extra processing.

In existing code this was handled in several different places, in
several ways, and alltogether fairly inconsistently. Some cases were
even not properly handled, causing e.g. crashes as in T82945.

This commit addresses those issues by:
 * Adding comments about the hackish/unsafe parts `psys` implies when
   copying some modifier data (since we need to ensure particle system
   copying and remapping of those pointers separately).
 * Adding as-best-as-possible handling of those cases to
   `BKE_object_copy_modifier` (note that it remains fragile, but is
   expected to behave 'good enough' in any practical usecase).
 * Remove special handling for specific editor code
   (`copy_or_reuse_particle_system`). This should never have been
   accepted in ED code area, and is now handled by
   `BKE_object_copy_modifier`.
 * Factorize copying of the whole modifier stack into new
   `BKE_object_modifier_stack_copy`, now used by both `object_copy_data`
   and `BKE_object_link_modifiers`.

Note that this implies that `BKE_object_copy_modifier` and
`BKE_object_copy_gpencil_modifier` are now to be used exclusively to
copy single modifiers. Full modifier stack copy should always use
`BKE_object_modifier_stack_copy` instead.

Fix T82945: Crash when dragging modifiers in Outliner.

Maniphest Tasks: T82945

Differential Revision: https://developer.blender.org/D10148

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

M	source/blender/blenkernel/BKE_object.h
M	source/blender/blenkernel/intern/dynamicpaint.c
M	source/blender/blenkernel/intern/fluid.c
M	source/blender/blenkernel/intern/object.c
M	source/blender/editors/object/object_modifier.c
M	source/blender/makesdna/DNA_dynamicpaint_types.h
M	source/blender/makesdna/DNA_modifier_types.h
M	source/blender/modifiers/intern/MOD_particlesystem.c

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

diff --git a/source/blender/blenkernel/BKE_object.h b/source/blender/blenkernel/BKE_object.h
index 32d99191067..904db053717 100644
--- a/source/blender/blenkernel/BKE_object.h
+++ b/source/blender/blenkernel/BKE_object.h
@@ -82,10 +82,16 @@ bool BKE_object_support_modifier_type_check(const struct Object *ob, int modifie
 void BKE_object_modifier_set_active(struct Object *ob, struct ModifierData *md);
 struct ModifierData *BKE_object_active_modifier(const struct Object *ob);
 
-bool BKE_object_copy_modifier(struct Object *ob_dst,
+bool BKE_object_copy_modifier(struct Main *bmain,
+                              struct Scene *scene,
+                              struct Object *ob_dst,
                               const struct Object *ob_src,
                               struct ModifierData *md);
 bool BKE_object_copy_gpencil_modifier(struct Object *ob_dst, struct GpencilModifierData *md);
+bool BKE_object_modifier_stack_copy(struct Object *ob_dst,
+                                    const struct Object *ob_src,
+                                    const bool do_copy_all,
+                                    const int flag_subdata);
 void BKE_object_link_modifiers(struct Object *ob_dst, const struct Object *ob_src);
 void BKE_object_free_modifiers(struct Object *ob, const int flag);
 void BKE_object_free_shaderfx(struct Object *ob, const int flag);
diff --git a/source/blender/blenkernel/intern/dynamicpaint.c b/source/blender/blenkernel/intern/dynamicpaint.c
index e18b2d87459..811a27c9f3f 100644
--- a/source/blender/blenkernel/intern/dynamicpaint.c
+++ b/source/blender/blenkernel/intern/dynamicpaint.c
@@ -1316,6 +1316,10 @@ void dynamicPaint_Modifier_copy(const struct DynamicPaintModifierData *pmd,
     t_brush->particle_radius = brush->particle_radius;
     t_brush->particle_smooth = brush->particle_smooth;
     t_brush->paint_distance = brush->paint_distance;
+
+    /* NOTE: This is dangerous, as it will generate invalid data in case we are copying between
+     * different objects. Extra external code has to be called then to ensure proper remapping of
+     * that pointer. See e.g. `BKE_object_copy_particlesystems` or `BKE_object_copy_modifier`. */
     t_brush->psys = brush->psys;
 
     if (brush->paint_ramp) {
diff --git a/source/blender/blenkernel/intern/fluid.c b/source/blender/blenkernel/intern/fluid.c
index 4356f1246ed..0f8a11a40b2 100644
--- a/source/blender/blenkernel/intern/fluid.c
+++ b/source/blender/blenkernel/intern/fluid.c
@@ -5136,6 +5136,9 @@ void BKE_fluid_modifier_copy(const struct FluidModifierData *fmd,
     FluidFlowSettings *tffs = tfmd->flow;
     FluidFlowSettings *ffs = fmd->flow;
 
+    /* NOTE: This is dangerous, as it will generate invalid data in case we are copying between
+     * different objects. Extra external code has to be called then to ensure proper remapping of
+     * that pointer. See e.g. `BKE_object_copy_particlesystems` or `BKE_object_copy_modifier`. */
     tffs->psys = ffs->psys;
     tffs->noise_texture = ffs->noise_texture;
 
diff --git a/source/blender/blenkernel/intern/object.c b/source/blender/blenkernel/intern/object.c
index ea0ce6a0f33..155508e9caa 100644
--- a/source/blender/blenkernel/intern/object.c
+++ b/source/blender/blenkernel/intern/object.c
@@ -218,26 +218,7 @@ static void object_copy_data(Main *bmain, ID *id_dst, const ID *id_src, const in
     ob_dst->runtime.bb = MEM_dupallocN(ob_src->runtime.bb);
   }
 
-  BLI_listbase_clear(&ob_dst->modifiers);
-
-  LISTBASE_FOREACH (ModifierData *, md, &ob_src->modifiers) {
-    ModifierData *nmd = BKE_modifier_new(md->type);
-    BLI_strncpy(nmd->name, md->name, sizeof(nmd->name));
-    BKE_modifier_copydata_ex(md, nmd, flag_subdata);
-    BLI_addtail(&ob_dst->modifiers, nmd);
-  }
-
-  BLI_listbase_clear(&ob_dst->greasepencil_modifiers);
-
-  LISTBASE_FOREACH (GpencilModifierData *, gmd, &ob_src->greasepencil_modifiers) {
-    GpencilModifierData *nmd = BKE_gpencil_modifier_new(gmd->type);
-    BLI_strncpy(nmd->name, gmd->name, sizeof(nmd->name));
-    BKE_gpencil_modifier_copydata_ex(gmd, nmd, flag_subdata);
-    BLI_addtail(&ob_dst->greasepencil_modifiers, nmd);
-  }
-
   BLI_listbase_clear(&ob_dst->shader_fx);
-
   LISTBASE_FOREACH (ShaderFxData *, fx, &ob_src->shader_fx) {
     ShaderFxData *nfx = BKE_shaderfx_new(fx->type);
     BLI_strncpy(nfx->name, fx->name, sizeof(nfx->name));
@@ -266,10 +247,12 @@ static void object_copy_data(Main *bmain, ID *id_dst, const ID *id_src, const in
       ob_dst->pd->rng = MEM_dupallocN(ob_src->pd->rng);
     }
   }
-  BKE_object_copy_softbody(ob_dst, ob_src, flag_subdata);
   BKE_rigidbody_object_copy(bmain, ob_dst, ob_src, flag_subdata);
 
-  BKE_object_copy_particlesystems(ob_dst, ob_src, flag_subdata);
+  BLI_listbase_clear(&ob_dst->modifiers);
+  BLI_listbase_clear(&ob_dst->greasepencil_modifiers);
+  /* Note: Also takes care of softbody and particle systems copying. */
+  BKE_object_modifier_stack_copy(ob_dst, ob_src, true, flag_subdata);
 
   BLI_listbase_clear((ListBase *)&ob_dst->drawdata);
   BLI_listbase_clear(&ob_dst->pc_ids);
@@ -1354,17 +1337,76 @@ bool BKE_object_support_modifier_type_check(const Object *ob, int modifier_type)
   return false;
 }
 
-bool BKE_object_copy_modifier(struct Object *ob_dst, const struct Object *ob_src, ModifierData *md)
+static bool object_modifier_type_copy_check(ModifierType md_type)
 {
-  if (ELEM(md->type, eModifierType_Hook, eModifierType_Collision)) {
-    return false;
+  return !ELEM(md_type, eModifierType_Hook, eModifierType_Collision);
+}
+
+/** Find a `psys` matching given `psys_src` in `ob_dst` (i.e. sharing the same ParticleSettings
+ * ID), or add one, and return valid `psys` from `ob_dst`.
+ *
+ * \note Order handling is fairly weak here. This code assumes that it is called **before** the
+ * modifier using the psys is actually copied, and that this copied modifier will be added at the
+ * end of the stack. That way we can be sure that the particle modifier will be before the one
+ * using its particle system in the stack.
+ */
+static ParticleSystem *object_copy_modifier_particle_system_ensure(Main *bmain,
+                                                                   Scene *scene,
+                                                                   Object *ob_dst,
+                                                                   ParticleSystem *psys_src)
+{
+  ParticleSystem *psys_dst = NULL;
+
+  /* Check if a particle system with the same particle settings
+   * already exists on the destination object. */
+  LISTBASE_FOREACH (ParticleSystem *, psys, &ob_dst->particlesystem) {
+    if (psys->part == psys_src->part) {
+      psys_dst = psys;
+      break;
+    }
   }
 
-  if (!BKE_object_support_modifier_type_check(ob_dst, md->type)) {
+  /* If it does not exist, copy the particle system to the destination object. */
+  if (psys_dst == NULL) {
+    ModifierData *md = object_copy_particle_system(bmain, scene, ob_dst, psys_src);
+    psys_dst = ((ParticleSystemModifierData *)md)->psys;
+  }
+
+  return psys_dst;
+}
+
+/** Copy a single modifier.
+ *
+ * \note **Do not** use this function to copy a whole modifier stack (see note below too). Use
+ * `BKE_object_modifier_stack_copy` instead.
+ *
+ * \note Complex modifiers relaying on other data (like e.g. dynamic paint or fluid using particle
+ * systems) are not always 100% 'correctly' copied here, since we have to use heuristics to decide
+ * which particle system to use or add in `ob_dst`, and it's placement in the stack, etc. If used
+ * more than once, this function should preferably be called in stack order. */
+bool BKE_object_copy_modifier(
+    Main *bmain, Scene *scene, Object *ob_dst, const Object *ob_src, ModifierData *md_src)
+{
+  BLI_assert(ob_dst->type != OB_GPENCIL);
+
+  const ModifierTypeInfo *mti = BKE_modifier_get_info(md_src->type);
+  if (!object_modifier_type_copy_check(md_src->type)) {
+    /* We never allow copying those modifiers here. */
+    return false;
+  }
+  if (!BKE_object_support_modifier_type_check(ob_dst, md_src->type)) {
     return false;
   }
+  if (mti->flags & eModifierTypeFlag_Single) {
+    if (BKE_modifiers_findby_type(ob_dst, md_src->type) != NULL) {
+      return false;
+    }
+  }
+
+  ParticleSystem *psys_src = NULL;
+  ParticleSystem *psys_dst = NULL;
 
-  switch (md->type) {
+  switch (md_src->type) {
     case eModifierType_Softbody:
       BKE_object_copy_softbody(ob_dst, ob_src, 0);
       break;
@@ -1372,66 +1414,158 @@ bool BKE_object_copy_modifier(struct Object *ob_dst, const struct Object *ob_src
       /* ensure skin-node customdata exists */
       BKE_mesh_ensure_skin_customdata(ob_dst->data);
       break;
+    case eModifierType_Fluid: {
+      FluidModifierData *fmd = (FluidModifierData *)md_src;
+      if (fmd->type == MOD_FLUID_TYPE_FLOW) {
+        if (fmd->flow != NULL && fmd->flow->psys != NULL) {
+          psys_src = fmd->flow->psys;
+          psys_dst = object_copy_modifier_particle_system_ensure(bmain, scene, ob_dst, psys_src);
+        }
+      }
+      break;
+    }
+    case eModifierType_DynamicPaint: {
+      DynamicPaintModifierData *dpmd = (DynamicPaintModifierData *)md_src;
+      if (dpmd->brush != NULL && dpmd->brush->psys != NULL) {
+        psys_src = dpmd->brush->psys;
+        psys_dst = object_copy_modifier_particle_system_ensure(bmain, scene, ob_dst, psys_src);
+      }
+      break;
+    }
+    default:
+      break;
+  }
+
+  ModifierData *md_dst;
+  if (md_src->type == eModifierType_ParticleSystem) {
+    md_dst = object_copy_particle_system(
+        bmain, scene, ob_dst, ((ParticleSystemModifierData *)md_src)->psys);
   }
+  else {
+    md_dst = BKE_modifier_new(md_src->type);
+
+    BLI_strncpy(md_dst->name, md_src->name, sizeof(md_dst->name));
 
-  ModifierData *nmd = BKE_modifier_new(md->type);
-  BLI_strncpy(nmd->name, md->name, sizeof(nmd->name));
+    if (md_src->type == eModifierType_Multires) {
+      /* Has to be done after mod creation, but *before* we actually copy its settings! */
+      multiresModifier_sync_levels_ex(
+          ob_dst, (MultiresModifierData *)md_src, (MultiresModifierData *)md_dst);
+    }
+
+    BKE_modifier_copydata(md_src, md_dst);
+
+    switch (md_dst->typ

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list