T55291: Matcaps were reset when loading file
[blender.git] / source / blender / blenloader / intern / versioning_280.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
24 /** \file blender/blenloader/intern/versioning_280.c
25  *  \ingroup blenloader
26  */
27
28 /* allow readfile to use deprecated functionality */
29 #define DNA_DEPRECATED_ALLOW
30
31 #include <string.h>
32 #include <float.h>
33
34 #include "BLI_listbase.h"
35 #include "BLI_math.h"
36 #include "BLI_mempool.h"
37 #include "BLI_string.h"
38 #include "BLI_string_utf8.h"
39 #include "BLI_utildefines.h"
40
41 #include "DNA_object_types.h"
42 #include "DNA_camera_types.h"
43 #include "DNA_constraint_types.h"
44 #include "DNA_gpu_types.h"
45 #include "DNA_group_types.h"
46 #include "DNA_lamp_types.h"
47 #include "DNA_layer_types.h"
48 #include "DNA_lightprobe_types.h"
49 #include "DNA_material_types.h"
50 #include "DNA_mesh_types.h"
51 #include "DNA_particle_types.h"
52 #include "DNA_scene_types.h"
53 #include "DNA_screen_types.h"
54 #include "DNA_view3d_types.h"
55 #include "DNA_genfile.h"
56 #include "DNA_workspace_types.h"
57
58 #include "BKE_collection.h"
59 #include "BKE_constraint.h"
60 #include "BKE_customdata.h"
61 #include "BKE_freestyle.h"
62 #include "BKE_idprop.h"
63 #include "BKE_layer.h"
64 #include "BKE_main.h"
65 #include "BKE_mesh.h"
66 #include "BKE_node.h"
67 #include "BKE_report.h"
68 #include "BKE_scene.h"
69 #include "BKE_screen.h"
70 #include "BKE_studiolight.h"
71 #include "BKE_workspace.h"
72
73 #include "BLO_readfile.h"
74 #include "readfile.h"
75
76 #include "MEM_guardedalloc.h"
77
78 static bScreen *screen_parent_find(const bScreen *screen)
79 {
80         /* can avoid lookup if screen state isn't maximized/full (parent and child store the same state) */
81         if (ELEM(screen->state, SCREENMAXIMIZED, SCREENFULL)) {
82                 for (const ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
83                         if (sa->full && sa->full != screen) {
84                                 BLI_assert(sa->full->state == screen->state);
85                                 return sa->full;
86                         }
87                 }
88         }
89
90         return NULL;
91 }
92
93 static void do_version_workspaces_create_from_screens(Main *bmain)
94 {
95         for (bScreen *screen = bmain->screen.first; screen; screen = screen->id.next) {
96                 const bScreen *screen_parent = screen_parent_find(screen);
97                 Scene *scene = screen->scene;
98                 WorkSpace *workspace;
99                 ViewLayer *layer = BLI_findlink(&scene->view_layers, scene->r.actlay);
100                 if (!layer) {
101                         layer = BKE_view_layer_default_view(scene);
102                 }
103
104                 if (screen_parent) {
105                         /* fullscreen with "Back to Previous" option, don't create
106                          * a new workspace, add layout workspace containing parent */
107                         workspace = BLI_findstring(
108                                 &bmain->workspaces, screen_parent->id.name + 2, offsetof(ID, name) + 2);
109                 }
110                 else {
111                         workspace = BKE_workspace_add(bmain, screen->id.name + 2);
112                 }
113                 BKE_workspace_layout_add(workspace, screen, screen->id.name + 2);
114                 BKE_workspace_view_layer_set(workspace, layer, scene);
115         }
116 }
117
118 static void do_version_area_change_space_to_space_action(ScrArea *area, const Scene *scene)
119 {
120         SpaceType *stype = BKE_spacetype_from_id(SPACE_ACTION);
121         SpaceAction *saction = (SpaceAction *)stype->new(area, scene);
122         ARegion *region_channels;
123
124         /* Properly free current regions */
125         for (ARegion *region = area->regionbase.first; region; region = region->next) {
126                 BKE_area_region_free(area->type, region);
127         }
128         BLI_freelistN(&area->regionbase);
129
130         area->type = stype;
131         area->spacetype = stype->spaceid;
132
133         BLI_addhead(&area->spacedata, saction);
134         area->regionbase = saction->regionbase;
135         BLI_listbase_clear(&saction->regionbase);
136
137         /* Different defaults for timeline */
138         region_channels = BKE_area_find_region_type(area, RGN_TYPE_CHANNELS);
139         region_channels->flag |= RGN_FLAG_HIDDEN;
140
141         saction->mode = SACTCONT_TIMELINE;
142         saction->ads.flag |= ADS_FLAG_SUMMARY_COLLAPSED;
143 }
144
145 /**
146  * \brief After lib-link versioning for new workspace design.
147  *
148  * - Adds a workspace for (almost) each screen of the old file
149  *   and adds the needed workspace-layout to wrap the screen.
150  * - Active screen isn't stored directly in window anymore, but in the active workspace.
151  * - Active scene isn't stored in screen anymore, but in window.
152  * - Create workspace instance hook for each window.
153  *
154  * \note Some of the created workspaces might be deleted again in case of reading the default startup.blend.
155  */
156 static void do_version_workspaces_after_lib_link(Main *bmain)
157 {
158         BLI_assert(BLI_listbase_is_empty(&bmain->workspaces));
159
160         do_version_workspaces_create_from_screens(bmain);
161
162         for (wmWindowManager *wm = bmain->wm.first; wm; wm = wm->id.next) {
163                 for (wmWindow *win = wm->windows.first; win; win = win->next) {
164                         bScreen *screen_parent = screen_parent_find(win->screen);
165                         bScreen *screen = screen_parent ? screen_parent : win->screen;
166                         WorkSpace *workspace = BLI_findstring(&bmain->workspaces, screen->id.name + 2, offsetof(ID, name) + 2);
167                         ListBase *layouts = BKE_workspace_layouts_get(workspace);
168
169                         win->workspace_hook = BKE_workspace_instance_hook_create(bmain);
170
171                         BKE_workspace_active_set(win->workspace_hook, workspace);
172                         BKE_workspace_active_layout_set(win->workspace_hook, layouts->first);
173
174                         win->scene = screen->scene;
175                         /* Deprecated from now on! */
176                         win->screen = NULL;
177                 }
178         }
179
180         for (bScreen *screen = bmain->screen.first; screen; screen = screen->id.next) {
181                 /* Deprecated from now on! */
182                 BLI_freelistN(&screen->scene->transform_spaces);
183                 screen->scene = NULL;
184         }
185 }
186
187 #ifdef USE_COLLECTION_COMPAT_28
188 enum {
189         COLLECTION_DEPRECATED_VISIBLE    = (1 << 0),
190         COLLECTION_DEPRECATED_VIEWPORT   = (1 << 0),
191         COLLECTION_DEPRECATED_SELECTABLE = (1 << 1),
192         COLLECTION_DEPRECATED_DISABLED   = (1 << 2),
193         COLLECTION_DEPRECATED_RENDER     = (1 << 3),
194 };
195
196 static void do_version_view_layer_visibility(ViewLayer *view_layer)
197 {
198         /* Convert from deprecated VISIBLE flag to DISABLED */
199         LayerCollection *lc;
200         for (lc = view_layer->layer_collections.first;
201              lc;
202              lc = lc->next)
203         {
204                 if (lc->flag & COLLECTION_DEPRECATED_DISABLED) {
205                         lc->flag &= ~COLLECTION_DEPRECATED_DISABLED;
206                 }
207
208                 if ((lc->flag & COLLECTION_DEPRECATED_VISIBLE) == 0) {
209                         lc->flag |= COLLECTION_DEPRECATED_DISABLED;
210                 }
211
212                 lc->flag |= COLLECTION_DEPRECATED_VIEWPORT | COLLECTION_DEPRECATED_RENDER;
213         }
214 }
215
216 static void do_version_layer_collection_pre(
217         ViewLayer *view_layer,
218         ListBase *lb,
219         GSet *enabled_set,
220         GSet *selectable_set)
221 {
222         /* Convert from deprecated DISABLED to new layer collection and collection flags */
223         for (LayerCollection *lc = lb->first; lc; lc = lc->next) {
224                 if (lc->scene_collection) {
225                         if (!(lc->flag & COLLECTION_DEPRECATED_DISABLED)) {
226                                 BLI_gset_insert(enabled_set, lc->scene_collection);
227                         }
228                         if (lc->flag & COLLECTION_DEPRECATED_SELECTABLE) {
229                                 BLI_gset_insert(selectable_set, lc->scene_collection);
230                         }
231                 }
232
233                 do_version_layer_collection_pre(view_layer, &lc->layer_collections, enabled_set, selectable_set);
234         }
235 }
236
237 static void do_version_layer_collection_post(
238         ViewLayer *view_layer,
239         ListBase *lb,
240         GSet *enabled_set,
241         GSet *selectable_set,
242         GHash *collection_map)
243 {
244         /* Apply layer collection exclude flags. */
245         for (LayerCollection *lc = lb->first; lc; lc = lc->next) {
246                 if (!(lc->collection->flag & COLLECTION_IS_MASTER)) {
247                         SceneCollection *sc = BLI_ghash_lookup(collection_map, lc->collection);
248                         const bool enabled = (sc && BLI_gset_haskey(enabled_set, sc));
249                         const bool selectable = (sc && BLI_gset_haskey(selectable_set, sc));
250
251                         if (!enabled) {
252                                 lc->flag |= LAYER_COLLECTION_EXCLUDE;
253                         }
254                         if (enabled && !selectable) {
255                                 lc->collection->flag |= COLLECTION_RESTRICT_SELECT;
256                         }
257                 }
258
259                 do_version_layer_collection_post(
260                         view_layer, &lc->layer_collections, enabled_set, selectable_set, collection_map);
261         }
262 }
263
264 static void do_version_scene_collection_convert(
265         Main *bmain,
266         ID *id,
267         SceneCollection *sc,
268         Collection *collection,
269         GHash *collection_map)
270 {
271         if (collection_map) {
272                 BLI_ghash_insert(collection_map, collection, sc);
273         }
274
275         for (SceneCollection *nsc = sc->scene_collections.first; nsc;) {
276                 SceneCollection *nsc_next = nsc->next;
277                 Collection *ncollection = BKE_collection_add(bmain, collection, nsc->name);
278                 ncollection->id.lib = id->lib;
279                 do_version_scene_collection_convert(bmain, id, nsc, ncollection, collection_map);
280                 nsc = nsc_next;
281         }
282
283         for (LinkData *link = sc->objects.first; link; link = link->next) {
284                 Object *ob = link->data;
285                 if (ob) {
286                         BKE_collection_object_add(bmain, collection, ob);
287                         id_us_min(&ob->id);
288                 }
289         }
290
291         BLI_freelistN(&sc->objects);
292         MEM_freeN(sc);
293 }
294
295 static void do_version_group_collection_to_collection(Main *bmain, Collection *group)
296 {
297         /* Convert old 2.8 group collections to new unified collections. */
298         if (group->collection) {
299                 do_version_scene_collection_convert(bmain, &group->id, group->collection, group, NULL);
300         }
301
302         group->collection = NULL;
303         group->view_layer = NULL;
304         id_fake_user_set(&group->id);
305 }
306
307 static void do_version_scene_collection_to_collection(Main *bmain, Scene *scene)
308 {
309         /* Convert old 2.8 scene collections to new unified collections. */
310
311         /* Temporarily clear view layers so we don't do any layer collection syncing
312          * and destroy old flags that we want to restore. */
313         ListBase view_layers = scene->view_layers;
314         BLI_listbase_clear(&scene->view_layers);
315
316         if (!scene->master_collection) {
317                 scene->master_collection = BKE_collection_master_add();
318         }
319
320         /* Convert scene collections. */
321         GHash *collection_map = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, __func__);
322         if (scene->collection) {
323                 do_version_scene_collection_convert(bmain, &scene->id, scene->collection, scene->master_collection, collection_map);
324                 scene->collection = NULL;
325         }
326
327         scene->view_layers = view_layers;
328
329         /* Convert layer collections. */
330         ViewLayer *view_layer;
331         for (view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
332                 GSet *enabled_set = BLI_gset_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, __func__);
333                 GSet *selectable_set = BLI_gset_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, __func__);
334
335                 do_version_layer_collection_pre(
336                         view_layer, &view_layer->layer_collections, enabled_set, selectable_set);
337
338                 BKE_layer_collection_sync(scene, view_layer);
339
340                 do_version_layer_collection_post(
341                         view_layer, &view_layer->layer_collections, enabled_set, selectable_set, collection_map);
342
343                 BLI_gset_free(enabled_set, NULL);
344                 BLI_gset_free(selectable_set, NULL);
345
346                 BKE_layer_collection_sync(scene, view_layer);
347         }
348
349         BLI_ghash_free(collection_map, NULL, NULL);
350 }
351 #endif
352
353
354 enum {
355         DO_VERSION_COLLECTION_VISIBLE     = 0,
356         DO_VERSION_COLLECTION_HIDE        = 1,
357         DO_VERSION_COLLECTION_HIDE_RENDER = 2,
358         DO_VERSION_COLLECTION_HIDE_ALL    = 3,
359 };
360
361 static void do_version_layers_to_collections(Main *bmain, Scene *scene)
362 {
363         /* Since we don't have access to FileData we check the (always valid) first
364          * render layer instead. */
365         if (!scene->master_collection) {
366                 scene->master_collection = BKE_collection_master_add();
367         }
368
369         if (scene->view_layers.first) {
370                 return;
371         }
372
373         /* Create collections from layers. */
374         Collection *collection_master = BKE_collection_master(scene);
375
376         struct DoVersionSceneCollections {
377                 Collection *collections[20];
378                 int created;
379                 const char *suffix;
380                 int flag;
381         } collections[] =
382         {
383                 {
384                         .collections = {NULL},
385                         .created = 0,
386                         .suffix = "",
387                         .flag = 0,
388                 },
389                 {
390                         .collections = {NULL},
391                         .created = 0,
392                         .suffix = " - Hide Viewport",
393                         .flag = COLLECTION_RESTRICT_VIEW,
394                 },
395                 {
396                         .collections = {NULL},
397                         .created = 0,
398                         .suffix = " - Hide Render",
399                         .flag = COLLECTION_RESTRICT_RENDER,
400                 },
401                 {
402                         .collections = {NULL},
403                         .created = 0,
404                         .suffix = " - Hide Render All",
405                         .flag = COLLECTION_RESTRICT_VIEW | COLLECTION_RESTRICT_RENDER,
406                 }
407         };
408
409         for (int layer = 0; layer < 20; layer++) {
410                 for (Base *base = scene->base.first; base; base = base->next) {
411                         if (base->lay & (1 << layer)) {
412                                 int collection_index = -1;
413                                 if ((base->object->restrictflag & OB_RESTRICT_VIEW) &&
414                                     (base->object->restrictflag & OB_RESTRICT_RENDER))
415                                 {
416                                         collection_index = DO_VERSION_COLLECTION_HIDE_ALL;
417                                 }
418                                 else if (base->object->restrictflag & OB_RESTRICT_VIEW) {
419                                         collection_index = DO_VERSION_COLLECTION_HIDE;
420                                 }
421                                 else if (base->object->restrictflag & OB_RESTRICT_RENDER) {
422                                         collection_index = DO_VERSION_COLLECTION_HIDE_RENDER;
423                                 }
424                                 else {
425                                         collection_index = DO_VERSION_COLLECTION_VISIBLE;
426                                 }
427
428                                 /* Create collections when needed only. */
429                                 if ((collections[collection_index].created & (1 << layer)) == 0) {
430                                         char name[MAX_NAME];
431
432                                         if ((collections[DO_VERSION_COLLECTION_VISIBLE].created & (1 << layer)) == 0) {
433                                                 BLI_snprintf(name,
434                                                              sizeof(collection_master->id.name),
435                                                              "Collection %d%s",
436                                                              layer + 1,
437                                                              collections[DO_VERSION_COLLECTION_VISIBLE].suffix);
438
439                                                 Collection *collection = BKE_collection_add(bmain, collection_master, name);
440                                                 collection->id.lib = scene->id.lib;
441                                                 collection->flag |= collections[DO_VERSION_COLLECTION_VISIBLE].flag;
442                                                 collections[DO_VERSION_COLLECTION_VISIBLE].collections[layer] = collection;
443                                                 collections[DO_VERSION_COLLECTION_VISIBLE].created |= (1 << layer);
444
445                                                 if (!(scene->lay & (1 << layer))) {
446                                                         collection->flag |= COLLECTION_RESTRICT_VIEW | COLLECTION_RESTRICT_RENDER;
447                                                 }
448                                         }
449
450                                         if (collection_index != DO_VERSION_COLLECTION_VISIBLE) {
451                                                 Collection *collection_parent;
452                                                 collection_parent = collections[DO_VERSION_COLLECTION_VISIBLE].collections[layer];
453                                                 BLI_snprintf(name,
454                                                              sizeof(collection_master->id.name),
455                                                              "Collection %d%s",
456                                                              layer + 1,
457                                                              collections[collection_index].suffix);
458
459                                                 Collection *collection = BKE_collection_add(bmain, collection_parent, name);
460                                                 collection->id.lib = scene->id.lib;
461                                                 collection->flag |= collections[collection_index].flag;
462                                                 collections[collection_index].collections[layer] = collection;
463                                                 collections[collection_index].created |= (1 << layer);
464
465                                                 if (!(scene->lay & (1 << layer))) {
466                                                         collection->flag |= COLLECTION_RESTRICT_VIEW | COLLECTION_RESTRICT_RENDER;
467                                                 }
468                                         }
469                                 }
470
471                                 /* Note usually this would do slow collection syncing for view layers,
472                                  * but since no view layers exists yet at this point it's fast. */
473                                 BKE_collection_object_add(
474                                         bmain,
475                                         collections[collection_index].collections[layer], base->object);
476                         }
477
478                         if (base->flag & SELECT) {
479                                 base->object->flag |= SELECT;
480                         }
481                         else {
482                                 base->object->flag &= ~SELECT;
483                         }
484                 }
485         }
486
487         /* Re-order the nested hidden collections. */
488         CollectionChild *child_parent = collection_master->children.first;
489         Collection *collection_parent = (child_parent) ? child_parent->collection : NULL;
490
491         for (int layer = 0; layer < 20; layer++) {
492                 if (collections[DO_VERSION_COLLECTION_VISIBLE].created & (1 << layer)) {
493                         CollectionChild *hide_child = BLI_findptr(
494                                 &collection_parent->children,
495                                 collections[DO_VERSION_COLLECTION_HIDE].collections[layer],
496                                 offsetof(CollectionChild, collection));
497
498                         if ((collections[DO_VERSION_COLLECTION_HIDE].created & (1 << layer)) &&
499                             (hide_child != collection_parent->children.first))
500                         {
501                                 BLI_listbase_swaplinks(
502                                         &collection_parent->children,
503                                         hide_child,
504                                         collection_parent->children.first);
505                         }
506
507                         CollectionChild *hide_all_child = BLI_findptr(
508                                 &collection_parent->children,
509                                 collections[DO_VERSION_COLLECTION_HIDE_ALL].collections[layer],
510                                 offsetof(CollectionChild, collection));
511
512                         if ((collections[DO_VERSION_COLLECTION_HIDE_ALL].created & (1 << layer)) &&
513                             (hide_all_child != collection_parent->children.last))
514                         {
515                                 BLI_listbase_swaplinks(
516                                         &collection_parent->children,
517                                         hide_all_child,
518                                         collection_parent->children.last);
519                         }
520
521                         child_parent = child_parent->next;
522                         collection_parent = (child_parent) ? child_parent->collection : NULL;
523                 }
524         }
525         BLI_assert(collection_parent == NULL);
526
527         /* Handle legacy render layers. */
528         bool have_override = false;
529
530         for (SceneRenderLayer *srl = scene->r.layers.first; srl; srl = srl->next) {
531                 ViewLayer *view_layer = BKE_view_layer_add(scene, srl->name);
532
533                 if (srl->samples != 0) {
534                         have_override = true;
535
536                         /* It is up to the external engine to handle
537                          * its own doversion in this case. */
538                         BKE_override_view_layer_int_add(
539                                 view_layer,
540                                 ID_SCE,
541                                 "samples",
542                                 srl->samples);
543                 }
544
545                 if (srl->mat_override) {
546                         have_override = true;
547
548                         BKE_override_view_layer_datablock_add(
549                                 view_layer,
550                                 ID_MA,
551                                 "self",
552                                 (ID *)srl->mat_override);
553                 }
554
555                 if (srl->layflag & SCE_LAY_DISABLE) {
556                         view_layer->flag &= ~VIEW_LAYER_RENDER;
557                 }
558
559                 if ((srl->layflag & SCE_LAY_FRS) == 0) {
560                         view_layer->flag &= ~VIEW_LAYER_FREESTYLE;
561                 }
562
563                 /* XXX If we are to keep layflag it should be merged with flag (dfelinto). */
564                 view_layer->layflag = srl->layflag;
565                 /* XXX Not sure if we should keep the passes (dfelinto). */
566                 view_layer->passflag = srl->passflag;
567                 view_layer->pass_xor = srl->pass_xor;
568                 view_layer->pass_alpha_threshold = srl->pass_alpha_threshold;
569
570                 BKE_freestyle_config_free(&view_layer->freestyle_config, true);
571                 view_layer->freestyle_config = srl->freestyleConfig;
572                 view_layer->id_properties = srl->prop;
573
574                 /* Set exclusion and overrides. */
575                 for (int layer = 0; layer < 20; layer++) {
576                         if (collections[DO_VERSION_COLLECTION_VISIBLE].created & (1 << layer)) {
577                                 Collection *collection = collections[DO_VERSION_COLLECTION_VISIBLE].collections[layer];
578                                 LayerCollection *lc = BKE_layer_collection_first_from_scene_collection(view_layer, collection);
579
580                                 if (srl->lay_exclude & (1 << layer)) {
581                                         /* Disable excluded layer. */
582                                         have_override = true;
583                                         lc->flag |= LAYER_COLLECTION_EXCLUDE;
584                                         for (LayerCollection *nlc = lc->layer_collections.first; nlc; nlc = nlc->next) {
585                                                 nlc->flag |= LAYER_COLLECTION_EXCLUDE;
586                                         }
587                                 }
588                                 else if ((scene->lay & srl->lay & ~(srl->lay_exclude) & (1 << layer)) ||
589                                          (srl->lay_zmask & (scene->lay | srl->lay_exclude) & (1 << layer)))
590                                 {
591                                         if (srl->lay_zmask & (1 << layer)) {
592                                                 have_override = true;
593
594                                                 BKE_override_layer_collection_boolean_add(
595                                                         lc,
596                                                         ID_OB,
597                                                         "cycles.is_holdout",
598                                                         true);
599                                         }
600
601                                         if ((srl->lay & (1 << layer)) == 0) {
602                                                 have_override = true;
603
604                                                 BKE_override_layer_collection_boolean_add(
605                                                         lc,
606                                                         ID_OB,
607                                                         "cycles_visibility.camera",
608                                                         false);
609                                         }
610                                 }
611
612                                 LayerCollection *nlc = lc->layer_collections.first;
613                                 for (int j = 1; j < 4; j++) {
614                                         if (collections[j].created & (1 << layer)) {
615                                                 nlc = nlc->next;
616                                         }
617                                 }
618                                 BLI_assert(nlc == NULL);
619                         }
620                 }
621
622                 /* for convenience set the same active object in all the layers */
623                 if (scene->basact) {
624                         view_layer->basact = BKE_view_layer_base_find(view_layer, scene->basact->object);
625                 }
626
627                 for (Base *base = view_layer->object_bases.first; base; base = base->next) {
628                         if ((base->flag & BASE_SELECTABLED) && (base->object->flag & SELECT)) {
629                                 base->flag |= BASE_SELECTED;
630                         }
631                 }
632         }
633
634         BLI_freelistN(&scene->r.layers);
635
636         /* If render layers included overrides, we also create a vanilla
637          * viewport layer without them. */
638         if (have_override) {
639                 ViewLayer *view_layer = BKE_view_layer_add(scene, "Viewport");
640
641                 /* Make it first in the list. */
642                 BLI_remlink(&scene->view_layers, view_layer);
643                 BLI_addhead(&scene->view_layers, view_layer);
644
645                 /* If we ported all the original render layers, we don't need to make the viewport layer renderable. */
646                 if (!BLI_listbase_is_single(&scene->view_layers)) {
647                         view_layer->flag &= ~VIEW_LAYER_RENDER;
648                 }
649
650                 /* convert active base */
651                 if (scene->basact) {
652                         view_layer->basact = BKE_view_layer_base_find(view_layer, scene->basact->object);
653                 }
654
655                 /* convert selected bases */
656                 for (Base *base = view_layer->object_bases.first; base; base = base->next) {
657                         if ((base->flag & BASE_SELECTABLED) && (base->object->flag & SELECT)) {
658                                 base->flag |= BASE_SELECTED;
659                         }
660
661                         /* keep lay around for forward compatibility (open those files in 2.79) */
662                         base->lay = base->object->lay;
663                 }
664         }
665
666         /* remove bases once and for all */
667         for (Base *base = scene->base.first; base; base = base->next) {
668                 id_us_min(&base->object->id);
669         }
670
671         BLI_freelistN(&scene->base);
672         scene->basact = NULL;
673 }
674
675 void do_versions_after_linking_280(Main *main)
676 {
677         bool use_collection_compat_28 = true;
678
679         if (!MAIN_VERSION_ATLEAST(main, 280, 0)) {
680                 use_collection_compat_28 = false;
681
682                 /* Convert group layer visibility flags to hidden nested collection. */
683                 for (Collection *collection = main->collection.first; collection; collection = collection->id.next) {
684                         /* Add fake user for all existing groups. */
685                         id_fake_user_set(&collection->id);
686
687                         if (collection->flag & (COLLECTION_RESTRICT_VIEW | COLLECTION_RESTRICT_RENDER)) {
688                                 continue;
689                         }
690
691                         Collection *collection_hidden = NULL;
692                         for (CollectionObject *cob = collection->gobject.first, *cob_next = NULL; cob; cob = cob_next) {
693                                 cob_next = cob->next;
694                                 Object *ob = cob->ob;
695
696                                 if (!(ob->lay & collection->layer)) {
697                                         if (collection_hidden == NULL) {
698                                                 collection_hidden = BKE_collection_add(main, collection, "Hidden");
699                                                 collection_hidden->id.lib = collection->id.lib;
700                                                 collection_hidden->flag |= COLLECTION_RESTRICT_VIEW | COLLECTION_RESTRICT_RENDER;
701                                         }
702
703                                         BKE_collection_object_add(main, collection_hidden, ob);
704                                         BKE_collection_object_remove(main, collection, ob, true);
705                                 }
706                         }
707                 }
708
709                 /* Convert layers to collections. */
710                 for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
711                         do_version_layers_to_collections(main, scene);
712                 }
713         }
714
715         if (!MAIN_VERSION_ATLEAST(main, 280, 0)) {
716                 for (bScreen *screen = main->screen.first; screen; screen = screen->id.next) {
717                         /* same render-layer as do_version_workspaces_after_lib_link will activate,
718                          * so same layer as BKE_view_layer_from_workspace_get would return */
719                         ViewLayer *layer = screen->scene->view_layers.first;
720
721                         for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
722                                 for (SpaceLink *space = sa->spacedata.first; space; space = space->next) {
723                                         if (space->spacetype == SPACE_OUTLINER) {
724                                                 SpaceOops *soutliner = (SpaceOops *)space;
725
726                                                 soutliner->outlinevis = SO_VIEW_LAYER;
727
728                                                 if (BLI_listbase_count_at_most(&layer->layer_collections, 2) == 1) {
729                                                         if (soutliner->treestore == NULL) {
730                                                                 soutliner->treestore = BLI_mempool_create(
731                                                                         sizeof(TreeStoreElem), 1, 512, BLI_MEMPOOL_ALLOW_ITER);
732                                                         }
733
734                                                         /* Create a tree store element for the collection. This is normally
735                                                          * done in check_persistent (outliner_tree.c), but we need to access
736                                                          * it here :/ (expand element if it's the only one) */
737                                                         TreeStoreElem *tselem = BLI_mempool_calloc(soutliner->treestore);
738                                                         tselem->type = TSE_LAYER_COLLECTION;
739                                                         tselem->id = layer->layer_collections.first;
740                                                         tselem->nr = tselem->used = 0;
741                                                         tselem->flag &= ~TSE_CLOSED;
742                                                 }
743                                         }
744                                 }
745                         }
746                 }
747         }
748
749         /* New workspace design */
750         if (!MAIN_VERSION_ATLEAST(main, 280, 1)) {
751                 do_version_workspaces_after_lib_link(main);
752         }
753
754         if (!MAIN_VERSION_ATLEAST(main, 280, 2)) {
755                 /* Cleanup any remaining SceneRenderLayer data for files that were created
756                  * with Blender 2.8 before the SceneRenderLayer > RenderLayer refactor. */
757                 for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
758                         for (SceneRenderLayer *srl = scene->r.layers.first; srl; srl = srl->next) {
759                                 if (srl->prop) {
760                                         IDP_FreeProperty(srl->prop);
761                                         MEM_freeN(srl->prop);
762                                 }
763                                 BKE_freestyle_config_free(&srl->freestyleConfig, true);
764                         }
765                         BLI_freelistN(&scene->r.layers);
766                 }
767         }
768
769         if (!MAIN_VERSION_ATLEAST(main, 280, 3)) {
770                 /* Due to several changes to particle RNA and draw code particles from older files may no longer
771                  * be visible. Here we correct this by setting a default draw size for those files. */
772                 for (Object *object = main->object.first; object; object = object->id.next) {
773                         for (ParticleSystem *psys = object->particlesystem.first; psys; psys = psys->next) {
774                                 if (psys->part->draw_size == 0.0f) {
775                                         psys->part->draw_size = 0.1f;
776                                 }
777                         }
778                 }
779         }
780
781         if (!MAIN_VERSION_ATLEAST(main, 280, 4)) {
782                 for (Object *object = main->object.first; object; object = object->id.next) {
783 #ifndef VERSION_280_SUBVERSION_4
784                         /* If any object already has an initialized value for
785                          * duplicator_visibility_flag it means we've already doversioned it.
786                          * TODO(all) remove the VERSION_280_SUBVERSION_4 code once the subversion was bumped. */
787                         if (object->duplicator_visibility_flag != 0) {
788                                 break;
789                         }
790 #endif
791                         if (object->particlesystem.first) {
792                                 object->duplicator_visibility_flag = OB_DUPLI_FLAG_VIEWPORT;
793                                 for (ParticleSystem *psys = object->particlesystem.first; psys; psys = psys->next) {
794                                         if (psys->part->draw & PART_DRAW_EMITTER) {
795                                                 object->duplicator_visibility_flag |= OB_DUPLI_FLAG_RENDER;
796 #ifndef VERSION_280_SUBVERSION_4
797                                                 psys->part->draw &= ~PART_DRAW_EMITTER;
798 #else
799                                                 break;
800 #endif
801                                         }
802                                 }
803                         }
804                         else if (object->transflag & OB_DUPLI) {
805                                 object->duplicator_visibility_flag = OB_DUPLI_FLAG_VIEWPORT;
806                         }
807                         else {
808                                 object->duplicator_visibility_flag = OB_DUPLI_FLAG_VIEWPORT | OB_DUPLI_FLAG_RENDER;
809                         }
810                 }
811         }
812
813         /* SpaceTime & SpaceLogic removal/replacing */
814         if (!MAIN_VERSION_ATLEAST(main, 280, 9)) {
815                 const wmWindowManager *wm = main->wm.first;
816                 const Scene *scene = main->scene.first;
817
818                 if (wm != NULL) {
819                         /* Action editors need a scene for creation. First, update active
820                          * screens using the active scene of the window they're displayed in.
821                          * Next, update remaining screens using first scene in main listbase. */
822
823                         for (wmWindow *win = wm->windows.first; win; win = win->next) {
824                                 const bScreen *screen = BKE_workspace_active_screen_get(win->workspace_hook);
825                                 for (ScrArea *area = screen->areabase.first; area; area = area->next) {
826                                         if (ELEM(area->butspacetype, SPACE_TIME, SPACE_LOGIC)) {
827                                                 do_version_area_change_space_to_space_action(area, win->scene);
828
829                                                 /* Don't forget to unset! */
830                                                 area->butspacetype = SPACE_EMPTY;
831                                         }
832                                 }
833                         }
834                 }
835                 if (scene != NULL) {
836                         for (bScreen *screen = main->screen.first; screen; screen = screen->id.next) {
837                                 for (ScrArea *area = screen->areabase.first; area; area = area->next) {
838                                         if (ELEM(area->butspacetype, SPACE_TIME, SPACE_LOGIC)) {
839                                                 /* Areas that were already handled won't be handled again */
840                                                 do_version_area_change_space_to_space_action(area, scene);
841
842                                                 /* Don't forget to unset! */
843                                                 area->butspacetype = SPACE_EMPTY;
844                                         }
845                                 }
846                         }
847                 }
848         }
849
850 #ifdef USE_COLLECTION_COMPAT_28
851         if (use_collection_compat_28 && !MAIN_VERSION_ATLEAST(main, 280, 14)) {
852                 for (Collection *group = main->collection.first; group; group = group->id.next) {
853                         do_version_group_collection_to_collection(main, group);
854                 }
855
856                 for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
857                         do_version_scene_collection_to_collection(main, scene);
858                 }
859         }
860 #endif
861 }
862
863 void blo_do_versions_280(FileData *fd, Library *UNUSED(lib), Main *main)
864 {
865         bool use_collection_compat_28 = true;
866
867         if (!MAIN_VERSION_ATLEAST(main, 280, 0)) {
868                 use_collection_compat_28 = false;
869
870                 for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
871                         scene->r.gauss = 1.5f;
872                 }
873         }
874
875         if (!MAIN_VERSION_ATLEAST(main, 280, 1)) {
876                 if (!DNA_struct_elem_find(fd->filesdna, "Lamp", "float", "bleedexp")) {
877                         for (Lamp *la = main->lamp.first; la; la = la->id.next) {
878                                 la->bleedexp = 2.5f;
879                         }
880                 }
881
882                 if (!DNA_struct_elem_find(fd->filesdna, "GPUDOFSettings", "float", "ratio")) {
883                         for (Camera *ca = main->camera.first; ca; ca = ca->id.next) {
884                                 ca->gpu_dof.ratio = 1.0f;
885                         }
886                 }
887
888                 /* MTexPoly now removed. */
889                 if (DNA_struct_find(fd->filesdna, "MTexPoly")) {
890                         const int cd_mtexpoly = 15;  /* CD_MTEXPOLY, deprecated */
891                         for (Mesh *me = main->mesh.first; me; me = me->id.next) {
892                                 /* If we have UV's, so this file will have MTexPoly layers too! */
893                                 if (me->mloopuv != NULL) {
894                                         CustomData_update_typemap(&me->pdata);
895                                         CustomData_free_layers(&me->pdata, cd_mtexpoly, me->totpoly);
896                                         BKE_mesh_update_customdata_pointers(me, false);
897                                 }
898                         }
899                 }
900         }
901
902         if (!MAIN_VERSION_ATLEAST(main, 280, 2)) {
903                 if (!DNA_struct_elem_find(fd->filesdna, "Lamp", "float", "cascade_max_dist")) {
904                         for (Lamp *la = main->lamp.first; la; la = la->id.next) {
905                                 la->cascade_max_dist = 1000.0f;
906                                 la->cascade_count = 4;
907                                 la->cascade_exponent = 0.8f;
908                                 la->cascade_fade = 0.1f;
909                         }
910                 }
911
912                 if (!DNA_struct_elem_find(fd->filesdna, "Lamp", "float", "contact_dist")) {
913                         for (Lamp *la = main->lamp.first; la; la = la->id.next) {
914                                 la->contact_dist = 1.0f;
915                                 la->contact_bias = 0.03f;
916                                 la->contact_spread = 0.2f;
917                                 la->contact_thickness = 0.5f;
918                         }
919                 }
920
921                 if (!DNA_struct_elem_find(fd->filesdna, "LightProbe", "float", "vis_bias")) {
922                         for (LightProbe *probe = main->lightprobe.first; probe; probe = probe->id.next) {
923                                 probe->vis_bias = 1.0f;
924                                 probe->vis_blur = 0.2f;
925                         }
926                 }
927
928                 typedef enum eNTreeDoVersionErrors {
929                         NTREE_DOVERSION_NO_ERROR = 0,
930                         NTREE_DOVERSION_NEED_OUTPUT = (1 << 0),
931                         NTREE_DOVERSION_TRANSPARENCY_EMISSION = (1 << 1),
932                 } eNTreeDoVersionErrors;
933
934                 /* Eevee shader nodes renamed because of the output node system.
935                  * Note that a new output node is not being added here, because it would be overkill
936                  * to handle this case in lib_verify_nodetree.
937                  *
938                  * Also, metallic node is now unified into the principled node. */
939                 eNTreeDoVersionErrors error = NTREE_DOVERSION_NO_ERROR;
940
941                 FOREACH_NODETREE(main, ntree, id) {
942                         if (ntree->type == NTREE_SHADER) {
943                                 for (bNode *node = ntree->nodes.first; node; node = node->next) {
944                                         if (node->type == 194 /* SH_NODE_EEVEE_METALLIC */ &&
945                                             STREQ(node->idname, "ShaderNodeOutputMetallic"))
946                                         {
947                                                 BLI_strncpy(node->idname, "ShaderNodeEeveeMetallic", sizeof(node->idname));
948                                                 error |= NTREE_DOVERSION_NEED_OUTPUT;
949                                         }
950
951                                         else if (node->type == SH_NODE_EEVEE_SPECULAR && STREQ(node->idname, "ShaderNodeOutputSpecular")) {
952                                                 BLI_strncpy(node->idname, "ShaderNodeEeveeSpecular", sizeof(node->idname));
953                                                 error |= NTREE_DOVERSION_NEED_OUTPUT;
954                                         }
955
956                                         else if (node->type == 196 /* SH_NODE_OUTPUT_EEVEE_MATERIAL */ &&
957                                                  STREQ(node->idname, "ShaderNodeOutputEeveeMaterial"))
958                                         {
959                                                 node->type = SH_NODE_OUTPUT_MATERIAL;
960                                                 BLI_strncpy(node->idname, "ShaderNodeOutputMaterial", sizeof(node->idname));
961                                         }
962
963                                         else if (node->type == 194 /* SH_NODE_EEVEE_METALLIC */ &&
964                                                  STREQ(node->idname, "ShaderNodeEeveeMetallic"))
965                                         {
966                                                 node->type = SH_NODE_BSDF_PRINCIPLED;
967                                                 BLI_strncpy(node->idname, "ShaderNodeBsdfPrincipled", sizeof(node->idname));
968                                                 node->custom1 = SHD_GLOSSY_MULTI_GGX;
969                                                 error |= NTREE_DOVERSION_TRANSPARENCY_EMISSION;
970                                         }
971                                 }
972                         }
973                 } FOREACH_NODETREE_END
974
975                 if (error & NTREE_DOVERSION_NEED_OUTPUT) {
976                         BKE_report(fd->reports, RPT_ERROR, "Eevee material conversion problem. Error in console");
977                         printf("You need to connect Principled and Eevee Specular shader nodes to new material output nodes.\n");
978                 }
979
980                 if (error & NTREE_DOVERSION_TRANSPARENCY_EMISSION) {
981                         BKE_report(fd->reports, RPT_ERROR, "Eevee material conversion problem. Error in console");
982                         printf("You need to combine transparency and emission shaders to the converted Principled shader nodes.\n");
983                 }
984
985 #ifdef USE_COLLECTION_COMPAT_28
986                 if (use_collection_compat_28 &&
987                     (DNA_struct_elem_find(fd->filesdna, "ViewLayer", "FreestyleConfig", "freestyle_config") == false) &&
988                     DNA_struct_elem_find(fd->filesdna, "Scene", "ListBase", "view_layers"))
989                 {
990                         for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
991                                 ViewLayer *view_layer;
992                                 for (view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
993                                         view_layer->flag |= VIEW_LAYER_FREESTYLE;
994                                         view_layer->layflag = 0x7FFF;   /* solid ztra halo edge strand */
995                                         view_layer->passflag = SCE_PASS_COMBINED | SCE_PASS_Z;
996                                         view_layer->pass_alpha_threshold = 0.5f;
997                                         BKE_freestyle_config_init(&view_layer->freestyle_config);
998                                 }
999                         }
1000                 }
1001 #endif
1002         }
1003
1004 #ifdef USE_COLLECTION_COMPAT_28
1005         if (use_collection_compat_28 && !MAIN_VERSION_ATLEAST(main, 280, 3)) {
1006                 for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
1007                         ViewLayer *view_layer;
1008                         for (view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
1009                                 do_version_view_layer_visibility(view_layer);
1010                         }
1011                 }
1012
1013                 for (Collection *group = main->collection.first; group; group = group->id.next) {
1014                         if (group->view_layer != NULL) {
1015                                 do_version_view_layer_visibility(group->view_layer);
1016                         }
1017                 }
1018         }
1019 #endif
1020
1021         if (!MAIN_VERSION_ATLEAST(main, 280, 6)) {
1022                 if (DNA_struct_elem_find(fd->filesdna, "SpaceOops", "int", "filter") == false) {
1023                         bScreen *sc;
1024                         ScrArea *sa;
1025                         SpaceLink *sl;
1026
1027                         /* Update files using invalid (outdated) outlinevis Outliner values. */
1028                         for (sc = main->screen.first; sc; sc = sc->id.next) {
1029                                 for (sa = sc->areabase.first; sa; sa = sa->next) {
1030                                         for (sl = sa->spacedata.first; sl; sl = sl->next) {
1031                                                 if (sl->spacetype == SPACE_OUTLINER) {
1032                                                         SpaceOops *so = (SpaceOops *)sl;
1033
1034                                                         if (!ELEM(so->outlinevis,
1035                                                                   SO_SCENES,
1036                                                                   SO_LIBRARIES,
1037                                                                   SO_SEQUENCE,
1038                                                                   SO_DATA_API,
1039                                                                   SO_ID_ORPHANS))
1040                                                         {
1041                                                                 so->outlinevis = SO_VIEW_LAYER;
1042                                                         }
1043                                                 }
1044                                         }
1045                                 }
1046                         }
1047                 }
1048
1049                 if (!DNA_struct_elem_find(fd->filesdna, "LightProbe", "float", "intensity")) {
1050                         for (LightProbe *probe = main->lightprobe.first; probe; probe = probe->id.next) {
1051                                 probe->intensity = 1.0f;
1052                         }
1053                 }
1054
1055                 for (Object *ob = main->object.first; ob; ob = ob->id.next) {
1056                         bConstraint *con, *con_next;
1057                         con = ob->constraints.first;
1058                         while (con) {
1059                                 con_next = con->next;
1060                                 if (con->type == 17) { /* CONSTRAINT_TYPE_RIGIDBODYJOINT */
1061                                         BLI_remlink(&ob->constraints, con);
1062                                         BKE_constraint_free_data(con);
1063                                         MEM_freeN(con);
1064                                 }
1065                                 con = con_next;
1066                         }
1067                 }
1068
1069                 if (!DNA_struct_elem_find(fd->filesdna, "Scene", "int", "orientation_index_custom")) {
1070                         for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
1071                                 scene->orientation_index_custom = -1;
1072                         }
1073                 }
1074
1075                 for (bScreen *sc = main->screen.first; sc; sc = sc->id.next) {
1076                         for (ScrArea *sa = sc->areabase.first; sa; sa = sa->next) {
1077                                 for (SpaceLink *sl = sa->spacedata.first; sl; sl = sl->next) {
1078                                         if (sl->spacetype == SPACE_VIEW3D) {
1079                                                 View3D *v3d = (View3D *)sl;
1080                                                 v3d->shading.light = V3D_LIGHTING_STUDIO;
1081                                                 v3d->shading.flag |= V3D_SHADING_OBJECT_OUTLINE;
1082
1083                                                 /* Assume (demo) files written with 2.8 want to show
1084                                                  * Eevee renders in the viewport. */
1085                                                 if (MAIN_VERSION_ATLEAST(main, 280, 0)) {
1086                                                         v3d->drawtype = OB_MATERIAL;
1087                                                 }
1088                                         }
1089                                 }
1090                         }
1091                 }
1092         }
1093
1094         if (!MAIN_VERSION_ATLEAST(main, 280, 7)) {
1095                 /* Render engine storage moved elsewhere and back during 2.8
1096                  * development, we assume any files saved in 2.8 had Eevee set
1097                  * as scene render engine. */
1098                 if (MAIN_VERSION_ATLEAST(main, 280, 0)) {
1099                         for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
1100                                 BLI_strncpy(scene->r.engine, RE_engine_id_BLENDER_EEVEE, sizeof(scene->r.engine));
1101                         }
1102                 }
1103         }
1104
1105         if (!MAIN_VERSION_ATLEAST(main, 280, 8)) {
1106                 /* Blender Internal removal */
1107                 for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
1108                         if (STREQ(scene->r.engine, "BLENDER_RENDER") ||
1109                             STREQ(scene->r.engine, "BLENDER_GAME"))
1110                         {
1111                                 BLI_strncpy(scene->r.engine, RE_engine_id_BLENDER_EEVEE, sizeof(scene->r.engine));
1112                         }
1113
1114                         scene->r.bake_mode = 0;
1115                 }
1116
1117                 for (Tex *tex = main->tex.first; tex; tex = tex->id.next) {
1118                         /* Removed envmap, pointdensity, voxeldata, ocean textures. */
1119                         if (ELEM(tex->type, 10, 14, 15, 16)) {
1120                                 tex->type = 0;
1121                         }
1122                 }
1123         }
1124
1125         if (!MAIN_VERSION_ATLEAST(main, 280, 11)) {
1126
1127                 /* Remove info editor, but only if at the top of the window. */
1128                 for (bScreen *screen = main->screen.first; screen; screen = screen->id.next) {
1129                         /* Calculate window width/height from screen vertices */
1130                         int win_width = 0, win_height = 0;
1131                         for (ScrVert *vert = screen->vertbase.first; vert; vert = vert->next) {
1132                                 win_width  = MAX2(win_width, vert->vec.x);
1133                                 win_height = MAX2(win_height, vert->vec.y);
1134                         }
1135
1136                         for (ScrArea *area = screen->areabase.first, *area_next; area; area = area_next) {
1137                                 area_next = area->next;
1138
1139                                 if (area->spacetype == SPACE_INFO) {
1140                                         if ((area->v2->vec.y == win_height) && (area->v1->vec.x == 0) && (area->v4->vec.x == win_width)) {
1141                                                 BKE_screen_area_free(area);
1142
1143                                                 BLI_remlink(&screen->areabase, area);
1144
1145                                                 BKE_screen_remove_double_scredges(screen);
1146                                                 BKE_screen_remove_unused_scredges(screen);
1147                                                 BKE_screen_remove_unused_scrverts(screen);
1148
1149                                                 MEM_freeN(area);
1150                                         }
1151                                 }
1152                                 /* AREA_TEMP_INFO is deprecated from now on, it should only be set for info areas
1153                                  * which are deleted above, so don't need to unset it. Its slot/bit can be reused */
1154                         }
1155                 }
1156         }
1157
1158         if (!MAIN_VERSION_ATLEAST(main, 280, 11)) {
1159                 for (Lamp *lamp = main->lamp.first; lamp; lamp = lamp->id.next) {
1160                         if (lamp->mode & (1 << 13)) { /* LA_SHAD_RAY */
1161                                 lamp->mode |= LA_SHADOW;
1162                                 lamp->mode &= ~(1 << 13);
1163                         }
1164                 }
1165         }
1166
1167         if (!MAIN_VERSION_ATLEAST(main, 280, 12)) {
1168                 /* Remove tool property regions. */
1169                 for (bScreen *screen = main->screen.first; screen; screen = screen->id.next) {
1170                         for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
1171                                 for (SpaceLink *sl = sa->spacedata.first; sl; sl = sl->next) {
1172                                         if (ELEM(sl->spacetype, SPACE_VIEW3D, SPACE_CLIP)) {
1173                                                 ListBase *regionbase = (sl == sa->spacedata.first) ? &sa->regionbase : &sl->regionbase;
1174
1175                                                 for (ARegion *region = regionbase->first, *region_next; region; region = region_next) {
1176                                                         region_next = region->next;
1177
1178                                                         if (region->regiontype == RGN_TYPE_TOOL_PROPS) {
1179                                                                 BKE_area_region_free(NULL, region);
1180                                                                 BLI_freelinkN(regionbase, region);
1181                                                         }
1182                                                 }
1183                                         }
1184                                 }
1185                         }
1186                 }
1187         }
1188
1189         if (!MAIN_VERSION_ATLEAST(main, 280, 13)) {
1190                 /* Initialize specular factor. */
1191                 if (!DNA_struct_elem_find(fd->filesdna, "Lamp", "float", "spec_fac")) {
1192                         for (Lamp *la = main->lamp.first; la; la = la->id.next) {
1193                                 la->spec_fac = 1.0f;
1194                         }
1195                 }
1196
1197                 /* Initialize new view3D options. */
1198                 for (bScreen *screen = main->screen.first; screen; screen = screen->id.next) {
1199                         for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
1200                                 for (SpaceLink *sl = sa->spacedata.first; sl; sl = sl->next) {
1201                                         if (sl->spacetype == SPACE_VIEW3D) {
1202                                                 View3D *v3d = (View3D *)sl;
1203                                                 v3d->shading.light = V3D_LIGHTING_STUDIO;
1204                                                 v3d->shading.color_type = V3D_SHADING_MATERIAL_COLOR;
1205                                                 copy_v3_fl(v3d->shading.single_color, 0.8f);
1206                                                 v3d->shading.shadow_intensity = 0.5;
1207
1208                                                 v3d->overlay.backwire_opacity = 0.5f;
1209                                                 v3d->overlay.normals_length = 0.1f;
1210                                                 v3d->overlay.flag = V3D_OVERLAY_LOOK_DEV;
1211                                         }
1212                                 }
1213                         }
1214                 }
1215
1216                 if (!DNA_struct_find(fd->filesdna, "View3DCursor")) {
1217                         for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
1218                                 unit_qt(scene->cursor.rotation);
1219                         }
1220                         for (bScreen *screen = main->screen.first; screen; screen = screen->id.next) {
1221                                 for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
1222                                         for (SpaceLink *sl = sa->spacedata.first; sl; sl = sl->next) {
1223                                                 if (sl->spacetype == SPACE_VIEW3D) {
1224                                                         View3D *v3d = (View3D *)sl;
1225                                                         unit_qt(v3d->cursor.rotation);
1226                                                 }
1227                                         }
1228                                 }
1229                         }
1230                 }
1231         }
1232
1233         if (!MAIN_VERSION_ATLEAST(main, 280, 14)) {
1234                 if (!DNA_struct_elem_find(fd->filesdna, "Scene", "SceneDisplay", "display")) {
1235                         /* Initialize new scene.SceneDisplay */
1236                         for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
1237                                 copy_v3_v3(scene->display.light_direction, (float[3]){-M_SQRT1_3, -M_SQRT1_3, M_SQRT1_3});
1238                         }
1239                 }
1240                 if (!DNA_struct_elem_find(fd->filesdna, "SceneDisplay", "float", "shadow_shift")) {
1241                         for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
1242                                 scene->display.shadow_shift = 0.1;
1243                         }
1244                 }
1245
1246                 if (!DNA_struct_elem_find(fd->filesdna, "Object", "ObjectDisplay", "display")) {
1247                         /* Initialize new object.ObjectDisplay */
1248                         for (Object *ob = main->object.first; ob; ob = ob->id.next) {
1249                                 ob->display.flag = OB_SHOW_SHADOW;
1250                         }
1251                 }
1252
1253                 if (!DNA_struct_elem_find(fd->filesdna, "ToolSettings", "char", "transform_pivot_point")) {
1254                         for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
1255                                 scene->toolsettings->transform_pivot_point = V3D_AROUND_CENTER_MEAN;
1256                         }
1257                 }
1258
1259                 if (!DNA_struct_find(fd->filesdna, "SceneEEVEE")) {
1260                         for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
1261                                 /* First set the default for all the properties. */
1262
1263                                 scene->eevee.gi_diffuse_bounces = 3;
1264                                 scene->eevee.gi_cubemap_resolution = 512;
1265                                 scene->eevee.gi_visibility_resolution = 32;
1266
1267                                 scene->eevee.taa_samples = 16;
1268                                 scene->eevee.taa_render_samples = 64;
1269
1270                                 scene->eevee.sss_samples = 7;
1271                                 scene->eevee.sss_jitter_threshold = 0.3f;
1272
1273                                 scene->eevee.ssr_quality = 0.25f;
1274                                 scene->eevee.ssr_max_roughness = 0.5f;
1275                                 scene->eevee.ssr_thickness = 0.2f;
1276                                 scene->eevee.ssr_border_fade = 0.075f;
1277                                 scene->eevee.ssr_firefly_fac = 10.0f;
1278
1279                                 scene->eevee.volumetric_start = 0.1f;
1280                                 scene->eevee.volumetric_end = 100.0f;
1281                                 scene->eevee.volumetric_tile_size = 8;
1282                                 scene->eevee.volumetric_samples = 64;
1283                                 scene->eevee.volumetric_sample_distribution = 0.8f;
1284                                 scene->eevee.volumetric_light_clamp = 0.0f;
1285                                 scene->eevee.volumetric_shadow_samples = 16;
1286
1287                                 scene->eevee.gtao_distance = 0.2f;
1288                                 scene->eevee.gtao_factor = 1.0f;
1289                                 scene->eevee.gtao_quality = 0.25f;
1290
1291                                 scene->eevee.bokeh_max_size = 100.0f;
1292                                 scene->eevee.bokeh_threshold = 1.0f;
1293
1294                                 copy_v3_fl(scene->eevee.bloom_color, 1.0f);
1295                                 scene->eevee.bloom_threshold = 0.8f;
1296                                 scene->eevee.bloom_knee = 0.5f;
1297                                 scene->eevee.bloom_intensity = 0.8f;
1298                                 scene->eevee.bloom_radius = 6.5f;
1299                                 scene->eevee.bloom_clamp = 1.0f;
1300
1301                                 scene->eevee.motion_blur_samples = 8;
1302                                 scene->eevee.motion_blur_shutter = 1.0f;
1303
1304                                 scene->eevee.shadow_method = SHADOW_ESM;
1305                                 scene->eevee.shadow_cube_size = 512;
1306                                 scene->eevee.shadow_cascade_size = 1024;
1307
1308                                 scene->eevee.flag =
1309                                         SCE_EEVEE_VOLUMETRIC_LIGHTS |
1310                                         SCE_EEVEE_VOLUMETRIC_COLORED |
1311                                         SCE_EEVEE_GTAO_BENT_NORMALS |
1312                                         SCE_EEVEE_GTAO_BOUNCE |
1313                                         SCE_EEVEE_TAA_REPROJECTION |
1314                                         SCE_EEVEE_SSR_HALF_RESOLUTION;
1315
1316
1317                                 /* If the file is pre-2.80 move on. */
1318                                 if (scene->layer_properties == NULL) {
1319                                         continue;
1320                                 }
1321
1322                                 /* Now we handle eventual properties that may be set in the file. */
1323 #define EEVEE_GET_BOOL(_props, _name, _flag) \
1324                                 { \
1325                                         IDProperty *_idprop = IDP_GetPropertyFromGroup(_props, #_name); \
1326                                         if (_idprop != NULL) { \
1327                                                 const int _value = IDP_Int(_idprop); \
1328                                                 if (_value) { \
1329                                                         scene->eevee.flag |= _flag; \
1330                                                 } \
1331                                                 else { \
1332                                                         scene->eevee.flag &= ~_flag; \
1333                                                 } \
1334                                         } \
1335                                 }
1336
1337 #define EEVEE_GET_INT(_props, _name) \
1338                                 { \
1339                                         IDProperty *_idprop = IDP_GetPropertyFromGroup(_props, #_name); \
1340                                         if (_idprop != NULL) { \
1341                                                 scene->eevee._name = IDP_Int(_idprop); \
1342                                         } \
1343                                 }
1344
1345 #define EEVEE_GET_FLOAT(_props, _name) \
1346                                 { \
1347                                         IDProperty *_idprop = IDP_GetPropertyFromGroup(_props, #_name); \
1348                                         if (_idprop != NULL) { \
1349                                                 scene->eevee._name = IDP_Float(_idprop); \
1350                                         } \
1351                                 }
1352
1353 #define EEVEE_GET_FLOAT_ARRAY(_props, _name, _length) \
1354                                 { \
1355                                         IDProperty *_idprop = IDP_GetPropertyFromGroup(_props, #_name); \
1356                                         if (_idprop != NULL) { \
1357                                                 const float *_values = IDP_Array(_idprop); \
1358                                                 for (int _i = 0; _i < _length; _i++) { \
1359                                                         scene->eevee._name [_i] = _values[_i]; \
1360                                                 } \
1361                                         } \
1362                                 }
1363
1364                                 IDProperty *props = IDP_GetPropertyFromGroup(scene->layer_properties, RE_engine_id_BLENDER_EEVEE);
1365                                 EEVEE_GET_BOOL(props, volumetric_enable, SCE_EEVEE_VOLUMETRIC_ENABLED);
1366                                 EEVEE_GET_BOOL(props, volumetric_lights, SCE_EEVEE_VOLUMETRIC_LIGHTS);
1367                                 EEVEE_GET_BOOL(props, volumetric_shadows, SCE_EEVEE_VOLUMETRIC_SHADOWS);
1368                                 EEVEE_GET_BOOL(props, volumetric_colored_transmittance, SCE_EEVEE_VOLUMETRIC_COLORED);
1369                                 EEVEE_GET_BOOL(props, gtao_enable, SCE_EEVEE_GTAO_ENABLED);
1370                                 EEVEE_GET_BOOL(props, gtao_use_bent_normals, SCE_EEVEE_GTAO_BENT_NORMALS);
1371                                 EEVEE_GET_BOOL(props, gtao_bounce, SCE_EEVEE_GTAO_BOUNCE);
1372                                 EEVEE_GET_BOOL(props, dof_enable, SCE_EEVEE_DOF_ENABLED);
1373                                 EEVEE_GET_BOOL(props, bloom_enable, SCE_EEVEE_BLOOM_ENABLED);
1374                                 EEVEE_GET_BOOL(props, motion_blur_enable, SCE_EEVEE_MOTION_BLUR_ENABLED);
1375                                 EEVEE_GET_BOOL(props, shadow_high_bitdepth, SCE_EEVEE_SHADOW_HIGH_BITDEPTH);
1376                                 EEVEE_GET_BOOL(props, taa_reprojection, SCE_EEVEE_TAA_REPROJECTION);
1377                                 EEVEE_GET_BOOL(props, sss_enable, SCE_EEVEE_SSS_ENABLED);
1378                                 EEVEE_GET_BOOL(props, sss_separate_albedo, SCE_EEVEE_SSS_SEPARATE_ALBEDO);
1379                                 EEVEE_GET_BOOL(props, ssr_enable, SCE_EEVEE_SSR_ENABLED);
1380                                 EEVEE_GET_BOOL(props, ssr_refraction, SCE_EEVEE_SSR_REFRACTION);
1381                                 EEVEE_GET_BOOL(props, ssr_halfres, SCE_EEVEE_SSR_HALF_RESOLUTION);
1382
1383                                 EEVEE_GET_INT(props, gi_diffuse_bounces);
1384                                 EEVEE_GET_INT(props, gi_diffuse_bounces);
1385                                 EEVEE_GET_INT(props, gi_cubemap_resolution);
1386                                 EEVEE_GET_INT(props, gi_visibility_resolution);
1387
1388                                 EEVEE_GET_INT(props, taa_samples);
1389                                 EEVEE_GET_INT(props, taa_render_samples);
1390
1391                                 EEVEE_GET_INT(props, sss_samples);
1392                                 EEVEE_GET_FLOAT(props, sss_jitter_threshold);
1393
1394                                 EEVEE_GET_FLOAT(props, ssr_quality);
1395                                 EEVEE_GET_FLOAT(props, ssr_max_roughness);
1396                                 EEVEE_GET_FLOAT(props, ssr_thickness);
1397                                 EEVEE_GET_FLOAT(props, ssr_border_fade);
1398                                 EEVEE_GET_FLOAT(props, ssr_firefly_fac);
1399
1400                                 EEVEE_GET_FLOAT(props, volumetric_start);
1401                                 EEVEE_GET_FLOAT(props, volumetric_end);
1402                                 EEVEE_GET_INT(props, volumetric_tile_size);
1403                                 EEVEE_GET_INT(props, volumetric_samples);
1404                                 EEVEE_GET_FLOAT(props, volumetric_sample_distribution);
1405                                 EEVEE_GET_FLOAT(props, volumetric_light_clamp);
1406                                 EEVEE_GET_INT(props, volumetric_shadow_samples);
1407
1408                                 EEVEE_GET_FLOAT(props, gtao_distance);
1409                                 EEVEE_GET_FLOAT(props, gtao_factor);
1410                                 EEVEE_GET_FLOAT(props, gtao_quality);
1411
1412                                 EEVEE_GET_FLOAT(props, bokeh_max_size);
1413                                 EEVEE_GET_FLOAT(props, bokeh_threshold);
1414
1415                                 EEVEE_GET_FLOAT_ARRAY(props, bloom_color, 3);
1416                                 EEVEE_GET_FLOAT(props, bloom_threshold);
1417                                 EEVEE_GET_FLOAT(props, bloom_knee);
1418                                 EEVEE_GET_FLOAT(props, bloom_intensity);
1419                                 EEVEE_GET_FLOAT(props, bloom_radius);
1420                                 EEVEE_GET_FLOAT(props, bloom_clamp);
1421
1422                                 EEVEE_GET_INT(props, motion_blur_samples);
1423                                 EEVEE_GET_FLOAT(props, motion_blur_shutter);
1424
1425                                 EEVEE_GET_INT(props, shadow_method);
1426                                 EEVEE_GET_INT(props, shadow_cube_size);
1427                                 EEVEE_GET_INT(props, shadow_cascade_size);
1428
1429                                 /* Cleanup. */
1430                                 IDP_FreeProperty(scene->layer_properties);
1431                                 MEM_freeN(scene->layer_properties);
1432                                 scene->layer_properties = NULL;
1433
1434 #undef EEVEE_GET_FLOAT_ARRAY
1435 #undef EEVEE_GET_FLOAT
1436 #undef EEVEE_GET_INT
1437 #undef EEVEE_GET_BOOL
1438                         }
1439                 }
1440
1441
1442                 if (!MAIN_VERSION_ATLEAST(main, 280, 15)) {
1443                         for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
1444                                 scene->display.matcap_icon = 1;
1445                                 scene->display.matcap_type = CLAY_MATCAP_NONE;
1446                                 scene->display.matcap_hue = 0.5f;
1447                                 scene->display.matcap_saturation = 0.5f;
1448                                 scene->display.matcap_value = 0.5f;
1449                                 scene->display.matcap_ssao_distance = 0.2f;
1450                                 scene->display.matcap_ssao_attenuation = 1.0f;
1451                                 scene->display.matcap_ssao_factor_cavity = 1.0f;
1452                                 scene->display.matcap_ssao_factor_edge = 1.0f;
1453                                 scene->display.matcap_ssao_samples = 16;
1454                         }
1455
1456                         for (bScreen *screen = main->screen.first; screen; screen = screen->id.next) {
1457                                 for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
1458                                         for (SpaceLink *sl = sa->spacedata.first; sl; sl = sl->next) {
1459                                                 if (sl->spacetype == SPACE_OUTLINER) {
1460                                                         SpaceOops *soops = (SpaceOops *)sl;
1461                                                         soops->filter_id_type = ID_GR;
1462                                                         soops->outlinevis = SO_VIEW_LAYER;
1463                                                 }
1464                                         }
1465                                 }
1466                         }
1467
1468                         for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
1469                                 switch (scene->toolsettings->snap_mode) {
1470                                         case 0: scene->toolsettings->snap_mode = SCE_SNAP_MODE_INCREMENT; break;
1471                                         case 1: scene->toolsettings->snap_mode = SCE_SNAP_MODE_VERTEX   ; break;
1472                                         case 2: scene->toolsettings->snap_mode = SCE_SNAP_MODE_EDGE     ; break;
1473                                         case 3: scene->toolsettings->snap_mode = SCE_SNAP_MODE_FACE     ; break;
1474                                         case 4: scene->toolsettings->snap_mode = SCE_SNAP_MODE_VOLUME   ; break;
1475                                 }
1476                                 switch (scene->toolsettings->snap_node_mode) {
1477                                         case 5: scene->toolsettings->snap_node_mode = SCE_SNAP_MODE_NODE_X; break;
1478                                         case 6: scene->toolsettings->snap_node_mode = SCE_SNAP_MODE_NODE_Y; break;
1479                                         case 7: scene->toolsettings->snap_node_mode = SCE_SNAP_MODE_NODE_X | SCE_SNAP_MODE_NODE_Y; break;
1480                                         case 8: scene->toolsettings->snap_node_mode = SCE_SNAP_MODE_GRID  ; break;
1481                                 }
1482                                 switch (scene->toolsettings->snap_uv_mode) {
1483                                         case 0: scene->toolsettings->snap_uv_mode = SCE_SNAP_MODE_INCREMENT; break;
1484                                         case 1: scene->toolsettings->snap_uv_mode = SCE_SNAP_MODE_VERTEX   ; break;
1485                                 }
1486                         }
1487
1488                         ParticleSettings *part;
1489                         for (part = main->particle.first; part; part = part->id.next) {
1490                                 part->shape_flag = PART_SHAPE_CLOSE_TIP;
1491                                 part->shape = 0.0f;
1492                                 part->rad_root = 1.0f;
1493                                 part->rad_tip = 0.0f;
1494                                 part->rad_scale = 0.01f;
1495                         }
1496                 }
1497
1498         }
1499         {
1500                 if (!DNA_struct_elem_find(fd->filesdna, "Material", "float", "roughness")) {
1501                         for (Material *mat = main->mat.first; mat; mat = mat->id.next) {
1502                                 if (mat->use_nodes) {
1503                                         if (MAIN_VERSION_ATLEAST(main, 280, 0)) {
1504                                                 mat->roughness = mat->gloss_mir;
1505                                         }
1506                                         else {
1507                                                 mat->roughness = 0.25f;
1508                                         }
1509                                 }
1510                                 else {
1511                                         mat->roughness = 1.0f - mat->gloss_mir;
1512                                 }
1513                                 mat->metallic = mat->ray_mirror;
1514                         }
1515
1516                         for (bScreen *screen = main->screen.first; screen; screen = screen->id.next) {
1517                                 for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
1518                                         for (SpaceLink *sl = sa->spacedata.first; sl; sl = sl->next) {
1519                                                 if (sl->spacetype == SPACE_VIEW3D) {
1520                                                         View3D *v3d = (View3D *)sl;
1521                                                         v3d->shading.flag |= V3D_SHADING_SPECULAR_HIGHLIGHT;
1522                                                 }
1523                                         }
1524                                 }
1525                         }
1526                 }
1527
1528                 if (!DNA_struct_elem_find(fd->filesdna, "View3DShading", "float", "xray_alpha")) {
1529                         for (bScreen *screen = main->screen.first; screen; screen = screen->id.next) {
1530                                 for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
1531                                         for (SpaceLink *sl = sa->spacedata.first; sl; sl = sl->next) {
1532                                                 if (sl->spacetype == SPACE_VIEW3D) {
1533                                                         View3D *v3d = (View3D *)sl;
1534                                                         v3d->shading.xray_alpha = 0.5f;
1535                                                 }
1536                                         }
1537                                 }
1538                         }
1539                 }
1540                 if (!DNA_struct_elem_find(fd->filesdna, "View3DShading", "char", "matcap[256]")) {
1541                         StudioLight *default_matcap = BKE_studiolight_find_first(STUDIOLIGHT_ORIENTATION_VIEWNORMAL);
1542                         /* when loading the internal file is loaded before the matcaps */
1543                         if (default_matcap) {
1544                                 for (bScreen *screen = main->screen.first; screen; screen = screen->id.next) {
1545                                         for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
1546                                                 for (SpaceLink *sl = sa->spacedata.first; sl; sl = sl->next) {
1547                                                         if (sl->spacetype == SPACE_VIEW3D) {
1548                                                                 View3D *v3d = (View3D *)sl;
1549                                                                 BLI_strncpy(v3d->shading.matcap, default_matcap->name, FILE_MAXFILE);
1550                                                         }
1551                                                 }
1552                                         }
1553                                 }
1554                         }
1555                 }
1556         }
1557 }