GSOC 2013 paint
[blender-staging.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_constraint_types.h"
39 #include "DNA_sdna_types.h"
40 #include "DNA_space_types.h"
41 #include "DNA_screen_types.h"
42 #include "DNA_object_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_modifier_types.h"
45 #include "DNA_linestyle_types.h"
46 #include "DNA_actuator_types.h"
47
48 #include "DNA_genfile.h"
49
50 #include "BLI_blenlib.h"
51 #include "BLI_math.h"
52
53 #include "BKE_main.h"
54 #include "BKE_node.h"
55
56 #include "BLI_math.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 void blo_do_versions_270(FileData *fd, Library *UNUSED(lib), Main *main)
116 {
117         if (!MAIN_VERSION_ATLEAST(main, 270, 0)) {
118
119                 if (!DNA_struct_elem_find(fd->filesdna, "BevelModifierData", "float", "profile")) {
120                         Object *ob;
121
122                         for (ob = main->object.first; ob; ob = ob->id.next) {
123                                 ModifierData *md;
124                                 for (md = ob->modifiers.first; md; md = md->next) {
125                                         if (md->type == eModifierType_Bevel) {
126                                                 BevelModifierData *bmd = (BevelModifierData *)md;
127                                                 bmd->profile = 0.5f;
128                                                 bmd->val_flags = MOD_BEVEL_AMT_OFFSET;
129                                         }
130                                 }
131                         }
132                 }
133
134                 /* nodes don't use fixed node->id any more, clean up */
135                 FOREACH_NODETREE(main, ntree, id) {
136                         if (ntree->type == NTREE_COMPOSIT) {
137                                 bNode *node;
138                                 for (node = ntree->nodes.first; node; node = node->next) {
139                                         if (ELEM(node->type, CMP_NODE_COMPOSITE, CMP_NODE_OUTPUT_FILE)) {
140                                                 node->id = NULL;
141                                         }
142                                 }
143                         }
144                 } FOREACH_NODETREE_END
145
146                 {
147                         bScreen *screen;
148
149                         for (screen = main->screen.first; screen; screen = screen->id.next) {
150                                 ScrArea *area;
151                                 for (area = screen->areabase.first; area; area = area->next) {
152                                         SpaceLink *space_link;
153                                         for (space_link = area->spacedata.first; space_link; space_link = space_link->next) {
154                                                 if (space_link->spacetype == SPACE_CLIP) {
155                                                         SpaceClip *space_clip = (SpaceClip *) space_link;
156                                                         if (space_clip->mode != SC_MODE_MASKEDIT) {
157                                                                 space_clip->mode = SC_MODE_TRACKING;
158                                                         }
159                                                 }
160                                         }
161                                 }
162                         }
163                 }
164
165                 if (!DNA_struct_elem_find(fd->filesdna, "MovieTrackingSettings", "float", "default_weight")) {
166                         MovieClip *clip;
167                         for (clip = main->movieclip.first; clip; clip = clip->id.next) {
168                                 clip->tracking.settings.default_weight = 1.0f;
169                         }
170                 }
171         }
172
173         if (!MAIN_VERSION_ATLEAST(main, 270, 1)) {
174                 Scene *sce;
175                 Object *ob;
176
177                 /* Update Transform constraint (another deg -> rad stuff). */
178                 for (ob = main->object.first; ob; ob = ob->id.next) {
179                         do_version_constraints_radians_degrees_270_1(&ob->constraints);
180
181                         if (ob->pose) {
182                                 /* Bones constraints! */
183                                 bPoseChannel *pchan;
184                                 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
185                                         do_version_constraints_radians_degrees_270_1(&pchan->constraints);
186                                 }
187                         }
188                 }
189
190                 for (sce = main->scene.first; sce; sce = sce->id.next) {
191                         if (sce->r.raytrace_structure == R_RAYSTRUCTURE_BLIBVH) {
192                                 sce->r.raytrace_structure = R_RAYSTRUCTURE_AUTO;
193                         }
194                 }
195         }
196
197         if (!MAIN_VERSION_ATLEAST(main, 270, 2)) {
198                 Mesh *me;
199
200                 /* Mesh smoothresh deg->rad. */
201                 for (me = main->mesh.first; me; me = me->id.next) {
202                         me->smoothresh = DEG2RADF(me->smoothresh);
203                 }
204         }
205
206         if (!MAIN_VERSION_ATLEAST(main, 270, 3)) {
207                 FreestyleLineStyle *linestyle;
208
209                 for (linestyle = main->linestyle.first; linestyle; linestyle = linestyle->id.next) {
210                         linestyle->flag |= LS_NO_SORTING;
211                         linestyle->sort_key = LS_SORT_KEY_DISTANCE_FROM_CAMERA;
212                         linestyle->integration_type = LS_INTEGRATION_MEAN;
213                 }
214         }
215
216         if (!MAIN_VERSION_ATLEAST(main, 270, 4)) {
217                 /* ui_previews were not handled correctly when copying areas, leading to corrupted files (see T39847).
218                  * This will always reset situation to a valid state.
219                  */
220                 bScreen *sc;
221
222                 for (sc = main->screen.first; sc; sc = sc->id.next) {
223                         ScrArea *sa;
224                         for (sa = sc->areabase.first; sa; sa = sa->next) {
225                                 SpaceLink *sl;
226
227                                 for (sl = sa->spacedata.first; sl; sl = sl->next) {
228                                         ARegion *ar;
229                                         ListBase *lb = (sl == sa->spacedata.first) ? &sa->regionbase : &sl->regionbase;
230
231                                         for (ar = lb->first; ar; ar = ar->next) {
232                                                 BLI_listbase_clear(&ar->ui_previews);
233                                         }
234                                 }
235                         }
236                 }
237         }
238
239         if (!MAIN_VERSION_ATLEAST(main, 270, 5)) {
240                 Object *ob;
241
242                 /* Update Transform constraint (again :|). */
243                 for (ob = main->object.first; ob; ob = ob->id.next) {
244                         do_version_constraints_radians_degrees_270_5(&ob->constraints);
245
246                         if (ob->pose) {
247                                 /* Bones constraints! */
248                                 bPoseChannel *pchan;
249                                 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
250                                         do_version_constraints_radians_degrees_270_5(&pchan->constraints);
251                                 }
252                         }
253                 }
254         }
255
256         if (!MAIN_VERSION_ATLEAST(main, 271, 0)) {
257                 if (!DNA_struct_elem_find(fd->filesdna, "Material", "int", "mode2")) {
258                         Material *ma;
259
260                         for (ma = main->mat.first; ma; ma = ma->id.next)
261                                 ma->mode2 = MA_CASTSHADOW;
262                 }
263
264                 if (!DNA_struct_elem_find(fd->filesdna, "RenderData", "BakeData", "bake")) {
265                         Scene *sce;
266
267                         for (sce = main->scene.first; sce; sce = sce->id.next) {
268                                 sce->r.bake.flag = R_BAKE_CLEAR;
269                                 sce->r.bake.width = 512;
270                                 sce->r.bake.height = 512;
271                                 sce->r.bake.margin = 16;
272                                 sce->r.bake.normal_space = R_BAKE_SPACE_TANGENT;
273                                 sce->r.bake.normal_swizzle[0] = R_BAKE_POSX;
274                                 sce->r.bake.normal_swizzle[1] = R_BAKE_POSY;
275                                 sce->r.bake.normal_swizzle[2] = R_BAKE_POSZ;
276                                 BLI_strncpy(sce->r.bake.filepath, U.renderdir, sizeof(sce->r.bake.filepath));
277
278                                 sce->r.bake.im_format.planes = R_IMF_PLANES_RGBA;
279                                 sce->r.bake.im_format.imtype = R_IMF_IMTYPE_PNG;
280                                 sce->r.bake.im_format.depth = R_IMF_CHAN_DEPTH_8;
281                                 sce->r.bake.im_format.quality = 90;
282                                 sce->r.bake.im_format.compress = 15;
283                         }
284                 }
285
286                 if (!DNA_struct_elem_find(fd->filesdna, "FreestyleLineStyle", "float", "texstep")) {
287                         FreestyleLineStyle *linestyle;
288
289                         for (linestyle = main->linestyle.first; linestyle; linestyle = linestyle->id.next) {
290                                 linestyle->flag |= LS_TEXTURE;
291                                 linestyle->texstep = 1.0;
292                         }
293                 }
294
295                 {
296                         Scene *scene;
297                         for (scene = main->scene.first; scene; scene = scene->id.next) {
298                                 int num_layers = BLI_countlist(&scene->r.layers);
299                                 scene->r.actlay = min_ff(scene->r.actlay, num_layers - 1);
300                         }
301                 }
302         }
303
304         if (!MAIN_VERSION_ATLEAST(main, 271, 1)) {
305                 if (!DNA_struct_elem_find(fd->filesdna, "Material", "float", "line_col[4]")) {
306                         Material *mat;
307
308                         for (mat = main->mat.first; mat; mat = mat->id.next) {
309                                 mat->line_col[0] = mat->line_col[1] = mat->line_col[2] = 0.0f;
310                                 mat->line_col[3] = mat->alpha;
311                         }
312                 }
313
314                 if (!DNA_struct_elem_find(fd->filesdna, "RenderData", "int", "preview_start_resolution")) {
315                         Scene *scene;
316                         for (scene = main->scene.first; scene; scene = scene->id.next) {
317                                 scene->r.preview_start_resolution = 64;
318                         }
319                 }
320         }
321
322         if (!MAIN_VERSION_ATLEAST(main, 271, 2)) {
323                 /* init up & track axis property of trackto actuators */
324                 Object *ob;
325
326                 for (ob = main->object.first; ob; ob = ob->id.next) {
327                         bActuator *act;
328                         for (act = ob->actuators.first; act; act = act->next) {
329                                 if (act->type == ACT_EDIT_OBJECT) {
330                                         bEditObjectActuator *eoact = act->data;
331                                         eoact->trackflag = ob->trackflag;
332                                         /* if trackflag is pointing +-Z axis then upflag should point Y axis.
333                                          * Rest of trackflag cases, upflag should be point z axis */
334                                         if ((ob->trackflag == OB_POSZ) || (ob->trackflag == OB_NEGZ)) {
335                                                 eoact->upflag = 1;
336                                         }
337                                         else {
338                                                 eoact->upflag = 2;
339                                         }
340                                 }
341                         }
342                 }
343         }
344
345         if (!MAIN_VERSION_ATLEAST(main, 271, 3)) {
346                 Scene *sce;
347                 Brush *br;
348
349                 for (sce = main->scene.first; sce; sce = sce->id.next) {
350                         sce->toolsettings->imapaint.slot_xresolution_default = 1024;
351                         sce->toolsettings->imapaint.slot_yresolution_default = 1024;
352                 }
353
354                 for (br = main->brush.first; br; br = br->id.next) {
355                         br->fill_threshold = 0.2f;
356                 }
357         }
358
359         if (!DNA_struct_elem_find(fd->filesdna, "RenderData", "int", "preview_start_resolution")) {
360                 Scene *scene;
361                 for (scene = main->scene.first; scene; scene = scene->id.next) {
362                         scene->r.preview_start_resolution = 64;
363                 }
364         }
365 }