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