Blender  V3.3
collection.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 /* Allow using deprecated functionality for .blend file I/O. */
8 #define DNA_DEPRECATED_ALLOW
9 
10 #include <string.h>
11 
12 #include "BLI_blenlib.h"
13 #include "BLI_iterator.h"
14 #include "BLI_listbase.h"
15 #include "BLI_math_base.h"
16 #include "BLI_threads.h"
17 #include "BLT_translation.h"
18 
19 #include "BKE_anim_data.h"
20 #include "BKE_collection.h"
21 #include "BKE_icons.h"
22 #include "BKE_idprop.h"
23 #include "BKE_idtype.h"
24 #include "BKE_layer.h"
25 #include "BKE_lib_id.h"
26 #include "BKE_lib_query.h"
27 #include "BKE_lib_remap.h"
28 #include "BKE_main.h"
29 #include "BKE_object.h"
30 #include "BKE_rigidbody.h"
31 #include "BKE_scene.h"
32 
33 #include "DNA_defaults.h"
34 
35 #include "DNA_ID.h"
36 #include "DNA_collection_types.h"
37 #include "DNA_layer_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_rigidbody_types.h"
40 #include "DNA_scene_types.h"
41 
42 #include "DEG_depsgraph.h"
43 #include "DEG_depsgraph_query.h"
44 
45 #include "MEM_guardedalloc.h"
46 
47 #include "BLO_read_write.h"
48 
49 /* -------------------------------------------------------------------- */
53 static bool collection_child_add(Collection *parent,
54  Collection *collection,
55  const int flag,
56  const bool add_us);
57 static bool collection_child_remove(Collection *parent, Collection *collection);
58 static bool collection_object_add(
59  Main *bmain, Collection *collection, Object *ob, int flag, const bool add_us);
60 static bool collection_object_remove(Main *bmain,
61  Collection *collection,
62  Object *ob,
63  const bool free_us);
64 
65 static CollectionChild *collection_find_child(Collection *parent, Collection *collection);
67 
68 static bool collection_find_child_recursive(const Collection *parent,
69  const Collection *collection);
70 
73 /* -------------------------------------------------------------------- */
77 static void collection_init_data(ID *id)
78 {
79  Collection *collection = (Collection *)id;
80  BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(collection, id));
81 
83 }
84 
95 static void collection_copy_data(Main *bmain, ID *id_dst, const ID *id_src, const int flag)
96 {
97  Collection *collection_dst = (Collection *)id_dst;
98  const Collection *collection_src = (const Collection *)id_src;
99 
100  BLI_assert(((collection_src->flag & COLLECTION_IS_MASTER) != 0) ==
101  ((collection_src->id.flag & LIB_EMBEDDED_DATA) != 0));
102 
103  /* Do not copy collection's preview (same behavior as for objects). */
104  if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0 && false) { /* XXX TODO: temp hack. */
105  BKE_previewimg_id_copy(&collection_dst->id, &collection_src->id);
106  }
107  else {
108  collection_dst->preview = NULL;
109  }
110 
111  collection_dst->flag &= ~COLLECTION_HAS_OBJECT_CACHE;
112  collection_dst->flag &= ~COLLECTION_HAS_OBJECT_CACHE_INSTANCED;
113  BLI_listbase_clear(&collection_dst->object_cache);
114  BLI_listbase_clear(&collection_dst->object_cache_instanced);
115 
116  BLI_listbase_clear(&collection_dst->gobject);
117  BLI_listbase_clear(&collection_dst->children);
118  BLI_listbase_clear(&collection_dst->parents);
119 
120  LISTBASE_FOREACH (CollectionChild *, child, &collection_src->children) {
121  collection_child_add(collection_dst, child->collection, flag, false);
122  }
123  LISTBASE_FOREACH (CollectionObject *, cob, &collection_src->gobject) {
124  collection_object_add(bmain, collection_dst, cob->ob, flag, false);
125  }
126 }
127 
128 static void collection_free_data(ID *id)
129 {
130  Collection *collection = (Collection *)id;
131 
132  /* No animation-data here. */
133  BKE_previewimg_free(&collection->preview);
134 
135  BLI_freelistN(&collection->gobject);
136  BLI_freelistN(&collection->children);
137  BLI_freelistN(&collection->parents);
138 
140 }
141 
143 {
144  Collection *collection = (Collection *)id;
145 
146  LISTBASE_FOREACH (CollectionObject *, cob, &collection->gobject) {
148  }
149  LISTBASE_FOREACH (CollectionChild *, child, &collection->children) {
151  data, child->collection, IDWALK_CB_NEVER_SELF | IDWALK_CB_USER);
152  }
153  LISTBASE_FOREACH (CollectionParent *, parent, &collection->parents) {
154  /* XXX This is very weak. The whole idea of keeping pointers to private IDs is very bad
155  * anyway... */
156  const int cb_flag = ((parent->collection != NULL &&
157  (parent->collection->id.flag & LIB_EMBEDDED_DATA) != 0) ?
159  IDWALK_CB_NOP);
161  data, parent->collection, IDWALK_CB_NEVER_SELF | IDWALK_CB_LOOPBACK | cb_flag);
162  }
163 }
164 
165 static ID *collection_owner_get(Main *bmain, ID *id, ID *owner_id_hint)
166 {
167  if ((id->flag & LIB_EMBEDDED_DATA) == 0) {
168  return id;
169  }
170  BLI_assert((id->tag & LIB_TAG_NO_MAIN) == 0);
171 
172  Collection *master_collection = (Collection *)id;
173  BLI_assert((master_collection->flag & COLLECTION_IS_MASTER) != 0);
174 
175  if (owner_id_hint != NULL && GS(owner_id_hint->name) == ID_SCE &&
176  ((Scene *)owner_id_hint)->master_collection == master_collection) {
177  return owner_id_hint;
178  }
179 
180  LISTBASE_FOREACH (Scene *, scene, &bmain->scenes) {
181  if (scene->master_collection == master_collection) {
182  return &scene->id;
183  }
184  }
185 
186  BLI_assert_msg(0, "Embedded collection with no owner. Critical Main inconsistency.");
187  return NULL;
188 }
189 
191 {
192  BKE_id_blend_write(writer, &collection->id);
193 
194  /* Shared function for collection data-blocks and scene master collection. */
195  BKE_previewimg_blend_write(writer, collection->preview);
196 
197  LISTBASE_FOREACH (CollectionObject *, cob, &collection->gobject) {
198  BLO_write_struct(writer, CollectionObject, cob);
199  }
200 
201  LISTBASE_FOREACH (CollectionChild *, child, &collection->children) {
202  BLO_write_struct(writer, CollectionChild, child);
203  }
204 }
205 
206 static void collection_blend_write(BlendWriter *writer, ID *id, const void *id_address)
207 {
208  Collection *collection = (Collection *)id;
209 
210  /* Clean up, important in undo case to reduce false detection of changed data-blocks. */
211  collection->flag &= ~COLLECTION_HAS_OBJECT_CACHE;
213  collection->tag = 0;
214  BLI_listbase_clear(&collection->object_cache);
216  BLI_listbase_clear(&collection->parents);
217 
218  /* write LibData */
219  BLO_write_id_struct(writer, Collection, id_address, &collection->id);
220 
221  BKE_collection_blend_write_nolib(writer, collection);
222 }
223 
224 #ifdef USE_COLLECTION_COMPAT_28
226 {
227  BLO_read_list(reader, &sc->objects);
228  BLO_read_list(reader, &sc->scene_collections);
229 
232  }
233 }
234 #endif
235 
237 {
238  BLO_read_list(reader, &collection->gobject);
239  BLO_read_list(reader, &collection->children);
240 
241  BLO_read_data_address(reader, &collection->preview);
242  BKE_previewimg_blend_read(reader, collection->preview);
243 
244  collection->flag &= ~COLLECTION_HAS_OBJECT_CACHE;
246  collection->tag = 0;
247  BLI_listbase_clear(&collection->object_cache);
249  BLI_listbase_clear(&collection->parents);
250 
251 #ifdef USE_COLLECTION_COMPAT_28
252  /* This runs before the very first doversion. */
253  BLO_read_data_address(reader, &collection->collection);
254  if (collection->collection != NULL) {
255  BKE_collection_compat_blend_read_data(reader, collection->collection);
256  }
257 
258  BLO_read_data_address(reader, &collection->view_layer);
259  if (collection->view_layer != NULL) {
260  BKE_view_layer_blend_read_data(reader, collection->view_layer);
261  }
262 #endif
263 }
264 
266 {
267  Collection *collection = (Collection *)id;
268  BKE_collection_blend_read_data(reader, collection);
269 }
270 
271 static void lib_link_collection_data(BlendLibReader *reader, Library *lib, Collection *collection)
272 {
273  LISTBASE_FOREACH_MUTABLE (CollectionObject *, cob, &collection->gobject) {
274  BLO_read_id_address(reader, lib, &cob->ob);
275 
276  if (cob->ob == NULL) {
277  BLI_freelinkN(&collection->gobject, cob);
278  }
279  }
280 
281  LISTBASE_FOREACH (CollectionChild *, child, &collection->children) {
282  BLO_read_id_address(reader, lib, &child->collection);
283  }
284 }
285 
286 #ifdef USE_COLLECTION_COMPAT_28
288  Library *lib,
289  SceneCollection *sc)
290 {
291  LISTBASE_FOREACH (LinkData *, link, &sc->objects) {
292  BLO_read_id_address(reader, lib, &link->data);
293  BLI_assert(link->data);
294  }
295 
298  }
299 }
300 #endif
301 
303 {
304 #ifdef USE_COLLECTION_COMPAT_28
305  if (collection->collection) {
306  BKE_collection_compat_blend_read_lib(reader, collection->id.lib, collection->collection);
307  }
308 
309  if (collection->view_layer) {
310  BKE_view_layer_blend_read_lib(reader, collection->id.lib, collection->view_layer);
311  }
312 #endif
313 
314  lib_link_collection_data(reader, collection->id.lib, collection);
315 }
316 
317 static void collection_blend_read_lib(BlendLibReader *reader, ID *id)
318 {
319  Collection *collection = (Collection *)id;
320  BKE_collection_blend_read_lib(reader, collection);
321 }
322 
323 #ifdef USE_COLLECTION_COMPAT_28
325  struct SceneCollection *sc)
326 {
327  LISTBASE_FOREACH (LinkData *, link, &sc->objects) {
328  BLO_expand(expander, link->data);
329  }
330 
333  }
334 }
335 #endif
336 
338 {
339  LISTBASE_FOREACH (CollectionObject *, cob, &collection->gobject) {
340  BLO_expand(expander, cob->ob);
341  }
342 
343  LISTBASE_FOREACH (CollectionChild *, child, &collection->children) {
344  BLO_expand(expander, child->collection);
345  }
346 
347 #ifdef USE_COLLECTION_COMPAT_28
348  if (collection->collection != NULL) {
349  BKE_collection_compat_blend_read_expand(expander, collection->collection);
350  }
351 #endif
352 }
353 
354 static void collection_blend_read_expand(BlendExpander *expander, ID *id)
355 {
356  Collection *collection = (Collection *)id;
357  BKE_collection_blend_read_expand(expander, collection);
358 }
359 
361  .id_code = ID_GR,
362  .id_filter = FILTER_ID_GR,
363  .main_listbase_index = INDEX_ID_GR,
364  .struct_size = sizeof(Collection),
365  .name = "Collection",
366  .name_plural = "collections",
367  .translation_context = BLT_I18NCONTEXT_ID_COLLECTION,
369  .asset_type_info = NULL,
370 
372  .copy_data = collection_copy_data,
373  .free_data = collection_free_data,
374  .make_local = NULL,
375  .foreach_id = collection_foreach_id,
376  .foreach_cache = NULL,
377  .foreach_path = NULL,
378  .owner_get = collection_owner_get,
379 
380  .blend_write = collection_blend_write,
381  .blend_read_data = collection_blend_read_data,
382  .blend_read_lib = collection_blend_read_lib,
383  .blend_read_expand = collection_blend_read_expand,
384 
385  .blend_read_undo_preserve = NULL,
386 
387  .lib_override_apply_post = NULL,
388 };
389 
392 /* -------------------------------------------------------------------- */
396 /* Add new collection, without view layer syncing. */
398  Collection *collection_parent,
399  const char *name_custom)
400 {
401  /* Determine new collection name. */
402  char name[MAX_NAME];
403 
404  if (name_custom) {
405  STRNCPY(name, name_custom);
406  }
407  else {
408  BKE_collection_new_name_get(collection_parent, name);
409  }
410 
411  /* Create new collection. */
412  Collection *collection = BKE_id_new(bmain, ID_GR, name);
413 
414  /* We increase collection user count when linking to Collections. */
415  id_us_min(&collection->id);
416 
417  /* Optionally add to parent collection. */
418  if (collection_parent) {
419  collection_child_add(collection_parent, collection, 0, true);
420  }
421 
422  return collection;
423 }
424 
425 Collection *BKE_collection_add(Main *bmain, Collection *collection_parent, const char *name_custom)
426 {
427  Collection *collection = collection_add(bmain, collection_parent, name_custom);
429  return collection;
430 }
431 
433  Scene *scene,
434  const Object *ob_src,
435  Collection *collection_dst)
436 {
437  bool is_instantiated = false;
438 
439  FOREACH_SCENE_COLLECTION_BEGIN (scene, collection) {
440  if (!ID_IS_LINKED(collection) && !ID_IS_OVERRIDABLE_LIBRARY(collection) &&
441  BKE_collection_has_object(collection, ob_src)) {
442  collection_child_add(collection, collection_dst, 0, true);
443  is_instantiated = true;
444  }
445  }
447 
448  if (!is_instantiated) {
449  collection_child_add(scene->master_collection, collection_dst, 0, true);
450  }
451 
453 }
454 
456  Scene *scene,
457  Collection *collection_src,
458  Collection *collection_dst)
459 {
460  bool is_instantiated = false;
461 
462  FOREACH_SCENE_COLLECTION_BEGIN (scene, collection) {
463  if (!ID_IS_LINKED(collection) && !ID_IS_OVERRIDABLE_LIBRARY(collection) &&
464  collection_find_child(collection, collection_src)) {
465  collection_child_add(collection, collection_dst, 0, true);
466  is_instantiated = true;
467  }
468  else if (!is_instantiated && collection_find_child(collection, collection_dst)) {
469  /* If given collection_dst is already instantiated in scene, even if its 'model' src one is
470  * not, do not add it to master scene collection. */
471  is_instantiated = true;
472  }
473  }
475 
476  if (!is_instantiated) {
477  collection_child_add(scene->master_collection, collection_dst, 0, true);
478  }
479 
481 }
482 
485 /* -------------------------------------------------------------------- */
490 {
491  BKE_libblock_free_data(&collection->id, false);
492  collection_free_data(&collection->id);
493 }
494 
495 bool BKE_collection_delete(Main *bmain, Collection *collection, bool hierarchy)
496 {
497  /* Master collection is not real datablock, can't be removed. */
498  if (collection->flag & COLLECTION_IS_MASTER) {
499  BLI_assert_msg(0, "Scene master collection can't be deleted");
500  return false;
501  }
502 
503  if (hierarchy) {
504  /* Remove child objects. */
505  CollectionObject *cob = collection->gobject.first;
506  while (cob != NULL) {
507  collection_object_remove(bmain, collection, cob->ob, true);
508  cob = collection->gobject.first;
509  }
510 
511  /* Delete all child collections recursively. */
512  CollectionChild *child = collection->children.first;
513  while (child != NULL) {
514  BKE_collection_delete(bmain, child->collection, hierarchy);
515  child = collection->children.first;
516  }
517  }
518  else {
519  /* Link child collections into parent collection. */
520  LISTBASE_FOREACH (CollectionChild *, child, &collection->children) {
521  LISTBASE_FOREACH (CollectionParent *, cparent, &collection->parents) {
522  Collection *parent = cparent->collection;
523  collection_child_add(parent, child->collection, 0, true);
524  }
525  }
526 
527  CollectionObject *cob = collection->gobject.first;
528  while (cob != NULL) {
529  /* Link child object into parent collections. */
530  LISTBASE_FOREACH (CollectionParent *, cparent, &collection->parents) {
531  Collection *parent = cparent->collection;
532  collection_object_add(bmain, parent, cob->ob, 0, true);
533  }
534 
535  /* Remove child object. */
536  collection_object_remove(bmain, collection, cob->ob, true);
537  cob = collection->gobject.first;
538  }
539  }
540 
541  BKE_id_delete(bmain, collection);
542 
544 
545  return true;
546 }
547 
550 /* -------------------------------------------------------------------- */
555  Collection *parent,
556  Collection *collection_old,
557  const eDupli_ID_Flags duplicate_flags,
558  const eLibIDDuplicateFlags duplicate_options)
559 {
560  Collection *collection_new;
561  bool do_full_process = false;
562  const bool is_collection_master = (collection_old->flag & COLLECTION_IS_MASTER) != 0;
563 
564  const bool do_objects = (duplicate_flags & USER_DUP_OBJECT) != 0;
565 
566  if (is_collection_master) {
567  /* We never duplicate master collections here, but we can still deep-copy their objects and
568  * collections. */
569  BLI_assert(parent == NULL);
570  collection_new = collection_old;
571  do_full_process = true;
572  }
573  else if (collection_old->id.newid == NULL) {
574  collection_new = (Collection *)BKE_id_copy_for_duplicate(
575  bmain, (ID *)collection_old, duplicate_flags, LIB_ID_COPY_DEFAULT);
576 
577  if (collection_new == collection_old) {
578  return collection_new;
579  }
580 
581  do_full_process = true;
582  }
583  else {
584  collection_new = (Collection *)collection_old->id.newid;
585  }
586 
587  /* Optionally add to parent (we always want to do that,
588  * even if collection_old had already been duplicated). */
589  if (parent != NULL) {
590  if (collection_child_add(parent, collection_new, 0, true)) {
591  /* Put collection right after existing one. */
592  CollectionChild *child = collection_find_child(parent, collection_old);
593  CollectionChild *child_new = collection_find_child(parent, collection_new);
594 
595  if (child && child_new) {
596  BLI_remlink(&parent->children, child_new);
597  BLI_insertlinkafter(&parent->children, child, child_new);
598  }
599  }
600  }
601 
602  /* If we are not doing any kind of deep-copy, we can return immediately.
603  * False do_full_process means collection_old had already been duplicated,
604  * no need to redo some deep-copy on it. */
605  if (!do_full_process) {
606  return collection_new;
607  }
608 
609  if (do_objects) {
610  /* We need to first duplicate the objects in a separate loop, to support the master collection
611  * case, where both old and new collections are the same.
612  * Otherwise, depending on naming scheme and sorting, we may end up duplicating the new objects
613  * we just added, in some infinite loop. */
614  LISTBASE_FOREACH (CollectionObject *, cob, &collection_old->gobject) {
615  Object *ob_old = cob->ob;
616 
617  if (ob_old->id.newid == NULL) {
619  bmain, ob_old, duplicate_flags, duplicate_options | LIB_ID_DUPLICATE_IS_SUBPROCESS);
620  }
621  }
622 
623  /* We can loop on collection_old's objects, but have to consider it mutable because with master
624  * collections collection_old and collection_new are the same data here. */
625  LISTBASE_FOREACH_MUTABLE (CollectionObject *, cob, &collection_old->gobject) {
626  Object *ob_old = cob->ob;
627  Object *ob_new = (Object *)ob_old->id.newid;
628 
629  /* New object can be NULL in master collection case, since new and old objects are in same
630  * collection. */
631  if (ELEM(ob_new, ob_old, NULL)) {
632  continue;
633  }
634 
635  collection_object_add(bmain, collection_new, ob_new, 0, true);
636  collection_object_remove(bmain, collection_new, ob_old, false);
637  }
638  }
639 
640  /* We can loop on collection_old's children,
641  * that list is currently identical the collection_new' children, and won't be changed here. */
642  LISTBASE_FOREACH_MUTABLE (CollectionChild *, child, &collection_old->children) {
643  Collection *child_collection_old = child->collection;
644 
645  Collection *child_collection_new = collection_duplicate_recursive(
646  bmain, collection_new, child_collection_old, duplicate_flags, duplicate_options);
647  if (child_collection_new != child_collection_old) {
648  collection_child_remove(collection_new, child_collection_old);
649  }
650  }
651 
652  return collection_new;
653 }
654 
656  Collection *parent,
657  Collection *collection,
658  eDupli_ID_Flags duplicate_flags,
659  eLibIDDuplicateFlags duplicate_options)
660 {
661  const bool is_subprocess = (duplicate_options & LIB_ID_DUPLICATE_IS_SUBPROCESS) != 0;
662  const bool is_root_id = (duplicate_options & LIB_ID_DUPLICATE_IS_ROOT_ID) != 0;
663 
664  if (!is_subprocess) {
666  }
667  if (is_root_id) {
668  /* In case root duplicated ID is linked, assume we want to get a local copy of it and duplicate
669  * all expected linked data. */
670  if (ID_IS_LINKED(collection)) {
671  duplicate_flags |= USER_DUP_LINKED_ID;
672  }
673  duplicate_options &= ~LIB_ID_DUPLICATE_IS_ROOT_ID;
674  }
675 
676  Collection *collection_new = collection_duplicate_recursive(
677  bmain, parent, collection, duplicate_flags, duplicate_options);
678 
679  if (!is_subprocess) {
680  /* `collection_duplicate_recursive` will also tag our 'root' collection, which is not required
681  * unless its duplication is a sub-process of another one. */
682  collection_new->id.tag &= ~LIB_TAG_NEW;
683 
684  /* This code will follow into all ID links using an ID tagged with LIB_TAG_NEW. */
685  BKE_libblock_relink_to_newid(bmain, &collection_new->id, 0);
686 
687 #ifndef NDEBUG
688  /* Call to `BKE_libblock_relink_to_newid` above is supposed to have cleared all those flags. */
689  ID *id_iter;
690  FOREACH_MAIN_ID_BEGIN (bmain, id_iter) {
691  if (id_iter->tag & LIB_TAG_NEW) {
692  BLI_assert((id_iter->tag & LIB_TAG_NEW) == 0);
693  }
694  }
696 #endif
697 
698  /* Cleanup. */
700 
702  }
703 
704  return collection_new;
705 }
706 
709 /* -------------------------------------------------------------------- */
713 void BKE_collection_new_name_get(Collection *collection_parent, char *rname)
714 {
715  char *name;
716 
717  if (!collection_parent) {
718  name = BLI_strdup(DATA_("Collection"));
719  }
720  else if (collection_parent->flag & COLLECTION_IS_MASTER) {
721  name = BLI_sprintfN(DATA_("Collection %d"),
722  BLI_listbase_count(&collection_parent->children) + 1);
723  }
724  else {
725  const int number = BLI_listbase_count(&collection_parent->children) + 1;
726  const int digits = integer_digits_i(number);
727  const int max_len = sizeof(collection_parent->id.name) - 1 /* NULL terminator */ -
728  (1 + digits) /* " %d" */ - 2 /* ID */;
729  name = BLI_sprintfN("%.*s %d", max_len, collection_parent->id.name + 2, number);
730  }
731 
732  BLI_strncpy(rname, name, MAX_NAME);
733  MEM_freeN(name);
734 }
735 
736 const char *BKE_collection_ui_name_get(struct Collection *collection)
737 {
738  if (collection->flag & COLLECTION_IS_MASTER) {
739  return IFACE_("Scene Collection");
740  }
741 
742  return collection->id.name + 2;
743 }
744 
747 /* -------------------------------------------------------------------- */
752  Collection *collection,
753  int parent_restrict,
754  bool with_instances)
755 {
756  int child_restrict = collection->flag | parent_restrict;
757 
758  LISTBASE_FOREACH (CollectionObject *, cob, &collection->gobject) {
759  Base *base = BLI_findptr(lb, cob->ob, offsetof(Base, object));
760 
761  if (base == NULL) {
762  base = MEM_callocN(sizeof(Base), "Object Base");
763  base->object = cob->ob;
764  BLI_addtail(lb, base);
765  if (with_instances && cob->ob->instance_collection) {
767  lb, cob->ob->instance_collection, child_restrict, with_instances);
768  }
769  }
770 
771  /* Only collection flags are checked here currently, object restrict flag is checked
772  * in FOREACH_COLLECTION_VISIBLE_OBJECT_RECURSIVE_BEGIN since it can be animated
773  * without updating the cache. */
774  if (((child_restrict & COLLECTION_HIDE_VIEWPORT) == 0)) {
775  base->flag |= BASE_ENABLED_VIEWPORT;
776  }
777  if (((child_restrict & COLLECTION_HIDE_RENDER) == 0)) {
778  base->flag |= BASE_ENABLED_RENDER;
779  }
780  }
781 
782  LISTBASE_FOREACH (CollectionChild *, child, &collection->children) {
783  collection_object_cache_fill(lb, child->collection, child_restrict, with_instances);
784  }
785 }
786 
788 {
789  if (!(collection->flag & COLLECTION_HAS_OBJECT_CACHE)) {
790  static ThreadMutex cache_lock = BLI_MUTEX_INITIALIZER;
791 
792  BLI_mutex_lock(&cache_lock);
793  if (!(collection->flag & COLLECTION_HAS_OBJECT_CACHE)) {
794  collection_object_cache_fill(&collection->object_cache, collection, 0, false);
795  collection->flag |= COLLECTION_HAS_OBJECT_CACHE;
796  }
797  BLI_mutex_unlock(&cache_lock);
798  }
799 
800  return collection->object_cache;
801 }
802 
804 {
805  if (!(collection->flag & COLLECTION_HAS_OBJECT_CACHE_INSTANCED)) {
806  static ThreadMutex cache_lock = BLI_MUTEX_INITIALIZER;
807 
808  BLI_mutex_lock(&cache_lock);
809  if (!(collection->flag & COLLECTION_HAS_OBJECT_CACHE_INSTANCED)) {
810  collection_object_cache_fill(&collection->object_cache_instanced, collection, 0, true);
812  }
813  BLI_mutex_unlock(&cache_lock);
814  }
815 
816  return collection->object_cache_instanced;
817 }
818 
819 static void collection_object_cache_free(Collection *collection)
820 {
821  /* Clear own cache an for all parents, since those are affected by changes as well. */
822  collection->flag &= ~COLLECTION_HAS_OBJECT_CACHE;
824  BLI_freelistN(&collection->object_cache);
826 
827  LISTBASE_FOREACH (CollectionParent *, parent, &collection->parents) {
828  collection_object_cache_free(parent->collection);
829  }
830 }
831 
833 {
834  collection_object_cache_free(collection);
835 }
836 
838 {
839  if (collection) {
840  return BKE_collection_object_cache_get(collection).first;
841  }
842 
843  return FIRSTBASE(view_layer);
844 }
845 
848 /* -------------------------------------------------------------------- */
853 {
854  /* Not an actual datablock, but owned by scene. */
855  Collection *master_collection = BKE_libblock_alloc(
857  master_collection->id.flag |= LIB_EMBEDDED_DATA;
858  master_collection->flag |= COLLECTION_IS_MASTER;
859  master_collection->color_tag = COLLECTION_COLOR_NONE;
860  return master_collection;
861 }
862 
865 /* -------------------------------------------------------------------- */
870 {
871  if (object->instance_collection) {
872  Collection *dup_collection = object->instance_collection;
873  if ((dup_collection->id.tag & LIB_TAG_DOIT) == 0) {
874  /* Cycle already exists in collections, let's prevent further crappyness */
875  return true;
876  }
877  /* flag the object to identify cyclic dependencies in further dupli collections */
878  dup_collection->id.tag &= ~LIB_TAG_DOIT;
879 
880  if (dup_collection == collection) {
881  return true;
882  }
883 
884  FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN (dup_collection, collection_object) {
885  if (collection_object_cyclic_check_internal(collection_object, dup_collection)) {
886  return true;
887  }
888  }
890 
891  /* un-flag the object, it's allowed to have the same collection multiple times in parallel */
892  dup_collection->id.tag |= LIB_TAG_DOIT;
893  }
894 
895  return false;
896 }
897 
898 bool BKE_collection_object_cyclic_check(Main *bmain, Object *object, Collection *collection)
899 {
900  /* first flag all collections */
902 
903  return collection_object_cyclic_check_internal(object, collection);
904 }
905 
908 /* -------------------------------------------------------------------- */
912 bool BKE_collection_has_object(Collection *collection, const Object *ob)
913 {
914  if (ELEM(NULL, collection, ob)) {
915  return false;
916  }
917 
918  return (BLI_findptr(&collection->gobject, ob, offsetof(CollectionObject, ob)));
919 }
920 
922 {
923  if (ELEM(NULL, collection, ob)) {
924  return false;
925  }
926 
927  const ListBase objects = BKE_collection_object_cache_get(collection);
928  return (BLI_findptr(&objects, ob, offsetof(Base, object)));
929 }
930 
932 {
933  if (ELEM(NULL, collection, ob)) {
934  return false;
935  }
936 
937  const ListBase objects = BKE_collection_object_cache_instanced_get(collection);
938  return (BLI_findptr(&objects, ob, offsetof(Base, object)));
939 }
940 
942 {
943  if (scene && collection == scene->master_collection) {
944  return bmain->collections.first;
945  }
946 
947  return collection->id.next;
948 }
949 
951  Scene *scene,
952  Collection *collection,
953  Object *ob)
954 {
955  if (collection) {
956  collection = collection_next_find(bmain, scene, collection);
957  }
958  else if (scene) {
959  collection = scene->master_collection;
960  }
961  else {
962  collection = bmain->collections.first;
963  }
964 
965  while (collection) {
966  if (BKE_collection_has_object(collection, ob)) {
967  return collection;
968  }
969  collection = collection_next_find(bmain, scene, collection);
970  }
971  return NULL;
972 }
973 
974 bool BKE_collection_is_empty(const Collection *collection)
975 {
976  return BLI_listbase_is_empty(&collection->gobject) &&
977  BLI_listbase_is_empty(&collection->children);
978 }
979 
982 /* -------------------------------------------------------------------- */
987  Collection *collection,
988  const int flag)
989 {
990  if (collection->flag & COLLECTION_IS_MASTER) {
991  return;
992  }
993 
994  DEG_id_tag_update_ex(bmain, &collection->id, flag);
995 
996  LISTBASE_FOREACH (CollectionParent *, collection_parent, &collection->parents) {
997  if (collection_parent->collection->flag & COLLECTION_IS_MASTER) {
998  /* We don't care about scene/master collection here. */
999  continue;
1000  }
1001  collection_tag_update_parent_recursive(bmain, collection_parent->collection, flag);
1002  }
1003 }
1004 
1006  Collection *collection)
1007 {
1008  if (!ID_IS_LINKED(collection) && !ID_IS_OVERRIDE_LIBRARY(collection) &&
1009  (view_layer == NULL || BKE_view_layer_has_collection(view_layer, collection))) {
1010  return collection;
1011  }
1012 
1013  if (collection->flag & COLLECTION_IS_MASTER) {
1014  return NULL;
1015  }
1016 
1017  LISTBASE_FOREACH (CollectionParent *, collection_parent, &collection->parents) {
1018  if (!ID_IS_LINKED(collection_parent->collection) &&
1019  !ID_IS_OVERRIDE_LIBRARY(collection_parent->collection)) {
1020  if (view_layer != NULL &&
1021  !BKE_view_layer_has_collection(view_layer, collection_parent->collection)) {
1022  /* In case this parent collection is not in given view_layer, there is no point in
1023  * searching in its ancestors either, we can skip that whole parenting branch. */
1024  continue;
1025  }
1026  return collection_parent->collection;
1027  }
1029  view_layer, collection_parent->collection);
1030  if (editable_collection != NULL) {
1031  return editable_collection;
1032  }
1033  }
1034 
1035  return NULL;
1036 }
1037 
1039  Main *bmain, Collection *collection, Object *ob, int flag, const bool add_us)
1040 {
1041  if (ob->instance_collection) {
1042  /* Cyclic dependency check. */
1044  ob->instance_collection == collection) {
1045  return false;
1046  }
1047  }
1048 
1049  CollectionObject *cob = BLI_findptr(&collection->gobject, ob, offsetof(CollectionObject, ob));
1050  if (cob) {
1051  return false;
1052  }
1053 
1054  cob = MEM_callocN(sizeof(CollectionObject), __func__);
1055  cob->ob = ob;
1056  BLI_addtail(&collection->gobject, cob);
1058 
1059  if (add_us && (flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
1060  id_us_plus(&ob->id);
1061  }
1062 
1063  if ((flag & LIB_ID_CREATE_NO_MAIN) == 0) {
1065  }
1066 
1067  if ((flag & LIB_ID_CREATE_NO_MAIN) == 0) {
1068  BKE_rigidbody_main_collection_object_add(bmain, collection, ob);
1069  }
1070 
1071  return true;
1072 }
1073 
1074 static bool collection_object_remove(Main *bmain,
1075  Collection *collection,
1076  Object *ob,
1077  const bool free_us)
1078 {
1079  CollectionObject *cob = BLI_findptr(&collection->gobject, ob, offsetof(CollectionObject, ob));
1080  if (cob == NULL) {
1081  return false;
1082  }
1083 
1084  BLI_freelinkN(&collection->gobject, cob);
1086 
1087  if (free_us) {
1088  BKE_id_free_us(bmain, ob);
1089  }
1090  else {
1091  id_us_min(&ob->id);
1092  }
1093 
1095 
1096  return true;
1097 }
1098 
1100 {
1101  if (ob == NULL) {
1102  return false;
1103  }
1104 
1105  /* Only case where this pointer can be NULL is when scene itself is linked, this case should
1106  * never be reached. */
1107  BLI_assert(collection != NULL);
1108  if (collection == NULL) {
1109  return false;
1110  }
1111 
1112  if (!collection_object_add(bmain, collection, ob, 0, true)) {
1113  return false;
1114  }
1115 
1116  if (BKE_collection_is_in_scene(collection)) {
1117  BKE_main_collection_sync(bmain);
1118  }
1119 
1120  DEG_id_tag_update(&collection->id, ID_RECALC_GEOMETRY);
1121 
1122  return true;
1123 }
1124 
1125 bool BKE_collection_object_add(Main *bmain, Collection *collection, Object *ob)
1126 {
1127  return BKE_collection_viewlayer_object_add(bmain, NULL, collection, ob);
1128 }
1129 
1131  const ViewLayer *view_layer,
1132  Collection *collection,
1133  Object *ob)
1134 {
1135  if (collection == NULL) {
1136  return false;
1137  }
1138 
1139  collection = BKE_collection_parent_editable_find_recursive(view_layer, collection);
1140 
1141  if (collection == NULL) {
1142  return false;
1143  }
1144 
1145  return BKE_collection_object_add_notest(bmain, collection, ob);
1146 }
1147 
1149 {
1150  bool is_instantiated = false;
1151 
1152  FOREACH_SCENE_COLLECTION_BEGIN (scene, collection) {
1153  if (!ID_IS_LINKED(collection) && !ID_IS_OVERRIDE_LIBRARY(collection) &&
1154  BKE_collection_has_object(collection, ob_src)) {
1155  collection_object_add(bmain, collection, ob_dst, 0, true);
1156  is_instantiated = true;
1157  }
1158  }
1160 
1161  if (!is_instantiated) {
1162  /* In case we could not find any non-linked collections in which instantiate our ob_dst,
1163  * fallback to scene's master collection... */
1164  collection_object_add(bmain, scene->master_collection, ob_dst, 0, true);
1165  }
1166 
1167  BKE_main_collection_sync(bmain);
1168 }
1169 
1171  Collection *collection,
1172  Object *ob,
1173  const bool free_us)
1174 {
1175  if (ELEM(NULL, collection, ob)) {
1176  return false;
1177  }
1178 
1179  if (!collection_object_remove(bmain, collection, ob, free_us)) {
1180  return false;
1181  }
1182 
1183  if (BKE_collection_is_in_scene(collection)) {
1184  BKE_main_collection_sync(bmain);
1185  }
1186 
1187  DEG_id_tag_update(&collection->id, ID_RECALC_GEOMETRY);
1188 
1189  return true;
1190 }
1191 
1197  Main *bmain, Scene *scene, Object *ob, const bool free_us, Collection *collection_skip)
1198 {
1199  bool removed = false;
1200 
1201  /* If given object is removed from all collections in given scene, then it can also be safely
1202  * removed from rigidbody world for given scene. */
1203  if (collection_skip == NULL) {
1204  BKE_scene_remove_rigidbody_object(bmain, scene, ob, free_us);
1205  }
1206 
1207  FOREACH_SCENE_COLLECTION_BEGIN (scene, collection) {
1208  if (ID_IS_LINKED(collection) || ID_IS_OVERRIDE_LIBRARY(collection)) {
1209  continue;
1210  }
1211  if (collection == collection_skip) {
1212  continue;
1213  }
1214 
1215  removed |= collection_object_remove(bmain, collection, ob, free_us);
1216  }
1218 
1219  BKE_main_collection_sync(bmain);
1220 
1221  return removed;
1222 }
1223 
1224 bool BKE_scene_collections_object_remove(Main *bmain, Scene *scene, Object *ob, const bool free_us)
1225 {
1226  return scene_collections_object_remove(bmain, scene, ob, free_us, NULL);
1227 }
1228 
1229 /*
1230  * Remove all NULL objects from collections.
1231  * This is used for library remapping, where these pointers have been set to NULL.
1232  * Otherwise this should never happen.
1233  */
1235 {
1236  bool changed = false;
1237 
1238  LISTBASE_FOREACH_MUTABLE (CollectionObject *, cob, &collection->gobject) {
1239  if (cob->ob == NULL) {
1240  BLI_freelinkN(&collection->gobject, cob);
1241  changed = true;
1242  }
1243  }
1244 
1245  if (changed) {
1247  }
1248 }
1249 
1251 {
1252  LISTBASE_FOREACH (Scene *, scene, &bmain->scenes) {
1254  }
1255 
1256  LISTBASE_FOREACH (Collection *, collection, &bmain->collections) {
1257  collection_object_remove_nulls(collection);
1258  }
1259 }
1260 
1261 /*
1262  * Remove all duplicate objects from collections.
1263  * This is used for library remapping, happens when remapping an object to another one already
1264  * present in the collection. Otherwise this should never happen.
1265  */
1267 {
1268  bool changed = false;
1269 
1270  LISTBASE_FOREACH_MUTABLE (CollectionObject *, cob, &collection->gobject) {
1271  if (cob->ob->runtime.collection_management) {
1272  BLI_freelinkN(&collection->gobject, cob);
1273  changed = true;
1274  continue;
1275  }
1276  cob->ob->runtime.collection_management = true;
1277  }
1278 
1279  /* Cleanup. */
1280  LISTBASE_FOREACH (CollectionObject *, cob, &collection->gobject) {
1281  cob->ob->runtime.collection_management = false;
1282  }
1283 
1284  if (changed) {
1286  }
1287 }
1288 
1290 {
1291  LISTBASE_FOREACH (Object *, ob, &bmain->objects) {
1292  ob->runtime.collection_management = false;
1293  }
1294 
1295  LISTBASE_FOREACH (Scene *, scene, &bmain->scenes) {
1297  }
1298 
1299  LISTBASE_FOREACH (Collection *, collection, &bmain->collections) {
1301  }
1302 }
1303 
1305 {
1306  LISTBASE_FOREACH_MUTABLE (CollectionChild *, child, &collection->children) {
1307  if (child->collection == NULL) {
1308  BLI_freelinkN(&collection->children, child);
1309  }
1310  }
1311 }
1312 
1314 {
1315  LISTBASE_FOREACH_MUTABLE (CollectionParent *, parent, &collection->parents) {
1316  if ((parent->collection == NULL) || !collection_find_child(parent->collection, collection)) {
1317  BLI_freelinkN(&collection->parents, parent);
1318  }
1319  }
1320 }
1321 
1323  Collection *parent_collection,
1324  Collection *child_collection)
1325 {
1326  if (child_collection == NULL) {
1327  if (parent_collection != NULL) {
1328  collection_null_children_remove(parent_collection);
1329  }
1330  else {
1331  /* We need to do the checks in two steps when more than one collection may be involved,
1332  * otherwise we can miss some cases...
1333  * Also, master collections are not in bmain, so we also need to loop over scenes.
1334  */
1335  LISTBASE_FOREACH (Collection *, collection, &bmain->collections) {
1336  collection_null_children_remove(collection);
1337  }
1338  LISTBASE_FOREACH (Scene *, scene, &bmain->scenes) {
1340  }
1341  }
1342 
1343  LISTBASE_FOREACH (Collection *, collection, &bmain->collections) {
1345  }
1346  LISTBASE_FOREACH (Scene *, scene, &bmain->scenes) {
1348  }
1349  }
1350  else {
1351  LISTBASE_FOREACH_MUTABLE (CollectionParent *, parent, &child_collection->parents) {
1352  collection_null_children_remove(parent->collection);
1353 
1354  if (!collection_find_child(parent->collection, child_collection)) {
1355  BLI_freelinkN(&child_collection->parents, parent);
1356  }
1357  }
1358  }
1359 }
1360 
1362  Main *bmain, Scene *scene, Collection *collection_dst, Collection *collection_src, Object *ob)
1363 {
1364  /* In both cases we first add the object, then remove it from the other collections.
1365  * Otherwise we lose the original base and whether it was active and selected. */
1366  if (collection_src != NULL) {
1367  if (BKE_collection_object_add(bmain, collection_dst, ob)) {
1368  BKE_collection_object_remove(bmain, collection_src, ob, false);
1369  }
1370  }
1371  else {
1372  /* Adding will fail if object is already in collection.
1373  * However we still need to remove it from the other collections. */
1374  BKE_collection_object_add(bmain, collection_dst, ob);
1375  scene_collections_object_remove(bmain, scene, ob, false, collection_dst);
1376  }
1377 }
1378 
1381 /* -------------------------------------------------------------------- */
1386 {
1387  if (collection->flag & COLLECTION_IS_MASTER) {
1388  return true;
1389  }
1390 
1391  LISTBASE_FOREACH (CollectionParent *, cparent, &collection->parents) {
1392  if (BKE_collection_is_in_scene(cparent->collection)) {
1393  return true;
1394  }
1395  }
1396 
1397  return false;
1398 }
1399 
1401 {
1402  /* Need to update layer collections because objects might have changed
1403  * in linked files, and because undo push does not include updated base
1404  * flags since those are refreshed after the operator completes. */
1405  BKE_main_collection_sync(bmain);
1406 }
1407 
1410 /* -------------------------------------------------------------------- */
1415  Collection *instance_collection)
1416 {
1417  LISTBASE_FOREACH (CollectionObject *, collection_object, &collection->gobject) {
1418  if (collection_object->ob != NULL &&
1419  /* Object from a given collection should never instantiate that collection either. */
1420  ELEM(collection_object->ob->instance_collection, instance_collection, collection)) {
1421  return true;
1422  }
1423  }
1424 
1425  LISTBASE_FOREACH (CollectionChild *, collection_child, &collection->children) {
1426  if (collection_child->collection != NULL &&
1427  collection_instance_find_recursive(collection_child->collection, instance_collection)) {
1428  return true;
1429  }
1430  }
1431 
1432  return false;
1433 }
1434 
1435 bool BKE_collection_cycle_find(Collection *new_ancestor, Collection *collection)
1436 {
1437  if (collection == new_ancestor) {
1438  return true;
1439  }
1440 
1441  if (collection == NULL) {
1442  collection = new_ancestor;
1443  }
1444 
1445  LISTBASE_FOREACH (CollectionParent *, parent, &new_ancestor->parents) {
1446  if (BKE_collection_cycle_find(parent->collection, collection)) {
1447  return true;
1448  }
1449  }
1450 
1451  /* Find possible objects in collection or its children, that would instantiate the given ancestor
1452  * collection (that would also make a fully invalid cycle of dependencies). */
1453  return collection_instance_find_recursive(collection, new_ancestor);
1454 }
1455 
1456 static bool collection_instance_fix_recursive(Collection *parent_collection,
1457  Collection *collection)
1458 {
1459  bool cycles_found = false;
1460 
1461  LISTBASE_FOREACH (CollectionObject *, collection_object, &parent_collection->gobject) {
1462  if (collection_object->ob != NULL &&
1463  collection_object->ob->instance_collection == collection) {
1464  id_us_min(&collection->id);
1465  collection_object->ob->instance_collection = NULL;
1466  cycles_found = true;
1467  }
1468  }
1469 
1470  LISTBASE_FOREACH (CollectionChild *, collection_child, &parent_collection->children) {
1471  if (collection_instance_fix_recursive(collection_child->collection, collection)) {
1472  cycles_found = true;
1473  }
1474  }
1475 
1476  return cycles_found;
1477 }
1478 
1480  Collection *parent_collection,
1481  Collection *collection)
1482 {
1483  bool cycles_found = false;
1484 
1485  LISTBASE_FOREACH_MUTABLE (CollectionParent *, parent, &parent_collection->parents) {
1486  if (BKE_collection_cycle_find(parent->collection, collection)) {
1487  BKE_collection_child_remove(bmain, parent->collection, parent_collection);
1488  cycles_found = true;
1489  }
1490  else if (collection_cycle_fix_recursive(bmain, parent->collection, collection)) {
1491  cycles_found = true;
1492  }
1493  }
1494 
1495  return cycles_found;
1496 }
1497 
1498 bool BKE_collection_cycles_fix(Main *bmain, Collection *collection)
1499 {
1500  return collection_cycle_fix_recursive(bmain, collection, collection) ||
1501  collection_instance_fix_recursive(collection, collection);
1502 }
1503 
1505 {
1506  return BLI_findptr(&parent->children, collection, offsetof(CollectionChild, collection));
1507 }
1508 
1509 static bool collection_find_child_recursive(const Collection *parent, const Collection *collection)
1510 {
1511  LISTBASE_FOREACH (const CollectionChild *, child, &parent->children) {
1512  if (child->collection == collection) {
1513  return true;
1514  }
1515 
1516  if (collection_find_child_recursive(child->collection, collection)) {
1517  return true;
1518  }
1519  }
1520 
1521  return false;
1522 }
1523 
1524 bool BKE_collection_has_collection(const Collection *parent, const Collection *collection)
1525 {
1526  return collection_find_child_recursive(parent, collection);
1527 }
1528 
1530 {
1531  return BLI_findptr(&child->parents, collection, offsetof(CollectionParent, collection));
1532 }
1533 
1534 static bool collection_child_add(Collection *parent,
1535  Collection *collection,
1536  const int flag,
1537  const bool add_us)
1538 {
1539  CollectionChild *child = collection_find_child(parent, collection);
1540  if (child) {
1541  return false;
1542  }
1543  if (BKE_collection_cycle_find(parent, collection)) {
1544  return false;
1545  }
1546 
1547  child = MEM_callocN(sizeof(CollectionChild), "CollectionChild");
1548  child->collection = collection;
1549  BLI_addtail(&parent->children, child);
1550 
1551  /* Don't add parent links for depsgraph datablocks, these are not kept in sync. */
1552  if ((flag & LIB_ID_CREATE_NO_MAIN) == 0) {
1553  CollectionParent *cparent = MEM_callocN(sizeof(CollectionParent), "CollectionParent");
1554  cparent->collection = parent;
1555  BLI_addtail(&collection->parents, cparent);
1556  }
1557 
1558  if (add_us) {
1559  id_us_plus(&collection->id);
1560  }
1561 
1563 
1564  return true;
1565 }
1566 
1567 static bool collection_child_remove(Collection *parent, Collection *collection)
1568 {
1569  CollectionChild *child = collection_find_child(parent, collection);
1570  if (child == NULL) {
1571  return false;
1572  }
1573 
1574  CollectionParent *cparent = collection_find_parent(collection, parent);
1575  BLI_freelinkN(&collection->parents, cparent);
1576  BLI_freelinkN(&parent->children, child);
1577 
1578  id_us_min(&collection->id);
1579 
1581 
1582  return true;
1583 }
1584 
1586 {
1587  if (!collection_child_add(parent, child, 0, true)) {
1588  return false;
1589  }
1590 
1591  BKE_main_collection_sync(bmain);
1592  return true;
1593 }
1594 
1596 {
1597  return collection_child_add(parent, child, 0, true);
1598 }
1599 
1601 {
1602  if (!collection_child_remove(parent, child)) {
1603  return false;
1604  }
1605 
1606  BKE_main_collection_sync(bmain);
1607  return true;
1608 }
1609 
1611 {
1612  LISTBASE_FOREACH_MUTABLE (CollectionChild *, child, &collection->children) {
1613  /* Check for duplicated children (can happen with remapping e.g.). */
1614  CollectionChild *other_child = collection_find_child(collection, child->collection);
1615  if (other_child != child) {
1616  BLI_freelinkN(&collection->children, child);
1617  continue;
1618  }
1619 
1620  /* Invalid child, either without a collection, or because it creates a dependency cycle. */
1621  if (child->collection == NULL || BKE_collection_cycle_find(collection, child->collection)) {
1622  BLI_freelinkN(&collection->children, child);
1623  continue;
1624  }
1625 
1626  /* Can happen when remapping data partially out-of-Main (during advanced ID management
1627  * operations like lib-override resync e.g.). */
1628  if ((child->collection->id.tag & (LIB_TAG_NO_MAIN | LIB_TAG_COPIED_ON_WRITE)) != 0) {
1629  continue;
1630  }
1631 
1632  BLI_assert(collection_find_parent(child->collection, collection) == NULL);
1633  CollectionParent *cparent = MEM_callocN(sizeof(CollectionParent), __func__);
1634  cparent->collection = collection;
1635  BLI_addtail(&child->collection->parents, cparent);
1636  }
1637 }
1638 
1640 {
1641  /* A same collection may be child of several others, no need to process it more than once. */
1642  if ((collection->tag & COLLECTION_TAG_RELATION_REBUILD) == 0) {
1643  return;
1644  }
1645 
1647  collection->tag &= ~COLLECTION_TAG_RELATION_REBUILD;
1648 
1649  LISTBASE_FOREACH (CollectionChild *, child, &collection->children) {
1650  /* See comment above in `BKE_collection_parent_relations_rebuild`. */
1651  if ((child->collection->id.tag & (LIB_TAG_NO_MAIN | LIB_TAG_COPIED_ON_WRITE)) != 0) {
1652  continue;
1653  }
1654  collection_parents_rebuild_recursive(child->collection);
1655  }
1656 }
1657 
1659 {
1660  /* Only collections not in bmain (master ones in scenes) have no parent... */
1661  LISTBASE_FOREACH (Collection *, collection, &bmain->collections) {
1662  BLI_freelistN(&collection->parents);
1663 
1664  collection->tag |= COLLECTION_TAG_RELATION_REBUILD;
1665  }
1666 
1667  /* Scene's master collections will be 'root' parent of most of our collections, so start with
1668  * them. */
1669  LISTBASE_FOREACH (Scene *, scene, &bmain->scenes) {
1670  /* This function can be called from readfile.c, when this pointer is not guaranteed to be NULL.
1671  */
1672  if (scene->master_collection != NULL) {
1676  }
1677  }
1678 
1679  /* We may have parent chains outside of scene's master_collection context? At least, readfile's
1680  * lib_link_collection_data() seems to assume that, so do the same here. */
1681  LISTBASE_FOREACH (Collection *, collection, &bmain->collections) {
1682  if (collection->tag & COLLECTION_TAG_RELATION_REBUILD) {
1683  /* NOTE: we do not have easy access to 'which collections is root' info in that case, which
1684  * means test for cycles in collection relationships may fail here. I don't think that is an
1685  * issue in practice here, but worth keeping in mind... */
1687  }
1688  }
1689 }
1690 
1693 /* -------------------------------------------------------------------- */
1698  const int index,
1699  int *index_current)
1700 {
1701  if (index == (*index_current)) {
1702  return collection;
1703  }
1704 
1705  (*index_current)++;
1706 
1707  LISTBASE_FOREACH (CollectionChild *, child, &collection->children) {
1708  Collection *nested = collection_from_index_recursive(child->collection, index, index_current);
1709  if (nested != NULL) {
1710  return nested;
1711  }
1712  }
1713  return NULL;
1714 }
1715 
1717 {
1718  int index_current = 0;
1719  Collection *master_collection = scene->master_collection;
1720  return collection_from_index_recursive(master_collection, index, &index_current);
1721 }
1722 
1723 static bool collection_objects_select(ViewLayer *view_layer, Collection *collection, bool deselect)
1724 {
1725  bool changed = false;
1726 
1727  if (collection->flag & COLLECTION_HIDE_SELECT) {
1728  return false;
1729  }
1730 
1731  LISTBASE_FOREACH (CollectionObject *, cob, &collection->gobject) {
1732  Base *base = BKE_view_layer_base_find(view_layer, cob->ob);
1733 
1734  if (base) {
1735  if (deselect) {
1736  if (base->flag & BASE_SELECTED) {
1737  base->flag &= ~BASE_SELECTED;
1738  changed = true;
1739  }
1740  }
1741  else {
1742  if ((base->flag & BASE_SELECTABLE) && !(base->flag & BASE_SELECTED)) {
1743  base->flag |= BASE_SELECTED;
1744  changed = true;
1745  }
1746  }
1747  }
1748  }
1749 
1750  LISTBASE_FOREACH (CollectionChild *, child, &collection->children) {
1751  if (collection_objects_select(view_layer, collection, deselect)) {
1752  changed = true;
1753  }
1754  }
1755 
1756  return changed;
1757 }
1758 
1759 bool BKE_collection_objects_select(ViewLayer *view_layer, Collection *collection, bool deselect)
1760 {
1762  collection);
1763 
1764  if (layer_collection != NULL) {
1765  return BKE_layer_collection_objects_select(view_layer, layer_collection, deselect);
1766  }
1767 
1768  return collection_objects_select(view_layer, collection, deselect);
1769 }
1770 
1773 /* -------------------------------------------------------------------- */
1778  Collection *to_parent,
1779  Collection *from_parent,
1780  Collection *relative,
1781  bool relative_after,
1782  Collection *collection)
1783 {
1784  if (collection->flag & COLLECTION_IS_MASTER) {
1785  return false;
1786  }
1787  if (BKE_collection_cycle_find(to_parent, collection)) {
1788  return false;
1789  }
1790 
1791  /* Move to new parent collection */
1792  if (from_parent) {
1793  collection_child_remove(from_parent, collection);
1794  }
1795 
1796  collection_child_add(to_parent, collection, 0, true);
1797 
1798  /* Move to specified location under parent. */
1799  if (relative) {
1800  CollectionChild *child = collection_find_child(to_parent, collection);
1801  CollectionChild *relative_child = collection_find_child(to_parent, relative);
1802 
1803  if (relative_child) {
1804  BLI_remlink(&to_parent->children, child);
1805 
1806  if (relative_after) {
1807  BLI_insertlinkafter(&to_parent->children, relative_child, child);
1808  }
1809  else {
1810  BLI_insertlinkbefore(&to_parent->children, relative_child, child);
1811  }
1812 
1814  }
1815  }
1816 
1817  /* Update layer collections. */
1818  BKE_main_collection_sync(bmain);
1819 
1820  return true;
1821 }
1822 
1825 /* -------------------------------------------------------------------- */
1829 /* Scene collection iterator. */
1830 
1831 typedef struct CollectionsIteratorData {
1833  void **array;
1834  int tot, cur;
1836 
1837 static void scene_collection_callback(Collection *collection,
1839  void *data)
1840 {
1841  callback(collection, data);
1842 
1843  LISTBASE_FOREACH (CollectionChild *, child, &collection->children) {
1844  scene_collection_callback(child->collection, callback, data);
1845  }
1846 }
1847 
1848 static void scene_collections_count(Collection *UNUSED(collection), void *data)
1849 {
1850  int *tot = data;
1851  (*tot)++;
1852 }
1853 
1854 static void scene_collections_build_array(Collection *collection, void *data)
1855 {
1856  Collection ***array = data;
1857  **array = collection;
1858  (*array)++;
1859 }
1860 
1862  Collection ***r_collections_array,
1863  int *r_collections_array_len)
1864 {
1865  *r_collections_array = NULL;
1866  *r_collections_array_len = 0;
1867 
1868  if (scene == NULL) {
1869  return;
1870  }
1871 
1872  Collection *collection = scene->master_collection;
1873  BLI_assert(collection != NULL);
1874  scene_collection_callback(collection, scene_collections_count, r_collections_array_len);
1875 
1876  BLI_assert(*r_collections_array_len > 0);
1877 
1879  *r_collections_array_len, sizeof(Collection *), "CollectionArray");
1880  *r_collections_array = array;
1882 }
1883 
1885 {
1886  Scene *scene = data_in;
1888 
1889  data->scene = scene;
1890 
1891  BLI_ITERATOR_INIT(iter);
1892  iter->data = data;
1893 
1894  scene_collections_array(scene, (Collection ***)&data->array, &data->tot);
1895  BLI_assert(data->tot != 0);
1896 
1897  data->cur = 0;
1898  iter->current = data->array[data->cur];
1899 }
1900 
1902 {
1904 
1905  if (++data->cur < data->tot) {
1906  iter->current = data->array[data->cur];
1907  }
1908  else {
1909  iter->valid = false;
1910  }
1911 }
1912 
1914 {
1916 
1917  if (data) {
1918  if (data->array) {
1919  MEM_freeN(data->array);
1920  }
1921  MEM_freeN(data);
1922  }
1923  iter->valid = false;
1924 }
1925 
1926 /* scene objects iterator */
1927 
1933 
1934 static void scene_objects_iterator_begin(BLI_Iterator *iter, Scene *scene, GSet *visited_objects)
1935 {
1937 
1938  BLI_ITERATOR_INIT(iter);
1939  iter->data = data;
1940 
1941  /* Lookup list to make sure that each object is only processed once. */
1942  if (visited_objects != NULL) {
1943  data->visited = visited_objects;
1944  }
1945  else {
1946  data->visited = BLI_gset_ptr_new(__func__);
1947  }
1948 
1949  /* We wrap the scenecollection iterator here to go over the scene collections. */
1950  BKE_scene_collections_iterator_begin(&data->scene_collection_iter, scene);
1951 
1952  Collection *collection = data->scene_collection_iter.current;
1953  data->cob_next = collection->gobject.first;
1954 
1956 }
1957 
1959 {
1960  Scene *scene = data_in;
1961 
1963 }
1964 
1966 {
1967  if (!iter->valid) {
1968  return;
1969  }
1970 
1971  /* Unpack the data. */
1973  iter->data = data->iter_data;
1974 
1975  Object *ob = iter->current;
1976  if (ob && (ob->flag & data->flag) == 0) {
1977  iter->skip = true;
1978  }
1979 
1980  /* Pack the data. */
1981  data->iter_data = iter->data;
1982  iter->data = data;
1983 }
1984 
1986 {
1987  SceneObjectsIteratorExData *data = data_in;
1988 
1989  BKE_scene_objects_iterator_begin(iter, data->scene);
1990 
1991  /* Pack the data. */
1992  data->iter_data = iter->data;
1993  iter->data = data_in;
1994 
1996 }
1997 
1999 {
2000  /* Unpack the data. */
2002  iter->data = data->iter_data;
2003 
2005 
2006  /* Pack the data. */
2007  data->iter_data = iter->data;
2008  iter->data = data;
2009 
2011 }
2012 
2014 {
2015  /* Unpack the data. */
2017  iter->data = data->iter_data;
2018 
2020 
2021  /* Pack the data. */
2022  data->iter_data = iter->data;
2023  iter->data = data;
2024 }
2025 
2030 {
2031  for (; cob != NULL; cob = cob->next) {
2032  Object *ob = cob->ob;
2033  void **ob_key_p;
2034  if (!BLI_gset_ensure_p_ex(gs, ob, &ob_key_p)) {
2035  *ob_key_p = ob;
2036  return cob;
2037  }
2038  }
2039  return NULL;
2040 }
2041 
2043 {
2045  CollectionObject *cob = data->cob_next ? object_base_unique(data->visited, data->cob_next) :
2046  NULL;
2047 
2048  if (cob) {
2049  data->cob_next = cob->next;
2050  iter->current = cob->ob;
2051  }
2052  else {
2053  /* if this is the last object of this ListBase look at the next Collection */
2054  Collection *collection;
2055  BKE_scene_collections_iterator_next(&data->scene_collection_iter);
2056  do {
2057  collection = data->scene_collection_iter.current;
2058  /* get the first unique object of this collection */
2059  CollectionObject *new_cob = object_base_unique(data->visited, collection->gobject.first);
2060  if (new_cob) {
2061  data->cob_next = new_cob->next;
2062  iter->current = new_cob->ob;
2063  return;
2064  }
2065  BKE_scene_collections_iterator_next(&data->scene_collection_iter);
2066  } while (data->scene_collection_iter.valid);
2067 
2068  if (!data->scene_collection_iter.valid) {
2069  iter->valid = false;
2070  }
2071  }
2072 }
2073 
2075 {
2077  if (data) {
2078  BKE_scene_collections_iterator_end(&data->scene_collection_iter);
2079  if (data->visited != NULL) {
2080  BLI_gset_free(data->visited, NULL);
2081  }
2082  MEM_freeN(data);
2083  }
2084 }
2085 
2087 {
2088  BLI_Iterator iter;
2089  scene_objects_iterator_begin(&iter, scene, objects_gset);
2090  while (iter.valid) {
2092  }
2093 
2094  /* `return_gset` is either given `objects_gset` (if non-NULL), or the GSet allocated by the
2095  * iterator. Either way, we want to get it back, and prevent `BKE_scene_objects_iterator_end`
2096  * from freeing it. */
2097  GSet *return_gset = ((SceneObjectsIteratorData *)iter.data)->visited;
2098  ((SceneObjectsIteratorData *)iter.data)->visited = NULL;
2100 
2101  return return_gset;
2102 }
2103 
#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)
#define BKE_SCENE_COLLECTION_NAME
void BKE_previewimg_free(struct PreviewImage **prv)
Definition: icons.cc:283
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:615
void BKE_previewimg_blend_write(struct BlendWriter *writer, const struct PreviewImage *prv)
@ IDTYPE_FLAGS_APPEND_IS_REUSABLE
Definition: BKE_idtype.h:39
@ IDTYPE_FLAGS_NO_ANIMDATA
Definition: BKE_idtype.h:41
bool BKE_layer_collection_objects_select(struct ViewLayer *view_layer, struct LayerCollection *lc, bool deselect)
Definition: layer.c:1409
void BKE_main_collection_sync(const struct Main *bmain)
bool BKE_view_layer_has_collection(const struct ViewLayer *view_layer, const struct Collection *collection)
void BKE_view_layer_blend_read_data(struct BlendDataReader *reader, struct ViewLayer *view_layer)
Definition: layer.c:2315
struct Base * BKE_view_layer_base_find(struct ViewLayer *view_layer, struct Object *ob)
Definition: layer.c:379
struct LayerCollection * BKE_layer_collection_first_from_scene_collection(const 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:2357
@ LIB_ID_COPY_NO_PREVIEW
Definition: BKE_lib_id.h:150
@ LIB_ID_CREATE_NO_USER_REFCOUNT
Definition: BKE_lib_id.h:126
@ LIB_ID_CREATE_NO_MAIN
Definition: BKE_lib_id.h:122
@ LIB_ID_COPY_DEFAULT
Definition: BKE_lib_id.h:181
void BKE_main_id_newptr_and_tag_clear(struct Main *bmain)
Definition: lib_id.c:1465
void BKE_libblock_free_data(struct ID *id, bool do_id_user) ATTR_NONNULL()
Definition: lib_id_delete.c:44
void id_us_min(struct ID *id)
Definition: lib_id.c:313
void * BKE_libblock_alloc(struct Main *bmain, short type, const char *name, int flag) ATTR_WARN_UNUSED_RESULT
Definition: lib_id.c:1050
void id_us_plus(struct ID *id)
Definition: lib_id.c:305
void BKE_id_blend_write(struct BlendWriter *writer, struct ID *id)
Definition: lib_id.c:2008
void * BKE_id_new(struct Main *bmain, short type, const char *name)
Definition: lib_id.c:1159
struct ID * BKE_id_copy_for_duplicate(struct Main *bmain, struct ID *id, uint duplicate_flags, int copy_flags)
void BKE_id_delete(struct Main *bmain, void *idv) ATTR_NONNULL()
void BKE_main_id_tag_listbase(struct ListBase *lb, int tag, bool value)
Definition: lib_id.c:904
eLibIDDuplicateFlags
Definition: BKE_lib_id.h:219
@ LIB_ID_DUPLICATE_IS_ROOT_ID
Definition: BKE_lib_id.h:228
@ LIB_ID_DUPLICATE_IS_SUBPROCESS
Definition: BKE_lib_id.h:225
void BKE_id_free_us(struct Main *bmain, void *idv) ATTR_NONNULL()
#define BKE_LIB_FOREACHID_PROCESS_IDSUPER(_data, _id_super, _cb_flag)
@ IDWALK_CB_LOOPBACK
Definition: BKE_lib_query.h:54
@ IDWALK_CB_NEVER_SELF
Definition: BKE_lib_query.h:35
@ IDWALK_CB_USER
Definition: BKE_lib_query.h:73
@ IDWALK_CB_EMBEDDED
Definition: BKE_lib_query.h:48
@ IDWALK_CB_NOP
Definition: BKE_lib_query.h:33
void BKE_libblock_relink_to_newid(struct Main *bmain, struct ID *id, int remap_flag) ATTR_NONNULL()
Definition: lib_remap.c:894
#define FOREACH_MAIN_ID_END
Definition: BKE_main.h:367
#define FOREACH_MAIN_ID_BEGIN(_bmain, _id)
Definition: BKE_main.h:361
General operations, lookup, etc. for blender objects.
struct Object * BKE_object_duplicate(struct Main *bmain, struct Object *ob, uint dupflag, uint duplicate_options)
Definition: object.cc:2661
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:2371
void BKE_scene_remove_rigidbody_object(struct Main *bmain, struct Scene *scene, struct Object *ob, bool free_us)
Definition: scene.cc:2364
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define BLI_assert_msg(a, msg)
Definition: BLI_assert.h:53
struct GSet GSet
Definition: BLI_ghash.h:340
bool BLI_gset_ensure_p_ex(GSet *gs, const void *key, void ***r_key)
Definition: BLI_ghash.c:974
GSet * BLI_gset_ptr_new(const char *info)
void BLI_gset_free(GSet *gs, GSetKeyFreeFP keyfreefp)
Definition: BLI_ghash.c:1037
#define BLI_ITERATOR_INIT(iter)
Definition: BLI_iterator.h:23
BLI_INLINE bool BLI_listbase_is_empty(const struct ListBase *lb)
Definition: BLI_listbase.h:269
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:336
void BLI_freelinkN(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:239
#define LISTBASE_FOREACH_MUTABLE(type, var, list)
Definition: BLI_listbase.h:354
BLI_INLINE void BLI_listbase_clear(struct ListBase *lb)
Definition: BLI_listbase.h:273
void BLI_insertlinkafter(struct ListBase *listbase, void *vprevlink, void *vnewlink) ATTR_NONNULL(1)
Definition: listbase.c:301
void void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1)
Definition: listbase.c:466
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:80
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:100
void BLI_insertlinkbefore(struct ListBase *listbase, void *vnextlink, void *vnewlink) ATTR_NONNULL(1)
Definition: listbase.c:340
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, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
MINLINE int integer_digits_i(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:483
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL() ATTR_MALLOC
Definition: string.c:42
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL()
Definition: string.c:64
#define BLI_MUTEX_INITIALIZER
Definition: BLI_threads.h:83
void BLI_mutex_lock(ThreadMutex *mutex)
Definition: threads.cc:373
void BLI_mutex_unlock(ThreadMutex *mutex)
Definition: threads.cc:378
pthread_mutex_t ThreadMutex
Definition: BLI_threads.h:82
#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:5172
#define BLO_read_id_address(reader, lib, id_ptr_p)
#define BLO_expand(expander, id)
#define IFACE_(msgid)
#define BLT_I18NCONTEXT_ID_COLLECTION
#define DATA_(msgid)
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:834
@ ID_RECALC_GEOMETRY
Definition: DNA_ID.h:791
#define ID_IS_OVERRIDABLE_LIBRARY(_id)
Definition: DNA_ID.h:574
@ INDEX_ID_GR
Definition: DNA_ID.h:1040
#define ID_IS_LINKED(_id)
Definition: DNA_ID.h:566
#define FILTER_ID_GR
Definition: DNA_ID.h:905
@ LIB_TAG_NEW
Definition: DNA_ID.h:704
@ LIB_TAG_COPIED_ON_WRITE
Definition: DNA_ID.h:720
@ LIB_TAG_DOIT
Definition: DNA_ID.h:707
@ LIB_TAG_NO_MAIN
Definition: DNA_ID.h:744
@ LIB_EMBEDDED_DATA
Definition: DNA_ID.h:635
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition: DNA_ID.h:588
@ ID_SCE
Definition: DNA_ID_enums.h:45
@ ID_GR
Definition: DNA_ID_enums.h:65
Object groups, one object can be in many groups at once.
@ COLLECTION_TAG_RELATION_REBUILD
struct Collection Collection
@ COLLECTION_HIDE_RENDER
@ COLLECTION_HAS_OBJECT_CACHE_INSTANCED
@ COLLECTION_HIDE_SELECT
@ COLLECTION_IS_MASTER
@ COLLECTION_HAS_OBJECT_CACHE
@ COLLECTION_HIDE_VIEWPORT
@ COLLECTION_COLOR_NONE
#define DNA_struct_default_get(struct_name)
Definition: DNA_defaults.h:29
#define MAX_NAME
Definition: DNA_defs.h:48
@ 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:751
static bool collection_cycle_fix_recursive(Main *bmain, Collection *parent_collection, Collection *collection)
Definition: collection.c:1479
bool BKE_collection_has_object_recursive_instanced(Collection *collection, Object *ob)
Definition: collection.c:931
void BKE_scene_objects_iterator_begin_ex(BLI_Iterator *iter, void *data_in)
Definition: collection.c:1985
static void scene_collections_count(Collection *UNUSED(collection), void *data)
Definition: collection.c:1848
static CollectionParent * collection_find_parent(Collection *child, Collection *collection)
Definition: collection.c:1529
bool BKE_collection_has_object_recursive(Collection *collection, Object *ob)
Definition: collection.c:921
Collection * BKE_collection_from_index(Scene *scene, const int index)
Definition: collection.c:1716
void BKE_collection_add_from_collection(Main *bmain, Scene *scene, Collection *collection_src, Collection *collection_dst)
Definition: collection.c:455
static void collection_object_remove_duplicates(Collection *collection)
Definition: collection.c:1266
static bool collection_child_remove(Collection *parent, Collection *collection)
Definition: collection.c:1567
const char * BKE_collection_ui_name_get(struct Collection *collection)
Definition: collection.c:736
void BKE_collection_blend_write_nolib(BlendWriter *writer, Collection *collection)
Definition: collection.c:190
bool BKE_collection_child_remove(Main *bmain, Collection *parent, Collection *child)
Definition: collection.c:1600
static bool collection_child_add(Collection *parent, Collection *collection, const int flag, const bool add_us)
Definition: collection.c:1534
Collection * BKE_collection_master_add()
Definition: collection.c:852
bool BKE_collection_viewlayer_object_add(Main *bmain, const ViewLayer *view_layer, Collection *collection, Object *ob)
Definition: collection.c:1130
void BKE_scene_objects_iterator_next_ex(struct BLI_Iterator *iter)
Definition: collection.c:1998
static CollectionObject * object_base_unique(GSet *gs, CollectionObject *cob)
Definition: collection.c:2029
static bool collection_object_remove(Main *bmain, Collection *collection, Object *ob, const bool free_us)
Definition: collection.c:1074
static bool collection_instance_find_recursive(Collection *collection, Collection *instance_collection)
Definition: collection.c:1414
Collection * BKE_collection_parent_editable_find_recursive(const ViewLayer *view_layer, Collection *collection)
Definition: collection.c:1005
bool BKE_collection_has_object(Collection *collection, const Object *ob)
Definition: collection.c:912
ListBase BKE_collection_object_cache_instanced_get(Collection *collection)
Definition: collection.c:803
static void collection_object_cache_free(Collection *collection)
Definition: collection.c:819
static void collection_blend_read_lib(BlendLibReader *reader, ID *id)
Definition: collection.c:317
Collection * BKE_collection_duplicate(Main *bmain, Collection *parent, Collection *collection, eDupli_ID_Flags duplicate_flags, eLibIDDuplicateFlags duplicate_options)
Definition: collection.c:655
static bool collection_object_add(Main *bmain, Collection *collection, Object *ob, int flag, const bool add_us)
Definition: collection.c:1038
void BKE_collections_object_remove_duplicates(struct Main *bmain)
Definition: collection.c:1289
static void collection_blend_read_expand(BlendExpander *expander, ID *id)
Definition: collection.c:354
GSet * BKE_scene_objects_as_gset(Scene *scene, GSet *objects_gset)
Definition: collection.c:2086
bool BKE_collection_child_add_no_sync(Collection *parent, Collection *child)
Definition: collection.c:1595
Collection * BKE_collection_object_find(Main *bmain, Scene *scene, Collection *collection, Object *ob)
Definition: collection.c:950
void BKE_scene_objects_iterator_end(BLI_Iterator *iter)
Definition: collection.c:2074
bool BKE_collection_is_empty(const Collection *collection)
Definition: collection.c:974
static void collection_parents_rebuild_recursive(Collection *collection)
Definition: collection.c:1639
bool BKE_collection_move(Main *bmain, Collection *to_parent, Collection *from_parent, Collection *relative, bool relative_after, Collection *collection)
Definition: collection.c:1777
bool BKE_collection_is_in_scene(Collection *collection)
Definition: collection.c:1385
static void scene_collection_callback(Collection *collection, BKE_scene_collections_Cb callback, void *data)
Definition: collection.c:1837
bool BKE_collection_child_add(Main *bmain, Collection *parent, Collection *child)
Definition: collection.c:1585
void BKE_collection_object_move(Main *bmain, Scene *scene, Collection *collection_dst, Collection *collection_src, Object *ob)
Definition: collection.c:1361
void BKE_collection_add_from_object(Main *bmain, Scene *scene, const Object *ob_src, Collection *collection_dst)
Definition: collection.c:432
void BKE_collection_blend_read_expand(BlendExpander *expander, Collection *collection)
Definition: collection.c:337
static bool collection_object_cyclic_check_internal(Object *object, Collection *collection)
Definition: collection.c:869
static Collection * collection_next_find(Main *bmain, Scene *scene, Collection *collection)
Definition: collection.c:941
void BKE_scene_objects_iterator_end_ex(struct BLI_Iterator *iter)
Definition: collection.c:2013
bool BKE_scene_collections_object_remove(Main *bmain, Scene *scene, Object *ob, const bool free_us)
Definition: collection.c:1224
bool BKE_collection_objects_select(ViewLayer *view_layer, Collection *collection, bool deselect)
Definition: collection.c:1759
void BKE_collections_child_remove_nulls(Main *bmain, Collection *parent_collection, Collection *child_collection)
Definition: collection.c:1322
static void collection_copy_data(Main *bmain, ID *id_dst, const ID *id_src, const int flag)
Definition: collection.c:95
static Collection * collection_add(Main *bmain, Collection *collection_parent, const char *name_custom)
Definition: collection.c:397
void BKE_collection_object_cache_free(Collection *collection)
Definition: collection.c:832
bool BKE_collection_object_remove(Main *bmain, Collection *collection, Object *ob, const bool free_us)
Definition: collection.c:1170
static ID * collection_owner_get(Main *bmain, ID *id, ID *owner_id_hint)
Definition: collection.c:165
static void lib_link_collection_data(BlendLibReader *reader, Library *lib, Collection *collection)
Definition: collection.c:271
void BKE_scene_collections_iterator_begin(BLI_Iterator *iter, void *data_in)
Definition: collection.c:1884
void BKE_collection_new_name_get(Collection *collection_parent, char *rname)
Definition: collection.c:713
void BKE_collection_free_data(Collection *collection)
Definition: collection.c:489
Base * BKE_collection_or_layer_objects(const ViewLayer *view_layer, Collection *collection)
Definition: collection.c:837
Collection * BKE_collection_add(Main *bmain, Collection *collection_parent, const char *name_custom)
Definition: collection.c:425
static bool collection_find_child_recursive(const Collection *parent, const Collection *collection)
Definition: collection.c:1509
static CollectionChild * collection_find_child(Collection *parent, Collection *collection)
Definition: collection.c:1504
bool BKE_collection_cycles_fix(Main *bmain, Collection *collection)
Definition: collection.c:1498
struct SceneObjectsIteratorData SceneObjectsIteratorData
bool BKE_collection_object_add_notest(Main *bmain, Collection *collection, Object *ob)
Definition: collection.c:1099
static void collection_free_data(ID *id)
Definition: collection.c:128
static bool collection_instance_fix_recursive(Collection *parent_collection, Collection *collection)
Definition: collection.c:1456
static void collection_object_remove_nulls(Collection *collection)
Definition: collection.c:1234
void BKE_collection_object_add_from(Main *bmain, Scene *scene, Object *ob_src, Object *ob_dst)
Definition: collection.c:1148
static void collection_blend_read_data(BlendDataReader *reader, ID *id)
Definition: collection.c:265
static void scene_collections_array(Scene *scene, Collection ***r_collections_array, int *r_collections_array_len)
Definition: collection.c:1861
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:554
void BKE_collection_blend_read_lib(BlendLibReader *reader, Collection *collection)
Definition: collection.c:302
bool BKE_collection_delete(Main *bmain, Collection *collection, bool hierarchy)
Definition: collection.c:495
void BKE_collections_object_remove_nulls(Main *bmain)
Definition: collection.c:1250
bool BKE_collection_cycle_find(Collection *new_ancestor, Collection *collection)
Definition: collection.c:1435
static void scene_objects_iterator_begin(BLI_Iterator *iter, Scene *scene, GSet *visited_objects)
Definition: collection.c:1934
static void collection_null_children_remove(Collection *collection)
Definition: collection.c:1304
static void collection_blend_write(BlendWriter *writer, ID *id, const void *id_address)
Definition: collection.c:206
static Collection * collection_from_index_recursive(Collection *collection, const int index, int *index_current)
Definition: collection.c:1697
bool BKE_collection_object_cyclic_check(Main *bmain, Object *object, Collection *collection)
Definition: collection.c:898
void BKE_collection_blend_read_data(BlendDataReader *reader, Collection *collection)
Definition: collection.c:236
static void collection_missing_parents_remove(Collection *collection)
Definition: collection.c:1313
void BKE_collections_after_lib_link(Main *bmain)
Definition: collection.c:1400
bool BKE_collection_has_collection(const Collection *parent, const Collection *collection)
Definition: collection.c:1524
static void scene_collections_build_array(Collection *collection, void *data)
Definition: collection.c:1854
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:1196
static void collection_init_data(ID *id)
Definition: collection.c:77
ListBase BKE_collection_object_cache_get(Collection *collection)
Definition: collection.c:787
void BKE_scene_collections_iterator_end(struct BLI_Iterator *iter)
Definition: collection.c:1913
void BKE_scene_objects_iterator_begin(BLI_Iterator *iter, void *data_in)
Definition: collection.c:1958
void BKE_scene_collections_iterator_next(struct BLI_Iterator *iter)
Definition: collection.c:1901
void BKE_collection_parent_relations_rebuild(Collection *collection)
Definition: collection.c:1610
static void collection_tag_update_parent_recursive(Main *bmain, Collection *collection, const int flag)
Definition: collection.c:986
static void scene_objects_iterator_skip_invalid_flag(BLI_Iterator *iter)
Definition: collection.c:1965
void BKE_scene_objects_iterator_next(BLI_Iterator *iter)
Definition: collection.c:2042
static void collection_foreach_id(ID *id, LibraryForeachIDData *data)
Definition: collection.c:142
void BKE_main_collections_parent_relations_rebuild(Main *bmain)
Definition: collection.c:1658
IDTypeInfo IDType_ID_GR
Definition: collection.c:360
static bool collection_objects_select(ViewLayer *view_layer, Collection *collection, bool deselect)
Definition: collection.c:1723
bool BKE_collection_object_add(Main *bmain, Collection *collection, Object *ob)
Definition: collection.c:1125
Scene scene
DEGForeachIDComponentCallback callback
DRWShaderLibrary * lib
#define GS(x)
Definition: iris.c:225
void *(* MEM_malloc_arrayN)(size_t len, size_t size, const char *str)
Definition: mallocn.c:34
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
void * current
Definition: BLI_iterator.h:14
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:114
Definition: DNA_ID.h:368
int tag
Definition: DNA_ID.h:387
struct Library * lib
Definition: DNA_ID.h:372
struct ID * newid
Definition: DNA_ID.h:370
short flag
Definition: DNA_ID.h:383
void * next
Definition: DNA_ID.h:369
char name[66]
Definition: DNA_ID.h:378
void * first
Definition: DNA_listBase.h:31
Definition: BKE_main.h:121
ListBase scenes
Definition: BKE_main.h:168
ListBase collections
Definition: BKE_main.h:189
ListBase objects
Definition: BKE_main.h:170
struct Collection * instance_collection
ListBase scene_collections
BLI_Iterator scene_collection_iter
Definition: collection.c:1931
CollectionObject * cob_next
Definition: collection.c:1930
struct Collection * master_collection