Freestyle: Fix for versioning code not working properly with new texture options.
[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_constraint_types.h"
38 #include "DNA_sdna_types.h"
39 #include "DNA_space_types.h"
40 #include "DNA_screen_types.h"
41 #include "DNA_object_types.h"
42 #include "DNA_mesh_types.h"
43 #include "DNA_modifier_types.h"
44 #include "DNA_linestyle_types.h"
45
46 #include "DNA_genfile.h"
47
48 #include "BLI_blenlib.h"
49 #include "BLI_math.h"
50
51 #include "BKE_main.h"
52 #include "BKE_node.h"
53
54 #include "BLI_math.h"
55 #include "BLI_string.h"
56
57 #include "BLO_readfile.h"
58
59 #include "readfile.h"
60
61
62 static void do_version_constraints_radians_degrees_270_1(ListBase *lb)
63 {
64         bConstraint *con;
65
66         for (con = lb->first; con; con = con->next) {
67                 if (con->type == CONSTRAINT_TYPE_TRANSFORM) {
68                         bTransformConstraint *data = (bTransformConstraint *)con->data;
69                         const float deg_to_rad_f = DEG2RADF(1.0f);
70
71                         if (data->from == TRANS_ROTATION) {
72                                 mul_v3_fl(data->from_min, deg_to_rad_f);
73                                 mul_v3_fl(data->from_max, deg_to_rad_f);
74                         }
75
76                         if (data->to == TRANS_ROTATION) {
77                                 mul_v3_fl(data->to_min, deg_to_rad_f);
78                                 mul_v3_fl(data->to_max, deg_to_rad_f);
79                         }
80                 }
81         }
82 }
83
84 static void do_version_constraints_radians_degrees_270_5(ListBase *lb)
85 {
86         bConstraint *con;
87
88         for (con = lb->first; con; con = con->next) {
89                 if (con->type == CONSTRAINT_TYPE_TRANSFORM) {
90                         bTransformConstraint *data = (bTransformConstraint *)con->data;
91
92                         if (data->from == TRANS_ROTATION) {
93                                 copy_v3_v3(data->from_min_rot, data->from_min);
94                                 copy_v3_v3(data->from_max_rot, data->from_max);
95                         }
96                         else if (data->from == TRANS_SCALE) {
97                                 copy_v3_v3(data->from_min_scale, data->from_min);
98                                 copy_v3_v3(data->from_max_scale, data->from_max);
99                         }
100
101                         if (data->to == TRANS_ROTATION) {
102                                 copy_v3_v3(data->to_min_rot, data->to_min);
103                                 copy_v3_v3(data->to_max_rot, data->to_max);
104                         }
105                         else if (data->to == TRANS_SCALE) {
106                                 copy_v3_v3(data->to_min_scale, data->to_min);
107                                 copy_v3_v3(data->to_max_scale, data->to_max);
108                         }
109                 }
110         }
111 }
112
113 void blo_do_versions_270(FileData *fd, Library *UNUSED(lib), Main *main)
114 {
115         if (!MAIN_VERSION_ATLEAST(main, 270, 0)) {
116
117                 if (!DNA_struct_elem_find(fd->filesdna, "BevelModifierData", "float", "profile")) {
118                         Object *ob;
119
120                         for (ob = main->object.first; ob; ob = ob->id.next) {
121                                 ModifierData *md;
122                                 for (md = ob->modifiers.first; md; md = md->next) {
123                                         if (md->type == eModifierType_Bevel) {
124                                                 BevelModifierData *bmd = (BevelModifierData *)md;
125                                                 bmd->profile = 0.5f;
126                                                 bmd->val_flags = MOD_BEVEL_AMT_OFFSET;
127                                         }
128                                 }
129                         }
130                 }
131
132                 /* nodes don't use fixed node->id any more, clean up */
133                 FOREACH_NODETREE(main, ntree, id) {
134                         if (ntree->type == NTREE_COMPOSIT) {
135                                 bNode *node;
136                                 for (node = ntree->nodes.first; node; node = node->next) {
137                                         if (ELEM(node->type, CMP_NODE_COMPOSITE, CMP_NODE_OUTPUT_FILE)) {
138                                                 node->id = NULL;
139                                         }
140                                 }
141                         }
142                 } FOREACH_NODETREE_END
143
144                 {
145                         bScreen *screen;
146
147                         for (screen = main->screen.first; screen; screen = screen->id.next) {
148                                 ScrArea *area;
149                                 for (area = screen->areabase.first; area; area = area->next) {
150                                         SpaceLink *space_link;
151                                         for (space_link = area->spacedata.first; space_link; space_link = space_link->next) {
152                                                 if (space_link->spacetype == SPACE_CLIP) {
153                                                         SpaceClip *space_clip = (SpaceClip *) space_link;
154                                                         if (space_clip->mode != SC_MODE_MASKEDIT) {
155                                                                 space_clip->mode = SC_MODE_TRACKING;
156                                                         }
157                                                 }
158                                         }
159                                 }
160                         }
161                 }
162
163                 if (!DNA_struct_elem_find(fd->filesdna, "MovieTrackingSettings", "float", "default_weight")) {
164                         MovieClip *clip;
165                         for (clip = main->movieclip.first; clip; clip = clip->id.next) {
166                                 clip->tracking.settings.default_weight = 1.0f;
167                         }
168                 }
169         }
170
171         if (!MAIN_VERSION_ATLEAST(main, 270, 1)) {
172                 Scene *sce;
173                 Object *ob;
174
175                 /* Update Transform constraint (another deg -> rad stuff). */
176                 for (ob = main->object.first; ob; ob = ob->id.next) {
177                         do_version_constraints_radians_degrees_270_1(&ob->constraints);
178
179                         if (ob->pose) {
180                                 /* Bones constraints! */
181                                 bPoseChannel *pchan;
182                                 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
183                                         do_version_constraints_radians_degrees_270_1(&pchan->constraints);
184                                 }
185                         }
186                 }
187
188                 for (sce = main->scene.first; sce; sce = sce->id.next) {
189                         if (sce->r.raytrace_structure == R_RAYSTRUCTURE_BLIBVH) {
190                                 sce->r.raytrace_structure = R_RAYSTRUCTURE_AUTO;
191                         }
192                 }
193         }
194
195         if (!MAIN_VERSION_ATLEAST(main, 270, 2)) {
196                 Mesh *me;
197
198                 /* Mesh smoothresh deg->rad. */
199                 for (me = main->mesh.first; me; me = me->id.next) {
200                         me->smoothresh = DEG2RADF(me->smoothresh);
201                 }
202         }
203
204         if (!MAIN_VERSION_ATLEAST(main, 270, 3)) {
205                 FreestyleLineStyle *linestyle;
206
207                 for (linestyle = main->linestyle.first; linestyle; linestyle = linestyle->id.next) {
208                         linestyle->flag |= LS_NO_SORTING;
209                         linestyle->sort_key = LS_SORT_KEY_DISTANCE_FROM_CAMERA;
210                         linestyle->integration_type = LS_INTEGRATION_MEAN;
211                 }
212         }
213
214         if (!MAIN_VERSION_ATLEAST(main, 270, 4)) {
215                 /* ui_previews were not handled correctly when copying areas, leading to corrupted files (see T39847).
216                  * This will always reset situation to a valid state.
217                  */
218                 bScreen *sc;
219
220                 for (sc = main->screen.first; sc; sc = sc->id.next) {
221                         ScrArea *sa;
222                         for (sa = sc->areabase.first; sa; sa = sa->next) {
223                                 SpaceLink *sl;
224
225                                 for (sl = sa->spacedata.first; sl; sl = sl->next) {
226                                         ARegion *ar;
227                                         ListBase *lb = (sl == sa->spacedata.first) ? &sa->regionbase : &sl->regionbase;
228
229                                         for (ar = lb->first; ar; ar = ar->next) {
230                                                 BLI_listbase_clear(&ar->ui_previews);
231                                         }
232                                 }
233                         }
234                 }
235         }
236
237         if (!MAIN_VERSION_ATLEAST(main, 270, 5)) {
238                 Object *ob;
239
240                 /* Update Transform constraint (again :|). */
241                 for (ob = main->object.first; ob; ob = ob->id.next) {
242                         do_version_constraints_radians_degrees_270_5(&ob->constraints);
243
244                         if (ob->pose) {
245                                 /* Bones constraints! */
246                                 bPoseChannel *pchan;
247                                 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
248                                         do_version_constraints_radians_degrees_270_5(&pchan->constraints);
249                                 }
250                         }
251                 }
252         }
253
254         if (!DNA_struct_elem_find(fd->filesdna, "Material", "int", "mode2")) {
255                 Material *ma;
256
257                 for (ma = main->mat.first; ma; ma = ma->id.next)
258                         ma->mode2 = MA_CASTSHADOW;
259         }
260
261         if (!DNA_struct_elem_find(fd->filesdna, "RenderData", "BakeData", "bake")) {
262                 Scene *sce;
263
264                 for (sce = main->scene.first; sce; sce = sce->id.next) {
265                         sce->r.bake.flag = R_BAKE_CLEAR;
266                         sce->r.bake.width = 512;
267                         sce->r.bake.height = 512;
268                         sce->r.bake.margin = 16;
269                         sce->r.bake.normal_space = R_BAKE_SPACE_TANGENT;
270                         sce->r.bake.normal_swizzle[0] = R_BAKE_POSX;
271                         sce->r.bake.normal_swizzle[1] = R_BAKE_POSY;
272                         sce->r.bake.normal_swizzle[2] = R_BAKE_POSZ;
273                         BLI_strncpy(sce->r.bake.filepath, U.renderdir, sizeof(sce->r.bake.filepath));
274
275                         sce->r.bake.im_format.planes = R_IMF_PLANES_RGBA;
276                         sce->r.bake.im_format.imtype = R_IMF_IMTYPE_PNG;
277                         sce->r.bake.im_format.depth = R_IMF_CHAN_DEPTH_8;
278                         sce->r.bake.im_format.quality = 90;
279                         sce->r.bake.im_format.compress = 15;
280                 }
281         }
282
283         if (!DNA_struct_elem_find(fd->filesdna, "FreestyleLineStyle", "float", "texstep")) {
284                 FreestyleLineStyle *linestyle;
285
286                 for (linestyle = main->linestyle.first; linestyle; linestyle = linestyle->id.next) {
287                         linestyle->flag |= LS_TEXTURE;
288                         linestyle->texstep = 1.0;
289                 }
290         }
291 }