Cleanup: comments (long lines) in 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,
1104  * but it may change it for the collection itself.
1105  *
1106  * Return true if depsgraph needs update.
1107  */
1108 bool BKE_layer_collection_set_visible(ViewLayer *view_layer,
1109                                       LayerCollection *lc,
1110                                       const bool visible,
1111                                       const bool hierarchy)
1112 {
1113   bool depsgraph_changed = false;
1114
1115   if (visible && (!ID_IS_LINKED(lc->collection)) &&
1116       ((lc->collection->flag & COLLECTION_RESTRICT_VIEW) != 0)) {
1117     lc->collection->flag &= ~COLLECTION_RESTRICT_VIEW;
1118     depsgraph_changed = true;
1119   }
1120
1121   if (hierarchy) {
1122     if (visible) {
1123       layer_collection_flag_unset_recursive(lc, LAYER_COLLECTION_RESTRICT_VIEW);
1124       layer_collection_bases_show_recursive(view_layer, lc);
1125     }
1126     else {
1127       layer_collection_flag_set_recursive(lc, LAYER_COLLECTION_RESTRICT_VIEW);
1128       layer_collection_bases_hide_recursive(view_layer, lc);
1129     }
1130   }
1131   else {
1132     if (visible) {
1133       lc->flag &= ~LAYER_COLLECTION_RESTRICT_VIEW;
1134     }
1135     else {
1136       lc->flag |= LAYER_COLLECTION_RESTRICT_VIEW;
1137     }
1138   }
1139   return depsgraph_changed;
1140 }
1141
1142 /* ---------------------------------------------------------------------- */
1143
1144 static LayerCollection *find_layer_collection_by_scene_collection(LayerCollection *lc,
1145                                                                   const Collection *collection)
1146 {
1147   if (lc->collection == collection) {
1148     return lc;
1149   }
1150
1151   for (LayerCollection *nlc = lc->layer_collections.first; nlc; nlc = nlc->next) {
1152     LayerCollection *found = find_layer_collection_by_scene_collection(nlc, collection);
1153     if (found) {
1154       return found;
1155     }
1156   }
1157   return NULL;
1158 }
1159
1160 /**
1161  * Return the first matching LayerCollection in the ViewLayer for the Collection.
1162  */
1163 LayerCollection *BKE_layer_collection_first_from_scene_collection(ViewLayer *view_layer,
1164                                                                   const Collection *collection)
1165 {
1166   for (LayerCollection *layer_collection = view_layer->layer_collections.first;
1167        layer_collection != NULL;
1168        layer_collection = layer_collection->next) {
1169     LayerCollection *found = find_layer_collection_by_scene_collection(layer_collection,
1170                                                                        collection);
1171
1172     if (found != NULL) {
1173       return found;
1174     }
1175   }
1176   return NULL;
1177 }
1178
1179 /**
1180  * See if view layer has the scene collection linked directly, or indirectly (nested)
1181  */
1182 bool BKE_view_layer_has_collection(ViewLayer *view_layer, const Collection *collection)
1183 {
1184   return BKE_layer_collection_first_from_scene_collection(view_layer, collection) != NULL;
1185 }
1186
1187 /**
1188  * See if the object is in any of the scene layers of the scene
1189  */
1190 bool BKE_scene_has_object(Scene *scene, Object *ob)
1191 {
1192   for (ViewLayer *view_layer = scene->view_layers.first; view_layer;
1193        view_layer = view_layer->next) {
1194     Base *base = BKE_view_layer_base_find(view_layer, ob);
1195     if (base) {
1196       return true;
1197     }
1198   }
1199   return false;
1200 }
1201
1202 /** \} */
1203
1204 /* Iterators */
1205
1206 /* -------------------------------------------------------------------- */
1207 /** \name Private Iterator Helpers
1208  * \{ */
1209
1210 typedef struct LayerObjectBaseIteratorData {
1211   View3D *v3d;
1212   Base *base;
1213 } LayerObjectBaseIteratorData;
1214
1215 static bool object_bases_iterator_is_valid(View3D *v3d, Base *base, const int flag)
1216 {
1217   BLI_assert((v3d == NULL) || (v3d->spacetype == SPACE_VIEW3D));
1218
1219   /* Any flag satisfies the condition. */
1220   if (flag == ~0) {
1221     return (base->flag != 0);
1222   }
1223
1224   /* Flags may be more than one flag, so we can't check != 0. */
1225   return BASE_VISIBLE(v3d, base) && ((base->flag & flag) == flag);
1226 }
1227
1228 static void object_bases_iterator_begin(BLI_Iterator *iter, void *data_in_v, const int flag)
1229 {
1230   ObjectsVisibleIteratorData *data_in = data_in_v;
1231   ViewLayer *view_layer = data_in->view_layer;
1232   View3D *v3d = data_in->v3d;
1233   Base *base = view_layer->object_bases.first;
1234
1235   /* when there are no objects */
1236   if (base == NULL) {
1237     iter->data = NULL;
1238     iter->valid = false;
1239     return;
1240   }
1241
1242   LayerObjectBaseIteratorData *data = MEM_callocN(sizeof(LayerObjectBaseIteratorData), __func__);
1243   iter->data = data;
1244
1245   data->v3d = v3d;
1246   data->base = base;
1247
1248   if (object_bases_iterator_is_valid(v3d, base, flag) == false) {
1249     object_bases_iterator_next(iter, flag);
1250   }
1251   else {
1252     iter->current = base;
1253   }
1254 }
1255
1256 static void object_bases_iterator_next(BLI_Iterator *iter, const int flag)
1257 {
1258   LayerObjectBaseIteratorData *data = iter->data;
1259   Base *base = data->base->next;
1260
1261   while (base) {
1262     if (object_bases_iterator_is_valid(data->v3d, base, flag)) {
1263       iter->current = base;
1264       data->base = base;
1265       return;
1266     }
1267     base = base->next;
1268   }
1269
1270   iter->valid = false;
1271 }
1272
1273 static void object_bases_iterator_end(BLI_Iterator *iter)
1274 {
1275   MEM_SAFE_FREE(iter->data);
1276 }
1277
1278 static void objects_iterator_begin(BLI_Iterator *iter, void *data_in, const int flag)
1279 {
1280   object_bases_iterator_begin(iter, data_in, flag);
1281
1282   if (iter->valid) {
1283     iter->current = ((Base *)iter->current)->object;
1284   }
1285 }
1286
1287 static void objects_iterator_next(BLI_Iterator *iter, const int flag)
1288 {
1289   object_bases_iterator_next(iter, flag);
1290
1291   if (iter->valid) {
1292     iter->current = ((Base *)iter->current)->object;
1293   }
1294 }
1295
1296 static void objects_iterator_end(BLI_Iterator *iter)
1297 {
1298   object_bases_iterator_end(iter);
1299 }
1300
1301 /* -------------------------------------------------------------------- */
1302 /** \name BKE_view_layer_selected_objects_iterator
1303  * See: #FOREACH_SELECTED_OBJECT_BEGIN
1304  * \{ */
1305
1306 void BKE_view_layer_selected_objects_iterator_begin(BLI_Iterator *iter, void *data_in)
1307 {
1308   objects_iterator_begin(iter, data_in, BASE_VISIBLE | BASE_SELECTED);
1309 }
1310
1311 void BKE_view_layer_selected_objects_iterator_next(BLI_Iterator *iter)
1312 {
1313   objects_iterator_next(iter, BASE_VISIBLE | BASE_SELECTED);
1314 }
1315
1316 void BKE_view_layer_selected_objects_iterator_end(BLI_Iterator *iter)
1317 {
1318   objects_iterator_end(iter);
1319 }
1320
1321 /** \} */
1322
1323 /* -------------------------------------------------------------------- */
1324 /** \name BKE_view_layer_visible_objects_iterator
1325  * \{ */
1326
1327 void BKE_view_layer_visible_objects_iterator_begin(BLI_Iterator *iter, void *data_in)
1328 {
1329   objects_iterator_begin(iter, data_in, 0);
1330 }
1331
1332 void BKE_view_layer_visible_objects_iterator_next(BLI_Iterator *iter)
1333 {
1334   objects_iterator_next(iter, 0);
1335 }
1336
1337 void BKE_view_layer_visible_objects_iterator_end(BLI_Iterator *iter)
1338 {
1339   objects_iterator_end(iter);
1340 }
1341
1342 /** \} */
1343
1344 /* -------------------------------------------------------------------- */
1345 /** \name BKE_view_layer_selected_editable_objects_iterator
1346  * \{ */
1347
1348 void BKE_view_layer_selected_editable_objects_iterator_begin(BLI_Iterator *iter, void *data_in)
1349 {
1350   objects_iterator_begin(iter, data_in, BASE_VISIBLE | BASE_SELECTED);
1351   if (iter->valid) {
1352     if (BKE_object_is_libdata((Object *)iter->current) == false) {
1353       // First object is valid (selectable and not libdata) -> all good.
1354       return;
1355     }
1356     else {
1357       // Object is selectable but not editable -> search for another one.
1358       BKE_view_layer_selected_editable_objects_iterator_next(iter);
1359     }
1360   }
1361 }
1362
1363 void BKE_view_layer_selected_editable_objects_iterator_next(BLI_Iterator *iter)
1364 {
1365   // Search while there are objects and the one we have is not editable (editable = not libdata).
1366   do {
1367     objects_iterator_next(iter, BASE_VISIBLE | BASE_SELECTED);
1368   } while (iter->valid && BKE_object_is_libdata((Object *)iter->current) != false);
1369 }
1370
1371 void BKE_view_layer_selected_editable_objects_iterator_end(BLI_Iterator *iter)
1372 {
1373   objects_iterator_end(iter);
1374 }
1375
1376 /** \} */
1377
1378 /* -------------------------------------------------------------------- */
1379 /** \name BKE_view_layer_selected_bases_iterator
1380  * \{ */
1381
1382 void BKE_view_layer_selected_bases_iterator_begin(BLI_Iterator *iter, void *data_in)
1383 {
1384   objects_iterator_begin(iter, data_in, BASE_VISIBLE | BASE_SELECTED);
1385 }
1386
1387 void BKE_view_layer_selected_bases_iterator_next(BLI_Iterator *iter)
1388 {
1389   object_bases_iterator_next(iter, BASE_VISIBLE | BASE_SELECTED);
1390 }
1391
1392 void BKE_view_layer_selected_bases_iterator_end(BLI_Iterator *iter)
1393 {
1394   object_bases_iterator_end(iter);
1395 }
1396
1397 /** \} */
1398
1399 /* -------------------------------------------------------------------- */
1400 /** \name BKE_view_layer_visible_bases_iterator
1401  * \{ */
1402
1403 void BKE_view_layer_visible_bases_iterator_begin(BLI_Iterator *iter, void *data_in)
1404 {
1405   object_bases_iterator_begin(iter, data_in, 0);
1406 }
1407
1408 void BKE_view_layer_visible_bases_iterator_next(BLI_Iterator *iter)
1409 {
1410   object_bases_iterator_next(iter, 0);
1411 }
1412
1413 void BKE_view_layer_visible_bases_iterator_end(BLI_Iterator *iter)
1414 {
1415   object_bases_iterator_end(iter);
1416 }
1417
1418 /** \} */
1419
1420 /* -------------------------------------------------------------------- */
1421 /** \name BKE_view_layer_bases_in_mode_iterator
1422  * \{ */
1423
1424 static bool base_is_in_mode(struct ObjectsInModeIteratorData *data, Base *base)
1425 {
1426   return BASE_VISIBLE(data->v3d, base) && (base->object->type == data->object_type) &&
1427          (base->object->mode & data->object_mode) != 0;
1428 }
1429
1430 void BKE_view_layer_bases_in_mode_iterator_begin(BLI_Iterator *iter, void *data_in)
1431 {
1432   struct ObjectsInModeIteratorData *data = data_in;
1433   Base *base = data->base_active;
1434
1435   /* when there are no objects */
1436   if (base == NULL) {
1437     iter->valid = false;
1438     return;
1439   }
1440   iter->data = data_in;
1441   iter->current = base;
1442
1443   /* default type is active object type */
1444   if (data->object_type < 0) {
1445     data->object_type = base->object->type;
1446   }
1447
1448   if (!base_is_in_mode(data, base)) {
1449     BKE_view_layer_bases_in_mode_iterator_next(iter);
1450   }
1451 }
1452
1453 void BKE_view_layer_bases_in_mode_iterator_next(BLI_Iterator *iter)
1454 {
1455   struct ObjectsInModeIteratorData *data = iter->data;
1456   Base *base = iter->current;
1457
1458   if (base == data->base_active) {
1459     /* first step */
1460     base = data->view_layer->object_bases.first;
1461     if (base == data->base_active) {
1462       base = base->next;
1463     }
1464   }
1465   else {
1466     base = base->next;
1467   }
1468
1469   while (base) {
1470     if ((base != data->base_active) && base_is_in_mode(data, base)) {
1471       iter->current = base;
1472       return;
1473     }
1474     base = base->next;
1475   }
1476   iter->valid = false;
1477 }
1478
1479 void BKE_view_layer_bases_in_mode_iterator_end(BLI_Iterator *UNUSED(iter))
1480 {
1481   /* do nothing */
1482 }
1483
1484 /** \} */
1485
1486 /* Evaluation  */
1487
1488 /* Applies object's restrict flags on top of flags coming from the collection
1489  * and stores those in base->flag. BASE_VISIBLE is based on viewport visibility. */
1490 void BKE_base_eval_flags(Base *base)
1491 {
1492   /* Apply collection flags. */
1493   base->flag &= ~g_base_collection_flags;
1494   base->flag |= (base->flag_from_collection & g_base_collection_flags);
1495
1496   /* Apply object restrictions. */
1497   const int object_restrict = base->object->restrictflag;
1498   if (object_restrict & OB_RESTRICT_VIEW) {
1499     base->flag &= ~BASE_ENABLED_VIEWPORT;
1500   }
1501   if (object_restrict & OB_RESTRICT_RENDER) {
1502     base->flag &= ~BASE_ENABLED_RENDER;
1503   }
1504   if (object_restrict & OB_RESTRICT_SELECT) {
1505     base->flag &= ~BASE_SELECTABLE;
1506   }
1507
1508   /* Apply viewport visibility by default. The dependency graph for render
1509    * can change these again, but for tools we always want the viewport
1510    * visibility to be in sync regardless if depsgraph was evaluated. */
1511   if (!(base->flag & BASE_ENABLED_VIEWPORT) || (base->flag & BASE_HIDDEN)) {
1512     base->flag &= ~(BASE_VISIBLE | BASE_SELECTABLE);
1513   }
1514
1515   /* Deselect unselectable objects. */
1516   if (!(base->flag & BASE_SELECTABLE)) {
1517     base->flag &= ~BASE_SELECTED;
1518   }
1519 }
1520
1521 static void layer_eval_view_layer(struct Depsgraph *depsgraph,
1522                                   struct Scene *UNUSED(scene),
1523                                   ViewLayer *view_layer)
1524 {
1525   DEG_debug_print_eval(depsgraph, __func__, view_layer->name, view_layer);
1526
1527   /* Create array of bases, for fast index-based lookup. */
1528   const int num_object_bases = BLI_listbase_count(&view_layer->object_bases);
1529   MEM_SAFE_FREE(view_layer->object_bases_array);
1530   view_layer->object_bases_array = MEM_malloc_arrayN(
1531       num_object_bases, sizeof(Base *), "view_layer->object_bases_array");
1532   int base_index = 0;
1533   for (Base *base = view_layer->object_bases.first; base; base = base->next) {
1534     view_layer->object_bases_array[base_index++] = base;
1535   }
1536 }
1537
1538 void BKE_layer_eval_view_layer_indexed(struct Depsgraph *depsgraph,
1539                                        struct Scene *scene,
1540                                        int view_layer_index)
1541 {
1542   BLI_assert(view_layer_index >= 0);
1543   ViewLayer *view_layer = BLI_findlink(&scene->view_layers, view_layer_index);
1544   BLI_assert(view_layer != NULL);
1545   layer_eval_view_layer(depsgraph, scene, view_layer);
1546 }