[Bf-blender-cvs] [ad6cccf058d] master: Refactor duplicate of data-blocks.

Bastien Montagne noreply at git.blender.org
Wed Jun 17 17:09:16 CEST 2020


Commit: ad6cccf058d0296a8741a6583d12967366a31705
Author: Bastien Montagne
Date:   Wed Jun 17 15:02:04 2020 +0200
Branches: master
https://developer.blender.org/rBad6cccf058d0296a8741a6583d12967366a31705

Refactor duplicate of data-blocks.

Main change from user side, besides that all pointers should now be
properly remapped to new IDs, is that linked objects are no longer
preserved when doing a full copy of scenes.

Will open a task to check whether we actually still want that behavior
(and re-code it in a more correct way then).

This is the main part of work done here, it aims at uniformizing and
sanitizing that 'deep copy' process for supported IDs (currently scenes,
collections and objects).

Note that there will be more follow up commits after that one, but this
should be the most risky and changing one.

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

M	source/blender/blenkernel/BKE_collection.h
M	source/blender/blenkernel/BKE_lib_id.h
M	source/blender/blenkernel/BKE_object.h
M	source/blender/blenkernel/intern/collection.c
M	source/blender/blenkernel/intern/object.c
M	source/blender/blenkernel/intern/scene.c
M	source/blender/editors/object/object_add.c
M	source/blender/editors/space_outliner/outliner_collections.c
M	source/blender/makesdna/DNA_userdef_types.h
M	tests/python/bl_pyapi_idprop_datablock.py

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

diff --git a/source/blender/blenkernel/BKE_collection.h b/source/blender/blenkernel/BKE_collection.h
index 2db2be131df..4cf33640ebd 100644
--- a/source/blender/blenkernel/BKE_collection.h
+++ b/source/blender/blenkernel/BKE_collection.h
@@ -62,8 +62,8 @@ bool BKE_collection_delete(struct Main *bmain, struct Collection *collection, bo
 struct Collection *BKE_collection_duplicate(struct Main *bmain,
                                             struct Collection *parent,
                                             struct Collection *collection,
-                                            const bool do_objects,
-                                            const bool do_obdata);
+                                            const uint duplicate_flags,
+                                            const uint duplicate_options);
 
 /* Master Collection for Scene */
 
diff --git a/source/blender/blenkernel/BKE_lib_id.h b/source/blender/blenkernel/BKE_lib_id.h
index 7f5a6e3e36a..8ee5562baae 100644
--- a/source/blender/blenkernel/BKE_lib_id.h
+++ b/source/blender/blenkernel/BKE_lib_id.h
@@ -144,6 +144,16 @@ struct ID *BKE_libblock_find_name(struct Main *bmain,
                                   const short type,
                                   const char *name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
 
+/**
+ * Duplicate (a.k.a. deep copy) common processing options.
+ * See also eDupli_ID_Flags for options controlling what kind of IDs to duplicate.
+ */
+typedef enum eLibIDDuplicateFlags {
+  /** This call to a duplicate function is part of another call for some parent ID.
+   * Therefore, this sub-process should not clear `newid` pointers, nor handle remapping itself. */
+  LIB_ID_DUPLICATE_IS_SUBPROCESS = 1 << 0,
+} eLibIDDuplicateFlags;
+
 /* lib_remap.c (keep here since they're general functions) */
 /**
  * New freeing logic options.
diff --git a/source/blender/blenkernel/BKE_object.h b/source/blender/blenkernel/BKE_object.h
index 342e48f5016..d830a35dda0 100644
--- a/source/blender/blenkernel/BKE_object.h
+++ b/source/blender/blenkernel/BKE_object.h
@@ -138,8 +138,9 @@ bool BKE_object_is_libdata(const struct Object *ob);
 bool BKE_object_obdata_is_libdata(const struct Object *ob);
 
 struct Object *BKE_object_duplicate(struct Main *bmain,
-                                    const struct Object *ob,
-                                    const uint dupflag);
+                                    struct Object *ob,
+                                    const uint dupflag,
+                                    const uint duplicate_options);
 
 void BKE_object_obdata_size_init(struct Object *ob, const float scale);
 
diff --git a/source/blender/blenkernel/intern/collection.c b/source/blender/blenkernel/intern/collection.c
index 9abcce7c38f..6127ad075f4 100644
--- a/source/blender/blenkernel/intern/collection.c
+++ b/source/blender/blenkernel/intern/collection.c
@@ -326,15 +326,16 @@ bool BKE_collection_delete(Main *bmain, Collection *collection, bool hierarchy)
 static Collection *collection_duplicate_recursive(Main *bmain,
                                                   Collection *parent,
                                                   Collection *collection_old,
-                                                  const bool do_objects,
-                                                  const bool do_obdata)
+                                                  const eDupli_ID_Flags duplicate_flags,
+                                                  const eLibIDDuplicateFlags duplicate_options)
 {
   Collection *collection_new;
   bool do_full_process = false;
-  const int object_dupflag = (do_obdata) ? U.dupflag : 0;
   const bool is_collection_master = (collection_old->flag & COLLECTION_IS_MASTER) != 0;
   const bool is_collection_liboverride = ID_IS_OVERRIDE_LIBRARY(collection_old);
 
+  const bool do_objects = (duplicate_flags & USER_DUP_OBJECT) != 0;
+
   if (is_collection_master) {
     /* We never duplicate master collections here, but we can still deep-copy their objects and
      * collections. */
@@ -391,8 +392,8 @@ static Collection *collection_duplicate_recursive(Main *bmain,
       }
 
       if (ob_new == NULL) {
-        ob_new = BKE_object_duplicate(bmain, ob_old, (eDupli_ID_Flags)object_dupflag);
-        ID_NEW_SET(ob_old, ob_new);
+        ob_new = BKE_object_duplicate(
+            bmain, ob_old, duplicate_flags, duplicate_options | LIB_ID_DUPLICATE_IS_SUBPROCESS);
       }
 
       collection_object_add(bmain, collection_new, ob_new, 0, true);
@@ -410,7 +411,7 @@ static Collection *collection_duplicate_recursive(Main *bmain,
     }
 
     collection_duplicate_recursive(
-        bmain, collection_new, child_collection_old, do_objects, do_obdata);
+        bmain, collection_new, child_collection_old, duplicate_flags, duplicate_options);
     collection_child_remove(collection_new, child_collection_old);
   }
 
@@ -420,7 +421,9 @@ static Collection *collection_duplicate_recursive(Main *bmain,
 /**
  * Make a deep copy (aka duplicate) of the given collection and all of its children, recusrsively.
  *
- * \warning This functions will clear all \a bmain id.idnew pointers.
+ * \warning This functions will clear all \a bmain #ID.idnew pointers, unless \a
+ * LIB_ID_DUPLICATE_IS_SUBPROCESS duplicate option is passed on, in which case caller is reponsible
+ * to reconstruct collection dependencies informations (i.e. call #BKE_main_collection_sync).
  *
  * \param do_objects: If true, it will also make copies of objects.
  * \param do_obdata: If true, it will also make duplicates of objects,
@@ -430,23 +433,44 @@ static Collection *collection_duplicate_recursive(Main *bmain,
 Collection *BKE_collection_duplicate(Main *bmain,
                                      Collection *parent,
                                      Collection *collection,
-                                     const bool do_objects,
-                                     const bool do_obdata)
+                                     eDupli_ID_Flags duplicate_flags,
+                                     eLibIDDuplicateFlags duplicate_options)
 {
-  BKE_main_id_tag_all(bmain, LIB_TAG_NEW, false);
-  BKE_main_id_clear_newpoins(bmain);
+  const bool is_subprocess = (duplicate_options & LIB_ID_DUPLICATE_IS_SUBPROCESS) != 0;
 
-  Collection *collection_new = collection_duplicate_recursive(
-      bmain, parent, collection, do_objects, do_obdata);
+  if (!is_subprocess) {
+    BKE_main_id_tag_all(bmain, LIB_TAG_NEW, false);
+    BKE_main_id_clear_newpoins(bmain);
+  }
 
-  /* This code will follow into all ID links using an ID tagged with LIB_TAG_NEW.*/
-  BKE_libblock_relink_to_newid(&collection_new->id);
+  Collection *collection_new = collection_duplicate_recursive(
+      bmain, parent, collection, duplicate_flags, duplicate_options);
+
+  if (!is_subprocess) {
+    /* `collection_duplicate_recursive` will also tag our 'root' collection, whic is not required
+     * unless its duplication is a subprocess of another one. */
+    collection_new->id.tag &= ~LIB_TAG_NEW;
+
+    /* This code will follow into all ID links using an ID tagged with LIB_TAG_NEW.*/
+    BKE_libblock_relink_to_newid(&collection_new->id);
+
+#ifndef NDEBUG
+    /* Call to `BKE_libblock_relink_to_newid` above is supposed to have cleared all those flags. */
+    ID *id_iter;
+    FOREACH_MAIN_ID_BEGIN (bmain, id_iter) {
+      if (id_iter->tag & LIB_TAG_NEW) {
+        BLI_assert((id_iter->tag & LIB_TAG_NEW) == 0);
+      }
+    }
+    FOREACH_MAIN_ID_END;
+#endif
 
-  /* Cleanup. */
-  BKE_main_id_tag_all(bmain, LIB_TAG_NEW, false);
-  BKE_main_id_clear_newpoins(bmain);
+    /* Cleanup. */
+    BKE_main_id_tag_all(bmain, LIB_TAG_NEW, false);
+    BKE_main_id_clear_newpoins(bmain);
 
-  BKE_main_collection_sync(bmain);
+    BKE_main_collection_sync(bmain);
+  }
 
   return collection_new;
 }
diff --git a/source/blender/blenkernel/intern/object.c b/source/blender/blenkernel/intern/object.c
index fd7ddc9eb6d..15cfe4a1d6a 100644
--- a/source/blender/blenkernel/intern/object.c
+++ b/source/blender/blenkernel/intern/object.c
@@ -1758,31 +1758,35 @@ Object *BKE_object_copy(Main *bmain, const Object *ob)
  * \note Caller MUST free \a newid pointers itself (#BKE_main_id_clear_newpoins()) and call updates
  * of DEG too (#DAG_relations_tag_update()).
  */
-Object *BKE_object_duplicate(Main *bmain, const Object *ob, const uint dupflag)
+Object *BKE_object_duplicate(Main *bmain,
+                             Object *ob,
+                             const eDupli_ID_Flags dupflag,
+                             const eLibIDDuplicateFlags duplicate_options)
 {
+  const bool is_subprocess = (duplicate_options & LIB_ID_DUPLICATE_IS_SUBPROCESS) != 0;
+
+  if (!is_subprocess) {
+    BKE_main_id_tag_all(bmain, LIB_TAG_NEW, false);
+    BKE_main_id_clear_newpoins(bmain);
+  }
+
   Material ***matarar;
-  ID *id;
-  int a, didit;
+  ID *id, *id_new;
+  int a;
   const bool is_object_liboverride = ID_IS_OVERRIDE_LIBRARY(ob);
 
-  Object *obn = BKE_object_copy(bmain, ob);
+  Object *obn;
+  BKE_id_copy(bmain, &ob->id, (ID **)&obn);
+  id_us_min(&obn->id);
+  if (is_subprocess) {
+    ID_NEW_SET(ob, obn);
+  }
 
   /* 0 == full linked. */
   if (dupflag == 0) {
     return obn;
   }
 
-#define ID_NEW_REMAP_US(a) \
-  if ((a)->id.newid) { \
-    (a) = (void *)(a)->id.newid; \
-    (a)->id.us++; \
-  }
-#define ID_NEW_REMAP_US2(a) \
-  if (((ID *)a)->newid) { \
-    (a) = ((ID *)a)->newid; \
-    ((ID *)a)->us++; \
-  }
-
   /* duplicates using userflags */
   if (dupflag & USER_DUP_ACT) {
     BKE_animdata_copy_id_action(bmain, &obn->id, true);
@@ -1791,19 +1795,16 @@ Object *BKE_object_duplicate(Main *bmain, const Object *ob, const uint dupflag)
   if (dupflag & USER_DUP_MAT) {
     for (a = 0; a < obn->totcol; a++) {
       id = (ID *)obn->mat[a];
-      if (id) {
+      if (id && id->newid == NULL) {
         if (is_object_liboverride && ID_IS_LINKED(id)) {
           continue;
         }
-        ID_NEW_REMAP_US(obn->mat[a])
-        else
-        {
-          obn->mat[a] = ID_NEW_SET(obn->mat[a], BKE_material_copy(bmain, obn->mat[a]));
-          if (dupflag & USER_DUP_ACT) {
-            BKE_animdata_copy_id_action(bmain, &ob

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list