Blender V5.3
collection.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9/* Allow using deprecated functionality for .blend file I/O. */
10#define DNA_DEPRECATED_ALLOW
11
12#include "CLG_log.h"
13
14#include <fmt/format.h>
15
16#include <cstring>
17#include <optional>
18
19#include "BLI_iterator.hh"
20#include "BLI_listbase.hh"
21#include "BLI_math_base_c.hh"
22#include "BLI_mutex.hh"
23#include "BLI_string.hh"
24#include "BLI_string_utf8.hh"
25#include "BLI_string_utils.hh"
26
27#include "BLT_translation.hh"
28
29#include "BKE_anim_data.hh"
30#include "BKE_collection.hh"
31#include "BKE_file_handler.hh"
32#include "BKE_idprop.hh"
33#include "BKE_idtype.hh"
34#include "BKE_layer.hh"
35#include "BKE_lib_id.hh"
36#include "BKE_lib_query.hh"
37#include "BKE_lib_remap.hh"
38#include "BKE_library.hh"
39#include "BKE_main.hh"
40#include "BKE_object.hh"
41#include "BKE_preview_image.hh"
42#include "BKE_rigidbody.h"
43#include "BKE_scene.hh"
44
45#include "DNA_ID.h"
47#include "DNA_layer_types.h"
48#include "DNA_listBase.h"
49#include "DNA_object_types.h"
50#include "DNA_rigidbody_types.h"
51#include "DNA_scene_types.h"
52
53#include "DEG_depsgraph.hh"
55
56#include "MEM_guardedalloc.h"
57
58#include "BLO_read_write.hh"
59
60namespace blender {
61
62static CLG_LogRef LOG = {"object.collection"};
63
67// #define USE_DEBUG_EXTRA_GOBJECT_ASSERT
68
69/* -------------------------------------------------------------------- */
72
74static bool collection_child_add(Main *bmain,
75 Collection *parent,
76 Collection *collection,
77 const CollectionLightLinking *light_linking,
78 const int id_create_flag,
79 const bool add_us);
81static bool collection_child_remove(Main *bmain,
82 Collection *parent,
83 Collection *collection,
84 const int id_create_flag);
86static bool collection_object_add(Main *bmain,
87 Collection *collection,
88 Object *ob,
89 CollectionLightLinking *light_linking,
90 const int id_create_flag,
91 const bool add_us);
92
94static void collection_object_remove_no_gobject_hash(Main *bmain,
95 Collection *collection,
96 CollectionObject *cob,
97 const int id_create_flag,
98 const bool free_us);
100static bool collection_object_remove(
101 Main *bmain, Collection *collection, Object *ob, const int id_create_flag, const bool free_us);
102
103static CollectionParent *collection_find_parent(Collection *child, Collection *collection);
104
105static bool collection_find_child_recursive(const Collection *parent,
106 const Collection *collection);
107
109static void collection_object_cache_free(const Main *bmain,
110 Collection *collection,
111 const int id_create_flag,
112 const uint id_recalc_flag);
113
114static void collection_gobject_hash_ensure(Collection *collection);
115static void collection_gobject_hash_update_object(Collection *collection,
116 Object *ob_old,
117 CollectionObject *cob);
118static void collection_importer_copy(Collection *collection, const CollectionImport *data);
119static void collection_exporter_copy(Collection *collection, CollectionExport *data);
120
122
123/* -------------------------------------------------------------------- */
126
127static void collection_init_data(ID *id)
128{
129 Collection *collection = id_cast<Collection *>(id);
130 INIT_DEFAULT_STRUCT_AFTER(collection, id);
131 collection->runtime = MEM_new<bke::CollectionRuntime>(__func__);
132}
133
144static void collection_copy_data(Main *bmain,
145 std::optional<Library *> /*owner_library*/,
146 ID *id_dst,
147 const ID *id_src,
148 const int flag)
149{
150 Collection *collection_dst = id_cast<Collection *>(id_dst);
151 const Collection *collection_src = id_cast<const Collection *>(id_src);
152
153 collection_dst->runtime = MEM_new<bke::CollectionRuntime>(__func__);
154
155 BLI_assert(((collection_src->flag & COLLECTION_IS_MASTER) != 0) ==
156 ((collection_src->id.flag & ID_FLAG_EMBEDDED_DATA) != 0));
157
158 if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0) {
159 BKE_previewimg_id_copy(&collection_dst->id, &collection_src->id);
160 }
161 else {
162 collection_dst->preview = nullptr;
163 }
164
166
167 collection_dst->gobject.clear_no_delete();
168 collection_dst->children.clear_no_delete();
169 collection_dst->exporters.clear_no_delete();
170 collection_dst->importer = nullptr;
171
172 for (CollectionChild &child : collection_src->children) {
174 bmain, collection_dst, child.collection, &child.light_linking, flag, false);
175 }
176 for (CollectionObject &cob : collection_src->gobject) {
177 collection_object_add(bmain, collection_dst, cob.ob, &cob.light_linking, flag, false);
178 }
179 for (CollectionExport &data : collection_src->exporters) {
180 collection_exporter_copy(collection_dst, &data);
181 }
182 if (collection_src->importer) {
183 collection_importer_copy(collection_dst, collection_src->importer);
184 }
185}
186
187static void collection_free_data(ID *id)
188{
189 Collection *collection = id_cast<Collection *>(id);
190
191 /* No animation-data here. */
192 BKE_previewimg_id_free(&collection->id);
193
194 collection->gobject.free_no_destruct();
195 if (collection->runtime->gobject_hash) {
196 MEM_delete(collection->runtime->gobject_hash);
197 collection->runtime->gobject_hash = nullptr;
198 }
199
200 collection->children.free_no_destruct();
201 collection->runtime->parents.free_no_destruct();
202
203 for (CollectionExport &data : collection->exporters) {
205 }
206 collection->exporters.free_no_destruct();
207
208 if (collection->importer) {
210 MEM_delete(collection->importer);
211 collection->importer = nullptr;
212 }
213
214 /* No need for depsgraph tagging here, since the data is being deleted. */
216
217 MEM_delete(collection->runtime);
218}
219
221{
222 Collection *collection = id_cast<Collection *>(id);
223 const int data_flags = BKE_lib_query_foreachid_process_flags_get(data);
224
226 data,
227 collection->owner_id,
229
230 for (CollectionObject &cob : collection->gobject) {
231 Object *cob_ob_old = cob.ob;
232
235
236 if (collection->runtime->gobject_hash) {
237 /* If the remapping does not create inconsistent data (nullptr object pointer or duplicate
238 * CollectionObjects), keeping the ghash consistent is also possible. Otherwise, this call
239 * will take care of tagging the collection objects list as dirty. */
240 collection_gobject_hash_update_object(collection, cob_ob_old, &cob);
241 }
242 else if (cob_ob_old != cob.ob || cob.ob == nullptr) {
243 /* If there is no reference GHash, duplicates cannot be reliably detected, so assume that any
244 * nullptr pointer or changed pointer may create an invalid collection object list. */
246 }
247 }
248 for (CollectionChild &child : collection->children) {
250 child.collection,
253 }
254 for (CollectionParent &parent : collection->runtime->parents) {
255 /* XXX This is very weak. The whole idea of keeping pointers to private IDs is very bad
256 * anyway... */
257 const LibraryForeachIDCallbackFlag cb_flag =
258 ((parent.collection != nullptr && (data_flags & IDWALK_NO_ORIG_POINTERS_ACCESS) == 0 &&
259 (parent.collection->id.flag & ID_FLAG_EMBEDDED_DATA) != 0) ?
264 }
265
266 /* Clear the archive library pointer when the library is deleted. */
267 if (collection->importer && collection->importer->runtime) {
269 collection->importer->runtime->archive_library,
272 }
273}
274
275static ID **collection_owner_pointer_get(ID *id, const bool debug_relationship_assert)
276{
277 if ((id->flag & ID_FLAG_EMBEDDED_DATA) == 0) {
278 return nullptr;
279 }
280
281 Collection *master_collection = id_cast<Collection *>(id);
282 BLI_assert((master_collection->flag & COLLECTION_IS_MASTER) != 0);
283 if (debug_relationship_assert) {
284 BLI_assert(master_collection->owner_id != nullptr);
285 BLI_assert(GS(master_collection->owner_id->name) == ID_SCE);
286 BLI_assert(((Scene *)master_collection->owner_id)->master_collection == master_collection);
287 }
288
289 return &master_collection->owner_id;
290}
291
293{
294 collection->runtime = nullptr;
295 /* Clean up, important in undo case to reduce false detection of changed data-blocks. */
296 collection->flag &= ~COLLECTION_FLAG_ALL_RUNTIME;
297}
298
300{
301 BKE_id_blend_write(writer, &collection->id);
302
303 /* Shared function for collection data-blocks and scene master collection. */
304 BKE_previewimg_blend_write(writer, collection->preview);
305
306 for (CollectionObject &cob : collection->gobject) {
307 writer->write_struct(&cob);
308 }
309
310 for (CollectionChild &child : collection->children) {
311 writer->write_struct(&child);
312 }
313
314 writer->write_struct(collection->importer,
315 [](BlendStructWriter<CollectionImport> &struct_writer) {
316 struct_writer.shallow_data.runtime = nullptr;
317 });
318 if (collection->importer && collection->importer->import_properties) {
319 IDP_BlendWrite(writer, collection->importer->import_properties);
320 }
321
322 for (CollectionExport &data : collection->exporters) {
323 writer->write_struct(&data);
324 if (data.export_properties) {
325 IDP_BlendWrite(writer, data.export_properties);
326 }
327 writer->write_struct_list(&data.layout_panel_states);
328 for (const LayoutPanelState &state : data.layout_panel_states) {
329 writer->write_string(state.idname);
330 }
331 }
332}
333
334static void collection_blend_write(BlendWriter *writer, ID *id, const void *id_address)
335{
336 Collection *collection = id_cast<Collection *>(id);
337
339
340 /* write LibData */
341 writer->write_id_struct(id_address, collection);
342
343 BKE_collection_blend_write_nolib(writer, collection);
344}
345
346void BKE_collection_blend_read_data(BlendDataReader *reader, Collection *collection, ID *owner_id)
347{
348 /* Special case for this pointer, do not rely on regular `lib_link` process here. Avoids needs
349 * for do_versioning, and ensures coherence of data in any case.
350 *
351 * NOTE: Old versions are very often 'broken' here, just fix it silently in these cases.
352 */
353 if (BLO_read_fileversion_get(reader) > 300) {
354 BLI_assert((collection->id.flag & ID_FLAG_EMBEDDED_DATA) != 0 || owner_id == nullptr);
355 }
356 BLI_assert(owner_id == nullptr || owner_id->lib == collection->id.lib);
357 if (owner_id != nullptr && (collection->id.flag & ID_FLAG_EMBEDDED_DATA) == 0) {
358 /* This is unfortunate, but currently a lot of existing files (including startup ones) have
359 * missing `ID_FLAG_EMBEDDED_DATA` flag.
360 *
361 * NOTE: Using do_version is not a solution here, since this code will be called before any
362 * do_version takes place. Keeping it here also ensures future (or unknown existing) similar
363 * bugs won't go easily unnoticed. */
364 if (BLO_read_fileversion_get(reader) > 300) {
365 CLOG_WARN(&LOG,
366 "Fixing root node tree '%s' owned by '%s' missing EMBEDDED tag, please consider "
367 "re-saving your (startup) file",
368 collection->id.name,
369 owner_id->name);
370 }
371 collection->id.flag |= ID_FLAG_EMBEDDED_DATA;
372 }
373
374 collection->runtime = MEM_new<bke::CollectionRuntime>(__func__);
375 collection->flag &= ~COLLECTION_FLAG_ALL_RUNTIME;
376
377 collection->owner_id = owner_id;
378
379 BLO_read_struct_list(reader, CollectionObject, &collection->gobject);
380 BLO_read_struct_list(reader, CollectionChild, &collection->children);
381
382 BLO_read_struct(reader, CollectionImport, &collection->importer);
383 if (collection->importer) {
384 collection->importer->runtime = MEM_new<bke::CollectionImportRuntime>(
385 "CollectionImportRuntime");
386
388 IDP_BlendDataRead(reader, &collection->importer->import_properties);
389 }
390
391 BLO_read_struct_list(reader, CollectionExport, &collection->exporters);
392 for (CollectionExport &data : collection->exporters) {
393 BLO_read_struct(reader, IDProperty, &data.export_properties);
394 IDP_BlendDataRead(reader, &data.export_properties);
395 BLO_read_struct_list(reader, LayoutPanelState, &data.layout_panel_states);
396 for (LayoutPanelState &state : data.layout_panel_states) {
397 BLO_read_string(reader, &state.idname);
398 }
399 }
400
401 BLO_read_struct(reader, PreviewImage, &collection->preview);
402 BKE_previewimg_blend_read(reader, collection->preview);
403}
404
406{
407 Collection *collection = id_cast<Collection *>(id);
408 BKE_collection_blend_read_data(reader, collection, nullptr);
409}
410
412{
413 Collection *collection = reinterpret_cast<Collection *>(id);
414
415 /* Sanity check over Collection/Object data. */
416 BLI_assert(collection->runtime->gobject_hash == nullptr);
417 for (CollectionObject &cob : collection->gobject.items_mutable()) {
418 if (cob.ob == nullptr) {
419 BLI_freelinkN(&collection->gobject, &cob);
420 }
421 }
422
423 /* foreach_id code called by generic lib_link process has most likely set this flag, however it
424 * is not needed during readfile process since the runtime data is affects are not yet built, so
425 * just clear it here. */
426 BLI_assert(collection->runtime->gobject_hash == nullptr);
428
429 /* Restore the collection importer's archive library by using the first object's library. */
430 if (collection->importer) {
431 if (!collection->gobject.is_empty()) {
432 const CollectionObject *cob = collection->gobject.first_as<CollectionObject>();
433 Library *lib = cob->ob->id.lib;
434
436 collection->importer->runtime->archive_library = lib;
437 }
438 }
439}
440
442 .id_code = Collection::id_type,
443 .id_filter = FILTER_ID_GR,
444 .dependencies_id_types = FILTER_ID_OB | FILTER_ID_GR | FILTER_ID_LI,
445 .main_listbase_index = INDEX_ID_GR,
446 .struct_size = sizeof(Collection),
447 .name = "Collection",
448 .name_plural = N_("collections"),
449 .translation_context = BLT_I18NCONTEXT_ID_COLLECTION,
451 .asset_type_info = nullptr,
452
453 .init_data = collection_init_data,
454 .copy_data = collection_copy_data,
455 .free_data = collection_free_data,
456 .make_local = nullptr,
457 .foreach_id = collection_foreach_id,
458 .foreach_cache = nullptr,
459 .foreach_path = nullptr,
460 .foreach_working_space_color = nullptr,
461 .foreach_asset_weak_reference = nullptr,
462 .owner_pointer_get = collection_owner_pointer_get,
463
464 .blend_write = collection_blend_write,
465 .blend_read_data = collection_blend_read_data,
466 .blend_read_after_liblink = collection_blend_read_after_liblink,
467
468 .blend_read_undo_preserve = nullptr,
469
470 .lib_override_apply_post = nullptr,
471};
472
474
475/* -------------------------------------------------------------------- */
478
479/* Add new collection, without view layer syncing. */
481 Collection *collection_parent,
482 const char *name_custom)
483{
484 /* Determine new collection name. */
485 char name[MAX_ID_NAME - 2];
486
487 if (name_custom) {
488 STRNCPY_UTF8(name, name_custom);
489 }
490 else {
491 BKE_collection_new_name_get(collection_parent, name);
492 }
493
494 /* Create new collection. */
495 Collection *collection = BKE_id_new<Collection>(bmain, name);
496
497 /* We increase collection user count when linking to Collections. */
498 id_us_min(&collection->id);
499
500 /* Optionally add to parent collection. */
501 if (collection_parent) {
502 collection_child_add(bmain, collection_parent, collection, nullptr, 0, true);
503 collection->color_tag = collection_parent->color_tag;
504 }
505
506 return collection;
507}
508
509Collection *BKE_collection_add(Main *bmain, Collection *collection_parent, const char *name_custom)
510{
511 Collection *collection = collection_add(bmain, collection_parent, name_custom);
513 return collection;
514}
515
517 Scene *scene,
518 const Object *ob_src,
519 Collection *collection_dst)
520{
521 bool is_instantiated = false;
522
523 FOREACH_SCENE_COLLECTION_BEGIN (scene, collection) {
524 if (!ID_IS_LINKED(collection) && !ID_IS_OVERRIDABLE_LIBRARY(collection) &&
525 BKE_collection_has_object(collection, ob_src))
526 {
527 collection_child_add(bmain, collection, collection_dst, nullptr, 0, true);
528 is_instantiated = true;
529 }
530 }
532
533 if (!is_instantiated) {
534 collection_child_add(bmain, scene->master_collection, collection_dst, nullptr, 0, true);
535 }
536
538}
539
541 Scene *scene,
542 Collection *collection_src,
543 Collection *collection_dst)
544{
545 bool is_instantiated = false;
546
547 FOREACH_SCENE_COLLECTION_BEGIN (scene, collection) {
548 if (BKE_collection_is_content_editable(collection) &&
549 BKE_collection_child_find(collection, collection_src))
550 {
551 collection_child_add(bmain, collection, collection_dst, nullptr, 0, true);
552 is_instantiated = true;
553 }
554 else if (!is_instantiated && BKE_collection_child_find(collection, collection_dst)) {
555 /* If given collection_dst is already instantiated in scene, even if its 'model'
556 * collection_src one is not, do not add it to master scene collection. */
557 is_instantiated = true;
558 }
559 }
561
562 if (!is_instantiated) {
563 collection_child_add(bmain, scene->master_collection, collection_dst, nullptr, 0, true);
564 }
565
567}
568
570
571/* -------------------------------------------------------------------- */
574
576{
577 BKE_libblock_free_data(&collection->id, false);
578 collection_free_data(&collection->id);
579}
580
581void BKE_collection_exporter_name_set(const ListBaseT<CollectionExport> *exporters,
583 const char *newname)
584{
585 /* Only use the new name if it's not empty. */
586 if (newname && newname[0] != '\0') {
587 ListBaseT<CollectionExport> list = exporters ? *exporters : BLI_listbase_from_link(data);
588
589 STRNCPY(data->name, newname);
591 &list, data, newname, '.', offsetof(CollectionExport, name), sizeof(data->name));
592 }
593}
594
596{
597 if (data->import_properties) {
598 IDP_FreeProperty(data->import_properties);
599 }
600
601 MEM_delete(data->runtime);
602}
603
605{
606 if (data->export_properties) {
607 IDP_FreeProperty(data->export_properties);
608 }
609 for (const LayoutPanelState &state : data->layout_panel_states.items_mutable()) {
610 MEM_delete(state.idname);
611 MEM_delete(&state);
612 }
613}
614
615bool BKE_collection_delete(Main *bmain, Collection *collection, bool hierarchy)
616{
617 /* Master collection is not real datablock, can't be removed. */
618 if (collection->flag & COLLECTION_IS_MASTER) {
619 BLI_assert_msg(0, "Scene master collection cannot be deleted");
620 return false;
621 }
622
623 /* This is being deleted, no need to handle each item.
624 * NOTE: While it might seem an advantage to use the hash instead of the list-lookup
625 * it is in fact slower because the items are removed in-order,
626 * so the list-lookup succeeds on the first test. */
627 if (collection->runtime->gobject_hash) {
628 MEM_delete(collection->runtime->gobject_hash);
629 collection->runtime->gobject_hash = nullptr;
630 }
631
632 if (hierarchy) {
633 /* Remove child objects. */
634 CollectionObject *cob = collection->gobject.first();
635 while (cob != nullptr) {
637 bmain, collection, cob, LIB_ID_CREATE_NO_DEG_TAG, true);
638 cob = collection->gobject.first();
639 }
640
641 /* Delete all child collections recursively. */
642 CollectionChild *child = collection->children.first();
643 while (child != nullptr) {
644 BKE_collection_delete(bmain, child->collection, hierarchy);
645 child = collection->children.first();
646 }
647 }
648 else {
649 /* Link child collections into parent collection. */
650 for (CollectionChild &child : collection->children) {
651 for (CollectionParent &cparent : collection->runtime->parents) {
652 Collection *parent = cparent.collection;
653 collection_child_add(bmain, parent, child.collection, nullptr, 0, true);
654 }
655 }
656
657 CollectionObject *cob = collection->gobject.first();
658 while (cob != nullptr) {
659 /* Link child object into parent collections. */
660 for (CollectionParent &cparent : collection->runtime->parents) {
661 Collection *parent = cparent.collection;
662 collection_object_add(bmain, parent, cob->ob, nullptr, 0, true);
663 }
664
665 /* Remove child object. */
667 bmain, collection, cob, LIB_ID_CREATE_NO_DEG_TAG, true);
668 cob = collection->gobject.first();
669 }
670 }
671
672 BKE_id_delete(bmain, collection);
673
675
676 return true;
677}
678
680
681/* -------------------------------------------------------------------- */
684
686 Collection *parent,
687 Collection *collection_old,
688 CollectionChild *child_old,
689 const int id_create_flag,
690 const eDupli_ID_Flags duplicate_flags,
691 const eLibIDDuplicateFlags duplicate_options)
692{
693 Collection *collection_new;
694 bool do_full_process = false;
695 const bool is_collection_master = (collection_old->flag & COLLECTION_IS_MASTER) != 0;
696
697 const bool do_objects = (duplicate_flags & USER_DUP_OBJECT) != 0;
698
699 if (is_collection_master) {
700 /* We never duplicate master collections here, but we can still deep-copy their objects and
701 * collections. */
702 BLI_assert(parent == nullptr);
703 collection_new = collection_old;
704 do_full_process = true;
705 }
706 else if (collection_old->id.newid == nullptr) {
707 collection_new = id_cast<Collection *>(BKE_id_copy_for_duplicate(
708 bmain, id_cast<ID *>(collection_old), duplicate_flags, id_create_flag));
709
710 if (collection_new == collection_old) {
711 return collection_new;
712 }
713
714 do_full_process = true;
715 }
716 else {
717 collection_new = id_cast<Collection *>(collection_old->id.newid);
718 }
719
720 /* Optionally add to parent (we always want to do that,
721 * even if collection_old had already been duplicated). */
722 if (parent != nullptr) {
723 if (collection_child_add(bmain,
724 parent,
725 collection_new,
726 child_old ? &child_old->light_linking : nullptr,
727 id_create_flag,
728 true))
729 {
730 /* Put collection right after existing one.
731 * NOTE: Lookup child in the actual parent, as the parent might be different
732 * one when duplicating collection in a linked parent. */
733 CollectionChild *child = BKE_collection_child_find(parent, collection_old);
734 CollectionChild *child_new = BKE_collection_child_find(parent, collection_new);
735
736 if (child && child_new) {
737 BLI_remlink(&parent->children, child_new);
738 BLI_insertlinkafter(&parent->children, child, child_new);
739 }
740 }
741 }
742
743 /* If we are not doing any kind of deep-copy, we can return immediately.
744 * False do_full_process means collection_old had already been duplicated,
745 * no need to redo some deep-copy on it. */
746 if (!do_full_process) {
747 return collection_new;
748 }
749
750 if (do_objects) {
751 /* We need to first duplicate the objects in a separate loop, to support the master collection
752 * case, where both old and new collections are the same.
753 * Otherwise, depending on naming scheme and sorting, we may end up duplicating the new objects
754 * we just added, in some infinite loop. */
755 for (CollectionObject &cob : collection_old->gobject) {
756 Object *ob_old = cob.ob;
757
758 if (ob_old->id.newid == nullptr) {
760 bmain, ob_old, duplicate_flags, duplicate_options | LIB_ID_DUPLICATE_IS_SUBPROCESS);
761 }
762 }
763
764 /* We can loop on collection_old's objects, but have to consider it mutable because with master
765 * collections collection_old and collection_new are the same data here. */
766 for (CollectionObject &cob : collection_old->gobject.items_mutable()) {
767 Object *ob_old = cob.ob;
768 Object *ob_new = id_cast<Object *>(ob_old->id.newid);
769
770 /* New object can be nullptr in master collection case, since new and old objects are in same
771 * collection. */
772 if (ELEM(ob_new, ob_old, nullptr)) {
773 continue;
774 }
775
777 bmain, collection_new, ob_new, &cob.light_linking, id_create_flag, true);
778 collection_object_remove(bmain, collection_new, ob_old, id_create_flag, false);
779 }
780 }
781
782 /* We can loop on collection_old's children,
783 * that list is currently identical the collection_new' children, and won't be changed here. */
784 for (CollectionChild &child_iter : collection_old->children.items_mutable()) {
785 Collection *child_collection_old = child_iter.collection;
786
787 Collection *child_collection_new = collection_duplicate_recursive(bmain,
788 collection_new,
789 child_collection_old,
790 &child_iter,
791 id_create_flag,
792 duplicate_flags,
793 duplicate_options);
794 if (child_collection_new != child_collection_old) {
795 collection_child_remove(bmain, collection_new, child_collection_old, id_create_flag);
796 }
797 }
798
799 return collection_new;
800}
801
803 Collection *parent,
804 CollectionChild *child_old,
805 Collection *collection,
806 eDupli_ID_Flags duplicate_flags,
807 /*eLibIDDuplicateFlags*/ uint duplicate_options)
808{
809 const bool is_subprocess = (duplicate_options & LIB_ID_DUPLICATE_IS_SUBPROCESS) != 0;
810 const bool is_root_id = (duplicate_options & LIB_ID_DUPLICATE_IS_ROOT_ID) != 0;
811 const int id_create_flag = (collection->id.tag & ID_TAG_NO_MAIN) ? LIB_ID_CREATE_NO_MAIN : 0;
812
813 if (!is_subprocess) {
815 }
816 if (is_root_id) {
817 /* In case root duplicated ID is linked, assume we want to get a local copy of it and duplicate
818 * all expected linked data. */
819 if (ID_IS_LINKED(collection)) {
820 duplicate_flags |= USER_DUP_LINKED_ID;
821 }
822 duplicate_options &= ~LIB_ID_DUPLICATE_IS_ROOT_ID;
823 }
824
826 bmain,
827 parent,
828 collection,
829 child_old,
830 id_create_flag,
831 duplicate_flags,
832 eLibIDDuplicateFlags(duplicate_options));
833
834 if (!is_subprocess) {
835 /* `collection_duplicate_recursive` will also tag our 'root' collection, which is not required
836 * unless its duplication is a sub-process of another one. */
837 collection_new->id.tag &= ~ID_TAG_NEW;
838
839 /* This code will follow into all ID links using an ID tagged with ID_TAG_NEW. */
840 /* Unfortunate, but with some types (e.g. meshes), an object is considered in Edit mode if its
841 * obdata contains edit mode runtime data. This can be the case of all newly duplicated
842 * objects, as even though duplicate code move the object back in Object mode, they are still
843 * using the original obdata ID, leading to them being falsely detected as being in Edit mode,
844 * and therefore not remapping their obdata to the newly duplicated one.
845 * See #139715. */
848
849#ifndef NDEBUG
850 /* Call to `BKE_libblock_relink_to_newid` above is supposed to have cleared all those flags. */
851 ID *id_iter;
852 FOREACH_MAIN_ID_BEGIN (bmain, id_iter) {
853 if (id_iter->tag & ID_TAG_NEW) {
854 BLI_assert((id_iter->tag & ID_TAG_NEW) == 0);
855 }
856 }
858#endif
859
860 /* Cleanup. */
862
864 }
865
866 return collection_new;
867}
868
870
871/* -------------------------------------------------------------------- */
874
875void BKE_collection_new_name_get(Collection *collection_parent, char r_name[MAX_ID_NAME - 2])
876{
877 const size_t name_maxncpy = MAX_ID_NAME - 2;
878 if (!collection_parent) {
879 BLI_strncpy_utf8(r_name, DATA_("Collection"), name_maxncpy);
880 }
881 else if (collection_parent->flag & COLLECTION_IS_MASTER) {
883 r_name, name_maxncpy, DATA_("Collection %d"), collection_parent->children.count() + 1);
884 }
885 else {
886 const int number = collection_parent->children.count() + 1;
887 const int digits = integer_digits_i(number);
888 const size_t name_part_maxncpy = name_maxncpy - (1 + digits);
889 const size_t name_part_len = BLI_strncpy_utf8_rlen(
890 r_name, collection_parent->id.name + 2, name_part_maxncpy);
891 BLI_snprintf(r_name + name_part_len, name_maxncpy - name_part_len, " %d", number);
892 }
893}
894
896{
897 if (collection->flag & COLLECTION_IS_MASTER) {
898 return IFACE_("Scene Collection");
899 }
900
901 return collection->id.name + 2;
902}
903
905
906/* -------------------------------------------------------------------- */
909
910static void collection_object_cache_fill(ListBaseT<Base> *lb,
911 Collection *collection,
912 int parent_restrict,
913 bool with_instances)
914{
915 int child_restrict = collection->flag | parent_restrict;
916
917 for (CollectionObject &cob : collection->gobject) {
918 Base *base = static_cast<Base *>(BLI_findptr(lb, cob.ob, offsetof(Base, object)));
919
920 if (base == nullptr) {
921 base = MEM_new<Base>("Object Base");
922 base->object = cob.ob;
923 BLI_addtail(lb, base);
924 if (with_instances && cob.ob->instance_collection) {
926 lb, cob.ob->instance_collection, child_restrict, with_instances);
927 }
928 }
929
930 /* Only collection flags are checked here currently, object restrict flag is checked
931 * in FOREACH_COLLECTION_VISIBLE_OBJECT_RECURSIVE_BEGIN since it can be animated
932 * without updating the cache. */
933 if ((child_restrict & COLLECTION_HIDE_VIEWPORT) == 0) {
935 }
936 if ((child_restrict & COLLECTION_HIDE_RENDER) == 0) {
937 base->flag |= BASE_ENABLED_RENDER;
938 }
939 }
940
941 for (CollectionChild &child : collection->children) {
942 collection_object_cache_fill(lb, child.collection, child_restrict, with_instances);
943 }
944}
945
946ListBaseT<Base> BKE_collection_object_cache_get(Collection *collection)
947{
948 if (!(collection->flag & COLLECTION_HAS_OBJECT_CACHE)) {
949 static Mutex cache_lock;
950
951 std::scoped_lock lock(cache_lock);
952 if (!(collection->flag & COLLECTION_HAS_OBJECT_CACHE)) {
953 collection_object_cache_fill(&collection->runtime->object_cache, collection, 0, false);
954 collection->flag |= COLLECTION_HAS_OBJECT_CACHE;
955 }
956 }
957
958 return collection->runtime->object_cache;
959}
960
962{
963 if (!(collection->flag & COLLECTION_HAS_OBJECT_CACHE_INSTANCED)) {
964 static Mutex cache_lock;
965
966 std::scoped_lock lock(cache_lock);
967 if (!(collection->flag & COLLECTION_HAS_OBJECT_CACHE_INSTANCED)) {
969 &collection->runtime->object_cache_instanced, collection, 0, true);
971 }
972 }
973
974 return collection->runtime->object_cache_instanced;
975}
976
977static void collection_object_cache_free(const Main *bmain,
978 Collection *collection,
979 const int id_create_flag,
980 const uint id_recalc_flag)
981{
983 collection->runtime->object_cache.free_no_destruct();
984 collection->runtime->object_cache_instanced.free_no_destruct();
985
986 /* Although it may seem abusive to call depsgraph updates from this utility function,
987 * it is called from any code-path modifying the collections hierarchy and/or their objects.
988 * Including the reversed-hierarchy walked by #collection_object_cache_free_parent_recursive.
989 *
990 * Plus, the main reason to tag the hierarchy of parents for deg update is because their object
991 * caches are being freed.
992 *
993 * Having this code here avoids the need for another utility tagging function processing the
994 * parent hierarchy as well. */
995 if (id_recalc_flag && (id_create_flag & (LIB_ID_CREATE_NO_MAIN | LIB_ID_CREATE_NO_DEG_TAG)) == 0)
996 {
997 BLI_assert(bmain != nullptr);
998 DEG_id_tag_update_ex(const_cast<Main *>(bmain), &collection->id, id_recalc_flag);
999 }
1000}
1001
1004 Collection *collection,
1005 const int id_create_flag,
1006 const uint id_recalc_flag)
1007{
1008 collection_object_cache_free(bmain, collection, id_create_flag, id_recalc_flag);
1009
1010 /* Clear cache in all parents recursively, since those are affected by changes as well. */
1011 for (CollectionParent &parent : collection->runtime->parents) {
1012 /* In theory there should be no nullptr pointer here. However, this code can be called from
1013 * non-valid temporary states (e.g. indirectly from #BKE_collections_object_remove_invalids
1014 * as part of ID remapping process). */
1015 if (parent.collection == nullptr) {
1016 continue;
1017 }
1019 bmain, parent.collection, id_create_flag, id_recalc_flag);
1020 }
1021}
1022
1024 Collection *collection,
1025 const int id_create_flag)
1026{
1027 BLI_assert(collection != nullptr);
1029 bmain, collection, id_create_flag, ID_RECALC_HIERARCHY | ID_RECALC_GEOMETRY);
1030}
1031
1033{
1034 for (Scene *scene = bmain->scenes.first(); scene != nullptr;
1035 scene = static_cast<Scene *>(scene->id.next))
1036 {
1038 bmain, scene->master_collection, 0, ID_RECALC_HIERARCHY | ID_RECALC_GEOMETRY);
1039 }
1040
1041 for (Collection *collection = bmain->collections.first(); collection != nullptr;
1042 collection = static_cast<Collection *>(collection->id.next))
1043 {
1045 }
1046}
1047
1049 const Scene *scene,
1050 ViewLayer *view_layer,
1051 Collection *collection)
1052{
1053 if (collection) {
1054 return BKE_collection_object_cache_get(collection).first();
1055 }
1056 BKE_view_layer_synced_ensure(bmain, scene, view_layer);
1057 return BKE_view_layer_object_bases_get(view_layer)->first();
1058}
1059
1061
1062/* -------------------------------------------------------------------- */
1065
1067{
1068 BLI_assert(scene != nullptr && scene->master_collection == nullptr);
1069
1070 /* Not an actual datablock, but owned by scene. */
1071 Collection *master_collection = static_cast<Collection *>(BKE_libblock_alloc_in_lib(
1073 master_collection->id.flag |= ID_FLAG_EMBEDDED_DATA;
1074 master_collection->owner_id = &scene->id;
1075 master_collection->flag |= COLLECTION_IS_MASTER;
1076 master_collection->color_tag = COLLECTION_COLOR_NONE;
1077
1078 BLI_assert(scene->id.lib == master_collection->id.lib);
1079 master_collection->runtime = MEM_new<bke::CollectionRuntime>(__func__);
1080
1081 return master_collection;
1082}
1083
1085
1086/* -------------------------------------------------------------------- */
1089
1091{
1092 if (object->instance_collection) {
1093 Collection *dup_collection = object->instance_collection;
1094 if ((dup_collection->id.tag & ID_TAG_DOIT) == 0) {
1095 /* Cycle already exists in collections, let's prevent further creepiness. */
1096 return true;
1097 }
1098 /* flag the object to identify cyclic dependencies in further dupli collections */
1099 dup_collection->id.tag &= ~ID_TAG_DOIT;
1100
1101 if (dup_collection == collection) {
1102 return true;
1103 }
1104
1105 FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN (dup_collection, collection_object) {
1106 if (collection_object_cyclic_check_internal(collection_object, dup_collection)) {
1107 return true;
1108 }
1109 }
1111
1112 /* un-flag the object, it's allowed to have the same collection multiple times in parallel */
1113 dup_collection->id.tag |= ID_TAG_DOIT;
1114 }
1115
1116 return false;
1117}
1118
1120{
1121 /* first flag all collections */
1122 BKE_main_id_tag_listbase(&bmain->collections.cast<ID>(), ID_TAG_DOIT, true);
1123
1124 return collection_object_cyclic_check_internal(object, collection);
1125}
1126
1128
1129/* -------------------------------------------------------------------- */
1132
1133bool BKE_collection_has_object(Collection *collection, const Object *ob)
1134{
1135 if (ELEM(nullptr, collection, ob)) {
1136 return false;
1137 }
1139 return collection->runtime->gobject_hash->contains(ob);
1140}
1141
1143{
1144 if (ELEM(nullptr, collection, ob)) {
1145 return false;
1146 }
1147
1148 const ListBaseT<Base> objects = BKE_collection_object_cache_get(collection);
1149 return BLI_findptr(&objects, ob, offsetof(Base, object));
1150}
1151
1153{
1154 if (ELEM(nullptr, collection, ob)) {
1155 return false;
1156 }
1157
1158 const ListBaseT<Base> objects = BKE_collection_object_cache_instanced_get(collection);
1159 return BLI_findptr(&objects, ob, offsetof(Base, object));
1160}
1161
1163 Object *object_eval)
1164{
1165 BLI_assert(collection_eval->id.tag & ID_TAG_COPIED_ON_EVAL);
1166 const Object *ob_orig = DEG_get_original(object_eval);
1167 const ListBaseT<Base> objects = BKE_collection_object_cache_instanced_get(collection_eval);
1168 for (Base &base : objects) {
1169 if (DEG_get_original(base.object) == ob_orig) {
1170 return true;
1171 }
1172 }
1173 return false;
1174}
1175
1177{
1178 for (CollectionObject &col_ob : collection->gobject) {
1179 if (col_ob.ob->visibility_flag & OB_HIDE_RENDER) {
1180 continue;
1181 }
1183 return true;
1184 }
1185 }
1186
1187 for (CollectionChild &child_col : collection->children) {
1188 if (child_col.collection->flag & COLLECTION_HIDE_RENDER) {
1189 continue;
1190 }
1192 return true;
1193 }
1194 }
1195
1196 return false;
1197}
1198
1199static Collection *collection_next_find(Main *bmain, Scene *scene, Collection *collection)
1200{
1201 if (scene && collection == scene->master_collection) {
1202 return bmain->collections.first();
1203 }
1204
1205 return static_cast<Collection *>(collection->id.next);
1206}
1207
1209 Scene *scene,
1210 Collection *collection,
1211 Object *ob)
1212{
1213 if (collection) {
1214 collection = collection_next_find(bmain, scene, collection);
1215 }
1216 else if (scene) {
1217 collection = scene->master_collection;
1218 }
1219 else {
1220 collection = bmain->collections.first();
1221 }
1222
1223 while (collection) {
1224 if (BKE_collection_has_object(collection, ob)) {
1225 return collection;
1226 }
1227 collection = collection_next_find(bmain, scene, collection);
1228 }
1229 return nullptr;
1230}
1231
1233{
1234 return collection->gobject.is_empty() && collection->children.is_empty();
1235}
1236
1238
1239/* -------------------------------------------------------------------- */
1242
1243static void collection_gobject_assert_internal_consistency(Collection *collection,
1244 const bool do_extensive_check);
1245
1247{
1248 auto *gobject_hash = MEM_new<CollectionObjectMap>(__func__);
1249 gobject_hash->reserve(collection->gobject.count());
1250 return gobject_hash;
1251}
1252
1254{
1255 CollectionObjectMap *gobject_hash = collection_gobject_hash_alloc(collection);
1256 for (CollectionObject &cob : collection->gobject) {
1257 if (cob.ob == nullptr) [[unlikely]] {
1259 continue;
1260 }
1261 /* Do not overwrite an already existing entry. */
1262 if (!gobject_hash->add(cob.ob, &cob)) {
1264 }
1265 }
1266 collection->runtime->gobject_hash = gobject_hash;
1267}
1268
1270{
1271 if (collection->runtime->gobject_hash) {
1272#ifdef USE_DEBUG_EXTRA_GOBJECT_ASSERT
1274#endif
1275 return;
1276 }
1277
1279
1281}
1282
1288{
1289 bool changed = false;
1290
1291 if ((collection->runtime->tag & COLLECTION_TAG_COLLECTION_OBJECT_DIRTY) == 0) {
1292#ifdef USE_DEBUG_EXTRA_GOBJECT_ASSERT
1294#endif
1295 return;
1296 }
1297
1298 CollectionObjectMap *gobject_hash = collection->runtime->gobject_hash;
1299 if (gobject_hash) {
1300 gobject_hash->clear_and_keep_capacity();
1301 }
1302 else {
1303 collection->runtime->gobject_hash = gobject_hash = collection_gobject_hash_alloc(collection);
1304 }
1305
1306 for (CollectionObject &cob : collection->gobject.items_mutable()) {
1307 if (cob.ob == nullptr) {
1308 BLI_freelinkN(&collection->gobject, &cob);
1309 changed = true;
1310 continue;
1311 }
1312
1313 if (!gobject_hash->add(cob.ob, &cob)) {
1314 BLI_freelinkN(&collection->gobject, &cob);
1315 changed = true;
1316 continue;
1317 }
1318 }
1319
1320 if (changed) {
1321 BKE_collection_object_cache_free(bmain, collection, 0);
1322 }
1323
1326}
1327
1343 Object *ob_old,
1344 CollectionObject *cob)
1345{
1346 if (ob_old == cob->ob) {
1347 return;
1348 }
1349
1350 if (ob_old) {
1351 CollectionObject *cob_old = collection->runtime->gobject_hash->pop_default(ob_old, nullptr);
1352 if (cob_old != cob) {
1353 /* Old object already removed from the #GHash. */
1355 }
1356 }
1357
1358 if (cob->ob) {
1359 if (collection->runtime->gobject_hash->add(cob->ob, cob)) {
1360 /* Duplicate #CollectionObject entries. */
1362 }
1363 }
1364 else {
1365 /* #CollectionObject with nullptr object pointer. */
1367 }
1368}
1369
1384 const bool do_extensive_check)
1385{
1387 if (!do_extensive_check) {
1388 return;
1389 }
1390
1391 if (collection->runtime->gobject_hash == nullptr) {
1392 /* NOTE: If the `ghash` does not exist yet, it's creation will assert on errors,
1393 * so in theory the second loop below could be skipped. */
1395 }
1396#ifndef NDEBUG
1397 CollectionObjectMap *gobject_hash = collection->runtime->gobject_hash;
1398 for (CollectionObject &cob : collection->gobject) {
1399 BLI_assert(cob.ob != nullptr);
1400 /* If there are more than one #CollectionObject for the same object,
1401 * at most one of them will pass this test. */
1402 BLI_assert(gobject_hash->lookup_default(cob.ob, nullptr) == &cob);
1403 }
1404#endif
1405}
1406
1416 Collection *collection,
1417 bool &r_is_in_viewlayer)
1418{
1419 LayerCollection *layer_collection = view_layer ?
1421 view_layer, collection) :
1422 nullptr;
1423 r_is_in_viewlayer = layer_collection != nullptr;
1424
1425 if (!BKE_collection_is_content_editable(collection)) {
1426 return false;
1427 }
1428 if (!view_layer) {
1429 return true;
1430 }
1431
1432 if (!layer_collection) {
1433 return false;
1434 }
1435 if (layer_collection->flag & LAYER_COLLECTION_EXCLUDE) {
1436 return false;
1437 }
1438 return true;
1439}
1440
1442 Collection *collection)
1443{
1444 bool is_in_viewlayer = false;
1445 if (collection_is_editable_in_viewlayer(view_layer, collection, is_in_viewlayer)) {
1446 return collection;
1447 }
1448 if (view_layer && !is_in_viewlayer) {
1449 /* In case the collection is not in given view_layer, there is no point in searching in its
1450 * ancestors either. */
1451 return nullptr;
1452 }
1453
1454 if (collection->flag & COLLECTION_IS_MASTER) {
1455 return nullptr;
1456 }
1457
1458 for (CollectionParent &collection_parent : collection->runtime->parents) {
1460 view_layer, collection_parent.collection);
1461 if (editable_collection) {
1462 return editable_collection;
1463 }
1464 }
1465
1466 return nullptr;
1467}
1468
1470{
1471 collection_gobject_hash_ensure(const_cast<Collection *>(&collection));
1472 return collection.runtime->gobject_hash->lookup_default(&ob, nullptr);
1473}
1474
1476{
1477 Collection *collection = nullptr;
1478 while ((collection = BKE_collection_object_find(&bmain, nullptr, collection, &ob))) {
1479 CollectionObject *cob = BKE_collection_object_find_in(*collection, ob);
1480 if (cob != nullptr) {
1481 cob->parented_sort_index = -1;
1482 }
1483 }
1484}
1485
1487{
1488 Collection *collection = nullptr;
1489 while ((collection = BKE_collection_object_find(&bmain, nullptr, collection, &ob))) {
1490 CollectionObject *cob = BKE_collection_object_find_in(*collection, ob);
1491 if (cob != nullptr) {
1492 cob->parented_sort_index = -1;
1493 if (ob.parent != nullptr) {
1494 for (Object *parent_iter = ob.parent; parent_iter != nullptr;
1495 parent_iter = parent_iter->parent)
1496 {
1497 if (BKE_collection_object_find_in(*collection, *parent_iter) != nullptr) {
1498 cob->sort_index = -1;
1499 break;
1500 }
1501 }
1502 }
1503 }
1504 }
1505}
1506
1507static bool collection_object_add(Main *bmain,
1508 Collection *collection,
1509 Object *ob,
1510 CollectionLightLinking *light_linking,
1511 const int id_create_flag,
1512 const bool add_us)
1513{
1514 /* Cyclic dependency check. */
1515 if (ob->instance_collection) {
1516 if ((ob->instance_collection == collection) ||
1518 {
1519 return false;
1520 }
1521 }
1522
1524
1525 bool newly_added = false;
1526 CollectionObject *cob = collection->runtime->gobject_hash->lookup_or_add_cb(ob, [&]() {
1527 newly_added = true;
1528 return MEM_new<CollectionObject>("collection_object_add");
1529 });
1530 if (!newly_added) {
1531 return false;
1532 }
1533
1534 cob->ob = ob;
1535 if (light_linking) {
1536 cob->light_linking = *light_linking;
1537 }
1538 BLI_addtail(&collection->gobject, cob);
1539 BKE_collection_object_cache_free(bmain, collection, id_create_flag);
1540
1541 if (add_us && (id_create_flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
1542 id_us_plus(&ob->id);
1543 }
1544
1545 if ((id_create_flag & LIB_ID_CREATE_NO_MAIN) == 0) {
1546 BKE_rigidbody_main_collection_object_add(bmain, collection, ob);
1547 }
1548
1549 return true;
1550}
1551
1557 Collection *collection,
1558 CollectionObject *cob,
1559 const int id_create_flag,
1560 const bool free_us)
1561{
1562 Object *ob = cob->ob;
1563 BLI_freelinkN(&collection->gobject, cob);
1564 BKE_collection_object_cache_free(bmain, collection, id_create_flag);
1565
1566 if (free_us) {
1567 BKE_id_free_us(bmain, ob);
1568 }
1569 else {
1570 id_us_min(&ob->id);
1571 }
1572}
1573
1575 Main *bmain, Collection *collection, Object *ob, const int id_create_flag, const bool free_us)
1576{
1578 CollectionObject *cob = collection->runtime->gobject_hash->pop_default(ob, nullptr);
1579 if (cob == nullptr) {
1580 return false;
1581 }
1582 collection_object_remove_no_gobject_hash(bmain, collection, cob, id_create_flag, free_us);
1583 return true;
1584}
1585
1586bool BKE_collection_is_content_editable(const Collection *collection, std::string *r_reason)
1587{
1588 if (ID_IS_OVERRIDE_LIBRARY(collection)) {
1589 if (r_reason) {
1590 *r_reason = fmt::format(fmt::runtime(RPT_("Collection '{}' is overridden.")),
1591 collection->id.name + 2);
1592 }
1593 return false;
1594 }
1595
1596 if (!ID_IS_EDITABLE(collection)) {
1597 if (r_reason) {
1598 *r_reason = fmt::format(fmt::runtime(RPT_("Collection '{}' is linked.")),
1599 collection->id.name + 2);
1600 }
1601 return false;
1602 }
1603
1604 if (collection->importer != nullptr) {
1605 if (r_reason) {
1606 *r_reason = fmt::format(
1607 fmt::runtime(RPT_("Collection '{}' belongs to a collection importer.")),
1608 collection->id.name + 2);
1609 }
1610 return false;
1611 }
1612
1613 return true;
1614}
1615
1617{
1618 /* Add a new #CollectionExport item to our handler list and fill it with #FileHandlerType
1619 * information. Also load in the operator's properties now as well. */
1620 CollectionImport *data = MEM_new<CollectionImport>("CollectionImport");
1621 STRNCPY(data->fh_idname, idname);
1622
1623 IDPropertyTemplate val{};
1624 data->import_properties = IDP_New(IDP_GROUP, &val, "import_properties");
1625 data->flag |= IO_HANDLER_PANEL_OPEN;
1626
1627 data->runtime = MEM_new<bke::CollectionImportRuntime>("CollectionImportRuntime");
1628
1629 collection->importer = data;
1630
1631 return data;
1632}
1633
1634CollectionExport *BKE_collection_exporter_add(Collection *collection, char *idname, char *label)
1635{
1636 /* Add a new #CollectionExport item to our handler list and fill it with #FileHandlerType
1637 * information. Also load in the operator's properties now as well. */
1638 CollectionExport *data = MEM_new<CollectionExport>("CollectionExport");
1639 STRNCPY(data->fh_idname, idname);
1640
1641 BKE_collection_exporter_name_set(&collection->exporters, data, label);
1642
1643 IDPropertyTemplate val{};
1644 data->export_properties = IDP_New(IDP_GROUP, &val, "export_properties");
1645 data->flag |= IO_HANDLER_PANEL_OPEN;
1646
1647 BLI_addtail(&collection->exporters, data);
1648 collection->active_exporter_index = collection->exporters.count() - 1;
1649
1650 return data;
1651}
1652
1654{
1655 ListBaseT<CollectionExport> *exporters = &collection->exporters;
1656 BLI_remlink(exporters, data);
1658
1659 MEM_delete(data);
1660
1661 const int count = exporters->count();
1662 const int new_index = count == 0 ? 0 : std::min(collection->active_exporter_index, count - 1);
1663 collection->active_exporter_index = new_index;
1664}
1665
1666bool BKE_collection_exporter_move(Collection *collection, const int from, const int to)
1667{
1668 if (from == to) {
1669 return false;
1670 }
1671
1672 return BLI_listbase_move_index(&collection->exporters, from, to);
1673}
1674
1676{
1677 CollectionImport *new_data = MEM_new<CollectionImport>("CollectionImport");
1678 STRNCPY(new_data->fh_idname, data->fh_idname);
1679 new_data->import_properties = IDP_CopyProperty(data->import_properties);
1680 new_data->flag = data->flag;
1681 new_data->runtime = MEM_new<bke::CollectionImportRuntime>("CollectionImportRuntime");
1682 new_data->runtime->archive_library = data->runtime->archive_library;
1683 collection->importer = new_data;
1684}
1685
1687{
1688 CollectionExport *new_data = MEM_new<CollectionExport>("CollectionExport");
1689 STRNCPY(new_data->fh_idname, data->fh_idname);
1690 new_data->export_properties = IDP_CopyProperty(data->export_properties);
1691 new_data->flag = data->flag;
1692
1693 /* Clear the `filepath` property. */
1694 IDProperty *filepath = IDP_GetPropertyFromGroup(new_data->export_properties, "filepath");
1695 if (filepath) {
1696 IDP_AssignString(filepath, "");
1697 }
1698 for (LayoutPanelState &state : data->layout_panel_states) {
1699 LayoutPanelState *new_state = MEM_new<LayoutPanelState>(__func__, state);
1700 new_state->idname = BLI_strdup(state.idname);
1701 BLI_addtail(&new_data->layout_panel_states, new_state);
1702 }
1703 BLI_addtail(&collection->exporters, new_data);
1704}
1705
1707{
1708 if (ob == nullptr) {
1709 return false;
1710 }
1711
1712 /* Only case where this pointer can be nullptr is when scene itself is linked, this case should
1713 * never be reached. */
1714 BLI_assert(collection != nullptr);
1715 if (collection == nullptr) {
1716 return false;
1717 }
1718
1719 const int id_create_flag = (collection->id.tag & ID_TAG_NO_MAIN) ? LIB_ID_CREATE_NO_MAIN : 0;
1720 if (!collection_object_add(bmain, collection, ob, nullptr, id_create_flag, true)) {
1721 return false;
1722 }
1723
1724 if (BKE_collection_is_in_scene(collection)) {
1726 }
1727
1728 return true;
1729}
1730
1731bool BKE_collection_object_add(Main *bmain, Collection *collection, Object *ob)
1732{
1733 return BKE_collection_viewlayer_object_add(bmain, nullptr, collection, ob);
1734}
1735
1737 const ViewLayer *view_layer,
1738 Collection *collection,
1739 Object *ob)
1740{
1741 if (collection == nullptr) {
1742 return false;
1743 }
1744
1745 collection = BKE_collection_parent_editable_find_recursive(view_layer, collection);
1746
1747 if (collection == nullptr) {
1748 return false;
1749 }
1750
1751 return BKE_collection_object_add_notest(bmain, collection, ob);
1752}
1753
1754void BKE_collection_object_add_from(Main *bmain, Scene *scene, Object *ob_src, Object *ob_dst)
1755{
1756 bool is_instantiated = false;
1757
1758 FOREACH_SCENE_COLLECTION_BEGIN (scene, collection) {
1759 if (BKE_collection_is_content_editable(collection) &&
1760 BKE_collection_has_object(collection, ob_src))
1761 {
1762 collection_object_add(bmain, collection, ob_dst, nullptr, 0, true);
1763 is_instantiated = true;
1764 }
1765 }
1767
1768 if (!is_instantiated) {
1769 /* In case we could not find any non-linked collections in which instantiate our ob_dst,
1770 * fall back to scene's master collection... */
1771 collection_object_add(bmain, scene->master_collection, ob_dst, nullptr, 0, true);
1772 }
1773
1775}
1776
1778 Collection *collection,
1779 Object *ob,
1780 const bool free_us)
1781{
1782 if (ELEM(nullptr, collection, ob)) {
1783 return false;
1784 }
1785
1786 const int id_create_flag = (collection->id.tag & ID_TAG_NO_MAIN) ? LIB_ID_CREATE_NO_MAIN : 0;
1787 if (!collection_object_remove(bmain, collection, ob, id_create_flag, free_us)) {
1788 return false;
1789 }
1790
1791 if (BKE_collection_is_in_scene(collection)) {
1793 }
1794
1795 return true;
1796}
1797
1799 Collection *collection,
1800 Object *ob_old,
1801 Object *ob_new)
1802{
1804 CollectionObject *cob;
1805 cob = collection->runtime->gobject_hash->pop_default(ob_old, nullptr);
1806 if (cob == nullptr) {
1807 return false;
1808 }
1809
1810 if (!collection->runtime->gobject_hash->contains(ob_new)) {
1811 id_us_min(&cob->ob->id);
1812 cob->ob = ob_new;
1813 id_us_plus(&cob->ob->id);
1814
1815 collection->runtime->gobject_hash->add(cob->ob, cob);
1816 }
1817 else {
1818 collection_object_remove_no_gobject_hash(bmain, collection, cob, 0, false);
1819 }
1820
1821 if (BKE_collection_is_in_scene(collection)) {
1823 }
1824
1825 return true;
1826}
1827
1833 Main *bmain, Scene *scene, Object *ob, const bool free_us, Collection *collection_skip)
1834{
1835 bool removed = false;
1836 const int id_create_flag = (scene->id.tag & ID_TAG_NO_MAIN) ? LIB_ID_CREATE_NO_MAIN : 0;
1837
1838 /* If given object is removed from all collections in given scene, then it can also be safely
1839 * removed from rigidbody world for given scene. */
1840 if (collection_skip == nullptr) {
1841 BKE_scene_remove_rigidbody_object(bmain, scene, ob, free_us);
1842 }
1843
1844 FOREACH_SCENE_COLLECTION_BEGIN (scene, collection) {
1845 if (!BKE_collection_is_content_editable(collection)) {
1846 continue;
1847 }
1848 if (collection == collection_skip) {
1849 continue;
1850 }
1851
1852 removed |= collection_object_remove(bmain, collection, ob, id_create_flag, free_us);
1853 }
1855
1857
1858 return removed;
1859}
1860
1861bool BKE_scene_collections_object_remove(Main *bmain, Scene *scene, Object *ob, const bool free_us)
1862{
1863 return scene_collections_object_remove(bmain, scene, ob, free_us, nullptr);
1864}
1865
1867{
1868 for (Scene &scene : bmain->scenes) {
1870 }
1871
1872 for (Collection &collection : bmain->collections) {
1873 collection_gobject_hash_ensure_fix(bmain, &collection);
1874 }
1875}
1876
1878{
1879 for (CollectionChild &child : collection->children.items_mutable()) {
1880 if (child.collection == nullptr) {
1881 BLI_freelinkN(&collection->children, &child);
1882 }
1883 }
1884}
1885
1887{
1888 for (CollectionParent &parent : collection->runtime->parents.items_mutable()) {
1889 if ((parent.collection == nullptr) ||
1890 !BKE_collection_child_find(parent.collection, collection))
1891 {
1892 BLI_freelinkN(&collection->runtime->parents, &parent);
1893 }
1894 }
1895}
1896
1898 Collection *parent_collection,
1899 Collection *child_collection)
1900{
1901 if (child_collection == nullptr) {
1902 if (parent_collection != nullptr) {
1903 collection_null_children_remove(parent_collection);
1904 }
1905 else {
1906 /* We need to do the checks in two steps when more than one collection may be involved,
1907 * otherwise we can miss some cases...
1908 * Also, master collections are not in bmain, so we also need to loop over scenes.
1909 */
1910 for (Collection &collection : bmain->collections) {
1912 }
1913 for (Scene &scene : bmain->scenes) {
1915 }
1916 }
1917
1918 for (Collection &collection : bmain->collections) {
1920 }
1921 for (Scene &scene : bmain->scenes) {
1923 }
1924 }
1925 else {
1926 for (CollectionParent &parent : child_collection->runtime->parents.items_mutable()) {
1927 collection_null_children_remove(parent.collection);
1928
1929 if (!BKE_collection_child_find(parent.collection, child_collection)) {
1930 BLI_freelinkN(&child_collection->runtime->parents, &parent);
1931 }
1932 }
1933 }
1934}
1935
1937 Main *bmain, Scene *scene, Collection *collection_dst, Collection *collection_src, Object *ob)
1938{
1939 /* In both cases we first add the object, then remove it from the other collections.
1940 * Otherwise we lose the original base and whether it was active and selected. */
1941 if (collection_src != nullptr) {
1942 if (BKE_collection_object_add(bmain, collection_dst, ob)) {
1943 BKE_collection_object_remove(bmain, collection_src, ob, false);
1944 }
1945 }
1946 else {
1947 /* Adding will fail if object is already in collection.
1948 * However we still need to remove it from the other collections. */
1949 BKE_collection_object_add(bmain, collection_dst, ob);
1950 scene_collections_object_remove(bmain, scene, ob, false, collection_dst);
1951 }
1952}
1953
1955
1956/* -------------------------------------------------------------------- */
1959
1961{
1962 if (collection->flag & COLLECTION_IS_MASTER) {
1963 return true;
1964 }
1965
1966 for (CollectionParent &cparent : collection->runtime->parents) {
1968 return true;
1969 }
1970 }
1971
1972 return false;
1973}
1974
1976{
1977 /* Need to update layer collections because objects might have changed
1978 * in linked files, and because undo push does not include updated base
1979 * flags since those are refreshed after the operator completes. */
1981}
1982
1984
1985/* -------------------------------------------------------------------- */
1988
1990 Collection *instance_collection)
1991{
1992 for (CollectionObject &collection_object : collection->gobject) {
1993 if (collection_object.ob != nullptr &&
1994 /* Object from a given collection should never instantiate that collection either. */
1995 ELEM(collection_object.ob->instance_collection, instance_collection, collection))
1996 {
1997 return true;
1998 }
1999 }
2000
2001 for (CollectionChild &collection_child : collection->children) {
2002 if (collection_child.collection != nullptr &&
2003 collection_instance_find_recursive(collection_child.collection, instance_collection))
2004 {
2005 return true;
2006 }
2007 }
2008
2009 return false;
2010}
2011
2012bool BKE_collection_cycle_find(Collection *new_ancestor, Collection *collection)
2013{
2014 if (collection == new_ancestor) {
2015 return true;
2016 }
2017
2018 if (collection == nullptr) {
2019 collection = new_ancestor;
2020 }
2021
2022 for (CollectionParent &parent : new_ancestor->runtime->parents) {
2023 if (BKE_collection_cycle_find(parent.collection, collection)) {
2024 return true;
2025 }
2026 }
2027
2028 /* Find possible objects in collection or its children, that would instantiate the given ancestor
2029 * collection (that would also make a fully invalid cycle of dependencies). */
2030 return collection_instance_find_recursive(collection, new_ancestor);
2031}
2032
2033static bool collection_instance_fix_recursive(Collection *parent_collection,
2034 Collection *collection)
2035{
2036 bool cycles_found = false;
2037
2038 for (CollectionObject &collection_object : parent_collection->gobject) {
2039 if (collection_object.ob != nullptr && collection_object.ob->instance_collection == collection)
2040 {
2041 id_us_min(&collection->id);
2042 collection_object.ob->instance_collection = nullptr;
2043 cycles_found = true;
2044 }
2045 }
2046
2047 for (CollectionChild &collection_child : parent_collection->children) {
2048 if (collection_instance_fix_recursive(collection_child.collection, collection)) {
2049 cycles_found = true;
2050 }
2051 }
2052
2053 return cycles_found;
2054}
2055
2057 Collection *parent_collection,
2058 Collection *collection)
2059{
2060 bool cycles_found = false;
2061
2062 for (CollectionParent &parent : parent_collection->runtime->parents.items_mutable()) {
2063 if (BKE_collection_cycle_find(parent.collection, collection)) {
2064 BKE_collection_child_remove(bmain, parent.collection, parent_collection);
2065 cycles_found = true;
2066 }
2067 else if (collection_cycle_fix_recursive(bmain, parent.collection, collection)) {
2068 cycles_found = true;
2069 }
2070 }
2071
2072 return cycles_found;
2073}
2074
2076{
2077 return collection_cycle_fix_recursive(bmain, collection, collection) ||
2078 collection_instance_fix_recursive(collection, collection);
2079}
2080
2082{
2083 return static_cast<CollectionChild *>(
2084 BLI_findptr(&parent->children, collection, offsetof(CollectionChild, collection)));
2085}
2086
2087static bool collection_find_child_recursive(const Collection *parent, const Collection *collection)
2088{
2089 for (const CollectionChild &child : parent->children) {
2090 if (child.collection == collection) {
2091 return true;
2092 }
2093
2094 if (collection_find_child_recursive(child.collection, collection)) {
2095 return true;
2096 }
2097 }
2098
2099 return false;
2100}
2101
2102bool BKE_collection_has_collection(const Collection *parent, const Collection *collection)
2103{
2104 return collection_find_child_recursive(parent, collection);
2105}
2106
2108{
2109 return static_cast<CollectionParent *>(
2110 BLI_findptr(&child->runtime->parents, collection, offsetof(CollectionParent, collection)));
2111}
2112
2113static bool collection_child_add(Main *bmain,
2114 Collection *parent,
2115 Collection *collection,
2116 const CollectionLightLinking *light_linking,
2117 const int id_create_flag,
2118 const bool add_us)
2119{
2120 CollectionChild *child = BKE_collection_child_find(parent, collection);
2121 if (child) {
2122 return false;
2123 }
2124 if (BKE_collection_cycle_find(parent, collection)) {
2125 return false;
2126 }
2127
2128 child = MEM_new<CollectionChild>("CollectionChild");
2129 child->collection = collection;
2130 if (light_linking) {
2131 child->light_linking = *light_linking;
2132 }
2133 BLI_addtail(&parent->children, child);
2134
2135 /* Don't add parent links for depsgraph datablocks, these are not kept in sync. */
2136 if ((id_create_flag & LIB_ID_CREATE_NO_MAIN) == 0) {
2137 CollectionParent *cparent = MEM_new_zeroed<CollectionParent>("CollectionParent");
2138 cparent->collection = parent;
2139 BLI_addtail(&collection->runtime->parents, cparent);
2140 }
2141
2142 if (add_us) {
2143 id_us_plus(&collection->id);
2144 }
2145
2146 BKE_collection_object_cache_free(bmain, parent, id_create_flag);
2147
2148 return true;
2149}
2150
2152 Collection *parent,
2153 Collection *collection,
2154 const int id_create_flag)
2155{
2156 CollectionChild *child = BKE_collection_child_find(parent, collection);
2157 if (child == nullptr) {
2158 return false;
2159 }
2160
2161 CollectionParent *cparent = collection_find_parent(collection, parent);
2162 BLI_freelinkN(&collection->runtime->parents, cparent);
2163 BLI_freelinkN(&parent->children, child);
2164
2165 id_us_min(&collection->id);
2166
2167 BKE_collection_object_cache_free(bmain, parent, id_create_flag);
2168
2169 return true;
2170}
2171
2173{
2174 if (!collection_child_add(bmain, parent, child, nullptr, 0, true)) {
2175 return false;
2176 }
2177
2179 return true;
2180}
2181
2183{
2184 return collection_child_add(bmain, parent, child, nullptr, 0, true);
2185}
2186
2188{
2189 if (!collection_child_remove(bmain, parent, child, 0)) {
2190 return false;
2191 }
2192
2194 return true;
2195}
2196
2198{
2199 for (CollectionChild &child : collection->children.items_mutable()) {
2200 /* Check for duplicated children (can happen with remapping e.g.). */
2201 CollectionChild *other_child = BKE_collection_child_find(collection, child.collection);
2202 if (other_child != &child) {
2203 BLI_freelinkN(&collection->children, &child);
2204 continue;
2205 }
2206
2207 /* Invalid child, either without a collection, or because it creates a dependency cycle. */
2208 if (child.collection == nullptr || BKE_collection_cycle_find(collection, child.collection)) {
2209 BLI_freelinkN(&collection->children, &child);
2210 continue;
2211 }
2212
2213 /* Can happen when remapping data partially out-of-Main (during advanced ID management
2214 * operations like lib-override resync e.g.). */
2215 if ((child.collection->id.tag & (ID_TAG_NO_MAIN | ID_TAG_COPIED_ON_EVAL)) != 0) {
2216 continue;
2217 }
2218
2219 BLI_assert(collection_find_parent(child.collection, collection) == nullptr);
2221 cparent->collection = collection;
2222 BLI_addtail(&child.collection->runtime->parents, cparent);
2223 }
2224}
2225
2227{
2228 /* A same collection may be child of several others, no need to process it more than once. */
2229 if ((collection->runtime->tag & COLLECTION_TAG_RELATION_REBUILD) == 0) {
2230 return;
2231 }
2232
2235
2236 for (CollectionChild &child : collection->children) {
2237 /* See comment above in `BKE_collection_parent_relations_rebuild`. */
2238 if ((child.collection->id.tag & (ID_TAG_NO_MAIN | ID_TAG_COPIED_ON_EVAL)) != 0) {
2239 continue;
2240 }
2242 }
2243}
2244
2246{
2247 /* Only collections not in bmain (master ones in scenes) have no parent... */
2248 for (Collection &collection : bmain->collections) {
2249 collection.runtime->parents.free_no_destruct();
2250
2252 }
2253
2254 /* Scene's master collections will be 'root' parent of most of our collections, so start with
2255 * them. */
2256 for (Scene &scene : bmain->scenes) {
2257 /* This function can be called from `readfile.cc`, when this pointer is not guaranteed to be
2258 * nullptr.
2259 */
2260 if (scene.master_collection != nullptr) {
2261 BLI_assert(scene.master_collection->runtime->parents.is_empty());
2264 }
2265 }
2266
2267 /* We may have parent chains outside of scene's master_collection context? At least, readfile's
2268 * #collection_blend_read_after_liblink() seems to assume that, so do the same here. */
2269 for (Collection &collection : bmain->collections) {
2270 if (collection.runtime->tag & COLLECTION_TAG_RELATION_REBUILD) {
2271 /* NOTE: we do not have easy access to 'which collections is root' info in that case, which
2272 * means test for cycles in collection relationships may fail here. I don't think that is an
2273 * issue in practice here, but worth keeping in mind... */
2275 }
2276 }
2277}
2278
2280{
2281 if (!BLI_listbase_validate(&collection->children)) {
2282 return false;
2283 }
2284 if (!BLI_listbase_validate(&collection->runtime->parents)) {
2285 return false;
2286 }
2287 if (BKE_collection_cycle_find(collection, nullptr)) {
2288 return false;
2289 }
2290
2291 bool is_ok = true;
2292
2293 /* Check that children have each collection used/referenced only once. */
2294 Set<Collection *> processed_collections;
2295 for (CollectionChild &child : collection->children) {
2296 if (!processed_collections.add(child.collection)) {
2297 is_ok = false;
2298 }
2299 }
2300
2301 /* Check that parents have each collection used/referenced only once. */
2302 processed_collections.clear();
2303 for (CollectionParent &parent : collection->runtime->parents) {
2304 if (!processed_collections.add(parent.collection)) {
2305 is_ok = false;
2306 }
2307 }
2308
2309 return is_ok;
2310}
2311
2313
2314/* -------------------------------------------------------------------- */
2317
2319 uint64_t session_uid)
2320{
2321
2322 for (CollectionChild &child : collection->children) {
2323 if (child.collection->id.session_uid == session_uid) {
2324 return child.collection;
2325 }
2326 Collection *nested = collection_from_session_uid_recursive(child.collection, session_uid);
2327 if (nested != nullptr) {
2328 return nested;
2329 }
2330 }
2331 return nullptr;
2332}
2333
2335{
2336 Collection *master_collection = scene->master_collection;
2337 if (master_collection->id.session_uid == session_uid) {
2338 return scene->master_collection;
2339 }
2340 return collection_from_session_uid_recursive(master_collection, session_uid);
2341}
2342
2344{
2345 for (Scene &scene : bmain->scenes) {
2346 Collection *collection = BKE_collection_from_session_uid(&scene, session_uid);
2347 if (collection) {
2348 if (r_scene) {
2349 *r_scene = &scene;
2350 }
2351 return collection;
2352 }
2353 }
2354 return nullptr;
2355}
2356
2358
2359/* -------------------------------------------------------------------- */
2362
2364 Collection *to_parent,
2365 Collection *from_parent,
2366 Collection *relative,
2367 bool relative_after,
2368 Collection *collection)
2369{
2370 if (collection->flag & COLLECTION_IS_MASTER) {
2371 return false;
2372 }
2373 if (BKE_collection_cycle_find(to_parent, collection)) {
2374 return false;
2375 }
2376
2377 /* Move to new parent collection */
2378 if (from_parent) {
2379 collection_child_remove(bmain, from_parent, collection, 0);
2380 }
2381
2382 collection_child_add(bmain, to_parent, collection, nullptr, 0, true);
2383
2384 /* Move to specified location under parent. */
2385 if (relative) {
2386 CollectionChild *child = BKE_collection_child_find(to_parent, collection);
2387 CollectionChild *relative_child = BKE_collection_child_find(to_parent, relative);
2388
2389 if (relative_child) {
2390 BLI_remlink(&to_parent->children, child);
2391
2392 if (relative_after) {
2393 BLI_insertlinkafter(&to_parent->children, relative_child, child);
2394 }
2395 else {
2396 BLI_insertlinkbefore(&to_parent->children, relative_child, child);
2397 }
2398
2399 BKE_collection_object_cache_free(bmain, to_parent, 0);
2400 }
2401 }
2402
2403 /* Update layer collections. */
2405
2406 return true;
2407}
2408
2410
2411/* -------------------------------------------------------------------- */
2414
2415/* Scene collection iterator. */
2416
2422
2424 BKE_scene_collections_Cb callback,
2425 void *data)
2426{
2427 callback(collection, data);
2428
2429 for (CollectionChild &child : collection->children) {
2430 scene_collection_callback(child.collection, callback, data);
2431 }
2432}
2433
2434static void scene_collections_count(Collection * /*collection*/, void *data)
2435{
2436 int *tot = static_cast<int *>(data);
2437 (*tot)++;
2438}
2439
2440static void scene_collections_build_array(Collection *collection, void *data)
2441{
2442 Collection ***array = static_cast<Collection ***>(data);
2443 **array = collection;
2444 (*array)++;
2445}
2446
2448 Collection ***r_collections_array,
2449 int *r_collections_array_len)
2450{
2451 *r_collections_array = nullptr;
2452 *r_collections_array_len = 0;
2453
2454 if (scene == nullptr) {
2455 return;
2456 }
2457
2458 Collection *collection = scene->master_collection;
2459 BLI_assert(collection != nullptr);
2460 scene_collection_callback(collection, scene_collections_count, r_collections_array_len);
2461
2462 BLI_assert(*r_collections_array_len > 0);
2463
2464 Collection **array = MEM_new_array_uninitialized<Collection *>(size_t(*r_collections_array_len),
2465 "CollectionArray");
2466 *r_collections_array = array;
2468}
2469
2471{
2472 Scene *scene = static_cast<Scene *>(data_in);
2474
2475 data->scene = scene;
2476
2477 BLI_ITERATOR_INIT(iter);
2478 iter->data = data;
2479
2480 scene_collections_array(scene, reinterpret_cast<Collection ***>(&data->array), &data->tot);
2481 BLI_assert(data->tot != 0);
2482
2483 data->cur = 0;
2484 iter->current = data->array[data->cur];
2485}
2486
2488{
2490
2491 if (++data->cur < data->tot) {
2492 iter->current = data->array[data->cur];
2493 }
2494 else {
2495 iter->valid = false;
2496 }
2497}
2498
2500{
2502
2503 if (data) {
2504 if (data->array) {
2505 MEM_delete(data->array);
2506 }
2507 MEM_delete(data);
2508 }
2509 iter->valid = false;
2510}
2511
2512/* scene objects iterator */
2513
2519
2521 Scene *scene,
2522 Set<Object *> *visited_objects)
2523{
2525
2526 BLI_ITERATOR_INIT(iter);
2527 iter->data = data;
2528
2529 /* Lookup list to make sure that each object is only processed once. */
2530 if (visited_objects != nullptr) {
2531 data->visited = visited_objects;
2532 }
2533 else {
2534 data->visited = MEM_new<Set<Object *>>(__func__);
2535 }
2536
2537 /* We wrap the scene-collection iterator here to go over the scene collections. */
2538 BKE_scene_collections_iterator_begin(&data->scene_collection_iter, scene);
2539
2540 Collection *collection = static_cast<Collection *>(data->scene_collection_iter.current);
2541 data->cob_next = collection->gobject.first();
2542
2544}
2545
2547{
2548 Scene *scene = static_cast<Scene *>(data_in);
2549
2550 scene_objects_iterator_begin(iter, scene, nullptr);
2551}
2552
2554{
2555 if (!iter->valid) {
2556 return;
2557 }
2558
2559 /* Unpack the data. */
2561 iter->data = data->iter_data;
2562
2563 Object *ob = static_cast<Object *>(iter->current);
2564 if (ob && (ob->flag & data->flag) == 0) {
2565 iter->skip = true;
2566 }
2567
2568 /* Pack the data. */
2569 data->iter_data = iter->data;
2570 iter->data = data;
2571}
2572
2574{
2576
2578
2579 /* Pack the data. */
2580 data->iter_data = iter->data;
2581 iter->data = data_in;
2582
2584}
2585
2587{
2588 /* Unpack the data. */
2590 iter->data = data->iter_data;
2591
2593
2594 /* Pack the data. */
2595 data->iter_data = iter->data;
2596 iter->data = data;
2597
2599}
2600
2602{
2603 /* Unpack the data. */
2605 iter->data = data->iter_data;
2606
2608
2609 /* Pack the data. */
2610 data->iter_data = iter->data;
2611 iter->data = data;
2612}
2613
2618{
2619 for (; cob != nullptr; cob = cob->next) {
2620 if (gs.add(cob->ob)) {
2621 return cob;
2622 }
2623 }
2624 return nullptr;
2625}
2626
2628{
2630 CollectionObject *cob = data->cob_next ? object_base_unique(*data->visited, data->cob_next) :
2631 nullptr;
2632
2633 if (cob) {
2634 data->cob_next = cob->next;
2635 iter->current = cob->ob;
2636 }
2637 else {
2638 /* if this is the last object of this ListBaseT look at the next Collection */
2639 Collection *collection;
2640 BKE_scene_collections_iterator_next(&data->scene_collection_iter);
2641 do {
2642 collection = static_cast<Collection *>(data->scene_collection_iter.current);
2643 /* get the first unique object of this collection */
2644 CollectionObject *new_cob = object_base_unique(*data->visited, collection->gobject.first());
2645 if (new_cob) {
2646 data->cob_next = new_cob->next;
2647 iter->current = new_cob->ob;
2648 return;
2649 }
2650 BKE_scene_collections_iterator_next(&data->scene_collection_iter);
2651 } while (data->scene_collection_iter.valid);
2652
2653 if (!data->scene_collection_iter.valid) {
2654 iter->valid = false;
2655 }
2656 }
2657}
2658
2660{
2662 if (data) {
2663 BKE_scene_collections_iterator_end(&data->scene_collection_iter);
2664 if (data->visited != nullptr) {
2665 MEM_delete(data->visited);
2666 }
2667 MEM_delete(data);
2668 }
2669}
2670
2672{
2673 BLI_Iterator iter;
2674 scene_objects_iterator_begin(&iter, scene, objects_set);
2675 while (iter.valid) {
2677 }
2678
2679 /* `return_set` is either given `objects_set` (if non-nullptr), or the Set allocated by the
2680 * iterator. Either way, we want to get it back, and prevent `BKE_scene_objects_iterator_end`
2681 * from freeing it. */
2682 Set<Object *> *return_set = (static_cast<SceneObjectsIteratorData *>(iter.data))->visited;
2683 (static_cast<SceneObjectsIteratorData *>(iter.data))->visited = nullptr;
2685
2686 return return_set;
2687}
2688
2690
2691} // namespace blender
#define FOREACH_COLLECTION_OBJECT_RECURSIVE_END
#define FOREACH_SCENE_COLLECTION_END
#define FOREACH_SCENE_COLLECTION_BEGIN(scene, _instance)
#define FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(_collection, _object)
#define BKE_SCENE_COLLECTION_NAME
#define IDP_BlendDataRead(reader, prop)
#define BKE_LIB_FOREACHID_PROCESS_IDSUPER(data_, id_super_, cb_flag_)
#define BKE_LIB_FOREACHID_PROCESS_ID(data_, id_, cb_flag_)
#define FOREACH_MAIN_ID_END
Definition BKE_main.hh:735
#define FOREACH_MAIN_ID_BEGIN(_bmain, _id)
Definition BKE_main.hh:729
General operations, lookup, etc. for blender objects.
API for Blender-side Rigid Body stuff.
#define BLI_ITERATOR_INIT(iter)
#define STRNCPY_UTF8(dst, src)
#define BLO_read_struct_list(reader, struct_name, list)
#define BLO_read_struct(reader, struct_name, ptr_p)
#define RPT_(msgid)
#define IFACE_(msgid)
#define BLT_I18NCONTEXT_ID_COLLECTION
#define DATA_(msgid)
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
ID and Library types, which are fundamental for SDNA.
#define FILTER_ID_OB
Definition DNA_ID.h:1216
#define ID_IS_OVERRIDABLE_LIBRARY(_id)
Definition DNA_ID.h:721
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:697
#define MAX_ID_NAME
Definition DNA_ID.h:367
#define FILTER_ID_GR
Definition DNA_ID.h:1205
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:736
#define FILTER_ID_LI
Definition DNA_ID.h:1237
Object groups, one object can be in many groups at once.
#define COLLECTION_FLAG_ALL_RUNTIME
These structs are the foundation for all linked lists in the library system.
Object is a sort of wrapper for general info.
Types and defines for representing Rigid Body entities.
Read Blender memory allocation and freeing.
volatile int lock
#define INIT_DEFAULT_STRUCT_AFTER(struct_dst, member)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
Value lookup_default(const Key &key, const Value &default_value) const
Definition BLI_map.hh:570
void clear_and_keep_capacity()
Definition BLI_map.hh:1051
Value & lookup_or_add_cb(const Key &key, const CreateValueF &create_value)
Definition BLI_map.hh:620
bool contains(const Key &key) const
Definition BLI_map.hh:353
Value pop_default(const Key &key, const Value &default_value)
Definition BLI_map.hh:439
bool add(const Key &key)
Definition BLI_set.hh:248
void clear()
Definition BLI_set.hh:570
#define offsetof(t, d)
const int state
BMesh Iterators.
@ COLLECTION_TAG_RELATION_REBUILD
@ COLLECTION_TAG_COLLECTION_OBJECT_DIRTY
void * BLI_findptr(const ListBase *listbase, const void *ptr, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:710
static bool scene_collections_object_remove(Main *bmain, Scene *scene, Object *ob, const bool free_us, Collection *collection_skip)
bool BKE_collection_delete(Main *bmain, Collection *collection, bool hierarchy)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
size_t size_t size_t BLI_snprintf_utf8(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
unsigned __int64 uint64_t
Definition BKE_text.h:91
size_t BLI_snprintf(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
static bool collection_cycle_fix_recursive(Main *bmain, Collection *parent_collection, Collection *collection)
void BKE_previewimg_id_free(ID *id)
static void collection_parents_rebuild_recursive(Collection *collection)
void BKE_scene_objects_iterator_end(BLI_Iterator *iter)
static void collection_object_cache_fill(ListBaseT< Base > *lb, Collection *collection, int parent_restrict, bool with_instances)
char * STRNCPY(char(&dst)[N], const char *src)
void BKE_scene_objects_iterator_begin(BLI_Iterator *iter, void *data_in)
static void collection_gobject_hash_ensure(Collection *collection)
void BKE_id_delete(Main *bmain, void *idv, const BKEIDDeleteOptions &options={}) ATTR_NONNULL()
bool BKE_collection_has_object_recursive_instanced(Collection *collection, Object *ob)
Collection * BKE_collection_duplicate(Main *bmain, Collection *parent, CollectionChild *child_old, Collection *collection, eDupli_ID_Flags duplicate_flags, uint duplicate_options)
void * BKE_libblock_alloc_in_lib(Main *bmain, std::optional< Library * > owner_library, short type, const char *name, int flag) ATTR_WARN_UNUSED_RESULT
Definition lib_id.cc:1439
bool BKE_collection_has_object_recursive_instanced_orig_id(Collection *collection_eval, Object *object_eval)
unsigned int uint
Definition BKE_text.h:55
static void collection_init_data(ID *id)
void BKE_previewimg_blend_read(BlendDataReader *reader, PreviewImage *prv)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void BKE_id_free_us(Main *bmain, void *idv) ATTR_NONNULL()
void BKE_libblock_relink_to_newid(Main *bmain, ID *id, int remap_flag) ATTR_NONNULL()
Definition lib_remap.cc:932
const char * BKE_collection_ui_name_get(Collection *collection)
void BKE_collections_child_remove_nulls(Main *bmain, Collection *parent_collection, Collection *child_collection)
static void collection_object_cache_free_parent_recursive(const Main *bmain, Collection *collection, const int id_create_flag, const uint id_recalc_flag)
static void collection_gobject_hash_create(Collection *collection)
void id_us_plus(ID *id)
Definition lib_id.cc:433
Collection * BKE_collection_from_session_uid(Scene *scene, uint64_t session_uid)
void BKE_collection_blend_write_prepare_nolib(BlendWriter *writer, Collection *collection)
bool BKE_collection_viewlayer_object_add(Main *bmain, const ViewLayer *view_layer, Collection *collection, Object *ob)
@ BASE_ENABLED_VIEWPORT
static CollectionParent * collection_find_parent(Collection *child, Collection *collection)
char size_t BLI_strncpy_utf8_rlen(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
@ IDWALK_NO_ORIG_POINTERS_ACCESS
bool BKE_collection_child_add(Main *bmain, Collection *parent, Collection *child)
void BKE_previewimg_blend_write(BlendWriter *writer, const PreviewImage *prv)
IDProperty * IDP_CopyProperty(const IDProperty *prop) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:986
void BKE_scene_remove_rigidbody_object(Main *bmain, Scene *scene, Object *ob, bool free_us)
Definition scene.cc:2547
IDProperty * IDP_GetPropertyFromGroup(const IDProperty *prop, StringRef name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:818
void IDP_BlendWrite(BlendWriter *writer, const IDProperty *prop)
Definition idprop.cc:1696
CollectionObject * BKE_collection_object_find_in(const Collection &collection, const Object &ob)
void BKE_main_collections_parent_relations_rebuild(Main *bmain)
MINLINE int integer_digits_i(int i)
static void scene_collections_build_array(Collection *collection, void *data)
static bool collection_object_cyclic_check_internal(Object *object, Collection *collection)
void BKE_main_collections_object_cache_free(const Main *bmain)
static void collection_object_remove_no_gobject_hash(Main *bmain, Collection *collection, CollectionObject *cob, const int id_create_flag, const bool free_us)
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
bool BKE_scene_collections_object_remove(Main *bmain, Scene *scene, Object *ob, bool free_us)
void BKE_scene_collections_iterator_next(BLI_Iterator *iter)
ID * BKE_id_copy_for_duplicate(Main *bmain, ID *id, eDupli_ID_Flags duplicate_flags, int copy_flags)
Definition lib_id.cc:861
static void collection_free_data(ID *id)
static Collection * collection_add(Main *bmain, Collection *collection_parent, const char *name_custom)
void BKE_collection_importer_free_data(CollectionImport *data)
@ ID_REMAP_SKIP_USER_CLEAR
@ ID_REMAP_FORCE_OBDATA_IN_EDITMODE
void BKE_collection_object_parent_clear_sort_index_reset(Main &bmain, Object &ob)
LibraryForeachIDFlag BKE_lib_query_foreachid_process_flags_get(const LibraryForeachIDData *data)
Definition lib_query.cc:133
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition lib_id.cc:2890
CollectionImport * BKE_collection_importer_add(Collection *collection, const char *idname)
void BKE_collection_object_cache_free(const Main *bmain, Collection *collection, const int id_create_flag)
static void collection_gobject_hash_update_object(Collection *collection, Object *ob_old, CollectionObject *cob)
static void collection_exporter_copy(Collection *collection, CollectionExport *data)
static void collection_blend_write(BlendWriter *writer, ID *id, const void *id_address)
bool BKE_collection_cycles_fix(Main *bmain, Collection *collection)
@ LIB_ID_CREATE_NO_USER_REFCOUNT
@ LIB_ID_COPY_NO_PREVIEW
@ LIB_ID_CREATE_NO_DEG_TAG
@ LIB_ID_CREATE_NO_MAIN
static bool collection_instance_fix_recursive(Collection *parent_collection, Collection *collection)
bool BKE_collection_exporter_move(Collection *collection, const int from, const int to)
bool BKE_collection_has_object(Collection *collection, const Object *ob)
void BKE_libblock_free_data(ID *id, bool do_id_user) ATTR_NONNULL()
bool BKE_collection_object_add_notest(Main *bmain, Collection *collection, Object *ob)
eLibIDDuplicateFlags
@ LIB_ID_DUPLICATE_IS_SUBPROCESS
@ LIB_ID_DUPLICATE_IS_ROOT_ID
Collection * BKE_collection_object_find(Main *bmain, Scene *scene, Collection *collection, Object *ob)
bool BLI_listbase_validate(ListBase *lb)
Definition listbase.cc:895
void BKE_collection_free_data(Collection *collection)
bool BKE_collection_validate(Collection *collection)
static void collection_object_cache_free(const Main *bmain, Collection *collection, const int id_create_flag, const uint id_recalc_flag)
void BLI_uniquename(const ListBase *list, void *vlink, const char *defname, char delim, int name_offset, size_t name_maxncpy) ATTR_NONNULL(1
static ID ** collection_owner_pointer_get(ID *id, const bool debug_relationship_assert)
void DEG_id_tag_update_ex(Main *bmain, ID *id, unsigned int flags)
static void scene_collections_array(Scene *scene, Collection ***r_collections_array, int *r_collections_array_len)
static void scene_objects_iterator_skip_invalid_flag(BLI_Iterator *iter)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void BKE_collection_object_parented_sort_index_reset(Main &bmain, Object &ob)
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
static bool collection_find_child_recursive(const Collection *parent, const Collection *collection)
BMesh const char void * data
void BLI_insertlinkafter(ListBase *listbase, void *vprevlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:339
@ LAYER_COLLECTION_EXCLUDE
bool BKE_collection_contains_geometry_recursive(const Collection *collection)
void BKE_collection_add_from_object(Main *bmain, Scene *scene, const Object *ob_src, Collection *collection_dst)
Object * BKE_object_duplicate(Main *bmain, Object *ob, eDupli_ID_Flags dupflag, uint duplicate_options)
Definition object.cc:2659
bool BKE_collection_move(Main *bmain, Collection *to_parent, Collection *from_parent, Collection *relative, bool relative_after, Collection *collection)
void BKE_collections_object_remove_invalids(Main *bmain)
bool BLI_listbase_move_index(ListBase *listbase, int from, int to) ATTR_NONNULL()
Definition listbase.cc:474
bool BKE_collection_is_in_scene(Collection *collection)
bool BKE_collection_is_empty(const Collection *collection)
static bool collection_is_editable_in_viewlayer(const ViewLayer *view_layer, Collection *collection, bool &r_is_in_viewlayer)
bool BKE_collection_object_cyclic_check(Main *bmain, Object *object, Collection *collection)
static void collection_gobject_hash_ensure_fix(Main *bmain, Collection *collection)
void IDP_AssignString(IDProperty *prop, const char *st) ATTR_NONNULL()
Definition idprop.cc:505
CollectionChild * BKE_collection_child_find(Collection *parent, Collection *collection)
static void collection_missing_parents_remove(Collection *collection)
static Collection * collection_next_find(Main *bmain, Scene *scene, Collection *collection)
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:277
Base * BKE_collection_or_layer_objects(const Main &bmain, const Scene *scene, ViewLayer *view_layer, Collection *collection)
void BKE_collection_add_from_collection(Main *bmain, Scene *scene, Collection *collection_src, Collection *collection_dst)
CollectionExport * BKE_collection_exporter_add(Collection *collection, char *idname, char *label)
void BKE_collection_blend_write_nolib(BlendWriter *writer, Collection *collection)
Collection * BKE_collection_parent_editable_find_recursive(const ViewLayer *view_layer, Collection *collection)
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
LayerCollection * BKE_layer_collection_first_from_scene_collection(const ViewLayer *view_layer, const Collection *collection)
bool BKE_collection_child_remove(Main *bmain, Collection *parent, Collection *child)
void BKE_collections_after_lib_link(Main *bmain)
void BKE_collection_object_add_from(Main *bmain, Scene *scene, Object *ob_src, Object *ob_dst)
void BKE_collection_object_move(Main *bmain, Scene *scene, Collection *collection_dst, Collection *collection_src, Object *ob)
bool BKE_collection_object_add(Main *bmain, Collection *collection, Object *ob)
void BKE_scene_collections_iterator_begin(BLI_Iterator *iter, void *data_in)
bool BKE_collection_has_collection(const Collection *parent, const Collection *collection)
static void collection_blend_read_after_liblink(BlendLibReader *, ID *id)
IDTypeInfo IDType_ID_GR
@ LIBRARY_FLAG_IS_ARCHIVE
@ LIBRARY_FLAG_IS_EXTERNAL
static void scene_collection_callback(Collection *collection, BKE_scene_collections_Cb callback, void *data)
ListBaseT< Base > BKE_collection_object_cache_instanced_get(Collection *collection)
void BKE_scene_collections_iterator_end(BLI_Iterator *iter)
ListBaseT< Base > BKE_collection_object_cache_get(Collection *collection)
void BLI_insertlinkbefore(ListBase *listbase, void *vnextlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:378
void(*)(Collection *ob, void *data) BKE_scene_collections_Cb
static void collection_null_children_remove(Collection *collection)
void BKE_rigidbody_main_collection_object_add(struct Main *bmain, struct Collection *collection, struct Object *object)
void BKE_scene_objects_iterator_next(BLI_Iterator *iter)
bool BKE_collection_cycle_find(Collection *new_ancestor, Collection *collection)
bool BKE_collection_child_add_no_sync(Main *bmain, Collection *parent, Collection *child)
static void collection_blend_read_data(BlendDataReader *reader, ID *id)
void BKE_main_id_newptr_and_tag_clear(Main *bmain)
Definition lib_id.cc:2097
Set< Object * > * BKE_scene_objects_as_set(Scene *scene, Set< Object * > *objects_set)
void BKE_collection_parent_relations_rebuild(Collection *collection)
void BKE_previewimg_id_copy(ID *new_id, const ID *old_id)
bool BKE_collection_is_content_editable(const Collection *collection, std::string *reason=nullptr)
static void scene_collections_count(Collection *, void *data)
void BKE_scene_objects_iterator_next_ex(BLI_Iterator *iter)
T * DEG_get_original(T *id)
bool BKE_collection_object_remove(Main *bmain, Collection *collection, Object *ob, bool free_us)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
@ COLLECTION_HAS_OBJECT_CACHE_INSTANCED
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
@ INDEX_ID_GR
Definition DNA_ID.h:1342
void IDP_FreeProperty(IDProperty *prop)
Definition idprop.cc:1403
void BKE_collection_exporter_remove(Collection *collection, CollectionExport *data)
int BLO_read_fileversion_get(BlendDataReader *reader)
Definition readfile.cc:6546
bool BKE_collection_object_replace(Main *bmain, Collection *collection, Object *ob_old, Object *ob_new)
Collection * BKE_collection_master_add(Scene *scene)
void BKE_collection_exporter_name_set(const ListBaseT< CollectionExport > *exporters, CollectionExport *data, const char *newname)
void BKE_scene_objects_iterator_end_ex(BLI_Iterator *iter)
std::mutex Mutex
Definition BLI_mutex.hh:51
static void collection_copy_data(Main *bmain, std::optional< Library * >, ID *id_dst, const ID *id_src, const int flag)
@ ID_RECALC_HIERARCHY
Definition DNA_ID.h:1158
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
ID_Type GS(const char *name)
Definition DNA_ID.h:410
static CollectionObjectMap * collection_gobject_hash_alloc(const Collection *collection)
void BKE_collection_exporter_free_data(CollectionExport *data)
static void collection_importer_copy(Collection *collection, const CollectionImport *data)
static bool collection_instance_find_recursive(Collection *collection, Collection *instance_collection)
@ ID_TAG_NO_MAIN
Definition DNA_ID.h:981
@ ID_TAG_COPIED_ON_EVAL
Definition DNA_ID.h:1000
@ ID_TAG_DOIT
Definition DNA_ID.h:1029
@ ID_TAG_NEW
Definition DNA_ID.h:922
void BKE_main_id_tag_listbase(ListBaseT< ID > *lb, int tag, bool value)
Definition lib_id.cc:1291
bool DEG_object_has_geometry_component(Object *object)
void BKE_scene_objects_iterator_begin_ex(BLI_Iterator *iter, void *data_in)
static CLG_LogRef LOG
Definition logging.cpp:12
IDProperty * IDP_New(char type, const IDPropertyTemplate *val, StringRef name, eIDPropertyFlag flags={}) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:1132
static Collection * collection_duplicate_recursive(Main *bmain, Collection *parent, Collection *collection_old, CollectionChild *child_old, const int id_create_flag, const eDupli_ID_Flags duplicate_flags, const eLibIDDuplicateFlags duplicate_options)
@ ID_FLAG_EMBEDDED_DATA
Definition DNA_ID.h:775
static bool collection_child_add(Main *bmain, Collection *parent, Collection *collection, const CollectionLightLinking *light_linking, const int id_create_flag, const bool add_us)
bool BKE_collection_has_object_recursive(Collection *collection, Object *ob)
static void collection_gobject_assert_internal_consistency(Collection *collection, const bool do_extensive_check)
LibraryForeachIDCallbackFlag
@ IDWALK_CB_LOOPBACK
@ IDWALK_CB_WRITEFILE_IGNORE
@ IDWALK_CB_EMBEDDED_NOT_OWNING
@ IDWALK_CB_OVERRIDE_LIBRARY_HIERARCHY_DEFAULT
@ IDWALK_CB_NEVER_SELF
@ IDWALK_CB_READFILE_IGNORE
static bool collection_object_add(Main *bmain, Collection *collection, Object *ob, CollectionLightLinking *light_linking, const int id_create_flag, const bool add_us)
Map< const Object *, CollectionObject * > CollectionObjectMap
static void scene_objects_iterator_begin(BLI_Iterator *iter, Scene *scene, Set< Object * > *visited_objects)
void BKE_collection_new_name_get(Collection *collection_parent, char r_name[256])
bool BKE_main_collection_sync(const Main *bmain)
static CollectionObject * object_base_unique(Set< Object * > &gs, CollectionObject *cob)
ListBase BLI_listbase_from_link(Link *some_link)
Definition listbase.cc:814
void id_us_min(ID *id)
Definition lib_id.cc:441
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1612
ListBaseT< Base > * BKE_view_layer_object_bases_get(ViewLayer *view_layer)
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:6653
void BKE_collection_blend_read_data(BlendDataReader *reader, Collection *collection, ID *owner_id)
static bool collection_object_remove(Main *bmain, Collection *collection, Object *ob, const int id_create_flag, const bool free_us)
static void collection_foreach_id(ID *id, LibraryForeachIDData *data)
static Collection * collection_from_session_uid_recursive(Collection *collection, uint64_t session_uid)
Collection * BKE_collection_add(Main *bmain, Collection *collection_parent, const char *name_custom)
@ IDTYPE_FLAGS_NO_ANIMDATA
Definition BKE_idtype.hh:52
@ IDTYPE_FLAGS_APPEND_IS_REUSABLE
Definition BKE_idtype.hh:50
static bool collection_child_remove(Main *bmain, Collection *parent, Collection *collection, const int id_create_flag)
struct Object * object
void write_string(const char *data)
void write_id_struct(const void *id_address, const T *id, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct_list(const ListBaseT< T > *list, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
CollectionLightLinking light_linking
ListBaseT< LayoutPanelState > layout_panel_states
bke::CollectionImportRuntime * runtime
CollectionLightLinking light_linking
struct CollectionObject * next
struct Collection * collection
bke::CollectionRuntime * runtime
ListBaseT< CollectionExport > exporters
ListBaseT< CollectionChild > children
ListBaseT< CollectionObject > gobject
struct PreviewImage * preview
CollectionColorTag color_tag
CollectionImport * importer
char name[258]
Definition DNA_ID.h:433
unsigned int session_uid
Definition DNA_ID.h:463
struct ID * newid
Definition DNA_ID.h:419
struct Library * lib
Definition DNA_ID.h:421
short flag
Definition DNA_ID.h:439
void * next
Definition DNA_ID.h:418
eLayerCollection_Flag flag
ListBaseT< Collection > collections
Definition BKE_main.hh:406
ListBaseT< Scene > scenes
Definition BKE_main.hh:386
struct Object * parent
eObject_VisibilityFlag visibility_flag
struct Collection * instance_collection
struct Collection * master_collection
ListBaseT< Base > object_cache_instanced
ListBaseT< CollectionParent > parents
CollectionObjectMap * gobject_hash
#define N_(msgid)