Fix for regression on bevel material: need do_version support for earlier files.
[blender.git] / source / blender / blenloader / intern / versioning_270.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): Blender Foundation
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  *
22  */
23
24 /** \file blender/blenloader/intern/versioning_270.c
25  *  \ingroup blenloader
26  */
27
28 #include "BLI_utildefines.h"
29 #include "BLI_compiler_attrs.h"
30
31 /* for MinGW32 definition of NULL, could use BLI_blenlib.h instead too */
32 #include <stddef.h>
33
34 /* allow readfile to use deprecated functionality */
35 #define DNA_DEPRECATED_ALLOW
36
37 #include "DNA_brush_types.h"
38 #include "DNA_cloth_types.h"
39 #include "DNA_constraint_types.h"
40 #include "DNA_sdna_types.h"
41 #include "DNA_space_types.h"
42 #include "DNA_screen_types.h"
43 #include "DNA_object_types.h"
44 #include "DNA_mesh_types.h"
45 #include "DNA_modifier_types.h"
46 #include "DNA_particle_types.h"
47 #include "DNA_linestyle_types.h"
48 #include "DNA_actuator_types.h"
49
50 #include "DNA_genfile.h"
51
52 #include "BLI_blenlib.h"
53 #include "BLI_math.h"
54
55 #include "BKE_main.h"
56 #include "BKE_node.h"
57
58 #include "BLI_math.h"
59 #include "BLI_string.h"
60
61 #include "BLO_readfile.h"
62
63 #include "readfile.h"
64
65
66 static void do_version_constraints_radians_degrees_270_1(ListBase *lb)
67 {
68         bConstraint *con;
69
70         for (con = lb->first; con; con = con->next) {
71                 if (con->type == CONSTRAINT_TYPE_TRANSFORM) {
72                         bTransformConstraint *data = (bTransformConstraint *)con->data;
73                         const float deg_to_rad_f = DEG2RADF(1.0f);
74
75                         if (data->from == TRANS_ROTATION) {
76                                 mul_v3_fl(data->from_min, deg_to_rad_f);
77                                 mul_v3_fl(data->from_max, deg_to_rad_f);
78                         }
79
80                         if (data->to == TRANS_ROTATION) {
81                                 mul_v3_fl(data->to_min, deg_to_rad_f);
82                                 mul_v3_fl(data->to_max, deg_to_rad_f);
83                         }
84                 }
85         }
86 }
87
88 static void do_version_constraints_radians_degrees_270_5(ListBase *lb)
89 {
90         bConstraint *con;
91
92         for (con = lb->first; con; con = con->next) {
93                 if (con->type == CONSTRAINT_TYPE_TRANSFORM) {
94                         bTransformConstraint *data = (bTransformConstraint *)con->data;
95
96                         if (data->from == TRANS_ROTATION) {
97                                 copy_v3_v3(data->from_min_rot, data->from_min);
98                                 copy_v3_v3(data->from_max_rot, data->from_max);
99                         }
100                         else if (data->from == TRANS_SCALE) {
101                                 copy_v3_v3(data->from_min_scale, data->from_min);
102                                 copy_v3_v3(data->from_max_scale, data->from_max);
103                         }
104
105                         if (data->to == TRANS_ROTATION) {
106                                 copy_v3_v3(data->to_min_rot, data->to_min);
107                                 copy_v3_v3(data->to_max_rot, data->to_max);
108                         }
109                         else if (data->to == TRANS_SCALE) {
110                                 copy_v3_v3(data->to_min_scale, data->to_min);
111                                 copy_v3_v3(data->to_max_scale, data->to_max);
112                         }
113                 }
114         }
115 }
116
117 void blo_do_versions_270(FileData *fd, Library *UNUSED(lib), Main *main)
118 {
119         if (!MAIN_VERSION_ATLEAST(main, 270, 0)) {
120
121                 if (!DNA_struct_elem_find(fd->filesdna, "BevelModifierData", "float", "profile")) {
122                         Object *ob;
123
124                         for (ob = main->object.first; ob; ob = ob->id.next) {
125                                 ModifierData *md;
126                                 for (md = ob->modifiers.first; md; md = md->next) {
127                                         if (md->type == eModifierType_Bevel) {
128                                                 BevelModifierData *bmd = (BevelModifierData *)md;
129                                                 bmd->profile = 0.5f;
130                                                 bmd->val_flags = MOD_BEVEL_AMT_OFFSET;
131                                         }
132                                 }
133                         }
134                 }
135
136                 /* nodes don't use fixed node->id any more, clean up */
137                 FOREACH_NODETREE(main, ntree, id) {
138                         if (ntree->type == NTREE_COMPOSIT) {
139                                 bNode *node;
140                                 for (node = ntree->nodes.first; node; node = node->next) {
141                                         if (ELEM(node->type, CMP_NODE_COMPOSITE, CMP_NODE_OUTPUT_FILE)) {
142                                                 node->id = NULL;
143                                         }
144                                 }
145                         }
146                 } FOREACH_NODETREE_END
147
148                 {
149                         bScreen *screen;
150
151                         for (screen = main->screen.first; screen; screen = screen->id.next) {
152                                 ScrArea *area;
153                                 for (area = screen->areabase.first; area; area = area->next) {
154                                         SpaceLink *space_link;
155                                         for (space_link = area->spacedata.first; space_link; space_link = space_link->next) {
156                                                 if (space_link->spacetype == SPACE_CLIP) {
157                                                         SpaceClip *space_clip = (SpaceClip *) space_link;
158                                                         if (space_clip->mode != SC_MODE_MASKEDIT) {
159                                                                 space_clip->mode = SC_MODE_TRACKING;
160                                                         }
161                                                 }
162                                         }
163                                 }
164                         }
165                 }
166
167                 if (!DNA_struct_elem_find(fd->filesdna, "MovieTrackingSettings", "float", "default_weight")) {
168                         MovieClip *clip;
169                         for (clip = main->movieclip.first; clip; clip = clip->id.next) {
170                                 clip->tracking.settings.default_weight = 1.0f;
171                         }
172                 }
173         }
174
175         if (!MAIN_VERSION_ATLEAST(main, 270, 1)) {
176                 Scene *sce;
177                 Object *ob;
178
179                 /* Update Transform constraint (another deg -> rad stuff). */
180                 for (ob = main->object.first; ob; ob = ob->id.next) {
181                         do_version_constraints_radians_degrees_270_1(&ob->constraints);
182
183                         if (ob->pose) {
184                                 /* Bones constraints! */
185                                 bPoseChannel *pchan;
186                                 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
187                                         do_version_constraints_radians_degrees_270_1(&pchan->constraints);
188                                 }
189                         }
190                 }
191
192                 for (sce = main->scene.first; sce; sce = sce->id.next) {
193                         if (sce->r.raytrace_structure == R_RAYSTRUCTURE_BLIBVH) {
194                                 sce->r.raytrace_structure = R_RAYSTRUCTURE_AUTO;
195                         }
196                 }
197         }
198
199         if (!MAIN_VERSION_ATLEAST(main, 270, 2)) {
200                 Mesh *me;
201
202                 /* Mesh smoothresh deg->rad. */
203                 for (me = main->mesh.first; me; me = me->id.next) {
204                         me->smoothresh = DEG2RADF(me->smoothresh);
205                 }
206         }
207
208         if (!MAIN_VERSION_ATLEAST(main, 270, 3)) {
209                 FreestyleLineStyle *linestyle;
210
211                 for (linestyle = main->linestyle.first; linestyle; linestyle = linestyle->id.next) {
212                         linestyle->flag |= LS_NO_SORTING;
213                         linestyle->sort_key = LS_SORT_KEY_DISTANCE_FROM_CAMERA;
214                         linestyle->integration_type = LS_INTEGRATION_MEAN;
215                 }
216         }
217
218         if (!MAIN_VERSION_ATLEAST(main, 270, 4)) {
219                 /* ui_previews were not handled correctly when copying areas, leading to corrupted files (see T39847).
220                  * This will always reset situation to a valid state.
221                  */
222                 bScreen *sc;
223
224                 for (sc = main->screen.first; sc; sc = sc->id.next) {
225                         ScrArea *sa;
226                         for (sa = sc->areabase.first; sa; sa = sa->next) {
227                                 SpaceLink *sl;
228
229                                 for (sl = sa->spacedata.first; sl; sl = sl->next) {
230                                         ARegion *ar;
231                                         ListBase *lb = (sl == sa->spacedata.first) ? &sa->regionbase : &sl->regionbase;
232
233                                         for (ar = lb->first; ar; ar = ar->next) {
234                                                 BLI_listbase_clear(&ar->ui_previews);
235                                         }
236                                 }
237                         }
238                 }
239         }
240
241         if (!MAIN_VERSION_ATLEAST(main, 270, 5)) {
242                 Object *ob;
243
244                 /* Update Transform constraint (again :|). */
245                 for (ob = main->object.first; ob; ob = ob->id.next) {
246                         do_version_constraints_radians_degrees_270_5(&ob->constraints);
247
248                         if (ob->pose) {
249                                 /* Bones constraints! */
250                                 bPoseChannel *pchan;
251                                 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
252                                         do_version_constraints_radians_degrees_270_5(&pchan->constraints);
253                                 }
254                         }
255                 }
256         }
257
258         if (!MAIN_VERSION_ATLEAST(main, 271, 0)) {
259                 if (!DNA_struct_elem_find(fd->filesdna, "Material", "int", "mode2")) {
260                         Material *ma;
261
262                         for (ma = main->mat.first; ma; ma = ma->id.next)
263                                 ma->mode2 = MA_CASTSHADOW;
264                 }
265
266                 if (!DNA_struct_elem_find(fd->filesdna, "RenderData", "BakeData", "bake")) {
267                         Scene *sce;
268
269                         for (sce = main->scene.first; sce; sce = sce->id.next) {
270                                 sce->r.bake.flag = R_BAKE_CLEAR;
271                                 sce->r.bake.width = 512;
272                                 sce->r.bake.height = 512;
273                                 sce->r.bake.margin = 16;
274                                 sce->r.bake.normal_space = R_BAKE_SPACE_TANGENT;
275                                 sce->r.bake.normal_swizzle[0] = R_BAKE_POSX;
276                                 sce->r.bake.normal_swizzle[1] = R_BAKE_POSY;
277                                 sce->r.bake.normal_swizzle[2] = R_BAKE_POSZ;
278                                 BLI_strncpy(sce->r.bake.filepath, U.renderdir, sizeof(sce->r.bake.filepath));
279
280                                 sce->r.bake.im_format.planes = R_IMF_PLANES_RGBA;
281                                 sce->r.bake.im_format.imtype = R_IMF_IMTYPE_PNG;
282                                 sce->r.bake.im_format.depth = R_IMF_CHAN_DEPTH_8;
283                                 sce->r.bake.im_format.quality = 90;
284                                 sce->r.bake.im_format.compress = 15;
285                         }
286                 }
287
288                 if (!DNA_struct_elem_find(fd->filesdna, "FreestyleLineStyle", "float", "texstep")) {
289                         FreestyleLineStyle *linestyle;
290
291                         for (linestyle = main->linestyle.first; linestyle; linestyle = linestyle->id.next) {
292                                 linestyle->flag |= LS_TEXTURE;
293                                 linestyle->texstep = 1.0;
294                         }
295                 }
296
297                 {
298                         Scene *scene;
299                         for (scene = main->scene.first; scene; scene = scene->id.next) {
300                                 int num_layers = BLI_countlist(&scene->r.layers);
301                                 scene->r.actlay = min_ff(scene->r.actlay, num_layers - 1);
302                         }
303                 }
304         }
305
306         if (!MAIN_VERSION_ATLEAST(main, 271, 1)) {
307                 if (!DNA_struct_elem_find(fd->filesdna, "Material", "float", "line_col[4]")) {
308                         Material *mat;
309
310                         for (mat = main->mat.first; mat; mat = mat->id.next) {
311                                 mat->line_col[0] = mat->line_col[1] = mat->line_col[2] = 0.0f;
312                                 mat->line_col[3] = mat->alpha;
313                         }
314                 }
315
316                 if (!DNA_struct_elem_find(fd->filesdna, "RenderData", "int", "preview_start_resolution")) {
317                         Scene *scene;
318                         for (scene = main->scene.first; scene; scene = scene->id.next) {
319                                 scene->r.preview_start_resolution = 64;
320                         }
321                 }
322         }
323
324         if (!MAIN_VERSION_ATLEAST(main, 271, 2)) {
325                 /* init up & track axis property of trackto actuators */
326                 Object *ob;
327
328                 for (ob = main->object.first; ob; ob = ob->id.next) {
329                         bActuator *act;
330                         for (act = ob->actuators.first; act; act = act->next) {
331                                 if (act->type == ACT_EDIT_OBJECT) {
332                                         bEditObjectActuator *eoact = act->data;
333                                         eoact->trackflag = ob->trackflag;
334                                         /* if trackflag is pointing +-Z axis then upflag should point Y axis.
335                                          * Rest of trackflag cases, upflag should be point z axis */
336                                         if ((ob->trackflag == OB_POSZ) || (ob->trackflag == OB_NEGZ)) {
337                                                 eoact->upflag = 1;
338                                         }
339                                         else {
340                                                 eoact->upflag = 2;
341                                         }
342                                 }
343                         }
344                 }
345         }
346
347         if (!MAIN_VERSION_ATLEAST(main, 271, 3)) {
348                 Brush *br;
349
350                 for (br = main->brush.first; br; br = br->id.next) {
351                         br->fill_threshold = 0.2f;
352                 }
353
354                 if (!DNA_struct_elem_find(fd->filesdna, "BevelModifierData", "int", "mat")) {
355                         Object *ob;
356                         for (ob = main->object.first; ob; ob = ob->id.next) {
357                                 ModifierData *md;
358
359                                 for (md = ob->modifiers.first; md; md = md->next) {
360                                         if (md->type == eModifierType_Bevel) {
361                                                 BevelModifierData *bmd = (BevelModifierData *)md;
362                                                 bmd->mat = -1;
363                                         }
364                                 }
365                         }
366                 }
367         }
368
369         if (!MAIN_VERSION_ATLEAST(main, 271, 6)) {
370                 Object *ob;
371                 for (ob = main->object.first; ob; ob = ob->id.next) {
372                         ModifierData *md;
373
374                         for (md = ob->modifiers.first; md; md = md->next) {
375                                 if (md->type == eModifierType_ParticleSystem) {
376                                         ParticleSystemModifierData *pmd = (ParticleSystemModifierData *)md;
377                                         if (pmd->psys && pmd->psys->clmd) {
378                                                 pmd->psys->clmd->sim_parms->vel_damping = 1.0f;
379                                         }
380                                 }
381                         }
382                 }
383         }
384
385         if (!MAIN_VERSION_ATLEAST(main, 272, 0)) {
386                 if (!DNA_struct_elem_find(fd->filesdna, "RenderData", "int", "preview_start_resolution")) {
387                         Scene *scene;
388                         for (scene = main->scene.first; scene; scene = scene->id.next) {
389                                 scene->r.preview_start_resolution = 64;
390                         }
391                 }
392         }
393 }