[Bf-blender-cvs] [cddb7920210] master: ID management: Add new version of `relink_to_newid` using proper new remapping code.

Bastien Montagne noreply at git.blender.org
Tue Sep 14 18:07:32 CEST 2021


Commit: cddb7920210f10b4c4b8595f80499cbbc5d5f57c
Author: Bastien Montagne
Date:   Tue Sep 14 17:43:41 2021 +0200
Branches: master
https://developer.blender.org/rBcddb7920210f10b4c4b8595f80499cbbc5d5f57c

ID management: Add new version of `relink_to_newid` using proper new remapping code.

Current `BKE_libblock_relink_to_newid` is using its own simplistic,
limited and not really correct version of ID remapping.

While doing a full replacement would have been ideal, this is
risky/time-constrained for Blender 3.0 release, so for now we'll have
both versions co-existing.

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

M	source/blender/blenkernel/BKE_lib_remap.h
M	source/blender/blenkernel/intern/lib_remap.c

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

diff --git a/source/blender/blenkernel/BKE_lib_remap.h b/source/blender/blenkernel/BKE_lib_remap.h
index c90a284c204..c70521f9593 100644
--- a/source/blender/blenkernel/BKE_lib_remap.h
+++ b/source/blender/blenkernel/BKE_lib_remap.h
@@ -112,6 +112,7 @@ void BKE_libblock_relink_ex(struct Main *bmain,
                             const short remap_flags) ATTR_NONNULL(1, 2);
 
 void BKE_libblock_relink_to_newid(struct ID *id) ATTR_NONNULL();
+void BKE_libblock_relink_to_newid_new(struct Main *bmain, struct ID *id) ATTR_NONNULL();
 
 typedef void (*BKE_library_free_notifier_reference_cb)(const void *);
 typedef void (*BKE_library_remap_editor_id_reference_cb)(struct ID *, struct ID *);
diff --git a/source/blender/blenkernel/intern/lib_remap.c b/source/blender/blenkernel/intern/lib_remap.c
index bba15a3bcdf..250b8d4d515 100644
--- a/source/blender/blenkernel/intern/lib_remap.c
+++ b/source/blender/blenkernel/intern/lib_remap.c
@@ -699,6 +699,9 @@ static int id_relink_to_newid_looper(LibraryIDLinkCallbackData *cb_data)
  *
  * Very specific usage, not sure we'll keep it on the long run,
  * currently only used in Object/Collection duplication code...
+ *
+ * WARNING: This is a deprecated version of this function, should not be used by new code. See
+ * #BKE_libblock_relink_to_newid_new below.
  */
 void BKE_libblock_relink_to_newid(ID *id)
 {
@@ -708,3 +711,53 @@ void BKE_libblock_relink_to_newid(ID *id)
 
   BKE_library_foreach_ID_link(NULL, id, id_relink_to_newid_looper, NULL, 0);
 }
+
+/* ************************
+ * FIXME: Port all usages of #BKE_libblock_relink_to_newid to this
+ *        #BKE_libblock_relink_to_newid_new new code and remove old one.
+ ************************** */
+static int id_relink_to_newid_looper_new(LibraryIDLinkCallbackData *cb_data)
+{
+  const int cb_flag = cb_data->cb_flag;
+  if (cb_flag & IDWALK_CB_EMBEDDED) {
+    return IDWALK_RET_NOP;
+  }
+
+  Main *bmain = cb_data->bmain;
+  ID *id_owner = cb_data->id_owner;
+  ID **id_pointer = cb_data->id_pointer;
+  ID *id = *id_pointer;
+  if (id) {
+    /* See: NEW_ID macro */
+    if (id->newid != NULL) {
+      BKE_libblock_relink_ex(bmain, id_owner, id, id->newid, ID_REMAP_SKIP_INDIRECT_USAGE);
+      id = id->newid;
+    }
+    if (id->tag & LIB_TAG_NEW) {
+      id->tag &= ~LIB_TAG_NEW;
+      BKE_libblock_relink_to_newid_new(bmain, id);
+    }
+  }
+  return IDWALK_RET_NOP;
+}
+
+/**
+ * Remaps ID usages of given ID to their `id->newid` pointer if not None, and proceeds recursively
+ * in the dependency tree of IDs for all data-blocks tagged with `LIB_TAG_NEW`.
+ *
+ * NOTE: `LIB_TAG_NEW` is cleared
+ *
+ * Very specific usage, not sure we'll keep it on the long run,
+ * currently only used in Object/Collection duplication code...
+ */
+void BKE_libblock_relink_to_newid_new(Main *bmain, ID *id)
+{
+  if (ID_IS_LINKED(id)) {
+    return;
+  }
+  /* We do not want to have those cached relationship data here. */
+  BLI_assert(bmain->relations == NULL);
+
+  id->tag &= ~LIB_TAG_NEW;
+  BKE_library_foreach_ID_link(bmain, id, id_relink_to_newid_looper_new, NULL, 0);
+}



More information about the Bf-blender-cvs mailing list