Merge branch 'master' into blender2.8
[blender.git] / source / blender / blenkernel / intern / layer.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Dalai Felinto
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/blenkernel/intern/layer.c
24  *  \ingroup bke
25  */
26
27 #include <string.h>
28
29 #include "BLI_array.h"
30 #include "BLI_listbase.h"
31 #include "BLI_string.h"
32 #include "BLI_string_utf8.h"
33 #include "BLI_string_utils.h"
34 #include "BLI_threads.h"
35 #include "BLT_translation.h"
36
37 #include "BKE_animsys.h"
38 #include "BKE_collection.h"
39 #include "BKE_freestyle.h"
40 #include "BKE_global.h"
41 #include "BKE_idprop.h"
42 #include "BKE_layer.h"
43 #include "BKE_main.h"
44 #include "BKE_node.h"
45 #include "BKE_object.h"
46
47 #include "DNA_ID.h"
48 #include "DNA_space_types.h"
49 #include "DNA_collection_types.h"
50 #include "DNA_layer_types.h"
51 #include "DNA_object_types.h"
52 #include "DNA_node_types.h"
53 #include "DNA_scene_types.h"
54 #include "DNA_windowmanager_types.h"
55 #include "DNA_workspace_types.h"
56
57 #include "DEG_depsgraph.h"
58 #include "DEG_depsgraph_debug.h"
59 #include "DEG_depsgraph_query.h"
60
61 #include "DRW_engine.h"
62
63 #include "MEM_guardedalloc.h"
64
65
66 /* prototype */
67 static void object_bases_iterator_next(BLI_Iterator *iter, const int flag);
68
69
70 /*********************** Layer Collections and bases *************************/
71
72 static LayerCollection *layer_collection_add(ListBase *lb_parent, Collection *collection)
73 {
74         LayerCollection *lc = MEM_callocN(sizeof(LayerCollection), "Collection Base");
75         lc->collection = collection;
76         BLI_addtail(lb_parent, lc);
77
78         return lc;
79 }
80
81 static void layer_collection_free(ViewLayer *view_layer, LayerCollection *lc)
82 {
83         if (lc == view_layer->active_collection) {
84                 view_layer->active_collection = NULL;
85         }
86
87         for (LayerCollection *nlc = lc->layer_collections.first; nlc; nlc = nlc->next) {
88                 layer_collection_free(view_layer, nlc);
89         }
90
91         BLI_freelistN(&lc->layer_collections);
92 }
93
94 static Base *object_base_new(Object *ob)
95 {
96         Base *base = MEM_callocN(sizeof(Base), "Object Base");
97         base->object = ob;
98         return base;
99 }
100
101 /********************************* View Layer ********************************/
102
103
104 /* RenderLayer */
105
106 /* Returns the default view layer to view in workspaces if there is
107  * none linked to the workspace yet. */
108 ViewLayer *BKE_view_layer_default_view(const Scene *scene)
109 {
110         for (ViewLayer *view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
111                 if (!(view_layer->flag & VIEW_LAYER_RENDER)) {
112                         return view_layer;
113                 }
114         }
115
116         BLI_assert(scene->view_layers.first);
117         return scene->view_layers.first;
118 }
119
120 /* Returns the default view layer to render if we need to render just one. */
121 ViewLayer *BKE_view_layer_default_render(const Scene *scene)
122 {
123         for (ViewLayer *view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
124                 if (view_layer->flag & VIEW_LAYER_RENDER) {
125                         return view_layer;
126                 }
127         }
128
129         BLI_assert(scene->view_layers.first);
130         return scene->view_layers.first;
131 }
132
133 /* Returns view layer with matching name, or NULL if not found. */
134 ViewLayer *BKE_view_layer_find(const Scene *scene, const char *layer_name)
135 {
136         for (ViewLayer *view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
137                 if (STREQ(view_layer->name, layer_name)) {
138                         return view_layer;
139                 }
140         }
141
142         return NULL;
143 }
144
145 /**
146  * This is a placeholder to know which areas of the code need to be addressed
147  * for the Workspace changes. Never use this, you should typically get the
148  * active layer from the context or window.
149  */
150 ViewLayer *BKE_view_layer_context_active_PLACEHOLDER(const Scene *scene)
151 {
152         BLI_assert(scene->view_layers.first);
153         return scene->view_layers.first;
154 }
155
156 static ViewLayer *view_layer_add(const char *name)
157 {
158         if (!name) {
159                 name = DATA_("View Layer");
160         }
161
162         ViewLayer *view_layer = MEM_callocN(sizeof(ViewLayer), "View Layer");
163         view_layer->flag = VIEW_LAYER_RENDER | VIEW_LAYER_FREESTYLE;
164
165         BLI_strncpy_utf8(view_layer->name, name, sizeof(view_layer->name));
166
167         /* Pure rendering pipeline settings. */
168         view_layer->layflag = 0x7FFF;   /* solid ztra halo edge strand */
169         view_layer->passflag = SCE_PASS_COMBINED | SCE_PASS_Z;
170         view_layer->pass_alpha_threshold = 0.5f;
171         BKE_freestyle_config_init(&view_layer->freestyle_config);
172
173         return view_layer;
174 }
175
176 /**
177  * Add a new view layer
178  * by default, a view layer has the master collection
179  */
180 ViewLayer *BKE_view_layer_add(Scene *scene, const char *name)
181 {
182         ViewLayer *view_layer = view_layer_add(name);
183
184         BLI_addtail(&scene->view_layers, view_layer);
185
186         /* unique name */
187         BLI_uniquename(
188                 &scene->view_layers, view_layer, DATA_("ViewLayer"), '.',
189                 offsetof(ViewLayer, name), sizeof(view_layer->name));
190
191         BKE_layer_collection_sync(scene, view_layer);
192
193         return view_layer;
194 }
195
196 void BKE_view_layer_free(ViewLayer *view_layer)
197 {
198         BKE_view_layer_free_ex(view_layer, true);
199 }
200
201 /**
202  * Free (or release) any data used by this ViewLayer.
203  */
204 void BKE_view_layer_free_ex(ViewLayer *view_layer, const bool do_id_user)
205 {
206         view_layer->basact = NULL;
207
208         BLI_freelistN(&view_layer->object_bases);
209
210         if (view_layer->object_bases_hash) {
211                 BLI_ghash_free(view_layer->object_bases_hash, NULL, NULL);
212         }
213
214         for (LayerCollection *lc = view_layer->layer_collections.first; lc; lc = lc->next) {
215                 layer_collection_free(view_layer, lc);
216         }
217         BLI_freelistN(&view_layer->layer_collections);
218
219         for (ViewLayerEngineData *sled = view_layer->drawdata.first; sled; sled = sled->next) {
220                 if (sled->storage) {
221                         if (sled->free) {
222                                 sled->free(sled->storage);
223                         }
224                         MEM_freeN(sled->storage);
225                 }
226         }
227         BLI_freelistN(&view_layer->drawdata);
228
229         MEM_SAFE_FREE(view_layer->stats);
230
231         BKE_freestyle_config_free(&view_layer->freestyle_config, do_id_user);
232
233         if (view_layer->id_properties) {
234                 IDP_FreeProperty(view_layer->id_properties);
235                 MEM_freeN(view_layer->id_properties);
236         }
237
238         MEM_SAFE_FREE(view_layer->object_bases_array);
239
240         MEM_freeN(view_layer);
241 }
242
243 /**
244  * Tag all the selected objects of a renderlayer
245  */
246 void BKE_view_layer_selected_objects_tag(ViewLayer *view_layer, const int tag)
247 {
248         for (Base *base = view_layer->object_bases.first; base; base = base->next) {
249                 if ((base->flag & BASE_SELECTED) != 0) {
250                         base->object->flag |= tag;
251                 }
252                 else {
253                         base->object->flag &= ~tag;
254                 }
255         }
256 }
257
258 static bool find_scene_collection_in_scene_collections(ListBase *lb, const LayerCollection *lc)
259 {
260         for (LayerCollection *lcn = lb->first; lcn; lcn = lcn->next) {
261                 if (lcn == lc) {
262                         return true;
263                 }
264                 if (find_scene_collection_in_scene_collections(&lcn->layer_collections, lc)) {
265                         return true;
266                 }
267         }
268         return false;
269 }
270
271 /**
272  * Fallback for when a Scene has no camera to use
273  *
274  * \param view_layer: in general you want to use the same ViewLayer that is used
275  * for depsgraph. If rendering you pass the scene active layer, when viewing in the viewport
276  * you want to get ViewLayer from context.
277  */
278 Object *BKE_view_layer_camera_find(ViewLayer *view_layer)
279 {
280         for (Base *base = view_layer->object_bases.first; base; base = base->next) {
281                 if (base->object->type == OB_CAMERA) {
282                         return base->object;
283                 }
284         }
285
286         return NULL;
287 }
288
289 /**
290  * Find the ViewLayer a LayerCollection belongs to
291  */
292 ViewLayer *BKE_view_layer_find_from_collection(const Scene *scene, LayerCollection *lc)
293 {
294         for (ViewLayer *view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
295                 if (find_scene_collection_in_scene_collections(&view_layer->layer_collections, lc)) {
296                         return view_layer;
297                 }
298         }
299
300         return NULL;
301 }
302
303 /* Base */
304
305 static void view_layer_bases_hash_create(ViewLayer *view_layer)
306 {
307         static ThreadMutex hash_lock = BLI_MUTEX_INITIALIZER;
308
309         if (view_layer->object_bases_hash == NULL) {
310                 BLI_mutex_lock(&hash_lock);
311
312                 if (view_layer->object_bases_hash == NULL) {
313                         view_layer->object_bases_hash = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, __func__);
314
315                         for (Base *base = view_layer->object_bases.first; base; base = base->next) {
316                                 if (base->object) {
317                                         BLI_ghash_insert(view_layer->object_bases_hash, base->object, base);
318                                 }
319                         }
320                 }
321
322                 BLI_mutex_unlock(&hash_lock);
323         }
324 }
325
326 Base *BKE_view_layer_base_find(ViewLayer *view_layer, Object *ob)
327 {
328         if (!view_layer->object_bases_hash) {
329                 view_layer_bases_hash_create(view_layer);
330         }
331
332         return BLI_ghash_lookup(view_layer->object_bases_hash, ob);
333 }
334
335 void BKE_view_layer_base_deselect_all(ViewLayer *view_layer)
336 {
337         Base *base;
338
339         for (base = view_layer->object_bases.first; base; base = base->next) {
340                 base->flag &= ~BASE_SELECTED;
341         }
342 }
343
344 void BKE_view_layer_base_select(Base *selbase)
345 {
346         if ((selbase->flag & BASE_SELECTABLE) != 0) {
347                 selbase->flag |= BASE_SELECTED;
348         }
349 }
350
351 void BKE_view_layer_base_select_and_set_active(struct ViewLayer *view_layer, Base *selbase)
352 {
353         view_layer->basact = selbase;
354         BKE_view_layer_base_select(selbase);
355 }
356
357 /**************************** Copy View Layer and Layer Collections ***********************/
358
359 static void layer_collections_copy_data(
360         ViewLayer *view_layer_dst, const ViewLayer *view_layer_src,
361         ListBase *layer_collections_dst, const ListBase *layer_collections_src)
362 {
363         BLI_duplicatelist(layer_collections_dst, layer_collections_src);
364
365         LayerCollection *layer_collection_dst = layer_collections_dst->first;
366         const LayerCollection *layer_collection_src = layer_collections_src->first;
367
368         while (layer_collection_dst != NULL) {
369                 layer_collections_copy_data(
370                         view_layer_dst,
371                         view_layer_src,
372                         &layer_collection_dst->layer_collections,
373                         &layer_collection_src->layer_collections);
374
375                 if (layer_collection_src == view_layer_src->active_collection) {
376                         view_layer_dst->active_collection = layer_collection_dst;
377                 }
378
379                 layer_collection_dst = layer_collection_dst->next;
380                 layer_collection_src = layer_collection_src->next;
381         }
382 }
383
384 /**
385  * Only copy internal data of ViewLayer from source to already allocated/initialized destination.
386  *
387  * \param flag: Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more).
388  */
389 void BKE_view_layer_copy_data(
390         Scene *scene_dst, const Scene *UNUSED(scene_src),
391         ViewLayer *view_layer_dst, const ViewLayer *view_layer_src,
392         const int flag)
393 {
394         if (view_layer_dst->id_properties != NULL) {
395                 view_layer_dst->id_properties = IDP_CopyProperty_ex(view_layer_dst->id_properties, flag);
396         }
397         BKE_freestyle_config_copy(&view_layer_dst->freestyle_config, &view_layer_src->freestyle_config, flag);
398
399         view_layer_dst->stats = NULL;
400
401         /* Clear temporary data. */
402         BLI_listbase_clear(&view_layer_dst->drawdata);
403         view_layer_dst->object_bases_array = NULL;
404         view_layer_dst->object_bases_hash = NULL;
405
406         /* Copy layer collections and object bases. */
407         /* Inline 'BLI_duplicatelist' and update the active base. */
408         BLI_listbase_clear(&view_layer_dst->object_bases);
409         for (Base *base_src = view_layer_src->object_bases.first; base_src; base_src = base_src->next) {
410                 Base *base_dst = MEM_dupallocN(base_src);
411                 BLI_addtail(&view_layer_dst->object_bases, base_dst);
412                 if (view_layer_src->basact == base_src) {
413                         view_layer_dst->basact = base_dst;
414                 }
415         }
416
417         view_layer_dst->active_collection = NULL;
418         layer_collections_copy_data(
419                 view_layer_dst,
420                 view_layer_src,
421                 &view_layer_dst->layer_collections,
422                 &view_layer_src->layer_collections);
423
424         LayerCollection *lc_scene_dst = view_layer_dst->layer_collections.first;
425         lc_scene_dst->collection = scene_dst->master_collection;
426 }
427
428 void BKE_view_layer_rename(Main *bmain, Scene *scene, ViewLayer *view_layer, const char *newname)
429 {
430         char oldname[sizeof(view_layer->name)];
431
432         BLI_strncpy(oldname, view_layer->name, sizeof(view_layer->name));
433
434         BLI_strncpy_utf8(view_layer->name, newname, sizeof(view_layer->name));
435         BLI_uniquename(&scene->view_layers, view_layer, DATA_("ViewLayer"), '.', offsetof(ViewLayer, name), sizeof(view_layer->name));
436
437         if (scene->nodetree) {
438                 bNode *node;
439                 int index = BLI_findindex(&scene->view_layers, view_layer);
440
441                 for (node = scene->nodetree->nodes.first; node; node = node->next) {
442                         if (node->type == CMP_NODE_R_LAYERS && node->id == NULL) {
443                                 if (node->custom1 == index)
444                                         BLI_strncpy(node->name, view_layer->name, NODE_MAXSTR);
445                         }
446                 }
447         }
448
449         /* Fix all the animation data and windows which may link to this. */
450         BKE_animdata_fix_paths_rename_all(NULL, "view_layers", oldname, view_layer->name);
451
452         /* WM can be missing on startup. */
453         wmWindowManager *wm = bmain->wm.first;
454         if (wm) {
455                 for (wmWindow *win = wm->windows.first; win; win = win->next) {
456                         if (win->scene == scene && STREQ(win->view_layer_name, oldname)) {
457                                 STRNCPY(win->view_layer_name, view_layer->name);
458                         }
459                 }
460         }
461
462         /* Dependency graph uses view layer name based lookups. */
463         DEG_id_tag_update(&scene->id, 0);
464 }
465
466 /* LayerCollection */
467
468 /**
469  * Recursively get the collection for a given index
470  */
471 static LayerCollection *collection_from_index(ListBase *lb, const int number, int *i)
472 {
473         for (LayerCollection *lc = lb->first; lc; lc = lc->next) {
474                 if (*i == number) {
475                         return lc;
476                 }
477
478                 (*i)++;
479         }
480
481         for (LayerCollection *lc = lb->first; lc; lc = lc->next) {
482                 LayerCollection *lc_nested = collection_from_index(&lc->layer_collections, number, i);
483                 if (lc_nested) {
484                         return lc_nested;
485                 }
486         }
487         return NULL;
488 }
489
490 /**
491  * Get the collection for a given index
492  */
493 LayerCollection *BKE_layer_collection_from_index(ViewLayer *view_layer, const int index)
494 {
495         int i = 0;
496         return collection_from_index(&view_layer->layer_collections, index, &i);
497 }
498
499 /**
500  * Get the active collection
501  */
502 LayerCollection *BKE_layer_collection_get_active(ViewLayer *view_layer)
503 {
504         return view_layer->active_collection;
505 }
506
507 /*
508  * Activate collection
509  */
510 bool BKE_layer_collection_activate(ViewLayer *view_layer, LayerCollection *lc)
511 {
512         if (lc->flag & LAYER_COLLECTION_EXCLUDE) {
513                 return false;
514         }
515
516         view_layer->active_collection = lc;
517         return true;
518 }
519
520 /**
521  * Activate first parent collection
522  */
523 LayerCollection *BKE_layer_collection_activate_parent(ViewLayer *view_layer, LayerCollection *lc)
524 {
525         CollectionParent *parent = lc->collection->parents.first;
526
527         if (parent) {
528                 lc = BKE_layer_collection_first_from_scene_collection(view_layer, parent->collection);
529         }
530         else {
531                 lc = NULL;
532         }
533
534         if (lc && (lc->flag & LAYER_COLLECTION_EXCLUDE)) {
535                 /* Don't activate excluded collections. */
536                 return BKE_layer_collection_activate_parent(view_layer, lc);
537         }
538
539         if (!lc) {
540                 lc = view_layer->layer_collections.first;
541         }
542
543         view_layer->active_collection = lc;
544         return lc;
545 }
546
547 /**
548  * Recursively get the count of collections
549  */
550 static int collection_count(ListBase *lb)
551 {
552         int i = 0;
553         for (LayerCollection *lc = lb->first; lc; lc = lc->next) {
554                 i += collection_count(&lc->layer_collections) + 1;
555         }
556         return i;
557 }
558
559 /**
560  * Get the total number of collections
561  * (including all the nested collections)
562  */
563 int BKE_layer_collection_count(ViewLayer *view_layer)
564 {
565         return collection_count(&view_layer->layer_collections);
566 }
567
568 /**
569  * Recursively get the index for a given collection
570  */
571 static int index_from_collection(ListBase *lb, const LayerCollection *lc, int *i)
572 {
573         for (LayerCollection *lcol = lb->first; lcol; lcol = lcol->next) {
574                 if (lcol == lc) {
575                         return *i;
576                 }
577
578                 (*i)++;
579         }
580
581         for (LayerCollection *lcol = lb->first; lcol; lcol = lcol->next) {
582                 int i_nested = index_from_collection(&lcol->layer_collections, lc, i);
583                 if (i_nested != -1) {
584                         return i_nested;
585                 }
586         }
587         return -1;
588 }
589
590 /**
591  * Return -1 if not found
592  */
593 int BKE_layer_collection_findindex(ViewLayer *view_layer, const LayerCollection *lc)
594 {
595         int i = 0;
596         return index_from_collection(&view_layer->layer_collections, lc, &i);
597 }
598
599 /*********************************** Syncing *********************************
600  *
601  * The layer collection tree mirrors the scene collection tree. Whenever that
602  * changes we need to synchronize them so that there is a corresponding layer
603  * collection for each collection. Note that the scene collection tree can
604  * contain link or override collections, and so this is also called on .blend
605  * file load to ensure any new or removed collections are synced.
606  *
607  * The view layer also contains a list of bases for each object that exists
608  * in at least one layer collection. That list is also synchronized here, and
609  * stores state like selection. */
610
611 static short layer_collection_sync(
612         ViewLayer *view_layer, const ListBase *lb_scene,
613         ListBase *lb_layer, ListBase *new_object_bases,
614         short parent_exclude, short parent_restrict)
615 {
616         /* TODO: support recovery after removal of intermediate collections, reordering, ..
617          * For local edits we can make editing operating do the appropriate thing, but for
618          * linking we can only sync after the fact. */
619
620         /* Remove layer collections that no longer have a corresponding scene collection. */
621         for (LayerCollection *lc = lb_layer->first; lc;) {
622                 /* Note ID remap can set lc->collection to NULL when deleting collections. */
623                 LayerCollection *lc_next = lc->next;
624                 Collection *collection = (lc->collection) ?
625                         BLI_findptr(lb_scene, lc->collection, offsetof(CollectionChild, collection)) : NULL;
626
627                 if (!collection) {
628                         if (lc == view_layer->active_collection) {
629                                 view_layer->active_collection = NULL;
630                         }
631
632                         /* Free recursively. */
633                         layer_collection_free(view_layer, lc);
634                         BLI_freelinkN(lb_layer, lc);
635                 }
636
637                 lc = lc_next;
638         }
639
640         /* Add layer collections for any new scene collections, and ensure order is the same. */
641         ListBase new_lb_layer = {NULL, NULL};
642         short runtime_flag = 0;
643
644         for (const CollectionChild *child = lb_scene->first; child; child = child->next) {
645                 Collection *collection = child->collection;
646                 LayerCollection *lc = BLI_findptr(lb_layer, collection, offsetof(LayerCollection, collection));
647
648                 if (lc) {
649                         BLI_remlink(lb_layer, lc);
650                         BLI_addtail(&new_lb_layer, lc);
651                 }
652                 else {
653                         lc = layer_collection_add(&new_lb_layer, collection);
654                         lc->flag = parent_exclude;
655                 }
656
657                 /* Collection restrict is inherited. */
658                 short child_restrict = parent_restrict;
659                 if (!(collection->flag & COLLECTION_IS_MASTER)) {
660                         child_restrict |= collection->flag;
661                 }
662
663                 /* Sync child collections. */
664                 short child_runtime_flag = layer_collection_sync(
665                         view_layer, &collection->children,
666                         &lc->layer_collections, new_object_bases,
667                         lc->flag, child_restrict);
668
669                 /* Layer collection exclude is not inherited. */
670                 if (lc->flag & LAYER_COLLECTION_EXCLUDE) {
671                         lc->runtime_flag = 0;
672                         continue;
673                 }
674                 else {
675                         lc->runtime_flag = child_runtime_flag;
676                 }
677
678                 /* Sync objects, except if collection was excluded. */
679                 for (CollectionObject *cob = collection->gobject.first; cob; cob = cob->next) {
680                         if (cob->ob == NULL) {
681                                 continue;
682                         }
683
684                         void **base_p;
685                         Base  *base;
686                         if (BLI_ghash_ensure_p(view_layer->object_bases_hash, cob->ob, &base_p)) {
687                                 /* Move from old base list to new base list. Base might have already
688                                  * been moved to the new base list and the first/last test ensure that
689                                  * case also works. */
690                                 base = *base_p;
691                                 if (!ELEM(base, new_object_bases->first, new_object_bases->last)) {
692                                         BLI_remlink(&view_layer->object_bases, base);
693                                         BLI_addtail(new_object_bases, base);
694                                 }
695                         }
696                         else {
697                                 /* Create new base. */
698                                 base = object_base_new(cob->ob);
699                                 *base_p = base;
700                                 BLI_addtail(new_object_bases, base);
701                         }
702
703                         int object_restrict = base->object->restrictflag;
704
705                         if (((child_restrict & COLLECTION_RESTRICT_VIEW) == 0) &&
706                             ((object_restrict & OB_RESTRICT_VIEW) == 0))
707                         {
708                                 base->flag |= BASE_VISIBLE | BASE_ENABLED | BASE_ENABLED_VIEWPORT;
709
710                                 if (((child_restrict & COLLECTION_RESTRICT_SELECT) == 0) &&
711                                     ((object_restrict & OB_RESTRICT_SELECT) == 0))
712                                 {
713                                         base->flag |= BASE_SELECTABLE;
714                                 }
715                         }
716
717                         if (((child_restrict & COLLECTION_RESTRICT_RENDER) == 0) &&
718                             ((object_restrict & OB_RESTRICT_RENDER) == 0))
719
720                         {
721                                 base->flag |= BASE_ENABLED_RENDER;
722                         }
723
724                         /* Update runtime flags used for display and tools. */
725                         if (base->flag & BASE_VISIBLE) {
726                                 lc->runtime_flag |= LAYER_COLLECTION_HAS_ENABLED_OBJECTS;
727                         }
728
729                         if (base->flag & BASE_HIDDEN) {
730                                 view_layer->runtime_flag |= VIEW_LAYER_HAS_HIDE;
731                                 lc->runtime_flag |= LAYER_COLLECTION_HAS_HIDDEN_OBJECTS;
732                         }
733                         else if (base->flag & BASE_VISIBLE) {
734                                 lc->runtime_flag |= LAYER_COLLECTION_HAS_VISIBLE_OBJECTS;
735                         }
736
737                         /* Holdout and indirect only */
738                         if (lc->flag & LAYER_COLLECTION_HOLDOUT) {
739                                 base->flag |= BASE_HOLDOUT;
740                         }
741                         if (lc->flag & LAYER_COLLECTION_INDIRECT_ONLY) {
742                                 base->flag |= BASE_INDIRECT_ONLY;
743                         }
744
745                         lc->runtime_flag |= LAYER_COLLECTION_HAS_OBJECTS;
746                 }
747
748                 runtime_flag |= lc->runtime_flag;
749         }
750
751         /* Replace layer collection list with new one. */
752         *lb_layer = new_lb_layer;
753         BLI_assert(BLI_listbase_count(lb_scene) == BLI_listbase_count(lb_layer));
754
755         return runtime_flag;
756 }
757
758 /**
759  * Update view layer collection tree from collections used in the scene.
760  * This is used when collections are removed or added, both while editing
761  * and on file loaded in case linked data changed or went missing.
762  */
763 void BKE_layer_collection_sync(const Scene *scene, ViewLayer *view_layer)
764 {
765         if (!scene->master_collection) {
766                 /* Happens for old files that don't have versioning applied yet. */
767                 return;
768         }
769
770         /* Free cache. */
771         MEM_SAFE_FREE(view_layer->object_bases_array);
772
773         /* Create object to base hash if it does not exist yet. */
774         if (!view_layer->object_bases_hash) {
775                 view_layer_bases_hash_create(view_layer);
776         }
777
778         /* Clear visible and selectable flags to be reset. */
779         for (Base *base = view_layer->object_bases.first; base; base = base->next) {
780                 base->flag &= ~(BASE_VISIBLE |
781                                 BASE_ENABLED |
782                                 BASE_SELECTABLE |
783                                 BASE_ENABLED_VIEWPORT |
784                                 BASE_ENABLED_RENDER |
785                                 BASE_HOLDOUT |
786                                 BASE_INDIRECT_ONLY);
787         }
788
789         view_layer->runtime_flag = 0;
790
791         /* Generate new layer connections and object bases when collections changed. */
792         CollectionChild child = {NULL, NULL, scene->master_collection};
793         const ListBase collections = {&child, &child};
794         ListBase new_object_bases = {NULL, NULL};
795
796         const short parent_exclude = 0, parent_restrict = 0;
797         layer_collection_sync(
798                 view_layer, &collections,
799                 &view_layer->layer_collections, &new_object_bases,
800                 parent_exclude, parent_restrict);
801
802         /* Any remaning object bases are to be removed. */
803         for (Base *base = view_layer->object_bases.first; base; base = base->next) {
804                 if (view_layer->basact == base) {
805                         view_layer->basact = NULL;
806                 }
807
808                 if (base->object) {
809                         BLI_ghash_remove(view_layer->object_bases_hash, base->object, NULL, NULL);
810                 }
811         }
812
813         BLI_freelistN(&view_layer->object_bases);
814         view_layer->object_bases = new_object_bases;
815
816         /* Always set a valid active collection. */
817         LayerCollection *active = view_layer->active_collection;
818
819         if (active && (active->flag & LAYER_COLLECTION_EXCLUDE)) {
820                 BKE_layer_collection_activate_parent(view_layer, active);
821         }
822         else if (active == NULL) {
823                 view_layer->active_collection = view_layer->layer_collections.first;
824         }
825 }
826
827 void BKE_scene_collection_sync(const Scene *scene)
828 {
829         for (ViewLayer *view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
830                 BKE_layer_collection_sync(scene, view_layer);
831         }
832 }
833
834 void BKE_main_collection_sync(const Main *bmain)
835 {
836         /* TODO: if a single collection changed, figure out which
837          * scenes it belongs to and only update those. */
838
839         /* TODO: optimize for file load so only linked collections get checked? */
840
841         for (const Scene *scene = bmain->scene.first; scene; scene = scene->id.next) {
842                 BKE_scene_collection_sync(scene);
843         }
844 }
845
846 void BKE_main_collection_sync_remap(const Main *bmain)
847 {
848         /* On remapping of object or collection pointers free caches. */
849         /* TODO: try to make this faster */
850
851         for (const Scene *scene = bmain->scene.first; scene; scene = scene->id.next) {
852                 for (ViewLayer *view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
853                         MEM_SAFE_FREE(view_layer->object_bases_array);
854
855                         if (view_layer->object_bases_hash) {
856                                 BLI_ghash_free(view_layer->object_bases_hash, NULL, NULL);
857                                 view_layer->object_bases_hash = NULL;
858                         }
859                 }
860         }
861
862         for (Collection *collection = bmain->collection.first; collection; collection = collection->id.next) {
863                 BKE_collection_object_cache_free(collection);
864                 DEG_id_tag_update_ex((Main *)bmain, &collection->id, ID_RECALC_COPY_ON_WRITE);
865         }
866
867         BKE_main_collection_sync(bmain);
868 }
869
870 /* ---------------------------------------------------------------------- */
871
872 /**
873  * Select all the objects of this layer collection
874  *
875  * It also select the objects that are in nested collections.
876  * \note Recursive
877  */
878 bool BKE_layer_collection_objects_select(ViewLayer *view_layer, LayerCollection *lc, bool deselect)
879 {
880         if (lc->collection->flag & COLLECTION_RESTRICT_SELECT) {
881                 return false;
882         }
883
884         bool changed = false;
885
886         if (!(lc->flag & LAYER_COLLECTION_EXCLUDE)) {
887                 for (CollectionObject *cob = lc->collection->gobject.first; cob; cob = cob->next) {
888                         Base *base = BKE_view_layer_base_find(view_layer, cob->ob);
889
890                         if (base) {
891                                 if (deselect) {
892                                         if (base->flag & BASE_SELECTED) {
893                                                 base->flag &= ~BASE_SELECTED;
894                                                 changed = true;
895                                         }
896                                 }
897                                 else {
898                                         if ((base->flag & BASE_SELECTABLE) && !(base->flag & BASE_SELECTED)) {
899                                                 base->flag |= BASE_SELECTED;
900                                                 changed = true;
901                                         }
902                                 }
903                         }
904                 }
905         }
906
907         for (LayerCollection *iter = lc->layer_collections.first; iter; iter = iter->next) {
908                 changed |= BKE_layer_collection_objects_select(view_layer, iter, deselect);
909         }
910
911         return changed;
912 }
913
914 bool BKE_layer_collection_has_selected_objects(ViewLayer *view_layer, LayerCollection *lc)
915 {
916         if (lc->collection->flag & COLLECTION_RESTRICT_SELECT) {
917                 return false;
918         }
919
920         if (!(lc->flag & LAYER_COLLECTION_EXCLUDE)) {
921                 for (CollectionObject *cob = lc->collection->gobject.first; cob; cob = cob->next) {
922                         Base *base = BKE_view_layer_base_find(view_layer, cob->ob);
923
924                         if (base && (base->flag & BASE_SELECTED)) {
925                                 return true;
926                         }
927                 }
928         }
929
930         for (LayerCollection *iter = lc->layer_collections.first; iter; iter = iter->next) {
931                 if (BKE_layer_collection_has_selected_objects(view_layer, iter)) {
932                         return true;
933                 }
934         }
935
936         return false;
937 }
938
939 /* ---------------------------------------------------------------------- */
940
941 /* Update after toggling visibility of an object base. */
942 void BKE_base_set_visible(Scene *scene, ViewLayer *view_layer, Base *base, bool extend)
943 {
944         if (!extend) {
945                 /* Make only one base visible. */
946                 for (Base *other = view_layer->object_bases.first; other; other = other->next) {
947                         other->flag |= BASE_HIDDEN;
948                 }
949
950                 base->flag &= ~BASE_HIDDEN;
951         }
952         else {
953                 /* Toggle visibility of one base. */
954                 base->flag ^= BASE_HIDDEN;
955         }
956
957         BKE_layer_collection_sync(scene, view_layer);
958 }
959
960 void BKE_layer_collection_set_visible(Scene *scene, ViewLayer *view_layer, LayerCollection *lc, bool extend)
961 {
962         if (!extend) {
963                 /* Make only objects from one collection visible. */
964                 for (Base *base = view_layer->object_bases.first; base; base = base->next) {
965                         base->flag |= BASE_HIDDEN;
966                 }
967
968                 FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(lc->collection, ob)
969                 {
970                         Base *base = BLI_ghash_lookup(view_layer->object_bases_hash, ob);
971
972                         if (base) {
973                                 base->flag &= ~BASE_HIDDEN;
974                         }
975                 }
976                 FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
977
978                 BKE_layer_collection_activate(view_layer, lc);
979         }
980         else {
981                 /* Toggle visibility of objects from collection. */
982                 bool hide = (lc->runtime_flag & LAYER_COLLECTION_HAS_VISIBLE_OBJECTS) != 0;
983
984                 FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(lc->collection, ob)
985                 {
986                         Base *base = BLI_ghash_lookup(view_layer->object_bases_hash, ob);
987
988                         if (base) {
989                                 if (hide) {
990                                         base->flag |= BASE_HIDDEN;
991                                 }
992                                 else {
993                                         base->flag &= ~BASE_HIDDEN;
994                                 }
995                         }
996                 }
997                 FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
998         }
999
1000         BKE_layer_collection_sync(scene, view_layer);
1001 }
1002
1003 /* ---------------------------------------------------------------------- */
1004
1005 static LayerCollection *find_layer_collection_by_scene_collection(LayerCollection *lc, const Collection *collection)
1006 {
1007         if (lc->collection == collection) {
1008                 return lc;
1009         }
1010
1011         for (LayerCollection *nlc = lc->layer_collections.first; nlc; nlc = nlc->next) {
1012                 LayerCollection *found = find_layer_collection_by_scene_collection(nlc, collection);
1013                 if (found) {
1014                         return found;
1015                 }
1016         }
1017         return NULL;
1018 }
1019
1020 /**
1021  * Return the first matching LayerCollection in the ViewLayer for the Collection.
1022  */
1023 LayerCollection *BKE_layer_collection_first_from_scene_collection(ViewLayer *view_layer, const Collection *collection)
1024 {
1025         for (LayerCollection *layer_collection = view_layer->layer_collections.first;
1026              layer_collection != NULL;
1027              layer_collection = layer_collection->next)
1028         {
1029                 LayerCollection *found = find_layer_collection_by_scene_collection(layer_collection, collection);
1030
1031                 if (found != NULL) {
1032                         return found;
1033                 }
1034         }
1035         return NULL;
1036 }
1037
1038 /**
1039  * See if view layer has the scene collection linked directly, or indirectly (nested)
1040  */
1041 bool BKE_view_layer_has_collection(ViewLayer *view_layer, const Collection *collection)
1042 {
1043         return BKE_layer_collection_first_from_scene_collection(view_layer, collection) != NULL;
1044 }
1045
1046 /**
1047  * See if the object is in any of the scene layers of the scene
1048  */
1049 bool BKE_scene_has_object(Scene *scene, Object *ob)
1050 {
1051         for (ViewLayer *view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
1052                 Base *base = BKE_view_layer_base_find(view_layer, ob);
1053                 if (base) {
1054                         return true;
1055                 }
1056         }
1057         return false;
1058 }
1059
1060 /* ---------------------------------------------------------------------- */
1061 /* Override */
1062
1063 /**
1064  * Add a new datablock override
1065  */
1066 void BKE_override_view_layer_datablock_add(
1067         ViewLayer *view_layer, int id_type, const char *data_path, const ID *owner_id)
1068 {
1069         UNUSED_VARS(view_layer, id_type, data_path, owner_id);
1070         TODO_LAYER_OVERRIDE;
1071 }
1072
1073 /**
1074  * Add a new int override
1075  */
1076 void BKE_override_view_layer_int_add(
1077         ViewLayer *view_layer, int id_type, const char *data_path, const int value)
1078 {
1079         UNUSED_VARS(view_layer, id_type, data_path, value);
1080         TODO_LAYER_OVERRIDE;
1081 }
1082
1083 /**
1084  * Add a new boolean override
1085  */
1086 void BKE_override_layer_collection_boolean_add(
1087         struct LayerCollection *layer_collection, int id_type, const char *data_path, const bool value)
1088 {
1089         UNUSED_VARS(layer_collection, id_type, data_path, value);
1090         TODO_LAYER_OVERRIDE;
1091 }
1092
1093 /** \} */
1094
1095 /* Iterators */
1096
1097 /* -------------------------------------------------------------------- */
1098 /** \name Private Iterator Helpers
1099  * \{ */
1100
1101 typedef struct LayerObjectBaseIteratorData {
1102         View3D *v3d;
1103         Base *base;
1104 } LayerObjectBaseIteratorData;
1105
1106 static bool object_bases_iterator_is_valid_ex(View3D *v3d, Base *base, const int flag)
1107 {
1108         if (v3d != NULL) {
1109                 BLI_assert(v3d->spacetype == SPACE_VIEW3D);
1110                 if ((v3d->object_type_exclude_viewport & (1 << base->object->type)) != 0) {
1111                         return false;
1112                 }
1113
1114                 if (v3d->localvd && ((base->local_view_bits & v3d->local_view_uuid) == 0)) {
1115                         return false;
1116                 }
1117         }
1118
1119         if ((base->flag & flag) == 0) {
1120                 return false;
1121         }
1122
1123         return true;
1124 }
1125
1126 static bool object_bases_iterator_is_valid(View3D *v3d, Base *base)
1127 {
1128         return object_bases_iterator_is_valid_ex(v3d, base, ~(0));
1129 }
1130
1131 static void object_bases_iterator_begin(BLI_Iterator *iter, void *data_in_v, const int flag)
1132 {
1133         ObjectsVisibleIteratorData *data_in = data_in_v;
1134         ViewLayer *view_layer = data_in->view_layer;
1135         View3D *v3d = data_in->v3d;
1136         Base *base = view_layer->object_bases.first;
1137
1138         /* when there are no objects */
1139         if (base ==  NULL) {
1140                 iter->data = NULL;
1141                 iter->valid = false;
1142                 return;
1143         }
1144
1145         LayerObjectBaseIteratorData *data = MEM_callocN(sizeof(LayerObjectBaseIteratorData), __func__);
1146         iter->data = data;
1147
1148         data->v3d = v3d;
1149         data->base = base;
1150
1151         if (object_bases_iterator_is_valid_ex(v3d, base, flag) == false) {
1152                 object_bases_iterator_next(iter, flag);
1153         }
1154         else {
1155                 iter->current = base;
1156         }
1157 }
1158
1159 static void object_bases_iterator_next(BLI_Iterator *iter, const int flag)
1160 {
1161         LayerObjectBaseIteratorData *data = iter->data;
1162         Base *base = data->base->next;
1163
1164         while (base) {
1165                 if (object_bases_iterator_is_valid_ex(data->v3d, base, flag)) {
1166                         iter->current = base;
1167                         data->base = base;
1168                         return;
1169                 }
1170                 base = base->next;
1171         }
1172
1173         iter->valid = false;
1174 }
1175
1176 static void object_bases_iterator_end(BLI_Iterator *iter)
1177 {
1178         MEM_SAFE_FREE(iter->data);
1179 }
1180
1181 static void objects_iterator_begin(BLI_Iterator *iter, void *data_in, const int flag)
1182 {
1183         object_bases_iterator_begin(iter, data_in, flag);
1184
1185         if (iter->valid) {
1186                 iter->current = ((Base *)iter->current)->object;
1187         }
1188 }
1189
1190 static void objects_iterator_next(BLI_Iterator *iter, const int flag)
1191 {
1192         object_bases_iterator_next(iter, flag);
1193
1194         if (iter->valid) {
1195                 iter->current = ((Base *)iter->current)->object;
1196         }
1197 }
1198
1199 static void objects_iterator_end(BLI_Iterator *iter)
1200 {
1201         object_bases_iterator_end(iter);
1202 }
1203
1204 /* -------------------------------------------------------------------- */
1205 /** \name BKE_view_layer_selected_objects_iterator
1206  * See: #FOREACH_SELECTED_OBJECT_BEGIN
1207  * \{ */
1208
1209 void BKE_view_layer_selected_objects_iterator_begin(BLI_Iterator *iter, void *data_in)
1210 {
1211         objects_iterator_begin(iter, data_in, BASE_SELECTED);
1212 }
1213
1214 void BKE_view_layer_selected_objects_iterator_next(BLI_Iterator *iter)
1215 {
1216         objects_iterator_next(iter, BASE_SELECTED);
1217 }
1218
1219 void BKE_view_layer_selected_objects_iterator_end(BLI_Iterator *iter)
1220 {
1221         objects_iterator_end(iter);
1222 }
1223
1224 /** \} */
1225
1226 /* -------------------------------------------------------------------- */
1227 /** \name BKE_view_layer_visible_objects_iterator
1228  * \{ */
1229
1230 void BKE_view_layer_visible_objects_iterator_begin(BLI_Iterator *iter, void *data_in)
1231 {
1232         objects_iterator_begin(iter, data_in, BASE_VISIBLE);
1233 }
1234
1235 void BKE_view_layer_visible_objects_iterator_next(BLI_Iterator *iter)
1236 {
1237         objects_iterator_next(iter, BASE_VISIBLE);
1238 }
1239
1240 void BKE_view_layer_visible_objects_iterator_end(BLI_Iterator *iter)
1241 {
1242         objects_iterator_end(iter);
1243 }
1244
1245 /** \} */
1246
1247 /* -------------------------------------------------------------------- */
1248 /** \name BKE_view_layer_selected_editable_objects_iterator
1249  * \{ */
1250
1251 void BKE_view_layer_selected_editable_objects_iterator_begin(BLI_Iterator *iter, void *data_in)
1252 {
1253         objects_iterator_begin(iter, data_in, BASE_SELECTED);
1254         if (iter->valid) {
1255                 if (BKE_object_is_libdata((Object *)iter->current) == false) {
1256                         // First object is valid (selectable and not libdata) -> all good.
1257                         return;
1258                 }
1259                 else {
1260                         // Object is selectable but not editable -> search for another one.
1261                         BKE_view_layer_selected_editable_objects_iterator_next(iter);
1262                 }
1263         }
1264 }
1265
1266 void BKE_view_layer_selected_editable_objects_iterator_next(BLI_Iterator *iter)
1267 {
1268         // Search while there are objects and the one we have is not editable (editable = not libdata).
1269         do {
1270                 objects_iterator_next(iter, BASE_SELECTED);
1271         } while (iter->valid && BKE_object_is_libdata((Object *)iter->current) != false);
1272 }
1273
1274 void BKE_view_layer_selected_editable_objects_iterator_end(BLI_Iterator *iter)
1275 {
1276         objects_iterator_end(iter);
1277 }
1278
1279 /** \} */
1280
1281 /* -------------------------------------------------------------------- */
1282 /** \name BKE_view_layer_selected_bases_iterator
1283  * \{ */
1284
1285 void BKE_view_layer_selected_bases_iterator_begin(BLI_Iterator *iter, void *data_in)
1286 {
1287         objects_iterator_begin(iter, data_in, BASE_SELECTED);
1288 }
1289
1290 void BKE_view_layer_selected_bases_iterator_next(BLI_Iterator *iter)
1291 {
1292         object_bases_iterator_next(iter, BASE_SELECTED);
1293 }
1294
1295 void BKE_view_layer_selected_bases_iterator_end(BLI_Iterator *iter)
1296 {
1297         object_bases_iterator_end(iter);
1298 }
1299
1300 /** \} */
1301
1302 /* -------------------------------------------------------------------- */
1303 /** \name BKE_view_layer_visible_bases_iterator
1304  * \{ */
1305
1306 void BKE_view_layer_visible_bases_iterator_begin(BLI_Iterator *iter, void *data_in)
1307 {
1308         object_bases_iterator_begin(iter, data_in, BASE_VISIBLE);
1309 }
1310
1311 void BKE_view_layer_visible_bases_iterator_next(BLI_Iterator *iter)
1312 {
1313         object_bases_iterator_next(iter, BASE_VISIBLE);
1314 }
1315
1316 void BKE_view_layer_visible_bases_iterator_end(BLI_Iterator *iter)
1317 {
1318         object_bases_iterator_end(iter);
1319 }
1320
1321 /** \} */
1322
1323 /* -------------------------------------------------------------------- */
1324 /** \name BKE_view_layer_renderable_objects_iterator
1325  * \{ */
1326
1327 void BKE_view_layer_renderable_objects_iterator_begin(BLI_Iterator *iter, void *data_in)
1328 {
1329         struct ObjectsRenderableIteratorData *data = data_in;
1330
1331         /* Tag objects to prevent going over the same object twice. */
1332         for (Scene *scene = data->scene; scene; scene = scene->set) {
1333                 for (ViewLayer *view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
1334                         for (Base *base = view_layer->object_bases.first; base; base = base->next) {
1335                                  base->object->id.flag |= LIB_TAG_DOIT;
1336                         }
1337                 }
1338         }
1339
1340         ViewLayer *view_layer = data->scene->view_layers.first;
1341         data->iter.view_layer = view_layer;
1342
1343         data->base_temp.next = view_layer->object_bases.first;
1344         data->iter.base = &data->base_temp;
1345
1346         data->iter.set = NULL;
1347
1348         iter->data = data_in;
1349         BKE_view_layer_renderable_objects_iterator_next(iter);
1350 }
1351
1352 void BKE_view_layer_renderable_objects_iterator_next(BLI_Iterator *iter)
1353 {
1354         /* Set it early in case we need to exit and we are running from within a loop. */
1355         iter->skip = true;
1356
1357         struct ObjectsRenderableIteratorData *data = iter->data;
1358         Base *base = data->iter.base->next;
1359
1360         /* There is still a base in the current scene layer. */
1361         if (base != NULL) {
1362                 Object *ob = base->object;
1363
1364                 /* We need to set the iter.base even if the rest fail otherwise
1365                  * we keep checking the exactly same base over and over again. */
1366                 data->iter.base = base;
1367
1368                 if (ob->id.flag & LIB_TAG_DOIT) {
1369                         ob->id.flag &= ~LIB_TAG_DOIT;
1370
1371                         if ((base->flag & BASE_VISIBLE) != 0) {
1372                                 iter->skip = false;
1373                                 iter->current = ob;
1374                         }
1375                 }
1376                 return;
1377         }
1378
1379         /* Time to go to the next scene layer. */
1380         if (data->iter.set == NULL) {
1381                 while ((data->iter.view_layer = data->iter.view_layer->next)) {
1382                         ViewLayer *view_layer = data->iter.view_layer;
1383                         if (view_layer->flag & VIEW_LAYER_RENDER) {
1384                                 data->base_temp.next = view_layer->object_bases.first;
1385                                 data->iter.base = &data->base_temp;
1386                                 return;
1387                         }
1388                 }
1389
1390                 /* Setup the "set" for the next iteration. */
1391                 data->scene_temp.set = data->scene;
1392                 data->iter.set = &data->scene_temp;
1393                 return;
1394         }
1395
1396         /* Look for an object in the next set. */
1397         while ((data->iter.set = data->iter.set->set)) {
1398                 ViewLayer *view_layer = BKE_view_layer_default_render(data->iter.set);
1399                 data->base_temp.next = view_layer->object_bases.first;
1400                 data->iter.base = &data->base_temp;
1401                 return;
1402         }
1403
1404         iter->valid = false;
1405 }
1406
1407 void BKE_view_layer_renderable_objects_iterator_end(BLI_Iterator *UNUSED(iter))
1408 {
1409         /* Do nothing - iter->data was static allocated, we can't free it. */
1410 }
1411
1412 /** \} */
1413
1414 /* -------------------------------------------------------------------- */
1415 /** \name BKE_view_layer_bases_in_mode_iterator
1416  * \{ */
1417
1418 static bool base_is_in_mode(struct ObjectsInModeIteratorData *data, Base *base)
1419 {
1420         return (base->object->type == data->object_type) && (base->object->mode & data->object_mode) != 0;
1421 }
1422
1423 void BKE_view_layer_bases_in_mode_iterator_begin(BLI_Iterator *iter, void *data_in)
1424 {
1425         struct ObjectsInModeIteratorData *data = data_in;
1426         Base *base = data->base_active;
1427
1428         /* when there are no objects */
1429         if (base == NULL) {
1430                 iter->valid = false;
1431                 return;
1432         }
1433         iter->data = data_in;
1434         iter->current = base;
1435
1436         /* default type is active object type */
1437         if (data->object_type < 0) {
1438                 data->object_type = base->object->type;
1439         }
1440
1441         if (object_bases_iterator_is_valid(data->v3d, base) == false || !base_is_in_mode(data, base)) {
1442                 BKE_view_layer_bases_in_mode_iterator_next(iter);
1443         }
1444 }
1445
1446 void BKE_view_layer_bases_in_mode_iterator_next(BLI_Iterator *iter)
1447 {
1448         struct ObjectsInModeIteratorData *data = iter->data;
1449         Base *base = iter->current;
1450
1451         if (base == data->base_active) {
1452                 /* first step */
1453                 base = data->view_layer->object_bases.first;
1454                 if (base == data->base_active) {
1455                         base = base->next;
1456                 }
1457         }
1458         else {
1459                 base = base->next;
1460         }
1461
1462         while (base) {
1463                 if ((base != data->base_active) &&
1464                     base_is_in_mode(data, base) &&
1465                     object_bases_iterator_is_valid(data->v3d, base))
1466                 {
1467                         iter->current = base;
1468                         return;
1469                 }
1470                 base = base->next;
1471         }
1472         iter->valid = false;
1473 }
1474
1475 void BKE_view_layer_bases_in_mode_iterator_end(BLI_Iterator *UNUSED(iter))
1476 {
1477         /* do nothing */
1478 }
1479
1480 /** \} */
1481
1482 /* Evaluation  */
1483
1484 void BKE_layer_eval_view_layer(
1485         struct Depsgraph *depsgraph,
1486         struct Scene *UNUSED(scene),
1487         ViewLayer *view_layer)
1488 {
1489         DEG_debug_print_eval(depsgraph, __func__, view_layer->name, view_layer);
1490
1491         /* Visibility based on depsgraph mode. */
1492         const eEvaluationMode mode = DEG_get_mode(depsgraph);
1493         const int base_visible_flag = (mode == DAG_EVAL_VIEWPORT)
1494                 ? BASE_ENABLED_VIEWPORT
1495                 : BASE_ENABLED_RENDER;
1496         /* Create array of bases, for fast index-based lookup. */
1497         const int num_object_bases = BLI_listbase_count(&view_layer->object_bases);
1498         MEM_SAFE_FREE(view_layer->object_bases_array);
1499         view_layer->object_bases_array = MEM_malloc_arrayN(
1500                 num_object_bases, sizeof(Base *), "view_layer->object_bases_array");
1501         int base_index = 0;
1502         for (Base *base = view_layer->object_bases.first; base; base = base->next) {
1503                 /* Compute visibility for depsgraph evaluation mode. */
1504                 if (base->flag & base_visible_flag) {
1505                         base->flag |= BASE_ENABLED | BASE_VISIBLE;
1506                         if (mode == DAG_EVAL_VIEWPORT && (base->flag & BASE_HIDDEN)) {
1507                                 base->flag &= ~BASE_VISIBLE;
1508                         }
1509                 }
1510                 else {
1511                         base->flag &= ~(BASE_ENABLED | BASE_VISIBLE | BASE_SELECTABLE);
1512                 }
1513                 /* If base is not selectabled, clear select. */
1514                 if ((base->flag & BASE_SELECTABLE) == 0) {
1515                         base->flag &= ~BASE_SELECTED;
1516                 }
1517                 view_layer->object_bases_array[base_index++] = base;
1518         }
1519         /* Flush back base flag to the original view layer for editing. */
1520         if (view_layer == DEG_get_evaluated_view_layer(depsgraph)) {
1521                 ViewLayer *view_layer_orig = DEG_get_input_view_layer(depsgraph);
1522                 Base *base_orig = view_layer_orig->object_bases.first;
1523                 const Base *base_eval = view_layer->object_bases.first;
1524                 while (base_orig != NULL) {
1525                         if (base_orig->flag & base_visible_flag) {
1526                                 base_orig->flag = base_eval->flag;
1527                                 base_eval = base_eval->next;
1528                         }
1529                         base_orig = base_orig->next;
1530                 }
1531         }
1532 }
1533
1534 void BKE_layer_eval_view_layer_indexed(
1535         struct Depsgraph *depsgraph,
1536         struct Scene *scene,
1537         int view_layer_index)
1538 {
1539         BLI_assert(view_layer_index >= 0);
1540         ViewLayer *view_layer = BLI_findlink(&scene->view_layers, view_layer_index);
1541         BLI_assert(view_layer != NULL);
1542         BKE_layer_eval_view_layer(depsgraph, scene, view_layer);
1543 }