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