Outliner: Remove User Preferences
[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
33 #include "BLI_listbase.h"
34 #include "BLI_mempool.h"
35 #include "BLI_string.h"
36 #include "BLI_string_utf8.h"
37 #include "BLI_utildefines.h"
38
39 #include "DNA_object_types.h"
40 #include "DNA_camera_types.h"
41 #include "DNA_gpu_types.h"
42 #include "DNA_group_types.h"
43 #include "DNA_lamp_types.h"
44 #include "DNA_layer_types.h"
45 #include "DNA_lightprobe_types.h"
46 #include "DNA_material_types.h"
47 #include "DNA_mesh_types.h"
48 #include "DNA_particle_types.h"
49 #include "DNA_scene_types.h"
50 #include "DNA_screen_types.h"
51 #include "DNA_view3d_types.h"
52 #include "DNA_genfile.h"
53 #include "DNA_workspace_types.h"
54
55 #include "BKE_collection.h"
56 #include "BKE_customdata.h"
57 #include "BKE_freestyle.h"
58 #include "BKE_group.h"
59 #include "BKE_idprop.h"
60 #include "BKE_layer.h"
61 #include "BKE_main.h"
62 #include "BKE_mesh.h"
63 #include "BKE_node.h"
64 #include "BKE_report.h"
65 #include "BKE_scene.h"
66 #include "BKE_workspace.h"
67
68 #include "BLO_readfile.h"
69 #include "readfile.h"
70
71 #include "MEM_guardedalloc.h"
72
73
74 static bScreen *screen_parent_find(const bScreen *screen)
75 {
76         /* can avoid lookup if screen state isn't maximized/full (parent and child store the same state) */
77         if (ELEM(screen->state, SCREENMAXIMIZED, SCREENFULL)) {
78                 for (const ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
79                         if (sa->full && sa->full != screen) {
80                                 BLI_assert(sa->full->state == screen->state);
81                                 return sa->full;
82                         }
83                 }
84         }
85
86         return NULL;
87 }
88
89 static void do_version_workspaces_create_from_screens(Main *bmain)
90 {
91         for (bScreen *screen = bmain->screen.first; screen; screen = screen->id.next) {
92                 const bScreen *screen_parent = screen_parent_find(screen);
93                 Scene *scene = screen->scene;
94                 WorkSpace *workspace;
95                 ViewLayer *layer = BKE_view_layer_from_scene_get(scene);
96                 ListBase *transform_orientations;
97
98                 if (screen_parent) {
99                         /* fullscreen with "Back to Previous" option, don't create
100                          * a new workspace, add layout workspace containing parent */
101                         workspace = BLI_findstring(
102                                 &bmain->workspaces, screen_parent->id.name + 2, offsetof(ID, name) + 2);
103                 }
104                 else {
105                         workspace = BKE_workspace_add(bmain, screen->id.name + 2);
106                 }
107                 BKE_workspace_layout_add(workspace, screen, screen->id.name + 2);
108                 BKE_workspace_view_layer_set(workspace, layer, scene);
109
110 #ifdef WITH_CLAY_ENGINE
111                 BLI_strncpy(workspace->view_render.engine_id, RE_engine_id_BLENDER_CLAY,
112                             sizeof(workspace->view_render.engine_id));
113 #else
114                 BLI_strncpy(workspace->view_render.engine_id, RE_engine_id_BLENDER_EEVEE,
115                             sizeof(workspace->view_render.engine_id));
116 #endif
117
118                 transform_orientations = BKE_workspace_transform_orientations_get(workspace);
119                 BLI_duplicatelist(transform_orientations, &scene->transform_spaces);
120         }
121 }
122
123 /**
124  * \brief After lib-link versioning for new workspace design.
125  *
126  *  *  Adds a workspace for (almost) each screen of the old file
127  *     and adds the needed workspace-layout to wrap the screen.
128  *  *  Active screen isn't stored directly in window anymore, but in the active workspace.
129  *  *  Active scene isn't stored in screen anymore, but in window.
130  *  *  Create workspace instance hook for each window.
131  *
132  * \note Some of the created workspaces might be deleted again in case of reading the default startup.blend.
133  */
134 static void do_version_workspaces_after_lib_link(Main *bmain)
135 {
136         BLI_assert(BLI_listbase_is_empty(&bmain->workspaces));
137
138         do_version_workspaces_create_from_screens(bmain);
139
140         for (wmWindowManager *wm = bmain->wm.first; wm; wm = wm->id.next) {
141                 for (wmWindow *win = wm->windows.first; win; win = win->next) {
142                         bScreen *screen_parent = screen_parent_find(win->screen);
143                         bScreen *screen = screen_parent ? screen_parent : win->screen;
144                         WorkSpace *workspace = BLI_findstring(&bmain->workspaces, screen->id.name + 2, offsetof(ID, name) + 2);
145                         ListBase *layouts = BKE_workspace_layouts_get(workspace);
146
147                         win->workspace_hook = BKE_workspace_instance_hook_create(bmain);
148
149                         BKE_workspace_active_set(win->workspace_hook, workspace);
150                         BKE_workspace_active_layout_set(win->workspace_hook, layouts->first);
151
152                         win->scene = screen->scene;
153                         /* Deprecated from now on! */
154                         win->screen = NULL;
155                 }
156         }
157
158         for (bScreen *screen = bmain->screen.first; screen; screen = screen->id.next) {
159                 /* Deprecated from now on! */
160                 BLI_freelistN(&screen->scene->transform_spaces);
161                 screen->scene = NULL;
162         }
163 }
164
165 enum {
166         DO_VERSION_COLLECTION_VISIBLE     = 0,
167         DO_VERSION_COLLECTION_HIDE        = 1,
168         DO_VERSION_COLLECTION_HIDE_RENDER = 2,
169         DO_VERSION_COLLECTION_HIDE_ALL    = 3,
170 };
171
172 void do_versions_after_linking_280(Main *main)
173 {
174         if (!MAIN_VERSION_ATLEAST(main, 280, 0)) {
175                 for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
176                         /* since we don't have access to FileData we check the (always valid) first render layer instead */
177                         if (scene->view_layers.first == NULL) {
178                                 SceneCollection *sc_master = BKE_collection_master(&scene->id);
179                                 BLI_strncpy(sc_master->name, "Master Collection", sizeof(sc_master->name));
180
181                                 struct DoVersionSceneCollections {
182                                         SceneCollection *collections[20];
183                                         int created;
184                                         const char *suffix;
185                                         int flag_viewport;
186                                         int flag_render;
187                                 } collections[] =
188                                 {
189                                         {
190                                                 .collections = {NULL},
191                                                 .created = 0,
192                                                 .suffix = "",
193                                                 .flag_viewport = COLLECTION_SELECTABLE,
194                                                 .flag_render = COLLECTION_SELECTABLE
195                                         },
196                                         {
197                                                 .collections = {NULL},
198                                                 .created = 0,
199                                                 .suffix = " - Hide Viewport",
200                                                 .flag_viewport = COLLECTION_SELECTABLE,
201                                                 .flag_render = COLLECTION_SELECTABLE
202                                         },
203                                         {
204                                                 .collections = {NULL},
205                                                 .created = 0,
206                                                 .suffix = " - Hide Render",
207                                                 .flag_viewport = COLLECTION_SELECTABLE,
208                                                 .flag_render = COLLECTION_SELECTABLE | COLLECTION_DISABLED
209                                         },
210                                         {
211                                                 .collections = {NULL},
212                                                 .created = 0,
213                                                 .suffix = " - Hide Render All",
214                                                 .flag_viewport = COLLECTION_SELECTABLE | COLLECTION_DISABLED,
215                                                 .flag_render = COLLECTION_SELECTABLE | COLLECTION_DISABLED
216                                         }
217                                 };
218
219                                 for (int layer = 0; layer < 20; layer++) {
220                                         for (Base *base = scene->base.first; base; base = base->next) {
221                                                 if (base->lay & (1 << layer)) {
222                                                         int collection_index = -1;
223                                                         if ((base->object->restrictflag & OB_RESTRICT_VIEW) &&
224                                                             (base->object->restrictflag & OB_RESTRICT_RENDER))
225                                                         {
226                                                                 collection_index = DO_VERSION_COLLECTION_HIDE_ALL;
227                                                         }
228                                                         else if (base->object->restrictflag & OB_RESTRICT_VIEW) {
229                                                                 collection_index = DO_VERSION_COLLECTION_HIDE;
230                                                         }
231                                                         else if (base->object->restrictflag & OB_RESTRICT_RENDER) {
232                                                                 collection_index = DO_VERSION_COLLECTION_HIDE_RENDER;
233                                                         }
234                                                         else {
235                                                                 collection_index = DO_VERSION_COLLECTION_VISIBLE;
236                                                         }
237
238                                                         /* Create collections when needed only. */
239                                                         if ((collections[collection_index].created & (1 << layer)) == 0) {
240                                                                 char name[MAX_NAME];
241
242                                                                 if ((collections[DO_VERSION_COLLECTION_VISIBLE].created & (1 << layer)) == 0) {
243                                                                         BLI_snprintf(name,
244                                                                                      sizeof(sc_master->name),
245                                                                                      "Collection %d%s",
246                                                                                      layer + 1,
247                                                                                      collections[DO_VERSION_COLLECTION_VISIBLE].suffix);
248                                                                         collections[DO_VERSION_COLLECTION_VISIBLE].collections[layer] =
249                                                                                 BKE_collection_add(&scene->id, sc_master, COLLECTION_TYPE_NONE, name);
250                                                                         collections[DO_VERSION_COLLECTION_VISIBLE].created |= (1 << layer);
251                                                                 }
252
253                                                                 if (collection_index != DO_VERSION_COLLECTION_VISIBLE) {
254                                                                         SceneCollection *sc_parent;
255                                                                         sc_parent = collections[DO_VERSION_COLLECTION_VISIBLE].collections[layer];
256                                                                         BLI_snprintf(name,
257                                                                                      sizeof(sc_master->name),
258                                                                                      "Collection %d%s",
259                                                                                      layer + 1,
260                                                                                      collections[collection_index].suffix);
261                                                                         collections[collection_index].collections[layer] = BKE_collection_add(
262                                                                                 &scene->id,
263                                                                                 sc_parent,
264                                                                                 COLLECTION_TYPE_NONE,
265                                                                                 name);
266                                                                         collections[collection_index].created |= (1 << layer);
267                                                                 }
268                                                         }
269
270                                                         BKE_collection_object_add(
271                                                                 &scene->id, collections[collection_index].collections[layer], base->object);
272                                                 }
273
274                                                 if (base->flag & SELECT) {
275                                                         base->object->flag |= SELECT;
276                                                 }
277                                                 else {
278                                                         base->object->flag &= ~SELECT;
279                                                 }
280                                         }
281                                 }
282
283                                 /* Re-order the nested hidden collections. */
284                                 SceneCollection *scene_collection_parent = sc_master->scene_collections.first;
285
286                                 for (int layer = 0; layer < 20; layer++) {
287                                         if (collections[DO_VERSION_COLLECTION_VISIBLE].created & (1 << layer)) {
288
289                                                 if ((collections[DO_VERSION_COLLECTION_HIDE].created & (1 << layer)) &&
290                                                     (collections[DO_VERSION_COLLECTION_HIDE].collections[layer] !=
291                                                      scene_collection_parent->scene_collections.first))
292                                                 {
293                                                         BLI_listbase_swaplinks(
294                                                                 &scene_collection_parent->scene_collections,
295                                                                 collections[DO_VERSION_COLLECTION_HIDE].collections[layer],
296                                                                 scene_collection_parent->scene_collections.first);
297                                                 }
298
299                                                 if ((collections[DO_VERSION_COLLECTION_HIDE_ALL].created & (1 << layer)) &&
300                                                     (collections[DO_VERSION_COLLECTION_HIDE_ALL].collections[layer] !=
301                                                      scene_collection_parent->scene_collections.last))
302                                                 {
303                                                         BLI_listbase_swaplinks(
304                                                                 &scene_collection_parent->scene_collections,
305                                                                 collections[DO_VERSION_COLLECTION_HIDE_ALL].collections[layer],
306                                                                 scene_collection_parent->scene_collections.last);
307                                                 }
308
309                                                 scene_collection_parent = scene_collection_parent->next;
310                                         }
311                                 }
312                                 BLI_assert(scene_collection_parent == NULL);
313
314                                 scene->active_view_layer = 0;
315
316                                 /* Handle legacy render layers. */
317                                 if (!BKE_scene_uses_blender_game(scene)) {
318                                         for (SceneRenderLayer *srl = scene->r.layers.first; srl; srl = srl->next) {
319
320                                                 ViewLayer *view_layer = BKE_view_layer_add(scene, srl->name);
321
322                                                 if (srl->samples != 0) {
323                                                         /* It is up to the external engine to handle
324                                                          * its own doversion in this case. */
325                                                         BKE_override_view_layer_int_add(
326                                                                 view_layer,
327                                                                 ID_SCE,
328                                                                 "samples",
329                                                                 srl->samples);
330                                                 }
331
332                                                 if (srl->mat_override) {
333                                                         BKE_override_view_layer_datablock_add(
334                                                                 view_layer,
335                                                                 ID_MA,
336                                                                 "self",
337                                                                 (ID *)srl->mat_override);
338                                                 }
339
340                                                 if (srl->layflag & SCE_LAY_DISABLE) {
341                                                         view_layer->flag &= ~VIEW_LAYER_RENDER;
342                                                 }
343
344                                                 if ((srl->layflag & SCE_LAY_FRS) == 0) {
345                                                         view_layer->flag &= ~VIEW_LAYER_FREESTYLE;
346                                                 }
347
348                                                 /* XXX If we are to keep layflag it should be merged with flag (dfelinto). */
349                                                 view_layer->layflag = srl->layflag;
350                                                 /* XXX Not sure if we should keep the passes (dfelinto). */
351                                                 view_layer->passflag = srl->passflag;
352                                                 view_layer->pass_xor = srl->pass_xor;
353                                                 view_layer->pass_alpha_threshold = srl->pass_alpha_threshold;
354
355                                                 BKE_freestyle_config_free(&view_layer->freestyle_config, true);
356                                                 view_layer->freestyle_config = srl->freestyleConfig;
357                                                 view_layer->id_properties = srl->prop;
358
359                                                 /* unlink master collection  */
360                                                 BKE_collection_unlink(view_layer, view_layer->layer_collections.first);
361
362                                                 /* Add new collection bases. */
363                                                 for (int layer = 0; layer < 20; layer++) {
364                                                         if ((scene->lay & srl->lay & ~(srl->lay_exclude) & (1 << layer)) ||
365                                                             (srl->lay_zmask & (scene->lay | srl->lay_exclude) & (1 << layer)))
366                                                         {
367                                                                 if (collections[DO_VERSION_COLLECTION_VISIBLE].created & (1 << layer)) {
368
369                                                                         LayerCollection *layer_collection_parent;
370                                                                         layer_collection_parent = BKE_collection_link(
371                                                                                 view_layer,
372                                                                                 collections[DO_VERSION_COLLECTION_VISIBLE].collections[layer]);
373
374                                                                         if (srl->lay_zmask & (1 << layer)) {
375                                                                                 BKE_override_layer_collection_boolean_add(
376                                                                                         layer_collection_parent,
377                                                                                         ID_OB,
378                                                                                         "cycles.is_holdout",
379                                                                                         true);
380                                                                         }
381
382                                                                         if ((srl->lay & (1 << layer)) == 0) {
383                                                                                 BKE_override_layer_collection_boolean_add(
384                                                                                         layer_collection_parent,
385                                                                                         ID_OB,
386                                                                                         "cycles_visibility.camera",
387                                                                                         false);
388                                                                         }
389
390                                                                         LayerCollection *layer_collection_child;
391                                                                         layer_collection_child = layer_collection_parent->layer_collections.first;
392
393                                                                         for (int j = 1; j < 4; j++) {
394                                                                                 if (collections[j].created & (1 << layer)) {
395                                                                                         layer_collection_child->flag = COLLECTION_VIEWPORT |
396                                                                                                                        COLLECTION_RENDER |
397                                                                                                                        collections[j].flag_render;
398                                                                                         layer_collection_child = layer_collection_child->next;
399                                                                                 }
400                                                                         }
401                                                                         BLI_assert(layer_collection_child == NULL);
402                                                                 }
403                                                         }
404                                                 }
405
406                                                 /* for convenience set the same active object in all the layers */
407                                                 if (scene->basact) {
408                                                         view_layer->basact = BKE_view_layer_base_find(view_layer, scene->basact->object);
409                                                 }
410
411                                                 for (Base *base = view_layer->object_bases.first; base; base = base->next) {
412                                                         if ((base->flag & BASE_SELECTABLED) && (base->object->flag & SELECT)) {
413                                                                 base->flag |= BASE_SELECTED;
414                                                         }
415                                                 }
416                                         }
417
418                                         if (BLI_findlink(&scene->view_layers, scene->r.actlay)) {
419                                                 scene->active_view_layer = scene->r.actlay;
420                                         }
421                                 }
422                                 else {
423                                         for (SceneRenderLayer *srl = scene->r.layers.first; srl; srl = srl->next) {
424                                                 if (srl->prop) {
425                                                         IDP_FreeProperty(srl->prop);
426                                                         MEM_freeN(srl->prop);
427                                                 }
428                                                 BKE_freestyle_config_free(&srl->freestyleConfig, true);
429                                         }
430                                 }
431                                 BLI_freelistN(&scene->r.layers);
432
433                                 ViewLayer *view_layer = BKE_view_layer_add(scene, "Viewport");
434                                 /* If we ported all the original render layers, we don't need to make the viewport layer renderable. */
435                                 if (!BLI_listbase_is_single(&scene->view_layers)) {
436                                         view_layer->flag &= ~VIEW_LAYER_RENDER;
437                                 }
438
439                                 /* If layer was not set, disable it. */
440                                 LayerCollection *layer_collection_parent;
441                                 layer_collection_parent =
442                                         ((LayerCollection *)view_layer->layer_collections.first)->layer_collections.first;
443
444                                 for (int layer = 0; layer < 20; layer++) {
445                                         if (collections[DO_VERSION_COLLECTION_VISIBLE].created & (1 << layer)) {
446                                                 const bool is_disabled = (scene->lay & (1 << layer)) == 0;
447
448                                                 /* We only need to disable the parent collection. */
449                                                 if (is_disabled) {
450                                                         layer_collection_parent->flag |= COLLECTION_DISABLED;
451                                                 }
452
453                                                 LayerCollection *layer_collection_child;
454                                                 layer_collection_child = layer_collection_parent->layer_collections.first;
455
456                                                 for (int j = 1; j < 4; j++) {
457                                                         if (collections[j].created & (1 << layer)) {
458                                                                 layer_collection_child->flag = COLLECTION_VIEWPORT |
459                                                                                                COLLECTION_RENDER |
460                                                                                                collections[j].flag_viewport;
461                                                                 layer_collection_child = layer_collection_child->next;
462                                                         }
463                                                 }
464                                                 BLI_assert(layer_collection_child == NULL);
465                                                 layer_collection_parent = layer_collection_parent->next;
466                                         }
467                                 }
468                                 BLI_assert(layer_collection_parent == NULL);
469
470                                 /* convert active base */
471                                 if (scene->basact) {
472                                         view_layer->basact = BKE_view_layer_base_find(view_layer, scene->basact->object);
473                                 }
474
475                                 /* convert selected bases */
476                                 for (Base *base = scene->base.first; base; base = base->next) {
477                                         if ((base->flag & BASE_SELECTABLED) && (base->object->flag & SELECT)) {
478                                                 base->flag |= BASE_SELECTED;
479                                         }
480
481                                         /* keep lay around for forward compatibility (open those files in 2.79) */
482                                         base->lay = base->object->lay;
483                                 }
484
485                                 /* remove bases once and for all */
486                                 for (Base *base = scene->base.first; base; base = base->next) {
487                                         id_us_min(&base->object->id);
488                                 }
489                                 BLI_freelistN(&scene->base);
490                                 scene->basact = NULL;
491                         }
492                 }
493         }
494
495         if (!MAIN_VERSION_ATLEAST(main, 280, 0)) {
496                 for (bScreen *screen = main->screen.first; screen; screen = screen->id.next) {
497                         /* same render-layer as do_version_workspaces_after_lib_link will activate,
498                          * so same layer as BKE_view_layer_from_workspace_get would return */
499                         ViewLayer *layer = screen->scene->view_layers.first;
500
501                         for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
502                                 for (SpaceLink *view_layer = sa->spacedata.first; view_layer; view_layer = view_layer->next) {
503                                         if (view_layer->spacetype == SPACE_OUTLINER) {
504                                                 SpaceOops *soutliner = (SpaceOops *)view_layer;
505
506                                                 soutliner->outlinevis = SO_VIEW_LAYER;
507
508                                                 if (BLI_listbase_count_ex(&layer->layer_collections, 2) == 1) {
509                                                         if (soutliner->treestore == NULL) {
510                                                                 soutliner->treestore = BLI_mempool_create(
511                                                                         sizeof(TreeStoreElem), 1, 512, BLI_MEMPOOL_ALLOW_ITER);
512                                                         }
513
514                                                         /* Create a tree store element for the collection. This is normally
515                                                          * done in check_persistent (outliner_tree.c), but we need to access
516                                                          * it here :/ (expand element if it's the only one) */
517                                                         TreeStoreElem *tselem = BLI_mempool_calloc(soutliner->treestore);
518                                                         tselem->type = TSE_LAYER_COLLECTION;
519                                                         tselem->id = layer->layer_collections.first;
520                                                         tselem->nr = tselem->used = 0;
521                                                         tselem->flag &= ~TSE_CLOSED;
522                                                 }
523                                         }
524                                 }
525                         }
526                 }
527         }
528
529         /* New workspace design */
530         if (!MAIN_VERSION_ATLEAST(main, 280, 1)) {
531                 do_version_workspaces_after_lib_link(main);
532         }
533
534         if (!MAIN_VERSION_ATLEAST(main, 280, 2)) {
535                 /* Cleanup any remaining SceneRenderLayer data for files that were created
536                  * with Blender 2.8 before the SceneRenderLayer > RenderLayer refactor. */
537                 for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
538                         for (SceneRenderLayer *srl = scene->r.layers.first; srl; srl = srl->next) {
539                                 if (srl->prop) {
540                                         IDP_FreeProperty(srl->prop);
541                                         MEM_freeN(srl->prop);
542                                 }
543                                 BKE_freestyle_config_free(&srl->freestyleConfig, true);
544                         }
545                         BLI_freelistN(&scene->r.layers);
546                 }
547         }
548
549         {
550                 for (WorkSpace *workspace = main->workspaces.first; workspace; workspace = workspace->id.next) {
551                         if (workspace->view_layer) {
552                                 /* During 2.8 work we temporarly stored view-layer in the
553                                  * workspace directly, but should be stored there per-scene. */
554                                 for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
555                                         if (BLI_findindex(&scene->view_layers, workspace->view_layer) != -1) {
556                                                 BKE_workspace_view_layer_set(workspace, workspace->view_layer, scene);
557                                                 workspace->view_layer = NULL;
558                                         }
559                                 }
560                         }
561                         /* While this should apply to most cases, it fails when reading workspaces.blend
562                          * to get its list of workspaces without actually appending any of them. */
563 //                      BLI_assert(workspace->view_layer == NULL);
564                 }
565         }
566
567         {
568                 /* Since we don't have access to FileData we check the (always valid) master collection of the group. */
569                 for (Group *group = main->group.first; group; group = group->id.next) {
570                         if (group->collection == NULL) {
571                                 BKE_group_init(group);
572                                 SceneCollection *sc = GROUP_MASTER_COLLECTION(group);
573                                 SceneCollection *sc_hidden = NULL;
574
575                                 for (GroupObject *go = group->gobject.first; go; go = go->next) {
576                                         if (go->ob->lay & group->layer) {
577                                                 BKE_collection_object_add(&group->id, sc, go->ob);
578                                         }
579                                         else {
580                                                 if (sc_hidden == NULL) {
581                                                         sc_hidden = BKE_collection_add(&group->id, sc, COLLECTION_TYPE_GROUP_INTERNAL, "Hidden");
582                                                 }
583                                                 BKE_collection_object_add(&group->id, sc_hidden, go->ob);
584                                         }
585                                 }
586
587                                 if (sc_hidden != NULL) {
588                                         LayerCollection *layer_collection_master, *layer_collection_hidden;
589                                         layer_collection_master = group->view_layer->layer_collections.first;
590                                         layer_collection_hidden = layer_collection_master->layer_collections.first;
591                                         layer_collection_hidden->flag |= COLLECTION_DISABLED;
592                                 }
593                         }
594
595                         GroupObject *go;
596                         while ((go = BLI_pophead(&group->gobject))) {
597                                 MEM_freeN(go);
598                         }
599                 }
600         }
601
602         {
603                 for (Object *object = main->object.first; object; object = object->id.next) {
604 #ifndef VERSION_280_SUBVERSION_4
605                         /* If any object already has an initialized value for
606                          * duplicator_visibility_flag it means we've already doversioned it.
607                          * TODO(all) remove the VERSION_280_SUBVERSION_4 code once the subversion was bumped. */
608                         if (object->duplicator_visibility_flag != 0) {
609                                 break;
610                         }
611 #endif
612                         if (object->particlesystem.first) {
613                                 object->duplicator_visibility_flag = OB_DUPLI_FLAG_VIEWPORT;
614                                 for (ParticleSystem *psys = object->particlesystem.first; psys; psys=psys->next) {
615                                         if (psys->part->draw & PART_DRAW_EMITTER) {
616                                                 object->duplicator_visibility_flag |= OB_DUPLI_FLAG_RENDER;
617 #ifndef VERSION_280_SUBVERSION_4
618                                                 psys->part->draw &= ~PART_DRAW_EMITTER;
619 #else
620                                                 break;
621 #endif
622                                         }
623                                 }
624                         }
625                         else if (object->transflag & OB_DUPLI){
626                                 object->duplicator_visibility_flag = OB_DUPLI_FLAG_VIEWPORT;
627                         }
628                         else {
629                                 object->duplicator_visibility_flag = OB_DUPLI_FLAG_VIEWPORT | OB_DUPLI_FLAG_RENDER;
630                         }
631                 }
632         }
633 }
634
635 static void do_version_layer_collections_idproperties(ListBase *lb)
636 {
637         IDPropertyTemplate val = {0};
638         for (LayerCollection *lc = lb->first; lc; lc = lc->next) {
639                 lc->properties = IDP_New(IDP_GROUP, &val, ROOT_PROP);
640                 BKE_layer_collection_engine_settings_create(lc->properties);
641
642                 /* No overrides at first */
643                 for (IDProperty *prop = lc->properties->data.group.first; prop; prop = prop->next) {
644                         while (prop->data.group.first) {
645                                 IDP_FreeFromGroup(prop, prop->data.group.first);
646                         }
647                 }
648
649                 /* Do it recursively */
650                 do_version_layer_collections_idproperties(&lc->layer_collections);
651         }
652 }
653
654 static void do_version_view_layer_visibility(ViewLayer *view_layer)
655 {
656         LayerCollection *layer_collection;
657         for (layer_collection = view_layer->layer_collections.first;
658              layer_collection;
659              layer_collection = layer_collection->next)
660         {
661                 if (layer_collection->flag & COLLECTION_DISABLED) {
662                         BKE_collection_enable(view_layer, layer_collection);
663                         layer_collection->flag &= ~COLLECTION_DISABLED;
664                 }
665
666                 if ((layer_collection->flag & (1 << 0)) == 0) { /* !COLLECTION_VISIBLE */
667                         layer_collection->flag |= COLLECTION_DISABLED;
668                 }
669                 layer_collection->flag |= COLLECTION_VIEWPORT | COLLECTION_RENDER;
670         }
671 }
672
673 void blo_do_versions_280(FileData *fd, Library *UNUSED(lib), Main *main)
674 {
675
676         if (!MAIN_VERSION_ATLEAST(main, 280, 0)) {
677                 for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
678                         if (STREQ(scene->r.engine, RE_engine_id_BLENDER_RENDER)) {
679 #ifdef WITH_CLAY_ENGINE
680                                 BLI_strncpy(scene->r.engine, RE_engine_id_BLENDER_CLAY, sizeof(scene->r.engine));
681 #else
682                                 BLI_strncpy(scene->r.engine, RE_engine_id_BLENDER_EEVEE, sizeof(scene->r.engine));
683 #endif
684                         }
685                 }
686
687                 if (!DNA_struct_elem_find(fd->filesdna, "Scene", "ListBase", "view_layers")) {
688                         for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
689                                 /* Master Collection */
690                                 scene->collection = MEM_callocN(sizeof(SceneCollection), "Master Collection");
691                                 BLI_strncpy(scene->collection->name, "Master Collection", sizeof(scene->collection->name));
692                         }
693                 }
694
695                 if (DNA_struct_elem_find(fd->filesdna, "LayerCollection", "ListBase", "engine_settings") &&
696                     !DNA_struct_elem_find(fd->filesdna, "LayerCollection", "IDProperty", "properties"))
697                 {
698                         for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
699                                 for (ViewLayer *view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
700                                         do_version_layer_collections_idproperties(&view_layer->layer_collections);
701                                 }
702                         }
703                 }
704         }
705
706         if (!MAIN_VERSION_ATLEAST(main, 280, 1)) {
707                 if (!DNA_struct_elem_find(fd->filesdna, "Lamp", "float", "bleedexp")) {
708                         for (Lamp *la = main->lamp.first; la; la = la->id.next) {
709                                 la->bleedexp = 2.5f;
710                         }
711                 }
712
713                 if (!DNA_struct_elem_find(fd->filesdna, "GPUDOFSettings", "float", "ratio")) {
714                         for (Camera *ca = main->camera.first; ca; ca = ca->id.next) {
715                                 ca->gpu_dof.ratio = 1.0f;
716                         }
717                 }
718
719                 if (!DNA_struct_elem_find(fd->filesdna, "ViewLayer", "IDProperty", "*properties")) {
720                         for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
721                                 for (ViewLayer *view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
722                                         IDPropertyTemplate val = {0};
723                                         view_layer->properties = IDP_New(IDP_GROUP, &val, ROOT_PROP);
724                                         BKE_view_layer_engine_settings_create(view_layer->properties);
725                                 }
726                         }
727                 }
728
729                 /* MTexPoly now removed. */
730                 if (DNA_struct_find(fd->filesdna, "MTexPoly")) {
731                         const int cd_mtexpoly = 15;  /* CD_MTEXPOLY, deprecated */
732                         for (Mesh *me = main->mesh.first; me; me = me->id.next) {
733                                 /* If we have UV's, so this file will have MTexPoly layers too! */
734                                 if (me->mloopuv != NULL) {
735                                         CustomData_update_typemap(&me->pdata);
736                                         CustomData_free_layers(&me->pdata, cd_mtexpoly, me->totpoly);
737                                         BKE_mesh_update_customdata_pointers(me, false);
738                                 }
739                         }
740                 }
741         }
742
743         if (!MAIN_VERSION_ATLEAST(main, 280, 2)) {
744                 if (!DNA_struct_elem_find(fd->filesdna, "View3D", "short", "custom_orientation_index")) {
745                         for (bScreen *screen = main->screen.first; screen; screen = screen->id.next) {
746                                 for (ScrArea *area = screen->areabase.first; area; area = area->next) {
747                                         for (SpaceLink *sl = area->spacedata.first; sl; sl = sl->next) {
748                                                 if (sl->spacetype == SPACE_VIEW3D) {
749                                                         View3D *v3d = (View3D *)sl;
750                                                         if (v3d->twmode >= V3D_MANIP_CUSTOM) {
751                                                                 v3d->custom_orientation_index = v3d->twmode - V3D_MANIP_CUSTOM;
752                                                                 v3d->twmode = V3D_MANIP_CUSTOM;
753                                                         }
754                                                         else {
755                                                                 v3d->custom_orientation_index = -1;
756                                                         }
757                                                 }
758                                         }
759                                 }
760                         }
761                 }
762
763                 if (!DNA_struct_elem_find(fd->filesdna, "Lamp", "float", "cascade_max_dist")) {
764                         for (Lamp *la = main->lamp.first; la; la = la->id.next) {
765                                 la->cascade_max_dist = 1000.0f;
766                                 la->cascade_count = 4;
767                                 la->cascade_exponent = 0.8f;
768                                 la->cascade_fade = 0.1f;
769                         }
770                 }
771
772                 if (!DNA_struct_elem_find(fd->filesdna, "Lamp", "float", "contact_dist")) {
773                         for (Lamp *la = main->lamp.first; la; la = la->id.next) {
774                                 la->contact_dist = 1.0f;
775                                 la->contact_bias = 0.03f;
776                                 la->contact_spread = 0.2f;
777                                 la->contact_thickness = 0.5f;
778                         }
779                 }
780
781                 if (!DNA_struct_elem_find(fd->filesdna, "LightProbe", "float", "vis_bias")) {
782                         for (LightProbe *probe = main->lightprobe.first; probe; probe = probe->id.next) {
783                                 probe->vis_bias = 1.0f;
784                                 probe->vis_blur = 0.2f;
785                         }
786                 }
787
788                 typedef enum eNTreeDoVersionErrors {
789                         NTREE_DOVERSION_NO_ERROR = 0,
790                         NTREE_DOVERSION_NEED_OUTPUT = (1 << 0),
791                         NTREE_DOVERSION_TRANSPARENCY_EMISSION = (1 << 1),
792                 } eNTreeDoVersionErrors;
793
794                 /* Eevee shader nodes renamed because of the output node system.
795                  * Note that a new output node is not being added here, because it would be overkill
796                  * to handle this case in lib_verify_nodetree.
797                  *
798                  * Also, metallic node is now unified into the principled node. */
799                 eNTreeDoVersionErrors error = NTREE_DOVERSION_NO_ERROR;
800
801                 FOREACH_NODETREE(main, ntree, id) {
802                         if (ntree->type == NTREE_SHADER) {
803                                 for (bNode *node = ntree->nodes.first; node; node = node->next) {
804                                         if (node->type == 194 /* SH_NODE_EEVEE_METALLIC */ &&
805                                             STREQ(node->idname, "ShaderNodeOutputMetallic"))
806                                         {
807                                                 BLI_strncpy(node->idname, "ShaderNodeEeveeMetallic", sizeof(node->idname));
808                                                 error |= NTREE_DOVERSION_NEED_OUTPUT;
809                                         }
810
811                                         else if (node->type == SH_NODE_EEVEE_SPECULAR && STREQ(node->idname, "ShaderNodeOutputSpecular")) {
812                                                 BLI_strncpy(node->idname, "ShaderNodeEeveeSpecular", sizeof(node->idname));
813                                                 error |= NTREE_DOVERSION_NEED_OUTPUT;
814                                         }
815
816                                         else if (node->type == 196 /* SH_NODE_OUTPUT_EEVEE_MATERIAL */ &&
817                                                  STREQ(node->idname, "ShaderNodeOutputEeveeMaterial"))
818                                         {
819                                                 node->type = SH_NODE_OUTPUT_MATERIAL;
820                                                 BLI_strncpy(node->idname, "ShaderNodeOutputMaterial", sizeof(node->idname));
821                                         }
822
823                                         else if (node->type == 194 /* SH_NODE_EEVEE_METALLIC */ &&
824                                                  STREQ(node->idname, "ShaderNodeEeveeMetallic"))
825                                         {
826                                                 node->type = SH_NODE_BSDF_PRINCIPLED;
827                                                 BLI_strncpy(node->idname, "ShaderNodeBsdfPrincipled", sizeof(node->idname));
828                                                 node->custom1 = SHD_GLOSSY_MULTI_GGX;
829                                                 error |= NTREE_DOVERSION_TRANSPARENCY_EMISSION;
830                                         }
831                                 }
832                         }
833                 } FOREACH_NODETREE_END
834
835                 if (error & NTREE_DOVERSION_NEED_OUTPUT) {
836                         BKE_report(fd->reports, RPT_ERROR, "Eevee material conversion problem. Error in console");
837                         printf("You need to connect Principled and Eevee Specular shader nodes to new material output nodes.\n");
838                 }
839
840                 if (error & NTREE_DOVERSION_TRANSPARENCY_EMISSION) {
841                         BKE_report(fd->reports, RPT_ERROR, "Eevee material conversion problem. Error in console");
842                         printf("You need to combine transparency and emission shaders to the converted Principled shader nodes.\n");
843                 }
844
845                 if (!DNA_struct_elem_find(fd->filesdna, "Scene", "ViewRender", "view_render")) {
846                         for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
847                                 BLI_strncpy_utf8(scene->view_render.engine_id, scene->r.engine,
848                                                  sizeof(scene->view_render.engine_id));
849                         }
850
851                         for (WorkSpace *workspace = main->workspaces.first; workspace; workspace = workspace->id.next) {
852                                 BKE_viewrender_init(&workspace->view_render);
853                         }
854                 }
855
856                 if ((DNA_struct_elem_find(fd->filesdna, "ViewLayer", "FreestyleConfig", "freestyle_config") == false) &&
857                     DNA_struct_elem_find(fd->filesdna, "Scene", "ListBase", "view_layers"))
858                 {
859                         for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
860                                 ViewLayer *view_layer;
861                                 for (view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
862                                         view_layer->flag |= VIEW_LAYER_FREESTYLE;
863                                         view_layer->layflag = 0x7FFF;   /* solid ztra halo edge strand */
864                                         view_layer->passflag = SCE_PASS_COMBINED | SCE_PASS_Z;
865                                         view_layer->pass_alpha_threshold = 0.5f;
866                                         BKE_freestyle_config_init(&view_layer->freestyle_config);
867                                 }
868                         }
869                 }
870         }
871
872         if (!MAIN_VERSION_ATLEAST(main, 280, 3)) {
873                 for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
874                         ViewLayer *view_layer;
875                         for (view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
876                                 do_version_view_layer_visibility(view_layer);
877                         }
878                 }
879
880                 for (Group *group = main->group.first; group; group = group->id.next) {
881                         if (group->view_layer != NULL){
882                                 do_version_view_layer_visibility(group->view_layer);
883                         }
884                 }
885         }
886
887         {
888                 if (DNA_struct_elem_find(fd->filesdna, "SpaceOops", "int", "filter") == false) {
889                         bScreen *sc;
890                         ScrArea *sa;
891                         SpaceLink *sl;
892
893                         /* Update files using invalid (outdated) outlinevis Outliner values. */
894                         for (sc = main->screen.first; sc; sc = sc->id.next) {
895                                 for (sa = sc->areabase.first; sa; sa = sa->next) {
896                                         for (sl = sa->spacedata.first; sl; sl = sl->next) {
897                                                 if (sl->spacetype == SPACE_OUTLINER) {
898                                                         SpaceOops *so = (SpaceOops *)sl;
899
900                                                         if (!ELEM(so->outlinevis,
901                                                                           SO_SCENES,
902                                                                           SO_GROUPS,
903                                                                           SO_LIBRARIES,
904                                                                           SO_SEQUENCE,
905                                                                           SO_DATABLOCKS,
906                                                                   SO_ID_ORPHANS,
907                                                                   SO_VIEW_LAYER,
908                                                                   SO_COLLECTIONS))
909                                                         {
910                                                                 so->outlinevis = SO_VIEW_LAYER;
911                                                         }
912                                                 }
913                                         }
914                                 }
915                         }
916                 }
917         }
918 }