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