From fe1e2c2f89b54302b213621f6ffd2b6089016155 Mon Sep 17 00:00:00 2001 From: Dalai Felinto Date: Fri, 22 Dec 2017 19:15:06 -0200 Subject: [PATCH] Collections: deletea collection move objects to master collection if users=0 The mental model is that a scene collection is a small wrap on top of the master collection, so all objects are in the master collection at all times. When we remove a collection there is no reason to remove an object. So if the object was not linked to any other collection, we add link it to the master one. --- source/blender/blenkernel/intern/collection.c | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/source/blender/blenkernel/intern/collection.c b/source/blender/blenkernel/intern/collection.c index f8934184928..ea8dbc64c9f 100644 --- a/source/blender/blenkernel/intern/collection.c +++ b/source/blender/blenkernel/intern/collection.c @@ -175,6 +175,8 @@ static void layer_collection_remove(ViewLayer *view_layer, ListBase *lb, const S /** * Remove a collection from the scene, and syncronize all render layers + * + * If an object is in any other collection, link the object to the master collection. */ bool BKE_collection_remove(ID *owner_id, SceneCollection *sc) { @@ -190,6 +192,36 @@ bool BKE_collection_remove(ID *owner_id, SceneCollection *sc) BLI_assert(false); } + /* If an object is no longer in any collection, we add it to the master collection. */ + ListBase collection_objects; + BLI_duplicatelist(&collection_objects, &sc->objects); + + FOREACH_SCENE_COLLECTION(owner_id, scene_collection_iter) + { + if (scene_collection_iter == sc) { + continue; + } + + LinkData *link_next, *link = collection_objects.first; + while (link) { + link_next = link->next; + + if (BLI_findptr(&scene_collection_iter->objects, link->data, offsetof(LinkData, data))) { + BLI_remlink(&collection_objects, link); + MEM_freeN(link); + } + + link = link_next; + } + } + FOREACH_SCENE_COLLECTION_END + + for (LinkData *link = collection_objects.first; link; link = link->next) { + BKE_collection_object_add(owner_id, sc_master, link->data); + } + + BLI_freelistN(&collection_objects); + /* Clear the collection items. */ collection_free(sc, true);