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