Eevee: Add Lamp Specular multiplier.
[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_mempool.h"
36 #include "BLI_string.h"
37 #include "BLI_string_utf8.h"
38 #include "BLI_utildefines.h"
39
40 #include "DNA_object_types.h"
41 #include "DNA_camera_types.h"
42 #include "DNA_constraint_types.h"
43 #include "DNA_gpu_types.h"
44 #include "DNA_group_types.h"
45 #include "DNA_lamp_types.h"
46 #include "DNA_layer_types.h"
47 #include "DNA_lightprobe_types.h"
48 #include "DNA_material_types.h"
49 #include "DNA_mesh_types.h"
50 #include "DNA_particle_types.h"
51 #include "DNA_scene_types.h"
52 #include "DNA_screen_types.h"
53 #include "DNA_view3d_types.h"
54 #include "DNA_genfile.h"
55 #include "DNA_workspace_types.h"
56
57 #include "BKE_collection.h"
58 #include "BKE_constraint.h"
59 #include "BKE_customdata.h"
60 #include "BKE_freestyle.h"
61 #include "BKE_group.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
78 static bScreen *screen_parent_find(const bScreen *screen)
79 {
80         /* can avoid lookup if screen state isn't maximized/full (parent and child store the same state) */
81         if (ELEM(screen->state, SCREENMAXIMIZED, SCREENFULL)) {
82                 for (const ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
83                         if (sa->full && sa->full != screen) {
84                                 BLI_assert(sa->full->state == screen->state);
85                                 return sa->full;
86                         }
87                 }
88         }
89
90         return NULL;
91 }
92
93 static void do_version_workspaces_create_from_screens(Main *bmain)
94 {
95         for (bScreen *screen = bmain->screen.first; screen; screen = screen->id.next) {
96                 const bScreen *screen_parent = screen_parent_find(screen);
97                 Scene *scene = screen->scene;
98                 WorkSpace *workspace;
99                 ViewLayer *layer = BLI_findlink(&scene->view_layers, scene->r.actlay);
100                 if (!layer) {
101                         layer = BKE_view_layer_default_view(scene);
102                 }
103
104                 if (screen_parent) {
105                         /* fullscreen with "Back to Previous" option, don't create
106                          * a new workspace, add layout workspace containing parent */
107                         workspace = BLI_findstring(
108                                 &bmain->workspaces, screen_parent->id.name + 2, offsetof(ID, name) + 2);
109                 }
110                 else {
111                         workspace = BKE_workspace_add(bmain, screen->id.name + 2);
112                 }
113                 BKE_workspace_layout_add(workspace, screen, screen->id.name + 2);
114                 BKE_workspace_view_layer_set(workspace, layer, scene);
115         }
116 }
117
118 static void do_version_area_change_space_to_space_action(ScrArea *area, const Scene *scene)
119 {
120         SpaceType *stype = BKE_spacetype_from_id(SPACE_ACTION);
121         SpaceAction *saction = (SpaceAction *)stype->new(area, scene);
122         ARegion *region_channels;
123
124         /* Properly free current regions */
125         for (ARegion *region = area->regionbase.first; region; region = region->next) {
126                 BKE_area_region_free(area->type, region);
127         }
128         BLI_freelistN(&area->regionbase);
129
130         area->type = stype;
131         area->spacetype = stype->spaceid;
132
133         BLI_addhead(&area->spacedata, saction);
134         area->regionbase = saction->regionbase;
135         BLI_listbase_clear(&saction->regionbase);
136
137         /* Different defaults for timeline */
138         region_channels = BKE_area_find_region_type(area, RGN_TYPE_CHANNELS);
139         region_channels->flag |= RGN_FLAG_HIDDEN;
140
141         saction->mode = SACTCONT_TIMELINE;
142         saction->ads.flag |= ADS_FLAG_SUMMARY_COLLAPSED;
143 }
144
145 /**
146  * \brief After lib-link versioning for new workspace design.
147  *
148  *  *  Adds a workspace for (almost) each screen of the old file
149  *     and adds the needed workspace-layout to wrap the screen.
150  *  *  Active screen isn't stored directly in window anymore, but in the active workspace.
151  *  *  Active scene isn't stored in screen anymore, but in window.
152  *  *  Create workspace instance hook for each window.
153  *
154  * \note Some of the created workspaces might be deleted again in case of reading the default startup.blend.
155  */
156 static void do_version_workspaces_after_lib_link(Main *bmain)
157 {
158         BLI_assert(BLI_listbase_is_empty(&bmain->workspaces));
159
160         do_version_workspaces_create_from_screens(bmain);
161
162         for (wmWindowManager *wm = bmain->wm.first; wm; wm = wm->id.next) {
163                 for (wmWindow *win = wm->windows.first; win; win = win->next) {
164                         bScreen *screen_parent = screen_parent_find(win->screen);
165                         bScreen *screen = screen_parent ? screen_parent : win->screen;
166                         WorkSpace *workspace = BLI_findstring(&bmain->workspaces, screen->id.name + 2, offsetof(ID, name) + 2);
167                         ListBase *layouts = BKE_workspace_layouts_get(workspace);
168
169                         win->workspace_hook = BKE_workspace_instance_hook_create(bmain);
170
171                         BKE_workspace_active_set(win->workspace_hook, workspace);
172                         BKE_workspace_active_layout_set(win->workspace_hook, layouts->first);
173
174                         win->scene = screen->scene;
175                         /* Deprecated from now on! */
176                         win->screen = NULL;
177                 }
178         }
179
180         for (bScreen *screen = bmain->screen.first; screen; screen = screen->id.next) {
181                 /* Deprecated from now on! */
182                 BLI_freelistN(&screen->scene->transform_spaces);
183                 screen->scene = NULL;
184         }
185 }
186
187 enum {
188         DO_VERSION_COLLECTION_VISIBLE     = 0,
189         DO_VERSION_COLLECTION_HIDE        = 1,
190         DO_VERSION_COLLECTION_HIDE_RENDER = 2,
191         DO_VERSION_COLLECTION_HIDE_ALL    = 3,
192 };
193
194 void do_versions_after_linking_280(Main *main)
195 {
196         if (!MAIN_VERSION_ATLEAST(main, 280, 0)) {
197                 for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
198                         /* since we don't have access to FileData we check the (always valid) first render layer instead */
199                         if (scene->view_layers.first == NULL) {
200                                 SceneCollection *sc_master = BKE_collection_master(&scene->id);
201                                 BLI_strncpy(sc_master->name, "Master Collection", sizeof(sc_master->name));
202
203                                 struct DoVersionSceneCollections {
204                                         SceneCollection *collections[20];
205                                         int created;
206                                         const char *suffix;
207                                         int flag_viewport;
208                                         int flag_render;
209                                 } collections[] =
210                                 {
211                                         {
212                                                 .collections = {NULL},
213                                                 .created = 0,
214                                                 .suffix = "",
215                                                 .flag_viewport = COLLECTION_SELECTABLE,
216                                                 .flag_render = COLLECTION_SELECTABLE
217                                         },
218                                         {
219                                                 .collections = {NULL},
220                                                 .created = 0,
221                                                 .suffix = " - Hide Viewport",
222                                                 .flag_viewport = COLLECTION_SELECTABLE,
223                                                 .flag_render = COLLECTION_SELECTABLE
224                                         },
225                                         {
226                                                 .collections = {NULL},
227                                                 .created = 0,
228                                                 .suffix = " - Hide Render",
229                                                 .flag_viewport = COLLECTION_SELECTABLE,
230                                                 .flag_render = COLLECTION_SELECTABLE | COLLECTION_DISABLED
231                                         },
232                                         {
233                                                 .collections = {NULL},
234                                                 .created = 0,
235                                                 .suffix = " - Hide Render All",
236                                                 .flag_viewport = COLLECTION_SELECTABLE | COLLECTION_DISABLED,
237                                                 .flag_render = COLLECTION_SELECTABLE | COLLECTION_DISABLED
238                                         }
239                                 };
240
241                                 for (int layer = 0; layer < 20; layer++) {
242                                         for (Base *base = scene->base.first; base; base = base->next) {
243                                                 if (base->lay & (1 << layer)) {
244                                                         int collection_index = -1;
245                                                         if ((base->object->restrictflag & OB_RESTRICT_VIEW) &&
246                                                             (base->object->restrictflag & OB_RESTRICT_RENDER))
247                                                         {
248                                                                 collection_index = DO_VERSION_COLLECTION_HIDE_ALL;
249                                                         }
250                                                         else if (base->object->restrictflag & OB_RESTRICT_VIEW) {
251                                                                 collection_index = DO_VERSION_COLLECTION_HIDE;
252                                                         }
253                                                         else if (base->object->restrictflag & OB_RESTRICT_RENDER) {
254                                                                 collection_index = DO_VERSION_COLLECTION_HIDE_RENDER;
255                                                         }
256                                                         else {
257                                                                 collection_index = DO_VERSION_COLLECTION_VISIBLE;
258                                                         }
259
260                                                         /* Create collections when needed only. */
261                                                         if ((collections[collection_index].created & (1 << layer)) == 0) {
262                                                                 char name[MAX_NAME];
263
264                                                                 if ((collections[DO_VERSION_COLLECTION_VISIBLE].created & (1 << layer)) == 0) {
265                                                                         BLI_snprintf(name,
266                                                                                      sizeof(sc_master->name),
267                                                                                      "Collection %d%s",
268                                                                                      layer + 1,
269                                                                                      collections[DO_VERSION_COLLECTION_VISIBLE].suffix);
270                                                                         collections[DO_VERSION_COLLECTION_VISIBLE].collections[layer] =
271                                                                                 BKE_collection_add(&scene->id, sc_master, COLLECTION_TYPE_NONE, name);
272                                                                         collections[DO_VERSION_COLLECTION_VISIBLE].created |= (1 << layer);
273                                                                 }
274
275                                                                 if (collection_index != DO_VERSION_COLLECTION_VISIBLE) {
276                                                                         SceneCollection *sc_parent;
277                                                                         sc_parent = collections[DO_VERSION_COLLECTION_VISIBLE].collections[layer];
278                                                                         BLI_snprintf(name,
279                                                                                      sizeof(sc_master->name),
280                                                                                      "Collection %d%s",
281                                                                                      layer + 1,
282                                                                                      collections[collection_index].suffix);
283                                                                         collections[collection_index].collections[layer] = BKE_collection_add(
284                                                                                 &scene->id,
285                                                                                 sc_parent,
286                                                                                 COLLECTION_TYPE_NONE,
287                                                                                 name);
288                                                                         collections[collection_index].created |= (1 << layer);
289                                                                 }
290                                                         }
291
292                                                         BKE_collection_object_add(
293                                                                 &scene->id, collections[collection_index].collections[layer], base->object);
294                                                 }
295
296                                                 if (base->flag & SELECT) {
297                                                         base->object->flag |= SELECT;
298                                                 }
299                                                 else {
300                                                         base->object->flag &= ~SELECT;
301                                                 }
302                                         }
303                                 }
304
305                                 /* Re-order the nested hidden collections. */
306                                 SceneCollection *scene_collection_parent = sc_master->scene_collections.first;
307
308                                 for (int layer = 0; layer < 20; layer++) {
309                                         if (collections[DO_VERSION_COLLECTION_VISIBLE].created & (1 << layer)) {
310
311                                                 if ((collections[DO_VERSION_COLLECTION_HIDE].created & (1 << layer)) &&
312                                                     (collections[DO_VERSION_COLLECTION_HIDE].collections[layer] !=
313                                                      scene_collection_parent->scene_collections.first))
314                                                 {
315                                                         BLI_listbase_swaplinks(
316                                                                 &scene_collection_parent->scene_collections,
317                                                                 collections[DO_VERSION_COLLECTION_HIDE].collections[layer],
318                                                                 scene_collection_parent->scene_collections.first);
319                                                 }
320
321                                                 if ((collections[DO_VERSION_COLLECTION_HIDE_ALL].created & (1 << layer)) &&
322                                                     (collections[DO_VERSION_COLLECTION_HIDE_ALL].collections[layer] !=
323                                                      scene_collection_parent->scene_collections.last))
324                                                 {
325                                                         BLI_listbase_swaplinks(
326                                                                 &scene_collection_parent->scene_collections,
327                                                                 collections[DO_VERSION_COLLECTION_HIDE_ALL].collections[layer],
328                                                                 scene_collection_parent->scene_collections.last);
329                                                 }
330
331                                                 scene_collection_parent = scene_collection_parent->next;
332                                         }
333                                 }
334                                 BLI_assert(scene_collection_parent == NULL);
335
336                                 /* Handle legacy render layers. */
337                                 {
338                                         for (SceneRenderLayer *srl = scene->r.layers.first; srl; srl = srl->next) {
339
340                                                 ViewLayer *view_layer = BKE_view_layer_add(scene, srl->name);
341
342                                                 if (srl->samples != 0) {
343                                                         /* It is up to the external engine to handle
344                                                          * its own doversion in this case. */
345                                                         BKE_override_view_layer_int_add(
346                                                                 view_layer,
347                                                                 ID_SCE,
348                                                                 "samples",
349                                                                 srl->samples);
350                                                 }
351
352                                                 if (srl->mat_override) {
353                                                         BKE_override_view_layer_datablock_add(
354                                                                 view_layer,
355                                                                 ID_MA,
356                                                                 "self",
357                                                                 (ID *)srl->mat_override);
358                                                 }
359
360                                                 if (srl->layflag & SCE_LAY_DISABLE) {
361                                                         view_layer->flag &= ~VIEW_LAYER_RENDER;
362                                                 }
363
364                                                 if ((srl->layflag & SCE_LAY_FRS) == 0) {
365                                                         view_layer->flag &= ~VIEW_LAYER_FREESTYLE;
366                                                 }
367
368                                                 /* XXX If we are to keep layflag it should be merged with flag (dfelinto). */
369                                                 view_layer->layflag = srl->layflag;
370                                                 /* XXX Not sure if we should keep the passes (dfelinto). */
371                                                 view_layer->passflag = srl->passflag;
372                                                 view_layer->pass_xor = srl->pass_xor;
373                                                 view_layer->pass_alpha_threshold = srl->pass_alpha_threshold;
374
375                                                 BKE_freestyle_config_free(&view_layer->freestyle_config, true);
376                                                 view_layer->freestyle_config = srl->freestyleConfig;
377                                                 view_layer->id_properties = srl->prop;
378
379                                                 /* unlink master collection  */
380                                                 BKE_collection_unlink(view_layer, view_layer->layer_collections.first);
381
382                                                 /* Add new collection bases. */
383                                                 for (int layer = 0; layer < 20; layer++) {
384                                                         if ((scene->lay & srl->lay & ~(srl->lay_exclude) & (1 << layer)) ||
385                                                             (srl->lay_zmask & (scene->lay | srl->lay_exclude) & (1 << layer)))
386                                                         {
387                                                                 if (collections[DO_VERSION_COLLECTION_VISIBLE].created & (1 << layer)) {
388
389                                                                         LayerCollection *layer_collection_parent;
390                                                                         layer_collection_parent = BKE_collection_link(
391                                                                                 view_layer,
392                                                                                 collections[DO_VERSION_COLLECTION_VISIBLE].collections[layer]);
393
394                                                                         if (srl->lay_zmask & (1 << layer)) {
395                                                                                 BKE_override_layer_collection_boolean_add(
396                                                                                         layer_collection_parent,
397                                                                                         ID_OB,
398                                                                                         "cycles.is_holdout",
399                                                                                         true);
400                                                                         }
401
402                                                                         if ((srl->lay & (1 << layer)) == 0) {
403                                                                                 BKE_override_layer_collection_boolean_add(
404                                                                                         layer_collection_parent,
405                                                                                         ID_OB,
406                                                                                         "cycles_visibility.camera",
407                                                                                         false);
408                                                                         }
409
410                                                                         LayerCollection *layer_collection_child;
411                                                                         layer_collection_child = layer_collection_parent->layer_collections.first;
412
413                                                                         for (int j = 1; j < 4; j++) {
414                                                                                 if (collections[j].created & (1 << layer)) {
415                                                                                         layer_collection_child->flag = COLLECTION_VIEWPORT |
416                                                                                                                        COLLECTION_RENDER |
417                                                                                                                        collections[j].flag_render;
418                                                                                         layer_collection_child = layer_collection_child->next;
419                                                                                 }
420                                                                         }
421                                                                         BLI_assert(layer_collection_child == NULL);
422                                                                 }
423                                                         }
424                                                 }
425
426                                                 /* for convenience set the same active object in all the layers */
427                                                 if (scene->basact) {
428                                                         view_layer->basact = BKE_view_layer_base_find(view_layer, scene->basact->object);
429                                                 }
430
431                                                 for (Base *base = view_layer->object_bases.first; base; base = base->next) {
432                                                         if ((base->flag & BASE_SELECTABLED) && (base->object->flag & SELECT)) {
433                                                                 base->flag |= BASE_SELECTED;
434                                                         }
435                                                 }
436                                         }
437                                 }
438                                 BLI_freelistN(&scene->r.layers);
439
440                                 ViewLayer *view_layer = BKE_view_layer_add(scene, "Viewport");
441                                 /* If we ported all the original render layers, we don't need to make the viewport layer renderable. */
442                                 if (!BLI_listbase_is_single(&scene->view_layers)) {
443                                         view_layer->flag &= ~VIEW_LAYER_RENDER;
444                                 }
445
446                                 /* If layer was not set, disable it. */
447                                 LayerCollection *layer_collection_parent;
448                                 layer_collection_parent =
449                                         ((LayerCollection *)view_layer->layer_collections.first)->layer_collections.first;
450
451                                 for (int layer = 0; layer < 20; layer++) {
452                                         if (collections[DO_VERSION_COLLECTION_VISIBLE].created & (1 << layer)) {
453                                                 const bool is_disabled = (scene->lay & (1 << layer)) == 0;
454
455                                                 /* We only need to disable the parent collection. */
456                                                 if (is_disabled) {
457                                                         layer_collection_parent->flag |= COLLECTION_DISABLED;
458                                                 }
459
460                                                 LayerCollection *layer_collection_child;
461                                                 layer_collection_child = layer_collection_parent->layer_collections.first;
462
463                                                 for (int j = 1; j < 4; j++) {
464                                                         if (collections[j].created & (1 << layer)) {
465                                                                 layer_collection_child->flag = COLLECTION_VIEWPORT |
466                                                                                                COLLECTION_RENDER |
467                                                                                                collections[j].flag_viewport;
468                                                                 layer_collection_child = layer_collection_child->next;
469                                                         }
470                                                 }
471                                                 BLI_assert(layer_collection_child == NULL);
472                                                 layer_collection_parent = layer_collection_parent->next;
473                                         }
474                                 }
475                                 BLI_assert(layer_collection_parent == NULL);
476
477                                 /* convert active base */
478                                 if (scene->basact) {
479                                         view_layer->basact = BKE_view_layer_base_find(view_layer, scene->basact->object);
480                                 }
481
482                                 /* convert selected bases */
483                                 for (Base *base = view_layer->object_bases.first; base; base = base->next) {
484                                         if ((base->flag & BASE_SELECTABLED) && (base->object->flag & SELECT)) {
485                                                 base->flag |= BASE_SELECTED;
486                                         }
487
488                                         /* keep lay around for forward compatibility (open those files in 2.79) */
489                                         base->lay = base->object->lay;
490                                 }
491
492                                 /* remove bases once and for all */
493                                 for (Base *base = scene->base.first; base; base = base->next) {
494                                         id_us_min(&base->object->id);
495                                 }
496                                 BLI_freelistN(&scene->base);
497                                 scene->basact = NULL;
498                         }
499                 }
500         }
501
502         if (!MAIN_VERSION_ATLEAST(main, 280, 0)) {
503                 for (bScreen *screen = main->screen.first; screen; screen = screen->id.next) {
504                         /* same render-layer as do_version_workspaces_after_lib_link will activate,
505                          * so same layer as BKE_view_layer_from_workspace_get would return */
506                         ViewLayer *layer = screen->scene->view_layers.first;
507
508                         for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
509                                 for (SpaceLink *view_layer = sa->spacedata.first; view_layer; view_layer = view_layer->next) {
510                                         if (view_layer->spacetype == SPACE_OUTLINER) {
511                                                 SpaceOops *soutliner = (SpaceOops *)view_layer;
512
513                                                 soutliner->outlinevis = SO_COLLECTIONS;
514
515                                                 if (BLI_listbase_count_at_most(&layer->layer_collections, 2) == 1) {
516                                                         if (soutliner->treestore == NULL) {
517                                                                 soutliner->treestore = BLI_mempool_create(
518                                                                         sizeof(TreeStoreElem), 1, 512, BLI_MEMPOOL_ALLOW_ITER);
519                                                         }
520
521                                                         /* Create a tree store element for the collection. This is normally
522                                                          * done in check_persistent (outliner_tree.c), but we need to access
523                                                          * it here :/ (expand element if it's the only one) */
524                                                         TreeStoreElem *tselem = BLI_mempool_calloc(soutliner->treestore);
525                                                         tselem->type = TSE_LAYER_COLLECTION;
526                                                         tselem->id = layer->layer_collections.first;
527                                                         tselem->nr = tselem->used = 0;
528                                                         tselem->flag &= ~TSE_CLOSED;
529                                                 }
530                                         }
531                                 }
532                         }
533                 }
534         }
535
536         /* New workspace design */
537         if (!MAIN_VERSION_ATLEAST(main, 280, 1)) {
538                 do_version_workspaces_after_lib_link(main);
539         }
540
541         if (!MAIN_VERSION_ATLEAST(main, 280, 2)) {
542                 /* Cleanup any remaining SceneRenderLayer data for files that were created
543                  * with Blender 2.8 before the SceneRenderLayer > RenderLayer refactor. */
544                 for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
545                         for (SceneRenderLayer *srl = scene->r.layers.first; srl; srl = srl->next) {
546                                 if (srl->prop) {
547                                         IDP_FreeProperty(srl->prop);
548                                         MEM_freeN(srl->prop);
549                                 }
550                                 BKE_freestyle_config_free(&srl->freestyleConfig, true);
551                         }
552                         BLI_freelistN(&scene->r.layers);
553                 }
554         }
555
556         if (!MAIN_VERSION_ATLEAST(main, 280, 3)) {
557                 /* Due to several changes to particle RNA and draw code particles from older files may no longer
558                  * be visible. Here we correct this by setting a default draw size for those files. */
559                 for (Object *object = main->object.first; object; object = object->id.next) {
560                         for (ParticleSystem *psys = object->particlesystem.first; psys; psys = psys->next) {
561                                 if (psys->part->draw_size == 0.0f) {
562                                         psys->part->draw_size = 0.1f;
563                                 }
564                         }
565                 }
566         }
567
568         {
569                 for (WorkSpace *workspace = main->workspaces.first; workspace; workspace = workspace->id.next) {
570                         if (workspace->view_layer) {
571                                 /* During 2.8 work we temporarly stored view-layer in the
572                                  * workspace directly, but should be stored there per-scene. */
573                                 for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
574                                         if (BLI_findindex(&scene->view_layers, workspace->view_layer) != -1) {
575                                                 BKE_workspace_view_layer_set(workspace, workspace->view_layer, scene);
576                                                 workspace->view_layer = NULL;
577                                         }
578                                 }
579                         }
580                         /* While this should apply to most cases, it fails when reading workspaces.blend
581                          * to get its list of workspaces without actually appending any of them. */
582 //                      BLI_assert(workspace->view_layer == NULL);
583                 }
584         }
585
586         {
587                 /* Since we don't have access to FileData we check the (always valid) master collection of the group. */
588                 for (Group *group = main->group.first; group; group = group->id.next) {
589                         if (group->collection == NULL) {
590                                 BKE_group_init(group);
591                                 SceneCollection *sc = GROUP_MASTER_COLLECTION(group);
592                                 SceneCollection *sc_hidden = NULL;
593
594                                 for (GroupObject *go = group->gobject.first; go; go = go->next) {
595                                         if (go->ob->lay & group->layer) {
596                                                 BKE_collection_object_add(&group->id, sc, go->ob);
597                                         }
598                                         else {
599                                                 if (sc_hidden == NULL) {
600                                                         sc_hidden = BKE_collection_add(&group->id, sc, COLLECTION_TYPE_GROUP_INTERNAL, "Hidden");
601                                                 }
602                                                 BKE_collection_object_add(&group->id, sc_hidden, go->ob);
603                                         }
604                                 }
605
606                                 if (sc_hidden != NULL) {
607                                         LayerCollection *layer_collection_master, *layer_collection_hidden;
608                                         layer_collection_master = group->view_layer->layer_collections.first;
609                                         layer_collection_hidden = layer_collection_master->layer_collections.first;
610                                         layer_collection_hidden->flag |= COLLECTION_DISABLED;
611                                 }
612                         }
613
614                         GroupObject *go;
615                         while ((go = BLI_pophead(&group->gobject))) {
616                                 MEM_freeN(go);
617                         }
618                 }
619         }
620
621         {
622                 for (Object *object = main->object.first; object; object = object->id.next) {
623 #ifndef VERSION_280_SUBVERSION_4
624                         /* If any object already has an initialized value for
625                          * duplicator_visibility_flag it means we've already doversioned it.
626                          * TODO(all) remove the VERSION_280_SUBVERSION_4 code once the subversion was bumped. */
627                         if (object->duplicator_visibility_flag != 0) {
628                                 break;
629                         }
630 #endif
631                         if (object->particlesystem.first) {
632                                 object->duplicator_visibility_flag = OB_DUPLI_FLAG_VIEWPORT;
633                                 for (ParticleSystem *psys = object->particlesystem.first; psys; psys = psys->next) {
634                                         if (psys->part->draw & PART_DRAW_EMITTER) {
635                                                 object->duplicator_visibility_flag |= OB_DUPLI_FLAG_RENDER;
636 #ifndef VERSION_280_SUBVERSION_4
637                                                 psys->part->draw &= ~PART_DRAW_EMITTER;
638 #else
639                                                 break;
640 #endif
641                                         }
642                                 }
643                         }
644                         else if (object->transflag & OB_DUPLI) {
645                                 object->duplicator_visibility_flag = OB_DUPLI_FLAG_VIEWPORT;
646                         }
647                         else {
648                                 object->duplicator_visibility_flag = OB_DUPLI_FLAG_VIEWPORT | OB_DUPLI_FLAG_RENDER;
649                         }
650                 }
651         }
652
653         /* SpaceTime & SpaceLogic removal/replacing */
654         if (!MAIN_VERSION_ATLEAST(main, 280, 9)) {
655                 const wmWindowManager *wm = main->wm.first;
656                 const Scene *scene = main->scene.first;
657
658                 if (wm != NULL) {
659                         /* Action editors need a scene for creation. First, update active
660                          * screens using the active scene of the window they're displayed in.
661                          * Next, update remaining screens using first scene in main listbase. */
662
663                         for (wmWindow *win = wm->windows.first; win; win = win->next) {
664                                 const bScreen *screen = BKE_workspace_active_screen_get(win->workspace_hook);
665                                 for (ScrArea *area = screen->areabase.first; area; area = area->next) {
666                                         if (ELEM(area->butspacetype, SPACE_TIME, SPACE_LOGIC)) {
667                                                 do_version_area_change_space_to_space_action(area, win->scene);
668
669                                                 /* Don't forget to unset! */
670                                                 area->butspacetype = SPACE_EMPTY;
671                                         }
672                                 }
673                         }
674                 }
675                 if (scene != NULL) {
676                         for (bScreen *screen = main->screen.first; screen; screen = screen->id.next) {
677                                 for (ScrArea *area = screen->areabase.first; area; area = area->next) {
678                                         if (ELEM(area->butspacetype, SPACE_TIME, SPACE_LOGIC)) {
679                                                 /* Areas that were already handled won't be handled again */
680                                                 do_version_area_change_space_to_space_action(area, scene);
681
682                                                 /* Don't forget to unset! */
683                                                 area->butspacetype = SPACE_EMPTY;
684                                         }
685                                 }
686                         }
687                 }
688         }
689 }
690
691 static void do_version_layer_collections_idproperties(ListBase *lb)
692 {
693         IDPropertyTemplate val = {0};
694         for (LayerCollection *lc = lb->first; lc; lc = lc->next) {
695                 lc->properties = IDP_New(IDP_GROUP, &val, ROOT_PROP);
696                 BKE_layer_collection_engine_settings_create(lc->properties);
697
698                 /* No overrides at first */
699                 for (IDProperty *prop = lc->properties->data.group.first; prop; prop = prop->next) {
700                         while (prop->data.group.first) {
701                                 IDP_FreeFromGroup(prop, prop->data.group.first);
702                         }
703                 }
704
705                 /* Do it recursively */
706                 do_version_layer_collections_idproperties(&lc->layer_collections);
707         }
708 }
709
710 static void do_version_view_layer_visibility(ViewLayer *view_layer)
711 {
712         LayerCollection *layer_collection;
713         for (layer_collection = view_layer->layer_collections.first;
714              layer_collection;
715              layer_collection = layer_collection->next)
716         {
717                 if (layer_collection->flag & COLLECTION_DISABLED) {
718                         BKE_collection_enable(view_layer, layer_collection);
719                         layer_collection->flag &= ~COLLECTION_DISABLED;
720                 }
721
722                 if ((layer_collection->flag & (1 << 0)) == 0) { /* !COLLECTION_VISIBLE */
723                         layer_collection->flag |= COLLECTION_DISABLED;
724                 }
725                 layer_collection->flag |= COLLECTION_VIEWPORT | COLLECTION_RENDER;
726         }
727 }
728
729 void blo_do_versions_280(FileData *fd, Library *UNUSED(lib), Main *main)
730 {
731
732         if (!MAIN_VERSION_ATLEAST(main, 280, 0)) {
733                 if (!DNA_struct_elem_find(fd->filesdna, "Scene", "ListBase", "view_layers")) {
734                         for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
735                                 /* Master Collection */
736                                 scene->collection = MEM_callocN(sizeof(SceneCollection), "Master Collection");
737                                 BLI_strncpy(scene->collection->name, "Master Collection", sizeof(scene->collection->name));
738                         }
739                 }
740
741                 if (DNA_struct_elem_find(fd->filesdna, "LayerCollection", "ListBase", "engine_settings") &&
742                     !DNA_struct_elem_find(fd->filesdna, "LayerCollection", "IDProperty", "properties"))
743                 {
744                         for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
745                                 for (ViewLayer *view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
746                                         do_version_layer_collections_idproperties(&view_layer->layer_collections);
747                                 }
748                         }
749                 }
750
751                 for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
752                         scene->r.gauss = 1.5f;
753                 }
754         }
755
756         if (!MAIN_VERSION_ATLEAST(main, 280, 1)) {
757                 if (!DNA_struct_elem_find(fd->filesdna, "Lamp", "float", "bleedexp")) {
758                         for (Lamp *la = main->lamp.first; la; la = la->id.next) {
759                                 la->bleedexp = 2.5f;
760                         }
761                 }
762
763                 if (!DNA_struct_elem_find(fd->filesdna, "GPUDOFSettings", "float", "ratio")) {
764                         for (Camera *ca = main->camera.first; ca; ca = ca->id.next) {
765                                 ca->gpu_dof.ratio = 1.0f;
766                         }
767                 }
768
769                 if (!DNA_struct_elem_find(fd->filesdna, "ViewLayer", "IDProperty", "*properties")) {
770                         for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
771                                 for (ViewLayer *view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
772                                         IDPropertyTemplate val = {0};
773                                         view_layer->properties = IDP_New(IDP_GROUP, &val, ROOT_PROP);
774                                         BKE_view_layer_engine_settings_create(view_layer->properties);
775                                 }
776                         }
777                 }
778
779                 /* MTexPoly now removed. */
780                 if (DNA_struct_find(fd->filesdna, "MTexPoly")) {
781                         const int cd_mtexpoly = 15;  /* CD_MTEXPOLY, deprecated */
782                         for (Mesh *me = main->mesh.first; me; me = me->id.next) {
783                                 /* If we have UV's, so this file will have MTexPoly layers too! */
784                                 if (me->mloopuv != NULL) {
785                                         CustomData_update_typemap(&me->pdata);
786                                         CustomData_free_layers(&me->pdata, cd_mtexpoly, me->totpoly);
787                                         BKE_mesh_update_customdata_pointers(me, false);
788                                 }
789                         }
790                 }
791         }
792
793         if (!MAIN_VERSION_ATLEAST(main, 280, 2)) {
794                 if (!DNA_struct_elem_find(fd->filesdna, "Lamp", "float", "cascade_max_dist")) {
795                         for (Lamp *la = main->lamp.first; la; la = la->id.next) {
796                                 la->cascade_max_dist = 1000.0f;
797                                 la->cascade_count = 4;
798                                 la->cascade_exponent = 0.8f;
799                                 la->cascade_fade = 0.1f;
800                         }
801                 }
802
803                 if (!DNA_struct_elem_find(fd->filesdna, "Lamp", "float", "contact_dist")) {
804                         for (Lamp *la = main->lamp.first; la; la = la->id.next) {
805                                 la->contact_dist = 1.0f;
806                                 la->contact_bias = 0.03f;
807                                 la->contact_spread = 0.2f;
808                                 la->contact_thickness = 0.5f;
809                         }
810                 }
811
812                 if (!DNA_struct_elem_find(fd->filesdna, "LightProbe", "float", "vis_bias")) {
813                         for (LightProbe *probe = main->lightprobe.first; probe; probe = probe->id.next) {
814                                 probe->vis_bias = 1.0f;
815                                 probe->vis_blur = 0.2f;
816                         }
817                 }
818
819                 typedef enum eNTreeDoVersionErrors {
820                         NTREE_DOVERSION_NO_ERROR = 0,
821                         NTREE_DOVERSION_NEED_OUTPUT = (1 << 0),
822                         NTREE_DOVERSION_TRANSPARENCY_EMISSION = (1 << 1),
823                 } eNTreeDoVersionErrors;
824
825                 /* Eevee shader nodes renamed because of the output node system.
826                  * Note that a new output node is not being added here, because it would be overkill
827                  * to handle this case in lib_verify_nodetree.
828                  *
829                  * Also, metallic node is now unified into the principled node. */
830                 eNTreeDoVersionErrors error = NTREE_DOVERSION_NO_ERROR;
831
832                 FOREACH_NODETREE(main, ntree, id) {
833                         if (ntree->type == NTREE_SHADER) {
834                                 for (bNode *node = ntree->nodes.first; node; node = node->next) {
835                                         if (node->type == 194 /* SH_NODE_EEVEE_METALLIC */ &&
836                                             STREQ(node->idname, "ShaderNodeOutputMetallic"))
837                                         {
838                                                 BLI_strncpy(node->idname, "ShaderNodeEeveeMetallic", sizeof(node->idname));
839                                                 error |= NTREE_DOVERSION_NEED_OUTPUT;
840                                         }
841
842                                         else if (node->type == SH_NODE_EEVEE_SPECULAR && STREQ(node->idname, "ShaderNodeOutputSpecular")) {
843                                                 BLI_strncpy(node->idname, "ShaderNodeEeveeSpecular", sizeof(node->idname));
844                                                 error |= NTREE_DOVERSION_NEED_OUTPUT;
845                                         }
846
847                                         else if (node->type == 196 /* SH_NODE_OUTPUT_EEVEE_MATERIAL */ &&
848                                                  STREQ(node->idname, "ShaderNodeOutputEeveeMaterial"))
849                                         {
850                                                 node->type = SH_NODE_OUTPUT_MATERIAL;
851                                                 BLI_strncpy(node->idname, "ShaderNodeOutputMaterial", sizeof(node->idname));
852                                         }
853
854                                         else if (node->type == 194 /* SH_NODE_EEVEE_METALLIC */ &&
855                                                  STREQ(node->idname, "ShaderNodeEeveeMetallic"))
856                                         {
857                                                 node->type = SH_NODE_BSDF_PRINCIPLED;
858                                                 BLI_strncpy(node->idname, "ShaderNodeBsdfPrincipled", sizeof(node->idname));
859                                                 node->custom1 = SHD_GLOSSY_MULTI_GGX;
860                                                 error |= NTREE_DOVERSION_TRANSPARENCY_EMISSION;
861                                         }
862                                 }
863                         }
864                 } FOREACH_NODETREE_END
865
866                 if (error & NTREE_DOVERSION_NEED_OUTPUT) {
867                         BKE_report(fd->reports, RPT_ERROR, "Eevee material conversion problem. Error in console");
868                         printf("You need to connect Principled and Eevee Specular shader nodes to new material output nodes.\n");
869                 }
870
871                 if (error & NTREE_DOVERSION_TRANSPARENCY_EMISSION) {
872                         BKE_report(fd->reports, RPT_ERROR, "Eevee material conversion problem. Error in console");
873                         printf("You need to combine transparency and emission shaders to the converted Principled shader nodes.\n");
874                 }
875
876                 if ((DNA_struct_elem_find(fd->filesdna, "ViewLayer", "FreestyleConfig", "freestyle_config") == false) &&
877                     DNA_struct_elem_find(fd->filesdna, "Scene", "ListBase", "view_layers"))
878                 {
879                         for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
880                                 ViewLayer *view_layer;
881                                 for (view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
882                                         view_layer->flag |= VIEW_LAYER_FREESTYLE;
883                                         view_layer->layflag = 0x7FFF;   /* solid ztra halo edge strand */
884                                         view_layer->passflag = SCE_PASS_COMBINED | SCE_PASS_Z;
885                                         view_layer->pass_alpha_threshold = 0.5f;
886                                         BKE_freestyle_config_init(&view_layer->freestyle_config);
887                                 }
888                         }
889                 }
890         }
891
892         if (!MAIN_VERSION_ATLEAST(main, 280, 3)) {
893                 for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
894                         ViewLayer *view_layer;
895                         for (view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
896                                 do_version_view_layer_visibility(view_layer);
897                         }
898                 }
899
900                 for (Group *group = main->group.first; group; group = group->id.next) {
901                         if (group->view_layer != NULL) {
902                                 do_version_view_layer_visibility(group->view_layer);
903                         }
904                 }
905         }
906
907         if (!MAIN_VERSION_ATLEAST(main, 280, 6)) {
908                 if (DNA_struct_elem_find(fd->filesdna, "SpaceOops", "int", "filter") == false) {
909                         bScreen *sc;
910                         ScrArea *sa;
911                         SpaceLink *sl;
912
913                         /* Update files using invalid (outdated) outlinevis Outliner values. */
914                         for (sc = main->screen.first; sc; sc = sc->id.next) {
915                                 for (sa = sc->areabase.first; sa; sa = sa->next) {
916                                         for (sl = sa->spacedata.first; sl; sl = sl->next) {
917                                                 if (sl->spacetype == SPACE_OUTLINER) {
918                                                         SpaceOops *so = (SpaceOops *)sl;
919
920                                                         if (!ELEM(so->outlinevis,
921                                                                   SO_SCENES,
922                                                                   SO_GROUPS,
923                                                                   SO_LIBRARIES,
924                                                                   SO_SEQUENCE,
925                                                                   SO_DATABLOCKS,
926                                                                   SO_ID_ORPHANS,
927                                                                   SO_COLLECTIONS))
928                                                         {
929                                                                 so->outlinevis = SO_COLLECTIONS;
930                                                         }
931                                                 }
932                                         }
933                                 }
934                         }
935                 }
936
937                 if (!DNA_struct_elem_find(fd->filesdna, "LightProbe", "float", "intensity")) {
938                         for (LightProbe *probe = main->lightprobe.first; probe; probe = probe->id.next) {
939                                 probe->intensity = 1.0f;
940                         }
941                 }
942
943                 for (Object *ob = main->object.first; ob; ob = ob->id.next) {
944                         bConstraint *con, *con_next;
945                         con = ob->constraints.first;
946                         while (con) {
947                                 con_next = con->next;
948                                 if (con->type == 17) { /* CONSTRAINT_TYPE_RIGIDBODYJOINT */
949                                         BLI_remlink(&ob->constraints, con);
950                                         BKE_constraint_free_data(con);
951                                         MEM_freeN(con);
952                                 }
953                                 con = con_next;
954                         }
955                 }
956
957                 if (!DNA_struct_elem_find(fd->filesdna, "Scene", "int", "orientation_index_custom")) {
958                         for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
959                                 scene->orientation_index_custom = -1;
960                         }
961                 }
962
963                 for (bScreen *sc = main->screen.first; sc; sc = sc->id.next) {
964                         for (ScrArea *sa = sc->areabase.first; sa; sa = sa->next) {
965                                 for (SpaceLink *sl = sa->spacedata.first; sl; sl = sl->next) {
966                                         if (sl->spacetype == SPACE_VIEW3D) {
967                                                 View3D *v3d = (View3D *)sl;
968                                                 v3d->drawtype_lighting = V3D_LIGHTING_STUDIO;
969
970                                                 /* Assume (demo) files written with 2.8 want to show
971                                                  * Eevee renders in the viewport. */
972                                                 if (MAIN_VERSION_ATLEAST(main, 280, 0)) {
973                                                         v3d->drawtype = OB_MATERIAL;
974                                                 }
975                                         }
976                                 }
977                         }
978                 }
979         }
980
981         if (!MAIN_VERSION_ATLEAST(main, 280, 7)) {
982                 /* Render engine storage moved elsewhere and back during 2.8
983                  * development, we assume any files saved in 2.8 had Eevee set
984                  * as scene render engine. */
985                 if (MAIN_VERSION_ATLEAST(main, 280, 0)) {
986                         for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
987                                 BLI_strncpy(scene->r.engine, RE_engine_id_BLENDER_EEVEE, sizeof(scene->r.engine));
988                         }
989                 }
990         }
991
992         if (!MAIN_VERSION_ATLEAST(main, 280, 8)) {
993                 /* Blender Internal removal */
994                 for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
995                         if (STREQ(scene->r.engine, "BLENDER_RENDER") ||
996                             STREQ(scene->r.engine, "BLENDER_GAME"))
997                         {
998                                 BLI_strncpy(scene->r.engine, RE_engine_id_BLENDER_EEVEE, sizeof(scene->r.engine));
999                         }
1000
1001                         scene->r.bake_mode = 0;
1002                 }
1003
1004                 for (Tex *tex = main->tex.first; tex; tex = tex->id.next) {
1005                         /* Removed envmap, pointdensity, voxeldata, ocean textures. */
1006                         if (ELEM(tex->type, 10, 14, 15, 16)) {
1007                                 tex->type = 0;
1008                         }
1009                 }
1010         }
1011
1012         if (!MAIN_VERSION_ATLEAST(main, 280, 11)) {
1013
1014                 /* Remove info editor, but only if at the top of the window. */
1015                 for (bScreen *screen = main->screen.first; screen; screen = screen->id.next) {
1016                         /* Calculate window width/height from screen vertices */
1017                         int win_width = 0, win_height = 0;
1018                         for (ScrVert *vert = screen->vertbase.first; vert; vert = vert->next) {
1019                                 win_width  = MAX2(win_width, vert->vec.x);
1020                                 win_height = MAX2(win_height, vert->vec.y);
1021                         }
1022
1023                         for (ScrArea *area = screen->areabase.first, *area_next; area; area = area_next) {
1024                                 area_next = area->next;
1025
1026                                 if (area->spacetype == SPACE_INFO) {
1027                                         if ((area->v2->vec.y == win_height) && (area->v1->vec.x == 0) && (area->v4->vec.x == win_width)) {
1028                                                 BKE_screen_area_free(area);
1029
1030                                                 BLI_remlink(&screen->areabase, area);
1031
1032                                                 BKE_screen_remove_double_scredges(screen);
1033                                                 BKE_screen_remove_unused_scredges(screen);
1034                                                 BKE_screen_remove_unused_scrverts(screen);
1035
1036                                                 MEM_freeN(area);
1037                                         }
1038                                 }
1039                                 /* AREA_TEMP_INFO is deprecated from now on, it should only be set for info areas
1040                                  * which are deleted above, so don't need to unset it. Its slot/bit can be reused */
1041                         }
1042                 }
1043         }
1044
1045         if (!MAIN_VERSION_ATLEAST(main, 280, 11)) {
1046                 for (Lamp *lamp = main->lamp.first; lamp; lamp = lamp->id.next) {
1047                         if (lamp->mode & (1 << 13)) { /* LA_SHAD_RAY */
1048                                 lamp->mode |= LA_SHADOW;
1049                                 lamp->mode &= ~(1 << 13);
1050                         }
1051                 }
1052         }
1053
1054         if (!MAIN_VERSION_ATLEAST(main, 280, 12)) {
1055                 /* Remove tool property regions. */
1056                 for (bScreen *screen = main->screen.first; screen; screen = screen->id.next) {
1057                         for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
1058                                 for (SpaceLink *sl = sa->spacedata.first; sl; sl = sl->next) {
1059                                         if (ELEM(sl->spacetype, SPACE_VIEW3D, SPACE_CLIP)) {
1060                                                 ListBase *regionbase = (sl == sa->spacedata.first) ? &sa->regionbase : &sl->regionbase;
1061
1062                                                 for (ARegion *region = regionbase->first, *region_next; region; region = region_next) {
1063                                                         region_next = region->next;
1064
1065                                                         if (region->regiontype == RGN_TYPE_TOOL_PROPS) {
1066                                                                 BKE_area_region_free(NULL, region);
1067                                                                 BLI_freelinkN(regionbase, region);
1068                                                         }
1069                                                 }
1070                                         }
1071                                 }
1072                         }
1073                 }
1074         }
1075
1076         {
1077                 if (!DNA_struct_elem_find(fd->filesdna, "Lamp", "float", "spec_fac")) {
1078                         for (Lamp *la = main->lamp.first; la; la = la->id.next) {
1079                                 la->spec_fac = 1.0f;
1080                         }
1081                 }
1082         }
1083 }