Fix T55126: COW problem: there is an active object even if unselectable
[blender.git] / source / blender / blenkernel / intern / layer.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Dalai Felinto
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/blenkernel/intern/layer.c
24  *  \ingroup bke
25  */
26
27 #include <string.h>
28
29 #include "BLI_array.h"
30 #include "BLI_listbase.h"
31 #include "BLI_string.h"
32 #include "BLI_string_utf8.h"
33 #include "BLI_string_utils.h"
34 #include "BLI_threads.h"
35 #include "BLT_translation.h"
36
37 #include "BKE_animsys.h"
38 #include "BKE_collection.h"
39 #include "BKE_freestyle.h"
40 #include "BKE_global.h"
41 #include "BKE_idprop.h"
42 #include "BKE_layer.h"
43 #include "BKE_main.h"
44 #include "BKE_node.h"
45 #include "BKE_workspace.h"
46 #include "BKE_object.h"
47
48 #include "DNA_group_types.h"
49 #include "DNA_ID.h"
50 #include "DNA_layer_types.h"
51 #include "DNA_object_types.h"
52 #include "DNA_node_types.h"
53 #include "DNA_scene_types.h"
54 #include "DNA_windowmanager_types.h"
55 #include "DNA_workspace_types.h"
56
57 #include "DEG_depsgraph.h"
58 #include "DEG_depsgraph_debug.h"
59 #include "DEG_depsgraph_query.h"
60
61 #include "DRW_engine.h"
62
63 #include "MEM_guardedalloc.h"
64
65
66 /* prototype */
67 static void object_bases_iterator_next(BLI_Iterator *iter, const int flag);
68
69
70 /*********************** Layer Collections and bases *************************/
71
72 static LayerCollection *layer_collection_add(ListBase *lb_parent, Collection *collection)
73 {
74         LayerCollection *lc = MEM_callocN(sizeof(LayerCollection), "Collection Base");
75         lc->collection = collection;
76         BLI_addtail(lb_parent, lc);
77
78         return lc;
79 }
80
81 static void layer_collection_free(ViewLayer *view_layer, LayerCollection *lc)
82 {
83         if (lc == view_layer->active_collection) {
84                 view_layer->active_collection = view_layer->layer_collections.first;
85         }
86
87         for (LayerCollection *nlc = lc->layer_collections.first; nlc; nlc = nlc->next) {
88                 layer_collection_free(view_layer, nlc);
89         }
90
91         BLI_freelistN(&lc->layer_collections);
92 }
93
94 static Base *object_base_new(Object *ob)
95 {
96         Base *base = MEM_callocN(sizeof(Base), "Object Base");
97         base->object = ob;
98         return base;
99 }
100
101 /********************************* View Layer ********************************/
102
103
104 /* RenderLayer */
105
106 /* Returns the default view layer to view in workspaces if there is
107  * none linked to the workspace yet. */
108 ViewLayer *BKE_view_layer_default_view(const Scene *scene)
109 {
110         for (ViewLayer *view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
111                 if (!(view_layer->flag & VIEW_LAYER_RENDER)) {
112                         return view_layer;
113                 }
114         }
115
116         BLI_assert(scene->view_layers.first);
117         return scene->view_layers.first;
118 }
119
120 /* Returns the default view layer to render if we need to render just one. */
121 ViewLayer *BKE_view_layer_default_render(const Scene *scene)
122 {
123         for (ViewLayer *view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
124                 if (view_layer->flag & VIEW_LAYER_RENDER) {
125                         return view_layer;
126                 }
127         }
128
129         BLI_assert(scene->view_layers.first);
130         return scene->view_layers.first;
131 }
132
133 /**
134  * Returns the ViewLayer to be used for drawing, outliner, and other context related areas.
135  */
136 ViewLayer *BKE_view_layer_from_workspace_get(const struct Scene *scene, const struct WorkSpace *workspace)
137 {
138         return BKE_workspace_view_layer_get(workspace, scene);
139 }
140
141 /**
142  * This is a placeholder to know which areas of the code need to be addressed for the Workspace changes.
143  * Never use this, you should either use BKE_view_layer_from_workspace_get or get ViewLayer explicitly.
144  */
145 ViewLayer *BKE_view_layer_context_active_PLACEHOLDER(const Scene *scene)
146 {
147         BLI_assert(scene->view_layers.first);
148         return scene->view_layers.first;
149 }
150
151 static ViewLayer *view_layer_add(const char *name)
152 {
153         if (!name) {
154                 name = DATA_("View Layer");
155         }
156
157         ViewLayer *view_layer = MEM_callocN(sizeof(ViewLayer), "View Layer");
158         view_layer->flag = VIEW_LAYER_RENDER | VIEW_LAYER_FREESTYLE;
159
160         BLI_strncpy_utf8(view_layer->name, name, sizeof(view_layer->name));
161
162         /* Pure rendering pipeline settings. */
163         view_layer->layflag = 0x7FFF;   /* solid ztra halo edge strand */
164         view_layer->passflag = SCE_PASS_COMBINED | SCE_PASS_Z;
165         view_layer->pass_alpha_threshold = 0.5f;
166         BKE_freestyle_config_init(&view_layer->freestyle_config);
167
168         return view_layer;
169 }
170
171 /**
172  * Add a new view layer
173  * by default, a view layer has the master collection
174  */
175 ViewLayer *BKE_view_layer_add(Scene *scene, const char *name)
176 {
177         ViewLayer *view_layer = view_layer_add(name);
178
179         BLI_addtail(&scene->view_layers, view_layer);
180
181         /* unique name */
182         BLI_uniquename(
183                 &scene->view_layers, view_layer, DATA_("ViewLayer"), '.',
184                 offsetof(ViewLayer, name), sizeof(view_layer->name));
185
186         BKE_layer_collection_sync(scene, view_layer);
187
188         return view_layer;
189 }
190
191 void BKE_view_layer_free(ViewLayer *view_layer)
192 {
193         BKE_view_layer_free_ex(view_layer, true);
194 }
195
196 /**
197  * Free (or release) any data used by this ViewLayer.
198  */
199 void BKE_view_layer_free_ex(ViewLayer *view_layer, const bool do_id_user)
200 {
201         view_layer->basact = NULL;
202
203         BLI_freelistN(&view_layer->object_bases);
204
205         if (view_layer->object_bases_hash) {
206                 BLI_ghash_free(view_layer->object_bases_hash, NULL, NULL);
207         }
208
209         for (LayerCollection *lc = view_layer->layer_collections.first; lc; lc = lc->next) {
210                 layer_collection_free(view_layer, lc);
211         }
212         BLI_freelistN(&view_layer->layer_collections);
213
214         for (ViewLayerEngineData *sled = view_layer->drawdata.first; sled; sled = sled->next) {
215                 if (sled->storage) {
216                         if (sled->free) {
217                                 sled->free(sled->storage);
218                         }
219                         MEM_freeN(sled->storage);
220                 }
221         }
222         BLI_freelistN(&view_layer->drawdata);
223
224         MEM_SAFE_FREE(view_layer->stats);
225
226         BKE_freestyle_config_free(&view_layer->freestyle_config, do_id_user);
227
228         if (view_layer->id_properties) {
229                 IDP_FreeProperty(view_layer->id_properties);
230                 MEM_freeN(view_layer->id_properties);
231         }
232
233         MEM_SAFE_FREE(view_layer->object_bases_array);
234
235         MEM_freeN(view_layer);
236 }
237
238 /**
239  * Tag all the selected objects of a renderlayer
240  */
241 void BKE_view_layer_selected_objects_tag(ViewLayer *view_layer, const int tag)
242 {
243         for (Base *base = view_layer->object_bases.first; base; base = base->next) {
244                 if ((base->flag & BASE_SELECTED) != 0) {
245                         base->object->flag |= tag;
246                 }
247                 else {
248                         base->object->flag &= ~tag;
249                 }
250         }
251 }
252
253 static bool find_scene_collection_in_scene_collections(ListBase *lb, const LayerCollection *lc)
254 {
255         for (LayerCollection *lcn = lb->first; lcn; lcn = lcn->next) {
256                 if (lcn == lc) {
257                         return true;
258                 }
259                 if (find_scene_collection_in_scene_collections(&lcn->layer_collections, lc)) {
260                         return true;
261                 }
262         }
263         return false;
264 }
265
266 /**
267  * Fallback for when a Scene has no camera to use
268  *
269  * \param view_layer: in general you want to use the same ViewLayer that is used
270  * for depsgraph. If rendering you pass the scene active layer, when viewing in the viewport
271  * you want to get ViewLayer from context.
272  */
273 Object *BKE_view_layer_camera_find(ViewLayer *view_layer)
274 {
275         for (Base *base = view_layer->object_bases.first; base; base = base->next) {
276                 if (base->object->type == OB_CAMERA) {
277                         return base->object;
278                 }
279         }
280
281         return NULL;
282 }
283
284 /**
285  * Find the ViewLayer a LayerCollection belongs to
286  */
287 ViewLayer *BKE_view_layer_find_from_collection(const Scene *scene, LayerCollection *lc)
288 {
289         for (ViewLayer *view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
290                 if (find_scene_collection_in_scene_collections(&view_layer->layer_collections, lc)) {
291                         return view_layer;
292                 }
293         }
294
295         return NULL;
296 }
297
298 /* Base */
299
300 static void view_layer_bases_hash_create(ViewLayer *view_layer)
301 {
302         static ThreadMutex hash_lock = BLI_MUTEX_INITIALIZER;
303
304         if (!view_layer->object_bases_hash) {
305                 BLI_mutex_lock(&hash_lock);
306
307                 view_layer->object_bases_hash = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, __func__);
308
309                 for (Base *base = view_layer->object_bases.first; base; base = base->next) {
310                         BLI_ghash_insert(view_layer->object_bases_hash, base->object, base);
311                 }
312
313                 BLI_mutex_unlock(&hash_lock);
314         }
315 }
316
317 Base *BKE_view_layer_base_find(ViewLayer *view_layer, Object *ob)
318 {
319         if (!view_layer->object_bases_hash) {
320                 view_layer_bases_hash_create(view_layer);
321         }
322
323         return BLI_ghash_lookup(view_layer->object_bases_hash, ob);
324 }
325
326 void BKE_view_layer_base_deselect_all(ViewLayer *view_layer)
327 {
328         Base *base;
329
330         for (base = view_layer->object_bases.first; base; base = base->next) {
331                 base->flag &= ~BASE_SELECTED;
332         }
333 }
334
335 void BKE_view_layer_base_select(struct ViewLayer *view_layer, Base *selbase)
336 {
337         view_layer->basact = selbase;
338         if ((selbase->flag & BASE_SELECTABLED) != 0) {
339                 selbase->flag |= BASE_SELECTED;
340         }
341 }
342
343 /**************************** Copy View Layer and Layer Collections ***********************/
344
345 static void layer_collections_copy_data(ListBase *layer_collections_dst, const ListBase *layer_collections_src)
346 {
347         BLI_duplicatelist(layer_collections_dst, layer_collections_src);
348
349         LayerCollection *layer_collection_dst = layer_collections_dst->first;
350         const LayerCollection *layer_collection_src = layer_collections_src->first;
351
352         while (layer_collection_dst != NULL) {
353                 layer_collections_copy_data(
354                         &layer_collection_dst->layer_collections,
355                         &layer_collection_src->layer_collections);
356
357                 layer_collection_dst = layer_collection_dst->next;
358                 layer_collection_src = layer_collection_src->next;
359         }
360 }
361
362 /**
363  * Only copy internal data of ViewLayer from source to already allocated/initialized destination.
364  *
365  * \param flag  Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more).
366  */
367 void BKE_view_layer_copy_data(
368         Scene *UNUSED(scene_dst), const Scene *UNUSED(scene_src),
369         ViewLayer *view_layer_dst, const ViewLayer *view_layer_src,
370         const int flag)
371 {
372         if (view_layer_dst->id_properties != NULL) {
373                 view_layer_dst->id_properties = IDP_CopyProperty_ex(view_layer_dst->id_properties, flag);
374         }
375         BKE_freestyle_config_copy(&view_layer_dst->freestyle_config, &view_layer_src->freestyle_config, flag);
376
377         view_layer_dst->stats = NULL;
378
379         /* Clear temporary data. */
380         BLI_listbase_clear(&view_layer_dst->drawdata);
381         view_layer_dst->object_bases_array = NULL;
382         view_layer_dst->object_bases_hash = NULL;
383
384         /* Copy layer collections and object bases. */
385         /* Inline 'BLI_duplicatelist' and update the active base. */
386         BLI_listbase_clear(&view_layer_dst->object_bases);
387         for (Base *base_src = view_layer_src->object_bases.first; base_src; base_src = base_src->next) {
388                 Base *base_dst = MEM_dupallocN(base_src);
389                 BLI_addtail(&view_layer_dst->object_bases, base_dst);
390                 if (view_layer_src->basact == base_src) {
391                         view_layer_dst->basact = base_dst;
392                 }
393         }
394
395         layer_collections_copy_data(&view_layer_dst->layer_collections, &view_layer_src->layer_collections);
396
397         // TODO: not always safe to free BKE_layer_collection_sync(scene_dst, view_layer_dst);
398 }
399
400 void BKE_view_layer_rename(Main *bmain, Scene *scene, ViewLayer *view_layer, const char *newname)
401 {
402         char oldname[sizeof(view_layer->name)];
403
404         BLI_strncpy(oldname, view_layer->name, sizeof(view_layer->name));
405
406         BLI_strncpy_utf8(view_layer->name, newname, sizeof(view_layer->name));
407         BLI_uniquename(&scene->view_layers, view_layer, DATA_("ViewLayer"), '.', offsetof(ViewLayer, name), sizeof(view_layer->name));
408
409         if (scene->nodetree) {
410                 bNode *node;
411                 int index = BLI_findindex(&scene->view_layers, view_layer);
412
413                 for (node = scene->nodetree->nodes.first; node; node = node->next) {
414                         if (node->type == CMP_NODE_R_LAYERS && node->id == NULL) {
415                                 if (node->custom1 == index)
416                                         BLI_strncpy(node->name, view_layer->name, NODE_MAXSTR);
417                         }
418                 }
419         }
420
421         /* fix all the animation data and workspace which may link to this */
422         BKE_animdata_fix_paths_rename_all(NULL, "view_layers", oldname, view_layer->name);
423         BKE_workspace_view_layer_rename(bmain, scene, oldname, view_layer->name);
424
425         /* Dependency graph uses view layer name based lookups. */
426         DEG_id_tag_update(&scene->id, 0);
427 }
428
429 /* LayerCollection */
430
431 /**
432  * Recursively get the collection for a given index
433  */
434 static LayerCollection *collection_from_index(ListBase *lb, const int number, int *i)
435 {
436         for (LayerCollection *lc = lb->first; lc; lc = lc->next) {
437                 if (*i == number) {
438                         return lc;
439                 }
440
441                 (*i)++;
442
443                 LayerCollection *lc_nested = collection_from_index(&lc->layer_collections, number, i);
444                 if (lc_nested) {
445                         return lc_nested;
446                 }
447         }
448         return NULL;
449 }
450
451 /**
452  * Get the collection for a given index
453  */
454 LayerCollection *BKE_layer_collection_from_index(ViewLayer *view_layer, const int index)
455 {
456         int i = 0;
457         return collection_from_index(&view_layer->layer_collections, index, &i);
458 }
459
460 /**
461  * Get the active collection
462  */
463 LayerCollection *BKE_layer_collection_get_active(ViewLayer *view_layer)
464 {
465         return view_layer->active_collection;
466 }
467
468 /*
469  * Activate collection
470  */
471 bool BKE_layer_collection_activate(ViewLayer *view_layer, LayerCollection *lc)
472 {
473         if (lc->flag & LAYER_COLLECTION_EXCLUDE) {
474                 return false;
475         }
476
477         view_layer->active_collection = lc;
478         return true;
479 }
480
481 /**
482  * Activate first parent collection
483  */
484 LayerCollection *BKE_layer_collection_activate_parent(ViewLayer *view_layer, LayerCollection *lc)
485 {
486         CollectionParent *parent = lc->collection->parents.first;
487
488         if (parent) {
489                 lc = BKE_layer_collection_first_from_scene_collection(view_layer, parent->collection);
490         }
491         else {
492                 lc = NULL;
493         }
494
495         if (lc && (lc->flag & LAYER_COLLECTION_EXCLUDE)) {
496                 /* Don't activate excluded collections. */
497                 return BKE_layer_collection_activate_parent(view_layer, lc);
498         }
499
500         if (!lc) {
501                 lc = view_layer->layer_collections.first;
502         }
503
504         view_layer->active_collection = lc;
505         return lc;
506 }
507
508 /**
509  * Recursively get the count of collections
510  */
511 static int collection_count(ListBase *lb)
512 {
513         int i = 0;
514         for (LayerCollection *lc = lb->first; lc; lc = lc->next) {
515                 i += collection_count(&lc->layer_collections) + 1;
516         }
517         return i;
518 }
519
520 /**
521  * Get the total number of collections
522  * (including all the nested collections)
523  */
524 int BKE_layer_collection_count(ViewLayer *view_layer)
525 {
526         return collection_count(&view_layer->layer_collections);
527 }
528
529 /**
530  * Recursively get the index for a given collection
531  */
532 static int index_from_collection(ListBase *lb, const LayerCollection *lc, int *i)
533 {
534         for (LayerCollection *lcol = lb->first; lcol; lcol = lcol->next) {
535                 if (lcol == lc) {
536                         return *i;
537                 }
538
539                 (*i)++;
540
541                 int i_nested = index_from_collection(&lcol->layer_collections, lc, i);
542                 if (i_nested != -1) {
543                         return i_nested;
544                 }
545         }
546         return -1;
547 }
548
549 /**
550  * Return -1 if not found
551  */
552 int BKE_layer_collection_findindex(ViewLayer *view_layer, const LayerCollection *lc)
553 {
554         int i = 0;
555         return index_from_collection(&view_layer->layer_collections, lc, &i);
556 }
557
558 /*********************************** Syncing *********************************
559  *
560  * The layer collection tree mirrors the scene collection tree. Whenever that
561  * changes we need to synchronize them so that there is a corresponding layer
562  * collection for each collection. Note that the scene collection tree can
563  * contain link or override collections, and so this is also called on .blend
564  * file load to ensure any new or removed collections are synced.
565  *
566  * The view layer also contains a list of bases for each object that exists
567  * in at least one layer collection. That list is also synchronized here, and
568  * stores state like selection. */
569
570 static void layer_collection_sync(
571         ViewLayer *view_layer, const ListBase *lb_scene,
572         ListBase *lb_layer, ListBase *new_object_bases,
573         int parent_exclude, int parent_restrict)
574 {
575         /* TODO: support recovery after removal of intermediate collections, reordering, ..
576          * For local edits we can make editing operating do the appropriate thing, but for
577          * linking we can only sync after the fact. */
578
579         /* Remove layer collections that no longer have a corresponding scene collection. */
580         for (LayerCollection *lc = lb_layer->first; lc;) {
581                 /* Note ID remap can set lc->collection to NULL when deleting collections. */
582                 LayerCollection *lc_next = lc->next;
583                 Collection *collection = (lc->collection) ?
584                         BLI_findptr(lb_scene, lc->collection, offsetof(CollectionChild, collection)) : NULL;
585
586                 if (!collection) {
587                         /* Free recursively. */
588                         layer_collection_free(view_layer, lc);
589                         BLI_freelinkN(lb_layer, lc);
590                 }
591
592                 lc = lc_next;
593         }
594
595         /* Add layer collections for any new scene collections, and ensure order is the same. */
596         ListBase new_lb_layer = {NULL, NULL};
597
598         for (const CollectionChild *child = lb_scene->first; child; child = child->next) {
599                 Collection *collection = child->collection;
600                 LayerCollection *lc = BLI_findptr(lb_layer, collection, offsetof(LayerCollection, collection));
601
602                 if (lc) {
603                         BLI_remlink(lb_layer, lc);
604                         BLI_addtail(&new_lb_layer, lc);
605                 }
606                 else {
607                         lc = layer_collection_add(&new_lb_layer, collection);
608                         lc->flag = parent_exclude;
609                 }
610
611                 /* Collection restrict is inherited. */
612                 int child_restrict = parent_restrict;
613                 if (!(collection->flag & COLLECTION_IS_MASTER)) {
614                         child_restrict |= collection->flag;
615                 }
616
617                 /* Sync child collections. */
618                 layer_collection_sync(
619                         view_layer, &collection->children,
620                         &lc->layer_collections, new_object_bases,
621                         lc->flag, child_restrict);
622
623                 /* Layer collection exclude is not inherited. */
624                 if (lc->flag & LAYER_COLLECTION_EXCLUDE) {
625                         continue;
626                 }
627
628                 /* Sync objects, except if collection was excluded. */
629                 for (CollectionObject *cob = collection->gobject.first; cob; cob = cob->next) {
630                         Base *base = BLI_ghash_lookup(view_layer->object_bases_hash, cob->ob);
631
632                         if (base) {
633                                 /* Move from old base list to new base list. Base might have already
634                                  * been moved to the new base list and the first/last test ensure that
635                                  * case also works. */
636                                 if (!ELEM(base, new_object_bases->first, new_object_bases->last)) {
637                                         BLI_remlink(&view_layer->object_bases, base);
638                                         BLI_addtail(new_object_bases, base);
639                                 }
640                         }
641                         else {
642                                 /* Create new base. */
643                                 base = object_base_new(cob->ob);
644                                 BLI_addtail(new_object_bases, base);
645                                 BLI_ghash_insert(view_layer->object_bases_hash, base->object, base);
646                         }
647
648                         if ((child_restrict & COLLECTION_RESTRICT_VIEW) == 0) {
649                                 base->flag |= BASE_VISIBLED | BASE_VISIBLE_VIEWPORT;
650
651                                 if ((child_restrict & COLLECTION_RESTRICT_SELECT) == 0) {
652                                         base->flag |= BASE_SELECTABLED;
653                                 }
654                         }
655                         if ((child_restrict & COLLECTION_RESTRICT_RENDER) == 0) {
656                                 base->flag |= BASE_VISIBLE_RENDER;
657                         }
658                 }
659         }
660
661         /* Replace layer collection list with new one. */
662         *lb_layer = new_lb_layer;
663         BLI_assert(BLI_listbase_count(lb_scene) == BLI_listbase_count(lb_layer));
664 }
665
666 /**
667  * Update view layer collection tree from collections used in the scene.
668  * This is used when collections are removed or added, both while editing
669  * and on file loaded in case linked data changed or went missing.
670  */
671 void BKE_layer_collection_sync(const Scene *scene, ViewLayer *view_layer)
672 {
673         if (!scene->master_collection) {
674                 /* Happens for old files that don't have versioning applied yet. */
675                 return;
676         }
677
678         /* Free cache. */
679         MEM_SAFE_FREE(view_layer->object_bases_array);
680
681         /* Create object to base hash if it does not exist yet. */
682         if (!view_layer->object_bases_hash) {
683                 view_layer_bases_hash_create(view_layer);
684         }
685
686         /* Clear visible and selectable flags to be reset. */
687         for (Base *base = view_layer->object_bases.first; base; base = base->next) {
688                 base->flag &= ~(BASE_VISIBLED | BASE_SELECTABLED | BASE_VISIBLE_VIEWPORT | BASE_VISIBLE_RENDER);
689         }
690
691         /* Generate new layer connections and object bases when collections changed. */
692         CollectionChild child = {NULL, NULL, scene->master_collection};
693         const ListBase collections = {&child, &child};
694         ListBase new_object_bases = {NULL, NULL};
695
696         const int parent_exclude = 0, parent_restrict = 0;
697         layer_collection_sync(
698                 view_layer, &collections,
699                 &view_layer->layer_collections, &new_object_bases,
700                 parent_exclude, parent_restrict);
701
702         /* Any remaning object bases are to be removed. */
703         for (Base *base = view_layer->object_bases.first; base; base = base->next) {
704                 if (view_layer->basact == base) {
705                         view_layer->basact = NULL;
706                 }
707
708                 BLI_ghash_remove(view_layer->object_bases_hash, base->object, NULL, NULL);
709         }
710
711         BLI_freelistN(&view_layer->object_bases);
712         view_layer->object_bases = new_object_bases;
713
714         /* Always set a valid active collection. */
715         LayerCollection *active = view_layer->active_collection;
716
717         if (active && (active->flag & LAYER_COLLECTION_EXCLUDE)) {
718                 BKE_layer_collection_activate_parent(view_layer, active);
719         }
720         else if (active == NULL) {
721                 view_layer->active_collection = view_layer->layer_collections.first;
722         }
723 }
724
725 void BKE_scene_collection_sync(const Scene *scene)
726 {
727         for (ViewLayer *view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
728                 BKE_layer_collection_sync(scene, view_layer);
729         }
730 }
731
732 void BKE_main_collection_sync(const Main *bmain)
733 {
734         /* TODO: if a single collection changed, figure out which
735          * scenes it belongs to and only update those. */
736
737         /* TODO: optimize for file load so only linked collections get checked? */
738
739         for (const Scene *scene = bmain->scene.first; scene; scene = scene->id.next) {
740                 BKE_scene_collection_sync(scene);
741         }
742 }
743
744 void BKE_main_collection_sync_remap(const Main *bmain)
745 {
746         /* On remapping of object or collection pointers free caches. */
747         /* TODO: try to make this faster */
748
749         for (const Scene *scene = bmain->scene.first; scene; scene = scene->id.next) {
750                 for (ViewLayer *view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
751                         MEM_SAFE_FREE(view_layer->object_bases_array);
752
753                         if (view_layer->object_bases_hash) {
754                                 BLI_ghash_free(view_layer->object_bases_hash, NULL, NULL);
755                                 view_layer->object_bases_hash = NULL;
756                         }
757                 }
758         }
759
760         for (Collection *collection = bmain->collection.first; collection; collection = collection->id.next) {
761                 BKE_collection_object_cache_free(collection);
762         }
763
764         BKE_main_collection_sync(bmain);
765 }
766
767 /* ---------------------------------------------------------------------- */
768
769 /**
770  * Select all the objects of this layer collection
771  *
772  * It also select the objects that are in nested collections.
773  * \note Recursive
774  */
775 bool BKE_layer_collection_objects_select(ViewLayer *view_layer, LayerCollection *lc, bool deselect)
776 {
777         if (lc->collection->flag & COLLECTION_RESTRICT_SELECT) {
778                 return false;
779         }
780
781         bool changed = false;
782
783         if (!(lc->flag & LAYER_COLLECTION_EXCLUDE)) {
784                 for (CollectionObject *cob = lc->collection->gobject.first; cob; cob = cob->next) {
785                         Base *base = BKE_view_layer_base_find(view_layer, cob->ob);
786
787                         if (base) {
788                                 if (deselect) {
789                                         if (base->flag & BASE_SELECTED) {
790                                                 base->flag &= ~BASE_SELECTED;
791                                                 changed = true;
792                                         }
793                                 }
794                                 else {
795                                         if ((base->flag & BASE_SELECTABLED) && !(base->flag & BASE_SELECTED)) {
796                                                 base->flag |= BASE_SELECTED;
797                                                 changed = true;
798                                         }
799                                 }
800                         }
801                 }
802         }
803
804         for (LayerCollection *iter = lc->layer_collections.first; iter; iter = iter->next) {
805                 changed |= BKE_layer_collection_objects_select(view_layer, iter, deselect);
806         }
807
808         return changed;
809 }
810
811 /* ---------------------------------------------------------------------- */
812
813 static LayerCollection *find_layer_collection_by_scene_collection(LayerCollection *lc, const Collection *collection)
814 {
815         if (lc->collection == collection) {
816                 return lc;
817         }
818
819         for (LayerCollection *nlc = lc->layer_collections.first; nlc; nlc = nlc->next) {
820                 LayerCollection *found = find_layer_collection_by_scene_collection(nlc, collection);
821                 if (found) {
822                         return found;
823                 }
824         }
825         return NULL;
826 }
827
828 /**
829  * Return the first matching LayerCollection in the ViewLayer for the Collection.
830  */
831 LayerCollection *BKE_layer_collection_first_from_scene_collection(ViewLayer *view_layer, const Collection *collection)
832 {
833         for (LayerCollection *layer_collection = view_layer->layer_collections.first;
834              layer_collection != NULL;
835              layer_collection = layer_collection->next)
836         {
837                 LayerCollection *found = find_layer_collection_by_scene_collection(layer_collection, collection);
838
839                 if (found != NULL) {
840                         return found;
841                 }
842         }
843         return NULL;
844 }
845
846 /**
847  * See if view layer has the scene collection linked directly, or indirectly (nested)
848  */
849 bool BKE_view_layer_has_collection(ViewLayer *view_layer, const Collection *collection)
850 {
851         return BKE_layer_collection_first_from_scene_collection(view_layer, collection) != NULL;
852 }
853
854 /**
855  * See if the object is in any of the scene layers of the scene
856  */
857 bool BKE_scene_has_object(Scene *scene, Object *ob)
858 {
859         for (ViewLayer *view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
860                 Base *base = BKE_view_layer_base_find(view_layer, ob);
861                 if (base) {
862                         return true;
863                 }
864         }
865         return false;
866 }
867
868 /* ---------------------------------------------------------------------- */
869 /* Override */
870
871 /**
872  * Add a new datablock override
873  */
874 void BKE_override_view_layer_datablock_add(
875         ViewLayer *view_layer, int id_type, const char *data_path, const ID *owner_id)
876 {
877         UNUSED_VARS(view_layer, id_type, data_path, owner_id);
878         TODO_LAYER_OVERRIDE;
879 }
880
881 /**
882  * Add a new int override
883  */
884 void BKE_override_view_layer_int_add(
885         ViewLayer *view_layer, int id_type, const char *data_path, const int value)
886 {
887         UNUSED_VARS(view_layer, id_type, data_path, value);
888         TODO_LAYER_OVERRIDE;
889 }
890
891 /**
892  * Add a new boolean override
893  */
894 void BKE_override_layer_collection_boolean_add(
895         struct LayerCollection *layer_collection, int id_type, const char *data_path, const bool value)
896 {
897         UNUSED_VARS(layer_collection, id_type, data_path, value);
898         TODO_LAYER_OVERRIDE;
899 }
900
901 /** \} */
902
903 /* Iterators */
904
905 /* -------------------------------------------------------------------- */
906 /** \name Private Iterator Helpers
907  * \{ */
908
909 static void object_bases_iterator_begin(BLI_Iterator *iter, void *data_in, const int flag)
910 {
911         ViewLayer *view_layer = data_in;
912         Base *base = view_layer->object_bases.first;
913
914         /* when there are no objects */
915         if (base ==  NULL) {
916                 iter->valid = false;
917                 return;
918         }
919
920         iter->data = base;
921
922         if ((base->flag & flag) == 0) {
923                 object_bases_iterator_next(iter, flag);
924         }
925         else {
926                 iter->current = base;
927         }
928 }
929
930 static void object_bases_iterator_next(BLI_Iterator *iter, const int flag)
931 {
932         Base *base = ((Base *)iter->data)->next;
933
934         while (base) {
935                 if ((base->flag & flag) != 0) {
936                         iter->current = base;
937                         iter->data = base;
938                         return;
939                 }
940                 base = base->next;
941         }
942
943         iter->valid = false;
944 }
945
946 static void objects_iterator_begin(BLI_Iterator *iter, void *data_in, const int flag)
947 {
948         object_bases_iterator_begin(iter, data_in, flag);
949
950         if (iter->valid) {
951                 iter->current = ((Base *)iter->current)->object;
952         }
953 }
954
955 static void objects_iterator_next(BLI_Iterator *iter, const int flag)
956 {
957         object_bases_iterator_next(iter, flag);
958
959         if (iter->valid) {
960                 iter->current = ((Base *)iter->current)->object;
961         }
962 }
963
964 /* -------------------------------------------------------------------- */
965 /** \name BKE_view_layer_selected_objects_iterator
966  * See: #FOREACH_SELECTED_OBJECT_BEGIN
967  * \{ */
968
969 void BKE_view_layer_selected_objects_iterator_begin(BLI_Iterator *iter, void *data_in)
970 {
971         objects_iterator_begin(iter, data_in, BASE_SELECTED);
972 }
973
974 void BKE_view_layer_selected_objects_iterator_next(BLI_Iterator *iter)
975 {
976         objects_iterator_next(iter, BASE_SELECTED);
977 }
978
979 void BKE_view_layer_selected_objects_iterator_end(BLI_Iterator *UNUSED(iter))
980 {
981         /* do nothing */
982 }
983
984 /** \} */
985
986 /* -------------------------------------------------------------------- */
987 /** \name BKE_view_layer_visible_objects_iterator
988  * \{ */
989
990 void BKE_view_layer_visible_objects_iterator_begin(BLI_Iterator *iter, void *data_in)
991 {
992         objects_iterator_begin(iter, data_in, BASE_VISIBLED);
993 }
994
995 void BKE_view_layer_visible_objects_iterator_next(BLI_Iterator *iter)
996 {
997         objects_iterator_next(iter, BASE_VISIBLED);
998 }
999
1000 void BKE_view_layer_visible_objects_iterator_end(BLI_Iterator *UNUSED(iter))
1001 {
1002         /* do nothing */
1003 }
1004
1005 /** \} */
1006
1007 /* -------------------------------------------------------------------- */
1008 /** \name BKE_view_layer_selected_editable_objects_iterator
1009  * \{ */
1010
1011 void BKE_view_layer_selected_editable_objects_iterator_begin(BLI_Iterator *iter, void *data_in)
1012 {
1013         objects_iterator_begin(iter, data_in, BASE_SELECTED);
1014         if (iter->valid) {
1015                 if (BKE_object_is_libdata((Object *)iter->current) == false) {
1016                         // First object is valid (selectable and not libdata) -> all good.
1017                         return;
1018                 }
1019                 else {
1020                         // Object is selectable but not editable -> search for another one.
1021                         BKE_view_layer_selected_editable_objects_iterator_next(iter);
1022                 }
1023         }
1024 }
1025
1026 void BKE_view_layer_selected_editable_objects_iterator_next(BLI_Iterator *iter)
1027 {
1028         // Search while there are objects and the one we have is not editable (editable = not libdata).
1029         do {
1030                 objects_iterator_next(iter, BASE_SELECTED);
1031         } while (iter->valid && BKE_object_is_libdata((Object *)iter->current) != false);
1032 }
1033
1034 void BKE_view_layer_selected_editable_objects_iterator_end(BLI_Iterator *UNUSED(iter))
1035 {
1036         /* do nothing */
1037 }
1038
1039 /** \} */
1040
1041 /* -------------------------------------------------------------------- */
1042 /** \name BKE_view_layer_selected_bases_iterator
1043  * \{ */
1044
1045 void BKE_view_layer_selected_bases_iterator_begin(BLI_Iterator *iter, void *data_in)
1046 {
1047         object_bases_iterator_begin(iter, data_in, BASE_SELECTED);
1048 }
1049
1050 void BKE_view_layer_selected_bases_iterator_next(BLI_Iterator *iter)
1051 {
1052         object_bases_iterator_next(iter, BASE_SELECTED);
1053 }
1054
1055 void BKE_view_layer_selected_bases_iterator_end(BLI_Iterator *UNUSED(iter))
1056 {
1057         /* do nothing */
1058 }
1059
1060 /** \} */
1061
1062 /* -------------------------------------------------------------------- */
1063 /** \name BKE_view_layer_visible_bases_iterator
1064  * \{ */
1065
1066 void BKE_view_layer_visible_bases_iterator_begin(BLI_Iterator *iter, void *data_in)
1067 {
1068         object_bases_iterator_begin(iter, data_in, BASE_VISIBLED);
1069 }
1070
1071 void BKE_view_layer_visible_bases_iterator_next(BLI_Iterator *iter)
1072 {
1073         object_bases_iterator_next(iter, BASE_VISIBLED);
1074 }
1075
1076 void BKE_view_layer_visible_bases_iterator_end(BLI_Iterator *UNUSED(iter))
1077 {
1078         /* do nothing */
1079 }
1080
1081 /** \} */
1082
1083 /* -------------------------------------------------------------------- */
1084 /** \name BKE_view_layer_renderable_objects_iterator
1085  * \{ */
1086
1087 void BKE_view_layer_renderable_objects_iterator_begin(BLI_Iterator *iter, void *data_in)
1088 {
1089         struct ObjectsRenderableIteratorData *data = data_in;
1090
1091         /* Tag objects to prevent going over the same object twice. */
1092         for (Scene *scene = data->scene; scene; scene = scene->set) {
1093                 for (ViewLayer *view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
1094                         for (Base *base = view_layer->object_bases.first; base; base = base->next) {
1095                                  base->object->id.flag |= LIB_TAG_DOIT;
1096                         }
1097                 }
1098         }
1099
1100         ViewLayer *view_layer = data->scene->view_layers.first;
1101         data->iter.view_layer = view_layer;
1102
1103         data->base_temp.next = view_layer->object_bases.first;
1104         data->iter.base = &data->base_temp;
1105
1106         data->iter.set = NULL;
1107
1108         iter->data = data_in;
1109         BKE_view_layer_renderable_objects_iterator_next(iter);
1110 }
1111
1112 void BKE_view_layer_renderable_objects_iterator_next(BLI_Iterator *iter)
1113 {
1114         /* Set it early in case we need to exit and we are running from within a loop. */
1115         iter->skip = true;
1116
1117         struct ObjectsRenderableIteratorData *data = iter->data;
1118         Base *base = data->iter.base->next;
1119
1120         /* There is still a base in the current scene layer. */
1121         if (base != NULL) {
1122                 Object *ob = base->object;
1123
1124                 /* We need to set the iter.base even if the rest fail otherwise
1125                  * we keep checking the exactly same base over and over again. */
1126                 data->iter.base = base;
1127
1128                 if (ob->id.flag & LIB_TAG_DOIT) {
1129                         ob->id.flag &= ~LIB_TAG_DOIT;
1130
1131                         if ((base->flag & BASE_VISIBLED) != 0) {
1132                                 iter->skip = false;
1133                                 iter->current = ob;
1134                         }
1135                 }
1136                 return;
1137         }
1138
1139         /* Time to go to the next scene layer. */
1140         if (data->iter.set == NULL) {
1141                 while ((data->iter.view_layer = data->iter.view_layer->next)) {
1142                         ViewLayer *view_layer = data->iter.view_layer;
1143                         if (view_layer->flag & VIEW_LAYER_RENDER) {
1144                                 data->base_temp.next = view_layer->object_bases.first;
1145                                 data->iter.base = &data->base_temp;
1146                                 return;
1147                         }
1148                 }
1149
1150                 /* Setup the "set" for the next iteration. */
1151                 data->scene_temp.set = data->scene;
1152                 data->iter.set = &data->scene_temp;
1153                 return;
1154         }
1155
1156         /* Look for an object in the next set. */
1157         while ((data->iter.set = data->iter.set->set)) {
1158                 ViewLayer *view_layer = BKE_view_layer_default_render(data->iter.set);
1159                 data->base_temp.next = view_layer->object_bases.first;
1160                 data->iter.base = &data->base_temp;
1161                 return;
1162         }
1163
1164         iter->valid = false;
1165 }
1166
1167 void BKE_view_layer_renderable_objects_iterator_end(BLI_Iterator *UNUSED(iter))
1168 {
1169         /* Do nothing - iter->data was static allocated, we can't free it. */
1170 }
1171
1172 /** \} */
1173
1174 /* -------------------------------------------------------------------- */
1175 /** \name BKE_view_layer_bases_in_mode_iterator
1176  * \{ */
1177
1178 void BKE_view_layer_bases_in_mode_iterator_begin(BLI_Iterator *iter, void *data_in)
1179 {
1180         struct ObjectsInModeIteratorData *data = data_in;
1181         Base *base = data->base_active;
1182
1183         /* when there are no objects */
1184         if (base == NULL) {
1185                 iter->valid = false;
1186                 return;
1187         }
1188         iter->data = data_in;
1189         iter->current = base;
1190 }
1191
1192 void BKE_view_layer_bases_in_mode_iterator_next(BLI_Iterator *iter)
1193 {
1194         struct ObjectsInModeIteratorData *data = iter->data;
1195         Base *base = iter->current;
1196
1197         if (base == data->base_active) {
1198                 /* first step */
1199                 base = data->view_layer->object_bases.first;
1200                 if (base == data->base_active) {
1201                         base = base->next;
1202                 }
1203         }
1204         else {
1205                 base = base->next;
1206         }
1207
1208         while (base) {
1209                 if ((base->object->type == data->base_active->object->type) &&
1210                     (base != data->base_active) &&
1211                     (base->object->mode & data->object_mode))
1212                 {
1213                         iter->current = base;
1214                         return;
1215                 }
1216                 base = base->next;
1217         }
1218         iter->valid = false;
1219 }
1220
1221 void BKE_view_layer_bases_in_mode_iterator_end(BLI_Iterator *UNUSED(iter))
1222 {
1223         /* do nothing */
1224 }
1225
1226 /** \} */
1227
1228 /* Evaluation  */
1229
1230 void BKE_layer_eval_view_layer(
1231         struct Depsgraph *depsgraph,
1232         struct Scene *UNUSED(scene),
1233         ViewLayer *view_layer)
1234 {
1235         DEG_debug_print_eval(depsgraph, __func__, view_layer->name, view_layer);
1236
1237         /* Set visibility based on depsgraph mode. */
1238         const eEvaluationMode mode = DEG_get_mode(depsgraph);
1239         const int base_flag = (mode == DAG_EVAL_VIEWPORT) ? BASE_VISIBLE_VIEWPORT : BASE_VISIBLE_RENDER;
1240
1241         for (Base *base = view_layer->object_bases.first; base != NULL; base = base->next) {
1242                 if (base->flag & base_flag) {
1243                         base->flag |= BASE_VISIBLED;
1244                 }
1245                 else {
1246                         base->flag &= ~BASE_VISIBLED;
1247                 }
1248         }
1249
1250         /* TODO(sergey): Is it always required? */
1251         view_layer->flag |= VIEW_LAYER_ENGINE_DIRTY;
1252
1253         /* Create array of bases, for fast index-based lookup. */
1254         const int num_object_bases = BLI_listbase_count(&view_layer->object_bases);
1255         MEM_SAFE_FREE(view_layer->object_bases_array);
1256         view_layer->object_bases_array = MEM_malloc_arrayN(
1257                 num_object_bases, sizeof(Base *), "view_layer->object_bases_array");
1258         int base_index = 0;
1259         for (Base *base = view_layer->object_bases.first; base; base = base->next) {
1260                 /* if base is not selectabled, clear select. */
1261                 if ((base->flag & BASE_SELECTABLED) == 0) {
1262                         base->flag &= ~BASE_SELECTED;
1263                 }
1264                 /* Store base in the array. */
1265                 view_layer->object_bases_array[base_index++] = base;
1266         }
1267         if (view_layer == DEG_get_evaluated_view_layer(depsgraph)) {
1268                 ViewLayer *view_layer_orig = DEG_get_input_view_layer(depsgraph);
1269                 Base *base_orig = view_layer_orig->object_bases.first;
1270                 const Base *base_eval = view_layer->object_bases.first;
1271                 while (base_orig != NULL) {
1272                         base_orig->flag = base_eval->flag;
1273                         base_orig = base_orig->next;
1274                         base_eval = base_eval->next;
1275                 }
1276         }
1277 }
1278
1279 void BKE_layer_eval_view_layer_indexed(
1280         struct Depsgraph *depsgraph,
1281         struct Scene *scene,
1282         int view_layer_index)
1283 {
1284         BLI_assert(view_layer_index >= 0);
1285         ViewLayer *view_layer = BLI_findlink(&scene->view_layers, view_layer_index);
1286         BLI_assert(view_layer != NULL);
1287         BKE_layer_eval_view_layer(depsgraph, scene, view_layer);
1288 }