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