Merge branch 'master' into blender2.8
[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_listbase.h"
30 #include "BLI_string.h"
31 #include "BLI_string_utf8.h"
32 #include "BLI_string_utils.h"
33 #include "BLT_translation.h"
34
35 #include "BKE_collection.h"
36 #include "BKE_freestyle.h"
37 #include "BKE_global.h"
38 #include "BKE_group.h"
39 #include "BKE_idprop.h"
40 #include "BKE_layer.h"
41 #include "BKE_main.h"
42 #include "BKE_node.h"
43 #include "BKE_workspace.h"
44
45 #include "DEG_depsgraph.h"
46
47 #include "DNA_group_types.h"
48 #include "DNA_ID.h"
49 #include "DNA_layer_types.h"
50 #include "DNA_object_types.h"
51 #include "DNA_node_types.h"
52 #include "DNA_scene_types.h"
53 #include "DNA_windowmanager_types.h"
54 #include "DNA_workspace_types.h"
55
56 #include "DRW_engine.h"
57
58 #include "MEM_guardedalloc.h"
59
60 #define DEBUG_PRINT if (G.debug & G_DEBUG_DEPSGRAPH) printf
61
62 /* prototype */
63 struct EngineSettingsCB_Type;
64 static void layer_collections_sync_flags(ListBase *layer_collections_dst, const ListBase *layer_collections_src);
65 static void layer_collection_free(ViewLayer *view_layer, LayerCollection *lc);
66 static void layer_collection_objects_populate(ViewLayer *view_layer, LayerCollection *lc, ListBase *objects);
67 static LayerCollection *layer_collection_add(ViewLayer *view_layer, LayerCollection *parent, SceneCollection *sc);
68 static LayerCollection *find_layer_collection_by_scene_collection(LayerCollection *lc, const SceneCollection *sc);
69 static IDProperty *collection_engine_settings_create(struct EngineSettingsCB_Type *ces_type, const bool populate);
70 static IDProperty *collection_engine_get(IDProperty *root, const int type, const char *engine_name);
71 static void collection_engine_settings_init(IDProperty *root, const bool populate);
72 static void layer_engine_settings_init(IDProperty *root, const bool populate);
73 static void object_bases_iterator_next(BLI_Iterator *iter, const int flag);
74
75 /* RenderLayer */
76
77 /**
78  * Returns the ViewLayer to be used for rendering
79  * Most of the time BKE_view_layer_from_workspace_get should be used instead
80  */
81 ViewLayer *BKE_view_layer_from_scene_get(const Scene *scene)
82 {
83         ViewLayer *view_layer = BLI_findlink(&scene->view_layers, scene->active_view_layer);
84         BLI_assert(view_layer);
85         return view_layer;
86 }
87
88 /**
89  * Returns the ViewLayer to be used for drawing, outliner, and other context related areas.
90  */
91 ViewLayer *BKE_view_layer_from_workspace_get(const struct Scene *scene, const struct WorkSpace *workspace)
92 {
93         if (BKE_workspace_use_scene_settings_get(workspace)) {
94                 return BKE_view_layer_from_scene_get(scene);
95         }
96         else {
97                 return BKE_workspace_view_layer_get(workspace, scene);
98         }
99 }
100
101 /**
102  * This is a placeholder to know which areas of the code need to be addressed for the Workspace changes.
103  * Never use this, you should either use BKE_view_layer_from_workspace_get or get ViewLayer explicitly.
104  */
105 ViewLayer *BKE_view_layer_context_active_PLACEHOLDER(const Scene *scene)
106 {
107         return BKE_view_layer_from_scene_get(scene);
108 }
109
110 static ViewLayer *view_layer_add(const char *name, SceneCollection *master_scene_collection)
111 {
112         if (!name) {
113                 name = DATA_("View Layer");
114         }
115
116         IDPropertyTemplate val = {0};
117         ViewLayer *view_layer = MEM_callocN(sizeof(ViewLayer), "View Layer");
118         view_layer->flag = VIEW_LAYER_RENDER | VIEW_LAYER_FREESTYLE;
119
120         view_layer->properties = IDP_New(IDP_GROUP, &val, ROOT_PROP);
121         layer_engine_settings_init(view_layer->properties, false);
122         BLI_strncpy_utf8(view_layer->name, name, sizeof(view_layer->name));
123
124         /* Link the master collection by default. */
125         layer_collection_add(view_layer, NULL, master_scene_collection);
126
127         /* Pure rendering pipeline settings. */
128         view_layer->layflag = 0x7FFF;   /* solid ztra halo edge strand */
129         view_layer->passflag = SCE_PASS_COMBINED | SCE_PASS_Z;
130         view_layer->pass_alpha_threshold = 0.5f;
131         BKE_freestyle_config_init(&view_layer->freestyle_config);
132
133         return view_layer;
134 }
135
136 /**
137  * Add a new view layer
138  * by default, a view layer has the master collection
139  */
140 ViewLayer *BKE_view_layer_add(Scene *scene, const char *name)
141 {
142         SceneCollection *sc = BKE_collection_master(&scene->id);
143         ViewLayer *view_layer = view_layer_add(name, sc);
144
145         BLI_addtail(&scene->view_layers, view_layer);
146
147         /* unique name */
148         BLI_uniquename(
149                 &scene->view_layers, view_layer, DATA_("ViewLayer"), '.',
150                 offsetof(ViewLayer, name), sizeof(view_layer->name));
151
152         return view_layer;
153 }
154
155 /**
156  * Add a ViewLayer for a Group
157  * It should be added only once
158  */
159 ViewLayer *BKE_view_layer_group_add(Group *group)
160 {
161         BLI_assert(group->view_layer == NULL);
162         SceneCollection *sc = BKE_collection_master(&group->id);
163         ViewLayer *view_layer = view_layer_add(group->id.name + 2, sc);
164         return view_layer;
165 }
166
167 void BKE_view_layer_free(ViewLayer *view_layer)
168 {
169         BKE_view_layer_free_ex(view_layer, true);
170 }
171
172 /**
173  * Free (or release) any data used by this ViewLayer.
174  */
175 void BKE_view_layer_free_ex(ViewLayer *view_layer, const bool do_id_user)
176 {
177         view_layer->basact = NULL;
178
179         for (Base *base = view_layer->object_bases.first; base; base = base->next) {
180                 if (base->collection_properties) {
181                         IDP_FreeProperty(base->collection_properties);
182                         MEM_freeN(base->collection_properties);
183                 }
184         }
185         BLI_freelistN(&view_layer->object_bases);
186
187         for (LayerCollection *lc = view_layer->layer_collections.first; lc; lc = lc->next) {
188                 layer_collection_free(NULL, lc);
189         }
190         BLI_freelistN(&view_layer->layer_collections);
191
192         if (view_layer->properties) {
193                 IDP_FreeProperty(view_layer->properties);
194                 MEM_freeN(view_layer->properties);
195         }
196
197         if (view_layer->properties_evaluated) {
198                 IDP_FreeProperty(view_layer->properties_evaluated);
199                 MEM_freeN(view_layer->properties_evaluated);
200         }
201
202         for (ViewLayerEngineData *sled = view_layer->drawdata.first; sled; sled = sled->next) {
203                 if (sled->storage) {
204                         if (sled->free) {
205                                 sled->free(sled->storage);
206                         }
207                         MEM_freeN(sled->storage);
208                 }
209         }
210         BLI_freelistN(&view_layer->drawdata);
211
212         MEM_SAFE_FREE(view_layer->stats);
213
214         BKE_freestyle_config_free(&view_layer->freestyle_config, do_id_user);
215
216         if (view_layer->id_properties) {
217                 IDP_FreeProperty(view_layer->id_properties);
218                 MEM_freeN(view_layer->id_properties);
219         }
220
221         MEM_freeN(view_layer);
222 }
223
224 /**
225  * Tag all the selected objects of a renderlayer
226  */
227 void BKE_view_layer_selected_objects_tag(ViewLayer *view_layer, const int tag)
228 {
229         for (Base *base = view_layer->object_bases.first; base; base = base->next) {
230                 if ((base->flag & BASE_SELECTED) != 0) {
231                         base->object->flag |= tag;
232                 }
233                 else {
234                         base->object->flag &= ~tag;
235                 }
236         }
237 }
238
239 /**
240  * Return the first ViewLayer for a given id
241  */
242 ViewLayer *BKE_view_layer_first_from_id(const ID *owner_id)
243 {
244         switch (GS(owner_id->name)) {
245                 case ID_SCE:
246                         return ((Scene *)owner_id)->view_layers.first;
247                 case ID_GR:
248                         return ((Group *)owner_id)->view_layer;
249                 default:
250                         BLI_assert(!"ID doesn't support view layers");
251                         return NULL;
252         }
253 }
254
255 static bool find_scene_collection_in_scene_collections(ListBase *lb, const LayerCollection *lc)
256 {
257         for (LayerCollection *lcn = lb->first; lcn; lcn = lcn->next) {
258                 if (lcn == lc) {
259                         return true;
260                 }
261                 if (find_scene_collection_in_scene_collections(&lcn->layer_collections, lc)) {
262                         return true;
263                 }
264         }
265         return false;
266 }
267
268 /**
269  * Fallback for when a Scene has no camera to use
270  *
271  * \param view_layer: in general you want to use the same ViewLayer that is used
272  * for depsgraph. If rendering you pass the scene active layer, when viewing in the viewport
273  * you want to get ViewLayer from context.
274  */
275 Object *BKE_view_layer_camera_find(ViewLayer *view_layer)
276 {
277         for (Base *base = view_layer->object_bases.first; base; base = base->next) {
278                 if (base->object->type == OB_CAMERA) {
279                         return base->object;
280                 }
281         }
282
283         return NULL;
284 }
285
286 /**
287  * Find the ViewLayer a LayerCollection belongs to
288  */
289 ViewLayer *BKE_view_layer_find_from_collection(const ID *owner_id, LayerCollection *lc)
290 {
291         switch (GS(owner_id->name)) {
292                 case ID_GR:
293                         return ((Group *)owner_id)->view_layer;
294                 case ID_SCE:
295                 {
296                         Scene *scene = (Scene *)owner_id;
297                         for (ViewLayer *view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
298                                 if (find_scene_collection_in_scene_collections(&view_layer->layer_collections, lc)) {
299                                         return view_layer;
300                                 }
301                         }
302                         return NULL;
303                 }
304                 default:
305                         BLI_assert(!"ID doesn't support scene layers");
306                         return NULL;
307         }
308 }
309
310 /* Base */
311
312 Base *BKE_view_layer_base_find(ViewLayer *view_layer, Object *ob)
313 {
314         return BLI_findptr(&view_layer->object_bases, ob, offsetof(Base, object));
315 }
316
317 void BKE_view_layer_base_deselect_all(ViewLayer *view_layer)
318 {
319         Base *base;
320
321         for (base = view_layer->object_bases.first; base; base = base->next) {
322                 base->flag &= ~BASE_SELECTED;
323         }
324 }
325
326 void BKE_view_layer_base_select(struct ViewLayer *view_layer, Base *selbase)
327 {
328         view_layer->basact = selbase;
329         if ((selbase->flag & BASE_SELECTABLED) != 0) {
330                 selbase->flag |= BASE_SELECTED;
331         }
332 }
333
334 /****************************************************************************/
335 /* Copying functions for datablocks that use ViewLayer/SceneCollection */
336
337 /* Find the equivalent SceneCollection in the new tree */
338 static SceneCollection *scene_collection_from_new_tree(
339         SceneCollection *sc_reference, SceneCollection *sc_dst, SceneCollection *sc_src)
340 {
341         if (sc_src == sc_reference) {
342                 return sc_dst;
343         }
344
345         for (SceneCollection *nsc_src = sc_src->scene_collections.first, *nsc_dst = sc_dst->scene_collections.first;
346              nsc_src;
347              nsc_src = nsc_src->next, nsc_dst = nsc_dst->next)
348         {
349                 SceneCollection *found = scene_collection_from_new_tree(sc_reference, nsc_dst, nsc_src);
350                 if (found != NULL) {
351                         return found;
352                 }
353         }
354         return NULL;
355 }
356
357 static void layer_collection_sync_flags(
358         LayerCollection *layer_collection_dst,
359         const LayerCollection *layer_collection_src)
360 {
361         layer_collection_dst->flag = layer_collection_src->flag;
362
363         if (layer_collection_dst->properties != NULL) {
364                 IDP_FreeProperty(layer_collection_dst->properties);
365                 MEM_SAFE_FREE(layer_collection_dst->properties);
366         }
367
368         if (layer_collection_src->properties != NULL) {
369                 layer_collection_dst->properties = IDP_CopyProperty(layer_collection_src->properties);
370         }
371
372         layer_collections_sync_flags(&layer_collection_dst->layer_collections,
373                                      &layer_collection_src->layer_collections);
374 }
375
376 static void layer_collections_sync_flags(ListBase *layer_collections_dst, const ListBase *layer_collections_src)
377 {
378         LayerCollection *layer_collection_dst = (LayerCollection *)layer_collections_dst->first;
379         const LayerCollection *layer_collection_src = (const LayerCollection *)layer_collections_src->first;
380         while (layer_collection_dst != NULL) {
381                 layer_collection_sync_flags(layer_collection_dst, layer_collection_src);
382                 layer_collection_dst = layer_collection_dst->next;
383                 layer_collection_src = layer_collection_src->next;
384         }
385 }
386
387 static bool layer_collection_sync_if_match(
388         ListBase *lb,
389         const SceneCollection *scene_collection_dst,
390         const SceneCollection *scene_collection_src)
391 {
392         for (LayerCollection *layer_collection = lb->first;
393              layer_collection;
394              layer_collection = layer_collection->next)
395         {
396                 if (layer_collection->scene_collection == scene_collection_src) {
397                         LayerCollection *layer_collection_dst =
398                                 BLI_findptr(
399                                     lb,
400                                     scene_collection_dst,
401                                     offsetof(LayerCollection, scene_collection));
402
403                         if (layer_collection_dst != NULL) {
404                                 layer_collection_sync_flags(layer_collection_dst, layer_collection);
405                         }
406                         return true;
407                 }
408                 else {
409                         if (layer_collection_sync_if_match(
410                                 &layer_collection->layer_collections,
411                                 scene_collection_dst,
412                                 scene_collection_src))
413                         {
414                                 return true;
415                         }
416                 }
417         }
418         return false;
419 }
420
421 /**
422  * Sync sibling collections across all view layers
423  *
424  * Make sure every linked instance of \a scene_collection_dst has the same values
425  * (flags, overrides, ...) as the corresponding scene_collection_src.
426  *
427  * \note expect scene_collection_dst to be scene_collection_src->next, and it also
428  * expects both collections to have the same ammount of sub-collections.
429  */
430 void BKE_layer_collection_sync_flags(
431         ID *owner_id,
432         SceneCollection *scene_collection_dst,
433         SceneCollection *scene_collection_src)
434 {
435         for (ViewLayer *view_layer = BKE_view_layer_first_from_id(owner_id); view_layer; view_layer = view_layer->next) {
436                 for (LayerCollection *layer_collection = view_layer->layer_collections.first;
437                      layer_collection;
438                      layer_collection = layer_collection->next)
439                 {
440                         layer_collection_sync_if_match(
441                                     &layer_collection->layer_collections,
442                                     scene_collection_dst,
443                                     scene_collection_src);
444                 }
445         }
446 }
447
448 /* recreate the LayerCollection tree */
449 static void layer_collections_recreate(
450         ViewLayer *view_layer_dst, ListBase *lb_src, SceneCollection *mc_dst, SceneCollection *mc_src)
451 {
452         for (LayerCollection *lc_src = lb_src->first; lc_src; lc_src = lc_src->next) {
453                 SceneCollection *sc_dst = scene_collection_from_new_tree(lc_src->scene_collection, mc_dst, mc_src);
454                 BLI_assert(sc_dst);
455
456                 /* instead of synchronizing both trees we simply re-create it */
457                 BKE_collection_link(view_layer_dst, sc_dst);
458         }
459 }
460
461 /**
462  * Only copy internal data of ViewLayer from source to already allocated/initialized destination.
463  *
464  * \param mc_src Master Collection the source ViewLayer links in.
465  * \param mc_dst Master Collection the destination ViewLayer links in.
466  * \param flag  Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more).
467  */
468 void BKE_view_layer_copy_data(
469         ViewLayer *view_layer_dst, ViewLayer *view_layer_src, SceneCollection *mc_dst, SceneCollection *mc_src,
470         const int flag)
471 {
472         IDPropertyTemplate val = {0};
473
474         if (view_layer_dst->id_properties != NULL) {
475                 view_layer_dst->id_properties = IDP_CopyProperty_ex(view_layer_dst->id_properties, flag);
476         }
477         BKE_freestyle_config_copy(&view_layer_dst->freestyle_config, &view_layer_src->freestyle_config, flag);
478
479         view_layer_dst->stats = NULL;
480         view_layer_dst->properties_evaluated = NULL;
481         view_layer_dst->properties = IDP_New(IDP_GROUP, &val, ROOT_PROP);
482         IDP_MergeGroup_ex(view_layer_dst->properties, view_layer_src->properties, true, flag);
483
484         /* we start fresh with no overrides and no visibility flags set
485          * instead of syncing both trees we simply unlink and relink the scene collection */
486         BLI_listbase_clear(&view_layer_dst->layer_collections);
487         BLI_listbase_clear(&view_layer_dst->object_bases);
488         BLI_listbase_clear(&view_layer_dst->drawdata);
489
490         layer_collections_recreate(view_layer_dst, &view_layer_src->layer_collections, mc_dst, mc_src);
491
492         /* Now we handle the syncing for visibility, selectability, ... */
493         layer_collections_sync_flags(&view_layer_dst->layer_collections, &view_layer_src->layer_collections);
494
495         Object *active_ob = OBACT(view_layer_src);
496         for (Base *base_src = view_layer_src->object_bases.first, *base_dst = view_layer_dst->object_bases.first;
497              base_src;
498              base_src = base_src->next, base_dst = base_dst->next)
499         {
500                 base_dst->flag = base_src->flag;
501                 base_dst->flag_legacy = base_src->flag_legacy;
502
503                 if (base_dst->object == active_ob) {
504                         view_layer_dst->basact = base_dst;
505                 }
506         }
507 }
508
509 /**
510  * Find and return the ListBase of LayerCollection that has \a lc_child as one of its directly
511  * nested LayerCollection.
512  *
513  * \param lb_parent Initial ListBase of LayerCollection to look into recursively
514  * usually the view layer's collection list
515  */
516 static ListBase *find_layer_collection_parent_list_base(ListBase *lb_parent, const LayerCollection *lc_child)
517 {
518         for (LayerCollection *lc_nested = lb_parent->first; lc_nested; lc_nested = lc_nested->next) {
519                 if (lc_nested == lc_child) {
520                         return lb_parent;
521                 }
522
523                 ListBase *found = find_layer_collection_parent_list_base(&lc_nested->layer_collections, lc_child);
524                 if (found != NULL) {
525                         return found;
526                 }
527         }
528
529         return NULL;
530 }
531
532 /**
533  * Makes a shallow copy of a LayerCollection
534  *
535  * Add a new collection in the same level as the old one (linking if necessary),
536  * and copy all the collection data across them.
537  */
538 struct LayerCollection *BKE_layer_collection_duplicate(struct ID *owner_id, struct LayerCollection *layer_collection)
539 {
540         SceneCollection *scene_collection, *scene_collection_new;
541
542         scene_collection = layer_collection->scene_collection;
543         scene_collection_new = BKE_collection_duplicate(owner_id, scene_collection);
544
545         LayerCollection *layer_collection_new = NULL;
546
547         /* If the original layer_collection was directly linked to the view layer
548            we need to link the new scene collection here as well. */
549         for (ViewLayer *view_layer = BKE_view_layer_first_from_id(owner_id); view_layer; view_layer = view_layer->next) {
550                 if (BLI_findindex(&view_layer->layer_collections, layer_collection) != -1) {
551                         layer_collection_new = BKE_collection_link(view_layer, scene_collection_new);
552                         layer_collection_sync_flags(layer_collection_new, layer_collection);
553
554                         if (layer_collection_new != layer_collection->next) {
555                                 BLI_remlink(&view_layer->layer_collections, layer_collection_new);
556                                 BLI_insertlinkafter(&view_layer->layer_collections, layer_collection, layer_collection_new);
557                         }
558                         break;
559                 }
560         }
561
562         /* Otherwise just try to find the corresponding layer collection to return it back. */
563         if (layer_collection_new == NULL) {
564                 for (ViewLayer *view_layer = BKE_view_layer_first_from_id(owner_id); view_layer; view_layer = view_layer->next) {
565                         ListBase *layer_collections_parent;
566                         layer_collections_parent = find_layer_collection_parent_list_base(
567                                                        &view_layer->layer_collections,
568                                                        layer_collection);
569                         if (layer_collections_parent != NULL) {
570                                 layer_collection_new = BLI_findptr(
571                                                                                    layer_collections_parent,
572                                                                                    scene_collection_new,
573                                                                                    offsetof(LayerCollection, scene_collection));
574                                 break;
575                         }
576                 }
577         }
578         return layer_collection_new;
579 }
580
581 static void view_layer_object_base_unref(ViewLayer *view_layer, Base *base)
582 {
583         base->refcount--;
584
585         /* It only exists in the RenderLayer */
586         if (base->refcount == 0) {
587                 if (view_layer->basact == base) {
588                         view_layer->basact = NULL;
589                 }
590
591                 if (base->collection_properties) {
592                         IDP_FreeProperty(base->collection_properties);
593                         MEM_freeN(base->collection_properties);
594                 }
595
596                 BLI_remlink(&view_layer->object_bases, base);
597                 MEM_freeN(base);
598         }
599 }
600
601 /**
602  * Return the base if existent, or create it if necessary
603  * Always bump the refcount
604  */
605 static Base *object_base_add(ViewLayer *view_layer, Object *ob)
606 {
607         Base *base;
608         base = BKE_view_layer_base_find(view_layer, ob);
609
610         if (base == NULL) {
611                 base = MEM_callocN(sizeof(Base), "Object Base");
612
613                 /* Do not bump user count, leave it for SceneCollections. */
614                 base->object = ob;
615                 BLI_addtail(&view_layer->object_bases, base);
616
617                 IDPropertyTemplate val = {0};
618                 base->collection_properties = IDP_New(IDP_GROUP, &val, ROOT_PROP);
619         }
620
621         base->refcount++;
622         return base;
623 }
624
625 /* LayerCollection */
626
627 static void layer_collection_objects_unpopulate(ViewLayer *view_layer, LayerCollection *lc)
628 {
629         if (view_layer) {
630                 for (LinkData *link = lc->object_bases.first; link; link = link->next) {
631                         view_layer_object_base_unref(view_layer, link->data);
632                 }
633         }
634
635         BLI_freelistN(&lc->object_bases);
636 }
637
638 /**
639  * When freeing the entire ViewLayer at once we don't bother with unref
640  * otherwise ViewLayer is passed to keep the syncing of the LayerCollection tree
641  */
642 static void layer_collection_free(ViewLayer *view_layer, LayerCollection *lc)
643 {
644         layer_collection_objects_unpopulate(view_layer, lc);
645         BLI_freelistN(&lc->overrides);
646
647         if (lc->properties) {
648                 IDP_FreeProperty(lc->properties);
649                 MEM_freeN(lc->properties);
650         }
651
652         if (lc->properties_evaluated) {
653                 IDP_FreeProperty(lc->properties_evaluated);
654                 MEM_freeN(lc->properties_evaluated);
655         }
656
657         for (LayerCollection *nlc = lc->layer_collections.first; nlc; nlc = nlc->next) {
658                 layer_collection_free(view_layer, nlc);
659         }
660
661         BLI_freelistN(&lc->layer_collections);
662 }
663
664 /**
665  * Free (or release) LayerCollection from ViewLayer
666  * (does not free the LayerCollection itself).
667  */
668 void BKE_layer_collection_free(ViewLayer *view_layer, LayerCollection *lc)
669 {
670         layer_collection_free(view_layer, lc);
671 }
672
673 /* LayerCollection */
674
675 /**
676  * Recursively get the collection for a given index
677  */
678 static LayerCollection *collection_from_index(ListBase *lb, const int number, int *i)
679 {
680         for (LayerCollection *lc = lb->first; lc; lc = lc->next) {
681                 if (*i == number) {
682                         return lc;
683                 }
684
685                 (*i)++;
686
687                 LayerCollection *lc_nested = collection_from_index(&lc->layer_collections, number, i);
688                 if (lc_nested) {
689                         return lc_nested;
690                 }
691         }
692         return NULL;
693 }
694
695 /**
696  * Get the collection for a given index
697  */
698 LayerCollection *BKE_layer_collection_from_index(ViewLayer *view_layer, const int index)
699 {
700         int i = 0;
701         return collection_from_index(&view_layer->layer_collections, index, &i);
702 }
703
704 /**
705  * Get the active collection
706  */
707 LayerCollection *BKE_layer_collection_get_active(ViewLayer *view_layer)
708 {
709         int i = 0;
710         return collection_from_index(&view_layer->layer_collections, view_layer->active_collection, &i);
711 }
712
713
714 /**
715  * Return layer collection to add new object(s).
716  * Create one if none exists.
717  */
718 LayerCollection *BKE_layer_collection_get_active_ensure(Scene *scene, ViewLayer *view_layer)
719 {
720         LayerCollection *lc = BKE_layer_collection_get_active(view_layer);
721
722         if (lc == NULL) {
723                 BLI_assert(BLI_listbase_is_empty(&view_layer->layer_collections));
724                 /* When there is no collection linked to this ViewLayer, create one. */
725                 SceneCollection *sc = BKE_collection_add(&scene->id, NULL, COLLECTION_TYPE_NONE, NULL);
726                 lc = BKE_collection_link(view_layer, sc);
727                 /* New collection has to be the active one. */
728                 BLI_assert(lc == BKE_layer_collection_get_active(view_layer));
729         }
730
731         return lc;
732 }
733
734 /**
735  * Recursively get the count of collections
736  */
737 static int collection_count(ListBase *lb)
738 {
739         int i = 0;
740         for (LayerCollection *lc = lb->first; lc; lc = lc->next) {
741                 i += collection_count(&lc->layer_collections) + 1;
742         }
743         return i;
744 }
745
746 /**
747  * Get the total number of collections
748  * (including all the nested collections)
749  */
750 int BKE_layer_collection_count(ViewLayer *view_layer)
751 {
752         return collection_count(&view_layer->layer_collections);
753 }
754
755 /**
756  * Recursively get the index for a given collection
757  */
758 static int index_from_collection(ListBase *lb, const LayerCollection *lc, int *i)
759 {
760         for (LayerCollection *lcol = lb->first; lcol; lcol = lcol->next) {
761                 if (lcol == lc) {
762                         return *i;
763                 }
764
765                 (*i)++;
766
767                 int i_nested = index_from_collection(&lcol->layer_collections, lc, i);
768                 if (i_nested != -1) {
769                         return i_nested;
770                 }
771         }
772         return -1;
773 }
774
775 /**
776  * Return -1 if not found
777  */
778 int BKE_layer_collection_findindex(ViewLayer *view_layer, const LayerCollection *lc)
779 {
780         int i = 0;
781         return index_from_collection(&view_layer->layer_collections, lc, &i);
782 }
783
784 /**
785  * Lookup the listbase that contains \a lc.
786  */
787 static ListBase *layer_collection_listbase_find(ListBase *lb, LayerCollection *lc)
788 {
789         for (LayerCollection *lc_iter = lb->first; lc_iter; lc_iter = lc_iter->next) {
790                 if (lc_iter == lc) {
791                         return lb;
792                 }
793
794                 ListBase *lb_child_result;
795                 if ((lb_child_result = layer_collection_listbase_find(&lc_iter->layer_collections, lc))) {
796                         return lb_child_result;
797                 }
798         }
799
800         return NULL;
801 }
802
803 #if 0
804 /**
805  * Lookup the listbase that contains \a sc.
806  */
807 static ListBase *scene_collection_listbase_find(ListBase *lb, SceneCollection *sc)
808 {
809         for (SceneCollection *sc_iter = lb->first; sc_iter; sc_iter = sc_iter->next) {
810                 if (sc_iter == sc) {
811                         return lb;
812                 }
813
814                 ListBase *lb_child_result;
815                 if ((lb_child_result = scene_collection_listbase_find(&sc_iter->scene_collections, sc))) {
816                         return lb_child_result;
817                 }
818         }
819
820         return NULL;
821 }
822 #endif
823
824 /* ---------------------------------------------------------------------- */
825 /* Outliner drag and drop */
826
827 /**
828  * Nest a LayerCollection into another one
829  * Both collections must be from the same ViewLayer, return true if succeded.
830  *
831  * The LayerCollection will effectively be moved into the
832  * new (nested) position. So all the settings, overrides, ... go with it, and
833  * if the collection was directly linked to the ViewLayer it's then unlinked.
834  *
835  * For the other ViewLayers we simply resync the tree, without changing directly
836  * linked collections (even if they link to the same SceneCollection)
837  *
838  * \param lc_src LayerCollection to nest into \a lc_dst
839  * \param lc_dst LayerCollection to have \a lc_src inserted into
840  */
841
842 static void layer_collection_swap(
843         ViewLayer *view_layer, ListBase *lb_a, ListBase *lb_b,
844         LayerCollection *lc_a, LayerCollection *lc_b)
845 {
846         if (lb_a == NULL) {
847                 lb_a = layer_collection_listbase_find(&view_layer->layer_collections, lc_a);
848         }
849
850         if (lb_b == NULL) {
851                 lb_b = layer_collection_listbase_find(&view_layer->layer_collections, lc_b);
852         }
853
854         BLI_assert(lb_a);
855         BLI_assert(lb_b);
856
857         BLI_listbases_swaplinks(lb_a, lb_b, lc_a, lc_b);
858 }
859
860 /**
861  * Move \a lc_src into \a lc_dst. Both have to be stored in \a view_layer.
862  * If \a lc_src is directly linked to the ViewLayer it's unlinked
863  */
864 bool BKE_layer_collection_move_into(const ID *owner_id, LayerCollection *lc_dst, LayerCollection *lc_src)
865 {
866         ViewLayer *view_layer = BKE_view_layer_find_from_collection(owner_id, lc_src);
867         bool is_directly_linked = false;
868
869         if ((!view_layer) || (view_layer != BKE_view_layer_find_from_collection(owner_id, lc_dst))) {
870                 return false;
871         }
872
873         /* We can't nest the collection into itself */
874         if (lc_src->scene_collection == lc_dst->scene_collection) {
875                 return false;
876         }
877
878         /* Collection is already where we wanted it to be */
879         if (lc_dst->layer_collections.last == lc_src) {
880                 return false;
881         }
882
883         /* Collection is already where we want it to be in the scene tree
884          * but we want to swap it in the layer tree still */
885         if (lc_dst->scene_collection->scene_collections.last == lc_src->scene_collection) {
886                 LayerCollection *lc_swap = lc_dst->layer_collections.last;
887                 layer_collection_swap(view_layer, &lc_dst->layer_collections, NULL, lc_dst->layer_collections.last, lc_src);
888
889                 if (BLI_findindex(&view_layer->layer_collections, lc_swap) != -1) {
890                         BKE_collection_unlink(view_layer, lc_swap);
891                 }
892                 return true;
893         }
894         else {
895                 LayerCollection *lc_temp;
896                 is_directly_linked = BLI_findindex(&view_layer->layer_collections, lc_src) != -1;
897
898                 if (!is_directly_linked) {
899                         /* lc_src will be invalid after BKE_collection_move_into!
900                          * so we swap it with lc_temp to preserve its settings */
901                         lc_temp = BKE_collection_link(view_layer, lc_src->scene_collection);
902                         layer_collection_swap(view_layer, &view_layer->layer_collections, NULL, lc_temp, lc_src);
903                 }
904
905                 if (!BKE_collection_move_into(owner_id, lc_dst->scene_collection, lc_src->scene_collection)) {
906                         if (!is_directly_linked) {
907                                 /* Swap back and remove */
908                                 layer_collection_swap(view_layer, NULL, NULL, lc_temp, lc_src);
909                                 BKE_collection_unlink(view_layer, lc_temp);
910                         }
911                         return false;
912                 }
913         }
914
915         LayerCollection *lc_new = BLI_findptr(
916                 &lc_dst->layer_collections, lc_src->scene_collection, offsetof(LayerCollection, scene_collection));
917         BLI_assert(lc_new);
918         layer_collection_swap(view_layer, &lc_dst->layer_collections, NULL, lc_new, lc_src);
919
920         /* If it's directly linked, unlink it after the swap */
921         if (BLI_findindex(&view_layer->layer_collections, lc_new) != -1) {
922                 BKE_collection_unlink(view_layer, lc_new);
923         }
924
925         return true;
926 }
927
928 /**
929  * Move \a lc_src above \a lc_dst. Both have to be stored in \a view_layer.
930  * If \a lc_src is directly linked to the ViewLayer it's unlinked
931  */
932 bool BKE_layer_collection_move_above(const ID *owner_id, LayerCollection *lc_dst, LayerCollection *lc_src)
933 {
934         ViewLayer *view_layer = BKE_view_layer_find_from_collection(owner_id, lc_src);
935         const bool is_directly_linked_src = BLI_findindex(&view_layer->layer_collections, lc_src) != -1;
936         const bool is_directly_linked_dst = BLI_findindex(&view_layer->layer_collections, lc_dst) != -1;
937
938         if ((!view_layer) || (view_layer != BKE_view_layer_find_from_collection(owner_id, lc_dst))) {
939                 return false;
940         }
941
942         /* Collection is already where we wanted it to be */
943         if (lc_dst->prev == lc_src) {
944                 return false;
945         }
946
947         /* Collection is already where we want it to be in the scene tree
948          * but we want to swap it in the layer tree still */
949         if (lc_dst->prev && lc_dst->prev->scene_collection == lc_src->scene_collection) {
950                 LayerCollection *lc_swap = lc_dst->prev;
951                 layer_collection_swap(view_layer, NULL, NULL, lc_dst->prev, lc_src);
952
953                 if (BLI_findindex(&view_layer->layer_collections, lc_swap) != -1) {
954                         BKE_collection_unlink(view_layer, lc_swap);
955                 }
956                 return true;
957         }
958         /* We don't allow to move above/below a directly linked collection
959          * unless the source collection is also directly linked */
960         else if (is_directly_linked_dst) {
961                 /* Both directly linked to the ViewLayer, just need to swap */
962                 if (is_directly_linked_src) {
963                         BLI_remlink(&view_layer->layer_collections, lc_src);
964                         BLI_insertlinkbefore(&view_layer->layer_collections, lc_dst, lc_src);
965                         return true;
966                 }
967                 else {
968                         return false;
969                 }
970         }
971         else {
972                 LayerCollection *lc_temp;
973
974                 if (!is_directly_linked_src) {
975                         /* lc_src will be invalid after BKE_collection_move_into!
976                          * so we swap it with lc_temp to preserve its settings */
977                         lc_temp = BKE_collection_link(view_layer, lc_src->scene_collection);
978                         layer_collection_swap(view_layer, &view_layer->layer_collections, NULL, lc_temp, lc_src);
979                 }
980
981                 if (!BKE_collection_move_above(owner_id, lc_dst->scene_collection, lc_src->scene_collection)) {
982                         if (!is_directly_linked_src) {
983                                 /* Swap back and remove */
984                                 layer_collection_swap(view_layer, NULL, NULL, lc_temp, lc_src);
985                                 BKE_collection_unlink(view_layer, lc_temp);
986                         }
987                         return false;
988                 }
989         }
990
991         LayerCollection *lc_new = lc_dst->prev;
992         BLI_assert(lc_new);
993         layer_collection_swap(view_layer, NULL, NULL, lc_new, lc_src);
994
995         /* If it's directly linked, unlink it after the swap */
996         if (BLI_findindex(&view_layer->layer_collections, lc_new) != -1) {
997                 BKE_collection_unlink(view_layer, lc_new);
998         }
999
1000         return true;
1001 }
1002
1003 /**
1004  * Move \a lc_src below \a lc_dst. Both have to be stored in \a view_layer.
1005  * If \a lc_src is directly linked to the ViewLayer it's unlinked
1006  */
1007 bool BKE_layer_collection_move_below(const ID *owner_id, LayerCollection *lc_dst, LayerCollection *lc_src)
1008 {
1009         ViewLayer *view_layer = BKE_view_layer_find_from_collection(owner_id, lc_src);
1010         const bool is_directly_linked_src = BLI_findindex(&view_layer->layer_collections, lc_src) != -1;
1011         const bool is_directly_linked_dst = BLI_findindex(&view_layer->layer_collections, lc_dst) != -1;
1012
1013         if ((!view_layer) || (view_layer != BKE_view_layer_find_from_collection(owner_id, lc_dst))) {
1014                 return false;
1015         }
1016
1017         /* Collection is already where we wanted it to be */
1018         if (lc_dst->next == lc_src) {
1019                 return false;
1020         }
1021
1022         /* Collection is already where we want it to be in the scene tree
1023          * but we want to swap it in the layer tree still */
1024         if (lc_dst->next && lc_dst->next->scene_collection == lc_src->scene_collection) {
1025                 LayerCollection *lc_swap = lc_dst->next;
1026                 layer_collection_swap(view_layer, NULL, NULL, lc_dst->next, lc_src);
1027
1028                 if (BLI_findindex(&view_layer->layer_collections, lc_swap) != -1) {
1029                         BKE_collection_unlink(view_layer, lc_swap);
1030                 }
1031                 return true;
1032         }
1033         /* We don't allow to move above/below a directly linked collection
1034          * unless the source collection is also directly linked */
1035         else if (is_directly_linked_dst) {
1036                 /* Both directly linked to the ViewLayer, just need to swap */
1037                 if (is_directly_linked_src) {
1038                         BLI_remlink(&view_layer->layer_collections, lc_src);
1039                         BLI_insertlinkafter(&view_layer->layer_collections, lc_dst, lc_src);
1040                         return true;
1041                 }
1042                 else {
1043                         return false;
1044                 }
1045         }
1046         else {
1047                 LayerCollection *lc_temp;
1048
1049                 if (!is_directly_linked_src) {
1050                         /* lc_src will be invalid after BKE_collection_move_into!
1051                          * so we swap it with lc_temp to preserve its settings */
1052                         lc_temp = BKE_collection_link(view_layer, lc_src->scene_collection);
1053                         layer_collection_swap(view_layer, &view_layer->layer_collections, NULL, lc_temp, lc_src);
1054                 }
1055
1056                 if (!BKE_collection_move_below(owner_id, lc_dst->scene_collection, lc_src->scene_collection)) {
1057                         if (!is_directly_linked_src) {
1058                                 /* Swap back and remove */
1059                                 layer_collection_swap(view_layer, NULL, NULL, lc_temp, lc_src);
1060                                 BKE_collection_unlink(view_layer, lc_temp);
1061                         }
1062                         return false;
1063                 }
1064         }
1065
1066         LayerCollection *lc_new = lc_dst->next;
1067         BLI_assert(lc_new);
1068         layer_collection_swap(view_layer, NULL, NULL, lc_new, lc_src);
1069
1070         /* If it's directly linked, unlink it after the swap */
1071         if (BLI_findindex(&view_layer->layer_collections, lc_new) != -1) {
1072                 BKE_collection_unlink(view_layer, lc_new);
1073         }
1074
1075         return true;
1076 }
1077
1078 static bool layer_collection_resync(ViewLayer *view_layer, LayerCollection *lc, const SceneCollection *sc)
1079 {
1080         if (lc->scene_collection == sc) {
1081                 ListBase collections = {NULL};
1082                 BLI_movelisttolist(&collections, &lc->layer_collections);
1083
1084                 for (SceneCollection *sc_nested = sc->scene_collections.first; sc_nested; sc_nested = sc_nested->next) {
1085                         LayerCollection *lc_nested = BLI_findptr(&collections, sc_nested, offsetof(LayerCollection, scene_collection));
1086                         if (lc_nested) {
1087                                 BLI_remlink(&collections, lc_nested);
1088                                 BLI_addtail(&lc->layer_collections, lc_nested);
1089                         }
1090                         else {
1091                                 layer_collection_add(view_layer, lc, sc_nested);
1092                         }
1093                 }
1094
1095                 for (LayerCollection *lc_nested = collections.first; lc_nested; lc_nested = lc_nested->next) {
1096                         layer_collection_free(view_layer, lc_nested);
1097                 }
1098                 BLI_freelistN(&collections);
1099
1100                 BLI_assert(BLI_listbase_count(&lc->layer_collections) ==
1101                            BLI_listbase_count(&sc->scene_collections));
1102
1103                 return true;
1104         }
1105
1106         for (LayerCollection *lc_nested = lc->layer_collections.first; lc_nested; lc_nested = lc_nested->next) {
1107                 if (layer_collection_resync(view_layer, lc_nested, sc)) {
1108                         return true;
1109                 }
1110         }
1111
1112         return false;
1113 }
1114
1115 /**
1116  * Update the scene layers so that any LayerCollection that points
1117  * to \a sc is re-synced again
1118  */
1119 void BKE_layer_collection_resync(const ID *owner_id, const SceneCollection *sc)
1120 {
1121         for (ViewLayer *view_layer = BKE_view_layer_first_from_id(owner_id); view_layer; view_layer = view_layer->next) {
1122                 for (LayerCollection *lc = view_layer->layer_collections.first; lc; lc = lc->next) {
1123                         layer_collection_resync(view_layer, lc, sc);
1124                 }
1125         }
1126 }
1127
1128 /* ---------------------------------------------------------------------- */
1129
1130 /**
1131  * Select all the objects of this layer collection
1132  *
1133  * It also select the objects that are in nested collections.
1134  * \note Recursive
1135  */
1136 void BKE_layer_collection_objects_select(struct LayerCollection *layer_collection)
1137 {
1138         if ((layer_collection->flag & COLLECTION_DISABLED) ||
1139             ((layer_collection->flag & COLLECTION_SELECTABLE) == 0))
1140         {
1141                 return;
1142         }
1143
1144         for (LinkData *link = layer_collection->object_bases.first; link; link = link->next) {
1145                 Base *base = link->data;
1146                 if (base->flag & BASE_SELECTABLED) {
1147                         base->flag |= BASE_SELECTED;
1148                 }
1149         }
1150
1151         for (LayerCollection *iter = layer_collection->layer_collections.first; iter; iter = iter->next) {
1152                 BKE_layer_collection_objects_select(iter);
1153         }
1154 }
1155
1156 /* ---------------------------------------------------------------------- */
1157
1158 /**
1159  * Link a collection to a renderlayer
1160  * The collection needs to be created separately
1161  */
1162 LayerCollection *BKE_collection_link(ViewLayer *view_layer, SceneCollection *sc)
1163 {
1164         LayerCollection *lc = layer_collection_add(view_layer, NULL, sc);
1165         view_layer->active_collection = BKE_layer_collection_findindex(view_layer, lc);
1166         return lc;
1167 }
1168
1169 /**
1170  * Unlink a collection base from a renderlayer
1171  * The corresponding collection is not removed from the master collection
1172  */
1173 void BKE_collection_unlink(ViewLayer *view_layer, LayerCollection *lc)
1174 {
1175         BKE_layer_collection_free(view_layer, lc);
1176         BLI_remlink(&view_layer->layer_collections, lc);
1177         MEM_freeN(lc);
1178         view_layer->active_collection = 0;
1179 }
1180
1181 /**
1182  * Recursively enable nested collections
1183  */
1184 static void layer_collection_enable(ViewLayer *view_layer, LayerCollection *lc)
1185 {
1186         layer_collection_objects_populate(view_layer, lc, &lc->scene_collection->objects);
1187
1188         for (LayerCollection *nlc = lc->layer_collections.first; nlc; nlc = nlc->next) {
1189                 layer_collection_enable(view_layer, nlc);
1190         }
1191 }
1192
1193 /**
1194  * Enable collection
1195  * Add its objects bases to ViewLayer
1196  *
1197  * Only around for doversion.
1198  */
1199 void BKE_collection_enable(ViewLayer *view_layer, LayerCollection *lc)
1200 {
1201         if ((lc->flag & COLLECTION_DISABLED) == 0) {
1202                 return;
1203         }
1204
1205         lc->flag &= ~COLLECTION_DISABLED;
1206         layer_collection_enable(view_layer, lc);
1207 }
1208
1209 static void layer_collection_object_add(ViewLayer *view_layer, LayerCollection *lc, Object *ob)
1210 {
1211         Base *base = object_base_add(view_layer, ob);
1212
1213         /* Only add an object once. */
1214         if (BLI_findptr(&lc->object_bases, base, offsetof(LinkData, data))) {
1215                 return;
1216         }
1217
1218         bool is_visible = ((lc->flag & COLLECTION_VIEWPORT) != 0) && ((lc->flag & COLLECTION_DISABLED) == 0);
1219         bool is_selectable = is_visible && ((lc->flag & COLLECTION_SELECTABLE) != 0);
1220
1221         if (is_visible) {
1222                 base->flag |= BASE_VISIBLED;
1223         }
1224
1225         if (is_selectable) {
1226                 base->flag |= BASE_SELECTABLED;
1227         }
1228
1229         BLI_addtail(&lc->object_bases, BLI_genericNodeN(base));
1230 }
1231
1232 static void layer_collection_object_remove(ViewLayer *view_layer, LayerCollection *lc, Object *ob)
1233 {
1234         Base *base;
1235         base = BKE_view_layer_base_find(view_layer, ob);
1236
1237         LinkData *link = BLI_findptr(&lc->object_bases, base, offsetof(LinkData, data));
1238         BLI_remlink(&lc->object_bases, link);
1239         MEM_freeN(link);
1240
1241         view_layer_object_base_unref(view_layer, base);
1242 }
1243
1244 static void layer_collection_objects_populate(ViewLayer *view_layer, LayerCollection *lc, ListBase *objects)
1245 {
1246         for (LinkData *link = objects->first; link; link = link->next) {
1247                 layer_collection_object_add(view_layer, lc, link->data);
1248         }
1249 }
1250
1251 static void layer_collection_populate(ViewLayer *view_layer, LayerCollection *lc, SceneCollection *sc)
1252 {
1253         layer_collection_objects_populate(view_layer, lc, &sc->objects);
1254
1255         for (SceneCollection *nsc = sc->scene_collections.first; nsc; nsc = nsc->next) {
1256                 layer_collection_add(view_layer, lc, nsc);
1257         }
1258 }
1259
1260 static LayerCollection *layer_collection_add(ViewLayer *view_layer, LayerCollection *parent, SceneCollection *sc)
1261 {
1262         IDPropertyTemplate val = {0};
1263         LayerCollection *lc = MEM_callocN(sizeof(LayerCollection), "Collection Base");
1264
1265         lc->scene_collection = sc;
1266         lc->flag = COLLECTION_SELECTABLE | COLLECTION_VIEWPORT | COLLECTION_RENDER;
1267
1268         lc->properties = IDP_New(IDP_GROUP, &val, ROOT_PROP);
1269         collection_engine_settings_init(lc->properties, false);
1270
1271         if (parent != NULL) {
1272                 BLI_addtail(&parent->layer_collections, lc);
1273         }
1274         else {
1275                 BLI_addtail(&view_layer->layer_collections, lc);
1276         }
1277
1278         layer_collection_populate(view_layer, lc, sc);
1279
1280         return lc;
1281 }
1282
1283 /* ---------------------------------------------------------------------- */
1284
1285 /**
1286  * See if render layer has the scene collection linked directly, or indirectly (nested)
1287  */
1288 bool BKE_view_layer_has_collection(ViewLayer *view_layer, const SceneCollection *sc)
1289 {
1290         for (LayerCollection *lc = view_layer->layer_collections.first; lc; lc = lc->next) {
1291                 if (find_layer_collection_by_scene_collection(lc, sc) != NULL) {
1292                         return true;
1293                 }
1294         }
1295         return false;
1296 }
1297
1298 /**
1299  * See if the object is in any of the scene layers of the scene
1300  */
1301 bool BKE_scene_has_object(Scene *scene, Object *ob)
1302 {
1303         for (ViewLayer *view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
1304                 Base *base = BKE_view_layer_base_find(view_layer, ob);
1305                 if (base) {
1306                         return true;
1307                 }
1308         }
1309         return false;
1310 }
1311
1312
1313 /* ---------------------------------------------------------------------- */
1314 /* Syncing */
1315
1316 static LayerCollection *find_layer_collection_by_scene_collection(LayerCollection *lc, const SceneCollection *sc)
1317 {
1318         if (lc->scene_collection == sc) {
1319                 return lc;
1320         }
1321
1322         for (LayerCollection *nlc = lc->layer_collections.first; nlc; nlc = nlc->next) {
1323                 LayerCollection *found = find_layer_collection_by_scene_collection(nlc, sc);
1324                 if (found) {
1325                         return found;
1326                 }
1327         }
1328         return NULL;
1329 }
1330
1331 /**
1332  * Add a new LayerCollection for all the ViewLayers that have sc_parent
1333  */
1334 void BKE_layer_sync_new_scene_collection(ID *owner_id, const SceneCollection *sc_parent, SceneCollection *sc)
1335 {
1336         for (ViewLayer *view_layer = BKE_view_layer_first_from_id(owner_id); view_layer; view_layer = view_layer->next) {
1337                 for (LayerCollection *lc = view_layer->layer_collections.first; lc; lc = lc->next) {
1338                         LayerCollection *lc_parent = find_layer_collection_by_scene_collection(lc, sc_parent);
1339                         if (lc_parent) {
1340                                 layer_collection_add(view_layer, lc_parent, sc);
1341                         }
1342                 }
1343         }
1344 }
1345
1346 /**
1347  * Add a corresponding ObjectBase to all the equivalent LayerCollection
1348  */
1349 void BKE_layer_sync_object_link(const ID *owner_id, SceneCollection *sc, Object *ob)
1350 {
1351         for (ViewLayer *view_layer = BKE_view_layer_first_from_id(owner_id); view_layer; view_layer = view_layer->next) {
1352                 for (LayerCollection *lc = view_layer->layer_collections.first; lc; lc = lc->next) {
1353                         LayerCollection *found = find_layer_collection_by_scene_collection(lc, sc);
1354                         if (found) {
1355                                 layer_collection_object_add(view_layer, found, ob);
1356                         }
1357                 }
1358         }
1359 }
1360
1361 /**
1362  * Remove the equivalent object base to all layers that have this collection
1363  */
1364 void BKE_layer_sync_object_unlink(const ID *owner_id, SceneCollection *sc, Object *ob)
1365 {
1366         for (ViewLayer *view_layer = BKE_view_layer_first_from_id(owner_id); view_layer; view_layer = view_layer->next) {
1367                 for (LayerCollection *lc = view_layer->layer_collections.first; lc; lc = lc->next) {
1368                         LayerCollection *found = find_layer_collection_by_scene_collection(lc, sc);
1369                         if (found) {
1370                                 layer_collection_object_remove(view_layer, found, ob);
1371                         }
1372                 }
1373         }
1374 }
1375
1376 /* ---------------------------------------------------------------------- */
1377 /* Override */
1378
1379 /**
1380  * Add a new datablock override
1381  */
1382 void BKE_override_view_layer_datablock_add(ViewLayer *view_layer, int id_type, const char *data_path, const ID *owner_id)
1383 {
1384         UNUSED_VARS(view_layer, id_type, data_path, owner_id);
1385         TODO_LAYER_OVERRIDE;
1386 }
1387
1388 /**
1389  * Add a new int override
1390  */
1391 void BKE_override_view_layer_int_add(ViewLayer *view_layer, int id_type, const char *data_path, const int value)
1392 {
1393         UNUSED_VARS(view_layer, id_type, data_path, value);
1394         TODO_LAYER_OVERRIDE;
1395 }
1396
1397 /**
1398  * Add a new boolean override
1399  */
1400 void BKE_override_layer_collection_boolean_add(struct LayerCollection *layer_collection, int id_type, const char *data_path, const bool value)
1401 {
1402         UNUSED_VARS(layer_collection, id_type, data_path, value);
1403         TODO_LAYER_OVERRIDE;
1404 }
1405
1406 /* ---------------------------------------------------------------------- */
1407 /* Engine Settings */
1408
1409 ListBase R_layer_collection_engines_settings_callbacks = {NULL, NULL};
1410 ListBase R_view_layer_engines_settings_callbacks = {NULL, NULL};
1411
1412 typedef struct EngineSettingsCB_Type {
1413         struct EngineSettingsCB_Type *next, *prev;
1414
1415         char name[MAX_NAME]; /* engine name */
1416
1417         EngineSettingsCB callback;
1418
1419 } EngineSettingsCB_Type;
1420
1421 static void create_engine_settings_scene(IDProperty *root, EngineSettingsCB_Type *es_type)
1422 {
1423         if (collection_engine_get(root, COLLECTION_MODE_NONE, es_type->name)) {
1424                 return;
1425         }
1426
1427         IDProperty *props = collection_engine_settings_create(es_type, true);
1428         IDP_AddToGroup(root, props);
1429 }
1430
1431 static void create_layer_collection_engine_settings_scene(Scene *scene, EngineSettingsCB_Type *es_type)
1432 {
1433         create_engine_settings_scene(scene->collection_properties, es_type);
1434 }
1435
1436 static void create_view_layer_engine_settings_scene(Scene *scene, EngineSettingsCB_Type *es_type)
1437 {
1438         create_engine_settings_scene(scene->layer_properties, es_type);
1439 }
1440
1441 static void create_layer_collection_engine_settings_collection(LayerCollection *lc, EngineSettingsCB_Type *es_type)
1442 {
1443         if (BKE_layer_collection_engine_collection_get(lc, COLLECTION_MODE_NONE, es_type->name)) {
1444                 return;
1445         }
1446
1447         IDProperty *props = collection_engine_settings_create(es_type, false);
1448         IDP_AddToGroup(lc->properties, props);
1449
1450         for (LayerCollection *lcn = lc->layer_collections.first; lcn; lcn = lcn->next) {
1451                 create_layer_collection_engine_settings_collection(lcn, es_type);
1452         }
1453 }
1454
1455 static void create_layer_collection_engines_settings_scene(Scene *scene, EngineSettingsCB_Type *es_type)
1456 {
1457         /* Populate the scene with the new settings. */
1458         create_layer_collection_engine_settings_scene(scene, es_type);
1459
1460         for (ViewLayer *view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
1461                 for (LayerCollection *lc = view_layer->layer_collections.first; lc; lc = lc->next) {
1462                         create_layer_collection_engine_settings_collection(lc, es_type);
1463                 }
1464         }
1465 }
1466
1467 static void create_view_layer_engines_settings_scene(Scene *scene, EngineSettingsCB_Type *es_type)
1468 {
1469         /* Populate the scene with the new settings. */
1470         create_view_layer_engine_settings_scene(scene, es_type);
1471 }
1472
1473 static void create_view_layer_engines_settings_layer(ViewLayer *view_layer, EngineSettingsCB_Type *es_type)
1474 {
1475         if (BKE_view_layer_engine_layer_get(view_layer, COLLECTION_MODE_NONE, es_type->name)) {
1476                 return;
1477         }
1478
1479         IDProperty *props = collection_engine_settings_create(es_type, false);
1480         IDP_AddToGroup(view_layer->properties, props);
1481 }
1482
1483 static EngineSettingsCB_Type *engine_settings_callback_register(const char *engine_name, EngineSettingsCB func, ListBase *lb)
1484 {
1485         EngineSettingsCB_Type *es_type;
1486
1487         /* Cleanup in case it existed. */
1488         es_type = BLI_findstring(lb, engine_name, offsetof(EngineSettingsCB_Type, name));
1489
1490         if (es_type) {
1491                 BLI_remlink(lb, es_type);
1492                 MEM_freeN(es_type);
1493         }
1494
1495         es_type = MEM_callocN(sizeof(EngineSettingsCB_Type), __func__);
1496         BLI_strncpy_utf8(es_type->name, engine_name, sizeof(es_type->name));
1497         es_type->callback = func;
1498         BLI_addtail(lb, es_type);
1499
1500         return es_type;
1501 }
1502
1503 void BKE_layer_collection_engine_settings_callback_register(
1504         Main *bmain, const char *engine_name, EngineSettingsCB func)
1505 {
1506         EngineSettingsCB_Type *es_type =
1507                 engine_settings_callback_register(engine_name, func, &R_layer_collection_engines_settings_callbacks);
1508
1509         if (bmain) {
1510                 /* Populate all of the collections of the scene with those settings. */
1511                 for (Scene *scene = bmain->scene.first; scene; scene = scene->id.next) {
1512                         create_layer_collection_engines_settings_scene(scene, es_type);
1513                 }
1514         }
1515 }
1516
1517 void BKE_view_layer_engine_settings_callback_register(
1518         Main *bmain, const char *engine_name, EngineSettingsCB func)
1519 {
1520         EngineSettingsCB_Type *es_type =
1521                 engine_settings_callback_register(engine_name, func, &R_view_layer_engines_settings_callbacks);
1522
1523         if (bmain) {
1524                 /* Populate all of the collections of the scene with those settings. */
1525                 for (Scene *scene = bmain->scene.first; scene; scene = scene->id.next) {
1526                         create_view_layer_engines_settings_scene(scene, es_type);
1527
1528                         for (ViewLayer *view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
1529                                 create_view_layer_engines_settings_layer(view_layer, es_type);
1530                         }
1531                 }
1532         }
1533 }
1534
1535 void BKE_layer_collection_engine_settings_callback_free(void)
1536 {
1537         BLI_freelistN(&R_layer_collection_engines_settings_callbacks);
1538 }
1539
1540 void BKE_view_layer_engine_settings_callback_free(void)
1541 {
1542         BLI_freelistN(&R_view_layer_engines_settings_callbacks);
1543 }
1544
1545 /**
1546  * Create a root IDProperty for this engine
1547  *
1548  * \param populate whether we want to pre-fill the collection with the default properties
1549  */
1550 static IDProperty *collection_engine_settings_create(EngineSettingsCB_Type *es_type, const bool populate)
1551 {
1552         IDProperty *props;
1553         IDPropertyTemplate val = {0};
1554
1555         props = IDP_New(IDP_GROUP, &val, es_type->name);
1556         props->subtype = IDP_GROUP_SUB_ENGINE_RENDER;
1557
1558         /* properties */
1559         if (populate) {
1560                 es_type->callback(NULL, props);
1561         }
1562
1563         return props;
1564 }
1565
1566 static void layer_collection_create_mode_settings_object(IDProperty *root, const bool populate)
1567 {
1568         IDProperty *props;
1569         IDPropertyTemplate val = {0};
1570
1571         props = IDP_New(IDP_GROUP, &val, "ObjectMode");
1572         props->subtype = IDP_GROUP_SUB_MODE_OBJECT;
1573
1574         /* properties */
1575         if (populate) {
1576                 OBJECT_collection_settings_create(props);
1577         }
1578
1579         IDP_AddToGroup(root, props);
1580 }
1581
1582 static void layer_collection_create_mode_settings_edit(IDProperty *root, const bool populate)
1583 {
1584         IDProperty *props;
1585         IDPropertyTemplate val = {0};
1586
1587         props = IDP_New(IDP_GROUP, &val, "EditMode");
1588         props->subtype = IDP_GROUP_SUB_MODE_EDIT;
1589
1590         /* properties */
1591         if (populate) {
1592                 EDIT_MESH_collection_settings_create(props);
1593         }
1594
1595         IDP_AddToGroup(root, props);
1596 }
1597
1598 static void layer_collection_create_mode_settings_paint_weight(IDProperty *root, const bool populate)
1599 {
1600         IDProperty *props;
1601         IDPropertyTemplate val = {0};
1602
1603         props = IDP_New(IDP_GROUP, &val, "WeightPaintMode");
1604         props->subtype = IDP_GROUP_SUB_MODE_PAINT_WEIGHT;
1605
1606         /* properties */
1607         if (populate) {
1608                 PAINT_WEIGHT_collection_settings_create(props);
1609         }
1610
1611         IDP_AddToGroup(root, props);
1612 }
1613
1614 static void layer_collection_create_mode_settings_paint_vertex(IDProperty *root, const bool populate)
1615 {
1616         IDProperty *props;
1617         IDPropertyTemplate val = {0};
1618
1619         props = IDP_New(IDP_GROUP, &val, "VertexPaintMode");
1620         props->subtype = IDP_GROUP_SUB_MODE_PAINT_VERTEX;
1621
1622         /* properties */
1623         if (populate) {
1624                 PAINT_VERTEX_collection_settings_create(props);
1625         }
1626
1627         IDP_AddToGroup(root, props);
1628 }
1629
1630 static void layer_collection_create_render_settings(IDProperty *root, const bool populate)
1631 {
1632         EngineSettingsCB_Type *es_type;
1633         for (es_type = R_layer_collection_engines_settings_callbacks.first; es_type; es_type = es_type->next) {
1634                 IDProperty *props = collection_engine_settings_create(es_type, populate);
1635                 IDP_AddToGroup(root, props);
1636         }
1637 }
1638
1639 static void view_layer_create_render_settings(IDProperty *root, const bool populate)
1640 {
1641         EngineSettingsCB_Type *es_type;
1642         for (es_type = R_view_layer_engines_settings_callbacks.first; es_type; es_type = es_type->next) {
1643                 IDProperty *props = collection_engine_settings_create(es_type, populate);
1644                 IDP_AddToGroup(root, props);
1645         }
1646 }
1647
1648 static void collection_create_mode_settings(IDProperty *root, const bool populate)
1649 {
1650         /* XXX TODO: put all those engines in the R_engines_settings_callbacks
1651          * and have IDP_AddToGroup outside the callbacks */
1652         layer_collection_create_mode_settings_object(root, populate);
1653         layer_collection_create_mode_settings_edit(root, populate);
1654         layer_collection_create_mode_settings_paint_weight(root, populate);
1655         layer_collection_create_mode_settings_paint_vertex(root, populate);
1656 }
1657
1658 static void layer_create_mode_settings(IDProperty *root, const bool populate)
1659 {
1660         TODO_LAYER; /* XXX like collection_create_mode_settings */
1661         UNUSED_VARS(root, populate);
1662 }
1663
1664 static int idproperty_group_subtype(const int mode_type)
1665 {
1666         int idgroup_type;
1667
1668         switch (mode_type) {
1669                 case COLLECTION_MODE_OBJECT:
1670                         idgroup_type = IDP_GROUP_SUB_MODE_OBJECT;
1671                         break;
1672                 case COLLECTION_MODE_EDIT:
1673                         idgroup_type = IDP_GROUP_SUB_MODE_EDIT;
1674                         break;
1675                 case COLLECTION_MODE_PAINT_WEIGHT:
1676                         idgroup_type = IDP_GROUP_SUB_MODE_PAINT_WEIGHT;
1677                         break;
1678                 case COLLECTION_MODE_PAINT_VERTEX:
1679                         idgroup_type = IDP_GROUP_SUB_MODE_PAINT_VERTEX;
1680                         break;
1681                 default:
1682                 case COLLECTION_MODE_NONE:
1683                         return IDP_GROUP_SUB_ENGINE_RENDER;
1684                         break;
1685         }
1686
1687         return idgroup_type;
1688 }
1689
1690 /**
1691  * Return collection enginne settings for either Object s of LayerCollection s
1692  */
1693 static IDProperty *collection_engine_get(
1694         IDProperty *root, const int type, const char *engine_name)
1695 {
1696         const int subtype = idproperty_group_subtype(type);
1697
1698         if (subtype == IDP_GROUP_SUB_ENGINE_RENDER) {
1699                 return IDP_GetPropertyFromGroup(root, engine_name);
1700         }
1701         else {
1702                 IDProperty *prop;
1703                 for (prop = root->data.group.first; prop; prop = prop->next) {
1704                         if (prop->subtype == subtype) {
1705                                 return prop;
1706                         }
1707                 }
1708         }
1709
1710         BLI_assert(false);
1711         return NULL;
1712 }
1713
1714 /**
1715  * Return collection engine settings from Object for specified engine of mode
1716  */
1717 IDProperty *BKE_layer_collection_engine_evaluated_get(Object *ob, const int type, const char *engine_name)
1718 {
1719         return collection_engine_get(ob->base_collection_properties, type, engine_name);
1720 }
1721 /**
1722  * Return layer collection engine settings for specified engine
1723  */
1724 IDProperty *BKE_layer_collection_engine_collection_get(LayerCollection *lc, const int type, const char *engine_name)
1725 {
1726         return collection_engine_get(lc->properties, type, engine_name);
1727 }
1728
1729 /**
1730  * Return layer collection engine settings for specified engine in the scene
1731  */
1732 IDProperty *BKE_layer_collection_engine_scene_get(Scene *scene, const int type, const char *engine_name)
1733 {
1734         return collection_engine_get(scene->collection_properties, type, engine_name);
1735 }
1736
1737 /**
1738  * Return scene layer engine settings for specified engine in the scene
1739  */
1740 IDProperty *BKE_view_layer_engine_scene_get(Scene *scene, const int type, const char *engine_name)
1741 {
1742         return collection_engine_get(scene->layer_properties, type, engine_name);
1743 }
1744
1745 /**
1746  * Return scene layer engine settings for specified engine
1747  */
1748 IDProperty *BKE_view_layer_engine_layer_get(ViewLayer *view_layer, const int type, const char *engine_name)
1749 {
1750         return collection_engine_get(view_layer->properties, type, engine_name);
1751 }
1752
1753 /**
1754  * Return scene layer evaluated engine settings for specified engine
1755  */
1756 IDProperty *BKE_view_layer_engine_evaluated_get(ViewLayer *view_layer, const int type, const char *engine_name)
1757 {
1758         return collection_engine_get(view_layer->properties_evaluated, type, engine_name);
1759 }
1760
1761 /* ---------------------------------------------------------------------- */
1762 /* Engine Settings Properties */
1763
1764 void BKE_collection_engine_property_add_float(IDProperty *props, const char *name, float value)
1765 {
1766         IDPropertyTemplate val = {0};
1767         val.f = value;
1768         IDP_AddToGroup(props, IDP_New(IDP_FLOAT, &val, name));
1769 }
1770
1771 void BKE_collection_engine_property_add_float_array(
1772         IDProperty *props, const char *name, const float *values, const int array_length)
1773 {
1774         IDPropertyTemplate val = {0};
1775         val.array.len = array_length;
1776         val.array.type = IDP_FLOAT;
1777
1778         IDProperty *idprop = IDP_New(IDP_ARRAY, &val, name);
1779         memcpy(IDP_Array(idprop), values, sizeof(float) * idprop->len);
1780         IDP_AddToGroup(props, idprop);
1781 }
1782
1783 void BKE_collection_engine_property_add_int(IDProperty *props, const char *name, int value)
1784 {
1785         IDPropertyTemplate val = {0};
1786         val.i = value;
1787         IDP_AddToGroup(props, IDP_New(IDP_INT, &val, name));
1788 }
1789
1790 void BKE_collection_engine_property_add_bool(IDProperty *props, const char *name, bool value)
1791 {
1792         IDPropertyTemplate val = {0};
1793         val.i = value;
1794         IDP_AddToGroup(props, IDP_New(IDP_INT, &val, name));
1795 }
1796
1797 int BKE_collection_engine_property_value_get_int(IDProperty *props, const char *name)
1798 {
1799         IDProperty *idprop = IDP_GetPropertyFromGroup(props, name);
1800         return idprop ? IDP_Int(idprop) : 0;
1801 }
1802
1803 float BKE_collection_engine_property_value_get_float(IDProperty *props, const char *name)
1804 {
1805         IDProperty *idprop = IDP_GetPropertyFromGroup(props, name);
1806         return idprop ? IDP_Float(idprop) : 0.0f;
1807 }
1808
1809 const float *BKE_collection_engine_property_value_get_float_array(IDProperty *props, const char *name)
1810 {
1811         IDProperty *idprop = IDP_GetPropertyFromGroup(props, name);
1812         return idprop ? IDP_Array(idprop) : NULL;
1813 }
1814
1815 bool BKE_collection_engine_property_value_get_bool(IDProperty *props, const char *name)
1816 {
1817         IDProperty *idprop = IDP_GetPropertyFromGroup(props, name);
1818         return idprop ? IDP_Int(idprop) : 0;
1819 }
1820
1821 void BKE_collection_engine_property_value_set_int(IDProperty *props, const char *name, int value)
1822 {
1823         IDProperty *idprop = IDP_GetPropertyFromGroup(props, name);
1824         IDP_Int(idprop) = value;
1825 }
1826
1827 void BKE_collection_engine_property_value_set_float(IDProperty *props, const char *name, float value)
1828 {
1829         IDProperty *idprop = IDP_GetPropertyFromGroup(props, name);
1830         IDP_Float(idprop) = value;
1831 }
1832
1833 void BKE_collection_engine_property_value_set_float_array(IDProperty *props, const char *name, const float *values)
1834 {
1835         IDProperty *idprop = IDP_GetPropertyFromGroup(props, name);
1836         memcpy(IDP_Array(idprop), values, sizeof(float) * idprop->len);
1837 }
1838
1839 void BKE_collection_engine_property_value_set_bool(IDProperty *props, const char *name, bool value)
1840 {
1841         IDProperty *idprop = IDP_GetPropertyFromGroup(props, name);
1842         IDP_Int(idprop) = value;
1843 }
1844
1845 /* Engine Settings recalculate  */
1846
1847 /* get all the default settings defined in scene and merge them here */
1848 static void collection_engine_settings_init(IDProperty *root, const bool populate)
1849 {
1850         /* render engines */
1851         layer_collection_create_render_settings(root, populate);
1852
1853         /* mode engines */
1854         collection_create_mode_settings(root, populate);
1855 }
1856
1857 /* get all the default settings defined in scene and merge them here */
1858 static void layer_engine_settings_init(IDProperty *root, const bool populate)
1859 {
1860         /* render engines */
1861         view_layer_create_render_settings(root, populate);
1862
1863         /* mode engines */
1864         layer_create_mode_settings(root, populate);
1865 }
1866
1867 /**
1868  * Initialize the layer collection render setings
1869  * It's used mainly for scenes
1870  */
1871 void BKE_layer_collection_engine_settings_create(IDProperty *root)
1872 {
1873         collection_engine_settings_init(root, true);
1874 }
1875
1876 /**
1877  * Initialize the render setings
1878  * It's used mainly for scenes
1879  */
1880 void BKE_view_layer_engine_settings_create(IDProperty *root)
1881 {
1882         layer_engine_settings_init(root, true);
1883 }
1884
1885 /**
1886  * Reference of IDProperty group scene collection settings
1887  * Used when reading blendfiles, to see if there is any missing settings.
1888  */
1889 static struct {
1890         struct {
1891                 IDProperty *collection_properties;
1892                 IDProperty *render_settings;
1893         } scene;
1894         IDProperty *view_layer;
1895         IDProperty *layer_collection;
1896 } root_reference = {
1897         .scene = {NULL, NULL},
1898         .view_layer = NULL,
1899         .layer_collection = NULL,
1900 };
1901
1902 /**
1903  * Free the reference scene collection settings IDProperty group.
1904  */
1905 static void engine_settings_validate_init(void)
1906 {
1907         IDPropertyTemplate val = {0};
1908
1909         /* LayerCollection engine settings. */
1910         if (root_reference.scene.collection_properties == NULL) {
1911                 root_reference.scene.collection_properties = IDP_New(IDP_GROUP, &val, ROOT_PROP);
1912                 collection_engine_settings_init(root_reference.scene.collection_properties, true);
1913         }
1914
1915         if (root_reference.layer_collection == NULL) {
1916                 root_reference.layer_collection = IDP_New(IDP_GROUP, &val, ROOT_PROP);
1917                 collection_engine_settings_init(root_reference.layer_collection, false);
1918         }
1919
1920         /* Render engine setting. */
1921         if (root_reference.scene.render_settings == NULL) {
1922                 root_reference.scene.render_settings = IDP_New(IDP_GROUP, &val, ROOT_PROP);
1923                 layer_engine_settings_init(root_reference.scene.render_settings, true);
1924         }
1925
1926         if (root_reference.view_layer == NULL) {
1927                 root_reference.view_layer = IDP_New(IDP_GROUP, &val, ROOT_PROP);
1928                 layer_engine_settings_init(root_reference.view_layer, false);
1929         }
1930 }
1931
1932 /**
1933  * Free the reference scene collection settings IDProperty group.
1934  */
1935 static void layer_collection_engine_settings_validate_free(void)
1936 {
1937         IDProperty *idprops[] = {
1938             root_reference.scene.render_settings,
1939             root_reference.scene.collection_properties,
1940             root_reference.view_layer,
1941             root_reference.layer_collection,
1942             NULL,
1943         };
1944
1945         IDProperty **idprop = &idprops[0];
1946         while (*idprop) {
1947                 if (*idprop) {
1948                         IDP_FreeProperty(*idprop);
1949                         MEM_freeN(*idprop);
1950                         *idprop = NULL;
1951                         idprop++;
1952                 }
1953         }
1954 }
1955
1956 /**
1957  * Make sure Scene has all required collection settings.
1958  */
1959 void BKE_layer_collection_engine_settings_validate_scene(Scene *scene)
1960 {
1961         if (root_reference.scene.collection_properties == NULL) {
1962                 engine_settings_validate_init();
1963         }
1964
1965         if (scene->collection_properties == NULL) {
1966                 IDPropertyTemplate val = {0};
1967                 scene->collection_properties = IDP_New(IDP_GROUP, &val, ROOT_PROP);
1968                 BKE_layer_collection_engine_settings_create(scene->collection_properties);
1969         }
1970         else {
1971                 IDP_MergeGroup(scene->collection_properties, root_reference.scene.collection_properties, false);
1972         }
1973 }
1974
1975 /**
1976  * Maker sure LayerCollection has all required collection settings.
1977  */
1978 void BKE_layer_collection_engine_settings_validate_collection(LayerCollection *lc)
1979 {
1980         if (root_reference.layer_collection == NULL) {
1981                 engine_settings_validate_init();
1982         }
1983
1984         BLI_assert(lc->properties != NULL);
1985         IDP_MergeGroup(lc->properties, root_reference.layer_collection, false);
1986 }
1987
1988 /**
1989  * Make sure Scene has all required collection settings.
1990  */
1991 void BKE_view_layer_engine_settings_validate_scene(Scene *scene)
1992 {
1993         if (root_reference.scene.render_settings == NULL) {
1994                 engine_settings_validate_init();
1995         }
1996
1997         if (scene->layer_properties == NULL) {
1998                 IDPropertyTemplate val = {0};
1999                 scene->layer_properties = IDP_New(IDP_GROUP, &val, ROOT_PROP);
2000                 BKE_view_layer_engine_settings_create(scene->layer_properties);
2001         }
2002         else {
2003                 IDP_MergeGroup(scene->layer_properties, root_reference.scene.render_settings, false);
2004         }
2005 }
2006
2007 /**
2008  * Make sure Scene has all required collection settings.
2009  */
2010 void BKE_view_layer_engine_settings_validate_layer(ViewLayer *view_layer)
2011 {
2012         if (root_reference.view_layer == NULL) {
2013                 engine_settings_validate_init();
2014         }
2015
2016         IDP_MergeGroup(view_layer->properties, root_reference.view_layer, false);
2017 }
2018
2019 /* ---------------------------------------------------------------------- */
2020 /* Iterators */
2021
2022 static void object_bases_iterator_begin(BLI_Iterator *iter, void *data_in, const int flag)
2023 {
2024         ViewLayer *view_layer = data_in;
2025         Base *base = view_layer->object_bases.first;
2026
2027         /* when there are no objects */
2028         if (base ==  NULL) {
2029                 iter->valid = false;
2030                 return;
2031         }
2032
2033         iter->data = base;
2034
2035         if ((base->flag & flag) == 0) {
2036                 object_bases_iterator_next(iter, flag);
2037         }
2038         else {
2039                 iter->current = base;
2040         }
2041 }
2042
2043 static void object_bases_iterator_next(BLI_Iterator *iter, const int flag)
2044 {
2045         Base *base = ((Base *)iter->data)->next;
2046
2047         while (base) {
2048                 if ((base->flag & flag) != 0) {
2049                         iter->current = base;
2050                         iter->data = base;
2051                         return;
2052                 }
2053                 base = base->next;
2054         }
2055
2056         iter->valid = false;
2057 }
2058
2059 static void objects_iterator_begin(BLI_Iterator *iter, void *data_in, const int flag)
2060 {
2061         object_bases_iterator_begin(iter, data_in, flag);
2062
2063         if (iter->valid) {
2064                 iter->current = ((Base *)iter->current)->object;
2065         }
2066 }
2067
2068 static void objects_iterator_next(BLI_Iterator *iter, const int flag)
2069 {
2070         object_bases_iterator_next(iter, flag);
2071
2072         if (iter->valid) {
2073                 iter->current = ((Base *)iter->current)->object;
2074         }
2075 }
2076
2077 void BKE_selected_objects_iterator_begin(BLI_Iterator *iter, void *data_in)
2078 {
2079         objects_iterator_begin(iter, data_in, BASE_SELECTED);
2080 }
2081
2082 void BKE_selected_objects_iterator_next(BLI_Iterator *iter)
2083 {
2084         objects_iterator_next(iter, BASE_SELECTED);
2085 }
2086
2087 void BKE_selected_objects_iterator_end(BLI_Iterator *UNUSED(iter))
2088 {
2089         /* do nothing */
2090 }
2091
2092 void BKE_visible_objects_iterator_begin(BLI_Iterator *iter, void *data_in)
2093 {
2094         objects_iterator_begin(iter, data_in, BASE_VISIBLED);
2095 }
2096
2097 void BKE_visible_objects_iterator_next(BLI_Iterator *iter)
2098 {
2099         objects_iterator_next(iter, BASE_VISIBLED);
2100 }
2101
2102 void BKE_visible_objects_iterator_end(BLI_Iterator *UNUSED(iter))
2103 {
2104         /* do nothing */
2105 }
2106
2107 void BKE_selected_bases_iterator_begin(BLI_Iterator *iter, void *data_in)
2108 {
2109         object_bases_iterator_begin(iter, data_in, BASE_SELECTED);
2110 }
2111
2112 void BKE_selected_bases_iterator_next(BLI_Iterator *iter)
2113 {
2114         object_bases_iterator_next(iter, BASE_SELECTED);
2115 }
2116
2117 void BKE_selected_bases_iterator_end(BLI_Iterator *UNUSED(iter))
2118 {
2119         /* do nothing */
2120 }
2121
2122 void BKE_visible_bases_iterator_begin(BLI_Iterator *iter, void *data_in)
2123 {
2124         object_bases_iterator_begin(iter, data_in, BASE_VISIBLED);
2125 }
2126
2127 void BKE_visible_bases_iterator_next(BLI_Iterator *iter)
2128 {
2129         object_bases_iterator_next(iter, BASE_VISIBLED);
2130 }
2131
2132 void BKE_visible_bases_iterator_end(BLI_Iterator *UNUSED(iter))
2133 {
2134         /* do nothing */
2135 }
2136
2137 void BKE_renderable_objects_iterator_begin(BLI_Iterator *iter, void *data_in)
2138 {
2139         ObjectsRenderableIteratorData *data = data_in;
2140
2141         /* Tag objects to prevent going over the same object twice. */
2142         for (Scene *scene = data->scene; scene; scene = scene->set) {
2143                 for (ViewLayer *view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
2144                         for (Base *base = view_layer->object_bases.first; base; base = base->next) {
2145                                  base->object->id.flag |= LIB_TAG_DOIT;
2146                         }
2147                 }
2148         }
2149
2150         ViewLayer *view_layer = data->scene->view_layers.first;
2151         data->iter.view_layer = view_layer;
2152
2153         data->base_temp.next = view_layer->object_bases.first;
2154         data->iter.base = &data->base_temp;
2155
2156         data->iter.set = NULL;
2157
2158         iter->data = data_in;
2159         BKE_renderable_objects_iterator_next(iter);
2160 }
2161
2162 void BKE_renderable_objects_iterator_next(BLI_Iterator *iter)
2163 {
2164         /* Set it early in case we need to exit and we are running from within a loop. */
2165         iter->skip = true;
2166
2167         ObjectsRenderableIteratorData *data = iter->data;
2168         Base *base = data->iter.base->next;
2169
2170         /* There is still a base in the current scene layer. */
2171         if (base != NULL) {
2172                 Object *ob = base->object;
2173
2174                 /* We need to set the iter.base even if the rest fail otherwise
2175                  * we keep checking the exactly same base over and over again. */
2176                 data->iter.base = base;
2177
2178                 if (ob->id.flag & LIB_TAG_DOIT) {
2179                         ob->id.flag &= ~LIB_TAG_DOIT;
2180
2181                         if ((base->flag & BASE_VISIBLED) != 0) {
2182                                 iter->skip = false;
2183                                 iter->current = ob;
2184                         }
2185                 }
2186                 return;
2187         }
2188
2189         /* Time to go to the next scene layer. */
2190         if (data->iter.set == NULL) {
2191                 while ((data->iter.view_layer = data->iter.view_layer->next)) {
2192                         ViewLayer *view_layer = data->iter.view_layer;
2193                         if (view_layer->flag & VIEW_LAYER_RENDER) {
2194                                 data->base_temp.next = view_layer->object_bases.first;
2195                                 data->iter.base = &data->base_temp;
2196                                 return;
2197                         }
2198                 }
2199
2200                 /* Setup the "set" for the next iteration. */
2201                 data->scene_temp.set = data->scene;
2202                 data->iter.set = &data->scene_temp;
2203                 return;
2204         }
2205
2206         /* Look for an object in the next set. */
2207         while ((data->iter.set = data->iter.set->set)) {
2208                 ViewLayer *view_layer = BKE_view_layer_from_scene_get(data->iter.set);
2209                 data->base_temp.next = view_layer->object_bases.first;
2210                 data->iter.base = &data->base_temp;
2211                 return;
2212         }
2213
2214         iter->valid = false;
2215 }
2216
2217 void BKE_renderable_objects_iterator_end(BLI_Iterator *UNUSED(iter))
2218 {
2219         /* Do nothing - iter->data was static allocated, we can't free it. */
2220 }
2221
2222 /* Evaluation  */
2223
2224 /**
2225  * Reset props
2226  *
2227  * If props_ref is pasted, copy props from it
2228  */
2229 static void idproperty_reset(IDProperty **props, IDProperty *props_ref)
2230 {
2231         IDPropertyTemplate val = {0};
2232
2233         if (*props) {
2234                 IDP_FreeProperty(*props);
2235                 MEM_freeN(*props);
2236         }
2237         *props = IDP_New(IDP_GROUP, &val, ROOT_PROP);
2238
2239         if (props_ref) {
2240                 IDP_MergeGroup(*props, props_ref, true);
2241         }
2242 }
2243
2244 void BKE_layer_eval_layer_collection_pre(const struct EvaluationContext *UNUSED(eval_ctx),
2245                                          ID *owner_id, ViewLayer *view_layer)
2246 {
2247         DEBUG_PRINT("%s on %s (%p)\n", __func__, view_layer->name, view_layer);
2248         Scene *scene = (GS(owner_id->name) == ID_SCE) ? (Scene *)owner_id : NULL;
2249
2250         for (Base *base = view_layer->object_bases.first; base != NULL; base = base->next) {
2251                 base->flag &= ~(BASE_VISIBLED | BASE_SELECTABLED);
2252                 idproperty_reset(&base->collection_properties, scene ? scene->collection_properties : NULL);
2253         }
2254
2255         /* Sync properties from scene to scene layer. */
2256         idproperty_reset(&view_layer->properties_evaluated, scene ? scene->layer_properties : NULL);
2257         IDP_MergeGroup(view_layer->properties_evaluated, view_layer->properties, true);
2258
2259         /* TODO(sergey): Is it always required? */
2260         view_layer->flag |= VIEW_LAYER_ENGINE_DIRTY;
2261 }
2262
2263 static const char *collection_type_lookup[] =
2264 {
2265     "None", /* COLLECTION_TYPE_NONE */
2266     "Group Internal", /* COLLECTION_TYPE_GROUP_INTERNAL */
2267 };
2268
2269 /**
2270  * \note We can't use layer_collection->flag because of 3 level nesting (where parent is visible, but not grand-parent)
2271  * So layer_collection->flag_evaluated is expected to be up to date with layer_collection->flag.
2272  */
2273 static bool layer_collection_visible_get(const EvaluationContext *eval_ctx, LayerCollection *layer_collection)
2274 {
2275         if (layer_collection->flag_evaluated & COLLECTION_DISABLED) {
2276                 return false;
2277         }
2278
2279         if (eval_ctx->mode == DAG_EVAL_VIEWPORT) {
2280                 return (layer_collection->flag_evaluated & COLLECTION_VIEWPORT) != 0;
2281         }
2282         else {
2283                 return (layer_collection->flag_evaluated & COLLECTION_RENDER) != 0;
2284         }
2285 }
2286
2287 void BKE_layer_eval_layer_collection(const EvaluationContext *eval_ctx,
2288                                      LayerCollection *layer_collection,
2289                                      LayerCollection *parent_layer_collection)
2290 {
2291         DEBUG_PRINT("%s on %s (%p) [%s], parent %s (%p) [%s]\n",
2292                     __func__,
2293                     layer_collection->scene_collection->name,
2294                     layer_collection->scene_collection,
2295                     collection_type_lookup[layer_collection->scene_collection->type],
2296                     (parent_layer_collection != NULL) ? parent_layer_collection->scene_collection->name : "NONE",
2297                     (parent_layer_collection != NULL) ? parent_layer_collection->scene_collection : NULL,
2298                     (parent_layer_collection != NULL) ? collection_type_lookup[parent_layer_collection->scene_collection->type] : "");
2299         BLI_assert(layer_collection != parent_layer_collection);
2300
2301         /* visibility */
2302         layer_collection->flag_evaluated = layer_collection->flag;
2303
2304         if (parent_layer_collection != NULL) {
2305                 if (layer_collection_visible_get(eval_ctx, parent_layer_collection) == false) {
2306                         layer_collection->flag_evaluated |= COLLECTION_DISABLED;
2307                 }
2308
2309                 if ((parent_layer_collection->flag_evaluated & COLLECTION_DISABLED) ||
2310                     (parent_layer_collection->flag_evaluated & COLLECTION_SELECTABLE) == 0)
2311                 {
2312                         layer_collection->flag_evaluated &= ~COLLECTION_SELECTABLE;
2313                 }
2314         }
2315
2316         const bool is_visible = layer_collection_visible_get(eval_ctx, layer_collection);
2317         const bool is_selectable = is_visible && ((layer_collection->flag_evaluated & COLLECTION_SELECTABLE) != 0);
2318
2319         /* overrides */
2320         if (is_visible) {
2321                 if (parent_layer_collection == NULL) {
2322                         idproperty_reset(&layer_collection->properties_evaluated, layer_collection->properties);
2323                 }
2324                 else {
2325                         idproperty_reset(&layer_collection->properties_evaluated, parent_layer_collection->properties_evaluated);
2326                         IDP_MergeGroup(layer_collection->properties_evaluated, layer_collection->properties, true);
2327                 }
2328         }
2329
2330         for (LinkData *link = layer_collection->object_bases.first; link != NULL; link = link->next) {
2331                 Base *base = link->data;
2332
2333                 if (is_visible) {
2334                         IDP_MergeGroup(base->collection_properties, layer_collection->properties_evaluated, true);
2335                         base->flag |= BASE_VISIBLED;
2336                 }
2337
2338                 if (is_selectable) {
2339                         base->flag |= BASE_SELECTABLED;
2340                 }
2341         }
2342 }
2343
2344 void BKE_layer_eval_layer_collection_post(const struct EvaluationContext *UNUSED(eval_ctx),
2345                                           ViewLayer *view_layer)
2346 {
2347         DEBUG_PRINT("%s on %s (%p)\n", __func__, view_layer->name, view_layer);
2348         /* if base is not selectabled, clear select */
2349         for (Base *base = view_layer->object_bases.first; base; base = base->next) {
2350                 if ((base->flag & BASE_SELECTABLED) == 0) {
2351                         base->flag &= ~BASE_SELECTED;
2352                 }
2353         }
2354 }
2355
2356 /**
2357  * Free any static allocated memory.
2358  */
2359 void BKE_layer_exit(void)
2360 {
2361         layer_collection_engine_settings_validate_free();
2362 }