[Bf-blender-cvs] [61e7026e272] temp-collection-objects-link-multiple: Added method to link multiple objects to a collection. Refactored the findptr method to use a gset to check whether the object is already in the collection.

Monique noreply at git.blender.org
Fri Oct 14 20:03:38 CEST 2022


Commit: 61e7026e272b5627df97d17ba2941336a4b9b241
Author: Monique
Date:   Fri Oct 14 11:07:57 2022 +0200
Branches: temp-collection-objects-link-multiple
https://developer.blender.org/rB61e7026e272b5627df97d17ba2941336a4b9b241

Added method to link multiple objects to a
collection. Refactored the findptr method to
use a gset to check whether the object is already
in the collection.

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

M	source/blender/blenkernel/BKE_collection.h
M	source/blender/blenkernel/intern/collection.c
M	source/blender/blenkernel/intern/object.cc
M	source/blender/blenloader/intern/versioning_280.c
M	source/blender/python/intern/bpy_rna_id_collection.c
M	source/blender/python/intern/bpy_rna_id_collection.h
M	source/blender/python/intern/bpy_rna_types_capi.c

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

diff --git a/source/blender/blenkernel/BKE_collection.h b/source/blender/blenkernel/BKE_collection.h
index dd7866d83e5..0b2603cc240 100644
--- a/source/blender/blenkernel/BKE_collection.h
+++ b/source/blender/blenkernel/BKE_collection.h
@@ -117,6 +117,15 @@ bool BKE_collection_object_add(struct Main *bmain,
                                struct Collection *collection,
                                struct Object *ob);
 
+/**
+ * Add multiple objects to given collection, ensuring this collection is 'editable' (i.e. local and
+ * not a liboverride), and finding a suitable parent one otherwise.
+ */
+bool BKE_collection_object_add_multiple(struct Main *bmain,
+                                        struct Collection *collection,
+                                        struct Object **objects,
+                                        int ob_len);
+
 /**
  * Add object to given collection, similar to #BKE_collection_object_add.
  *
@@ -126,7 +135,8 @@ bool BKE_collection_object_add(struct Main *bmain,
 bool BKE_collection_viewlayer_object_add(struct Main *bmain,
                                          const struct ViewLayer *view_layer,
                                          struct Collection *collection,
-                                         struct Object *ob);
+                                         struct Object **objects,
+                                         int objects_len);
 
 /**
  * Same as #BKE_collection_object_add, but unconditionally adds the object to the given collection.
@@ -135,7 +145,8 @@ bool BKE_collection_viewlayer_object_add(struct Main *bmain,
  */
 bool BKE_collection_object_add_notest(struct Main *bmain,
                                       struct Collection *collection,
-                                      struct Object *ob);
+                                      struct Object **objects,
+                                      int objects_len);
 /**
  * Add \a ob_dst to all scene collections that reference object \a ob_src is in.
  * Used for copying objects.
diff --git a/source/blender/blenkernel/intern/collection.c b/source/blender/blenkernel/intern/collection.c
index 751b5185e39..42b709104f3 100644
--- a/source/blender/blenkernel/intern/collection.c
+++ b/source/blender/blenkernel/intern/collection.c
@@ -59,8 +59,12 @@ static bool collection_child_add(Collection *parent,
                                  const int flag,
                                  const bool add_us);
 static bool collection_child_remove(Collection *parent, Collection *collection);
-static bool collection_object_add(
-    Main *bmain, Collection *collection, Object *ob, int flag, const bool add_us);
+static bool collection_object_add(Main *bmain,
+                                  Collection *collection,
+                                  Object **objects,
+                                  int objects_len,
+                                  int flag,
+                                  const bool add_us);
 static bool collection_object_remove(Main *bmain,
                                      Collection *collection,
                                      Object *ob,
@@ -125,7 +129,7 @@ static void collection_copy_data(Main *bmain, ID *id_dst, const ID *id_src, cons
     collection_child_add(collection_dst, child->collection, flag, false);
   }
   LISTBASE_FOREACH (CollectionObject *, cob, &collection_src->gobject) {
-    collection_object_add(bmain, collection_dst, cob->ob, flag, false);
+    collection_object_add(bmain, collection_dst, &cob->ob, 1, flag, false);
   }
 }
 
@@ -554,7 +558,7 @@ bool BKE_collection_delete(Main *bmain, Collection *collection, bool hierarchy)
       /* Link child object into parent collections. */
       LISTBASE_FOREACH (CollectionParent *, cparent, &collection->parents) {
         Collection *parent = cparent->collection;
-        collection_object_add(bmain, parent, cob->ob, 0, true);
+        collection_object_add(bmain, parent, &cob->ob, 1, 0, true);
       }
 
       /* Remove child object. */
@@ -657,7 +661,7 @@ static Collection *collection_duplicate_recursive(Main *bmain,
         continue;
       }
 
-      collection_object_add(bmain, collection_new, ob_new, 0, true);
+      collection_object_add(bmain, collection_new, &ob_new, 1, 0, true);
       collection_object_remove(bmain, collection_new, ob_old, false);
     }
   }
@@ -1066,40 +1070,65 @@ static Collection *collection_parent_editable_find_recursive(const ViewLayer *vi
   return NULL;
 }
 
-static bool collection_object_add(
-    Main *bmain, Collection *collection, Object *ob, int flag, const bool add_us)
-{
-  if (ob->instance_collection) {
-    /* Cyclic dependency check. */
-    if (collection_find_child_recursive(ob->instance_collection, collection) ||
-        ob->instance_collection == collection) {
-      return false;
+static bool collection_object_add(Main *bmain,
+                                  Collection *collection,
+                                  Object **objects,
+                                  int objects_len,
+                                  int flag,
+                                  const bool add_us)
+{
+  bool result = true;
+  GSet *collection_objects = NULL;
+
+  for (int i = 0; i < objects_len; i++) {
+    Object *ob = objects[i];
+    if (ob == NULL) {
+      result = false;
+      continue;
+    }
+    if (ob->instance_collection) {
+      /* Cyclic dependency check. */
+      if (collection_find_child_recursive(ob->instance_collection, collection) ||
+          ob->instance_collection == collection) {
+        result = false;
+        continue;
+      }
     }
-  }
 
-  CollectionObject *cob = BLI_findptr(&collection->gobject, ob, offsetof(CollectionObject, ob));
-  if (cob) {
-    return false;
-  }
+    if (collection_objects == NULL) {
+      collection_objects = BLI_gset_ptr_new(__func__);
+      LISTBASE_FOREACH (CollectionObject *, collection_object, &collection->gobject) {
+        BLI_gset_insert(collection_objects, collection_object->ob);
+      }
+    }
 
-  cob = MEM_callocN(sizeof(CollectionObject), __func__);
-  cob->ob = ob;
-  BLI_addtail(&collection->gobject, cob);
-  BKE_collection_object_cache_free(collection);
+    if (!BLI_gset_add(collection_objects, ob)) {
+      result = false;
+      continue;
+    }
 
-  if (add_us && (flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
-    id_us_plus(&ob->id);
-  }
+    CollectionObject *cob = MEM_callocN(sizeof(CollectionObject), __func__);
+    cob->ob = ob;
+    BLI_addtail(&collection->gobject, cob);
+    BKE_collection_object_cache_free(collection);
 
-  if ((flag & LIB_ID_CREATE_NO_MAIN) == 0) {
-    collection_tag_update_parent_recursive(bmain, collection, ID_RECALC_COPY_ON_WRITE);
-  }
+    if (add_us && (flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
+      id_us_plus(&ob->id);
+    }
 
-  if ((flag & LIB_ID_CREATE_NO_MAIN) == 0) {
-    BKE_rigidbody_main_collection_object_add(bmain, collection, ob);
+    if ((flag & LIB_ID_CREATE_NO_MAIN) == 0) {
+      collection_tag_update_parent_recursive(bmain, collection, ID_RECALC_COPY_ON_WRITE);
+    }
+
+    if ((flag & LIB_ID_CREATE_NO_MAIN) == 0) {
+      BKE_rigidbody_main_collection_object_add(bmain, collection, ob);
+    }
   }
 
-  return true;
+  if (collection_objects) {
+    BLI_gset_free(collection_objects, NULL);
+  }
+  return result;
 }
 
 static bool collection_object_remove(Main *bmain,
@@ -1127,9 +1156,12 @@ static bool collection_object_remove(Main *bmain,
   return true;
 }
 
-bool BKE_collection_object_add_notest(Main *bmain, Collection *collection, Object *ob)
+bool BKE_collection_object_add_notest(Main *bmain,
+                                      Collection *collection,
+                                      Object **objects,
+                                      int objects_len)
 {
-  if (ob == NULL) {
+  if (objects == NULL) {
     return false;
   }
 
@@ -1140,7 +1172,7 @@ bool BKE_collection_object_add_notest(Main *bmain, Collection *collection, Objec
     return false;
   }
 
-  if (!collection_object_add(bmain, collection, ob, 0, true)) {
+  if (!collection_object_add(bmain, collection, objects, objects_len, 0, true)) {
     return false;
   }
 
@@ -1155,13 +1187,22 @@ bool BKE_collection_object_add_notest(Main *bmain, Collection *collection, Objec
 
 bool BKE_collection_object_add(Main *bmain, Collection *collection, Object *ob)
 {
-  return BKE_collection_viewlayer_object_add(bmain, NULL, collection, ob);
+  return BKE_collection_object_add_multiple(bmain, collection, &ob, 1);
+}
+
+bool BKE_collection_object_add_multiple(Main *bmain,
+                                        Collection *collection,
+                                        Object **objects,
+                                        int objects_len)
+{
+  return BKE_collection_viewlayer_object_add(bmain, NULL, collection, objects, objects_len);
 }
 
 bool BKE_collection_viewlayer_object_add(Main *bmain,
                                          const ViewLayer *view_layer,
                                          Collection *collection,
-                                         Object *ob)
+                                         Object **objects,
+                                         int objects_len)
 {
   if (collection == NULL) {
     return false;
@@ -1173,7 +1214,7 @@ bool BKE_collection_viewlayer_object_add(Main *bmain,
     return false;
   }
 
-  return BKE_collection_object_add_notest(bmain, collection, ob);
+  return BKE_collection_object_add_notest(bmain, collection, objects, objects_len);
 }
 
 void BKE_collection_object_add_from(Main *bmain, Scene *scene, Object *ob_src, Object *ob_dst)
@@ -1183,7 +1224,7 @@ void BKE_collection_object_add_from(Main *bmain, Scene *scene, Object *ob_src, O
   FOREACH_SCENE_COLLECTION_BEGIN (scene, collection) {
     if (!ID_IS_LINKED(collection) && !ID_IS_OVERRIDE_LIBRARY(collection) &&
         BKE_collection_has_object(collection, ob_src)) {
-      collection_object_add(bmain, collection, ob_dst, 0, true);
+      collection_object_add(bmain, collection, &ob_dst, 1, 0, true);
       is_instantiated = true;
     }
   }
@@ -1192,7 +1233,7 @@ void BKE_collection_object_add_from(Main *bmain, Scene *scene, Object *ob_src, O
   if (!is_instantiated

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list