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