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