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