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