Add Custom Loop Normals.
[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 "BKE_main.h"
53 #include "BKE_node.h"
54
55 #include "BLI_math.h"
56 #include "BLI_listbase.h"
57 #include "BLI_string.h"
58
59 #include "BLO_readfile.h"
60
61 #include "readfile.h"
62
63
64 static void do_version_constraints_radians_degrees_270_1(ListBase *lb)
65 {
66         bConstraint *con;
67
68         for (con = lb->first; con; con = con->next) {
69                 if (con->type == CONSTRAINT_TYPE_TRANSFORM) {
70                         bTransformConstraint *data = (bTransformConstraint *)con->data;
71                         const float deg_to_rad_f = DEG2RADF(1.0f);
72
73                         if (data->from == TRANS_ROTATION) {
74                                 mul_v3_fl(data->from_min, deg_to_rad_f);
75                                 mul_v3_fl(data->from_max, deg_to_rad_f);
76                         }
77
78                         if (data->to == TRANS_ROTATION) {
79                                 mul_v3_fl(data->to_min, deg_to_rad_f);
80                                 mul_v3_fl(data->to_max, deg_to_rad_f);
81                         }
82                 }
83         }
84 }
85
86 static void do_version_constraints_radians_degrees_270_5(ListBase *lb)
87 {
88         bConstraint *con;
89
90         for (con = lb->first; con; con = con->next) {
91                 if (con->type == CONSTRAINT_TYPE_TRANSFORM) {
92                         bTransformConstraint *data = (bTransformConstraint *)con->data;
93
94                         if (data->from == TRANS_ROTATION) {
95                                 copy_v3_v3(data->from_min_rot, data->from_min);
96                                 copy_v3_v3(data->from_max_rot, data->from_max);
97                         }
98                         else if (data->from == TRANS_SCALE) {
99                                 copy_v3_v3(data->from_min_scale, data->from_min);
100                                 copy_v3_v3(data->from_max_scale, data->from_max);
101                         }
102
103                         if (data->to == TRANS_ROTATION) {
104                                 copy_v3_v3(data->to_min_rot, data->to_min);
105                                 copy_v3_v3(data->to_max_rot, data->to_max);
106                         }
107                         else if (data->to == TRANS_SCALE) {
108                                 copy_v3_v3(data->to_min_scale, data->to_min);
109                                 copy_v3_v3(data->to_max_scale, data->to_max);
110                         }
111                 }
112         }
113 }
114
115 static void do_version_constraints_stretch_to_limits(ListBase *lb)
116 {
117         bConstraint *con;
118
119         for (con = lb->first; con; con = con->next) {
120                 if (con->type == CONSTRAINT_TYPE_STRETCHTO) {
121                         bStretchToConstraint *data = (bStretchToConstraint *)con->data;
122                         data->bulge_min = 1.0f;
123                         data->bulge_max = 1.0f;
124                 }
125         }
126 }
127
128 void blo_do_versions_270(FileData *fd, Library *UNUSED(lib), Main *main)
129 {
130         if (!MAIN_VERSION_ATLEAST(main, 270, 0)) {
131
132                 if (!DNA_struct_elem_find(fd->filesdna, "BevelModifierData", "float", "profile")) {
133                         Object *ob;
134
135                         for (ob = main->object.first; ob; ob = ob->id.next) {
136                                 ModifierData *md;
137                                 for (md = ob->modifiers.first; md; md = md->next) {
138                                         if (md->type == eModifierType_Bevel) {
139                                                 BevelModifierData *bmd = (BevelModifierData *)md;
140                                                 bmd->profile = 0.5f;
141                                                 bmd->val_flags = MOD_BEVEL_AMT_OFFSET;
142                                         }
143                                 }
144                         }
145                 }
146
147                 /* nodes don't use fixed node->id any more, clean up */
148                 FOREACH_NODETREE(main, ntree, id) {
149                         if (ntree->type == NTREE_COMPOSIT) {
150                                 bNode *node;
151                                 for (node = ntree->nodes.first; node; node = node->next) {
152                                         if (ELEM(node->type, CMP_NODE_COMPOSITE, CMP_NODE_OUTPUT_FILE)) {
153                                                 node->id = NULL;
154                                         }
155                                 }
156                         }
157                 } FOREACH_NODETREE_END
158
159                 {
160                         bScreen *screen;
161
162                         for (screen = main->screen.first; screen; screen = screen->id.next) {
163                                 ScrArea *area;
164                                 for (area = screen->areabase.first; area; area = area->next) {
165                                         SpaceLink *space_link;
166                                         for (space_link = area->spacedata.first; space_link; space_link = space_link->next) {
167                                                 if (space_link->spacetype == SPACE_CLIP) {
168                                                         SpaceClip *space_clip = (SpaceClip *) space_link;
169                                                         if (space_clip->mode != SC_MODE_MASKEDIT) {
170                                                                 space_clip->mode = SC_MODE_TRACKING;
171                                                         }
172                                                 }
173                                         }
174                                 }
175                         }
176                 }
177
178                 if (!DNA_struct_elem_find(fd->filesdna, "MovieTrackingSettings", "float", "default_weight")) {
179                         MovieClip *clip;
180                         for (clip = main->movieclip.first; clip; clip = clip->id.next) {
181                                 clip->tracking.settings.default_weight = 1.0f;
182                         }
183                 }
184         }
185
186         if (!MAIN_VERSION_ATLEAST(main, 270, 1)) {
187                 Scene *sce;
188                 Object *ob;
189
190                 /* Update Transform constraint (another deg -> rad stuff). */
191                 for (ob = main->object.first; ob; ob = ob->id.next) {
192                         do_version_constraints_radians_degrees_270_1(&ob->constraints);
193
194                         if (ob->pose) {
195                                 /* Bones constraints! */
196                                 bPoseChannel *pchan;
197                                 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
198                                         do_version_constraints_radians_degrees_270_1(&pchan->constraints);
199                                 }
200                         }
201                 }
202
203                 for (sce = main->scene.first; sce; sce = sce->id.next) {
204                         if (sce->r.raytrace_structure == R_RAYSTRUCTURE_BLIBVH) {
205                                 sce->r.raytrace_structure = R_RAYSTRUCTURE_AUTO;
206                         }
207                 }
208         }
209
210         if (!MAIN_VERSION_ATLEAST(main, 270, 2)) {
211                 Mesh *me;
212
213                 /* Mesh smoothresh deg->rad. */
214                 for (me = main->mesh.first; me; me = me->id.next) {
215                         me->smoothresh = DEG2RADF(me->smoothresh);
216                 }
217         }
218
219         if (!MAIN_VERSION_ATLEAST(main, 270, 3)) {
220                 FreestyleLineStyle *linestyle;
221
222                 for (linestyle = main->linestyle.first; linestyle; linestyle = linestyle->id.next) {
223                         linestyle->flag |= LS_NO_SORTING;
224                         linestyle->sort_key = LS_SORT_KEY_DISTANCE_FROM_CAMERA;
225                         linestyle->integration_type = LS_INTEGRATION_MEAN;
226                 }
227         }
228
229         if (!MAIN_VERSION_ATLEAST(main, 270, 4)) {
230                 /* ui_previews were not handled correctly when copying areas, leading to corrupted files (see T39847).
231                  * This will always reset situation to a valid state.
232                  */
233                 bScreen *sc;
234
235                 for (sc = main->screen.first; sc; sc = sc->id.next) {
236                         ScrArea *sa;
237                         for (sa = sc->areabase.first; sa; sa = sa->next) {
238                                 SpaceLink *sl;
239
240                                 for (sl = sa->spacedata.first; sl; sl = sl->next) {
241                                         ARegion *ar;
242                                         ListBase *lb = (sl == sa->spacedata.first) ? &sa->regionbase : &sl->regionbase;
243
244                                         for (ar = lb->first; ar; ar = ar->next) {
245                                                 BLI_listbase_clear(&ar->ui_previews);
246                                         }
247                                 }
248                         }
249                 }
250         }
251
252         if (!MAIN_VERSION_ATLEAST(main, 270, 5)) {
253                 Object *ob;
254
255                 /* Update Transform constraint (again :|). */
256                 for (ob = main->object.first; ob; ob = ob->id.next) {
257                         do_version_constraints_radians_degrees_270_5(&ob->constraints);
258
259                         if (ob->pose) {
260                                 /* Bones constraints! */
261                                 bPoseChannel *pchan;
262                                 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
263                                         do_version_constraints_radians_degrees_270_5(&pchan->constraints);
264                                 }
265                         }
266                 }
267         }
268
269         if (!MAIN_VERSION_ATLEAST(main, 271, 0)) {
270                 if (!DNA_struct_elem_find(fd->filesdna, "Material", "int", "mode2")) {
271                         Material *ma;
272
273                         for (ma = main->mat.first; ma; ma = ma->id.next)
274                                 ma->mode2 = MA_CASTSHADOW;
275                 }
276
277                 if (!DNA_struct_elem_find(fd->filesdna, "RenderData", "BakeData", "bake")) {
278                         Scene *sce;
279
280                         for (sce = main->scene.first; sce; sce = sce->id.next) {
281                                 sce->r.bake.flag = R_BAKE_CLEAR;
282                                 sce->r.bake.width = 512;
283                                 sce->r.bake.height = 512;
284                                 sce->r.bake.margin = 16;
285                                 sce->r.bake.normal_space = R_BAKE_SPACE_TANGENT;
286                                 sce->r.bake.normal_swizzle[0] = R_BAKE_POSX;
287                                 sce->r.bake.normal_swizzle[1] = R_BAKE_POSY;
288                                 sce->r.bake.normal_swizzle[2] = R_BAKE_POSZ;
289                                 BLI_strncpy(sce->r.bake.filepath, U.renderdir, sizeof(sce->r.bake.filepath));
290
291                                 sce->r.bake.im_format.planes = R_IMF_PLANES_RGBA;
292                                 sce->r.bake.im_format.imtype = R_IMF_IMTYPE_PNG;
293                                 sce->r.bake.im_format.depth = R_IMF_CHAN_DEPTH_8;
294                                 sce->r.bake.im_format.quality = 90;
295                                 sce->r.bake.im_format.compress = 15;
296                         }
297                 }
298
299                 if (!DNA_struct_elem_find(fd->filesdna, "FreestyleLineStyle", "float", "texstep")) {
300                         FreestyleLineStyle *linestyle;
301
302                         for (linestyle = main->linestyle.first; linestyle; linestyle = linestyle->id.next) {
303                                 linestyle->flag |= LS_TEXTURE;
304                                 linestyle->texstep = 1.0;
305                         }
306                 }
307
308                 {
309                         Scene *scene;
310                         for (scene = main->scene.first; scene; scene = scene->id.next) {
311                                 int num_layers = BLI_listbase_count(&scene->r.layers);
312                                 scene->r.actlay = min_ff(scene->r.actlay, num_layers - 1);
313                         }
314                 }
315         }
316
317         if (!MAIN_VERSION_ATLEAST(main, 271, 1)) {
318                 if (!DNA_struct_elem_find(fd->filesdna, "Material", "float", "line_col[4]")) {
319                         Material *mat;
320
321                         for (mat = main->mat.first; mat; mat = mat->id.next) {
322                                 mat->line_col[0] = mat->line_col[1] = mat->line_col[2] = 0.0f;
323                                 mat->line_col[3] = mat->alpha;
324                         }
325                 }
326
327                 if (!DNA_struct_elem_find(fd->filesdna, "RenderData", "int", "preview_start_resolution")) {
328                         Scene *scene;
329                         for (scene = main->scene.first; scene; scene = scene->id.next) {
330                                 scene->r.preview_start_resolution = 64;
331                         }
332                 }
333         }
334
335         if (!MAIN_VERSION_ATLEAST(main, 271, 2)) {
336                 /* init up & track axis property of trackto actuators */
337                 Object *ob;
338
339                 for (ob = main->object.first; ob; ob = ob->id.next) {
340                         bActuator *act;
341                         for (act = ob->actuators.first; act; act = act->next) {
342                                 if (act->type == ACT_EDIT_OBJECT) {
343                                         bEditObjectActuator *eoact = act->data;
344                                         eoact->trackflag = ob->trackflag;
345                                         /* if trackflag is pointing +-Z axis then upflag should point Y axis.
346                                          * Rest of trackflag cases, upflag should be point z axis */
347                                         if ((ob->trackflag == OB_POSZ) || (ob->trackflag == OB_NEGZ)) {
348                                                 eoact->upflag = 1;
349                                         }
350                                         else {
351                                                 eoact->upflag = 2;
352                                         }
353                                 }
354                         }
355                 }
356         }
357
358         if (!MAIN_VERSION_ATLEAST(main, 271, 3)) {
359                 Brush *br;
360
361                 for (br = main->brush.first; br; br = br->id.next) {
362                         br->fill_threshold = 0.2f;
363                 }
364
365                 if (!DNA_struct_elem_find(fd->filesdna, "BevelModifierData", "int", "mat")) {
366                         Object *ob;
367                         for (ob = main->object.first; ob; ob = ob->id.next) {
368                                 ModifierData *md;
369
370                                 for (md = ob->modifiers.first; md; md = md->next) {
371                                         if (md->type == eModifierType_Bevel) {
372                                                 BevelModifierData *bmd = (BevelModifierData *)md;
373                                                 bmd->mat = -1;
374                                         }
375                                 }
376                         }
377                 }
378         }
379
380         if (!MAIN_VERSION_ATLEAST(main, 271, 6)) {
381                 Object *ob;
382                 for (ob = main->object.first; ob; ob = ob->id.next) {
383                         ModifierData *md;
384
385                         for (md = ob->modifiers.first; md; md = md->next) {
386                                 if (md->type == eModifierType_ParticleSystem) {
387                                         ParticleSystemModifierData *pmd = (ParticleSystemModifierData *)md;
388                                         if (pmd->psys && pmd->psys->clmd) {
389                                                 pmd->psys->clmd->sim_parms->vel_damping = 1.0f;
390                                         }
391                                 }
392                         }
393                 }
394         }
395
396         if (!MAIN_VERSION_ATLEAST(main, 272, 0)) {
397                 if (!DNA_struct_elem_find(fd->filesdna, "RenderData", "int", "preview_start_resolution")) {
398                         Scene *scene;
399                         for (scene = main->scene.first; scene; scene = scene->id.next) {
400                                 scene->r.preview_start_resolution = 64;
401                         }
402                 }
403         }
404
405         if (!MAIN_VERSION_ATLEAST(main, 272, 1)) {
406                 Brush *br;
407                 for (br = main->brush.first; br; br = br->id.next) {
408                         if ((br->ob_mode & OB_MODE_SCULPT) && ELEM(br->sculpt_tool, SCULPT_TOOL_GRAB, SCULPT_TOOL_SNAKE_HOOK))
409                                 br->alpha = 1.0f;
410                 }
411         }
412
413         if (!MAIN_VERSION_ATLEAST(main, 272, 2)) {
414                 if (!DNA_struct_elem_find(fd->filesdna, "Image", "float", "gen_color")) {
415                         Image *image;
416                         for (image = main->image.first; image != NULL; image = image->id.next) {
417                                 image->gen_color[3] = 1.0f;
418                         }
419                 }
420
421                 if (!DNA_struct_elem_find(fd->filesdna, "bStretchToConstraint", "float", "bulge_min")) {
422                         Object *ob;
423
424                         /* Update Transform constraint (again :|). */
425                         for (ob = main->object.first; ob; ob = ob->id.next) {
426                                 do_version_constraints_stretch_to_limits(&ob->constraints);
427
428                                 if (ob->pose) {
429                                         /* Bones constraints! */
430                                         bPoseChannel *pchan;
431                                         for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
432                                                 do_version_constraints_stretch_to_limits(&pchan->constraints);
433                                         }
434                                 }
435                         }
436                 }
437         }
438
439         if (!MAIN_VERSION_ATLEAST(main, 273, 1)) {
440 #define BRUSH_RAKE (1 << 7)
441 #define BRUSH_RANDOM_ROTATION (1 << 25)
442
443                 Brush *br;
444
445                 for (br = main->brush.first; br; br = br->id.next) {
446                         if (br->flag & BRUSH_RAKE) {
447                                 br->mtex.brush_angle_mode |= MTEX_ANGLE_RAKE;
448                                 br->mask_mtex.brush_angle_mode |= MTEX_ANGLE_RAKE;
449                         }
450                         else if (br->flag & BRUSH_RANDOM_ROTATION) {
451                                 br->mtex.brush_angle_mode |= MTEX_ANGLE_RANDOM;
452                                 br->mask_mtex.brush_angle_mode |= MTEX_ANGLE_RANDOM;
453                         }
454                         br->mtex.random_angle = 2.0 * M_PI;
455                         br->mask_mtex.random_angle = 2.0 * M_PI;
456                 }
457
458 #undef BRUSH_RAKE
459 #undef BRUSH_RANDOM_ROTATION
460         }
461
462         /* Customizable Safe Areas */
463         if (!MAIN_VERSION_ATLEAST(main, 273, 2)) {
464                 if (!DNA_struct_elem_find(fd->filesdna, "Scene", "DisplaySafeAreas", "safe_areas")) {
465                         Scene *scene;
466
467                         for (scene = main->scene.first; scene; scene = scene->id.next) {
468                                 copy_v2_fl2(scene->safe_areas.title, 3.5f / 100.0f, 3.5f / 100.0f);
469                                 copy_v2_fl2(scene->safe_areas.action, 10.0f / 100.0f, 5.0f / 100.0f);
470                                 copy_v2_fl2(scene->safe_areas.title_center, 17.5f / 100.0f, 5.0f / 100.0f);
471                                 copy_v2_fl2(scene->safe_areas.action_center, 15.0f / 100.0f, 5.0f / 100.0f);
472                         }
473                 }
474         }
475         
476         if (!MAIN_VERSION_ATLEAST(main, 273, 3)) {
477                 ParticleSettings *part;
478                 for (part = main->particle.first; part; part = part->id.next) {
479                         if (part->clumpcurve)
480                                 part->child_flag |= PART_CHILD_USE_CLUMP_CURVE;
481                         if (part->roughcurve)
482                                 part->child_flag |= PART_CHILD_USE_ROUGH_CURVE;
483                 }
484         }
485
486         if (!MAIN_VERSION_ATLEAST(main, 273, 6)) {
487                 if (!DNA_struct_elem_find(fd->filesdna, "ClothSimSettings", "float", "bending_damping")) {
488                         Object *ob;
489                         ModifierData *md;
490                         for (ob = main->object.first; ob; ob = ob->id.next) {
491                                 for (md = ob->modifiers.first; md; md = md->next) {
492                                         if (md->type == eModifierType_Cloth) {
493                                                 ClothModifierData *clmd = (ClothModifierData *)md;
494                                                 clmd->sim_parms->bending_damping = 0.5f;
495                                         }
496                                         else if (md->type == eModifierType_ParticleSystem) {
497                                                 ParticleSystemModifierData *pmd = (ParticleSystemModifierData *)md;
498                                                 if (pmd->psys->clmd) {
499                                                         pmd->psys->clmd->sim_parms->bending_damping = 0.5f;
500                                                 }
501                                         }
502                                 }
503                         }
504                 }
505
506                 if (!DNA_struct_elem_find(fd->filesdna, "ParticleSettings", "float", "clump_noise_size")) {
507                         ParticleSettings *part;
508                         for (part = main->particle.first; part; part = part->id.next) {
509                                 part->clump_noise_size = 1.0f;
510                         }
511                 }
512
513                 if (!DNA_struct_elem_find(fd->filesdna, "ParticleSettings", "int", "kink_extra_steps")) {
514                         ParticleSettings *part;
515                         for (part = main->particle.first; part; part = part->id.next) {
516                                 part->kink_extra_steps = 4;
517                         }
518                 }
519
520                 if (!DNA_struct_elem_find(fd->filesdna, "MTex", "float", "kinkampfac")) {
521                         ParticleSettings *part;
522                         for (part = main->particle.first; part; part = part->id.next) {
523                                 int a;
524                                 for (a = 0; a < MAX_MTEX; a++) {
525                                         MTex *mtex = part->mtex[a];
526                                         if (mtex) {
527                                                 mtex->kinkampfac = 1.0f;
528                                         }
529                                 }
530                         }
531                 }
532
533                 if (!DNA_struct_elem_find(fd->filesdna, "HookModifierData", "char", "flag")) {
534                         Object *ob;
535
536                         for (ob = main->object.first; ob; ob = ob->id.next) {
537                                 ModifierData *md;
538                                 for (md = ob->modifiers.first; md; md = md->next) {
539                                         if (md->type == eModifierType_Hook) {
540                                                 HookModifierData *hmd = (HookModifierData *)md;
541                                                 hmd->falloff_type = eHook_Falloff_InvSquare;
542                                         }
543                                 }
544                         }
545                 }
546
547                 if (!DNA_struct_elem_find(fd->filesdna, "NodePlaneTrackDeformData", "char", "flag")) {
548                         FOREACH_NODETREE(main, ntree, id) {
549                                 if (ntree->type == NTREE_COMPOSIT) {
550                                         bNode *node;
551                                         for (node = ntree->nodes.first; node; node = node->next) {
552                                                 if (ELEM(node->type, CMP_NODE_PLANETRACKDEFORM)) {
553                                                         NodePlaneTrackDeformData *data = node->storage;
554                                                         data->flag = 0;
555                                                         data->motion_blur_samples = 16;
556                                                         data->motion_blur_shutter = 0.5f;
557                                                 }
558                                         }
559                                 }
560                         }
561                         FOREACH_NODETREE_END
562                 }
563         }
564 }