[Bf-blender-cvs] [a3551ed8788] master: BKE main namemap: Add a way to clear all namemaps of a given Main.

Bastien Montagne noreply at git.blender.org
Mon Feb 6 19:29:27 CET 2023


Commit: a3551ed878858da34d77f1463b2a97f1ba7b7f5f
Author: Bastien Montagne
Date:   Mon Feb 6 19:20:45 2023 +0100
Branches: master
https://developer.blender.org/rBa3551ed878858da34d77f1463b2a97f1ba7b7f5f

BKE main namemap: Add a way to clear all namemaps of a given Main.

Existing `BKE_main_namemap_destroy` is too specific when a entire Main
needs to have its namemaps cleared, since it would not handle the
Library ones.

While in regular situation current code is fine, ID management code that
may also edit linked data needs a wider, simpler clearing tool.

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

M	source/blender/blenkernel/BKE_main_namemap.h
M	source/blender/blenkernel/intern/main_namemap.cc

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

diff --git a/source/blender/blenkernel/BKE_main_namemap.h b/source/blender/blenkernel/BKE_main_namemap.h
index d6f184b4b30..15fb285948d 100644
--- a/source/blender/blenkernel/BKE_main_namemap.h
+++ b/source/blender/blenkernel/BKE_main_namemap.h
@@ -29,6 +29,15 @@ struct UniqueName_Map;
 struct UniqueName_Map *BKE_main_namemap_create(void) ATTR_WARN_UNUSED_RESULT;
 void BKE_main_namemap_destroy(struct UniqueName_Map **r_name_map) ATTR_NONNULL();
 
+/**
+ * Destroy all name_maps in given bmain:
+ * - In bmain itself for local IDs.
+ * - In the split bmains in the list is any (for linked IDs in some cases, e.g. if called during
+ *   readfile code).
+ * - In all of the libraries IDs (for linked IDs).
+ */
+void BKE_main_namemap_clear(struct Main *bmain) ATTR_NONNULL();
+
 /**
  * Ensures the given name is unique within the given ID type.
  *
diff --git a/source/blender/blenkernel/intern/main_namemap.cc b/source/blender/blenkernel/intern/main_namemap.cc
index 5a8f90ea10a..85f4c813dc7 100644
--- a/source/blender/blenkernel/intern/main_namemap.cc
+++ b/source/blender/blenkernel/intern/main_namemap.cc
@@ -195,6 +195,22 @@ void BKE_main_namemap_destroy(struct UniqueName_Map **r_name_map)
   *r_name_map = nullptr;
 }
 
+void BKE_main_namemap_clear(Main *bmain)
+{
+  for (Main *bmain_iter = bmain; bmain_iter != nullptr; bmain_iter = bmain_iter->next) {
+    if (bmain_iter->name_map != nullptr) {
+      BKE_main_namemap_destroy(&bmain_iter->name_map);
+    }
+    for (Library *lib_iter = static_cast<Library *>(bmain_iter->libraries.first);
+         lib_iter != nullptr;
+         lib_iter = static_cast<Library *>(lib_iter->id.next)) {
+      if (lib_iter->runtime.name_map != nullptr) {
+        BKE_main_namemap_destroy(&lib_iter->runtime.name_map);
+      }
+    }
+  }
+}
+
 static void main_namemap_populate(UniqueName_Map *name_map, struct Main *bmain, ID *ignore_id)
 {
   BLI_assert_msg(name_map != nullptr, "name_map should not be null");



More information about the Bf-blender-cvs mailing list