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