Merge branch 'master' into blender2.8
[blender.git] / source / blender / blenloader / intern / versioning_280.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Dalai Felinto
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  *
22  */
23
24 /** \file blender/blenloader/intern/versioning_280.c
25  *  \ingroup blenloader
26  */
27
28 /* allow readfile to use deprecated functionality */
29 #define DNA_DEPRECATED_ALLOW
30
31 #include <string.h>
32
33 #include "DNA_object_types.h"
34 #include "DNA_camera_types.h"
35 #include "DNA_gpu_types.h"
36 #include "DNA_lamp_types.h"
37 #include "DNA_layer_types.h"
38 #include "DNA_material_types.h"
39 #include "DNA_mesh_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_screen_types.h"
42 #include "DNA_view3d_types.h"
43 #include "DNA_genfile.h"
44 #include "DNA_workspace_types.h"
45
46 #include "BKE_collection.h"
47 #include "BKE_customdata.h"
48 #include "BKE_idprop.h"
49 #include "BKE_layer.h"
50 #include "BKE_main.h"
51 #include "BKE_mesh.h"
52 #include "BKE_node.h"
53 #include "BKE_report.h"
54 #include "BKE_scene.h"
55 #include "BKE_workspace.h"
56
57 #include "BLI_listbase.h"
58 #include "BLI_mempool.h"
59 #include "BLI_string.h"
60 #include "BLI_utildefines.h"
61
62 #include "BLO_readfile.h"
63 #include "readfile.h"
64
65 #include "MEM_guardedalloc.h"
66
67
68 static bScreen *screen_parent_find(const bScreen *screen)
69 {
70         /* can avoid lookup if screen state isn't maximized/full (parent and child store the same state) */
71         if (ELEM(screen->state, SCREENMAXIMIZED, SCREENFULL)) {
72                 for (const ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
73                         if (sa->full && sa->full != screen) {
74                                 BLI_assert(sa->full->state == screen->state);
75                                 return sa->full;
76                         }
77                 }
78         }
79
80         return NULL;
81 }
82
83 static void do_version_workspaces_create_from_screens(Main *bmain)
84 {
85         for (bScreen *screen = bmain->screen.first; screen; screen = screen->id.next) {
86                 const bScreen *screen_parent = screen_parent_find(screen);
87                 WorkSpace *workspace;
88                 SceneLayer *layer = BKE_scene_layer_from_scene_get(screen->scene);
89                 ListBase *transform_orientations;
90
91                 if (screen_parent) {
92                         /* fullscreen with "Back to Previous" option, don't create
93                          * a new workspace, add layout workspace containing parent */
94                         workspace = BLI_findstring(
95                                 &bmain->workspaces, screen_parent->id.name + 2, offsetof(ID, name) + 2);
96                 }
97                 else {
98                         workspace = BKE_workspace_add(bmain, screen->id.name + 2);
99                 }
100                 BKE_workspace_layout_add(workspace, screen, screen->id.name + 2);
101                 BKE_workspace_render_layer_set(workspace, layer);
102
103                 transform_orientations = BKE_workspace_transform_orientations_get(workspace);
104                 BLI_duplicatelist(transform_orientations, &screen->scene->transform_spaces);
105         }
106 }
107
108 /**
109  * \brief After lib-link versioning for new workspace design.
110  *
111  *  *  Adds a workspace for (almost) each screen of the old file
112  *     and adds the needed workspace-layout to wrap the screen.
113  *  *  Active screen isn't stored directly in window anymore, but in the active workspace.
114  *  *  Active scene isn't stored in screen anymore, but in window.
115  *  *  Create workspace instance hook for each window.
116  *
117  * \note Some of the created workspaces might be deleted again in case of reading the default startup.blend.
118  */
119 static void do_version_workspaces_after_lib_link(Main *bmain)
120 {
121         BLI_assert(BLI_listbase_is_empty(&bmain->workspaces));
122
123         do_version_workspaces_create_from_screens(bmain);
124
125         for (wmWindowManager *wm = bmain->wm.first; wm; wm = wm->id.next) {
126                 for (wmWindow *win = wm->windows.first; win; win = win->next) {
127                         bScreen *screen_parent = screen_parent_find(win->screen);
128                         bScreen *screen = screen_parent ? screen_parent : win->screen;
129                         WorkSpace *workspace = BLI_findstring(&bmain->workspaces, screen->id.name + 2, offsetof(ID, name) + 2);
130                         ListBase *layouts = BKE_workspace_layouts_get(workspace);
131
132                         win->workspace_hook = BKE_workspace_instance_hook_create(bmain);
133
134                         BKE_workspace_active_set(win->workspace_hook, workspace);
135                         BKE_workspace_active_layout_set(win->workspace_hook, layouts->first);
136
137                         win->scene = screen->scene;
138                         /* Deprecated from now on! */
139                         win->screen = NULL;
140                 }
141         }
142
143         for (bScreen *screen = bmain->screen.first; screen; screen = screen->id.next) {
144                 /* Deprecated from now on! */
145                 BLI_freelistN(&screen->scene->transform_spaces);
146                 screen->scene = NULL;
147         }
148 }
149
150 void do_versions_after_linking_280(Main *main)
151 {
152         if (!MAIN_VERSION_ATLEAST(main, 280, 0)) {
153                 for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
154                         /* since we don't have access to FileData we check the (always valid) first render layer instead */
155                         if (scene->render_layers.first == NULL) {
156                                 SceneCollection *sc_master = BKE_collection_master(scene);
157                                 BLI_strncpy(sc_master->name, "Master Collection", sizeof(sc_master->name));
158
159                                 SceneCollection *collections[20] = {NULL};
160                                 bool is_visible[20];
161
162                                 int lay_used = 0;
163                                 for (int i = 0; i < 20; i++) {
164                                         char name[MAX_NAME];
165
166                                         BLI_snprintf(name, sizeof(collections[i]->name), "Collection %d", i + 1);
167                                         collections[i] = BKE_collection_add(scene, sc_master, name);
168
169                                         is_visible[i] = (scene->lay & (1 << i));
170                                 }
171
172                                 for (Base *base = scene->base.first; base; base = base->next) {
173                                         lay_used |= base->lay & ((1 << 20) - 1); /* ignore localview */
174
175                                         for (int i = 0; i < 20; i++) {
176                                                 if ((base->lay & (1 << i)) != 0) {
177                                                         BKE_collection_object_add(scene, collections[i], base->object);
178                                                 }
179                                         }
180
181                                         if (base->flag & SELECT) {
182                                                 base->object->flag |= SELECT;
183                                         }
184                                         else {
185                                                 base->object->flag &= ~SELECT;
186                                         }
187                                 }
188
189                                 scene->active_layer = 0;
190
191                                 if (!BKE_scene_uses_blender_game(scene)) {
192                                         for (SceneRenderLayer *srl = scene->r.layers.first; srl; srl = srl->next) {
193
194                                                 SceneLayer *sl = BKE_scene_layer_add(scene, srl->name);
195                                                 BKE_scene_layer_engine_set(sl, scene->r.engine);
196
197                                                 if (srl->mat_override) {
198                                                         BKE_collection_override_datablock_add((LayerCollection *)sl->layer_collections.first, "material", (ID *)srl->mat_override);
199                                                 }
200
201                                                 if (srl->light_override && BKE_scene_uses_blender_internal(scene)) {
202                                                         /* not sure how we handle this, pending until we design the override system */
203                                                         TODO_LAYER_OVERRIDE;
204                                                 }
205
206                                                 if (srl->lay != scene->lay) {
207                                                         /* unlink master collection  */
208                                                         BKE_collection_unlink(sl, sl->layer_collections.first);
209
210                                                         /* add new collection bases */
211                                                         for (int i = 0; i < 20; i++) {
212                                                                 if ((srl->lay & (1 << i)) != 0) {
213                                                                         BKE_collection_link(sl, collections[i]);
214                                                                 }
215                                                         }
216                                                 }
217
218                                                 /* for convenience set the same active object in all the layers */
219                                                 if (scene->basact) {
220                                                         sl->basact = BKE_scene_layer_base_find(sl, scene->basact->object);
221                                                 }
222
223                                                 for (Base *base = sl->object_bases.first; base; base = base->next) {
224                                                         if ((base->flag & BASE_SELECTABLED) && (base->object->flag & SELECT)) {
225                                                                 base->flag |= BASE_SELECTED;
226                                                         }
227                                                 }
228
229                                                 /* TODO: passes, samples, mask_layesr, exclude, ... */
230                                         }
231
232                                         if (BLI_findlink(&scene->render_layers, scene->r.actlay)) {
233                                                 scene->active_layer = scene->r.actlay;
234                                         }
235                                 }
236
237                                 SceneLayer *sl = BKE_scene_layer_add(scene, "Viewport");
238
239                                 /* In this particular case we can safely assume the data struct */
240                                 LayerCollection *lc = ((LayerCollection *)sl->layer_collections.first)->layer_collections.first;
241                                 for (int i = 0; i < 20; i++) {
242                                         if (!is_visible[i]) {
243                                                 lc->flag &= ~COLLECTION_VISIBLE;
244                                         }
245                                         lc = lc->next;
246                                 }
247
248                                 /* convert active base */
249                                 if (scene->basact) {
250                                         sl->basact = BKE_scene_layer_base_find(sl, scene->basact->object);
251                                 }
252
253                                 /* convert selected bases */
254                                 for (Base *base = scene->base.first; base; base = base->next) {
255                                         if ((base->flag & BASE_SELECTABLED) && (base->object->flag & SELECT)) {
256                                                 base->flag |= BASE_SELECTED;
257                                         }
258
259                                         /* keep lay around for forward compatibility (open those files in 2.79) */
260                                         base->lay = base->object->lay;
261                                 }
262
263                                 /* TODO: copy scene render data to layer */
264
265                                 /* Cleanup */
266                                 for (int i = 0; i < 20; i++) {
267                                         if ((lay_used & (1 << i)) == 0) {
268                                                 BKE_collection_remove(scene, collections[i]);
269                                         }
270                                 }
271
272                                 /* Fallback name if only one layer was found in the original file */
273                                 if (BLI_listbase_count_ex(&sc_master->scene_collections, 2) == 1) {
274                                         BKE_collection_rename(scene, sc_master->scene_collections.first, "Default Collection");
275                                 }
276
277                                 /* remove bases once and for all */
278                                 for (Base *base = scene->base.first; base; base = base->next) {
279                                         id_us_min(&base->object->id);
280                                 }
281                                 BLI_freelistN(&scene->base);
282                                 scene->basact = NULL;
283                         }
284                 }
285         }
286
287         if (!MAIN_VERSION_ATLEAST(main, 280, 0)) {
288                 for (bScreen *screen = main->screen.first; screen; screen = screen->id.next) {
289                         /* same render-layer as do_version_workspaces_after_lib_link will activate,
290                          * so same layer as BKE_scene_layer_from_workspace_get would return */
291                         SceneLayer *layer = screen->scene->render_layers.first;
292
293                         for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
294                                 for (SpaceLink *sl = sa->spacedata.first; sl; sl = sl->next) {
295                                         if (sl->spacetype == SPACE_OUTLINER) {
296                                                 SpaceOops *soutliner = (SpaceOops *)sl;
297
298                                                 soutliner->outlinevis = SO_ACT_LAYER;
299
300                                                 if (BLI_listbase_count_ex(&layer->layer_collections, 2) == 1) {
301                                                         if (soutliner->treestore == NULL) {
302                                                                 soutliner->treestore = BLI_mempool_create(
303                                                                         sizeof(TreeStoreElem), 1, 512, BLI_MEMPOOL_ALLOW_ITER);
304                                                         }
305
306                                                         /* Create a tree store element for the collection. This is normally
307                                                          * done in check_persistent (outliner_tree.c), but we need to access
308                                                          * it here :/ (expand element if it's the only one) */
309                                                         TreeStoreElem *tselem = BLI_mempool_calloc(soutliner->treestore);
310                                                         tselem->type = TSE_LAYER_COLLECTION;
311                                                         tselem->id = layer->layer_collections.first;
312                                                         tselem->nr = tselem->used = 0;
313                                                         tselem->flag &= ~TSE_CLOSED;
314                                                 }
315                                         }
316                                 }
317                         }
318                 }
319         }
320
321         /* New workspace design */
322         if (!MAIN_VERSION_ATLEAST(main, 280, 1)) {
323                 do_version_workspaces_after_lib_link(main);
324         }
325 }
326
327 static void do_version_layer_collections_idproperties(ListBase *lb)
328 {
329         IDPropertyTemplate val = {0};
330         for (LayerCollection *lc = lb->first; lc; lc = lc->next) {
331                 lc->properties = IDP_New(IDP_GROUP, &val, ROOT_PROP);
332                 BKE_layer_collection_engine_settings_create(lc->properties);
333
334                 /* No overrides at first */
335                 for (IDProperty *prop = lc->properties->data.group.first; prop; prop = prop->next) {
336                         while (prop->data.group.first) {
337                                 IDP_FreeFromGroup(prop, prop->data.group.first);
338                         }
339                 }
340
341                 /* Do it recursively */
342                 do_version_layer_collections_idproperties(&lc->layer_collections);
343         }
344 }
345
346 void blo_do_versions_280(FileData *fd, Library *UNUSED(lib), Main *main)
347 {
348
349         if (!MAIN_VERSION_ATLEAST(main, 280, 0)) {
350                 for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
351                         if (STREQ(scene->r.engine, RE_engine_id_BLENDER_RENDER)) {
352 #ifdef WITH_CLAY_ENGINE
353                                 BLI_strncpy(scene->r.engine, RE_engine_id_BLENDER_CLAY, sizeof(scene->r.engine));
354 #else
355                                 BLI_strncpy(scene->r.engine, RE_engine_id_BLENDER_EEVEE, sizeof(scene->r.engine));
356 #endif
357                         }
358                 }
359
360                 if (!DNA_struct_elem_find(fd->filesdna, "Scene", "ListBase", "render_layers")) {
361                         for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
362                                 /* Master Collection */
363                                 scene->collection = MEM_callocN(sizeof(SceneCollection), "Master Collection");
364                                 BLI_strncpy(scene->collection->name, "Master Collection", sizeof(scene->collection->name));
365                         }
366                 }
367
368                 if (DNA_struct_elem_find(fd->filesdna, "LayerCollection", "ListBase", "engine_settings") &&
369                     !DNA_struct_elem_find(fd->filesdna, "LayerCollection", "IDProperty", "properties"))
370                 {
371                         for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
372                                 for (SceneLayer *sl = scene->render_layers.first; sl; sl = sl->next) {
373                                         do_version_layer_collections_idproperties(&sl->layer_collections);
374                                 }
375                         }
376                 }
377         }
378
379         if (!MAIN_VERSION_ATLEAST(main, 280, 1)) {
380                 if (!DNA_struct_elem_find(fd->filesdna, "Lamp", "float", "bleedexp"))   {
381                         for (Lamp *la = main->lamp.first; la; la = la->id.next) {
382                                 la->bleedexp = 120.0f;
383                         }
384                 }
385
386                 if (!DNA_struct_elem_find(fd->filesdna, "GPUDOFSettings", "float", "ratio"))    {
387                         for (Camera *ca = main->camera.first; ca; ca = ca->id.next) {
388                                 ca->gpu_dof.ratio = 1.0f;
389                         }
390                 }
391
392                 if (!DNA_struct_elem_find(fd->filesdna, "SceneLayer", "IDProperty", "*properties")) {
393                         for (Scene *scene = main->scene.first; scene; scene = scene->id.next) {
394                                 for (SceneLayer *sl = scene->render_layers.first; sl; sl = sl->next) {
395                                         IDPropertyTemplate val = {0};
396                                         sl->properties = IDP_New(IDP_GROUP, &val, ROOT_PROP);
397                                         BKE_scene_layer_engine_settings_create(sl->properties);
398                                 }
399                         }
400                 }
401
402                 /* MTexPoly now removed. */
403                 if (DNA_struct_find(fd->filesdna, "MTexPoly")) {
404                         const int cd_mtexpoly = 15;  /* CD_MTEXPOLY, deprecated */
405                         for (Mesh *me = main->mesh.first; me; me = me->id.next) {
406                                 /* If we have UV's, so this file will have MTexPoly layers too! */
407                                 if (me->mloopuv != NULL) {
408                                         CustomData_update_typemap(&me->pdata);
409                                         CustomData_free_layers(&me->pdata, cd_mtexpoly, me->totpoly);
410                                         BKE_mesh_update_customdata_pointers(me, false);
411                                 }
412                         }
413                 }
414
415                 if (!DNA_struct_elem_find(fd->filesdna, "View3D", "short", "custom_orientation_index")) {
416                         for (bScreen *screen = main->screen.first; screen; screen = screen->id.next) {
417                                 for (ScrArea *area = screen->areabase.first; area; area = area->next) {
418                                         for (SpaceLink *sl = area->spacedata.first; sl; sl = sl->next) {
419                                                 if (sl->spacetype == SPACE_VIEW3D) {
420                                                         View3D *v3d = (View3D *)sl;
421                                                         if (v3d->twmode >= V3D_MANIP_CUSTOM) {
422                                                                 v3d->custom_orientation_index = v3d->twmode - V3D_MANIP_CUSTOM;
423                                                                 v3d->twmode = V3D_MANIP_CUSTOM;
424                                                         }
425                                                         else {
426                                                                 v3d->custom_orientation_index = -1;
427                                                         }
428                                                 }
429                                         }
430                                 }
431                         }
432                 }
433         }
434
435         {
436                 {
437                         /* Eevee shader nodes renamed because of the output node system.
438                          * Note that a new output node is not being added here, because it would be overkill
439                          * to handle this case in lib_verify_nodetree. */
440                         bool error = false;
441                         FOREACH_NODETREE(main, ntree, id) {
442                                 if (ntree->type == NTREE_SHADER) {
443                                         for (bNode *node = ntree->nodes.first; node; node = node->next) {
444                                                 if (node->type == SH_NODE_EEVEE_METALLIC && STREQ(node->idname, "ShaderNodeOutputMetallic")) {
445                                                         BLI_strncpy(node->idname, "ShaderNodeEeveeMetallic", sizeof(node->idname));
446                                                         error = true;
447                                                 }
448
449                                                 if (node->type == SH_NODE_EEVEE_SPECULAR && STREQ(node->idname, "ShaderNodeOutputSpecular")) {
450                                                         BLI_strncpy(node->idname, "ShaderNodeEeveeSpecular", sizeof(node->idname));
451                                                         error = true;
452                                                 }
453                                         }
454                                 }
455                         } FOREACH_NODETREE_END
456                         if (error) {
457                                 BKE_report(fd->reports, RPT_ERROR, "Eevee material conversion problem. Error in console");
458                                 printf("You need to connect Eevee Metallic and Specular shader nodes to new material output nodes.\n");
459                         }
460                 }
461         }
462 }