Blender V5.3
blenkernel/intern/layer.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 <atomic>
13#include <cstring>
14
15#include "CLG_log.h"
16
17#include "BLI_listbase.hh"
18#include "BLI_mempool.hh"
19#include "BLI_string.hh"
20#include "BLI_string_utf8.hh"
21#include "BLI_string_utils.hh"
22#include "BLI_threads.hh"
23#include "BLT_translation.hh"
24
25#include "BKE_animsys.hh"
26#include "BKE_collection.hh"
27#include "BKE_freestyle.h"
28#include "BKE_idprop.hh"
29#include "BKE_layer.hh"
30#include "BKE_lib_id.hh"
31#include "BKE_library.hh"
32#include "BKE_main.hh"
33#include "BKE_node.hh"
35#include "BKE_node_runtime.hh"
36#include "BKE_object.hh"
37
38#include "DNA_ID.h"
40#include "DNA_layer_types.h"
41#include "DNA_node_types.h"
42#include "DNA_object_types.h"
43#include "DNA_scene_types.h"
44#include "DNA_space_types.h"
45#include "DNA_view3d_types.h"
47#include "DNA_world_types.h"
48
49#include "RNA_path.hh"
50
51#include "DEG_depsgraph.hh"
54
55#include "SEQ_relations.hh"
56
57#include "DRW_engine.hh"
58
59#include "RE_engine.h"
60
61#include "MEM_guardedalloc.h"
62
63#include "BLO_read_write.hh"
64
65namespace blender {
66
67static CLG_LogRef LOG = {"object.layer"};
68
69/* Set of flags which are dependent on a collection settings. */
75
76/* prototype */
77static void object_bases_iterator_next(BLI_Iterator *iter, const int flag);
78
79/* -------------------------------------------------------------------- */
82
83static LayerCollection *layer_collection_add(ListBaseT<LayerCollection> *lb_parent,
84 Collection *collection)
85{
86 LayerCollection *lc = MEM_new<LayerCollection>("Collection Base");
87 lc->collection = collection;
89 BLI_addtail(lb_parent, lc);
90
91 return lc;
92}
93
94static void layer_collection_free(ViewLayer *view_layer, LayerCollection *lc)
95{
96 if (lc == view_layer->active_collection) {
97 view_layer->active_collection = nullptr;
98 }
99
100 for (LayerCollection &nlc : lc->layer_collections.items_mutable()) {
101 layer_collection_free(view_layer, &nlc);
102 MEM_delete(&nlc);
103 }
104 lc->layer_collections.clear_no_delete();
105}
106
108{
109 Base *base = MEM_new<Base>("Object Base");
110 base->object = ob;
111 base->local_view_bits = ~0;
112 if (ob->base_flag & BASE_SELECTED) {
113 base->flag |= BASE_SELECTED;
114 }
115 return base;
116}
117
119
120/* -------------------------------------------------------------------- */
123
124/* RenderLayer */
125
127{
128 for (ViewLayer &view_layer : scene->view_layers) {
129 if (!(view_layer.flag & VIEW_LAYER_RENDER)) {
130 return &view_layer;
131 }
132 }
133
134 BLI_assert(scene->view_layers.first());
135 return scene->view_layers.first();
136}
137
139{
140 for (ViewLayer &view_layer : scene->view_layers) {
141 if (view_layer.flag & VIEW_LAYER_RENDER) {
142 return &view_layer;
143 }
144 }
145
146 BLI_assert(scene->view_layers.first());
147 return scene->view_layers.first();
148}
149
150ViewLayer *BKE_view_layer_find(const Scene *scene, const char *layer_name)
151{
152 for (ViewLayer &view_layer : scene->view_layers) {
153 if (STREQ(view_layer.name, layer_name)) {
154 return &view_layer;
155 }
156 }
157
158 return nullptr;
159}
160
162{
163 BLI_assert(scene->view_layers.first());
164 return scene->view_layers.first();
165}
166
167static ViewLayer *view_layer_add(const char *name)
168{
169 if (!name) {
170 name = DATA_("ViewLayer");
171 }
172
173 ViewLayer *view_layer = MEM_new<ViewLayer>("View Layer");
174 view_layer->runtime = MEM_new<ViewLayerRuntime>("View Layer Runtime");
175 STRNCPY_UTF8(view_layer->name, name);
176
178
179 return view_layer;
180}
181
182static void layer_collection_exclude_all(LayerCollection *layer_collection)
183{
184 LayerCollection *sub_collection = layer_collection->layer_collections.first();
185 for (; sub_collection != nullptr; sub_collection = sub_collection->next) {
186 sub_collection->flag |= LAYER_COLLECTION_EXCLUDE;
187 layer_collection_exclude_all(sub_collection);
188 }
189}
190
192 Scene *scene,
193 const char *name,
194 ViewLayer *view_layer_source,
195 const int type)
196{
197 BLI_assert_msg(bmain || type != VIEWLAYER_ADD_EMPTY,
198 "A valid Main is required with `VIEWLAYER_ADD_EMPTY` type of process");
199
200 ViewLayer *view_layer_new;
201
202 if (view_layer_source) {
203 name = view_layer_source->name;
204 }
205
206 switch (type) {
207 default:
208 case VIEWLAYER_ADD_NEW: {
209 view_layer_new = view_layer_add(name);
210 BLI_addtail(&scene->view_layers, view_layer_new);
211 if (bmain) {
212 BKE_layer_collection_sync(*bmain, scene, view_layer_new);
213 }
214 else {
215 BKE_view_layer_need_resync_tag(view_layer_new);
216 }
217 break;
218 }
219 case VIEWLAYER_ADD_COPY: {
220 /* Allocate and copy view layer data */
221 view_layer_new = MEM_new<ViewLayer>("View Layer");
222 *view_layer_new = *view_layer_source;
223 BKE_view_layer_copy_data(scene, scene, view_layer_new, view_layer_source, 0);
224 BLI_addtail(&scene->view_layers, view_layer_new);
225
226 STRNCPY_UTF8(view_layer_new->name, name);
227 break;
228 }
229 case VIEWLAYER_ADD_EMPTY: {
230 view_layer_new = view_layer_add(name);
231 BLI_addtail(&scene->view_layers, view_layer_new);
232
233 /* Initialize layer-collections. */
234 BKE_layer_collection_sync(*bmain, scene, view_layer_new);
235 layer_collection_exclude_all(view_layer_new->layer_collections.first());
236
237 /* Update collections after changing visibility */
238 BKE_layer_collection_sync(*bmain, scene, view_layer_new);
239 break;
240 }
241 }
242
243 /* unique name */
245 view_layer_new,
246 DATA_("ViewLayer"),
247 '_',
248 offsetof(ViewLayer, name),
249 sizeof(view_layer_new->name));
250
251 return view_layer_new;
252}
253
255{
256 BKE_view_layer_free_ex(view_layer, true);
257}
258
259void BKE_view_layer_free_ex(ViewLayer *view_layer, const bool do_id_user)
260{
262
263 view_layer->aovs.free_no_destruct();
264 view_layer->active_aov = nullptr;
265 view_layer->lightgroups.free_no_destruct();
266 view_layer->active_lightgroup = nullptr;
267
268 /* Cannot use MEM_SAFE_DELETE, as #SceneStats type is only forward-declared in
269 * `DNA_layer_types.h`
270 */
271 if (view_layer->stats) {
272 MEM_delete_void(static_cast<void *>(view_layer->stats));
273 view_layer->stats = nullptr;
274 }
275
276 BKE_freestyle_config_free(&view_layer->freestyle_config, do_id_user);
277
278 if (view_layer->id_properties) {
279 IDP_FreeProperty_ex(view_layer->id_properties, do_id_user);
280 }
281 if (view_layer->system_properties) {
282 IDP_FreeProperty_ex(view_layer->system_properties, do_id_user);
283 }
284
285 MEM_delete(view_layer->runtime);
286 MEM_delete(view_layer);
287}
288
290{
291 view_layer->basact = nullptr;
292
293 view_layer->object_bases.free_no_destruct();
294
295 MEM_SAFE_DELETE(view_layer->runtime->object_bases_hash);
296
297 for (LayerCollection &lc : view_layer->layer_collections.items_mutable()) {
298 layer_collection_free(view_layer, &lc);
299 MEM_delete(&lc);
300 }
301 view_layer->layer_collections.clear_no_delete();
302}
303
305 const Scene *scene,
306 ViewLayer *view_layer,
307 const eObject_Flag tag)
308{
309 BKE_view_layer_synced_ensure(bmain, scene, view_layer);
310 for (Base &base : *BKE_view_layer_object_bases_get(view_layer)) {
311 if ((base.flag & BASE_SELECTED) != 0) {
312 base.object->flag |= tag;
313 }
314 else {
315 base.object->flag &= ~tag;
316 }
317 }
318}
319
320static bool find_scene_collection_in_scene_collections(ListBaseT<LayerCollection> *lb,
321 const LayerCollection *lc)
322{
323 for (LayerCollection &lcn : *lb) {
324 if (&lcn == lc) {
325 return true;
326 }
327 if (find_scene_collection_in_scene_collections(&lcn.layer_collections, lc)) {
328 return true;
329 }
330 }
331 return false;
332}
333
334Object *BKE_view_layer_camera_find(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
335{
336 BKE_view_layer_synced_ensure(bmain, scene, view_layer);
337 for (Base &base : *BKE_view_layer_object_bases_get(view_layer)) {
338 if (base.object->type == OB_CAMERA) {
339 return base.object;
340 }
341 }
342
343 return nullptr;
344}
345
347{
348 for (ViewLayer &view_layer : scene->view_layers) {
350 return &view_layer;
351 }
352 }
353
354 return nullptr;
355}
356
357/* Base */
358
359static void view_layer_bases_hash_create(ViewLayer *view_layer, const bool do_base_duplicates_fix)
360{
361 static Mutex hash_lock;
362
363 if (view_layer->runtime->object_bases_hash == nullptr) {
364 std::scoped_lock lock(hash_lock);
365
366 if (view_layer->runtime->object_bases_hash == nullptr) {
367 ObjectBasesMap *hash = MEM_new<ObjectBasesMap>(__func__);
368
369 for (Base &base : view_layer->object_bases.items_mutable()) {
370 if (base.object) {
371 if (!hash->add(base.object, &base)) {
372 /* The same object has several bases.
373 *
374 * In normal cases this is a serious bug, but this is a common situation when remapping
375 * an object into another one already present in the same View Layer. While ideally we
376 * would process this case separately, for performances reasons it makes more sense to
377 * tackle it here. */
378 if (do_base_duplicates_fix) {
379 if (view_layer->basact == &base) {
380 view_layer->basact = nullptr;
381 }
382 BLI_freelinkN(&view_layer->object_bases, &base);
383 }
384 else {
386 &LOG,
387 "Object '%s' has more than one entry in view layer's object bases listbase",
388 base.object->id.name + 2);
389 }
390 }
391 }
392 }
393
394 /* Assign pointer only after hash is complete. */
395 view_layer->runtime->object_bases_hash = hash;
396 }
397 }
398}
399
401{
402 BLI_assert_msg((view_layer->flag & VIEW_LAYER_OUT_OF_SYNC) == 0,
403 "View layer out of sync, invoke BKE_view_layer_synced_ensure.");
404 if (!view_layer->runtime->object_bases_hash) {
405 view_layer_bases_hash_create(view_layer, false);
406 }
407
408 return view_layer->runtime->object_bases_hash->lookup_default(ob, nullptr);
409}
410
411void BKE_view_layer_base_deselect_all(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
412{
413 BLI_assert(scene);
414 BLI_assert(view_layer);
415
416 BKE_view_layer_synced_ensure(bmain, scene, view_layer);
417 for (Base &base : *BKE_view_layer_object_bases_get(view_layer)) {
418 base.flag &= ~BASE_SELECTED;
419 }
420}
421
423{
424 view_layer->basact = selbase;
425 if ((selbase->flag & BASE_SELECTABLE) != 0) {
426 selbase->flag |= BASE_SELECTED;
427 }
428}
429
431
432/* -------------------------------------------------------------------- */
435
436static void layer_aov_copy_data(ViewLayer *view_layer_dst,
437 const ViewLayer *view_layer_src,
438 ListBaseT<ViewLayerAOV> *aovs_dst,
439 const ListBaseT<ViewLayerAOV> *aovs_src)
440{
441 BLI_duplicatelist(aovs_dst, aovs_src);
442
443 ViewLayerAOV *aov_dst = aovs_dst->first();
444 const ViewLayerAOV *aov_src = aovs_src->first();
445
446 while (aov_dst != nullptr) {
447 BLI_assert(aov_src);
448 if (aov_src == view_layer_src->active_aov) {
449 view_layer_dst->active_aov = aov_dst;
450 }
451
452 aov_dst = aov_dst->next;
453 aov_src = aov_src->next;
454 }
455}
456
457static void layer_lightgroup_copy_data(ViewLayer *view_layer_dst,
458 const ViewLayer *view_layer_src,
459 ListBaseT<ViewLayerLightgroup> *lightgroups_dst,
460 const ListBaseT<ViewLayerLightgroup> *lightgroups_src)
461{
462 if (lightgroups_src != nullptr) {
463 BLI_duplicatelist(lightgroups_dst, lightgroups_src);
464 }
465
466 ViewLayerLightgroup *lightgroup_dst = lightgroups_dst->first();
467 const ViewLayerLightgroup *lightgroup_src = lightgroups_src->first();
468
469 while (lightgroup_dst != nullptr) {
470 BLI_assert(lightgroup_src);
471 if (lightgroup_src == view_layer_src->active_lightgroup) {
472 view_layer_dst->active_lightgroup = lightgroup_dst;
473 }
474
475 lightgroup_dst = lightgroup_dst->next;
476 lightgroup_src = lightgroup_src->next;
477 }
478}
479
480static void layer_collections_copy_data(ViewLayer *view_layer_dst,
481 const ViewLayer *view_layer_src,
482 ListBaseT<LayerCollection> *layer_collections_dst,
483 const ListBaseT<LayerCollection> *layer_collections_src)
484{
485 BLI_duplicatelist(layer_collections_dst, layer_collections_src);
486
487 LayerCollection *layer_collection_dst = layer_collections_dst->first();
488 const LayerCollection *layer_collection_src = layer_collections_src->first();
489
490 while (layer_collection_dst != nullptr) {
491 layer_collections_copy_data(view_layer_dst,
492 view_layer_src,
493 &layer_collection_dst->layer_collections,
494 &layer_collection_src->layer_collections);
495
496 if (layer_collection_src == view_layer_src->active_collection) {
497 view_layer_dst->active_collection = layer_collection_dst;
498 }
499
500 layer_collection_dst = layer_collection_dst->next;
501 layer_collection_src = layer_collection_src->next;
502 }
503}
504
506 const Scene * /*scene_src*/,
507 ViewLayer *view_layer_dst,
508 const ViewLayer *view_layer_src,
509 const int flag)
510{
511 BLI_assert_msg(view_layer_dst->runtime == view_layer_src->runtime,
512 "view_layer_dst must be a shallow copy of view_layer_src");
513 view_layer_dst->runtime = MEM_new<ViewLayerRuntime>(__func__);
514
515 if (view_layer_dst->id_properties != nullptr) {
516 view_layer_dst->id_properties = IDP_CopyProperty_ex(view_layer_dst->id_properties, flag);
517 }
518 if (view_layer_dst->system_properties != nullptr) {
519 view_layer_dst->system_properties = IDP_CopyProperty_ex(view_layer_dst->system_properties,
520 flag);
521 }
523 &view_layer_dst->freestyle_config, &view_layer_src->freestyle_config, flag);
524
525 view_layer_dst->stats = nullptr;
526
527 /* Copy layer collections and object bases. */
528 /* Inline #BLI_duplicatelist and update the active base. */
529 view_layer_dst->object_bases.clear_no_delete();
530 BLI_assert_msg((view_layer_src->flag & VIEW_LAYER_OUT_OF_SYNC) == 0,
531 "View Layer Object Base out of sync, invoke BKE_view_layer_synced_ensure.");
532 for (const Base &base_src : view_layer_src->object_bases) {
533 Base *base_dst = MEM_dupalloc(&base_src);
534 BLI_addtail(&view_layer_dst->object_bases, base_dst);
535 if (view_layer_src->basact == &base_src) {
536 view_layer_dst->basact = base_dst;
537 }
538 }
539
540 view_layer_dst->active_collection = nullptr;
541 layer_collections_copy_data(view_layer_dst,
542 view_layer_src,
543 &view_layer_dst->layer_collections,
544 &view_layer_src->layer_collections);
545
546 LayerCollection *lc_scene_dst = view_layer_dst->layer_collections.first();
547 lc_scene_dst->collection = scene_dst->master_collection;
548
549 view_layer_dst->aovs.clear_no_delete();
551 view_layer_dst, view_layer_src, &view_layer_dst->aovs, &view_layer_src->aovs);
552
553 view_layer_dst->lightgroups.clear_no_delete();
555 view_layer_dst, view_layer_src, &view_layer_dst->lightgroups, &view_layer_src->lightgroups);
556
557 if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
558 id_us_plus(id_cast<ID *>(view_layer_dst->mat_override));
559 }
560}
561
562void BKE_view_layer_rename(Main *bmain, Scene *scene, ViewLayer *view_layer, const char *newname)
563{
564 char oldname[sizeof(view_layer->name)];
565
566 STRNCPY(oldname, view_layer->name);
567
568 STRNCPY_UTF8(view_layer->name, newname);
570 view_layer,
571 DATA_("ViewLayer"),
572 '.',
573 offsetof(ViewLayer, name),
574 sizeof(view_layer->name));
575
576 for (const SceneCompositorEffect &effect : scene->compositor_effects) {
577 if (!effect.node_group || ID_MISSING(effect.node_group)) {
578 continue;
579 }
580
581 int index = BLI_findindex(&scene->view_layers, view_layer);
582
583 for (bNode *node : effect.node_group->all_nodes()) {
584 if (node->type_legacy == CMP_NODE_R_LAYERS && node->id == nullptr) {
585 if (node->custom1 == index) {
586 STRNCPY_UTF8(node->name, view_layer->name);
587 }
588 }
589 }
590 }
591
592 /* Fix all the animation data and windows which may link to this. */
594 "view_layers",
595 RNA_path_name_to_infix(oldname),
596 RNA_path_name_to_infix(view_layer->name),
597 /*verify_paths=*/true,
598 *bmain);
599
600 /* WM can be missing on startup. */
601 wmWindowManager *wm = bmain->wm.first();
602 if (wm) {
603 for (wmWindow &win : wm->windows) {
604 if (win.scene == scene && STREQ(win.view_layer_name, oldname)) {
605 STRNCPY_UTF8(win.view_layer_name, view_layer->name);
606 }
607 }
608 }
609
610 /* Update any sequencer scene strips referencing this view layer by name. */
611 seq::relations_update_view_layer_scene_strips(bmain, scene, oldname, view_layer->name);
612
613 /* Dependency graph uses view layer name based lookups. */
615}
616
617/* LayerCollection */
618
622static LayerCollection *collection_from_index(ListBaseT<LayerCollection> *lb,
623 const int number,
624 int *i)
625{
626 for (LayerCollection &lc : *lb) {
627 if (*i == number) {
628 return &lc;
629 }
630
631 (*i)++;
632 }
633
634 for (LayerCollection &lc : *lb) {
635 LayerCollection *lc_nested = collection_from_index(&lc.layer_collections, number, i);
636 if (lc_nested) {
637 return lc_nested;
638 }
639 }
640 return nullptr;
641}
642
647{
648 if (lc->flag & LAYER_COLLECTION_EXCLUDE) {
649 return true;
650 }
651
652 /* Check visibility restriction flags */
654 return true;
655 }
656
657 /* Restriction flags stay set, so we need to check parents */
658 CollectionParent *parent = lc->collection->runtime->parents.first();
659
660 if (parent) {
662
663 return lc && layer_collection_hidden(view_layer, lc);
664 }
665
666 return false;
667}
668
670{
671 int i = 0;
672 return collection_from_index(&view_layer->layer_collections, index, &i);
673}
674
676{
677 return view_layer->active_collection;
678}
679
681{
682 if (lc->flag & LAYER_COLLECTION_EXCLUDE) {
683 return false;
684 }
685
686 view_layer->active_collection = lc;
687 return true;
688}
689
691{
692 CollectionParent *parent = lc->collection->runtime->parents.first();
693
694 if (parent) {
696 }
697 else {
698 lc = nullptr;
699 }
700
701 /* Don't activate excluded or hidden collections to prevent creating objects in a hidden
702 * collection from the UI */
703 if (lc && layer_collection_hidden(view_layer, lc)) {
704 return BKE_layer_collection_activate_parent(view_layer, lc);
705 }
706
707 if (!lc) {
708 lc = view_layer->layer_collections.first();
709 }
710
711 view_layer->active_collection = lc;
712 return lc;
713}
714
716{
718 while (!ID_IS_EDITABLE(lc->collection)) {
719 LayerCollection *parent_lc = BKE_layer_collection_activate_parent(view_layer, lc);
720 if (parent_lc == lc) {
721 break;
722 }
723 lc = parent_lc;
724 }
725 return lc;
726}
727
731static int collection_count(const ListBaseT<LayerCollection> *lb)
732{
733 int i = 0;
734 for (const LayerCollection &lc : *lb) {
735 i += collection_count(&lc.layer_collections) + 1;
736 }
737 return i;
738}
739
741{
742 return collection_count(&view_layer->layer_collections);
743}
744
748static int index_from_collection(ListBaseT<LayerCollection> *lb, const LayerCollection *lc, int *i)
749{
750 for (LayerCollection &lcol : *lb) {
751 if (&lcol == lc) {
752 return *i;
753 }
754
755 (*i)++;
756 }
757
758 for (LayerCollection &lcol : *lb) {
759 int i_nested = index_from_collection(&lcol.layer_collections, lc, i);
760 if (i_nested != -1) {
761 return i_nested;
762 }
763 }
764 return -1;
765}
766
768{
769 int i = 0;
770 return index_from_collection(&view_layer->layer_collections, lc, &i);
771}
772
774
775/* -------------------------------------------------------------------- */
810
811/* Maximum allowed levels of re-entrant calls to #BKE_layer_collection_resync_forbid. */
812[[maybe_unused]] static constexpr int no_resync_recurse_max = 16 * 256;
813
815{
816 BLI_assert(bmain.no_resync >= 0);
818 bmain.no_resync++;
819}
820
822{
823 BLI_assert(bmain.no_resync > 0);
825 bmain.no_resync--;
826}
827
830
831 /* Temp data used to generate a queue during valid layer search. See
832 * #layer_collection_resync_find. */
834
835 /* LayerCollection and Collection wrapped by this data. */
838
839 /* Hierarchical relationships in the old, existing ViewLayer state (except for newly created
840 * layers). */
842 ListBaseT<LayerCollectionResync> children_layer_resync;
843
844 /* This layer still points to a valid collection. */
846 /* This layer is still valid as a parent, i.e. at least one of its original layer children is
847 * usable and matches one of its current children collections. */
849 /* This layer is still valid as a child, i.e. its original layer parent is usable and matches one
850 * of its current parents collections. */
852 /* This layer is still fully valid in the new collection hierarchy, i.e. itself and all of its
853 * parents fully match the current collection hierarchy.
854 * OR
855 * This layer has already been re-used to match the new collections hierarchy. */
857};
858
860 LayerCollectionResync *parent_layer_resync, LayerCollection *layer, BLI_mempool *mempool)
861{
862 LayerCollectionResync *layer_resync = static_cast<LayerCollectionResync *>(
863 BLI_mempool_calloc(mempool));
864
865 layer_resync->layer = layer;
866 layer_resync->collection = layer->collection;
867 layer_resync->parent_layer_resync = parent_layer_resync;
868 if (parent_layer_resync != nullptr) {
869 BLI_addtail(&parent_layer_resync->children_layer_resync, layer_resync);
870 }
871
872 layer_resync->is_usable = (layer->collection != nullptr);
873 layer_resync->is_valid_as_child =
874 layer_resync->is_usable && (parent_layer_resync == nullptr ||
875 (parent_layer_resync->is_usable &&
876 BLI_findptr(&parent_layer_resync->layer->collection->children,
877 layer->collection,
878 offsetof(CollectionChild, collection)) != nullptr));
879 if (layer_resync->is_valid_as_child) {
880 layer_resync->is_used = parent_layer_resync != nullptr ? parent_layer_resync->is_used : true;
881 }
882 else {
883 layer_resync->is_used = false;
884 }
885
886 if (layer->layer_collections.is_empty()) {
887 layer_resync->is_valid_as_parent = layer_resync->is_usable;
888 }
889 else {
890 for (LayerCollection &child_layer : layer->layer_collections) {
892 layer_resync, &child_layer, mempool);
893 if (layer_resync->is_usable && child_layer_resync->is_valid_as_child) {
894 layer_resync->is_valid_as_parent = true;
895 }
896 }
897 }
898
900 "Old LayerCollection for %s is...\n\tusable: %d\n\tvalid parent: %d\n\tvalid child: "
901 "%d\n\tused: %d\n",
902 layer_resync->collection ? layer_resync->collection->id.name : "<NONE>",
903 layer_resync->is_usable,
904 layer_resync->is_valid_as_parent,
905 layer_resync->is_valid_as_child,
906 layer_resync->is_used);
907
908 return layer_resync;
909}
910
912 Collection *child_collection)
913{
914 /* Given the given parent, valid layer collection, find in the old hierarchy the best possible
915 * unused layer matching the given child collection.
916 *
917 * This uses the following heuristics:
918 * - Prefer a layer descendant of the given parent one if possible.
919 * - Prefer a layer as closely related as possible from the given parent.
920 * - Do not used layers that are not head (highest possible ancestor) of a local valid hierarchy
921 * branch, since we can assume we could then re-use its ancestor instead.
922 *
923 * A queue is used to ensure this order of preferences.
924 */
925
926 BLI_assert(layer_resync->collection != child_collection);
927 BLI_assert(child_collection != nullptr);
928
929 LayerCollectionResync *current_layer_resync = nullptr;
930 LayerCollectionResync *root_layer_resync = layer_resync;
931
932 LayerCollectionResync *queue_head = layer_resync, *queue_tail = layer_resync;
933 layer_resync->queue_next = nullptr;
934
935 while (queue_head != nullptr) {
936 current_layer_resync = queue_head;
937 queue_head = current_layer_resync->queue_next;
938
939 if (current_layer_resync->collection == child_collection &&
940 (current_layer_resync->parent_layer_resync == layer_resync ||
941 (!current_layer_resync->is_used && !current_layer_resync->is_valid_as_child)))
942 {
943 /* This layer is a valid candidate, because its collection matches the seeked one, AND:
944 * - It is a direct child of the initial given parent ('unchanged hierarchy' case), OR
945 * - It is not currently used, and not part of a valid hierarchy (sub-)chain.
946 */
947 break;
948 }
949
950 /* Else, add all its direct children for further searching. */
951 for (LayerCollectionResync &child_layer_resync : current_layer_resync->children_layer_resync) {
952 /* Add to tail of the queue. */
953 queue_tail->queue_next = &child_layer_resync;
954 child_layer_resync.queue_next = nullptr;
955 queue_tail = &child_layer_resync;
956 if (queue_head == nullptr) {
957 queue_head = queue_tail;
958 }
959 }
960
961 /* If all descendants from current layer have been processed, go one step higher and
962 * process all of its other siblings. */
963 if (queue_head == nullptr && root_layer_resync->parent_layer_resync != nullptr) {
964 for (LayerCollectionResync &sibling_layer_resync :
965 root_layer_resync->parent_layer_resync->children_layer_resync)
966 {
967 if (&sibling_layer_resync == root_layer_resync) {
968 continue;
969 }
970 /* Add to tail of the queue. */
971 queue_tail->queue_next = &sibling_layer_resync;
972 sibling_layer_resync.queue_next = nullptr;
973 queue_tail = &sibling_layer_resync;
974 if (queue_head == nullptr) {
975 queue_head = queue_tail;
976 }
977 }
978 root_layer_resync = root_layer_resync->parent_layer_resync;
979 }
980
981 current_layer_resync = nullptr;
982 }
983
984 return current_layer_resync;
985}
986
988 LayerCollectionResync *layer_resync)
989{
990 for (LayerCollectionResync &child_layer_resync : layer_resync->children_layer_resync) {
991 layer_collection_resync_unused_layers_free(view_layer, &child_layer_resync);
992 }
993
994 if (!layer_resync->is_used) {
996 "Freeing unused LayerCollection for %s",
997 layer_resync->collection != nullptr ? layer_resync->collection->id.name :
998 "<Deleted Collection>");
999
1000 if (layer_resync->layer == view_layer->active_collection) {
1001 view_layer->active_collection = nullptr;
1002 }
1003
1004 /* We do not want to go recursive here, this is handled through the LayerCollectionResync data
1005 * wrapper. */
1006 MEM_delete(layer_resync->layer);
1007 layer_resync->layer = nullptr;
1008 layer_resync->collection = nullptr;
1009 layer_resync->is_usable = false;
1010 }
1011}
1012
1014{
1015 view_layer->flag |= VIEW_LAYER_OUT_OF_SYNC;
1016}
1017
1019{
1020 return (view_layer.flag & VIEW_LAYER_OUT_OF_SYNC) == 0;
1021}
1022
1024 const Scene *scene,
1025 ViewLayer *view_layer)
1026{
1027 if (bmain) {
1028 BKE_view_layer_synced_ensure(*bmain, scene, view_layer);
1029 }
1030 else {
1032 }
1033}
1034
1035bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
1036{
1037 BLI_assert(scene);
1038 BLI_assert(view_layer);
1039
1040 bool is_all_resynced = true;
1041 if (view_layer->flag & VIEW_LAYER_OUT_OF_SYNC) {
1042 if (BKE_layer_collection_sync(bmain, scene, view_layer)) {
1043 view_layer->flag &= ~VIEW_LAYER_OUT_OF_SYNC;
1044 }
1045 else {
1046 is_all_resynced = false;
1047 }
1048 }
1049
1050 return is_all_resynced;
1051}
1052
1053bool BKE_scene_view_layers_synced_ensure(const Main &bmain, const Scene *scene)
1054{
1055 bool is_all_resynced = true;
1056 for (ViewLayer &view_layer : scene->view_layers) {
1057 if (!BKE_view_layer_synced_ensure(bmain, scene, &view_layer)) {
1058 is_all_resynced = false;
1059 }
1060 }
1061 return is_all_resynced;
1062}
1063
1065{
1066 bool is_all_resynced = true;
1067 for (const Scene *scene = bmain->scenes.first(); scene;
1068 scene = static_cast<const Scene *>(scene->id.next))
1069 {
1070 if (!BKE_scene_view_layers_synced_ensure(*bmain, scene)) {
1071 is_all_resynced = false;
1072 }
1073 }
1074
1075 /* NOTE: This is not (yet?) covered by the dirty tag and deferred re-sync system. */
1077 is_all_resynced = false;
1078 }
1079
1080 return is_all_resynced;
1081}
1082
1085 ListBaseT<Base> *r_lb_new_object_bases,
1086 const short collection_restrict,
1087 const short layer_restrict,
1088 const ushort local_collections_bits)
1089{
1090 /* No need to sync objects if the collection is excluded. */
1091 if ((layer->flag & LAYER_COLLECTION_EXCLUDE) != 0) {
1092 return;
1093 }
1094
1095 for (CollectionObject &cob : layer->collection->gobject) {
1096 if (cob.ob == nullptr) {
1097 continue;
1098 }
1099
1100 /* Tag linked object as a weak reference so we keep the object
1101 * base pointer on file load and remember hidden state. */
1103
1104 Base *base = view_layer->runtime->object_bases_hash->add_or_modify(
1105 cob.ob,
1106 [&](Base **base_p) {
1107 /* Create new base. */
1108 Base *base = object_base_new(cob.ob);
1109 base->local_collections_bits = local_collections_bits;
1110 *base_p = base;
1111 BLI_addtail(r_lb_new_object_bases, base);
1112 return base;
1113 },
1114 [&](Base **base_p) {
1115 /* Move from old base list to new base list. Base might have already
1116 * been moved to the new base list and the first/last test ensure that
1117 * case also works. */
1118 Base *base = *base_p;
1119 if (!ELEM(base, r_lb_new_object_bases->first(), r_lb_new_object_bases->last())) {
1120 BLI_remlink(&view_layer->object_bases, base);
1121 BLI_addtail(r_lb_new_object_bases, base);
1122 }
1123 return base;
1124 });
1125
1126 if ((collection_restrict & COLLECTION_HIDE_VIEWPORT) == 0) {
1127 base->flag_from_collection |= (BASE_ENABLED_VIEWPORT |
1129 if ((layer_restrict & LAYER_COLLECTION_HIDE) == 0) {
1130 base->flag_from_collection |= BASE_ENABLED_AND_VISIBLE_IN_DEFAULT_VIEWPORT;
1131 }
1132 if ((collection_restrict & COLLECTION_HIDE_SELECT) == 0) {
1133 base->flag_from_collection |= BASE_SELECTABLE;
1134 }
1135 }
1136
1137 if ((collection_restrict & COLLECTION_HIDE_RENDER) == 0) {
1138 base->flag_from_collection |= BASE_ENABLED_RENDER;
1139 }
1140
1141 /* Holdout and indirect only */
1142 if (layer->flag & LAYER_COLLECTION_HOLDOUT) {
1143 base->flag_from_collection |= BASE_HOLDOUT;
1144 }
1146 base->flag_from_collection |= BASE_INDIRECT_ONLY;
1147 }
1148
1149 layer->runtime_flag |= LAYER_COLLECTION_HAS_OBJECTS;
1150 }
1151}
1152
1153static void layer_collection_sync(ViewLayer *view_layer,
1154 LayerCollectionResync *layer_resync,
1155 BLI_mempool *layer_resync_mempool,
1156 ListBaseT<Base> *r_lb_new_object_bases,
1157 const eLayerCollection_Flag parent_layer_flag,
1158 const short parent_collection_restrict,
1159 const short parent_layer_restrict,
1160 const ushort parent_local_collections_bits)
1161{
1162 /* This function assumes current 'parent' layer collection is already fully (re)synced and valid
1163 * regarding current Collection hierarchy.
1164 *
1165 * It will process all the children collections of the collection from the given 'parent' layer,
1166 * re-use or create layer collections for each of them, and ensure orders also match.
1167 *
1168 * Then it will ensure that the objects owned by the given parent collection have a proper base.
1169 *
1170 * NOTE: This process is recursive.
1171 */
1172
1173 /* Temporary storage for all valid (new or reused) children layers. */
1174 ListBaseT<LayerCollection> new_lb_layer = {nullptr, nullptr};
1175
1176 BLI_assert(layer_resync->is_used);
1177
1178 uint64_t skipped_children = 0;
1179 for (CollectionChild &child : layer_resync->collection->children) {
1180 Collection *child_collection = child.collection;
1181 /* Collection relations may not have rebuild yet. */
1182 if (child_collection == nullptr) {
1183 skipped_children++;
1184 continue;
1185 }
1186 LayerCollectionResync *child_layer_resync = layer_collection_resync_find(layer_resync,
1187 child_collection);
1188
1189 if (child_layer_resync != nullptr) {
1190 BLI_assert(child_layer_resync->collection != nullptr);
1191 BLI_assert(child_layer_resync->layer != nullptr);
1192 BLI_assert(child_layer_resync->is_usable);
1193
1194 if (child_layer_resync->is_used) {
1195 CLOG_DEBUG(&LOG,
1196 "Found same existing LayerCollection for %s as child of %s",
1197 child_collection->id.name,
1198 layer_resync->collection->id.name);
1199 }
1200 else {
1201 CLOG_DEBUG(&LOG,
1202 "Found a valid unused LayerCollection for %s as child of %s, re-using it",
1203 child_collection->id.name,
1204 layer_resync->collection->id.name);
1205 }
1206
1207 child_layer_resync->is_used = true;
1208
1209 /* NOTE: Do not move the resync wrapper to match the new layer hierarchy, so that the old
1210 * parenting info remains available. In case a search for a valid layer in the children of
1211 * the current is required again, the old parenting hierarchy is needed as reference, not the
1212 * new one.
1213 */
1215 child_layer_resync->layer);
1216 BLI_addtail(&new_lb_layer, child_layer_resync->layer);
1217 }
1218 else {
1219 CLOG_DEBUG(&LOG,
1220 "No available LayerCollection for %s as child of %s, creating a new one",
1221 child_collection->id.name,
1222 layer_resync->collection->id.name);
1223
1224 LayerCollection *child_layer = layer_collection_add(&new_lb_layer, child_collection);
1225 child_layer->flag = parent_layer_flag;
1226
1227 child_layer_resync = static_cast<LayerCollectionResync *>(
1228 BLI_mempool_calloc(layer_resync_mempool));
1229 child_layer_resync->collection = child_collection;
1230 child_layer_resync->layer = child_layer;
1231 child_layer_resync->is_usable = true;
1232 child_layer_resync->is_used = true;
1233 child_layer_resync->is_valid_as_child = true;
1234 child_layer_resync->is_valid_as_parent = true;
1235 /* NOTE: Needs to be added to the layer_resync hierarchy so that the resync wrapper gets
1236 * freed at the end. */
1237 child_layer_resync->parent_layer_resync = layer_resync;
1238 BLI_addtail(&layer_resync->children_layer_resync, child_layer_resync);
1239 }
1240
1241 LayerCollection *child_layer = child_layer_resync->layer;
1242
1243 const ushort child_local_collections_bits = parent_local_collections_bits &
1244 child_layer->local_collections_bits;
1245
1246 /* Tag linked collection as a weak reference so we keep the layer
1247 * collection pointer on file load and remember exclude state. */
1248 id_lib_indirect_weak_link(&child_collection->id);
1249
1250 /* Collection restrict is inherited. */
1251 short child_collection_restrict = parent_collection_restrict;
1252 short child_layer_restrict = parent_layer_restrict;
1253 if (!(child_collection->flag & COLLECTION_IS_MASTER)) {
1254 child_collection_restrict |= child_collection->flag;
1255 child_layer_restrict |= child_layer->flag;
1256 }
1257
1258 /* Sync child collections. */
1259 layer_collection_sync(view_layer,
1260 child_layer_resync,
1261 layer_resync_mempool,
1262 r_lb_new_object_bases,
1263 child_layer->flag,
1264 child_collection_restrict,
1265 child_layer_restrict,
1266 child_local_collections_bits);
1267
1268 /* Layer collection exclude is not inherited. */
1269 child_layer->runtime_flag = {};
1270 if (child_layer->flag & LAYER_COLLECTION_EXCLUDE) {
1271 continue;
1272 }
1273
1274 /* We separate restrict viewport and visible view layer because a layer collection can be
1275 * hidden in the view layer yet (locally) visible in a viewport (if it is not restricted). */
1276 if (child_collection_restrict & COLLECTION_HIDE_VIEWPORT) {
1278 }
1279
1280 if (((child_layer->runtime_flag & LAYER_COLLECTION_HIDE_VIEWPORT) == 0) &&
1281 ((child_layer_restrict & LAYER_COLLECTION_HIDE) == 0))
1282 {
1284 }
1285
1286 if (!child_collection->exporters.is_empty() &&
1287 !(ID_IS_LINKED(&child_collection->id) || ID_IS_OVERRIDE_LIBRARY(&child_collection->id)))
1288 {
1290 }
1291 }
1292
1293 /* Replace layer collection list with new one. */
1294 layer_resync->layer->layer_collections = new_lb_layer;
1295 BLI_assert(layer_resync->collection->children.count() - skipped_children ==
1296 new_lb_layer.count());
1297 UNUSED_VARS_NDEBUG(skipped_children);
1298
1299 /* Update bases etc. for objects. */
1301 layer_resync->layer,
1302 r_lb_new_object_bases,
1303 parent_collection_restrict,
1304 parent_layer_restrict,
1305 parent_local_collections_bits);
1306}
1307
1308#ifndef NDEBUG
1310{
1311 bool is_valid = true;
1312
1313 if (layer == nullptr) {
1314 layer = view_layer->layer_collections.first();
1315 }
1316
1317 /* Only check for a collection's objects if its layer is not excluded. */
1318 if ((layer->flag & LAYER_COLLECTION_EXCLUDE) == 0) {
1319 for (CollectionObject &cob : layer->collection->gobject) {
1320 if (cob.ob == nullptr) {
1321 continue;
1322 }
1323 if (!view_layer->runtime->object_bases_hash->contains(cob.ob)) {
1324 CLOG_FATAL(
1325 &LOG,
1326 "Object '%s' from collection '%s' has no entry in view layer's object bases cache",
1327 cob.ob->id.name + 2,
1328 layer->collection->id.name + 2);
1329 is_valid = false;
1330 break;
1331 }
1332 }
1333 }
1334
1335 if (is_valid) {
1336 for (LayerCollection &layer_child : layer->layer_collections) {
1337 if (!view_layer_objects_base_cache_validate(view_layer, &layer_child)) {
1338 is_valid = false;
1339 break;
1340 }
1341 }
1342 }
1343
1344 return is_valid;
1345}
1346#else
1347static bool view_layer_objects_base_cache_validate(ViewLayer * /*view_layer*/,
1348 LayerCollection * /*layer*/)
1349{
1350 return true;
1351}
1352#endif
1353
1355{
1356 LayerCollection *first_layer_collection = view_layer->layer_collections.first();
1357 if (BLI_listbase_count_at_most(&view_layer->layer_collections, 2) > 1 ||
1358 first_layer_collection->collection != scene->master_collection)
1359 {
1360 /* In some cases (from older files) we do have a master collection, but no matching layer,
1361 * instead all the children of the master collection have their layer collections in the
1362 * viewlayer's list. This is not a valid situation, add a layer for the master collection and
1363 * add all existing first-level layers as children of that new master layer. */
1364 ListBaseT<LayerCollection> layer_collections = view_layer->layer_collections;
1365 view_layer->layer_collections.clear_no_delete();
1366 LayerCollection *master_layer_collection = layer_collection_add(&view_layer->layer_collections,
1367 scene->master_collection);
1368 master_layer_collection->layer_collections = layer_collections;
1369 }
1370}
1371
1372bool BKE_layer_collection_sync(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
1373{
1374 if (bmain.no_resync > 0) {
1375 return false;
1376 }
1377
1378 if (!scene->master_collection) {
1379 /* Happens for old files that don't have versioning applied yet. */
1380 return false;
1381 }
1382
1383 if (view_layer->layer_collections.is_empty()) {
1384 /* In some cases (from older files, or when creating a new ViewLayer from
1385 * #BKE_view_layer_add), we do have a master collection, yet no matching layer. Create the
1386 * master one here, so that the rest of the code can work as expected. */
1388 }
1389
1390#ifndef NDEBUG
1391 {
1392 BLI_assert_msg(view_layer->layer_collections.is_single(),
1393 "ViewLayer's first level of children layer collections should always have "
1394 "exactly one item");
1395
1396 LayerCollection *first_layer_collection = view_layer->layer_collections.first();
1397 BLI_assert_msg(first_layer_collection->collection == scene->master_collection,
1398 "ViewLayer's first layer collection should always be the one for the scene's "
1399 "master collection");
1400 }
1401#endif
1402
1403 /* Free cache. */
1405
1406 /* Create object to base hash if it does not exist yet. */
1407 if (!view_layer->runtime->object_bases_hash) {
1408 view_layer_bases_hash_create(view_layer, false);
1409 }
1410
1411 /* Clear visible and selectable flags to be reset. */
1412 for (Base &base : view_layer->object_bases) {
1415 }
1416
1417 /* Generate temporary data representing the old layers hierarchy, and how well it matches the
1418 * new collections hierarchy. */
1419 BLI_mempool *layer_resync_mempool = BLI_mempool_create(
1420 sizeof(LayerCollectionResync), 1024, 1024, BLI_MEMPOOL_NOP);
1422 nullptr, view_layer->layer_collections.first(), layer_resync_mempool);
1423
1424 /* Clear the cached flag indicating if the view layer has a collection exporter set. */
1426
1427 /* Generate new layer connections and object bases when collections changed. */
1428 ListBaseT<Base> new_object_bases{};
1429 const eLayerCollection_Flag parent_exclude{};
1430 const short parent_restrict = 0, parent_layer_restrict = 0;
1431 layer_collection_sync(view_layer,
1432 master_layer_resync,
1433 layer_resync_mempool,
1434 &new_object_bases,
1435 parent_exclude,
1436 parent_restrict,
1437 parent_layer_restrict,
1438 ~0);
1439
1440 layer_collection_resync_unused_layers_free(view_layer, master_layer_resync);
1441 BLI_mempool_destroy(layer_resync_mempool);
1442 master_layer_resync = nullptr;
1443
1444 /* Any remaining object bases are to be removed. */
1445 for (Base &base : view_layer->object_bases) {
1446 if (view_layer->basact == &base) {
1447 view_layer->basact = nullptr;
1448 }
1449
1450 if (base.object) {
1451 /* Those asserts are commented, since they are too expensive to perform even in debug, as
1452 * this layer resync function currently gets called way too often. */
1453#if 0
1454 BLI_assert(BLI_findindex(&new_object_bases, base) == -1);
1455 BLI_assert(BLI_findptr(&new_object_bases, base->object, offsetof(Base, object)) == nullptr);
1456#endif
1457 view_layer->runtime->object_bases_hash->remove(base.object);
1458 }
1459 }
1460
1461 view_layer->object_bases.free_no_destruct();
1462 view_layer->object_bases = new_object_bases;
1463
1464 view_layer_objects_base_cache_validate(view_layer, nullptr);
1465
1466 for (Base &base : view_layer->object_bases) {
1467 BKE_base_eval_flags(&base);
1468 }
1469
1470 /* Always set a valid active collection. */
1471 LayerCollection *active = view_layer->active_collection;
1472 if (active && layer_collection_hidden(view_layer, active)) {
1473 BKE_layer_collection_activate_parent(view_layer, active);
1474 }
1475 else if (active == nullptr) {
1476 view_layer->active_collection = view_layer->layer_collections.first();
1477 }
1478
1479 return true;
1480}
1481
1482bool BKE_scene_collection_sync(const Main &bmain, const Scene *scene)
1483{
1484 if (bmain.no_resync > 0) {
1485 return false;
1486 }
1487
1488 for (ViewLayer &view_layer : scene->view_layers) {
1489 BKE_view_layer_need_resync_tag(&view_layer);
1490 }
1491
1492 return true;
1493}
1494
1496{
1497 if (bmain->no_resync > 0) {
1498 return false;
1499 }
1500
1501 /* TODO: if a single collection changed, figure out which
1502 * scenes it belongs to and only update those. */
1503
1504 /* TODO: optimize for file load so only linked collections get checked? */
1505
1506 bool is_all_resynced = true;
1507 for (const Scene *scene = bmain->scenes.first(); scene;
1508 scene = static_cast<const Scene *>(scene->id.next))
1509 {
1510 if (!BKE_scene_collection_sync(*bmain, scene)) {
1511 is_all_resynced = false;
1512 }
1513 }
1514
1516 is_all_resynced = false;
1517 }
1518 return is_all_resynced;
1519}
1520
1522{
1523 if (bmain->no_resync > 0) {
1524 return false;
1525 }
1526
1527 /* On remapping of object or collection pointers free caches. */
1528 /* TODO: try to make this faster */
1529
1531
1532 for (Scene *scene = bmain->scenes.first(); scene; scene = static_cast<Scene *>(scene->id.next)) {
1533 for (ViewLayer &view_layer : scene->view_layers) {
1535
1536 MEM_SAFE_DELETE(view_layer.runtime->object_bases_hash);
1537
1538 /* Directly re-create the mapping here, so that we can also deal with duplicates in
1539 * `view_layer->object_bases` list of bases properly. This is the only place where such
1540 * duplicates should be fixed, and not considered as a critical error. */
1541 view_layer_bases_hash_create(&view_layer, true);
1542 }
1543
1545 const_cast<Main *>(bmain), &scene->master_collection->id, ID_RECALC_SYNC_TO_EVAL);
1546 DEG_id_tag_update_ex(const_cast<Main *>(bmain), &scene->id, ID_RECALC_SYNC_TO_EVAL);
1547 }
1548
1549 for (Collection *collection = bmain->collections.first(); collection;
1550 collection = static_cast<Collection *>(collection->id.next))
1551 {
1552 DEG_id_tag_update_ex(const_cast<Main *>(bmain), &collection->id, ID_RECALC_SYNC_TO_EVAL);
1553 }
1554
1555 return BKE_main_collection_sync(bmain);
1556}
1557
1559
1560/* -------------------------------------------------------------------- */
1563
1565 const Scene *scene,
1566 ViewLayer *view_layer,
1567 LayerCollection *lc,
1568 bool deselect)
1569{
1571 return false;
1572 }
1573
1574 bool changed = false;
1575
1576 if (!(lc->flag & LAYER_COLLECTION_EXCLUDE)) {
1577 BKE_view_layer_synced_ensure(bmain, scene, view_layer);
1578 for (CollectionObject &cob : lc->collection->gobject) {
1579 Base *base = BKE_view_layer_base_find(view_layer, cob.ob);
1580
1581 if (base) {
1582 if (deselect) {
1583 if (base->flag & BASE_SELECTED) {
1584 base->flag &= ~BASE_SELECTED;
1585 changed = true;
1586 }
1587 }
1588 else {
1589 if ((base->flag & BASE_SELECTABLE) && !(base->flag & BASE_SELECTED)) {
1590 base->flag |= BASE_SELECTED;
1591 changed = true;
1592 }
1593 }
1594 }
1595 }
1596 }
1597
1598 for (LayerCollection &iter : lc->layer_collections) {
1599 changed |= BKE_layer_collection_objects_select(bmain, scene, view_layer, &iter, deselect);
1600 }
1601
1602 return changed;
1603}
1604
1606 const Scene *scene,
1607 ViewLayer *view_layer,
1608 LayerCollection *lc)
1609{
1611 return false;
1612 }
1613
1614 if (!(lc->flag & LAYER_COLLECTION_EXCLUDE)) {
1615 BKE_view_layer_synced_ensure(bmain, scene, view_layer);
1616 for (CollectionObject &cob : lc->collection->gobject) {
1617 Base *base = BKE_view_layer_base_find(view_layer, cob.ob);
1618
1619 if (base && (base->flag & BASE_SELECTED) &&
1621 {
1622 return true;
1623 }
1624 }
1625 }
1626
1627 for (LayerCollection &iter : lc->layer_collections) {
1628 if (BKE_layer_collection_has_selected_objects(bmain, scene, view_layer, &iter)) {
1629 return true;
1630 }
1631 }
1632
1633 return false;
1634}
1635
1637 LayerCollection *lc_child)
1638{
1639 if (lc_parent == lc_child) {
1640 return true;
1641 }
1642
1643 for (LayerCollection &lc_iter : lc_parent->layer_collections) {
1644 if (BKE_layer_collection_has_layer_collection(&lc_iter, lc_child)) {
1645 return true;
1646 }
1647 }
1648 return false;
1649}
1650
1652
1653/* -------------------------------------------------------------------- */
1656
1658 const Main &bmain, Scene *scene, ViewLayer *view_layer, Base *base, bool extend)
1659{
1660 if (!extend) {
1661 /* Make only one base visible. */
1662 BKE_view_layer_synced_ensure(bmain, scene, view_layer);
1663 for (Base &other : *BKE_view_layer_object_bases_get(view_layer)) {
1664 other.flag |= BASE_HIDDEN;
1665 }
1666
1667 base->flag &= ~BASE_HIDDEN;
1668 }
1669 else {
1670 /* Toggle visibility of one base. */
1671 base->flag ^= BASE_HIDDEN;
1672 }
1673
1675}
1676
1677bool BKE_base_is_visible(const View3D *v3d, const Base *base)
1678{
1680 return false;
1681 }
1682
1683 if (v3d == nullptr) {
1685 }
1686
1687 if ((v3d->localvd) && ((v3d->local_view_uid & base->local_view_bits) == 0)) {
1688 return false;
1689 }
1690
1691 if (((1 << (base->object->type)) & v3d->object_type_exclude_viewport) != 0) {
1692 return false;
1693 }
1694
1695 if (v3d->flag & V3D_LOCAL_COLLECTIONS) {
1696 return (v3d->local_collections_uid & base->local_collections_bits) != 0;
1697 }
1698
1700}
1701
1703 const Object *ob,
1704 const ObjectType ob_type)
1705{
1706 BLI_assert(v3d != nullptr);
1707
1709 return false;
1710 }
1711
1712 if ((v3d->object_type_exclude_viewport & (1 << ob_type)) != 0) {
1713 return false;
1714 }
1715
1716 if (v3d->localvd && ((v3d->local_view_uid & ob->base_local_view_bits) == 0)) {
1717 return false;
1718 }
1719
1720 if ((v3d->flag & V3D_LOCAL_COLLECTIONS) &&
1722 {
1723 return false;
1724 }
1725
1726 /* If not using local collection the object may still be in a hidden collection. */
1727 if ((v3d->flag & V3D_LOCAL_COLLECTIONS) == 0) {
1729 }
1730
1731 return true;
1732}
1733
1735{
1736 return BKE_object_is_visible_in_viewport_ex(v3d, ob, ob->type);
1737}
1738
1740
1741/* -------------------------------------------------------------------- */
1744
1746 const eLayerCollection_Flag flag)
1747{
1748 lc->flag |= flag;
1749 for (LayerCollection &lc_iter : lc->layer_collections) {
1751 }
1752}
1753
1755 const eLayerCollection_Flag flag)
1756{
1757 lc->flag &= ~flag;
1758 for (LayerCollection &lc_iter : lc->layer_collections) {
1760 }
1761}
1762
1764 ViewLayer *view_layer,
1765 LayerCollection *lc,
1766 bool extend)
1767{
1768 LayerCollection *lc_master = view_layer->layer_collections.first();
1769 bool hide_it = extend && (lc->runtime_flag & LAYER_COLLECTION_VISIBLE_VIEW_LAYER);
1770
1771 if (!extend) {
1772 /* Hide all collections. */
1773 for (LayerCollection &lc_iter : lc_master->layer_collections) {
1775 }
1776 }
1777
1778 /* Make all the direct parents visible. */
1779 if (hide_it) {
1781 }
1782 else {
1783 LayerCollection *lc_parent = lc;
1784 for (LayerCollection &lc_iter : lc_master->layer_collections) {
1786 lc_parent = &lc_iter;
1787 break;
1788 }
1789 }
1790
1791 while (lc_parent != lc) {
1792 lc_parent->flag &= ~LAYER_COLLECTION_HIDE;
1793
1794 for (LayerCollection &lc_iter : lc_parent->layer_collections) {
1796 lc_parent = &lc_iter;
1797 break;
1798 }
1799 }
1800 }
1801
1802 /* Make all the children visible, but respect their disable state. */
1804
1805 BKE_layer_collection_activate(view_layer, lc);
1806 }
1807
1809}
1810
1812 const int local_collections_uid)
1813{
1814 layer_collection->local_collections_bits |= local_collections_uid;
1815 for (LayerCollection &child : layer_collection->layer_collections) {
1816 layer_collection_local_visibility_set_recursive(&child, local_collections_uid);
1817 }
1818}
1819
1821 const int local_collections_uid)
1822{
1823 layer_collection->local_collections_bits &= ~local_collections_uid;
1824 for (LayerCollection &child : layer_collection->layer_collections) {
1825 layer_collection_local_visibility_unset_recursive(&child, local_collections_uid);
1826 }
1827}
1828
1829static void layer_collection_local_sync(const Main &bmain,
1830 const Scene *scene,
1831 ViewLayer *view_layer,
1832 LayerCollection *layer_collection,
1833 const ushort local_collections_uid,
1834 bool visible)
1835{
1836 if ((layer_collection->local_collections_bits & local_collections_uid) == 0) {
1837 visible = false;
1838 }
1839
1840 if (visible) {
1841 for (CollectionObject &cob : layer_collection->collection->gobject) {
1842 if (cob.ob == nullptr) {
1843 continue;
1844 }
1845
1846 BKE_view_layer_synced_ensure(bmain, scene, view_layer);
1847 Base *base = BKE_view_layer_base_find(view_layer, cob.ob);
1848 base->local_collections_bits |= local_collections_uid;
1849 }
1850 }
1851
1852 for (LayerCollection &child : layer_collection->layer_collections) {
1853 if ((child.flag & LAYER_COLLECTION_EXCLUDE) == 0) {
1855 bmain, scene, view_layer, &child, local_collections_uid, visible);
1856 }
1857 }
1858}
1859
1861 const Scene *scene,
1862 ViewLayer *view_layer,
1863 const View3D *v3d)
1864{
1865 if (bmain.no_resync > 0) {
1866 return false;
1867 }
1868
1869 const ushort local_collections_uid = v3d->local_collections_uid;
1870
1871 /* Reset flags and set the bases visible by default. */
1872 BKE_view_layer_synced_ensure(bmain, scene, view_layer);
1873 for (Base &base : *BKE_view_layer_object_bases_get(view_layer)) {
1874 base.local_collections_bits &= ~local_collections_uid;
1875 }
1876
1877 for (LayerCollection &layer_collection : view_layer->layer_collections) {
1879 bmain, scene, view_layer, &layer_collection, local_collections_uid, true);
1880 }
1881
1882 return true;
1883}
1884
1886{
1887 if (bmain->no_resync > 0) {
1888 return false;
1889 }
1890
1891 bool is_all_resynced = true;
1892 for (Scene &scene : bmain->scenes) {
1893 for (ViewLayer &view_layer : scene.view_layers) {
1894 for (bScreen &screen : bmain->screens) {
1895 for (ScrArea &area : screen.areabase) {
1896 if (area.spacetype != SPACE_VIEW3D) {
1897 continue;
1898 }
1899 View3D *v3d = area.spacedata.first_as<View3D>();
1900 if (v3d->flag & V3D_LOCAL_COLLECTIONS) {
1901 if (!BKE_layer_collection_local_sync(*bmain, &scene, &view_layer, v3d)) {
1902 is_all_resynced = false;
1903 }
1904 }
1905 }
1906 }
1907 }
1908 }
1909
1910 return is_all_resynced;
1911}
1912
1914 const Scene *scene,
1915 ViewLayer *view_layer,
1916 const View3D *v3d,
1917 LayerCollection *lc,
1918 bool extend)
1919{
1920 LayerCollection *lc_master = view_layer->layer_collections.first();
1921 bool hide_it = extend && ((v3d->local_collections_uid & lc->local_collections_bits) != 0);
1922
1923 if (!extend) {
1924 /* Hide all collections. */
1925 for (LayerCollection &lc_iter : lc_master->layer_collections) {
1927 }
1928 }
1929
1930 /* Make all the direct parents visible. */
1931 if (hide_it) {
1933 }
1934 else {
1935 LayerCollection *lc_parent = lc;
1936 for (LayerCollection &lc_iter : lc_master->layer_collections) {
1938 lc_parent = &lc_iter;
1939 break;
1940 }
1941 }
1942
1943 while (lc_parent != lc) {
1945
1946 for (LayerCollection &lc_iter : lc_parent->layer_collections) {
1948 lc_parent = &lc_iter;
1949 break;
1950 }
1951 }
1952 }
1953
1954 /* Make all the children visible. */
1956 }
1957
1958 BKE_layer_collection_local_sync(bmain, scene, view_layer, v3d);
1959}
1960
1962 const Scene *scene,
1963 ViewLayer *view_layer,
1964 LayerCollection *lc)
1965{
1966 if ((lc->flag & LAYER_COLLECTION_EXCLUDE) == 0) {
1967 BKE_view_layer_synced_ensure(bmain, scene, view_layer);
1968 for (CollectionObject &cob : lc->collection->gobject) {
1969 Base *base = BKE_view_layer_base_find(view_layer, cob.ob);
1970 base->flag &= ~BASE_HIDDEN;
1971 }
1972 }
1973 for (LayerCollection &lc_iter : lc->layer_collections) {
1974 layer_collection_bases_show_recursive(bmain, scene, view_layer, &lc_iter);
1975 }
1976}
1977
1979 const Scene *scene,
1980 ViewLayer *view_layer,
1981 LayerCollection *lc)
1982{
1983 if ((lc->flag & LAYER_COLLECTION_EXCLUDE) == 0) {
1984 BKE_view_layer_synced_ensure(bmain, scene, view_layer);
1985 for (CollectionObject &cob : lc->collection->gobject) {
1986 Base *base = BKE_view_layer_base_find(view_layer, cob.ob);
1987 base->flag |= BASE_HIDDEN;
1988 }
1989 }
1990 for (LayerCollection &lc_iter : lc->layer_collections) {
1991 layer_collection_bases_hide_recursive(bmain, scene, view_layer, &lc_iter);
1992 }
1993}
1994
1996 const Scene *scene,
1997 ViewLayer *view_layer,
1998 LayerCollection *lc,
1999 const bool visible,
2000 const bool hierarchy)
2001{
2002 if (hierarchy) {
2003 if (visible) {
2005 layer_collection_bases_show_recursive(bmain, scene, view_layer, lc);
2006 }
2007 else {
2009 layer_collection_bases_hide_recursive(bmain, scene, view_layer, lc);
2010 }
2011 }
2012 else {
2013 if (visible) {
2015 }
2016 else {
2018 }
2019 }
2020}
2021
2027 const eLayerCollection_Flag flag,
2028 const bool value,
2029 const bool restore_flag)
2030{
2031 if (flag == LAYER_COLLECTION_EXCLUDE) {
2032 /* For exclude flag, we remember the state the children had before
2033 * excluding and restoring it when enabling the parent collection again. */
2034 if (value) {
2035 if (restore_flag) {
2038 }
2039 else {
2041 }
2042
2043 lc->flag |= flag;
2044 }
2045 else {
2047 lc->flag &= ~flag;
2048 }
2049 }
2050 }
2051 else {
2052 SET_FLAG_FROM_TEST(lc->flag, value, flag);
2053 }
2054
2055 for (LayerCollection &nlc : lc->layer_collections) {
2056 layer_collection_flag_recursive_set(&nlc, flag, value, true);
2057 }
2058}
2059
2061 const eLayerCollection_Flag flag,
2062 const bool value)
2063{
2064 layer_collection_flag_recursive_set(lc, flag, value, false);
2065}
2066
2067/* ---------------------------------------------------------------------- */
2068
2070 const Collection *collection)
2071{
2072 if (lc->collection == collection) {
2073 return lc;
2074 }
2075
2076 for (LayerCollection &nlc : lc->layer_collections) {
2078 if (found) {
2079 return found;
2080 }
2081 }
2082 return nullptr;
2083}
2084
2086 const Collection *collection)
2087{
2088 for (LayerCollection &layer_collection : view_layer->layer_collections) {
2090 collection);
2091 if (found != nullptr) {
2092 return found;
2093 }
2094 }
2095 return nullptr;
2096}
2097
2098bool BKE_view_layer_has_collection(const ViewLayer *view_layer, const Collection *collection)
2099{
2100 return BKE_layer_collection_first_from_scene_collection(view_layer, collection) != nullptr;
2101}
2102
2103bool BKE_scene_has_object(const Main &bmain, Scene *scene, Object *ob)
2104{
2105 for (ViewLayer &view_layer : scene->view_layers) {
2106 BKE_view_layer_synced_ensure(bmain, scene, &view_layer);
2107 Base *base = BKE_view_layer_base_find(&view_layer, ob);
2108 if (base) {
2109 return true;
2110 }
2111 }
2112 return false;
2113}
2114
2116
2117/* Iterators */
2118
2119/* -------------------------------------------------------------------- */
2122
2127
2128static bool object_bases_iterator_is_valid(const View3D *v3d, Base *base, const int flag)
2129{
2130 BLI_assert((v3d == nullptr) || (v3d->spacetype == SPACE_VIEW3D));
2131
2132 /* Any flag satisfies the condition. */
2133 if (flag == ~0) {
2134 return (base->flag != 0);
2135 }
2136
2137 /* Flags may be more than one flag, so we can't check != 0. */
2138 return BASE_VISIBLE(v3d, base) && ((base->flag & flag) == flag);
2139}
2140
2141static void object_bases_iterator_begin(BLI_Iterator *iter, void *data_in_v, const int flag)
2142{
2143 ObjectsVisibleIteratorData *data_in = static_cast<ObjectsVisibleIteratorData *>(data_in_v);
2144 ViewLayer *view_layer = data_in->view_layer;
2145 const View3D *v3d = data_in->v3d;
2146 Base *base = BKE_view_layer_object_bases_get(view_layer)->first();
2147
2148 /* when there are no objects */
2149 if (base == nullptr) {
2150 iter->data = nullptr;
2151 iter->valid = false;
2152 return;
2153 }
2154
2156 iter->data = data;
2157
2158 data->v3d = v3d;
2159 data->base = base;
2160
2161 if (object_bases_iterator_is_valid(v3d, base, flag) == false) {
2162 object_bases_iterator_next(iter, flag);
2163 }
2164 else {
2165 iter->current = base;
2166 }
2167}
2168
2169static void object_bases_iterator_next(BLI_Iterator *iter, const int flag)
2170{
2172 Base *base = data->base->next;
2173
2174 while (base) {
2175 if (object_bases_iterator_is_valid(data->v3d, base, flag)) {
2176 iter->current = base;
2177 data->base = base;
2178 return;
2179 }
2180 base = base->next;
2181 }
2182
2183 iter->valid = false;
2184}
2185
2187{
2188 if (iter->data) {
2189 MEM_delete(static_cast<LayerObjectBaseIteratorData *>(iter->data));
2190 iter->data = nullptr;
2191 }
2192}
2193
2194static void objects_iterator_begin(BLI_Iterator *iter, void *data_in, const int flag)
2195{
2196 object_bases_iterator_begin(iter, data_in, flag);
2197
2198 if (iter->valid) {
2199 iter->current = (static_cast<Base *>(iter->current))->object;
2200 }
2201}
2202
2203static void objects_iterator_next(BLI_Iterator *iter, const int flag)
2204{
2205 object_bases_iterator_next(iter, flag);
2206
2207 if (iter->valid) {
2208 iter->current = (static_cast<Base *>(iter->current))->object;
2209 }
2210}
2211
2213{
2215}
2216
2218
2219/* -------------------------------------------------------------------- */
2223
2229
2234
2239
2241
2242/* -------------------------------------------------------------------- */
2245
2247{
2248 objects_iterator_begin(iter, data_in, 0);
2249}
2250
2255
2260
2262
2263/* -------------------------------------------------------------------- */
2266
2268{
2271 if (iter->valid) {
2272 if (BKE_object_is_libdata(static_cast<Object *>(iter->current)) == false) {
2273 /* First object is valid (selectable and not libdata) -> all good. */
2274 return;
2275 }
2276
2277 /* Object is selectable but not editable -> search for another one. */
2279 }
2280}
2281
2283{
2284 /* Search while there are objects and the one we have is not editable (editable = not libdata).
2285 */
2286 do {
2288 } while (iter->valid && BKE_object_is_libdata(static_cast<Object *>(iter->current)) != false);
2289}
2290
2295
2297
2298/* -------------------------------------------------------------------- */
2301
2307
2312
2317
2319
2320/* -------------------------------------------------------------------- */
2323
2325{
2326 object_bases_iterator_begin(iter, data_in, 0);
2327}
2328
2333
2338
2340
2341/* -------------------------------------------------------------------- */
2344
2346{
2347 return (base->object->type == data->object_type) &&
2348 (base->object->mode & data->object_mode) != 0;
2349}
2350
2352{
2353 ObjectsInModeIteratorData *data = static_cast<ObjectsInModeIteratorData *>(data_in);
2354 Base *base = data->base_active;
2355
2356 /* In this case the result will always be empty, the caller must check for no mode. */
2357 BLI_assert(data->object_mode != 0);
2358
2359 /* when there are no objects */
2360 if (base == nullptr) {
2361 iter->valid = false;
2362 return;
2363 }
2364 iter->data = data_in;
2365 iter->current = base;
2366
2367 /* default type is active object type */
2368 if (data->object_type < 0) {
2369 data->object_type = base->object->type;
2370 }
2371
2372 if (!(base_is_in_mode(data, base) && BKE_base_is_visible(data->v3d, base))) {
2374 }
2375}
2376
2378{
2380 Base *base = static_cast<Base *>(iter->current);
2381
2382 if (base == data->base_active) {
2383 /* first step */
2384 base = data->view_layer->object_bases.first();
2385 if ((base == data->base_active) && BKE_base_is_visible(data->v3d, base)) {
2386 base = base->next;
2387 }
2388 }
2389 else {
2390 base = base->next;
2391 }
2392
2393 while (base) {
2394 if ((base != data->base_active) && base_is_in_mode(data, base) &&
2395 BKE_base_is_visible(data->v3d, base))
2396 {
2397 iter->current = base;
2398 return;
2399 }
2400 base = base->next;
2401 }
2402 iter->valid = false;
2403}
2404
2406{
2407 /* do nothing */
2408}
2409
2411
2412/* -------------------------------------------------------------------- */
2415
2417{
2418 /* Apply collection flags. */
2421
2422 /* Apply object restrictions. */
2423 const int object_restrict = base->object->visibility_flag;
2424 if (object_restrict & OB_HIDE_VIEWPORT) {
2426 }
2427 if (object_restrict & OB_HIDE_RENDER) {
2428 base->flag &= ~BASE_ENABLED_RENDER;
2429 }
2430 if (object_restrict & OB_HIDE_SELECT) {
2431 base->flag &= ~BASE_SELECTABLE;
2432 }
2433
2434 /* Apply viewport visibility by default. The dependency graph for render
2435 * can change these again, but for tools we always want the viewport
2436 * visibility to be in sync regardless if depsgraph was evaluated. */
2437 if (!(base->flag & BASE_ENABLED_VIEWPORT) || (base->flag & BASE_HIDDEN)) {
2440 }
2441
2442 /* Deselect unselectable objects. */
2443 if (!(base->flag & BASE_SELECTABLE)) {
2444 base->flag &= ~BASE_SELECTED;
2445 }
2446}
2447
2448Span<Base *> ViewLayer::object_bases_array() const
2449{
2450 this->runtime->object_bases_array_mutex.ensure([&]() {
2451 this->runtime->object_bases_array.clear();
2452 for (Base &base : *BKE_view_layer_object_bases_get(const_cast<ViewLayer *>(this))) {
2453 this->runtime->object_bases_array.append(&base);
2454 }
2455 });
2456 return this->runtime->object_bases_array;
2457}
2458
2459static void layer_eval_view_layer(Depsgraph *depsgraph, Scene *scene, ViewLayer *view_layer)
2460{
2461 DEG_debug_print_eval(depsgraph, __func__, view_layer->name, view_layer);
2462
2463 BKE_view_layer_synced_ensure(*DEG_get_bmain(depsgraph), scene, view_layer);
2464 /* Compute bases cache eagerly because it will be used later. */
2465 view_layer->object_bases_array();
2466}
2467
2468void BKE_layer_eval_view_layer_indexed(Depsgraph *depsgraph, Scene *scene, int view_layer_index)
2469{
2470 BLI_assert(view_layer_index >= 0);
2471 ViewLayer *view_layer = static_cast<ViewLayer *>(
2472 BLI_findlink(&scene->view_layers, view_layer_index));
2473 BLI_assert(view_layer != nullptr);
2474 layer_eval_view_layer(depsgraph, scene, view_layer);
2475}
2476
2478
2479/* -------------------------------------------------------------------- */
2482
2483static void write_layer_collections(BlendWriter *writer, ListBaseT<LayerCollection> *lb)
2484{
2485 for (LayerCollection &lc : *lb) {
2486 writer->write_struct(&lc, [](BlendStructWriter<LayerCollection> &struct_writer) {
2487 struct_writer.shallow_data.runtime_flag = {};
2488 });
2489
2490 write_layer_collections(writer, &lc.layer_collections);
2491 }
2492}
2493
2495 const Scene * /*scene*/,
2496 ViewLayer *view_layer)
2497{
2498 if (!BKE_view_layer_is_synced(*view_layer)) {
2500 }
2501 writer->write_struct(view_layer, [](BlendStructWriter<ViewLayer> &struct_writer) {
2502 struct_writer.shallow_data.stats = nullptr;
2503 struct_writer.shallow_data.runtime = nullptr;
2504 struct_writer.shallow_data.flag &= ~VIEW_LAYER_OUT_OF_SYNC;
2505 });
2507 [](BlendStructWriter<Base> &struct_writer) {
2508 struct_writer.shallow_data.base_orig = nullptr;
2509 });
2510
2511 if (view_layer->id_properties) {
2512 IDP_BlendWrite(writer, view_layer->id_properties);
2513 }
2514 if (view_layer->system_properties) {
2515 IDP_BlendWrite(writer, view_layer->system_properties);
2516 }
2517
2518 for (FreestyleModuleConfig &fmc : view_layer->freestyle_config.modules) {
2519 writer->write_struct(&fmc);
2520 }
2521
2522 for (FreestyleLineSet &fls : view_layer->freestyle_config.linesets) {
2523 writer->write_struct(&fls);
2524 }
2525 for (ViewLayerAOV &aov : view_layer->aovs) {
2526 writer->write_struct(&aov);
2527 }
2528 for (ViewLayerLightgroup &lightgroup : view_layer->lightgroups) {
2529 writer->write_struct(&lightgroup);
2530 }
2531 write_layer_collections(writer, &view_layer->layer_collections);
2532}
2533
2535 ViewLayer *view_layer,
2536 ListBaseT<LayerCollection> *lb,
2537 bool master,
2538 bool &active_collection_found)
2539{
2541 for (LayerCollection &lc : *lb) {
2542 /* Master collection is not a real data-block. */
2543 if (master) {
2544 BLO_read_struct(reader, Collection, &lc.collection);
2545 }
2546
2547 if (&lc == view_layer->active_collection) {
2548 active_collection_found = true;
2549 }
2550
2552 reader, view_layer, &lc.layer_collections, false, active_collection_found);
2553 }
2554}
2555
2557{
2558 view_layer->runtime = MEM_new<ViewLayerRuntime>(__func__);
2559 view_layer->stats = nullptr;
2560 BLO_read_struct_list(reader, Base, &view_layer->object_bases);
2561 BLO_read_struct(reader, Base, &view_layer->basact);
2562
2563 bool active_collection_found = false;
2564 BLO_read_struct(reader, LayerCollection, &view_layer->active_collection);
2565
2567 reader, view_layer, &view_layer->layer_collections, true, active_collection_found);
2568
2569 if (!active_collection_found) {
2570 /* Ensure pointer is valid, in case of corrupt blend file. */
2571 view_layer->active_collection = view_layer->layer_collections.first();
2572 }
2573
2574 BLO_read_struct(reader, IDProperty, &view_layer->id_properties);
2575 IDP_BlendDataRead(reader, &view_layer->id_properties);
2576 BLO_read_struct(reader, IDProperty, &view_layer->system_properties);
2577 IDP_BlendDataRead(reader, &view_layer->system_properties);
2578
2581
2582 BLO_read_struct_list(reader, ViewLayerAOV, &view_layer->aovs);
2583 BLO_read_struct(reader, ViewLayerAOV, &view_layer->active_aov);
2584
2587}
2588
2590 ID * /*self_id*/,
2591 ViewLayer *view_layer)
2592{
2593 for (Base &base : view_layer->object_bases.items_mutable()) {
2594 if (base.object == nullptr) {
2595 /* Free in case linked object got lost. */
2596 BLI_freelinkN(&view_layer->object_bases, &base);
2597 if (view_layer->basact == &base) {
2598 view_layer->basact = nullptr;
2599 }
2600 }
2601 }
2602}
2603
2605
2606/* -------------------------------------------------------------------- */
2609
2611{
2612 ViewLayerAOV *aov = view_layer->active_aov;
2613 if (aov == nullptr) {
2614 return;
2615 }
2616
2617 /* Don't allow dots, it's incompatible with OpenEXR convention to store channels
2618 * as "layer.pass.channel". */
2619 BLI_string_replace_char(aov->name, '.', '_');
2621 &view_layer->aovs, aov, DATA_("AOV"), '_', offsetof(ViewLayerAOV, name), sizeof(aov->name));
2622}
2623
2625{
2626 if (aov != nullptr) {
2627 BLI_assert(BLI_findindex(&view_layer->aovs, aov) != -1);
2628 view_layer->active_aov = aov;
2629 }
2630 else {
2631 view_layer->active_aov = nullptr;
2632 }
2633}
2634
2636{
2637 ViewLayerAOV *aov;
2638 aov = MEM_new<ViewLayerAOV>(__func__);
2639 aov->type = AOV_TYPE_COLOR;
2640 STRNCPY_UTF8(aov->name, DATA_("AOV"));
2641 BLI_addtail(&view_layer->aovs, aov);
2642 viewlayer_aov_active_set(view_layer, aov);
2644 return aov;
2645}
2646
2648{
2649 BLI_assert(BLI_findindex(&view_layer->aovs, aov) != -1);
2650 BLI_assert(aov != nullptr);
2651 if (view_layer->active_aov == aov) {
2652 if (aov->next) {
2653 viewlayer_aov_active_set(view_layer, aov->next);
2654 }
2655 else {
2656 viewlayer_aov_active_set(view_layer, aov->prev);
2657 }
2658 }
2659 BLI_freelinkN(&view_layer->aovs, aov);
2660}
2661
2663{
2664 viewlayer_aov_active_set(view_layer, aov);
2665}
2666
2668
2669static void bke_view_layer_verify_aov_cb(void *userdata,
2670 Scene * /*scene*/,
2671 ViewLayer * /*view_layer*/,
2672 const char *name,
2673 int /*channels*/,
2674 const char * /*chanid*/,
2675 eNodeSocketDatatype /*type*/)
2676{
2677 auto *name_count = static_cast<ViewLayerAOVNameCountMap *>(userdata);
2678 name_count->lookup_or_add(name, 0)++;
2679
2680 /* The alpha pass is implicitly added from the combined pass. */
2681 if (STREQ(name, RE_PASSNAME_COMBINED)) {
2682 name_count->lookup_or_add("Alpha", 0)++;
2683 }
2684}
2685
2686void BKE_view_layer_verify_aov(RenderEngine *engine, Scene *scene, ViewLayer *view_layer)
2687{
2689
2690 ViewLayerAOVNameCountMap name_count;
2691 for (ViewLayerAOV &aov : view_layer->aovs) {
2692 /* Disable conflict flag, so that the AOV is included when iterating over all passes below. */
2693 aov.flag &= ~AOV_CONFLICT;
2694 }
2696 engine, scene, view_layer, bke_view_layer_verify_aov_cb, &name_count);
2697 for (ViewLayerAOV &aov : view_layer->aovs) {
2698 const int count = name_count.lookup_default(aov.name, 0);
2699 SET_FLAG_FROM_TEST(aov.flag, count > 1, AOV_CONFLICT);
2700 }
2701}
2702
2704{
2705 for (ViewLayerAOV &aov : view_layer->aovs) {
2706 if ((aov.flag & AOV_CONFLICT) == 0) {
2707 return true;
2708 }
2709 }
2710 return false;
2711}
2712
2714{
2715 for (ViewLayer &view_layer : scene->view_layers) {
2716 if (BLI_findindex(&view_layer.aovs, aov) != -1) {
2717 return &view_layer;
2718 }
2719 }
2720 return nullptr;
2721}
2722
2724
2725/* -------------------------------------------------------------------- */
2728
2730 ViewLayerLightgroup *lightgroup)
2731{
2732 /* Don't allow dots, it's incompatible with OpenEXR convention to store channels
2733 * as "layer.pass.channel". */
2734 BLI_string_replace_char(lightgroup->name, '.', '_');
2735 BLI_uniquename(&view_layer->lightgroups,
2736 lightgroup,
2737 DATA_("Lightgroup"),
2738 '_',
2740 sizeof(lightgroup->name));
2741}
2742
2744{
2745 if (lightgroup != nullptr) {
2746 BLI_assert(BLI_findindex(&view_layer->lightgroups, lightgroup) != -1);
2747 view_layer->active_lightgroup = lightgroup;
2748 }
2749 else {
2750 view_layer->active_lightgroup = nullptr;
2751 }
2752}
2753
2755{
2756 ViewLayerLightgroup *lightgroup;
2757 lightgroup = MEM_new<ViewLayerLightgroup>(__func__);
2758 STRNCPY_UTF8(lightgroup->name, (name && name[0]) ? name : DATA_("Lightgroup"));
2759 BLI_addtail(&view_layer->lightgroups, lightgroup);
2760 viewlayer_lightgroup_active_set(view_layer, lightgroup);
2761 viewlayer_lightgroup_make_name_unique(view_layer, lightgroup);
2762 return lightgroup;
2763}
2764
2766{
2767 BLI_assert(BLI_findindex(&view_layer->lightgroups, lightgroup) != -1);
2768 BLI_assert(lightgroup != nullptr);
2769 if (view_layer->active_lightgroup == lightgroup) {
2770 if (lightgroup->next) {
2771 viewlayer_lightgroup_active_set(view_layer, lightgroup->next);
2772 }
2773 else {
2774 viewlayer_lightgroup_active_set(view_layer, lightgroup->prev);
2775 }
2776 }
2777 BLI_freelinkN(&view_layer->lightgroups, lightgroup);
2778}
2779
2781{
2782 viewlayer_lightgroup_active_set(view_layer, lightgroup);
2783}
2784
2786{
2787 for (ViewLayer &view_layer : scene->view_layers) {
2788 if (BLI_findindex(&view_layer.lightgroups, lightgroup) != -1) {
2789 return &view_layer;
2790 }
2791 }
2792 return nullptr;
2793}
2794
2796 ViewLayer *view_layer,
2797 ViewLayerLightgroup *lightgroup,
2798 const char *name)
2799{
2800 char old_name[64];
2801 STRNCPY_UTF8(old_name, lightgroup->name);
2802 STRNCPY_UTF8(lightgroup->name, name);
2803 viewlayer_lightgroup_make_name_unique(view_layer, lightgroup);
2804
2805 if (scene != nullptr) {
2806 /* Update objects in the scene to refer to the new name instead. */
2807 FOREACH_SCENE_OBJECT_BEGIN (scene, ob) {
2808 if (ID_IS_EDITABLE(ob) && ob->lightgroup != nullptr) {
2809 LightgroupMembership *lgm = ob->lightgroup;
2810 if (STREQ(lgm->name, old_name)) {
2811 STRNCPY_UTF8(lgm->name, lightgroup->name);
2812 }
2813 }
2814 }
2816
2817 /* Update the scene's world to refer to the new name instead. */
2818 if (scene->world != nullptr && ID_IS_EDITABLE(scene->world) &&
2819 scene->world->lightgroup != nullptr)
2820 {
2821 LightgroupMembership *lgm = scene->world->lightgroup;
2822 if (STREQ(lgm->name, old_name)) {
2823 STRNCPY_UTF8(lgm->name, lightgroup->name);
2824 }
2825 }
2826 }
2827}
2828
2830{
2831 if (lgm == nullptr) {
2832 name[0] = '\0';
2833 return 0;
2834 }
2835 return BLI_strncpy_utf8_rlen(name, lgm->name, sizeof(lgm->name));
2836}
2837
2839{
2840 if (lgm == nullptr) {
2841 return 0;
2842 }
2843 return strlen(lgm->name);
2844}
2845
2847{
2848 if (name[0] != '\0') {
2849 if (*lgm == nullptr) {
2850 *lgm = MEM_new<LightgroupMembership>(__func__);
2851 }
2852 BLI_strncpy_utf8((*lgm)->name, name, sizeof((*lgm)->name));
2853 }
2854 else {
2855 if (*lgm != nullptr) {
2856 MEM_delete(*lgm);
2857 *lgm = nullptr;
2858 }
2859 }
2860}
2861
2863
2864} // namespace blender
#define FOREACH_SCENE_OBJECT_END
#define FOREACH_SCENE_OBJECT_BEGIN(scene, _instance)
#define IDP_BlendDataRead(reader, prop)
#define CMP_NODE_R_LAYERS
General operations, lookup, etc. for blender objects.
#define STRNCPY_UTF8(dst, src)
#define BLO_read_struct_list(reader, struct_name, list)
#define BLO_read_struct(reader, struct_name, ptr_p)
#define DATA_(msgid)
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
#define CLOG_FATAL(clg_ref,...)
Definition CLG_log.h:187
ID and Library types, which are fundamental for SDNA.
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:697
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:736
#define ID_MISSING(_id)
Definition DNA_ID.h:695
Object groups, one object can be in many groups at once.
Object is a sort of wrapper for general info.
#define RE_PASSNAME_COMBINED
#define BASE_VISIBLE(v3d, base)
Read Blender memory allocation and freeing.
volatile int lock
#define UNUSED_VARS_NDEBUG(...)
#define SET_FLAG_FROM_TEST(value, test, flag)
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
Value lookup_default(const Key &key, const Value &default_value) const
Definition BLI_map.hh:570
bool remove(const Key &key)
Definition BLI_map.hh:368
bool contains(const Key &key) const
Definition BLI_map.hh:353
auto add_or_modify(const Key &key, const CreateValueF &create_value, const ModifyValueF &modify_value) -> decltype(create_value(nullptr))
Definition BLI_map.hh:481
Value & lookup_or_add(const Key &key, const Value &value)
Definition BLI_map.hh:588
#define offsetof(t, d)
#define layer
void relations_update_view_layer_scene_strips(Main *bmain, Scene *scene, const char *old_name, const char *new_name)
BMesh Iterators.
void * BLI_findptr(const ListBase *listbase, const void *ptr, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:710
static void direct_link_layer_collections(BlendDataReader *reader, ViewLayer *view_layer, ListBaseT< LayerCollection > *lb, bool master, bool &active_collection_found)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static void layer_collection_local_visibility_unset_recursive(LayerCollection *layer_collection, const int local_collections_uid)
static void viewlayer_aov_active_set(ViewLayer *view_layer, ViewLayerAOV *aov)
void BKE_view_layer_selected_objects_iterator_end(BLI_Iterator *iter)
unsigned __int64 uint64_t
Definition BKE_text.h:91
static bool base_is_in_mode(ObjectsInModeIteratorData *data, Base *base)
void IDP_FreeProperty_ex(IDProperty *prop, bool do_id_user)
Definition idprop.cc:1398
static bool view_layer_objects_base_cache_validate(ViewLayer *view_layer, LayerCollection *layer)
char * STRNCPY(char(&dst)[N], const char *src)
LayerCollection * BKE_layer_collection_get_active_editable(ViewLayer *view_layer)
void BKE_view_layer_base_select_and_set_active(ViewLayer *view_layer, Base *selbase)
static void objects_iterator_next(BLI_Iterator *iter, const int flag)
bool BKE_layer_collection_activate(ViewLayer *view_layer, LayerCollection *lc)
static void layer_collection_bases_hide_recursive(const Main &bmain, const Scene *scene, ViewLayer *view_layer, LayerCollection *lc)
void BKE_view_layer_visible_bases_iterator_begin(BLI_Iterator *iter, void *data_in)
static void layer_aov_copy_data(ViewLayer *view_layer_dst, const ViewLayer *view_layer_src, ListBaseT< ViewLayerAOV > *aovs_dst, const ListBaseT< ViewLayerAOV > *aovs_src)
bool BKE_scene_view_layers_synced_ensure(const Main &bmain, const Scene *scene)
static ViewLayer * view_layer_add(const char *name)
void _BKE_view_layer_synced_ensure_or_assert(const Main *bmain, const Scene *scene, ViewLayer *view_layer)
ViewLayerLightgroup * BKE_view_layer_add_lightgroup(ViewLayer *view_layer, const char *name)
void id_us_plus(ID *id)
Definition lib_id.cc:433
void BKE_view_layer_blend_write(BlendWriter *writer, const Scene *scene, ViewLayer *view_layer)
bool BKE_base_is_visible(const View3D *v3d, const Base *base)
LayerCollection * BKE_layer_collection_get_active(ViewLayer *view_layer)
void BKE_view_layer_selected_bases_iterator_next(BLI_Iterator *iter)
void void void void void void BLI_duplicatelist(ListBase *dst, const ListBase *src) ATTR_NONNULL(1
Definition listbase.cc:834
@ BASE_ENABLED_VIEWPORT
@ BASE_ENABLED_AND_VISIBLE_IN_DEFAULT_VIEWPORT
@ BASE_ENABLED_AND_MAYBE_VISIBLE_IN_VIEWPORT
@ VIEWLAYER_ADD_COPY
Definition BKE_layer.hh:42
@ VIEWLAYER_ADD_NEW
Definition BKE_layer.hh:40
@ VIEWLAYER_ADD_EMPTY
Definition BKE_layer.hh:41
void BKE_view_layer_bases_in_mode_iterator_begin(BLI_Iterator *iter, void *data_in)
void BKE_view_layer_visible_bases_iterator_end(BLI_Iterator *iter)
Map< const Object *, Base * > ObjectBasesMap
static void layer_lightgroup_copy_data(ViewLayer *view_layer_dst, const ViewLayer *view_layer_src, ListBaseT< ViewLayerLightgroup > *lightgroups_dst, const ListBaseT< ViewLayerLightgroup > *lightgroups_src)
ViewLayer * BKE_view_layer_find_with_lightgroup(Scene *scene, ViewLayerLightgroup *lightgroup)
static LayerCollectionResync * layer_collection_resync_find(LayerCollectionResync *layer_resync, Collection *child_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
void BKE_view_layer_selected_objects_iterator_next(BLI_Iterator *iter)
void BKE_freestyle_config_init(struct FreestyleConfig *config)
Definition freestyle.cc:33
ViewLayer * BKE_view_layer_find_with_aov(Scene *scene, ViewLayerAOV *aov)
Map< std::string, int > ViewLayerAOVNameCountMap
void BLI_string_replace_char(char *str, char src, char dst) ATTR_NONNULL(1)
void IDP_BlendWrite(BlendWriter *writer, const IDProperty *prop)
Definition idprop.cc:1696
static void viewlayer_aov_make_name_unique(ViewLayer *view_layer)
static void objects_iterator_end(BLI_Iterator *iter)
std::string RNA_path_name_to_infix(const StringRefNull string)
Definition rna_path.cc:1419
void BKE_main_collections_object_cache_free(const Main *bmain)
unsigned short ushort
Definition BKE_text.h:56
static LayerCollection * layer_collection_add(ListBaseT< LayerCollection > *lb_parent, Collection *collection)
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
void DEG_debug_print_eval(Depsgraph *depsgraph, const char *function_name, const char *object_name, const void *object_address)
int BKE_lightgroup_membership_get(const LightgroupMembership *lgm, char *name)
static void layer_eval_view_layer(Depsgraph *depsgraph, Scene *scene, ViewLayer *view_layer)
void BKE_view_layer_selected_objects_iterator_begin(BLI_Iterator *iter, void *data_in)
void RE_engine_update_render_passes(RenderEngine *engine, Scene *scene, ViewLayer *view_layer, update_render_passes_cb_t callback, void *callback_data)
@ V3D_LOCAL_COLLECTIONS
static void layer_collection_flag_unset_recursive(LayerCollection *lc, const eLayerCollection_Flag flag)
void BKE_view_layer_remove_aov(ViewLayer *view_layer, ViewLayerAOV *aov)
void BKE_lightgroup_membership_set(LightgroupMembership **lgm, const char *name)
static LayerCollection * find_layer_collection_by_scene_collection(LayerCollection *lc, const Collection *collection)
bool BKE_layer_collection_has_layer_collection(LayerCollection *lc_parent, LayerCollection *lc_child)
int BKE_layer_collection_findindex(ViewLayer *view_layer, const LayerCollection *lc)
void BKE_layer_collection_isolate_local(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d, LayerCollection *lc, bool extend)
static void layer_collection_flag_set_recursive(LayerCollection *lc, const eLayerCollection_Flag flag)
void BKE_view_layer_free_ex(ViewLayer *view_layer, bool do_id_user)
void BKE_view_layer_set_active_aov(ViewLayer *view_layer, ViewLayerAOV *aov)
void MEM_delete_void(void *vmemh)
void BKE_view_layer_need_resync_tag(ViewLayer *view_layer)
static void write_layer_collections(BlendWriter *writer, ListBaseT< LayerCollection > *lb)
@ LIB_ID_CREATE_NO_USER_REFCOUNT
ViewLayer * BKE_view_layer_default_view(const Scene *scene)
static void objects_iterator_begin(BLI_Iterator *iter, void *data_in, const int flag)
void BKE_layer_collection_set_flag(LayerCollection *lc, eLayerCollection_Flag flag, bool value)
static bool find_scene_collection_in_scene_collections(ListBaseT< LayerCollection > *lb, const LayerCollection *lc)
bool BKE_object_is_visible_in_viewport_ex(const View3D *v3d, const Object *ob, ObjectType ob_type)
void BKE_layer_collection_resync_forbid(Main &bmain)
void BKE_animdata_fix_paths(ID &id, StringRef prefix, StringRef old_infix, StringRef new_infix, bool verify_paths, const DriverMap &driver_map)
Object * BKE_view_layer_camera_find(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
void BKE_layer_eval_view_layer_indexed(Depsgraph *depsgraph, Scene *scene, int view_layer_index)
void BKE_view_layer_rename_lightgroup(Scene *scene, ViewLayer *view_layer, ViewLayerLightgroup *lightgroup, const char *name)
void id_lib_indirect_weak_link(ID *id)
Definition lib_id.cc:377
ViewLayerAOV * BKE_view_layer_add_aov(ViewLayer *view_layer)
static void layer_collection_flag_recursive_set(LayerCollection *lc, const eLayerCollection_Flag flag, const bool value, const bool restore_flag)
static constexpr int no_resync_recurse_max
bool BKE_main_collection_sync_remap(const Main *bmain)
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
void BLI_mempool_destroy(BLI_mempool *pool) ATTR_NONNULL(1)
void BLI_uniquename(const ListBase *list, void *vlink, const char *defname, char delim, int name_offset, size_t name_maxncpy) ATTR_NONNULL(1
bool BKE_scene_has_object(const Main &bmain, Scene *scene, Object *ob)
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
static bool object_bases_iterator_is_valid(const View3D *v3d, Base *base, const int flag)
LayerCollection * BKE_layer_collection_from_index(ViewLayer *view_layer, int index)
void DEG_id_tag_update_ex(Main *bmain, ID *id, unsigned int flags)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void BKE_layer_collection_resync_allow(Main &bmain)
void BKE_view_layer_bases_in_mode_iterator_end(BLI_Iterator *iter)
void BKE_view_layer_selected_editable_objects_iterator_begin(BLI_Iterator *iter, void *data_in)
@ VIEW_LAYER_OUT_OF_SYNC
@ VIEW_LAYER_HAS_EXPORT_COLLECTIONS
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
Main * DEG_get_bmain(const Depsgraph *graph)
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_layer_collection_isolate_global(Scene *scene, ViewLayer *view_layer, LayerCollection *lc, bool extend)
bool BKE_scene_collection_sync(const Main &bmain, const Scene *scene)
void BKE_view_layer_copy_data(Scene *scene_dst, const Scene *scene_src, ViewLayer *view_layer_dst, const ViewLayer *view_layer_src, int flag)
BMesh const char void * data
@ LAYER_COLLECTION_INDIRECT_ONLY
@ LAYER_COLLECTION_EXCLUDE
@ LAYER_COLLECTION_HIDE
@ LAYER_COLLECTION_PREVIOUSLY_EXCLUDED
@ LAYER_COLLECTION_HOLDOUT
bool BKE_view_layer_is_synced(const ViewLayer &view_layer)
BLI_mempool * BLI_mempool_create(unsigned int esize, unsigned int elem_num, unsigned int pchunk, unsigned int flag) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL
void BKE_view_layer_verify_aov(RenderEngine *engine, Scene *scene, ViewLayer *view_layer)
void BKE_view_layer_free_object_content(ViewLayer *view_layer)
void BKE_view_layer_rename(Main *bmain, Scene *scene, ViewLayer *view_layer, const char *newname)
bool BKE_main_view_layers_synced_ensure(const Main *bmain)
static void layer_collection_sync(ViewLayer *view_layer, LayerCollectionResync *layer_resync, BLI_mempool *layer_resync_mempool, ListBaseT< Base > *r_lb_new_object_bases, const eLayerCollection_Flag parent_layer_flag, const short parent_collection_restrict, const short parent_layer_restrict, const ushort parent_local_collections_bits)
@ LAYER_COLLECTION_VISIBLE_VIEW_LAYER
@ LAYER_COLLECTION_HIDE_VIEWPORT
@ LAYER_COLLECTION_HAS_OBJECTS
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:277
void BKE_freestyle_config_free(struct FreestyleConfig *config, bool do_id_user)
Definition freestyle.cc:46
static void layer_collection_resync_unused_layers_free(ViewLayer *view_layer, LayerCollectionResync *layer_resync)
void BKE_view_layer_visible_bases_iterator_next(BLI_Iterator *iter)
static LayerCollectionResync * layer_collection_resync_create_recurse(LayerCollectionResync *parent_layer_resync, LayerCollection *layer, BLI_mempool *mempool)
bool BKE_layer_collection_local_sync(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d)
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)
static void layer_collection_exclude_all(LayerCollection *layer_collection)
void BKE_view_layer_visible_objects_iterator_begin(BLI_Iterator *iter, void *data_in)
void BKE_view_layer_visible_objects_iterator_end(BLI_Iterator *iter)
int BKE_layer_collection_count(const ViewLayer *view_layer)
static int collection_count(const ListBaseT< LayerCollection > *lb)
static void layer_collection_local_visibility_set_recursive(LayerCollection *layer_collection, const int local_collections_uid)
ViewLayer * BKE_view_layer_find_from_collection(const Scene *scene, LayerCollection *lc)
void BKE_view_layer_blend_read_data(BlendDataReader *reader, ViewLayer *view_layer)
int BLI_listbase_count_at_most(const ListBase *listbase, int count_max) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:518
void BKE_base_set_visible(const Main &bmain, Scene *scene, ViewLayer *view_layer, Base *base, bool extend)
bool BKE_object_is_visible_in_viewport(const View3D *v3d, const Object *ob)
void BKE_view_layer_base_deselect_all(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
static const eBase_Flag g_base_collection_flags
void BKE_freestyle_config_copy(struct FreestyleConfig *new_config, const struct FreestyleConfig *config, int flag)
bool BKE_view_layer_has_valid_aov(ViewLayer *view_layer)
void BKE_view_layer_remove_lightgroup(ViewLayer *view_layer, ViewLayerLightgroup *lightgroup)
static void layer_collection_free(ViewLayer *view_layer, LayerCollection *lc)
static int index_from_collection(ListBaseT< LayerCollection > *lb, const LayerCollection *lc, int *i)
ViewLayer * BKE_view_layer_find(const Scene *scene, const char *layer_name)
void BKE_view_layer_free(ViewLayer *view_layer)
void BKE_layer_collection_doversion_2_80(const Scene *scene, ViewLayer *view_layer)
static void layer_collection_objects_sync(ViewLayer *view_layer, LayerCollection *layer, ListBaseT< Base > *r_lb_new_object_bases, const short collection_restrict, const short layer_restrict, const ushort local_collections_bits)
static LayerCollection * collection_from_index(ListBaseT< LayerCollection > *lb, const int number, int *i)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
static void view_layer_bases_hash_create(ViewLayer *view_layer, const bool do_base_duplicates_fix)
void BKE_view_layer_set_active_lightgroup(ViewLayer *view_layer, ViewLayerLightgroup *lightgroup)
void BKE_base_eval_flags(Base *base)
@ BLI_MEMPOOL_NOP
static void viewlayer_lightgroup_active_set(ViewLayer *view_layer, ViewLayerLightgroup *lightgroup)
std::mutex Mutex
Definition BLI_mutex.hh:51
void * BLI_mempool_calloc(BLI_mempool *pool) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(1)
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
@ ID_RECALC_BASE_FLAGS
Definition DNA_ID.h:1104
Base * BKE_view_layer_base_find(ViewLayer *view_layer, Object *ob)
LayerCollection * BKE_layer_collection_activate_parent(ViewLayer *view_layer, LayerCollection *lc)
void BKE_view_layer_bases_in_mode_iterator_next(BLI_Iterator *iter)
bool BKE_layer_collection_objects_select(const Main &bmain, const Scene *scene, ViewLayer *view_layer, LayerCollection *lc, bool deselect)
static CLG_LogRef LOG
Definition logging.cpp:12
ViewLayer * BKE_view_layer_add(const Main *bmain, Scene *scene, const char *name, ViewLayer *view_layer_source, int type)
void BKE_view_layer_selected_bases_iterator_begin(BLI_Iterator *iter, void *data_in)
ViewLayer * BKE_view_layer_context_active_PLACEHOLDER(const Scene *scene)
static bool layer_collection_hidden(ViewLayer *view_layer, LayerCollection *lc)
static void object_bases_iterator_end(BLI_Iterator *iter)
IDProperty * IDP_CopyProperty_ex(const IDProperty *prop, int flag) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:968
bool BKE_object_is_libdata(const Object *ob)
Definition object.cc:2840
bool BKE_main_collection_sync(const Main *bmain)
ViewLayer * BKE_view_layer_default_render(const Scene *scene)
static void bke_view_layer_verify_aov_cb(void *userdata, Scene *, ViewLayer *, const char *name, int, const char *, eNodeSocketDatatype)
void BKE_view_layer_selected_editable_objects_iterator_next(BLI_Iterator *iter)
static void layer_collections_copy_data(ViewLayer *view_layer_dst, const ViewLayer *view_layer_src, ListBaseT< LayerCollection > *layer_collections_dst, const ListBaseT< LayerCollection > *layer_collections_src)
static void object_bases_iterator_begin(BLI_Iterator *iter, void *data_in_v, const int flag)
void BKE_view_layer_blend_read_after_liblink(BlendLibReader *reader, ID *self_id, ViewLayer *view_layer)
static void layer_collection_local_sync(const Main &bmain, const Scene *scene, ViewLayer *view_layer, LayerCollection *layer_collection, const ushort local_collections_uid, bool visible)
void BKE_view_layer_visible_objects_iterator_next(BLI_Iterator *iter)
bool BKE_layer_collection_has_selected_objects(const Main &bmain, const Scene *scene, ViewLayer *view_layer, LayerCollection *lc)
ListBaseT< Base > * BKE_view_layer_object_bases_get(ViewLayer *view_layer)
void BKE_view_layer_selected_bases_iterator_end(BLI_Iterator *iter)
void BKE_view_layer_selected_objects_tag(const Main &bmain, const Scene *scene, ViewLayer *view_layer, eObject_Flag tag)
static void object_bases_iterator_next(BLI_Iterator *iter, const int flag)
bool BKE_view_layer_has_collection(const ViewLayer *view_layer, const Collection *collection)
static Base * object_base_new(Object *ob)
bool BKE_layer_collection_local_sync_all(const Main *bmain)
void BKE_view_layer_selected_editable_objects_iterator_end(BLI_Iterator *iter)
static void layer_collection_bases_show_recursive(const Main &bmain, const Scene *scene, ViewLayer *view_layer, LayerCollection *lc)
static void viewlayer_lightgroup_make_name_unique(ViewLayer *view_layer, ViewLayerLightgroup *lightgroup)
int BKE_lightgroup_membership_length(const LightgroupMembership *lgm)
void BKE_layer_collection_set_visible(const Main &bmain, const Scene *scene, ViewLayer *view_layer, LayerCollection *lc, bool visible, bool hierarchy)
bool BKE_layer_collection_sync(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
#define hash
Definition noise_c.cc:156
struct Object * object
unsigned short local_collections_bits
eBase_Flag flag_from_collection
struct Base * next
unsigned short local_view_bits
void write_struct_list(const ListBaseT< T > *list, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
struct Collection * collection
bke::CollectionRuntime * runtime
ListBaseT< CollectionExport > exporters
ListBaseT< CollectionChild > children
ListBaseT< CollectionObject > gobject
ListBaseT< FreestyleModuleConfig > modules
ListBaseT< FreestyleLineSet > linesets
char name[258]
Definition DNA_ID.h:433
ListBaseT< LayerCollectionResync > children_layer_resync
LayerCollectionResync * parent_layer_resync
eLayerCollection_Flag flag
struct Collection * collection
ListBaseT< LayerCollection > layer_collections
eLayerCollection_RuntimeFlag runtime_flag
struct LayerCollection * next
unsigned short local_collections_bits
ListBaseT< wmWindowManager > wm
Definition BKE_main.hh:415
ListBaseT< Collection > collections
Definition BKE_main.hh:406
std::atomic< int32_t > no_resync
Definition BKE_main.hh:464
ListBaseT< Scene > scenes
Definition BKE_main.hh:386
ListBaseT< bScreen > screens
Definition BKE_main.hh:400
bke::ObjectRuntime * runtime
eObject_VisibilityFlag visibility_flag
unsigned short base_local_view_bits
ListBaseT< ViewLayer > view_layers
struct World * world
struct Collection * master_collection
ListBaseT< SceneCompositorEffect > compositor_effects
ListBaseT< SpaceLink > spacedata
unsigned short local_collections_uid
struct View3D * localvd
unsigned short local_view_uid
eViewLayerAOVFlag flag
struct ViewLayerAOV * next
eViewLayerAOVType type
struct ViewLayerAOV * prev
struct ViewLayerLightgroup * prev
struct ViewLayerLightgroup * next
CacheMutex object_bases_array_mutex
Definition BKE_layer.hh:47
ObjectBasesMap * object_bases_hash
Definition BKE_layer.hh:49
struct FreestyleConfig freestyle_config
ViewLayerRuntime * runtime
ListBaseT< LayerCollection > layer_collections
struct SceneStats * stats
ListBaseT< ViewLayerAOV > aovs
ListBaseT< ViewLayerLightgroup > lightgroups
struct IDProperty * id_properties
eViewLayer_Flag flag
LayerCollection * active_collection
ListBaseT< Base > object_bases
struct Material * mat_override
struct IDProperty * system_properties
ViewLayerAOV * active_aov
ViewLayerLightgroup * active_lightgroup
ListBaseT< struct ScrArea > areabase
ListBaseT< CollectionParent > parents