Blender  V2.93
collection.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 /* Allow using deprecated functionality for .blend file I/O. */
22 #define DNA_DEPRECATED_ALLOW
23 
24 #include <string.h>
25 
26 #include "BLI_blenlib.h"
27 #include "BLI_iterator.h"
28 #include "BLI_listbase.h"
29 #include "BLI_math_base.h"
30 #include "BLI_threads.h"
31 #include "BLT_translation.h"
32 
33 #include "BKE_anim_data.h"
34 #include "BKE_collection.h"
35 #include "BKE_icons.h"
36 #include "BKE_idprop.h"
37 #include "BKE_idtype.h"
38 #include "BKE_layer.h"
39 #include "BKE_lib_id.h"
40 #include "BKE_lib_query.h"
41 #include "BKE_lib_remap.h"
42 #include "BKE_main.h"
43 #include "BKE_object.h"
44 #include "BKE_rigidbody.h"
45 #include "BKE_scene.h"
46 
47 #include "DNA_defaults.h"
48 
49 #include "DNA_ID.h"
50 #include "DNA_collection_types.h"
51 #include "DNA_layer_types.h"
52 #include "DNA_object_types.h"
53 #include "DNA_rigidbody_types.h"
54 #include "DNA_scene_types.h"
55 
56 #include "DEG_depsgraph.h"
57 #include "DEG_depsgraph_query.h"
58 
59 #include "MEM_guardedalloc.h"
60 
61 #include "BLO_read_write.h"
62 
63 /* -------------------------------------------------------------------- */
67 static bool collection_child_add(Collection *parent,
68  Collection *collection,
69  const int flag,
70  const bool add_us);
71 static bool collection_child_remove(Collection *parent, Collection *collection);
72 static bool collection_object_add(
73  Main *bmain, Collection *collection, Object *ob, int flag, const bool add_us);
74 static bool collection_object_remove(Main *bmain,
75  Collection *collection,
76  Object *ob,
77  const bool free_us);
78 
79 static CollectionChild *collection_find_child(Collection *parent, Collection *collection);
81 
82 static bool collection_find_child_recursive(Collection *parent, Collection *collection);
83 
86 /* -------------------------------------------------------------------- */
90 static void collection_init_data(ID *id)
91 {
92  Collection *collection = (Collection *)id;
93  BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(collection, id));
94 
96 }
97 
108 static void collection_copy_data(Main *bmain, ID *id_dst, const ID *id_src, const int flag)
109 {
110  Collection *collection_dst = (Collection *)id_dst;
111  const Collection *collection_src = (const Collection *)id_src;
112 
113  BLI_assert(((collection_src->flag & COLLECTION_IS_MASTER) != 0) ==
114  ((collection_src->id.flag & LIB_EMBEDDED_DATA) != 0));
115 
116  /* Do not copy collection's preview (same behavior as for objects). */
117  if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0 && false) { /* XXX TODO temp hack */
118  BKE_previewimg_id_copy(&collection_dst->id, &collection_src->id);
119  }
120  else {
121  collection_dst->preview = NULL;
122  }
123 
124  collection_dst->flag &= ~COLLECTION_HAS_OBJECT_CACHE;
125  collection_dst->flag &= ~COLLECTION_HAS_OBJECT_CACHE_INSTANCED;
126  BLI_listbase_clear(&collection_dst->object_cache);
127  BLI_listbase_clear(&collection_dst->object_cache_instanced);
128 
129  BLI_listbase_clear(&collection_dst->gobject);
130  BLI_listbase_clear(&collection_dst->children);
131  BLI_listbase_clear(&collection_dst->parents);
132 
133  LISTBASE_FOREACH (CollectionChild *, child, &collection_src->children) {
134  collection_child_add(collection_dst, child->collection, flag, false);
135  }
136  LISTBASE_FOREACH (CollectionObject *, cob, &collection_src->gobject) {
137  collection_object_add(bmain, collection_dst, cob->ob, flag, false);
138  }
139 }
140 
141 static void collection_free_data(ID *id)
142 {
143  Collection *collection = (Collection *)id;
144 
145  /* No animdata here. */
146  BKE_previewimg_free(&collection->preview);
147 
148  BLI_freelistN(&collection->gobject);
149  BLI_freelistN(&collection->children);
150  BLI_freelistN(&collection->parents);
151 
153 }
154 
156 {
157  Collection *collection = (Collection *)id;
158 
159  LISTBASE_FOREACH (CollectionObject *, cob, &collection->gobject) {
161  }
162  LISTBASE_FOREACH (CollectionChild *, child, &collection->children) {
164  }
165  LISTBASE_FOREACH (CollectionParent *, parent, &collection->parents) {
166  /* XXX This is very weak. The whole idea of keeping pointers to private IDs is very bad
167  * anyway... */
168  const int cb_flag = ((parent->collection != NULL &&
169  (parent->collection->id.flag & LIB_EMBEDDED_DATA) != 0) ?
171  IDWALK_CB_NOP);
173  data, parent->collection, IDWALK_CB_NEVER_SELF | IDWALK_CB_LOOPBACK | cb_flag);
174  }
175 }
176 
177 static ID *collection_owner_get(Main *bmain, ID *id)
178 {
179  if ((id->flag & LIB_EMBEDDED_DATA) == 0) {
180  return id;
181  }
182  BLI_assert((id->tag & LIB_TAG_NO_MAIN) == 0);
183 
184  Collection *master_collection = (Collection *)id;
185  BLI_assert((master_collection->flag & COLLECTION_IS_MASTER) != 0);
186 
187  for (Scene *scene = bmain->scenes.first; scene != NULL; scene = scene->id.next) {
188  if (scene->master_collection == master_collection) {
189  return &scene->id;
190  }
191  }
192 
193  BLI_assert(!"Embedded collection with no owner. Critical Main inconsistency.");
194  return NULL;
195 }
196 
198 {
199  BKE_id_blend_write(writer, &collection->id);
200 
201  /* Shared function for collection data-blocks and scene master collection. */
202  BKE_previewimg_blend_write(writer, collection->preview);
203 
204  LISTBASE_FOREACH (CollectionObject *, cob, &collection->gobject) {
205  BLO_write_struct(writer, CollectionObject, cob);
206  }
207 
208  LISTBASE_FOREACH (CollectionChild *, child, &collection->children) {
209  BLO_write_struct(writer, CollectionChild, child);
210  }
211 }
212 
213 static void collection_blend_write(BlendWriter *writer, ID *id, const void *id_address)
214 {
215  Collection *collection = (Collection *)id;
216  if (collection->id.us > 0 || BLO_write_is_undo(writer)) {
217  /* Clean up, important in undo case to reduce false detection of changed data-blocks. */
218  collection->flag &= ~COLLECTION_HAS_OBJECT_CACHE;
220  collection->tag = 0;
221  BLI_listbase_clear(&collection->object_cache);
223  BLI_listbase_clear(&collection->parents);
224 
225  /* write LibData */
226  BLO_write_id_struct(writer, Collection, id_address, &collection->id);
227 
228  BKE_collection_blend_write_nolib(writer, collection);
229  }
230 }
231 
232 #ifdef USE_COLLECTION_COMPAT_28
234 {
235  BLO_read_list(reader, &sc->objects);
236  BLO_read_list(reader, &sc->scene_collections);
237 
240  }
241 }
242 #endif
243 
245 {
246  BLO_read_list(reader, &collection->gobject);
247  BLO_read_list(reader, &collection->children);
248 
249  BLO_read_data_address(reader, &collection->preview);
250  BKE_previewimg_blend_read(reader, collection->preview);
251 
252  collection->flag &= ~COLLECTION_HAS_OBJECT_CACHE;
254  collection->tag = 0;
255  BLI_listbase_clear(&collection->object_cache);
257  BLI_listbase_clear(&collection->parents);
258 
259 #ifdef USE_COLLECTION_COMPAT_28
260  /* This runs before the very first doversion. */
261  BLO_read_data_address(reader, &collection->collection);
262  if (collection->collection != NULL) {
263  BKE_collection_compat_blend_read_data(reader, collection->collection);
264  }
265 
266  BLO_read_data_address(reader, &collection->view_layer);
267  if (collection->view_layer != NULL) {
268  BKE_view_layer_blend_read_data(reader, collection->view_layer);
269  }
270 #endif
271 }
272 
274 {
275  Collection *collection = (Collection *)id;
276  BKE_collection_blend_read_data(reader, collection);
277 }
278 
279 static void lib_link_collection_data(BlendLibReader *reader, Library *lib, Collection *collection)
280 {
281  LISTBASE_FOREACH_MUTABLE (CollectionObject *, cob, &collection->gobject) {
282  BLO_read_id_address(reader, lib, &cob->ob);
283 
284  if (cob->ob == NULL) {
285  BLI_freelinkN(&collection->gobject, cob);
286  }
287  }
288 
289  LISTBASE_FOREACH (CollectionChild *, child, &collection->children) {
290  BLO_read_id_address(reader, lib, &child->collection);
291  }
292 }
293 
294 #ifdef USE_COLLECTION_COMPAT_28
296  Library *lib,
297  SceneCollection *sc)
298 {
299  LISTBASE_FOREACH (LinkData *, link, &sc->objects) {
300  BLO_read_id_address(reader, lib, &link->data);
301  BLI_assert(link->data);
302  }
303 
306  }
307 }
308 #endif
309 
311 {
312 #ifdef USE_COLLECTION_COMPAT_28
313  if (collection->collection) {
314  BKE_collection_compat_blend_read_lib(reader, collection->id.lib, collection->collection);
315  }
316 
317  if (collection->view_layer) {
318  BKE_view_layer_blend_read_lib(reader, collection->id.lib, collection->view_layer);
319  }
320 #endif
321 
322  lib_link_collection_data(reader, collection->id.lib, collection);
323 }
324 
325 static void collection_blend_read_lib(BlendLibReader *reader, ID *id)
326 {
327  Collection *collection = (Collection *)id;
328  BKE_collection_blend_read_lib(reader, collection);
329 }
330 
331 #ifdef USE_COLLECTION_COMPAT_28
333  struct SceneCollection *sc)
334 {
335  LISTBASE_FOREACH (LinkData *, link, &sc->objects) {
336  BLO_expand(expander, link->data);
337  }
338 
341  }
342 }
343 #endif
344 
346 {
347  LISTBASE_FOREACH (CollectionObject *, cob, &collection->gobject) {
348  BLO_expand(expander, cob->ob);
349  }
350 
351  LISTBASE_FOREACH (CollectionChild *, child, &collection->children) {
352  BLO_expand(expander, child->collection);
353  }
354 
355 #ifdef USE_COLLECTION_COMPAT_28
356  if (collection->collection != NULL) {
357  BKE_collection_compat_blend_read_expand(expander, collection->collection);
358  }
359 #endif
360 }
361 
362 static void collection_blend_read_expand(BlendExpander *expander, ID *id)
363 {
364  Collection *collection = (Collection *)id;
365  BKE_collection_blend_read_expand(expander, collection);
366 }
367 
369  .id_code = ID_GR,
370  .id_filter = FILTER_ID_GR,
371  .main_listbase_index = INDEX_ID_GR,
372  .struct_size = sizeof(Collection),
373  .name = "Collection",
374  .name_plural = "collections",
375  .translation_context = BLT_I18NCONTEXT_ID_COLLECTION,
376  .flags = IDTYPE_FLAGS_NO_ANIMDATA,
377 
379  .copy_data = collection_copy_data,
380  .free_data = collection_free_data,
381  .make_local = NULL,
382  .foreach_id = collection_foreach_id,
383  .foreach_cache = NULL,
384  .owner_get = collection_owner_get,
385 
386  .blend_write = collection_blend_write,
387  .blend_read_data = collection_blend_read_data,
388  .blend_read_lib = collection_blend_read_lib,
389  .blend_read_expand = collection_blend_read_expand,
390 
391  .blend_read_undo_preserve = NULL,
392 
393  .lib_override_apply_post = NULL,
394 };
395 
398 /* -------------------------------------------------------------------- */
402 /* Add new collection, without view layer syncing. */
404  Collection *collection_parent,
405  const char *name_custom)
406 {
407  /* Determine new collection name. */
408  char name[MAX_NAME];
409 
410  if (name_custom) {
411  STRNCPY(name, name_custom);
412  }
413  else {
414  BKE_collection_new_name_get(collection_parent, name);
415  }
416 
417  /* Create new collection. */
418  Collection *collection = BKE_id_new(bmain, ID_GR, name);
419 
420  /* We increase collection user count when linking to Collections. */
421  id_us_min(&collection->id);
422 
423  /* Optionally add to parent collection. */
424  if (collection_parent) {
425  collection_child_add(collection_parent, collection, 0, true);
426  }
427 
428  return collection;
429 }
430 
435 Collection *BKE_collection_add(Main *bmain, Collection *collection_parent, const char *name_custom)
436 {
437  Collection *collection = collection_add(bmain, collection_parent, name_custom);
439  return collection;
440 }
441 
449  Scene *scene,
450  const Object *ob_src,
451  Collection *collection_dst)
452 {
453  bool is_instantiated = false;
454 
455  FOREACH_SCENE_COLLECTION_BEGIN (scene, collection) {
456  if (!ID_IS_LINKED(collection) && BKE_collection_has_object(collection, ob_src)) {
457  collection_child_add(collection, collection_dst, 0, true);
458  is_instantiated = true;
459  }
460  }
462 
463  if (!is_instantiated) {
464  collection_child_add(scene->master_collection, collection_dst, 0, true);
465  }
466 
468 }
469 
477  Scene *scene,
478  Collection *collection_src,
479  Collection *collection_dst)
480 {
481  bool is_instantiated = false;
482 
483  FOREACH_SCENE_COLLECTION_BEGIN (scene, collection) {
484  if (!ID_IS_LINKED(collection) && collection_find_child(collection, collection_src)) {
485  collection_child_add(collection, collection_dst, 0, true);
486  is_instantiated = true;
487  }
488  else if (!is_instantiated && collection_find_child(collection, collection_dst)) {
489  /* If given collection_dst is already instantiated in scene, even if its 'model' src one is
490  * not, do not add it to master scene collection. */
491  is_instantiated = true;
492  }
493  }
495 
496  if (!is_instantiated) {
497  collection_child_add(scene->master_collection, collection_dst, 0, true);
498  }
499 
501 }
502 
505 /* -------------------------------------------------------------------- */
511 {
512  BKE_libblock_free_data(&collection->id, false);
513  collection_free_data(&collection->id);
514 }
515 
520 bool BKE_collection_delete(Main *bmain, Collection *collection, bool hierarchy)
521 {
522  /* Master collection is not real datablock, can't be removed. */
523  if (collection->flag & COLLECTION_IS_MASTER) {
524  BLI_assert(!"Scene master collection can't be deleted");
525  return false;
526  }
527 
528  if (hierarchy) {
529  /* Remove child objects. */
530  CollectionObject *cob = collection->gobject.first;
531  while (cob != NULL) {
532  collection_object_remove(bmain, collection, cob->ob, true);
533  cob = collection->gobject.first;
534  }
535 
536  /* Delete all child collections recursively. */
537  CollectionChild *child = collection->children.first;
538  while (child != NULL) {
539  BKE_collection_delete(bmain, child->collection, hierarchy);
540  child = collection->children.first;
541  }
542  }
543  else {
544  /* Link child collections into parent collection. */
545  LISTBASE_FOREACH (CollectionChild *, child, &collection->children) {
546  LISTBASE_FOREACH (CollectionParent *, cparent, &collection->parents) {
547  Collection *parent = cparent->collection;
548  collection_child_add(parent, child->collection, 0, true);
549  }
550  }
551 
552  CollectionObject *cob = collection->gobject.first;
553  while (cob != NULL) {
554  /* Link child object into parent collections. */
555  LISTBASE_FOREACH (CollectionParent *, cparent, &collection->parents) {
556  Collection *parent = cparent->collection;
557  collection_object_add(bmain, parent, cob->ob, 0, true);
558  }
559 
560  /* Remove child object. */
561  collection_object_remove(bmain, collection, cob->ob, true);
562  cob = collection->gobject.first;
563  }
564  }
565 
566  BKE_id_delete(bmain, collection);
567 
569 
570  return true;
571 }
572 
575 /* -------------------------------------------------------------------- */
580  Collection *parent,
581  Collection *collection_old,
582  const eDupli_ID_Flags duplicate_flags,
583  const eLibIDDuplicateFlags duplicate_options)
584 {
585  Collection *collection_new;
586  bool do_full_process = false;
587  const bool is_collection_master = (collection_old->flag & COLLECTION_IS_MASTER) != 0;
588 
589  const bool do_objects = (duplicate_flags & USER_DUP_OBJECT) != 0;
590 
591  if (is_collection_master) {
592  /* We never duplicate master collections here, but we can still deep-copy their objects and
593  * collections. */
594  BLI_assert(parent == NULL);
595  collection_new = collection_old;
596  do_full_process = true;
597  }
598  else if (collection_old->id.newid == NULL) {
599  collection_new = (Collection *)BKE_id_copy_for_duplicate(
600  bmain, (ID *)collection_old, duplicate_flags);
601 
602  if (collection_new == collection_old) {
603  return collection_new;
604  }
605 
606  do_full_process = true;
607  }
608  else {
609  collection_new = (Collection *)collection_old->id.newid;
610  }
611 
612  /* Optionally add to parent (we always want to do that,
613  * even if collection_old had already been duplicated). */
614  if (parent != NULL) {
615  if (collection_child_add(parent, collection_new, 0, true)) {
616  /* Put collection right after existing one. */
617  CollectionChild *child = collection_find_child(parent, collection_old);
618  CollectionChild *child_new = collection_find_child(parent, collection_new);
619 
620  if (child && child_new) {
621  BLI_remlink(&parent->children, child_new);
622  BLI_insertlinkafter(&parent->children, child, child_new);
623  }
624  }
625  }
626 
627  /* If we are not doing any kind of deep-copy, we can return immediately.
628  * False do_full_process means collection_old had already been duplicated,
629  * no need to redo some deep-copy on it. */
630  if (!do_full_process) {
631  return collection_new;
632  }
633 
634  if (do_objects) {
635  /* We need to first duplicate the objects in a separate loop, to support the master collection
636  * case, where both old and new collections are the same.
637  * Otherwise, depending on naming scheme and sorting, we may end up duplicating the new objects
638  * we just added, in some infinite loop. */
639  LISTBASE_FOREACH (CollectionObject *, cob, &collection_old->gobject) {
640  Object *ob_old = cob->ob;
641 
642  if (ob_old->id.newid == NULL) {
644  bmain, ob_old, duplicate_flags, duplicate_options | LIB_ID_DUPLICATE_IS_SUBPROCESS);
645  }
646  }
647 
648  /* We can loop on collection_old's objects, but have to consider it mutable because with master
649  * collections collection_old and collection_new are the same data here. */
650  LISTBASE_FOREACH_MUTABLE (CollectionObject *, cob, &collection_old->gobject) {
651  Object *ob_old = cob->ob;
652  Object *ob_new = (Object *)ob_old->id.newid;
653 
654  /* New object can be NULL in master collection case, since new and old objects are in same
655  * collection. */
656  if (ELEM(ob_new, ob_old, NULL)) {
657  continue;
658  }
659 
660  collection_object_add(bmain, collection_new, ob_new, 0, true);
661  collection_object_remove(bmain, collection_new, ob_old, false);
662  }
663  }
664 
665  /* We can loop on collection_old's children,
666  * that list is currently identical the collection_new' children, and won't be changed here. */
667  LISTBASE_FOREACH_MUTABLE (CollectionChild *, child, &collection_old->children) {
668  Collection *child_collection_old = child->collection;
669 
670  Collection *child_collection_new = collection_duplicate_recursive(
671  bmain, collection_new, child_collection_old, duplicate_flags, duplicate_options);
672  if (child_collection_new != child_collection_old) {
673  collection_child_remove(collection_new, child_collection_old);
674  }
675  }
676 
677  return collection_new;
678 }
679 
689  Collection *parent,
690  Collection *collection,
691  eDupli_ID_Flags duplicate_flags,
692  eLibIDDuplicateFlags duplicate_options)
693 {
694  const bool is_subprocess = (duplicate_options & LIB_ID_DUPLICATE_IS_SUBPROCESS) != 0;
695  const bool is_root_id = (duplicate_options & LIB_ID_DUPLICATE_IS_ROOT_ID) != 0;
696 
697  if (!is_subprocess) {
698  BKE_main_id_tag_all(bmain, LIB_TAG_NEW, false);
700  }
701  if (is_root_id) {
702  /* In case root duplicated ID is linked, assume we want to get a local copy of it and duplicate
703  * all expected linked data. */
704  if (ID_IS_LINKED(collection)) {
705  duplicate_flags |= USER_DUP_LINKED_ID;
706  }
707  duplicate_options &= ~LIB_ID_DUPLICATE_IS_ROOT_ID;
708  }
709 
710  Collection *collection_new = collection_duplicate_recursive(
711  bmain, parent, collection, duplicate_flags, duplicate_options);
712 
713  if (!is_subprocess) {
714  /* `collection_duplicate_recursive` will also tag our 'root' collection, which is not required
715  * unless its duplication is a sub-process of another one. */
716  collection_new->id.tag &= ~LIB_TAG_NEW;
717 
718  /* This code will follow into all ID links using an ID tagged with LIB_TAG_NEW.*/
719  BKE_libblock_relink_to_newid(&collection_new->id);
720 
721 #ifndef NDEBUG
722  /* Call to `BKE_libblock_relink_to_newid` above is supposed to have cleared all those flags. */
723  ID *id_iter;
724  FOREACH_MAIN_ID_BEGIN (bmain, id_iter) {
725  if (id_iter->tag & LIB_TAG_NEW) {
726  BLI_assert((id_iter->tag & LIB_TAG_NEW) == 0);
727  }
728  }
730 #endif
731 
732  /* Cleanup. */
733  BKE_main_id_tag_all(bmain, LIB_TAG_NEW, false);
735 
737  }
738 
739  return collection_new;
740 }
741 
744 /* -------------------------------------------------------------------- */
751 void BKE_collection_new_name_get(Collection *collection_parent, char *rname)
752 {
753  char *name;
754 
755  if (!collection_parent) {
756  name = BLI_strdup("Collection");
757  }
758  else if (collection_parent->flag & COLLECTION_IS_MASTER) {
759  name = BLI_sprintfN("Collection %d", BLI_listbase_count(&collection_parent->children) + 1);
760  }
761  else {
762  const int number = BLI_listbase_count(&collection_parent->children) + 1;
763  const int digits = integer_digits_i(number);
764  const int max_len = sizeof(collection_parent->id.name) - 1 /* NULL terminator */ -
765  (1 + digits) /* " %d" */ - 2 /* ID */;
766  name = BLI_sprintfN("%.*s %d", max_len, collection_parent->id.name + 2, number);
767  }
768 
769  BLI_strncpy(rname, name, MAX_NAME);
770  MEM_freeN(name);
771 }
772 
776 const char *BKE_collection_ui_name_get(struct Collection *collection)
777 {
778  if (collection->flag & COLLECTION_IS_MASTER) {
779  return IFACE_("Scene Collection");
780  }
781 
782  return collection->id.name + 2;
783 }
784 
787 /* -------------------------------------------------------------------- */
792  Collection *collection,
793  int parent_restrict,
794  bool with_instances)
795 {
796  int child_restrict = collection->flag | parent_restrict;
797 
798  LISTBASE_FOREACH (CollectionObject *, cob, &collection->gobject) {
799  Base *base = BLI_findptr(lb, cob->ob, offsetof(Base, object));
800 
801  if (base == NULL) {
802  base = MEM_callocN(sizeof(Base), "Object Base");
803  base->object = cob->ob;
804  BLI_addtail(lb, base);
805  if (with_instances && cob->ob->instance_collection) {
807  lb, cob->ob->instance_collection, child_restrict, with_instances);
808  }
809  }
810 
811  /* Only collection flags are checked here currently, object restrict flag is checked
812  * in FOREACH_COLLECTION_VISIBLE_OBJECT_RECURSIVE_BEGIN since it can be animated
813  * without updating the cache. */
814  if (((child_restrict & COLLECTION_RESTRICT_VIEWPORT) == 0)) {
815  base->flag |= BASE_ENABLED_VIEWPORT;
816  }
817  if (((child_restrict & COLLECTION_RESTRICT_RENDER) == 0)) {
818  base->flag |= BASE_ENABLED_RENDER;
819  }
820  }
821 
822  LISTBASE_FOREACH (CollectionChild *, child, &collection->children) {
823  collection_object_cache_fill(lb, child->collection, child_restrict, with_instances);
824  }
825 }
826 
828 {
829  if (!(collection->flag & COLLECTION_HAS_OBJECT_CACHE)) {
830  static ThreadMutex cache_lock = BLI_MUTEX_INITIALIZER;
831 
832  BLI_mutex_lock(&cache_lock);
833  if (!(collection->flag & COLLECTION_HAS_OBJECT_CACHE)) {
834  collection_object_cache_fill(&collection->object_cache, collection, 0, false);
835  collection->flag |= COLLECTION_HAS_OBJECT_CACHE;
836  }
837  BLI_mutex_unlock(&cache_lock);
838  }
839 
840  return collection->object_cache;
841 }
842 
844 {
845  if (!(collection->flag & COLLECTION_HAS_OBJECT_CACHE_INSTANCED)) {
846  static ThreadMutex cache_lock = BLI_MUTEX_INITIALIZER;
847 
848  BLI_mutex_lock(&cache_lock);
849  if (!(collection->flag & COLLECTION_HAS_OBJECT_CACHE_INSTANCED)) {
850  collection_object_cache_fill(&collection->object_cache_instanced, collection, 0, true);
852  }
853  BLI_mutex_unlock(&cache_lock);
854  }
855 
856  return collection->object_cache_instanced;
857 }
858 
859 static void collection_object_cache_free(Collection *collection)
860 {
861  /* Clear own cache an for all parents, since those are affected by changes as well. */
862  collection->flag &= ~COLLECTION_HAS_OBJECT_CACHE;
864  BLI_freelistN(&collection->object_cache);
866 
867  LISTBASE_FOREACH (CollectionParent *, parent, &collection->parents) {
868  collection_object_cache_free(parent->collection);
869  }
870 }
871 
873 {
874  collection_object_cache_free(collection);
875 }
876 
878 {
879  if (collection) {
880  return BKE_collection_object_cache_get(collection).first;
881  }
882 
883  return FIRSTBASE(view_layer);
884 }
885 
888 /* -------------------------------------------------------------------- */
893 {
894  /* Not an actual datablock, but owned by scene. */
895  Collection *master_collection = BKE_libblock_alloc(
896  NULL, ID_GR, "Master Collection", LIB_ID_CREATE_NO_MAIN);
897  master_collection->id.flag |= LIB_EMBEDDED_DATA;
898  master_collection->flag |= COLLECTION_IS_MASTER;
899  master_collection->color_tag = COLLECTION_COLOR_NONE;
900  return master_collection;
901 }
902 
905 /* -------------------------------------------------------------------- */
910 {
911  if (object->instance_collection) {
912  Collection *dup_collection = object->instance_collection;
913  if ((dup_collection->id.tag & LIB_TAG_DOIT) == 0) {
914  /* Cycle already exists in collections, let's prevent further crappyness */
915  return true;
916  }
917  /* flag the object to identify cyclic dependencies in further dupli collections */
918  dup_collection->id.tag &= ~LIB_TAG_DOIT;
919 
920  if (dup_collection == collection) {
921  return true;
922  }
923 
924  FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN (dup_collection, collection_object) {
925  if (collection_object_cyclic_check_internal(collection_object, dup_collection)) {
926  return true;
927  }
928  }
930 
931  /* un-flag the object, it's allowed to have the same collection multiple times in parallel */
932  dup_collection->id.tag |= LIB_TAG_DOIT;
933  }
934 
935  return false;
936 }
937 
938 bool BKE_collection_object_cyclic_check(Main *bmain, Object *object, Collection *collection)
939 {
940  /* first flag all collections */
942 
943  return collection_object_cyclic_check_internal(object, collection);
944 }
945 
948 /* -------------------------------------------------------------------- */
952 bool BKE_collection_has_object(Collection *collection, const Object *ob)
953 {
954  if (ELEM(NULL, collection, ob)) {
955  return false;
956  }
957 
958  return (BLI_findptr(&collection->gobject, ob, offsetof(CollectionObject, ob)));
959 }
960 
962 {
963  if (ELEM(NULL, collection, ob)) {
964  return false;
965  }
966 
967  const ListBase objects = BKE_collection_object_cache_get(collection);
968  return (BLI_findptr(&objects, ob, offsetof(Base, object)));
969 }
970 
972 {
973  if (ELEM(NULL, collection, ob)) {
974  return false;
975  }
976 
977  const ListBase objects = BKE_collection_object_cache_instanced_get(collection);
978  return (BLI_findptr(&objects, ob, offsetof(Base, object)));
979 }
980 
982 {
983  if (scene && collection == scene->master_collection) {
984  return bmain->collections.first;
985  }
986 
987  return collection->id.next;
988 }
989 
991  Scene *scene,
992  Collection *collection,
993  Object *ob)
994 {
995  if (collection) {
996  collection = collection_next_find(bmain, scene, collection);
997  }
998  else if (scene) {
999  collection = scene->master_collection;
1000  }
1001  else {
1002  collection = bmain->collections.first;
1003  }
1004 
1005  while (collection) {
1006  if (BKE_collection_has_object(collection, ob)) {
1007  return collection;
1008  }
1009  collection = collection_next_find(bmain, scene, collection);
1010  }
1011  return NULL;
1012 }
1013 
1014 bool BKE_collection_is_empty(const Collection *collection)
1015 {
1016  return BLI_listbase_is_empty(&collection->gobject) &&
1017  BLI_listbase_is_empty(&collection->children);
1018 }
1019 
1022 /* -------------------------------------------------------------------- */
1027  Collection *collection,
1028  const int flag)
1029 {
1030  if (collection->flag & COLLECTION_IS_MASTER) {
1031  return;
1032  }
1033 
1034  DEG_id_tag_update_ex(bmain, &collection->id, flag);
1035 
1036  LISTBASE_FOREACH (CollectionParent *, collection_parent, &collection->parents) {
1037  if (collection_parent->collection->flag & COLLECTION_IS_MASTER) {
1038  /* We don't care about scene/master collection here. */
1039  continue;
1040  }
1041  collection_tag_update_parent_recursive(bmain, collection_parent->collection, flag);
1042  }
1043 }
1044 
1046 {
1047  if (!ID_IS_LINKED(collection) && !ID_IS_OVERRIDE_LIBRARY(collection)) {
1048  return collection;
1049  }
1050 
1051  if (collection->flag & COLLECTION_IS_MASTER) {
1052  return NULL;
1053  }
1054 
1055  LISTBASE_FOREACH (CollectionParent *, collection_parent, &collection->parents) {
1056  if (!ID_IS_LINKED(collection_parent->collection) &&
1057  !ID_IS_OVERRIDE_LIBRARY(collection_parent->collection)) {
1058  return collection_parent->collection;
1059  }
1061  collection_parent->collection);
1062  if (editable_collection != NULL) {
1063  return editable_collection;
1064  }
1065  }
1066 
1067  return NULL;
1068 }
1069 
1071  Main *bmain, Collection *collection, Object *ob, int flag, const bool add_us)
1072 {
1073  if (ob->instance_collection) {
1074  /* Cyclic dependency check. */
1076  ob->instance_collection == collection) {
1077  return false;
1078  }
1079  }
1080 
1081  CollectionObject *cob = BLI_findptr(&collection->gobject, ob, offsetof(CollectionObject, ob));
1082  if (cob) {
1083  return false;
1084  }
1085 
1086  cob = MEM_callocN(sizeof(CollectionObject), __func__);
1087  cob->ob = ob;
1088  BLI_addtail(&collection->gobject, cob);
1090 
1091  if (add_us && (flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
1092  id_us_plus(&ob->id);
1093  }
1094 
1095  if ((flag & LIB_ID_CREATE_NO_MAIN) == 0) {
1097  }
1098 
1099  if ((flag & LIB_ID_CREATE_NO_MAIN) == 0) {
1100  BKE_rigidbody_main_collection_object_add(bmain, collection, ob);
1101  }
1102 
1103  return true;
1104 }
1105 
1106 static bool collection_object_remove(Main *bmain,
1107  Collection *collection,
1108  Object *ob,
1109  const bool free_us)
1110 {
1111  CollectionObject *cob = BLI_findptr(&collection->gobject, ob, offsetof(CollectionObject, ob));
1112  if (cob == NULL) {
1113  return false;
1114  }
1115 
1116  BLI_freelinkN(&collection->gobject, cob);
1118 
1119  if (free_us) {
1120  BKE_id_free_us(bmain, ob);
1121  }
1122  else {
1123  id_us_min(&ob->id);
1124  }
1125 
1127 
1128  return true;
1129 }
1130 
1134 bool BKE_collection_object_add(Main *bmain, Collection *collection, Object *ob)
1135 {
1136  if (ELEM(NULL, collection, ob)) {
1137  return false;
1138  }
1139 
1140  collection = collection_parent_editable_find_recursive(collection);
1141 
1142  /* Only case where this pointer can be NULL is when scene itself is linked, this case should
1143  * never be reached. */
1144  BLI_assert(collection != NULL);
1145  if (collection == NULL) {
1146  return false;
1147  }
1148 
1149  if (!collection_object_add(bmain, collection, ob, 0, true)) {
1150  return false;
1151  }
1152 
1153  if (BKE_collection_is_in_scene(collection)) {
1154  BKE_main_collection_sync(bmain);
1155  }
1156 
1157  DEG_id_tag_update(&collection->id, ID_RECALC_GEOMETRY);
1158 
1159  return true;
1160 }
1161 
1169 {
1170  bool is_instantiated = false;
1171 
1172  FOREACH_SCENE_COLLECTION_BEGIN (scene, collection) {
1173  if (!ID_IS_LINKED(collection) && !ID_IS_OVERRIDE_LIBRARY(collection) &&
1174  BKE_collection_has_object(collection, ob_src)) {
1175  collection_object_add(bmain, collection, ob_dst, 0, true);
1176  is_instantiated = true;
1177  }
1178  }
1180 
1181  if (!is_instantiated) {
1182  /* In case we could not find any non-linked collections in which instantiate our ob_dst,
1183  * fallback to scene's master collection... */
1184  collection_object_add(bmain, scene->master_collection, ob_dst, 0, true);
1185  }
1186 
1187  BKE_main_collection_sync(bmain);
1188 }
1189 
1194  Collection *collection,
1195  Object *ob,
1196  const bool free_us)
1197 {
1198  if (ELEM(NULL, collection, ob)) {
1199  return false;
1200  }
1201 
1202  if (!collection_object_remove(bmain, collection, ob, free_us)) {
1203  return false;
1204  }
1205 
1206  if (BKE_collection_is_in_scene(collection)) {
1207  BKE_main_collection_sync(bmain);
1208  }
1209 
1210  DEG_id_tag_update(&collection->id, ID_RECALC_GEOMETRY);
1211 
1212  return true;
1213 }
1214 
1220  Main *bmain, Scene *scene, Object *ob, const bool free_us, Collection *collection_skip)
1221 {
1222  bool removed = false;
1223 
1224  if (collection_skip == NULL) {
1225  BKE_scene_remove_rigidbody_object(bmain, scene, ob, free_us);
1226  }
1227 
1228  FOREACH_SCENE_COLLECTION_BEGIN (scene, collection) {
1229  if (collection != collection_skip) {
1230  removed |= collection_object_remove(bmain, collection, ob, free_us);
1231  }
1232  }
1234 
1235  BKE_main_collection_sync(bmain);
1236 
1237  return removed;
1238 }
1239 
1243 bool BKE_scene_collections_object_remove(Main *bmain, Scene *scene, Object *ob, const bool free_us)
1244 {
1245  return scene_collections_object_remove(bmain, scene, ob, free_us, NULL);
1246 }
1247 
1248 /*
1249  * Remove all NULL objects from collections.
1250  * This is used for library remapping, where these pointers have been set to NULL.
1251  * Otherwise this should never happen.
1252  */
1254 {
1255  bool changed = false;
1256 
1257  for (CollectionObject *cob = collection->gobject.first, *cob_next = NULL; cob; cob = cob_next) {
1258  cob_next = cob->next;
1259 
1260  if (cob->ob == NULL) {
1261  BLI_freelinkN(&collection->gobject, cob);
1262  changed = true;
1263  }
1264  }
1265 
1266  if (changed) {
1268  }
1269 }
1270 
1272 {
1273  for (Scene *scene = bmain->scenes.first; scene; scene = scene->id.next) {
1275  }
1276 
1277  for (Collection *collection = bmain->collections.first; collection;
1278  collection = collection->id.next) {
1279  collection_object_remove_nulls(collection);
1280  }
1281 }
1282 
1284 {
1285  for (CollectionChild *child = collection->children.first, *child_next = NULL; child;
1286  child = child_next) {
1287  child_next = child->next;
1288 
1289  if (child->collection == NULL) {
1290  BLI_freelinkN(&collection->children, child);
1291  }
1292  }
1293 }
1294 
1296 {
1297  for (CollectionParent *parent = collection->parents.first, *parent_next; parent != NULL;
1298  parent = parent_next) {
1299  parent_next = parent->next;
1300  if ((parent->collection == NULL) || !collection_find_child(parent->collection, collection)) {
1301  BLI_freelinkN(&collection->parents, parent);
1302  }
1303  }
1304 }
1305 
1319  Collection *parent_collection,
1320  Collection *child_collection)
1321 {
1322  if (child_collection == NULL) {
1323  if (parent_collection != NULL) {
1324  collection_null_children_remove(parent_collection);
1325  }
1326  else {
1327  /* We need to do the checks in two steps when more than one collection may be involved,
1328  * otherwise we can miss some cases...
1329  * Also, master collections are not in bmain, so we also need to loop over scenes.
1330  */
1331  for (child_collection = bmain->collections.first; child_collection != NULL;
1332  child_collection = child_collection->id.next) {
1333  collection_null_children_remove(child_collection);
1334  }
1335  for (Scene *scene = bmain->scenes.first; scene != NULL; scene = scene->id.next) {
1337  }
1338  }
1339 
1340  for (child_collection = bmain->collections.first; child_collection != NULL;
1341  child_collection = child_collection->id.next) {
1342  collection_missing_parents_remove(child_collection);
1343  }
1344  for (Scene *scene = bmain->scenes.first; scene != NULL; scene = scene->id.next) {
1346  }
1347  }
1348  else {
1349  for (CollectionParent *parent = child_collection->parents.first, *parent_next; parent;
1350  parent = parent_next) {
1351  parent_next = parent->next;
1352 
1353  collection_null_children_remove(parent->collection);
1354 
1355  if (!collection_find_child(parent->collection, child_collection)) {
1356  BLI_freelinkN(&child_collection->parents, parent);
1357  }
1358  }
1359  }
1360 }
1361 
1368  Main *bmain, Scene *scene, Collection *collection_dst, Collection *collection_src, Object *ob)
1369 {
1370  /* In both cases we first add the object, then remove it from the other collections.
1371  * Otherwise we lose the original base and whether it was active and selected. */
1372  if (collection_src != NULL) {
1373  if (BKE_collection_object_add(bmain, collection_dst, ob)) {
1374  BKE_collection_object_remove(bmain, collection_src, ob, false);
1375  }
1376  }
1377  else {
1378  /* Adding will fail if object is already in collection.
1379  * However we still need to remove it from the other collections. */
1380  BKE_collection_object_add(bmain, collection_dst, ob);
1381  scene_collections_object_remove(bmain, scene, ob, false, collection_dst);
1382  }
1383 }
1384 
1387 /* -------------------------------------------------------------------- */
1392 {
1393  if (collection->flag & COLLECTION_IS_MASTER) {
1394  return true;
1395  }
1396 
1397  LISTBASE_FOREACH (CollectionParent *, cparent, &collection->parents) {
1398  if (BKE_collection_is_in_scene(cparent->collection)) {
1399  return true;
1400  }
1401  }
1402 
1403  return false;
1404 }
1405 
1407 {
1408  /* Need to update layer collections because objects might have changed
1409  * in linked files, and because undo push does not include updated base
1410  * flags since those are refreshed after the operator completes. */
1411  BKE_main_collection_sync(bmain);
1412 }
1413 
1416 /* -------------------------------------------------------------------- */
1421  Collection *instance_collection)
1422 {
1423  LISTBASE_FOREACH (CollectionObject *, collection_object, &collection->gobject) {
1424  if (collection_object->ob != NULL &&
1425  /* Object from a given collection should never instantiate that collection either. */
1426  ELEM(collection_object->ob->instance_collection, instance_collection, collection)) {
1427  return true;
1428  }
1429  }
1430 
1431  LISTBASE_FOREACH (CollectionChild *, collection_child, &collection->children) {
1432  if (collection_child->collection != NULL &&
1433  collection_instance_find_recursive(collection_child->collection, instance_collection)) {
1434  return true;
1435  }
1436  }
1437 
1438  return false;
1439 }
1440 
1450 bool BKE_collection_cycle_find(Collection *new_ancestor, Collection *collection)
1451 {
1452  if (collection == new_ancestor) {
1453  return true;
1454  }
1455 
1456  if (collection == NULL) {
1457  collection = new_ancestor;
1458  }
1459 
1460  LISTBASE_FOREACH (CollectionParent *, parent, &new_ancestor->parents) {
1461  if (BKE_collection_cycle_find(parent->collection, collection)) {
1462  return true;
1463  }
1464  }
1465 
1466  /* Find possible objects in collection or its children, that would instantiate the given ancestor
1467  * collection (that would also make a fully invalid cycle of dependencies) .*/
1468  return collection_instance_find_recursive(collection, new_ancestor);
1469 }
1470 
1471 static bool collection_instance_fix_recursive(Collection *parent_collection,
1472  Collection *collection)
1473 {
1474  bool cycles_found = false;
1475 
1476  LISTBASE_FOREACH (CollectionObject *, collection_object, &parent_collection->gobject) {
1477  if (collection_object->ob != NULL &&
1478  collection_object->ob->instance_collection == collection) {
1479  id_us_min(&collection->id);
1480  collection_object->ob->instance_collection = NULL;
1481  cycles_found = true;
1482  }
1483  }
1484 
1485  LISTBASE_FOREACH (CollectionChild *, collection_child, &parent_collection->children) {
1486  if (collection_instance_fix_recursive(collection_child->collection, collection)) {
1487  cycles_found = true;
1488  }
1489  }
1490 
1491  return cycles_found;
1492 }
1493 
1495  Collection *parent_collection,
1496  Collection *collection)
1497 {
1498  bool cycles_found = false;
1499 
1500  LISTBASE_FOREACH_MUTABLE (CollectionParent *, parent, &parent_collection->parents) {
1501  if (BKE_collection_cycle_find(parent->collection, collection)) {
1502  BKE_collection_child_remove(bmain, parent->collection, parent_collection);
1503  cycles_found = true;
1504  }
1505  else if (collection_cycle_fix_recursive(bmain, parent->collection, collection)) {
1506  cycles_found = true;
1507  }
1508  }
1509 
1510  return cycles_found;
1511 }
1512 
1519 bool BKE_collection_cycles_fix(Main *bmain, Collection *collection)
1520 {
1521  return collection_cycle_fix_recursive(bmain, collection, collection) ||
1522  collection_instance_fix_recursive(collection, collection);
1523 }
1524 
1526 {
1527  return BLI_findptr(&parent->children, collection, offsetof(CollectionChild, collection));
1528 }
1529 
1530 static bool collection_find_child_recursive(Collection *parent, Collection *collection)
1531 {
1532  LISTBASE_FOREACH (CollectionChild *, child, &parent->children) {
1533  if (child->collection == collection) {
1534  return true;
1535  }
1536 
1537  if (collection_find_child_recursive(child->collection, collection)) {
1538  return true;
1539  }
1540  }
1541 
1542  return false;
1543 }
1544 
1546 {
1547  return collection_find_child_recursive(parent, collection);
1548 }
1549 
1551 {
1552  return BLI_findptr(&child->parents, collection, offsetof(CollectionParent, collection));
1553 }
1554 
1555 static bool collection_child_add(Collection *parent,
1556  Collection *collection,
1557  const int flag,
1558  const bool add_us)
1559 {
1560  CollectionChild *child = collection_find_child(parent, collection);
1561  if (child) {
1562  return false;
1563  }
1564  if (BKE_collection_cycle_find(parent, collection)) {
1565  return false;
1566  }
1567 
1568  child = MEM_callocN(sizeof(CollectionChild), "CollectionChild");
1569  child->collection = collection;
1570  BLI_addtail(&parent->children, child);
1571 
1572  /* Don't add parent links for depsgraph datablocks, these are not kept in sync. */
1573  if ((flag & LIB_ID_CREATE_NO_MAIN) == 0) {
1574  CollectionParent *cparent = MEM_callocN(sizeof(CollectionParent), "CollectionParent");
1575  cparent->collection = parent;
1576  BLI_addtail(&collection->parents, cparent);
1577  }
1578 
1579  if (add_us) {
1580  id_us_plus(&collection->id);
1581  }
1582 
1584 
1585  return true;
1586 }
1587 
1588 static bool collection_child_remove(Collection *parent, Collection *collection)
1589 {
1590  CollectionChild *child = collection_find_child(parent, collection);
1591  if (child == NULL) {
1592  return false;
1593  }
1594 
1595  CollectionParent *cparent = collection_find_parent(collection, parent);
1596  BLI_freelinkN(&collection->parents, cparent);
1597  BLI_freelinkN(&parent->children, child);
1598 
1599  id_us_min(&collection->id);
1600 
1602 
1603  return true;
1604 }
1605 
1607 {
1608  if (!collection_child_add(parent, child, 0, true)) {
1609  return false;
1610  }
1611 
1612  BKE_main_collection_sync(bmain);
1613  return true;
1614 }
1615 
1617 {
1618  return collection_child_add(parent, child, 0, true);
1619 }
1620 
1622 {
1623  if (!collection_child_remove(parent, child)) {
1624  return false;
1625  }
1626 
1627  BKE_main_collection_sync(bmain);
1628  return true;
1629 }
1630 
1637 {
1638  LISTBASE_FOREACH_MUTABLE (CollectionChild *, child, &collection->children) {
1639  /* Check for duplicated children (can happen with remapping e.g.). */
1640  CollectionChild *other_child = collection_find_child(collection, child->collection);
1641  if (other_child != child) {
1642  BLI_freelinkN(&collection->children, child);
1643  continue;
1644  }
1645 
1646  /* Invalid child, either without a collection, or because it creates a dependency cycle. */
1647  if (child->collection == NULL || BKE_collection_cycle_find(collection, child->collection)) {
1648  BLI_freelinkN(&collection->children, child);
1649  continue;
1650  }
1651 
1652  CollectionParent *cparent = MEM_callocN(sizeof(CollectionParent), __func__);
1653  cparent->collection = collection;
1654  BLI_addtail(&child->collection->parents, cparent);
1655  }
1656 }
1657 
1659 {
1661  collection->tag &= ~COLLECTION_TAG_RELATION_REBUILD;
1662 
1663  for (CollectionChild *child = collection->children.first; child != NULL; child = child->next) {
1664  collection_parents_rebuild_recursive(child->collection);
1665  }
1666 }
1667 
1672 {
1673  /* Only collections not in bmain (master ones in scenes) have no parent... */
1674  for (Collection *collection = bmain->collections.first; collection != NULL;
1675  collection = collection->id.next) {
1676  BLI_freelistN(&collection->parents);
1677 
1678  collection->tag |= COLLECTION_TAG_RELATION_REBUILD;
1679  }
1680 
1681  /* Scene's master collections will be 'root' parent of most of our collections, so start with
1682  * them. */
1683  for (Scene *scene = bmain->scenes.first; scene != NULL; scene = scene->id.next) {
1684  /* This function can be called from readfile.c, when this pointer is not guaranteed to be NULL.
1685  */
1686  if (scene->master_collection != NULL) {
1688  }
1689  }
1690 
1691  /* We may have parent chains outside of scene's master_collection context? At least, readfile's
1692  * lib_link_collection_data() seems to assume that, so do the same here. */
1693  for (Collection *collection = bmain->collections.first; collection != NULL;
1694  collection = collection->id.next) {
1695  if (collection->tag & COLLECTION_TAG_RELATION_REBUILD) {
1696  /* Note: we do not have easy access to 'which collections is root' info in that case, which
1697  * means test for cycles in collection relationships may fail here. I don't think that is an
1698  * issue in practice here, but worth keeping in mind... */
1700  }
1701  }
1702 }
1703 
1706 /* -------------------------------------------------------------------- */
1711  const int index,
1712  int *index_current)
1713 {
1714  if (index == (*index_current)) {
1715  return collection;
1716  }
1717 
1718  (*index_current)++;
1719 
1720  LISTBASE_FOREACH (CollectionChild *, child, &collection->children) {
1721  Collection *nested = collection_from_index_recursive(child->collection, index, index_current);
1722  if (nested != NULL) {
1723  return nested;
1724  }
1725  }
1726  return NULL;
1727 }
1728 
1735 {
1736  int index_current = 0;
1737  Collection *master_collection = scene->master_collection;
1738  return collection_from_index_recursive(master_collection, index, &index_current);
1739 }
1740 
1741 static bool collection_objects_select(ViewLayer *view_layer, Collection *collection, bool deselect)
1742 {
1743  bool changed = false;
1744 
1745  if (collection->flag & COLLECTION_RESTRICT_SELECT) {
1746  return false;
1747  }
1748 
1749  LISTBASE_FOREACH (CollectionObject *, cob, &collection->gobject) {
1750  Base *base = BKE_view_layer_base_find(view_layer, cob->ob);
1751 
1752  if (base) {
1753  if (deselect) {
1754  if (base->flag & BASE_SELECTED) {
1755  base->flag &= ~BASE_SELECTED;
1756  changed = true;
1757  }
1758  }
1759  else {
1760  if ((base->flag & BASE_SELECTABLE) && !(base->flag & BASE_SELECTED)) {
1761  base->flag |= BASE_SELECTED;
1762  changed = true;
1763  }
1764  }
1765  }
1766  }
1767 
1768  LISTBASE_FOREACH (CollectionChild *, child, &collection->children) {
1769  if (collection_objects_select(view_layer, collection, deselect)) {
1770  changed = true;
1771  }
1772  }
1773 
1774  return changed;
1775 }
1776 
1781 bool BKE_collection_objects_select(ViewLayer *view_layer, Collection *collection, bool deselect)
1782 {
1784  collection);
1785 
1786  if (layer_collection != NULL) {
1787  return BKE_layer_collection_objects_select(view_layer, layer_collection, deselect);
1788  }
1789 
1790  return collection_objects_select(view_layer, collection, deselect);
1791 }
1792 
1795 /* -------------------------------------------------------------------- */
1799 /* Local temporary storage for layer collection flags. */
1800 typedef struct LayerCollectionFlag {
1807  int flag;
1811 
1812 static void layer_collection_flags_store_recursive(const LayerCollection *layer_collection,
1814 {
1815  flag->collection = layer_collection->collection;
1816  flag->flag = layer_collection->flag;
1817 
1818  LISTBASE_FOREACH (const LayerCollection *, child, &layer_collection->layer_collections) {
1819  LayerCollectionFlag *child_flag = MEM_callocN(sizeof(LayerCollectionFlag), __func__);
1820  BLI_addtail(&flag->children, child_flag);
1821  layer_collection_flags_store_recursive(child, child_flag);
1822  }
1823 }
1824 
1830  const Collection *collection,
1831  ListBase *r_layer_level_list)
1832 {
1833  BLI_listbase_clear(r_layer_level_list);
1834  LISTBASE_FOREACH (Scene *, scene, &bmain->scenes) {
1838  /* Skip this view layer if the collection isn't found for some reason. */
1839  if (layer_collection == NULL) {
1840  continue;
1841  }
1842 
1843  /* Store the flags for the collection and all of its children. */
1845  flag->view_layer = view_layer;
1846 
1847  /* Recursively save flags from collection children. */
1848  layer_collection_flags_store_recursive(layer_collection, flag);
1849 
1850  BLI_addtail(r_layer_level_list, flag);
1851  }
1852  }
1853 }
1854 
1856 {
1857  LISTBASE_FOREACH (LayerCollectionFlag *, child, &flag->children) {
1859  }
1860 
1861  BLI_freelistN(&flag->children);
1862 }
1863 
1866 {
1867  /* There should be a flag struct for every layer collection. */
1868  BLI_assert(BLI_listbase_count(&layer_collection->layer_collections) ==
1869  BLI_listbase_count(&flag->children));
1870  /* The flag and the layer collection should actually correspond. */
1871  BLI_assert(flag->collection == layer_collection->collection);
1872 
1873  LayerCollectionFlag *child_flag = flag->children.first;
1874  LISTBASE_FOREACH (LayerCollection *, child, &layer_collection->layer_collections) {
1875  layer_collection_flags_restore_recursive(child, child_flag);
1876 
1877  child_flag = child_flag->next;
1878  }
1879 
1880  /* We treat exclude as a special case.
1881  *
1882  * If in a different view layer the parent collection was disabled (e.g., background)
1883  * and now we moved a new collection to be part of the background this collection should
1884  * probably be disabled.
1885  *
1886  * Note: If we were to also keep the exclude flag we would need to re-sync the collections.
1887  */
1888  layer_collection->flag = flag->flag | (layer_collection->flag & LAYER_COLLECTION_EXCLUDE);
1889 }
1890 
1896 {
1898  ViewLayer *view_layer = flag->view_layer;
1899  /* The top level of flag structs must have this set. */
1901 
1904  /* Check that the collection is still in the scene (and therefore its view layers). In most
1905  * cases this is true, but if we move a sub-collection shared by several scenes to a collection
1906  * local to the target scene, it is effectively removed from every other scene's hierarchy
1907  * (e.g. moving into current scene's master collection). Then the other scene's view layers
1908  * won't contain a matching layer collection anymore, so there is nothing to restore to. */
1909  if (layer_collection != NULL) {
1911  }
1913  }
1914 
1915  BLI_freelistN(flags);
1916 }
1917 
1919  Collection *to_parent,
1920  Collection *from_parent,
1921  Collection *relative,
1922  bool relative_after,
1924 {
1926  return false;
1927  }
1928  if (BKE_collection_cycle_find(to_parent, collection)) {
1929  return false;
1930  }
1931 
1932  /* Move to new parent collection */
1933  if (from_parent) {
1934  collection_child_remove(from_parent, collection);
1935  }
1936 
1937  collection_child_add(to_parent, collection, 0, true);
1938 
1939  /* Move to specified location under parent. */
1940  if (relative) {
1941  CollectionChild *child = collection_find_child(to_parent, collection);
1942  CollectionChild *relative_child = collection_find_child(to_parent, relative);
1943 
1944  if (relative_child) {
1945  BLI_remlink(&to_parent->children, child);
1946 
1947  if (relative_after) {
1948  BLI_insertlinkafter(&to_parent->children, relative_child, child);
1949  }
1950  else {
1951  BLI_insertlinkbefore(&to_parent->children, relative_child, child);
1952  }
1953 
1955  }
1956  }
1957 
1958  /* Make sure we store the flag of the layer collections before we remove and re-create them.
1959  * Otherwise they will get lost and everything will be copied from the new parent collection.
1960  * Don't use flag syncing when moving a collection to a different scene, as it no longer exists
1961  * in the same view layers anyway. */
1962  const bool do_flag_sync = BKE_scene_find_from_collection(bmain, to_parent) ==
1964  ListBase layer_flags;
1965  if (do_flag_sync) {
1966  layer_collection_flags_store(bmain, collection, &layer_flags);
1967  }
1968 
1969  /* Create and remove layer collections. */
1970  BKE_main_collection_sync(bmain);
1971 
1972  /* Restore the original layer collection flags and free their temporary storage. */
1973  if (do_flag_sync) {
1975  }
1976 
1977  /* We need to sync it again to pass the correct flags to the collections objects. */
1978  BKE_main_collection_sync(bmain);
1979 
1980  return true;
1981 }
1982 
1985 /* -------------------------------------------------------------------- */
1989 /* Scene collection iterator. */
1990 
1991 typedef struct CollectionsIteratorData {
1993  void **array;
1994  int tot, cur;
1996 
1997 static void scene_collection_callback(Collection *collection,
1999  void *data)
2000 {
2001  callback(collection, data);
2002 
2003  LISTBASE_FOREACH (CollectionChild *, child, &collection->children) {
2004  scene_collection_callback(child->collection, callback, data);
2005  }
2006 }
2007 
2008 static void scene_collections_count(Collection *UNUSED(collection), void *data)
2009 {
2010  int *tot = data;
2011  (*tot)++;
2012 }
2013 
2014 static void scene_collections_build_array(Collection *collection, void *data)
2015 {
2016  Collection ***array = data;
2017  **array = collection;
2018  (*array)++;
2019 }
2020 
2022  Collection ***r_collections_array,
2023  int *r_collections_array_len)
2024 {
2025  *r_collections_array = NULL;
2026  *r_collections_array_len = 0;
2027 
2028  if (scene == NULL) {
2029  return;
2030  }
2031 
2032  Collection *collection = scene->master_collection;
2033  BLI_assert(collection != NULL);
2034  scene_collection_callback(collection, scene_collections_count, r_collections_array_len);
2035 
2036  BLI_assert(*r_collections_array_len > 0);
2037 
2039  *r_collections_array_len, sizeof(Collection *), "CollectionArray");
2040  *r_collections_array = array;
2042 }
2043 
2049 {
2050  Scene *scene = data_in;
2052 
2053  data->scene = scene;
2054 
2055  BLI_ITERATOR_INIT(iter);
2056  iter->data = data;
2057 
2058  scene_collections_array(scene, (Collection ***)&data->array, &data->tot);
2059  BLI_assert(data->tot != 0);
2060 
2061  data->cur = 0;
2062  iter->current = data->array[data->cur];
2063 }
2064 
2066 {
2068 
2069  if (++data->cur < data->tot) {
2070  iter->current = data->array[data->cur];
2071  }
2072  else {
2073  iter->valid = false;
2074  }
2075 }
2076 
2078 {
2080 
2081  if (data) {
2082  if (data->array) {
2083  MEM_freeN(data->array);
2084  }
2085  MEM_freeN(data);
2086  }
2087  iter->valid = false;
2088 }
2089 
2090 /* scene objects iterator */
2091 
2097 
2098 static void scene_objects_iterator_begin(BLI_Iterator *iter, Scene *scene, GSet *visited_objects)
2099 {
2101 
2102  BLI_ITERATOR_INIT(iter);
2103  iter->data = data;
2104 
2105  /* Lookup list to make sure that each object is only processed once. */
2106  if (visited_objects != NULL) {
2107  data->visited = visited_objects;
2108  }
2109  else {
2110  data->visited = BLI_gset_ptr_new(__func__);
2111  }
2112 
2113  /* We wrap the scenecollection iterator here to go over the scene collections. */
2114  BKE_scene_collections_iterator_begin(&data->scene_collection_iter, scene);
2115 
2116  Collection *collection = data->scene_collection_iter.current;
2117  data->cob_next = collection->gobject.first;
2118 
2120 }
2121 
2123 {
2124  Scene *scene = data_in;
2125 
2127 }
2128 
2133 {
2134  for (; cob != NULL; cob = cob->next) {
2135  Object *ob = cob->ob;
2136  void **ob_key_p;
2137  if (!BLI_gset_ensure_p_ex(gs, ob, &ob_key_p)) {
2138  *ob_key_p = ob;
2139  return cob;
2140  }
2141  }
2142  return NULL;
2143 }
2144 
2146 {
2148  CollectionObject *cob = data->cob_next ? object_base_unique(data->visited, data->cob_next) :
2149  NULL;
2150 
2151  if (cob) {
2152  data->cob_next = cob->next;
2153  iter->current = cob->ob;
2154  }
2155  else {
2156  /* if this is the last object of this ListBase look at the next Collection */
2157  Collection *collection;
2158  BKE_scene_collections_iterator_next(&data->scene_collection_iter);
2159  do {
2160  collection = data->scene_collection_iter.current;
2161  /* get the first unique object of this collection */
2162  CollectionObject *new_cob = object_base_unique(data->visited, collection->gobject.first);
2163  if (new_cob) {
2164  data->cob_next = new_cob->next;
2165  iter->current = new_cob->ob;
2166  return;
2167  }
2168  BKE_scene_collections_iterator_next(&data->scene_collection_iter);
2169  } while (data->scene_collection_iter.valid);
2170 
2171  if (!data->scene_collection_iter.valid) {
2172  iter->valid = false;
2173  }
2174  }
2175 }
2176 
2178 {
2180  if (data) {
2181  BKE_scene_collections_iterator_end(&data->scene_collection_iter);
2182  if (data->visited != NULL) {
2183  BLI_gset_free(data->visited, NULL);
2184  }
2185  MEM_freeN(data);
2186  }
2187 }
2188 
2197 {
2198  BLI_Iterator iter;
2199  scene_objects_iterator_begin(&iter, scene, objects_gset);
2200  while (iter.valid) {
2202  }
2203 
2204  /* `return_gset` is either given `objects_gset` (if non-NULL), or the GSet allocated by the
2205  * iterator. Either way, we want to get it back, and prevent `BKE_scene_objects_iterator_end`
2206  * from freeing it. */
2207  GSet *return_gset = ((SceneObjectsIteratorData *)iter.data)->visited;
2208  ((SceneObjectsIteratorData *)iter.data)->visited = NULL;
2210 
2211  return return_gset;
2212 }
2213 
#define FOREACH_COLLECTION_OBJECT_RECURSIVE_END
#define FOREACH_SCENE_COLLECTION_END
void(* BKE_scene_collections_Cb)(struct Collection *ob, void *data)
void BKE_collection_compat_blend_read_lib(struct BlendLibReader *reader, struct Library *lib, struct SceneCollection *sc)
void BKE_collection_compat_blend_read_expand(struct BlendExpander *expander, struct SceneCollection *sc)
#define FOREACH_SCENE_COLLECTION_BEGIN(scene, _instance)
void BKE_collection_compat_blend_read_data(struct BlendDataReader *reader, struct SceneCollection *sc)
#define FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(_collection, _object)
void BKE_previewimg_free(struct PreviewImage **prv)
Definition: icons.cc:295
void BKE_previewimg_id_copy(struct ID *new_id, const struct ID *old_id)
void BKE_previewimg_blend_read(struct BlendDataReader *reader, struct PreviewImage *prv)
Definition: icons.cc:651
void BKE_previewimg_blend_write(struct BlendWriter *writer, const struct PreviewImage *prv)
@ IDTYPE_FLAGS_NO_ANIMDATA
Definition: BKE_idtype.h:51
bool BKE_layer_collection_objects_select(struct ViewLayer *view_layer, struct LayerCollection *lc, bool deselect)
Definition: layer.c:1031
void BKE_main_collection_sync(const struct Main *bmain)
void BKE_view_layer_blend_read_data(struct BlendDataReader *reader, struct ViewLayer *view_layer)
Definition: layer.c:1937
struct Base * BKE_view_layer_base_find(struct ViewLayer *view_layer, struct Object *ob)
Definition: layer.c:394
struct LayerCollection * BKE_layer_collection_first_from_scene_collection(struct ViewLayer *view_layer, const struct Collection *collection)
void BKE_view_layer_blend_read_lib(struct BlendLibReader *reader, struct Library *lib, struct ViewLayer *view_layer)
Definition: layer.c:1976
void id_us_min(struct ID *id)
Definition: lib_id.c:297
void BKE_libblock_free_data(struct ID *id, const bool do_id_user) ATTR_NONNULL()
Definition: lib_id_delete.c:55
void BKE_main_id_tag_all(struct Main *mainvar, const int tag, const bool value)
Definition: lib_id.c:923
@ LIB_ID_COPY_NO_PREVIEW
Definition: BKE_lib_id.h:116
@ LIB_ID_CREATE_NO_USER_REFCOUNT
Definition: BKE_lib_id.h:92
@ LIB_ID_CREATE_NO_MAIN
Definition: BKE_lib_id.h:88
void * BKE_libblock_alloc(struct Main *bmain, short type, const char *name, const int flag) ATTR_WARN_UNUSED_RESULT
Definition: lib_id.c:1062
void BKE_main_id_clear_newpoins(struct Main *bmain)
Definition: lib_id.c:1738
void id_us_plus(struct ID *id)
Definition: lib_id.c:288
struct ID * BKE_id_copy_for_duplicate(struct Main *bmain, struct ID *id, const uint duplicate_flags)
void BKE_id_blend_write(struct BlendWriter *writer, struct ID *id)
Definition: lib_id.c:2395
void BKE_main_id_tag_listbase(struct ListBase *lb, const int tag, const bool value)
Definition: lib_id.c:891
void BKE_id_delete(struct Main *bmain, void *idv) ATTR_NONNULL()
eLibIDDuplicateFlags
Definition: BKE_lib_id.h:169
@ LIB_ID_DUPLICATE_IS_ROOT_ID
Definition: BKE_lib_id.h:178
@ LIB_ID_DUPLICATE_IS_SUBPROCESS
Definition: BKE_lib_id.h:175
void * BKE_id_new(struct Main *bmain, const short type, const char *name)
Definition: lib_id.c:1177
void BKE_id_free_us(struct Main *bmain, void *idv) ATTR_NONNULL()
#define BKE_LIB_FOREACHID_PROCESS(_data, _id_super, _cb_flag)
@ IDWALK_CB_LOOPBACK
Definition: BKE_lib_query.h:68
@ IDWALK_CB_NEVER_SELF
Definition: BKE_lib_query.h:49
@ IDWALK_CB_USER
Definition: BKE_lib_query.h:87
@ IDWALK_CB_EMBEDDED
Definition: BKE_lib_query.h:62
@ IDWALK_CB_NOP
Definition: BKE_lib_query.h:47
void void BKE_libblock_relink_to_newid(struct ID *id) ATTR_NONNULL()
Definition: lib_remap.c:702
#define FOREACH_MAIN_ID_END
Definition: BKE_main.h:250
#define FOREACH_MAIN_ID_BEGIN(_bmain, _id)
Definition: BKE_main.h:244
General operations, lookup, etc. for blender objects.
struct Object * BKE_object_duplicate(struct Main *bmain, struct Object *ob, uint dupflag, const uint duplicate_options)
API for Blender-side Rigid Body stuff.
void BKE_rigidbody_main_collection_object_add(struct Main *bmain, struct Collection *collection, struct Object *object)
Definition: rigidbody.c:2354
struct Scene * BKE_scene_find_from_collection(const struct Main *bmain, const struct Collection *collection)
void BKE_scene_remove_rigidbody_object(struct Main *bmain, struct Scene *scene, struct Object *ob, const bool free_us)
Definition: scene.c:2417
#define BLI_assert(a)
Definition: BLI_assert.h:58
struct GSet GSet
Definition: BLI_ghash.h:189
bool BLI_gset_ensure_p_ex(GSet *gs, const void *key, void ***r_key)
Definition: BLI_ghash.c:1171
GSet * BLI_gset_ptr_new(const char *info)
void BLI_gset_free(GSet *gs, GSetKeyFreeFP keyfreefp)
Definition: BLI_ghash.c:1253
#define BLI_ITERATOR_INIT(iter)
Definition: BLI_iterator.h:37
BLI_INLINE bool BLI_listbase_is_empty(const struct ListBase *lb)
Definition: BLI_listbase.h:124
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:172
void BLI_freelinkN(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:281
#define LISTBASE_FOREACH_MUTABLE(type, var, list)
Definition: BLI_listbase.h:188
BLI_INLINE void BLI_listbase_clear(struct ListBase *lb)
Definition: BLI_listbase.h:128
void BLI_insertlinkafter(struct ListBase *listbase, void *vprevlink, void *vnewlink) ATTR_NONNULL(1)
Definition: listbase.c:352
void void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1)
Definition: listbase.c:547
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:110
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:133
void BLI_insertlinkbefore(struct ListBase *listbase, void *vnextlink, void *vnewlink) ATTR_NONNULL(1)
Definition: listbase.c:395
int BLI_listbase_count(const struct ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void * BLI_findptr(const struct ListBase *listbase, const void *ptr, const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
MINLINE int integer_digits_i(const int i)
size_t size_t char * BLI_sprintfN(const char *__restrict format,...) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_PRINTF_FORMAT(1
#define STRNCPY(dst, src)
Definition: BLI_string.h:163
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL() ATTR_MALLOC
Definition: string.c:70
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL()
Definition: string.c:108
#define BLI_MUTEX_INITIALIZER
Definition: BLI_threads.h:84
void BLI_mutex_lock(ThreadMutex *mutex)
Definition: threads.cc:401
void BLI_mutex_unlock(ThreadMutex *mutex)
Definition: threads.cc:406
pthread_mutex_t ThreadMutex
Definition: BLI_threads.h:83
#define UNUSED(x)
#define ELEM(...)
#define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member)
#define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member)
#define BLO_read_data_address(reader, ptr_p)
#define BLO_write_id_struct(writer, struct_name, id_address, id)
#define BLO_write_struct(writer, struct_name, data_ptr)
void BLO_read_list(BlendDataReader *reader, struct ListBase *list)
Definition: readfile.c:5654
#define BLO_read_id_address(reader, lib, id_ptr_p)
#define BLO_expand(expander, id)
bool BLO_write_is_undo(BlendWriter *writer)
Definition: writefile.c:1412
#define IFACE_(msgid)
#define BLT_I18NCONTEXT_ID_COLLECTION
void DEG_id_tag_update_ex(struct Main *bmain, struct ID *id, int flag)
void DEG_id_tag_update(struct ID *id, int flag)
ID and Library types, which are fundamental for sdna.
@ ID_RECALC_COPY_ON_WRITE
Definition: DNA_ID.h:654
@ ID_RECALC_GEOMETRY
Definition: DNA_ID.h:611
@ LIB_TAG_NEW
Definition: DNA_ID.h:551
@ LIB_TAG_DOIT
Definition: DNA_ID.h:554
@ LIB_TAG_NO_MAIN
Definition: DNA_ID.h:572
#define ID_IS_LINKED(_id)
Definition: DNA_ID.h:426
#define FILTER_ID_GR
Definition: DNA_ID.h:711
@ LIB_EMBEDDED_DATA
Definition: DNA_ID.h:482
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition: DNA_ID.h:445
@ INDEX_ID_GR
Definition: DNA_ID.h:841
@ ID_GR
Definition: DNA_ID_enums.h:77
Object groups, one object can be in many groups at once.
struct Collection Collection
@ COLLECTION_COLOR_NONE
@ COLLECTION_RESTRICT_RENDER
@ COLLECTION_HAS_OBJECT_CACHE_INSTANCED
@ COLLECTION_RESTRICT_VIEWPORT
@ COLLECTION_IS_MASTER
@ COLLECTION_HAS_OBJECT_CACHE
@ COLLECTION_RESTRICT_SELECT
@ COLLECTION_TAG_RELATION_REBUILD
#define DNA_struct_default_get(struct_name)
Definition: DNA_defaults.h:44
#define MAX_NAME
Definition: DNA_defs.h:62
@ LAYER_COLLECTION_EXCLUDE
@ BASE_SELECTABLE
@ BASE_ENABLED_RENDER
@ BASE_ENABLED_VIEWPORT
@ BASE_SELECTED
Object is a sort of wrapper for general info.
Types and defines for representing Rigid Body entities.
#define FIRSTBASE(_view_layer)
eDupli_ID_Flags
@ USER_DUP_LINKED_ID
@ USER_DUP_OBJECT
Read Guarded memory(de)allocation.
static void init_data(ModifierData *md)
static void collection_object_cache_fill(ListBase *lb, Collection *collection, int parent_restrict, bool with_instances)
Definition: collection.c:791
static bool collection_cycle_fix_recursive(Main *bmain, Collection *parent_collection, Collection *collection)
Definition: collection.c:1494
static bool collection_find_child_recursive(Collection *parent, Collection *collection)
Definition: collection.c:1530
bool BKE_collection_has_object_recursive_instanced(Collection *collection, Object *ob)
Definition: collection.c:971
static void scene_collections_count(Collection *UNUSED(collection), void *data)
Definition: collection.c:2008
static CollectionParent * collection_find_parent(Collection *child, Collection *collection)
Definition: collection.c:1550
bool BKE_collection_has_object_recursive(Collection *collection, Object *ob)
Definition: collection.c:961
Collection * BKE_collection_from_index(Scene *scene, const int index)
Definition: collection.c:1734
void BKE_collection_add_from_collection(Main *bmain, Scene *scene, Collection *collection_src, Collection *collection_dst)
Definition: collection.c:476
static void layer_collection_flags_store(Main *bmain, const Collection *collection, ListBase *r_layer_level_list)
Definition: collection.c:1829
void BKE_collection_free(Collection *collection)
Definition: collection.c:510
static bool collection_child_remove(Collection *parent, Collection *collection)
Definition: collection.c:1588
const char * BKE_collection_ui_name_get(struct Collection *collection)
Definition: collection.c:776
void BKE_collection_blend_write_nolib(BlendWriter *writer, Collection *collection)
Definition: collection.c:197
bool BKE_collection_child_remove(Main *bmain, Collection *parent, Collection *child)
Definition: collection.c:1621
static bool collection_child_add(Collection *parent, Collection *collection, const int flag, const bool add_us)
Definition: collection.c:1555
Collection * BKE_collection_master_add()
Definition: collection.c:892
static CollectionObject * object_base_unique(GSet *gs, CollectionObject *cob)
Definition: collection.c:2132
static bool collection_object_remove(Main *bmain, Collection *collection, Object *ob, const bool free_us)
Definition: collection.c:1106
static bool collection_instance_find_recursive(Collection *collection, Collection *instance_collection)
Definition: collection.c:1420
bool BKE_collection_has_object(Collection *collection, const Object *ob)
Definition: collection.c:952
ListBase BKE_collection_object_cache_instanced_get(Collection *collection)
Definition: collection.c:843
static void collection_object_cache_free(Collection *collection)
Definition: collection.c:859
static void collection_blend_read_lib(BlendLibReader *reader, ID *id)
Definition: collection.c:325
Collection * BKE_collection_duplicate(Main *bmain, Collection *parent, Collection *collection, eDupli_ID_Flags duplicate_flags, eLibIDDuplicateFlags duplicate_options)
Definition: collection.c:688
static bool collection_object_add(Main *bmain, Collection *collection, Object *ob, int flag, const bool add_us)
Definition: collection.c:1070
static void collection_blend_read_expand(BlendExpander *expander, ID *id)
Definition: collection.c:362
GSet * BKE_scene_objects_as_gset(Scene *scene, GSet *objects_gset)
Definition: collection.c:2196
static void layer_collection_flags_restore_recursive(LayerCollection *layer_collection, LayerCollectionFlag *flag)
Definition: collection.c:1864
bool BKE_collection_child_add_no_sync(Collection *parent, Collection *child)
Definition: collection.c:1616
Collection * BKE_collection_object_find(Main *bmain, Scene *scene, Collection *collection, Object *ob)
Definition: collection.c:990
void BKE_scene_objects_iterator_end(BLI_Iterator *iter)
Definition: collection.c:2177
bool BKE_collection_is_empty(const Collection *collection)
Definition: collection.c:1014
static void collection_parents_rebuild_recursive(Collection *collection)
Definition: collection.c:1658
bool BKE_collection_move(Main *bmain, Collection *to_parent, Collection *from_parent, Collection *relative, bool relative_after, Collection *collection)
Definition: collection.c:1918
bool BKE_collection_is_in_scene(Collection *collection)
Definition: collection.c:1391
struct LayerCollectionFlag LayerCollectionFlag
static void scene_collection_callback(Collection *collection, BKE_scene_collections_Cb callback, void *data)
Definition: collection.c:1997
static void layer_collection_flags_free_recursive(LayerCollectionFlag *flag)
Definition: collection.c:1855
bool BKE_collection_child_add(Main *bmain, Collection *parent, Collection *child)
Definition: collection.c:1606
static ID * collection_owner_get(Main *bmain, ID *id)
Definition: collection.c:177
void BKE_collection_object_move(Main *bmain, Scene *scene, Collection *collection_dst, Collection *collection_src, Object *ob)
Definition: collection.c:1367
void BKE_collection_add_from_object(Main *bmain, Scene *scene, const Object *ob_src, Collection *collection_dst)
Definition: collection.c:448
void BKE_collection_blend_read_expand(BlendExpander *expander, Collection *collection)
Definition: collection.c:345
static bool collection_object_cyclic_check_internal(Object *object, Collection *collection)
Definition: collection.c:909
static Collection * collection_next_find(Main *bmain, Scene *scene, Collection *collection)
Definition: collection.c:981
bool BKE_scene_collections_object_remove(Main *bmain, Scene *scene, Object *ob, const bool free_us)
Definition: collection.c:1243
bool BKE_collection_objects_select(ViewLayer *view_layer, Collection *collection, bool deselect)
Definition: collection.c:1781
void BKE_collections_child_remove_nulls(Main *bmain, Collection *parent_collection, Collection *child_collection)
Definition: collection.c:1318
static void collection_copy_data(Main *bmain, ID *id_dst, const ID *id_src, const int flag)
Definition: collection.c:108
static Collection * collection_add(Main *bmain, Collection *collection_parent, const char *name_custom)
Definition: collection.c:403
bool BKE_collection_has_collection(Collection *parent, Collection *collection)
Definition: collection.c:1545
void BKE_collection_object_cache_free(Collection *collection)
Definition: collection.c:872
bool BKE_collection_object_remove(Main *bmain, Collection *collection, Object *ob, const bool free_us)
Definition: collection.c:1193
static void lib_link_collection_data(BlendLibReader *reader, Library *lib, Collection *collection)
Definition: collection.c:279
void BKE_scene_collections_iterator_begin(BLI_Iterator *iter, void *data_in)
Definition: collection.c:2048
void BKE_collection_new_name_get(Collection *collection_parent, char *rname)
Definition: collection.c:751
Base * BKE_collection_or_layer_objects(const ViewLayer *view_layer, Collection *collection)
Definition: collection.c:877
Collection * BKE_collection_add(Main *bmain, Collection *collection_parent, const char *name_custom)
Definition: collection.c:435
static CollectionChild * collection_find_child(Collection *parent, Collection *collection)
Definition: collection.c:1525
bool BKE_collection_cycles_fix(Main *bmain, Collection *collection)
Definition: collection.c:1519
struct SceneObjectsIteratorData SceneObjectsIteratorData
static void collection_free_data(ID *id)
Definition: collection.c:141
static bool collection_instance_fix_recursive(Collection *parent_collection, Collection *collection)
Definition: collection.c:1471
static void collection_object_remove_nulls(Collection *collection)
Definition: collection.c:1253
void BKE_collection_object_add_from(Main *bmain, Scene *scene, Object *ob_src, Object *ob_dst)
Definition: collection.c:1168
static void collection_blend_read_data(BlendDataReader *reader, ID *id)
Definition: collection.c:273
static void scene_collections_array(Scene *scene, Collection ***r_collections_array, int *r_collections_array_len)
Definition: collection.c:2021
static Collection * collection_duplicate_recursive(Main *bmain, Collection *parent, Collection *collection_old, const eDupli_ID_Flags duplicate_flags, const eLibIDDuplicateFlags duplicate_options)
Definition: collection.c:579
void BKE_collection_blend_read_lib(BlendLibReader *reader, Collection *collection)
Definition: collection.c:310
static void layer_collection_flags_restore(ListBase *flags, const Collection *collection)
Definition: collection.c:1895
bool BKE_collection_delete(Main *bmain, Collection *collection, bool hierarchy)
Definition: collection.c:520
void BKE_collections_object_remove_nulls(Main *bmain)
Definition: collection.c:1271
bool BKE_collection_cycle_find(Collection *new_ancestor, Collection *collection)
Definition: collection.c:1450
static void scene_objects_iterator_begin(BLI_Iterator *iter, Scene *scene, GSet *visited_objects)
Definition: collection.c:2098
static void collection_null_children_remove(Collection *collection)
Definition: collection.c:1283
static void collection_blend_write(BlendWriter *writer, ID *id, const void *id_address)
Definition: collection.c:213
static Collection * collection_from_index_recursive(Collection *collection, const int index, int *index_current)
Definition: collection.c:1710
bool BKE_collection_object_cyclic_check(Main *bmain, Object *object, Collection *collection)
Definition: collection.c:938
void BKE_collection_blend_read_data(BlendDataReader *reader, Collection *collection)
Definition: collection.c:244
static void collection_missing_parents_remove(Collection *collection)
Definition: collection.c:1295
void BKE_collections_after_lib_link(Main *bmain)
Definition: collection.c:1406
static void scene_collections_build_array(Collection *collection, void *data)
Definition: collection.c:2014
struct CollectionsIteratorData CollectionsIteratorData
static bool scene_collections_object_remove(Main *bmain, Scene *scene, Object *ob, const bool free_us, Collection *collection_skip)
Definition: collection.c:1219
static void collection_init_data(ID *id)
Definition: collection.c:90
ListBase BKE_collection_object_cache_get(Collection *collection)
Definition: collection.c:827
void BKE_scene_collections_iterator_end(struct BLI_Iterator *iter)
Definition: collection.c:2077
void BKE_scene_objects_iterator_begin(BLI_Iterator *iter, void *data_in)
Definition: collection.c:2122
void BKE_scene_collections_iterator_next(struct BLI_Iterator *iter)
Definition: collection.c:2065
void BKE_collection_parent_relations_rebuild(Collection *collection)
Definition: collection.c:1636
static void layer_collection_flags_store_recursive(const LayerCollection *layer_collection, LayerCollectionFlag *flag)
Definition: collection.c:1812
static Collection * collection_parent_editable_find_recursive(Collection *collection)
Definition: collection.c:1045
static void collection_tag_update_parent_recursive(Main *bmain, Collection *collection, const int flag)
Definition: collection.c:1026
void BKE_scene_objects_iterator_next(BLI_Iterator *iter)
Definition: collection.c:2145
static void collection_foreach_id(ID *id, LibraryForeachIDData *data)
Definition: collection.c:155
void BKE_main_collections_parent_relations_rebuild(Main *bmain)
Definition: collection.c:1671
IDTypeInfo IDType_ID_GR
Definition: collection.c:368
static bool collection_objects_select(ViewLayer *view_layer, Collection *collection, bool deselect)
Definition: collection.c:1741
bool BKE_collection_object_add(Main *bmain, Collection *collection, Object *ob)
Definition: collection.c:1134
Scene scene
DEGForeachIDComponentCallback callback
DRWShaderLibrary * lib
void *(* MEM_malloc_arrayN)(size_t len, size_t size, const char *str)
Definition: mallocn.c:48
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
void * current
Definition: BLI_iterator.h:28
short flag
struct Object * object
struct Collection * collection
struct Object * ob
struct CollectionObject * next
struct Collection * collection
struct PreviewImage * preview
ListBase object_cache_instanced
ListBase object_cache
short id_code
Definition: BKE_idtype.h:120
Definition: DNA_ID.h:273
int tag
Definition: DNA_ID.h:292
struct Library * lib
Definition: DNA_ID.h:277
int us
Definition: DNA_ID.h:293
struct ID * newid
Definition: DNA_ID.h:275
short flag
Definition: DNA_ID.h:288
void * next
Definition: DNA_ID.h:274
char name[66]
Definition: DNA_ID.h:283
struct LayerCollectionFlag * next
Definition: collection.c:1801
Collection * collection
Definition: collection.c:1805
struct LayerCollectionFlag * prev
Definition: collection.c:1801
ViewLayer * view_layer
Definition: collection.c:1803
ListBase layer_collections
struct Collection * collection
void * first
Definition: DNA_listBase.h:47
Definition: BKE_main.h:116
ListBase scenes
Definition: BKE_main.h:146
ListBase collections
Definition: BKE_main.h:167
struct Collection * instance_collection
ListBase scene_collections
BLI_Iterator scene_collection_iter
Definition: collection.c:2095
CollectionObject * cob_next
Definition: collection.c:2094
struct Collection * master_collection
ListBase view_layers