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_from_workspace_get 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. */
1046         if (BLI_findptr(&lc->object_bases, base, offsetof(LinkData, data))) {
1047                 return;
1048         }
1049
1050         bool is_visible = ((lc->flag & COLLECTION_VIEWPORT) != 0) && ((lc->flag & COLLECTION_DISABLED) == 0);
1051         bool is_selectable = is_visible && ((lc->flag & COLLECTION_SELECTABLE) != 0);
1052
1053         if (is_visible) {
1054                 base->flag |= BASE_VISIBLED;
1055         }
1056
1057         if (is_selectable) {
1058                 base->flag |= BASE_SELECTABLED;
1059         }
1060
1061         BLI_addtail(&lc->object_bases, BLI_genericNodeN(base));
1062 }
1063
1064 static void layer_collection_object_remove(ViewLayer *view_layer, LayerCollection *lc, Object *ob)
1065 {
1066         Base *base;
1067         base = BKE_view_layer_base_find(view_layer, ob);
1068
1069         LinkData *link = BLI_findptr(&lc->object_bases, base, offsetof(LinkData, data));
1070         BLI_remlink(&lc->object_bases, link);
1071         MEM_freeN(link);
1072
1073         view_layer_object_base_unref(view_layer, base);
1074 }
1075
1076 static void layer_collection_objects_populate(ViewLayer *view_layer, LayerCollection *lc, ListBase *objects)
1077 {
1078         for (LinkData *link = objects->first; link; link = link->next) {
1079                 layer_collection_object_add(view_layer, lc, link->data);
1080         }
1081 }
1082
1083 static void layer_collection_populate(ViewLayer *view_layer, LayerCollection *lc, SceneCollection *sc)
1084 {
1085         layer_collection_objects_populate(view_layer, lc, &sc->objects);
1086
1087         for (SceneCollection *nsc = sc->scene_collections.first; nsc; nsc = nsc->next) {
1088                 layer_collection_add(view_layer, lc, nsc);
1089         }
1090 }
1091
1092 static LayerCollection *layer_collection_add(ViewLayer *view_layer, LayerCollection *parent, SceneCollection *sc)
1093 {
1094         IDPropertyTemplate val = {0};
1095         LayerCollection *lc = MEM_callocN(sizeof(LayerCollection), "Collection Base");
1096
1097         lc->scene_collection = sc;
1098         lc->flag = COLLECTION_SELECTABLE | COLLECTION_VIEWPORT | COLLECTION_RENDER;
1099
1100         lc->properties = IDP_New(IDP_GROUP, &val, ROOT_PROP);
1101         collection_engine_settings_init(lc->properties, false);
1102
1103         if (parent != NULL) {
1104                 BLI_addtail(&parent->layer_collections, lc);
1105         }
1106         else {
1107                 BLI_addtail(&view_layer->layer_collections, lc);
1108         }
1109
1110         layer_collection_populate(view_layer, lc, sc);
1111
1112         return lc;
1113 }
1114
1115 /* ---------------------------------------------------------------------- */
1116
1117 /**
1118  * See if render layer has the scene collection linked directly, or indirectly (nested)
1119  */
1120 bool BKE_view_layer_has_collection(ViewLayer *view_layer, const SceneCollection *sc)
1121 {
1122         for (LayerCollection *lc = view_layer->layer_collections.first; lc; lc = lc->next) {
1123                 if (find_layer_collection_by_scene_collection(lc, sc) != NULL) {
1124                         return true;
1125                 }
1126         }
1127         return false;
1128 }
1129
1130 /**
1131  * See if the object is in any of the scene layers of the scene
1132  */
1133 bool BKE_scene_has_object(Scene *scene, Object *ob)
1134 {
1135         for (ViewLayer *view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
1136                 Base *base = BKE_view_layer_base_find(view_layer, ob);
1137                 if (base) {
1138                         return true;
1139                 }
1140         }
1141         return false;
1142 }
1143
1144
1145 /* ---------------------------------------------------------------------- */
1146 /* Syncing */
1147
1148 static LayerCollection *find_layer_collection_by_scene_collection(LayerCollection *lc, const SceneCollection *sc)
1149 {
1150         if (lc->scene_collection == sc) {
1151                 return lc;
1152         }
1153
1154         for (LayerCollection *nlc = lc->layer_collections.first; nlc; nlc = nlc->next) {
1155                 LayerCollection *found = find_layer_collection_by_scene_collection(nlc, sc);
1156                 if (found) {
1157                         return found;
1158                 }
1159         }
1160         return NULL;
1161 }
1162
1163 /**
1164  * Add a new LayerCollection for all the ViewLayers that have sc_parent
1165  */
1166 void BKE_layer_sync_new_scene_collection(ID *owner_id, const SceneCollection *sc_parent, SceneCollection *sc)
1167 {
1168         for (ViewLayer *view_layer = BKE_view_layer_first_from_id(owner_id); view_layer; view_layer = view_layer->next) {
1169                 for (LayerCollection *lc = view_layer->layer_collections.first; lc; lc = lc->next) {
1170                         LayerCollection *lc_parent = find_layer_collection_by_scene_collection(lc, sc_parent);
1171                         if (lc_parent) {
1172                                 layer_collection_add(view_layer, lc_parent, sc);
1173                         }
1174                 }
1175         }
1176 }
1177
1178 /**
1179  * Add a corresponding ObjectBase to all the equivalent LayerCollection
1180  */
1181 void BKE_layer_sync_object_link(const ID *owner_id, SceneCollection *sc, Object *ob)
1182 {
1183         for (ViewLayer *view_layer = BKE_view_layer_first_from_id(owner_id); view_layer; view_layer = view_layer->next) {
1184                 for (LayerCollection *lc = view_layer->layer_collections.first; lc; lc = lc->next) {
1185                         LayerCollection *found = find_layer_collection_by_scene_collection(lc, sc);
1186                         if (found) {
1187                                 layer_collection_object_add(view_layer, found, ob);
1188                         }
1189                 }
1190         }
1191 }
1192
1193 /**
1194  * Remove the equivalent object base to all layers that have this collection
1195  */
1196 void BKE_layer_sync_object_unlink(const ID *owner_id, SceneCollection *sc, Object *ob)
1197 {
1198         for (ViewLayer *view_layer = BKE_view_layer_first_from_id(owner_id); view_layer; view_layer = view_layer->next) {
1199                 for (LayerCollection *lc = view_layer->layer_collections.first; lc; lc = lc->next) {
1200                         LayerCollection *found = find_layer_collection_by_scene_collection(lc, sc);
1201                         if (found) {
1202                                 layer_collection_object_remove(view_layer, found, ob);
1203                         }
1204                 }
1205         }
1206 }
1207
1208 /* ---------------------------------------------------------------------- */
1209 /* Override */
1210
1211 /**
1212  * Add a new datablock override
1213  */
1214 void BKE_override_view_layer_datablock_add(ViewLayer *view_layer, int id_type, const char *data_path, const ID *owner_id)
1215 {
1216         UNUSED_VARS(view_layer, id_type, data_path, owner_id);
1217         TODO_LAYER_OVERRIDE;
1218 }
1219
1220 /**
1221  * Add a new int override
1222  */
1223 void BKE_override_view_layer_int_add(ViewLayer *view_layer, int id_type, const char *data_path, const int value)
1224 {
1225         UNUSED_VARS(view_layer, id_type, data_path, value);
1226         TODO_LAYER_OVERRIDE;
1227 }
1228
1229 /**
1230  * Add a new boolean override
1231  */
1232 void BKE_override_layer_collection_boolean_add(struct LayerCollection *layer_collection, int id_type, const char *data_path, const bool value)
1233 {
1234         UNUSED_VARS(layer_collection, id_type, data_path, value);
1235         TODO_LAYER_OVERRIDE;
1236 }
1237
1238 /* ---------------------------------------------------------------------- */
1239 /* Engine Settings */
1240
1241 ListBase R_layer_collection_engines_settings_callbacks = {NULL, NULL};
1242 ListBase R_view_layer_engines_settings_callbacks = {NULL, NULL};
1243
1244 typedef struct EngineSettingsCB_Type {
1245         struct EngineSettingsCB_Type *next, *prev;
1246
1247         char name[MAX_NAME]; /* engine name */
1248
1249         EngineSettingsCB callback;
1250
1251 } EngineSettingsCB_Type;
1252
1253 static void create_engine_settings_scene(IDProperty *root, EngineSettingsCB_Type *es_type)
1254 {
1255         if (collection_engine_get(root, COLLECTION_MODE_NONE, es_type->name)) {
1256                 return;
1257         }
1258
1259         IDProperty *props = collection_engine_settings_create(es_type, true);
1260         IDP_AddToGroup(root, props);
1261 }
1262
1263 static void create_layer_collection_engine_settings_scene(Scene *scene, EngineSettingsCB_Type *es_type)
1264 {
1265         create_engine_settings_scene(scene->collection_properties, es_type);
1266 }
1267
1268 static void create_view_layer_engine_settings_scene(Scene *scene, EngineSettingsCB_Type *es_type)
1269 {
1270         create_engine_settings_scene(scene->layer_properties, es_type);
1271 }
1272
1273 static void create_layer_collection_engine_settings_collection(LayerCollection *lc, EngineSettingsCB_Type *es_type)
1274 {
1275         if (BKE_layer_collection_engine_collection_get(lc, COLLECTION_MODE_NONE, es_type->name)) {
1276                 return;
1277         }
1278
1279         IDProperty *props = collection_engine_settings_create(es_type, false);
1280         IDP_AddToGroup(lc->properties, props);
1281
1282         for (LayerCollection *lcn = lc->layer_collections.first; lcn; lcn = lcn->next) {
1283                 create_layer_collection_engine_settings_collection(lcn, es_type);
1284         }
1285 }
1286
1287 static void create_layer_collection_engines_settings_scene(Scene *scene, EngineSettingsCB_Type *es_type)
1288 {
1289         /* Populate the scene with the new settings. */
1290         create_layer_collection_engine_settings_scene(scene, es_type);
1291
1292         for (ViewLayer *view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
1293                 for (LayerCollection *lc = view_layer->layer_collections.first; lc; lc = lc->next) {
1294                         create_layer_collection_engine_settings_collection(lc, es_type);
1295                 }
1296         }
1297 }
1298
1299 static void create_view_layer_engines_settings_scene(Scene *scene, EngineSettingsCB_Type *es_type)
1300 {
1301         /* Populate the scene with the new settings. */
1302         create_view_layer_engine_settings_scene(scene, es_type);
1303 }
1304
1305 static void create_view_layer_engines_settings_layer(ViewLayer *view_layer, EngineSettingsCB_Type *es_type)
1306 {
1307         if (BKE_view_layer_engine_layer_get(view_layer, COLLECTION_MODE_NONE, es_type->name)) {
1308                 return;
1309         }
1310
1311         IDProperty *props = collection_engine_settings_create(es_type, false);
1312         IDP_AddToGroup(view_layer->properties, props);
1313 }
1314
1315 static EngineSettingsCB_Type *engine_settings_callback_register(const char *engine_name, EngineSettingsCB func, ListBase *lb)
1316 {
1317         EngineSettingsCB_Type *es_type;
1318
1319         /* Cleanup in case it existed. */
1320         es_type = BLI_findstring(lb, engine_name, offsetof(EngineSettingsCB_Type, name));
1321
1322         if (es_type) {
1323                 BLI_remlink(lb, es_type);
1324                 MEM_freeN(es_type);
1325         }
1326
1327         es_type = MEM_callocN(sizeof(EngineSettingsCB_Type), __func__);
1328         BLI_strncpy_utf8(es_type->name, engine_name, sizeof(es_type->name));
1329         es_type->callback = func;
1330         BLI_addtail(lb, es_type);
1331
1332         return es_type;
1333 }
1334
1335 void BKE_layer_collection_engine_settings_callback_register(
1336         Main *bmain, const char *engine_name, EngineSettingsCB func)
1337 {
1338         EngineSettingsCB_Type *es_type =
1339                 engine_settings_callback_register(engine_name, func, &R_layer_collection_engines_settings_callbacks);
1340
1341         if (bmain) {
1342                 /* Populate all of the collections of the scene with those settings. */
1343                 for (Scene *scene = bmain->scene.first; scene; scene = scene->id.next) {
1344                         create_layer_collection_engines_settings_scene(scene, es_type);
1345                 }
1346         }
1347 }
1348
1349 void BKE_view_layer_engine_settings_callback_register(
1350         Main *bmain, const char *engine_name, EngineSettingsCB func)
1351 {
1352         EngineSettingsCB_Type *es_type =
1353                 engine_settings_callback_register(engine_name, func, &R_view_layer_engines_settings_callbacks);
1354
1355         if (bmain) {
1356                 /* Populate all of the collections of the scene with those settings. */
1357                 for (Scene *scene = bmain->scene.first; scene; scene = scene->id.next) {
1358                         create_view_layer_engines_settings_scene(scene, es_type);
1359
1360                         for (ViewLayer *view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
1361                                 create_view_layer_engines_settings_layer(view_layer, es_type);
1362                         }
1363                 }
1364         }
1365 }
1366
1367 void BKE_layer_collection_engine_settings_callback_free(void)
1368 {
1369         BLI_freelistN(&R_layer_collection_engines_settings_callbacks);
1370 }
1371
1372 void BKE_view_layer_engine_settings_callback_free(void)
1373 {
1374         BLI_freelistN(&R_view_layer_engines_settings_callbacks);
1375 }
1376
1377 /**
1378  * Create a root IDProperty for this engine
1379  *
1380  * \param populate whether we want to pre-fill the collection with the default properties
1381  */
1382 static IDProperty *collection_engine_settings_create(EngineSettingsCB_Type *es_type, const bool populate)
1383 {
1384         IDProperty *props;
1385         IDPropertyTemplate val = {0};
1386
1387         props = IDP_New(IDP_GROUP, &val, es_type->name);
1388         props->subtype = IDP_GROUP_SUB_ENGINE_RENDER;
1389
1390         /* properties */
1391         if (populate) {
1392                 es_type->callback(NULL, props);
1393         }
1394
1395         return props;
1396 }
1397
1398 static void layer_collection_create_mode_settings_object(IDProperty *root, const bool populate)
1399 {
1400         IDProperty *props;
1401         IDPropertyTemplate val = {0};
1402
1403         props = IDP_New(IDP_GROUP, &val, "ObjectMode");
1404         props->subtype = IDP_GROUP_SUB_MODE_OBJECT;
1405
1406         /* properties */
1407         if (populate) {
1408                 OBJECT_collection_settings_create(props);
1409         }
1410
1411         IDP_AddToGroup(root, props);
1412 }
1413
1414 static void layer_collection_create_mode_settings_edit(IDProperty *root, const bool populate)
1415 {
1416         IDProperty *props;
1417         IDPropertyTemplate val = {0};
1418
1419         props = IDP_New(IDP_GROUP, &val, "EditMode");
1420         props->subtype = IDP_GROUP_SUB_MODE_EDIT;
1421
1422         /* properties */
1423         if (populate) {
1424                 EDIT_MESH_collection_settings_create(props);
1425         }
1426
1427         IDP_AddToGroup(root, props);
1428 }
1429
1430 static void layer_collection_create_mode_settings_paint_weight(IDProperty *root, const bool populate)
1431 {
1432         IDProperty *props;
1433         IDPropertyTemplate val = {0};
1434
1435         props = IDP_New(IDP_GROUP, &val, "WeightPaintMode");
1436         props->subtype = IDP_GROUP_SUB_MODE_PAINT_WEIGHT;
1437
1438         /* properties */
1439         if (populate) {
1440                 PAINT_WEIGHT_collection_settings_create(props);
1441         }
1442
1443         IDP_AddToGroup(root, props);
1444 }
1445
1446 static void layer_collection_create_mode_settings_paint_vertex(IDProperty *root, const bool populate)
1447 {
1448         IDProperty *props;
1449         IDPropertyTemplate val = {0};
1450
1451         props = IDP_New(IDP_GROUP, &val, "VertexPaintMode");
1452         props->subtype = IDP_GROUP_SUB_MODE_PAINT_VERTEX;
1453
1454         /* properties */
1455         if (populate) {
1456                 PAINT_VERTEX_collection_settings_create(props);
1457         }
1458
1459         IDP_AddToGroup(root, props);
1460 }
1461
1462 static void layer_collection_create_render_settings(IDProperty *root, const bool populate)
1463 {
1464         EngineSettingsCB_Type *es_type;
1465         for (es_type = R_layer_collection_engines_settings_callbacks.first; es_type; es_type = es_type->next) {
1466                 IDProperty *props = collection_engine_settings_create(es_type, populate);
1467                 IDP_AddToGroup(root, props);
1468         }
1469 }
1470
1471 static void view_layer_create_render_settings(IDProperty *root, const bool populate)
1472 {
1473         EngineSettingsCB_Type *es_type;
1474         for (es_type = R_view_layer_engines_settings_callbacks.first; es_type; es_type = es_type->next) {
1475                 IDProperty *props = collection_engine_settings_create(es_type, populate);
1476                 IDP_AddToGroup(root, props);
1477         }
1478 }
1479
1480 static void collection_create_mode_settings(IDProperty *root, const bool populate)
1481 {
1482         /* XXX TODO: put all those engines in the R_engines_settings_callbacks
1483          * and have IDP_AddToGroup outside the callbacks */
1484         layer_collection_create_mode_settings_object(root, populate);
1485         layer_collection_create_mode_settings_edit(root, populate);
1486         layer_collection_create_mode_settings_paint_weight(root, populate);
1487         layer_collection_create_mode_settings_paint_vertex(root, populate);
1488 }
1489
1490 static void layer_create_mode_settings(IDProperty *root, const bool populate)
1491 {
1492         TODO_LAYER; /* XXX like collection_create_mode_settings */
1493         UNUSED_VARS(root, populate);
1494 }
1495
1496 static int idproperty_group_subtype(const int mode_type)
1497 {
1498         int idgroup_type;
1499
1500         switch (mode_type) {
1501                 case COLLECTION_MODE_OBJECT:
1502                         idgroup_type = IDP_GROUP_SUB_MODE_OBJECT;
1503                         break;
1504                 case COLLECTION_MODE_EDIT:
1505                         idgroup_type = IDP_GROUP_SUB_MODE_EDIT;
1506                         break;
1507                 case COLLECTION_MODE_PAINT_WEIGHT:
1508                         idgroup_type = IDP_GROUP_SUB_MODE_PAINT_WEIGHT;
1509                         break;
1510                 case COLLECTION_MODE_PAINT_VERTEX:
1511                         idgroup_type = IDP_GROUP_SUB_MODE_PAINT_VERTEX;
1512                         break;
1513                 default:
1514                 case COLLECTION_MODE_NONE:
1515                         return IDP_GROUP_SUB_ENGINE_RENDER;
1516                         break;
1517         }
1518
1519         return idgroup_type;
1520 }
1521
1522 /**
1523  * Return collection enginne settings for either Object s of LayerCollection s
1524  */
1525 static IDProperty *collection_engine_get(
1526         IDProperty *root, const int type, const char *engine_name)
1527 {
1528         const int subtype = idproperty_group_subtype(type);
1529
1530         if (subtype == IDP_GROUP_SUB_ENGINE_RENDER) {
1531                 return IDP_GetPropertyFromGroup(root, engine_name);
1532         }
1533         else {
1534                 IDProperty *prop;
1535                 for (prop = root->data.group.first; prop; prop = prop->next) {
1536                         if (prop->subtype == subtype) {
1537                                 return prop;
1538                         }
1539                 }
1540         }
1541
1542         BLI_assert(false);
1543         return NULL;
1544 }
1545
1546 /**
1547  * Return collection engine settings from Object for specified engine of mode
1548  */
1549 IDProperty *BKE_layer_collection_engine_evaluated_get(Object *ob, const int type, const char *engine_name)
1550 {
1551         return collection_engine_get(ob->base_collection_properties, type, engine_name);
1552 }
1553 /**
1554  * Return layer collection engine settings for specified engine
1555  */
1556 IDProperty *BKE_layer_collection_engine_collection_get(LayerCollection *lc, const int type, const char *engine_name)
1557 {
1558         return collection_engine_get(lc->properties, type, engine_name);
1559 }
1560
1561 /**
1562  * Return layer collection engine settings for specified engine in the scene
1563  */
1564 IDProperty *BKE_layer_collection_engine_scene_get(Scene *scene, const int type, const char *engine_name)
1565 {
1566         return collection_engine_get(scene->collection_properties, type, engine_name);
1567 }
1568
1569 /**
1570  * Return scene layer engine settings for specified engine in the scene
1571  */
1572 IDProperty *BKE_view_layer_engine_scene_get(Scene *scene, const int type, const char *engine_name)
1573 {
1574         return collection_engine_get(scene->layer_properties, type, engine_name);
1575 }
1576
1577 /**
1578  * Return scene layer engine settings for specified engine
1579  */
1580 IDProperty *BKE_view_layer_engine_layer_get(ViewLayer *view_layer, const int type, const char *engine_name)
1581 {
1582         return collection_engine_get(view_layer->properties, type, engine_name);
1583 }
1584
1585 /**
1586  * Return scene layer evaluated engine settings for specified engine
1587  */
1588 IDProperty *BKE_view_layer_engine_evaluated_get(ViewLayer *view_layer, const int type, const char *engine_name)
1589 {
1590         return collection_engine_get(view_layer->properties_evaluated, type, engine_name);
1591 }
1592
1593 /* ---------------------------------------------------------------------- */
1594 /* Engine Settings Properties */
1595
1596 void BKE_collection_engine_property_add_float(IDProperty *props, const char *name, float value)
1597 {
1598         IDPropertyTemplate val = {0};
1599         val.f = value;
1600         IDP_AddToGroup(props, IDP_New(IDP_FLOAT, &val, name));
1601 }
1602
1603 void BKE_collection_engine_property_add_float_array(
1604         IDProperty *props, const char *name, const float *values, const int array_length)
1605 {
1606         IDPropertyTemplate val = {0};
1607         val.array.len = array_length;
1608         val.array.type = IDP_FLOAT;
1609
1610         IDProperty *idprop = IDP_New(IDP_ARRAY, &val, name);
1611         memcpy(IDP_Array(idprop), values, sizeof(float) * idprop->len);
1612         IDP_AddToGroup(props, idprop);
1613 }
1614
1615 void BKE_collection_engine_property_add_int(IDProperty *props, const char *name, int value)
1616 {
1617         IDPropertyTemplate val = {0};
1618         val.i = value;
1619         IDP_AddToGroup(props, IDP_New(IDP_INT, &val, name));
1620 }
1621
1622 void BKE_collection_engine_property_add_bool(IDProperty *props, const char *name, bool value)
1623 {
1624         IDPropertyTemplate val = {0};
1625         val.i = value;
1626         IDP_AddToGroup(props, IDP_New(IDP_INT, &val, name));
1627 }
1628
1629 int BKE_collection_engine_property_value_get_int(IDProperty *props, const char *name)
1630 {
1631         IDProperty *idprop = IDP_GetPropertyFromGroup(props, name);
1632         return idprop ? IDP_Int(idprop) : 0;
1633 }
1634
1635 float BKE_collection_engine_property_value_get_float(IDProperty *props, const char *name)
1636 {
1637         IDProperty *idprop = IDP_GetPropertyFromGroup(props, name);
1638         return idprop ? IDP_Float(idprop) : 0.0f;
1639 }
1640
1641 const float *BKE_collection_engine_property_value_get_float_array(IDProperty *props, const char *name)
1642 {
1643         IDProperty *idprop = IDP_GetPropertyFromGroup(props, name);
1644         return idprop ? IDP_Array(idprop) : NULL;
1645 }
1646
1647 bool BKE_collection_engine_property_value_get_bool(IDProperty *props, const char *name)
1648 {
1649         IDProperty *idprop = IDP_GetPropertyFromGroup(props, name);
1650         return idprop ? IDP_Int(idprop) : 0;
1651 }
1652
1653 void BKE_collection_engine_property_value_set_int(IDProperty *props, const char *name, int value)
1654 {
1655         IDProperty *idprop = IDP_GetPropertyFromGroup(props, name);
1656         IDP_Int(idprop) = value;
1657 }
1658
1659 void BKE_collection_engine_property_value_set_float(IDProperty *props, const char *name, float value)
1660 {
1661         IDProperty *idprop = IDP_GetPropertyFromGroup(props, name);
1662         IDP_Float(idprop) = value;
1663 }
1664
1665 void BKE_collection_engine_property_value_set_float_array(IDProperty *props, const char *name, const float *values)
1666 {
1667         IDProperty *idprop = IDP_GetPropertyFromGroup(props, name);
1668         memcpy(IDP_Array(idprop), values, sizeof(float) * idprop->len);
1669 }
1670
1671 void BKE_collection_engine_property_value_set_bool(IDProperty *props, const char *name, bool value)
1672 {
1673         IDProperty *idprop = IDP_GetPropertyFromGroup(props, name);
1674         IDP_Int(idprop) = value;
1675 }
1676
1677 /* Engine Settings recalculate  */
1678
1679 /* get all the default settings defined in scene and merge them here */
1680 static void collection_engine_settings_init(IDProperty *root, const bool populate)
1681 {
1682         /* render engines */
1683         layer_collection_create_render_settings(root, populate);
1684
1685         /* mode engines */
1686         collection_create_mode_settings(root, populate);
1687 }
1688
1689 /* get all the default settings defined in scene and merge them here */
1690 static void layer_engine_settings_init(IDProperty *root, const bool populate)
1691 {
1692         /* render engines */
1693         view_layer_create_render_settings(root, populate);
1694
1695         /* mode engines */
1696         layer_create_mode_settings(root, populate);
1697 }
1698
1699 /**
1700  * Initialize the layer collection render setings
1701  * It's used mainly for scenes
1702  */
1703 void BKE_layer_collection_engine_settings_create(IDProperty *root)
1704 {
1705         collection_engine_settings_init(root, true);
1706 }
1707
1708 /**
1709  * Initialize the render setings
1710  * It's used mainly for scenes
1711  */
1712 void BKE_view_layer_engine_settings_create(IDProperty *root)
1713 {
1714         layer_engine_settings_init(root, true);
1715 }
1716
1717 /**
1718  * Reference of IDProperty group scene collection settings
1719  * Used when reading blendfiles, to see if there is any missing settings.
1720  */
1721 static struct {
1722         struct {
1723                 IDProperty *collection_properties;
1724                 IDProperty *render_settings;
1725         } scene;
1726         IDProperty *view_layer;
1727         IDProperty *layer_collection;
1728 } root_reference = {
1729         .scene = {NULL, NULL},
1730         .view_layer = NULL,
1731         .layer_collection = NULL,
1732 };
1733
1734 /**
1735  * Free the reference scene collection settings IDProperty group.
1736  */
1737 static void engine_settings_validate_init(void)
1738 {
1739         IDPropertyTemplate val = {0};
1740
1741         /* LayerCollection engine settings. */
1742         if (root_reference.scene.collection_properties == NULL) {
1743                 root_reference.scene.collection_properties = IDP_New(IDP_GROUP, &val, ROOT_PROP);
1744                 collection_engine_settings_init(root_reference.scene.collection_properties, true);
1745         }
1746
1747         if (root_reference.layer_collection == NULL) {
1748                 root_reference.layer_collection = IDP_New(IDP_GROUP, &val, ROOT_PROP);
1749                 collection_engine_settings_init(root_reference.layer_collection, false);
1750         }
1751
1752         /* Render engine setting. */
1753         if (root_reference.scene.render_settings == NULL) {
1754                 root_reference.scene.render_settings = IDP_New(IDP_GROUP, &val, ROOT_PROP);
1755                 layer_engine_settings_init(root_reference.scene.render_settings, true);
1756         }
1757
1758         if (root_reference.view_layer == NULL) {
1759                 root_reference.view_layer = IDP_New(IDP_GROUP, &val, ROOT_PROP);
1760                 layer_engine_settings_init(root_reference.view_layer, false);
1761         }
1762 }
1763
1764 /**
1765  * Free the reference scene collection settings IDProperty group.
1766  */
1767 static void layer_collection_engine_settings_validate_free(void)
1768 {
1769         IDProperty *idprops[] = {
1770             root_reference.scene.render_settings,
1771             root_reference.scene.collection_properties,
1772             root_reference.view_layer,
1773             root_reference.layer_collection,
1774             NULL,
1775         };
1776
1777         IDProperty **idprop = &idprops[0];
1778         while (*idprop) {
1779                 if (*idprop) {
1780                         IDP_FreeProperty(*idprop);
1781                         MEM_freeN(*idprop);
1782                         *idprop = NULL;
1783                         idprop++;
1784                 }
1785         }
1786 }
1787
1788 /**
1789  * Make sure Scene has all required collection settings.
1790  */
1791 void BKE_layer_collection_engine_settings_validate_scene(Scene *scene)
1792 {
1793         if (root_reference.scene.collection_properties == NULL) {
1794                 engine_settings_validate_init();
1795         }
1796
1797         if (scene->collection_properties == NULL) {
1798                 IDPropertyTemplate val = {0};
1799                 scene->collection_properties = IDP_New(IDP_GROUP, &val, ROOT_PROP);
1800                 BKE_layer_collection_engine_settings_create(scene->collection_properties);
1801         }
1802         else {
1803                 IDP_MergeGroup(scene->collection_properties, root_reference.scene.collection_properties, false);
1804         }
1805 }
1806
1807 /**
1808  * Maker sure LayerCollection has all required collection settings.
1809  */
1810 void BKE_layer_collection_engine_settings_validate_collection(LayerCollection *lc)
1811 {
1812         if (root_reference.layer_collection == NULL) {
1813                 engine_settings_validate_init();
1814         }
1815
1816         BLI_assert(lc->properties != NULL);
1817         IDP_MergeGroup(lc->properties, root_reference.layer_collection, false);
1818 }
1819
1820 /**
1821  * Make sure Scene has all required collection settings.
1822  */
1823 void BKE_view_layer_engine_settings_validate_scene(Scene *scene)
1824 {
1825         if (root_reference.scene.render_settings == NULL) {
1826                 engine_settings_validate_init();
1827         }
1828
1829         if (scene->layer_properties == NULL) {
1830                 IDPropertyTemplate val = {0};
1831                 scene->layer_properties = IDP_New(IDP_GROUP, &val, ROOT_PROP);
1832                 BKE_view_layer_engine_settings_create(scene->layer_properties);
1833         }
1834         else {
1835                 IDP_MergeGroup(scene->layer_properties, root_reference.scene.render_settings, false);
1836         }
1837 }
1838
1839 /**
1840  * Make sure Scene has all required collection settings.
1841  */
1842 void BKE_view_layer_engine_settings_validate_layer(ViewLayer *view_layer)
1843 {
1844         if (root_reference.view_layer == NULL) {
1845                 engine_settings_validate_init();
1846         }
1847
1848         IDP_MergeGroup(view_layer->properties, root_reference.view_layer, false);
1849 }
1850
1851 /* ---------------------------------------------------------------------- */
1852 /* Iterators */
1853
1854 static void object_bases_iterator_begin(BLI_Iterator *iter, void *data_in, const int flag)
1855 {
1856         ViewLayer *view_layer = data_in;
1857         Base *base = view_layer->object_bases.first;
1858
1859         /* when there are no objects */
1860         if (base ==  NULL) {
1861                 iter->valid = false;
1862                 return;
1863         }
1864
1865         iter->data = base;
1866
1867         if ((base->flag & flag) == 0) {
1868                 object_bases_iterator_next(iter, flag);
1869         }
1870         else {
1871                 iter->current = base;
1872         }
1873 }
1874
1875 static void object_bases_iterator_next(BLI_Iterator *iter, const int flag)
1876 {
1877         Base *base = ((Base *)iter->data)->next;
1878
1879         while (base) {
1880                 if ((base->flag & flag) != 0) {
1881                         iter->current = base;
1882                         iter->data = base;
1883                         return;
1884                 }
1885                 base = base->next;
1886         }
1887
1888         iter->valid = false;
1889 }
1890
1891 static void objects_iterator_begin(BLI_Iterator *iter, void *data_in, const int flag)
1892 {
1893         object_bases_iterator_begin(iter, data_in, flag);
1894
1895         if (iter->valid) {
1896                 iter->current = ((Base *)iter->current)->object;
1897         }
1898 }
1899
1900 static void objects_iterator_next(BLI_Iterator *iter, const int flag)
1901 {
1902         object_bases_iterator_next(iter, flag);
1903
1904         if (iter->valid) {
1905                 iter->current = ((Base *)iter->current)->object;
1906         }
1907 }
1908
1909 void BKE_selected_objects_iterator_begin(BLI_Iterator *iter, void *data_in)
1910 {
1911         objects_iterator_begin(iter, data_in, BASE_SELECTED);
1912 }
1913
1914 void BKE_selected_objects_iterator_next(BLI_Iterator *iter)
1915 {
1916         objects_iterator_next(iter, BASE_SELECTED);
1917 }
1918
1919 void BKE_selected_objects_iterator_end(BLI_Iterator *UNUSED(iter))
1920 {
1921         /* do nothing */
1922 }
1923
1924 void BKE_visible_objects_iterator_begin(BLI_Iterator *iter, void *data_in)
1925 {
1926         objects_iterator_begin(iter, data_in, BASE_VISIBLED);
1927 }
1928
1929 void BKE_visible_objects_iterator_next(BLI_Iterator *iter)
1930 {
1931         objects_iterator_next(iter, BASE_VISIBLED);
1932 }
1933
1934 void BKE_visible_objects_iterator_end(BLI_Iterator *UNUSED(iter))
1935 {
1936         /* do nothing */
1937 }
1938
1939 void BKE_selected_bases_iterator_begin(BLI_Iterator *iter, void *data_in)
1940 {
1941         object_bases_iterator_begin(iter, data_in, BASE_SELECTED);
1942 }
1943
1944 void BKE_selected_bases_iterator_next(BLI_Iterator *iter)
1945 {
1946         object_bases_iterator_next(iter, BASE_SELECTED);
1947 }
1948
1949 void BKE_selected_bases_iterator_end(BLI_Iterator *UNUSED(iter))
1950 {
1951         /* do nothing */
1952 }
1953
1954 void BKE_visible_bases_iterator_begin(BLI_Iterator *iter, void *data_in)
1955 {
1956         object_bases_iterator_begin(iter, data_in, BASE_VISIBLED);
1957 }
1958
1959 void BKE_visible_bases_iterator_next(BLI_Iterator *iter)
1960 {
1961         object_bases_iterator_next(iter, BASE_VISIBLED);
1962 }
1963
1964 void BKE_visible_bases_iterator_end(BLI_Iterator *UNUSED(iter))
1965 {
1966         /* do nothing */
1967 }
1968
1969 void BKE_renderable_objects_iterator_begin(BLI_Iterator *iter, void *data_in)
1970 {
1971         ObjectsRenderableIteratorData *data = data_in;
1972
1973         /* Tag objects to prevent going over the same object twice. */
1974         for (Scene *scene = data->scene; scene; scene = scene->set) {
1975                 for (ViewLayer *view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
1976                         for (Base *base = view_layer->object_bases.first; base; base = base->next) {
1977                                  base->object->id.flag |= LIB_TAG_DOIT;
1978                         }
1979                 }
1980         }
1981
1982         ViewLayer *view_layer = data->scene->view_layers.first;
1983         data->iter.view_layer = view_layer;
1984
1985         data->base_temp.next = view_layer->object_bases.first;
1986         data->iter.base = &data->base_temp;
1987
1988         data->iter.set = NULL;
1989
1990         iter->data = data_in;
1991         BKE_renderable_objects_iterator_next(iter);
1992 }
1993
1994 void BKE_renderable_objects_iterator_next(BLI_Iterator *iter)
1995 {
1996         /* Set it early in case we need to exit and we are running from within a loop. */
1997         iter->skip = true;
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                 /* We need to set the iter.base even if the rest fail otherwise
2007                  * we keep checking the exactly same base over and over again. */
2008                 data->iter.base = base;
2009
2010                 if (ob->id.flag & LIB_TAG_DOIT) {
2011                         ob->id.flag &= ~LIB_TAG_DOIT;
2012
2013                         if ((base->flag & BASE_VISIBLED) != 0) {
2014                                 iter->skip = false;
2015                                 iter->current = ob;
2016                         }
2017                 }
2018                 return;
2019         }
2020
2021         /* Time to go to the next scene layer. */
2022         if (data->iter.set == NULL) {
2023                 while ((data->iter.view_layer = data->iter.view_layer->next)) {
2024                         ViewLayer *view_layer = data->iter.view_layer;
2025                         if (view_layer->flag & VIEW_LAYER_RENDER) {
2026                                 data->base_temp.next = view_layer->object_bases.first;
2027                                 data->iter.base = &data->base_temp;
2028                                 return;
2029                         }
2030                 }
2031
2032                 /* Setup the "set" for the next iteration. */
2033                 data->scene_temp.set = data->scene;
2034                 data->iter.set = &data->scene_temp;
2035                 return;
2036         }
2037
2038         /* Look for an object in the next set. */
2039         while ((data->iter.set = data->iter.set->set)) {
2040                 ViewLayer *view_layer = BKE_view_layer_from_scene_get(data->iter.set);
2041                 data->base_temp.next = view_layer->object_bases.first;
2042                 data->iter.base = &data->base_temp;
2043                 return;
2044         }
2045
2046         iter->valid = false;
2047 }
2048
2049 void BKE_renderable_objects_iterator_end(BLI_Iterator *UNUSED(iter))
2050 {
2051         /* Do nothing - iter->data was static allocated, we can't free it. */
2052 }
2053
2054 /* Evaluation  */
2055
2056 /**
2057  * Reset props
2058  *
2059  * If props_ref is pasted, copy props from it
2060  */
2061 static void idproperty_reset(IDProperty **props, IDProperty *props_ref)
2062 {
2063         IDPropertyTemplate val = {0};
2064
2065         if (*props) {
2066                 IDP_FreeProperty(*props);
2067                 MEM_freeN(*props);
2068         }
2069         *props = IDP_New(IDP_GROUP, &val, ROOT_PROP);
2070
2071         if (props_ref) {
2072                 IDP_MergeGroup(*props, props_ref, true);
2073         }
2074 }
2075
2076 void BKE_layer_eval_layer_collection_pre(const struct EvaluationContext *UNUSED(eval_ctx),
2077                                          ID *owner_id, ViewLayer *view_layer)
2078 {
2079         DEBUG_PRINT("%s on %s (%p)\n", __func__, view_layer->name, view_layer);
2080         Scene *scene = (GS(owner_id->name) == ID_SCE) ? (Scene *)owner_id : NULL;
2081
2082         for (Base *base = view_layer->object_bases.first; base != NULL; base = base->next) {
2083                 base->flag &= ~(BASE_VISIBLED | BASE_SELECTABLED);
2084                 idproperty_reset(&base->collection_properties, scene ? scene->collection_properties : NULL);
2085         }
2086
2087         /* Sync properties from scene to scene layer. */
2088         idproperty_reset(&view_layer->properties_evaluated, scene ? scene->layer_properties : NULL);
2089         IDP_MergeGroup(view_layer->properties_evaluated, view_layer->properties, true);
2090
2091         /* TODO(sergey): Is it always required? */
2092         view_layer->flag |= VIEW_LAYER_ENGINE_DIRTY;
2093 }
2094
2095 static const char *collection_type_lookup[] =
2096 {
2097     "None", /* COLLECTION_TYPE_NONE */
2098     "Group Internal", /* COLLECTION_TYPE_GROUP_INTERNAL */
2099 };
2100
2101 /**
2102  * \note We can't use layer_collection->flag because of 3 level nesting (where parent is visible, but not grand-parent)
2103  * So layer_collection->flag_evaluated is expected to be up to date with layer_collection->flag.
2104  */
2105 static bool layer_collection_visible_get(const EvaluationContext *eval_ctx, LayerCollection *layer_collection)
2106 {
2107         if (layer_collection->flag_evaluated & COLLECTION_DISABLED) {
2108                 return false;
2109         }
2110
2111         if (eval_ctx->mode == DAG_EVAL_VIEWPORT) {
2112                 return (layer_collection->flag_evaluated & COLLECTION_VIEWPORT) != 0;
2113         }
2114         else {
2115                 return (layer_collection->flag_evaluated & COLLECTION_RENDER) != 0;
2116         }
2117 }
2118
2119 void BKE_layer_eval_layer_collection(const EvaluationContext *eval_ctx,
2120                                      LayerCollection *layer_collection,
2121                                      LayerCollection *parent_layer_collection)
2122 {
2123         DEBUG_PRINT("%s on %s (%p) [%s], parent %s (%p) [%s]\n",
2124                     __func__,
2125                     layer_collection->scene_collection->name,
2126                     layer_collection->scene_collection,
2127                     collection_type_lookup[layer_collection->scene_collection->type],
2128                     (parent_layer_collection != NULL) ? parent_layer_collection->scene_collection->name : "NONE",
2129                     (parent_layer_collection != NULL) ? parent_layer_collection->scene_collection : NULL,
2130                     (parent_layer_collection != NULL) ? collection_type_lookup[parent_layer_collection->scene_collection->type] : "");
2131         BLI_assert(layer_collection != parent_layer_collection);
2132
2133         /* visibility */
2134         layer_collection->flag_evaluated = layer_collection->flag;
2135
2136         if (parent_layer_collection != NULL) {
2137                 if (layer_collection_visible_get(eval_ctx, parent_layer_collection) == false) {
2138                         layer_collection->flag_evaluated |= COLLECTION_DISABLED;
2139                 }
2140
2141                 if ((parent_layer_collection->flag_evaluated & COLLECTION_DISABLED) ||
2142                     (parent_layer_collection->flag_evaluated & COLLECTION_SELECTABLE) == 0)
2143                 {
2144                         layer_collection->flag_evaluated &= ~COLLECTION_SELECTABLE;
2145                 }
2146         }
2147
2148         const bool is_visible = layer_collection_visible_get(eval_ctx, layer_collection);
2149         const bool is_selectable = is_visible && ((layer_collection->flag_evaluated & COLLECTION_SELECTABLE) != 0);
2150
2151         /* overrides */
2152         if (is_visible) {
2153                 if (parent_layer_collection == NULL) {
2154                         idproperty_reset(&layer_collection->properties_evaluated, layer_collection->properties);
2155                 }
2156                 else {
2157                         idproperty_reset(&layer_collection->properties_evaluated, parent_layer_collection->properties_evaluated);
2158                         IDP_MergeGroup(layer_collection->properties_evaluated, layer_collection->properties, true);
2159                 }
2160         }
2161
2162         for (LinkData *link = layer_collection->object_bases.first; link != NULL; link = link->next) {
2163                 Base *base = link->data;
2164
2165                 if (is_visible) {
2166                         IDP_MergeGroup(base->collection_properties, layer_collection->properties_evaluated, true);
2167                         base->flag |= BASE_VISIBLED;
2168                 }
2169
2170                 if (is_selectable) {
2171                         base->flag |= BASE_SELECTABLED;
2172                 }
2173         }
2174 }
2175
2176 void BKE_layer_eval_layer_collection_post(const struct EvaluationContext *UNUSED(eval_ctx),
2177                                           ViewLayer *view_layer)
2178 {
2179         DEBUG_PRINT("%s on %s (%p)\n", __func__, view_layer->name, view_layer);
2180         /* if base is not selectabled, clear select */
2181         for (Base *base = view_layer->object_bases.first; base; base = base->next) {
2182                 if ((base->flag & BASE_SELECTABLED) == 0) {
2183                         base->flag &= ~BASE_SELECTED;
2184                 }
2185         }
2186 }
2187
2188 /**
2189  * Free any static allocated memory.
2190  */
2191 void BKE_layer_exit(void)
2192 {
2193         layer_collection_engine_settings_validate_free();
2194 }