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