Sequencer: refactor clipboard copy to no longer increase user count.
[blender.git] / source / blender / blenkernel / intern / collection.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Dalai Felinto
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/blenkernel/intern/collection.c
24  *  \ingroup bke
25  */
26
27 #include <string.h>
28
29 #include "BLI_blenlib.h"
30 #include "BLI_ghash.h"
31 #include "BLI_iterator.h"
32 #include "BLI_listbase.h"
33 #include "BLI_math_base.h"
34 #include "BLI_threads.h"
35 #include "BLT_translation.h"
36 #include "BLI_string_utils.h"
37
38 #include "BKE_collection.h"
39 #include "BKE_icons.h"
40 #include "BKE_idprop.h"
41 #include "BKE_layer.h"
42 #include "BKE_library.h"
43 #include "BKE_main.h"
44 #include "BKE_object.h"
45 #include "BKE_rigidbody.h"
46 #include "BKE_scene.h"
47
48 #include "DNA_ID.h"
49 #include "DNA_collection_types.h"
50 #include "DNA_layer_types.h"
51 #include "DNA_object_types.h"
52 #include "DNA_scene_types.h"
53
54 #include "DEG_depsgraph.h"
55 #include "DEG_depsgraph_query.h"
56
57 #include "MEM_guardedalloc.h"
58
59 /******************************** Prototypes ********************************/
60
61 static bool collection_child_add(Collection *parent, Collection *collection, const int flag, const bool add_us);
62 static bool collection_child_remove(Collection *parent, Collection *collection);
63 static bool collection_object_add(Main *bmain, Collection *collection, Object *ob, int flag, const bool add_us);
64 static bool collection_object_remove(Main *bmain, Collection *collection, Object *ob, const bool free_us);
65
66 static CollectionChild *collection_find_child(Collection *parent, Collection *collection);
67 static CollectionParent *collection_find_parent(Collection *child, Collection *collection);
68
69 static bool collection_find_child_recursive(Collection *parent, Collection *collection);
70
71 /***************************** Add Collection *******************************/
72
73 /* Add new collection, without view layer syncing. */
74 static Collection *collection_add(Main *bmain, Collection *collection_parent, const char *name_custom)
75 {
76         /* Determine new collection name. */
77         char name[MAX_NAME];
78
79         if (name_custom) {
80                 STRNCPY(name, name_custom);
81         }
82         else {
83                 BKE_collection_new_name_get(collection_parent, name);
84         }
85
86         /* Create new collection. */
87         Collection *collection = BKE_libblock_alloc(bmain, ID_GR, name, 0);
88
89         /* We increase collection user count when linking to Collections. */
90         id_us_min(&collection->id);
91
92         /* Optionally add to parent collection. */
93         if (collection_parent) {
94                 collection_child_add(collection_parent, collection, 0, true);
95         }
96
97         return collection;
98 }
99
100 /**
101  * Add a collection to a collection ListBase and synchronize all render layers
102  * The ListBase is NULL when the collection is to be added to the master collection
103  */
104 Collection *BKE_collection_add(Main *bmain, Collection *collection_parent, const char *name_custom)
105 {
106         Collection *collection = collection_add(bmain, collection_parent, name_custom);
107         BKE_main_collection_sync(bmain);
108         return collection;
109 }
110
111 /*********************** Free and Delete Collection ****************************/
112
113 /** Free (or release) any data used by this collection (does not free the collection itself). */
114 void BKE_collection_free(Collection *collection)
115 {
116         /* No animdata here. */
117         BKE_previewimg_free(&collection->preview);
118
119         BLI_freelistN(&collection->gobject);
120         BLI_freelistN(&collection->children);
121         BLI_freelistN(&collection->parents);
122
123         BKE_collection_object_cache_free(collection);
124 }
125
126 /**
127  * Remove a collection, optionally removing its child objects or moving
128  * them to parent collections.
129  */
130 bool BKE_collection_delete(Main *bmain, Collection *collection, bool hierarchy)
131 {
132         /* Master collection is not real datablock, can't be removed. */
133         if (collection->flag & COLLECTION_IS_MASTER) {
134                 BLI_assert(!"Scene master collection can't be deleted");
135                 return false;
136         }
137
138         if (hierarchy) {
139                 /* Remove child objects. */
140                 CollectionObject *cob = collection->gobject.first;
141                 while (cob != NULL) {
142                         collection_object_remove(bmain, collection, cob->ob, true);
143                         cob = collection->gobject.first;
144                 }
145
146                 /* Delete all child collections recursively. */
147                 CollectionChild *child = collection->children.first;
148                 while (child != NULL) {
149                         BKE_collection_delete(bmain, child->collection, hierarchy);
150                         child = collection->children.first;
151                 }
152         }
153         else {
154                 /* Link child collections into parent collection. */
155                 for (CollectionChild *child = collection->children.first; child; child = child->next) {
156                         for (CollectionParent *cparent = collection->parents.first; cparent; cparent = cparent->next) {
157                                 Collection *parent = cparent->collection;
158                                 collection_child_add(parent, child->collection, 0, true);
159                         }
160                 }
161
162                 CollectionObject *cob = collection->gobject.first;
163                 while (cob != NULL) {
164                         /* Link child object into parent collections. */
165                         for (CollectionParent *cparent = collection->parents.first; cparent; cparent = cparent->next) {
166                                 Collection *parent = cparent->collection;
167                                 collection_object_add(bmain, parent, cob->ob, 0, true);
168                         }
169
170                         /* Remove child object. */
171                         collection_object_remove(bmain, collection, cob->ob, true);
172                         cob = collection->gobject.first;
173                 }
174         }
175
176         BKE_libblock_delete(bmain, collection);
177
178         BKE_main_collection_sync(bmain);
179
180         return true;
181 }
182
183 /***************************** Collection Copy *******************************/
184
185 /**
186  * Only copy internal data of Collection ID from source to already allocated/initialized destination.
187  * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs.
188  *
189  * WARNING! This function will not handle ID user count!
190  *
191  * \param flag: Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more).
192  */
193 void BKE_collection_copy_data(
194         Main *bmain, Collection *collection_dst, const Collection *collection_src, const int flag)
195 {
196         /* Do not copy collection's preview (same behavior as for objects). */
197         if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0 && false) {  /* XXX TODO temp hack */
198                 BKE_previewimg_id_copy(&collection_dst->id, &collection_src->id);
199         }
200         else {
201                 collection_dst->preview = NULL;
202         }
203
204         collection_dst->flag &= ~COLLECTION_HAS_OBJECT_CACHE;
205         BLI_listbase_clear(&collection_dst->object_cache);
206
207         BLI_listbase_clear(&collection_dst->gobject);
208         BLI_listbase_clear(&collection_dst->children);
209         BLI_listbase_clear(&collection_dst->parents);
210
211         for (CollectionChild *child = collection_src->children.first; child; child = child->next) {
212                 collection_child_add(collection_dst, child->collection, flag, false);
213         }
214         for (CollectionObject *cob = collection_src->gobject.first; cob; cob = cob->next) {
215                 collection_object_add(bmain, collection_dst, cob->ob, flag, false);
216         }
217 }
218
219 /**
220  * Makes a shallow copy of a Collection
221  *
222  * Add a new collection in the same level as the old one, copy any nested collections
223  * but link the objects to the new collection (as oppose to copy them).
224  */
225 Collection *BKE_collection_copy(Main *bmain, Collection *parent, Collection *collection)
226 {
227         /* It's not allowed to copy the master collection. */
228         if (collection->flag & COLLECTION_IS_MASTER) {
229                 BLI_assert("!Master collection can't be copied");
230                 return NULL;
231         }
232
233         Collection *collection_new;
234         BKE_id_copy_ex(bmain, &collection->id, (ID **)&collection_new, 0, false);
235         id_us_min(&collection_new->id);  /* Copying add one user by default, need to get rid of that one. */
236
237         /* Optionally add to parent. */
238         if (parent) {
239                 if (collection_child_add(parent, collection_new, 0, true)) {
240                         /* Put collection right after existing one. */
241                         CollectionChild *child = collection_find_child(parent, collection);
242                         CollectionChild *child_new = collection_find_child(parent, collection_new);
243
244                         if (child && child_new) {
245                                 BLI_remlink(&parent->children, child_new);
246                                 BLI_insertlinkafter(&parent->children, child, child_new);
247                         }
248                 }
249         }
250
251         BKE_main_collection_sync(bmain);
252
253         return collection_new;
254 }
255
256 Collection *BKE_collection_copy_master(Main *bmain, Collection *collection, const int flag)
257 {
258         BLI_assert(collection->flag & COLLECTION_IS_MASTER);
259
260         Collection *collection_dst = MEM_dupallocN(collection);
261         BKE_collection_copy_data(bmain, collection_dst, collection, flag);
262         return collection_dst;
263 }
264
265 void BKE_collection_copy_full(Main *UNUSED(bmain), Collection *UNUSED(collection))
266 {
267         // TODO: implement full scene copy
268 }
269
270 void BKE_collection_make_local(Main *bmain, Collection *collection, const bool lib_local)
271 {
272         BKE_id_make_local_generic(bmain, &collection->id, true, lib_local);
273 }
274
275 /********************************* Naming *******************************/
276
277 /**
278  * The automatic/fallback name of a new collection.
279  */
280 void BKE_collection_new_name_get(Collection *collection_parent, char *rname)
281 {
282         char *name;
283
284         if (!collection_parent) {
285                 name = BLI_sprintfN("Collection");
286         }
287         else if (collection_parent->flag & COLLECTION_IS_MASTER) {
288                 name = BLI_sprintfN("Collection %d", BLI_listbase_count(&collection_parent->children) + 1);
289         }
290         else {
291                 const int number = BLI_listbase_count(&collection_parent->children) + 1;
292                 const int digits = integer_digits_i(number);
293                 const int max_len =
294                         sizeof(collection_parent->id.name) - 1 /* NULL terminator */ - (1 + digits) /* " %d" */ - 2 /* ID */;
295                 name = BLI_sprintfN("%.*s %d", max_len, collection_parent->id.name + 2, number);
296         }
297
298         BLI_strncpy(rname, name, MAX_NAME);
299         MEM_freeN(name);
300 }
301
302 /**
303  * The name to show in the interface.
304  */
305 const char *BKE_collection_ui_name_get(struct Collection *collection)
306 {
307         if (collection->flag & COLLECTION_IS_MASTER) {
308                 return IFACE_("Scene Collection");
309         }
310         else {
311                 return collection->id.name + 2;
312         }
313 }
314
315 /* **************** Object List Cache *******************/
316
317 static void collection_object_cache_fill(ListBase *lb, Collection *collection, int parent_restrict)
318 {
319         int child_restrict = collection->flag | parent_restrict;
320
321         for (CollectionObject *cob = collection->gobject.first; cob; cob = cob->next) {
322                 Base *base = BLI_findptr(lb, cob->ob, offsetof(Base, object));
323
324                 if (base == NULL) {
325                         base = MEM_callocN(sizeof(Base), "Object Base");
326                         base->object = cob->ob;
327                         BLI_addtail(lb, base);
328                 }
329
330                 int object_restrict = base->object->restrictflag;
331
332                 if (((child_restrict & COLLECTION_RESTRICT_VIEW) == 0) &&
333                     ((object_restrict & OB_RESTRICT_VIEW) == 0))
334                 {
335                         base->flag |= BASE_ENABLED_VIEWPORT;
336                 }
337
338                 if (((child_restrict & COLLECTION_RESTRICT_RENDER) == 0) &&
339                     ((object_restrict & OB_RESTRICT_RENDER) == 0))
340                 {
341                         base->flag |= BASE_ENABLED_RENDER;
342                 }
343         }
344
345         for (CollectionChild *child = collection->children.first; child; child = child->next) {
346                 collection_object_cache_fill(lb, child->collection, child_restrict);
347         }
348 }
349
350 ListBase BKE_collection_object_cache_get(Collection *collection)
351 {
352         if (!(collection->flag & COLLECTION_HAS_OBJECT_CACHE)) {
353                 static ThreadMutex cache_lock = BLI_MUTEX_INITIALIZER;
354
355                 BLI_mutex_lock(&cache_lock);
356                 if (!(collection->flag & COLLECTION_HAS_OBJECT_CACHE)) {
357                         collection_object_cache_fill(&collection->object_cache, collection, 0);
358                         collection->flag |= COLLECTION_HAS_OBJECT_CACHE;
359                 }
360                 BLI_mutex_unlock(&cache_lock);
361         }
362
363         return collection->object_cache;
364 }
365
366 static void collection_object_cache_free(Collection *collection)
367 {
368         /* Clear own cache an for all parents, since those are affected by changes as well. */
369         collection->flag &= ~COLLECTION_HAS_OBJECT_CACHE;
370         BLI_freelistN(&collection->object_cache);
371
372         for (CollectionParent *parent = collection->parents.first; parent; parent = parent->next) {
373                 collection_object_cache_free(parent->collection);
374         }
375 }
376
377 void BKE_collection_object_cache_free(Collection *collection)
378 {
379         collection_object_cache_free(collection);
380 }
381
382 Base *BKE_collection_or_layer_objects(const ViewLayer *view_layer, Collection *collection)
383 {
384         if (collection) {
385                 return BKE_collection_object_cache_get(collection).first;
386         }
387         else {
388                 return FIRSTBASE(view_layer);
389         }
390 }
391
392 /*********************** Scene Master Collection ***************/
393
394 Collection *BKE_collection_master_add()
395 {
396         /* Not an actual datablock, but owned by scene. */
397         Collection *master_collection = MEM_callocN(sizeof(Collection), "Master Collection");
398         STRNCPY(master_collection->id.name, "GRMaster Collection");
399         master_collection->flag |= COLLECTION_IS_MASTER;
400         return master_collection;
401 }
402
403 Collection *BKE_collection_master(const Scene *scene)
404 {
405         return scene->master_collection;
406 }
407
408 /*********************** Cyclic Checks ************************/
409
410 static bool collection_object_cyclic_check_internal(Object *object, Collection *collection)
411 {
412         if (object->dup_group) {
413                 Collection *dup_collection = object->dup_group;
414                 if ((dup_collection->id.tag & LIB_TAG_DOIT) == 0) {
415                         /* Cycle already exists in collections, let's prevent further crappyness */
416                         return true;
417                 }
418                 /* flag the object to identify cyclic dependencies in further dupli collections */
419                 dup_collection->id.tag &= ~LIB_TAG_DOIT;
420
421                 if (dup_collection == collection) {
422                         return true;
423                 }
424                 else {
425                         FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(dup_collection, collection_object)
426                         {
427                                 if (collection_object_cyclic_check_internal(collection_object, dup_collection)) {
428                                         return true;
429                                 }
430                         }
431                         FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
432                 }
433
434                 /* un-flag the object, it's allowed to have the same collection multiple times in parallel */
435                 dup_collection->id.tag |= LIB_TAG_DOIT;
436         }
437
438         return false;
439 }
440
441 bool BKE_collection_object_cyclic_check(Main *bmain, Object *object, Collection *collection)
442 {
443         /* first flag all collections */
444         BKE_main_id_tag_listbase(&bmain->collection, LIB_TAG_DOIT, true);
445
446         return collection_object_cyclic_check_internal(object, collection);
447 }
448
449 /******************* Collection Object Membership *******************/
450
451 bool BKE_collection_has_object(Collection *collection, Object *ob)
452 {
453         if (ELEM(NULL, collection, ob)) {
454                 return false;
455         }
456
457         return (BLI_findptr(&collection->gobject, ob, offsetof(CollectionObject, ob)));
458 }
459
460 bool BKE_collection_has_object_recursive(Collection *collection, Object *ob)
461 {
462         if (ELEM(NULL, collection, ob)) {
463                 return false;
464         }
465
466         const ListBase objects = BKE_collection_object_cache_get(collection);
467         return (BLI_findptr(&objects, ob, offsetof(Base, object)));
468 }
469
470 Collection *BKE_collection_object_find(Main *bmain, Collection *collection, Object *ob)
471 {
472         if (collection)
473                 collection = collection->id.next;
474         else
475                 collection = bmain->collection.first;
476
477         while (collection) {
478                 if (BKE_collection_has_object(collection, ob))
479                         return collection;
480                 collection = collection->id.next;
481         }
482         return NULL;
483 }
484
485 bool BKE_collection_is_empty(Collection *collection)
486 {
487         return BLI_listbase_is_empty(&collection->gobject) && BLI_listbase_is_empty(&collection->children);
488 }
489
490 /********************** Collection Objects *********************/
491
492 static bool collection_object_add(Main *bmain, Collection *collection, Object *ob, int flag, const bool add_us)
493 {
494         if (ob->dup_group) {
495                 /* Cyclic dependency check. */
496                 if (collection_find_child_recursive(ob->dup_group, collection)) {
497                         return false;
498                 }
499         }
500
501         CollectionObject *cob = BLI_findptr(&collection->gobject, ob, offsetof(CollectionObject, ob));
502         if (cob) {
503                 return false;
504         }
505
506         cob = MEM_callocN(sizeof(CollectionObject), __func__);
507         cob->ob = ob;
508         BLI_addtail(&collection->gobject, cob);
509         BKE_collection_object_cache_free(collection);
510
511         if (add_us && (flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
512                 id_us_plus(&ob->id);
513         }
514
515         if ((flag & LIB_ID_CREATE_NO_MAIN) == 0) {
516                 DEG_id_tag_update_ex(bmain, &collection->id, ID_RECALC_COPY_ON_WRITE);
517         }
518
519         if ((flag & LIB_ID_CREATE_NO_MAIN) == 0) {
520                 BKE_rigidbody_main_collection_object_add(bmain, collection, ob);
521         }
522
523         return true;
524 }
525
526 static bool collection_object_remove(Main *bmain, Collection *collection, Object *ob, const bool free_us)
527 {
528         CollectionObject *cob = BLI_findptr(&collection->gobject, ob, offsetof(CollectionObject, ob));
529         if (cob == NULL) {
530                 return false;
531         }
532
533         BLI_freelinkN(&collection->gobject, cob);
534         BKE_collection_object_cache_free(collection);
535
536         if (free_us) {
537                 BKE_libblock_free_us(bmain, ob);
538         }
539         else {
540                 id_us_min(&ob->id);
541         }
542
543         DEG_id_tag_update_ex(bmain, &collection->id, ID_RECALC_COPY_ON_WRITE);
544
545         return true;
546 }
547
548 /**
549  * Add object to collection
550  */
551 bool BKE_collection_object_add(Main *bmain, Collection *collection, Object *ob)
552 {
553         if (ELEM(NULL, collection, ob)) {
554                 return false;
555         }
556
557         if (!collection_object_add(bmain, collection, ob, 0, true)) {
558                 return false;
559         }
560
561         if (BKE_collection_is_in_scene(collection)) {
562                 BKE_main_collection_sync(bmain);
563         }
564
565         return true;
566 }
567
568 /**
569  * Add object to all scene collections that reference objects is in
570  * (used to copy objects)
571  */
572 void BKE_collection_object_add_from(Main *bmain, Scene *scene, Object *ob_src, Object *ob_dst)
573 {
574         FOREACH_SCENE_COLLECTION_BEGIN(scene, collection)
575         {
576                 if (BKE_collection_has_object(collection, ob_src)) {
577                         collection_object_add(bmain, collection, ob_dst, 0, true);
578                 }
579         }
580         FOREACH_SCENE_COLLECTION_END;
581
582         BKE_main_collection_sync(bmain);
583 }
584
585 /**
586  * Remove object from collection.
587  */
588 bool BKE_collection_object_remove(Main *bmain, Collection *collection, Object *ob, const bool free_us)
589 {
590         if (ELEM(NULL, collection, ob)) {
591                 return false;
592         }
593
594         if (!collection_object_remove(bmain, collection, ob, free_us)) {
595                 return false;
596         }
597
598         if (BKE_collection_is_in_scene(collection)) {
599                 BKE_main_collection_sync(bmain);
600         }
601
602         return true;
603 }
604
605 /**
606  * Remove object from all collections of scene
607  * \param scene_collection_skip: Don't remove base from this collection.
608  */
609 static bool scene_collections_object_remove(Main *bmain, Scene *scene, Object *ob, const bool free_us,
610                                       Collection *collection_skip)
611 {
612         bool removed = false;
613
614         BKE_scene_remove_rigidbody_object(bmain, scene, ob);
615
616         FOREACH_SCENE_COLLECTION_BEGIN(scene, collection)
617         {
618                 if (collection != collection_skip) {
619                         removed |= collection_object_remove(bmain, collection, ob, free_us);
620                 }
621         }
622         FOREACH_SCENE_COLLECTION_END;
623
624         BKE_main_collection_sync(bmain);
625
626         return removed;
627 }
628
629 /**
630  * Remove object from all collections of scene
631  */
632 bool BKE_scene_collections_object_remove(Main *bmain, Scene *scene, Object *ob, const bool free_us)
633 {
634         return scene_collections_object_remove(bmain, scene, ob, free_us, NULL);
635 }
636
637 /*
638  * Remove all NULL objects from collections.
639  * This is used for library remapping, where these pointers have been set to NULL.
640  * Otherwise this should never happen.
641  */
642 static void collection_object_remove_nulls(Collection *collection)
643 {
644         bool changed = false;
645
646         for (CollectionObject *cob = collection->gobject.first, *cob_next = NULL; cob; cob = cob_next) {
647                 cob_next = cob->next;
648
649                 if (cob->ob == NULL) {
650                         BLI_freelinkN(&collection->gobject, cob);
651                         changed = true;
652                 }
653         }
654
655         if (changed) {
656                 BKE_collection_object_cache_free(collection);
657         }
658 }
659
660 void BKE_collections_object_remove_nulls(Main *bmain)
661 {
662         for (Scene *scene = bmain->scene.first; scene; scene = scene->id.next) {
663                 collection_object_remove_nulls(scene->master_collection);
664         }
665
666         for (Collection *collection = bmain->collection.first; collection; collection = collection->id.next) {
667                 collection_object_remove_nulls(collection);
668         }
669 }
670
671 static void collection_null_children_remove(Collection *collection)
672 {
673         for (CollectionChild *child = collection->children.first, *child_next = NULL; child; child = child_next) {
674                 child_next = child->next;
675
676                 if (child->collection == NULL) {
677                         BLI_freelinkN(&collection->children, child);
678                 }
679         }
680 }
681
682 static void collection_missing_parents_remove(Collection *collection)
683 {
684         for (CollectionParent *parent = collection->parents.first, *parent_next; parent != NULL; parent = parent_next) {
685                 parent_next = parent->next;
686                 if ((parent->collection == NULL) ||
687                     !collection_find_child(parent->collection, collection))
688                 {
689                         BLI_freelinkN(&collection->parents, parent);
690                 }
691         }
692 }
693
694 /**
695  * Remove all NULL children from parent collections of changed \a collection.
696  * This is used for library remapping, where these pointers have been set to NULL.
697  * Otherwise this should never happen.
698  *
699  * \note caller must ensure BKE_main_collection_sync_remap() is called afterwards!
700  *
701  * \param collection: may be \a NULL, in which case whole \a bmain database of collections is checked.
702  */
703 void BKE_collections_child_remove_nulls(Main *bmain, Collection *collection)
704 {
705         if (collection == NULL) {
706                 /* We need to do the checks in two steps when more than one collection may be involved,
707                  * otherwise we can miss some cases...
708                  * Also, master collections are not in bmain, so we also need to loop over scenes.
709                  */
710                 for (collection = bmain->collection.first; collection != NULL; collection = collection->id.next) {
711                         collection_null_children_remove(collection);
712                 }
713                 for (Scene *scene = bmain->scene.first; scene != NULL; scene = scene->id.next) {
714                         collection_null_children_remove(BKE_collection_master(scene));
715                 }
716
717                 for (collection = bmain->collection.first; collection != NULL; collection = collection->id.next) {
718                         collection_missing_parents_remove(collection);
719                 }
720                 for (Scene *scene = bmain->scene.first; scene != NULL; scene = scene->id.next) {
721                         collection_missing_parents_remove(BKE_collection_master(scene));
722                 }
723         }
724         else {
725                 for (CollectionParent *parent = collection->parents.first, *parent_next; parent; parent = parent_next) {
726                         parent_next = parent->next;
727
728                         collection_null_children_remove(parent->collection);
729
730                         if (!collection_find_child(parent->collection, collection)) {
731                                 BLI_freelinkN(&collection->parents, parent);
732                         }
733                 }
734         }
735 }
736
737 /**
738  * Move object from a collection into another
739  *
740  * If source collection is NULL move it from all the existing collections.
741  */
742 void BKE_collection_object_move(
743         Main *bmain, Scene *scene, Collection *collection_dst, Collection *collection_src, Object *ob)
744 {
745         /* In both cases we first add the object, then remove it from the other collections.
746          * Otherwise we lose the original base and whether it was active and selected. */
747         if (collection_src != NULL) {
748                 if (BKE_collection_object_add(bmain, collection_dst, ob)) {
749                         BKE_collection_object_remove(bmain, collection_src, ob, false);
750                 }
751         }
752         else {
753                 /* Adding will fail if object is already in collection.
754                  * However we still need to remove it from the other collections. */
755                 BKE_collection_object_add(bmain, collection_dst, ob);
756                 scene_collections_object_remove(bmain, scene, ob, false, collection_dst);
757         }
758 }
759
760 /***************** Collection Scene Membership ****************/
761
762 bool BKE_collection_is_in_scene(Collection *collection)
763 {
764         if (collection->flag & COLLECTION_IS_MASTER) {
765                 return true;
766         }
767
768         for (CollectionParent *cparent = collection->parents.first; cparent; cparent = cparent->next) {
769                 if (BKE_collection_is_in_scene(cparent->collection)) {
770                         return true;
771                 }
772         }
773
774         return false;
775 }
776
777 void BKE_collections_after_lib_link(Main *bmain)
778 {
779         /* Update view layer collections to match any changes in linked
780          * collections after file load. */
781         BKE_main_collection_sync(bmain);
782 }
783
784 /********************** Collection Children *******************/
785
786 bool BKE_collection_find_cycle(Collection *new_ancestor, Collection *collection)
787 {
788         if (collection == new_ancestor) {
789                 return true;
790         }
791
792         for (CollectionParent *parent = new_ancestor->parents.first; parent; parent = parent->next) {
793                 if (BKE_collection_find_cycle(parent->collection, collection)) {
794                         return true;
795                 }
796         }
797
798         return false;
799 }
800
801 static CollectionChild *collection_find_child(Collection *parent, Collection *collection)
802 {
803         return BLI_findptr(&parent->children, collection, offsetof(CollectionChild, collection));
804 }
805
806 static bool collection_find_child_recursive(Collection *parent, Collection *collection)
807 {
808         for (CollectionChild *child = parent->children.first; child; child = child->next) {
809                 if (child->collection == collection) {
810                         return true;
811                 }
812
813                 if (collection_find_child_recursive(child->collection, collection)) {
814                         return true;
815                 }
816         }
817
818         return false;
819 }
820
821 static CollectionParent *collection_find_parent(Collection *child, Collection *collection)
822 {
823         return BLI_findptr(&child->parents, collection, offsetof(CollectionParent, collection));
824 }
825
826 static bool collection_child_add(Collection *parent, Collection *collection, const int flag, const bool add_us)
827 {
828         CollectionChild *child = collection_find_child(parent, collection);
829         if (child) {
830                 return false;
831         }
832         if (BKE_collection_find_cycle(parent, collection)) {
833                 return false;
834         }
835
836         child = MEM_callocN(sizeof(CollectionChild), "CollectionChild");
837         child->collection = collection;
838         BLI_addtail(&parent->children, child);
839
840         /* Don't add parent links for depsgraph datablocks, these are not kept in sync. */
841         if ((flag & LIB_ID_CREATE_NO_MAIN) == 0) {
842                 CollectionParent *cparent = MEM_callocN(sizeof(CollectionParent), "CollectionParent");
843                 cparent->collection = parent;
844                 BLI_addtail(&collection->parents, cparent);
845         }
846
847         if (add_us) {
848                 id_us_plus(&collection->id);
849         }
850
851         BKE_collection_object_cache_free(parent);
852
853         return true;
854 }
855
856 static bool collection_child_remove(Collection *parent, Collection *collection)
857 {
858         CollectionChild *child = collection_find_child(parent, collection);
859         if (child == NULL) {
860                 return false;
861         }
862
863         CollectionParent *cparent = collection_find_parent(collection, parent);
864         BLI_freelinkN(&collection->parents, cparent);
865         BLI_freelinkN(&parent->children, child);
866
867         id_us_min(&collection->id);
868
869         BKE_collection_object_cache_free(parent);
870
871         return true;
872 }
873
874 bool BKE_collection_child_add(Main *bmain, Collection *parent, Collection *child)
875 {
876         if (!collection_child_add(parent, child, 0, true)) {
877                 return false;
878         }
879
880         BKE_main_collection_sync(bmain);
881         return true;
882 }
883
884 bool BKE_collection_child_remove(Main *bmain, Collection *parent, Collection *child)
885 {
886         if (!collection_child_remove(parent, child)) {
887                 return false;
888         }
889
890         BKE_main_collection_sync(bmain);
891         return true;
892 }
893
894 /********************** Collection index *********************/
895
896 static Collection *collection_from_index_recursive(Collection *collection, const int index, int *index_current)
897 {
898         if (index == (*index_current)) {
899                 return collection;
900         }
901
902         (*index_current)++;
903
904         for (CollectionChild *child = collection->children.first; child; child = child->next) {
905                 Collection *nested = collection_from_index_recursive(child->collection, index, index_current);
906                 if (nested != NULL) {
907                         return nested;
908                 }
909         }
910         return NULL;
911 }
912
913 /**
914  * Return Scene Collection for a given index.
915  *
916  * The index is calculated from top to bottom counting the children before the siblings.
917  */
918 Collection *BKE_collection_from_index(Scene *scene, const int index)
919 {
920         int index_current = 0;
921         Collection *master_collection = BKE_collection_master(scene);
922         return collection_from_index_recursive(master_collection, index, &index_current);
923 }
924
925 static bool collection_objects_select(ViewLayer *view_layer, Collection *collection, bool deselect)
926 {
927         bool changed = false;
928
929         if (collection->flag & COLLECTION_RESTRICT_SELECT) {
930                 return false;
931         }
932
933         for (CollectionObject *cob = collection->gobject.first; cob; cob = cob->next) {
934                 Base *base = BKE_view_layer_base_find(view_layer, cob->ob);
935
936                 if (base) {
937                         if (deselect) {
938                                 if (base->flag & BASE_SELECTED) {
939                                         base->flag &= ~BASE_SELECTED;
940                                         changed = true;
941                                 }
942                         }
943                         else {
944                                 if ((base->flag & BASE_SELECTABLE) && !(base->flag & BASE_SELECTED)) {
945                                         base->flag |= BASE_SELECTED;
946                                         changed = true;
947                                 }
948                         }
949                 }
950         }
951
952         for (CollectionChild *child = collection->children.first; child; child = child->next) {
953                 if (collection_objects_select(view_layer, collection, deselect)) {
954                         changed = true;
955                 }
956         }
957
958         return changed;
959 }
960
961 /**
962  * Select all the objects in this Collection (and its nested collections) for this ViewLayer.
963  * Return true if any object was selected.
964  */
965 bool BKE_collection_objects_select(ViewLayer *view_layer, Collection *collection, bool deselect)
966 {
967         LayerCollection *layer_collection = BKE_layer_collection_first_from_scene_collection(view_layer, collection);
968
969         if (layer_collection != NULL) {
970                 return BKE_layer_collection_objects_select(view_layer, layer_collection, deselect);
971         }
972         else {
973                 return collection_objects_select(view_layer, collection, deselect);
974         }
975 }
976
977 /***************** Collection move (outliner drag & drop) *********************/
978
979 bool BKE_collection_move(Main *bmain,
980                          Collection *to_parent,
981                          Collection *from_parent,
982                          Collection *relative,
983                          bool relative_after,
984                          Collection *collection)
985 {
986         if (collection->flag & COLLECTION_IS_MASTER) {
987                 return false;
988         }
989         if (BKE_collection_find_cycle(to_parent, collection)) {
990                 return false;
991         }
992
993         /* Move to new parent collection */
994         if (from_parent) {
995                 collection_child_remove(from_parent, collection);
996         }
997
998         collection_child_add(to_parent, collection, 0, true);
999
1000         /* Move to specified location under parent. */
1001         if (relative) {
1002                 CollectionChild *child = collection_find_child(to_parent, collection);
1003                 CollectionChild *relative_child = collection_find_child(to_parent, relative);
1004
1005                 if (relative_child) {
1006                         BLI_remlink(&to_parent->children, child);
1007
1008                         if (relative_after) {
1009                                 BLI_insertlinkafter(&to_parent->children, relative_child, child);
1010                         }
1011                         else {
1012                                 BLI_insertlinkbefore(&to_parent->children, relative_child, child);
1013                         }
1014
1015                         BKE_collection_object_cache_free(to_parent);
1016                 }
1017         }
1018
1019         BKE_main_collection_sync(bmain);
1020
1021         return true;
1022 }
1023
1024 /**************************** Iterators ******************************/
1025
1026 /* scene collection iteractor */
1027
1028 typedef struct CollectionsIteratorData {
1029         Scene *scene;
1030         void **array;
1031         int tot, cur;
1032 } CollectionsIteratorData;
1033
1034 static void scene_collection_callback(Collection *collection, BKE_scene_collections_Cb callback, void *data)
1035 {
1036         callback(collection, data);
1037
1038         for (CollectionChild *child = collection->children.first; child; child = child->next) {
1039                 scene_collection_callback(child->collection, callback, data);
1040         }
1041 }
1042
1043 static void scene_collections_count(Collection *UNUSED(collection), void *data)
1044 {
1045         int *tot = data;
1046         (*tot)++;
1047 }
1048
1049 static void scene_collections_build_array(Collection *collection, void *data)
1050 {
1051         Collection ***array = data;
1052         **array = collection;
1053         (*array)++;
1054 }
1055
1056 static void scene_collections_array(Scene *scene, Collection ***collections_array, int *tot)
1057 {
1058         Collection *collection;
1059         Collection **array;
1060
1061         *collections_array = NULL;
1062         *tot = 0;
1063
1064         if (scene == NULL) {
1065                 return;
1066         }
1067
1068         collection = BKE_collection_master(scene);
1069         BLI_assert(collection != NULL);
1070         scene_collection_callback(collection, scene_collections_count, tot);
1071
1072         if (*tot == 0)
1073                 return;
1074
1075         *collections_array = array = MEM_mallocN(sizeof(Collection *) * (*tot), "CollectionArray");
1076         scene_collection_callback(collection, scene_collections_build_array, &array);
1077 }
1078
1079 /**
1080  * Only use this in non-performance critical situations
1081  * (it iterates over all scene collections twice)
1082  */
1083 void BKE_scene_collections_iterator_begin(BLI_Iterator *iter, void *data_in)
1084 {
1085         Scene *scene = data_in;
1086         CollectionsIteratorData *data = MEM_callocN(sizeof(CollectionsIteratorData), __func__);
1087
1088         data->scene = scene;
1089         iter->data = data;
1090         iter->valid = true;
1091
1092         scene_collections_array(scene, (Collection ***)&data->array, &data->tot);
1093         BLI_assert(data->tot != 0);
1094
1095         data->cur = 0;
1096         iter->current = data->array[data->cur];
1097 }
1098
1099 void BKE_scene_collections_iterator_next(struct BLI_Iterator *iter)
1100 {
1101         CollectionsIteratorData *data = iter->data;
1102
1103         if (++data->cur < data->tot) {
1104                 iter->current = data->array[data->cur];
1105         }
1106         else {
1107                 iter->valid = false;
1108         }
1109 }
1110
1111 void BKE_scene_collections_iterator_end(struct BLI_Iterator *iter)
1112 {
1113         CollectionsIteratorData *data = iter->data;
1114
1115         if (data) {
1116                 if (data->array) {
1117                         MEM_freeN(data->array);
1118                 }
1119                 MEM_freeN(data);
1120         }
1121         iter->valid = false;
1122 }
1123
1124
1125 /* scene objects iteractor */
1126
1127 typedef struct SceneObjectsIteratorData {
1128         GSet *visited;
1129         CollectionObject *cob_next;
1130         BLI_Iterator scene_collection_iter;
1131 } SceneObjectsIteratorData;
1132
1133 void BKE_scene_objects_iterator_begin(BLI_Iterator *iter, void *data_in)
1134 {
1135         Scene *scene = data_in;
1136         SceneObjectsIteratorData *data = MEM_callocN(sizeof(SceneObjectsIteratorData), __func__);
1137         iter->data = data;
1138
1139         /* lookup list ot make sure each object is object called once */
1140         data->visited = BLI_gset_ptr_new(__func__);
1141
1142         /* we wrap the scenecollection iterator here to go over the scene collections */
1143         BKE_scene_collections_iterator_begin(&data->scene_collection_iter, scene);
1144
1145         Collection *collection = data->scene_collection_iter.current;
1146         data->cob_next = collection->gobject.first;
1147
1148         BKE_scene_objects_iterator_next(iter);
1149 }
1150
1151 /**
1152  * Ensures we only get each object once, even when included in several collections.
1153  */
1154 static CollectionObject *object_base_unique(GSet *gs, CollectionObject *cob)
1155 {
1156         for (; cob != NULL; cob = cob->next) {
1157                 Object *ob = cob->ob;
1158                 void **ob_key_p;
1159                 if (!BLI_gset_ensure_p_ex(gs, ob, &ob_key_p)) {
1160                         *ob_key_p = ob;
1161                         return cob;
1162                 }
1163         }
1164         return NULL;
1165 }
1166
1167 void BKE_scene_objects_iterator_next(BLI_Iterator *iter)
1168 {
1169         SceneObjectsIteratorData *data = iter->data;
1170         CollectionObject *cob = data->cob_next ? object_base_unique(data->visited, data->cob_next) : NULL;
1171
1172         if (cob) {
1173                 data->cob_next = cob->next;
1174                 iter->current = cob->ob;
1175         }
1176         else {
1177                 /* if this is the last object of this ListBase look at the next Collection */
1178                 Collection *collection;
1179                 BKE_scene_collections_iterator_next(&data->scene_collection_iter);
1180                 do {
1181                         collection = data->scene_collection_iter.current;
1182                         /* get the first unique object of this collection */
1183                         CollectionObject *new_cob = object_base_unique(data->visited, collection->gobject.first);
1184                         if (new_cob) {
1185                                 data->cob_next = new_cob->next;
1186                                 iter->current = new_cob->ob;
1187                                 return;
1188                         }
1189                         BKE_scene_collections_iterator_next(&data->scene_collection_iter);
1190                 } while (data->scene_collection_iter.valid);
1191
1192                 if (!data->scene_collection_iter.valid) {
1193                         iter->valid = false;
1194                 }
1195         }
1196 }
1197
1198 void BKE_scene_objects_iterator_end(BLI_Iterator *iter)
1199 {
1200         SceneObjectsIteratorData *data = iter->data;
1201         if (data) {
1202                 BKE_scene_collections_iterator_end(&data->scene_collection_iter);
1203                 BLI_gset_free(data->visited, NULL);
1204                 MEM_freeN(data);
1205         }
1206 }