Moved function declarations from BKE_DerivedMesh.h to BKE_mesh_runtime.h
[blender.git] / source / blender / blenloader / intern / versioning_250.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_250.c
25  *  \ingroup blenloader
26  */
27
28 #ifndef WIN32
29 #  include <unistd.h>  /* for read close */
30 #else
31 #  include <zlib.h>  /* odd include order-issue */
32 #  include <io.h> // for open close read
33 #  include "winsock2.h"
34 #  include "BLI_winstuff.h"
35 #endif
36
37 /* allow readfile to use deprecated functionality */
38 #define DNA_DEPRECATED_ALLOW
39
40 #include "DNA_anim_types.h"
41 #include "DNA_armature_types.h"
42 #include "DNA_brush_types.h"
43 #include "DNA_camera_types.h"
44 #include "DNA_cloth_types.h"
45 #include "DNA_constraint_types.h"
46 #include "DNA_ipo_types.h"
47 #include "DNA_key_types.h"
48 #include "DNA_lattice_types.h"
49 #include "DNA_lamp_types.h"
50 #include "DNA_material_types.h"
51 #include "DNA_mesh_types.h"
52 #include "DNA_meshdata_types.h"
53 #include "DNA_node_types.h"
54 #include "DNA_object_fluidsim_types.h"
55 #include "DNA_object_types.h"
56 #include "DNA_view3d_types.h"
57 #include "DNA_screen_types.h"
58 #include "DNA_sdna_types.h"
59 #include "DNA_sequence_types.h"
60 #include "DNA_smoke_types.h"
61 #include "DNA_sound_types.h"
62 #include "DNA_space_types.h"
63 #include "DNA_world_types.h"
64
65 #include "MEM_guardedalloc.h"
66
67 #include "BLI_utildefines.h"
68 #include "BLI_blenlib.h"
69 #include "BLI_math.h"
70
71 #include "BKE_anim.h"
72 #include "BKE_armature.h"
73 #include "BKE_colortools.h"
74 #include "BKE_global.h" // for G
75 #include "BKE_library.h" // for which_libbase
76 #include "BKE_main.h" // for Main
77 #include "BKE_mesh.h" // for ME_ defines (patching)
78 #include "BKE_modifier.h"
79 #include "BKE_multires.h"
80 #include "BKE_particle.h"
81 #include "BKE_pointcache.h"
82 #include "BKE_screen.h"
83 #include "BKE_sequencer.h"
84 #include "BKE_texture.h"
85 #include "BKE_sound.h"
86
87 #include "NOD_socket.h"
88
89 #include "BLO_readfile.h"
90
91 #include "readfile.h"
92
93 #include <errno.h>
94
95 /* 2.50 patch */
96 static void area_add_header_region(ScrArea *sa, ListBase *lb)
97 {
98         ARegion *ar = MEM_callocN(sizeof(ARegion), "area region from do_versions");
99
100         BLI_addtail(lb, ar);
101         ar->regiontype = RGN_TYPE_HEADER;
102         if (sa->headertype == 1)
103                 ar->alignment = RGN_ALIGN_BOTTOM;
104         else
105                 ar->alignment = RGN_ALIGN_TOP;
106
107         /* initialize view2d data for header region, to allow panning */
108         /* is copy from ui_view2d.c */
109         ar->v2d.keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_LIMITZOOM|V2D_KEEPASPECT);
110         ar->v2d.keepofs = V2D_LOCKOFS_Y;
111         ar->v2d.keeptot = V2D_KEEPTOT_STRICT;
112         ar->v2d.align = V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_NEG_Y;
113         ar->v2d.flag = (V2D_PIXELOFS_X|V2D_PIXELOFS_Y);
114 }
115
116 static void sequencer_init_preview_region(ARegion *ar)
117 {
118         // XXX a bit ugly still, copied from space_sequencer
119         /* NOTE: if you change values here, also change them in space_sequencer.c, sequencer_new */
120         ar->regiontype = RGN_TYPE_PREVIEW;
121         ar->alignment = RGN_ALIGN_TOP;
122         ar->flag |= RGN_FLAG_HIDDEN;
123         ar->v2d.keepzoom = V2D_KEEPASPECT | V2D_KEEPZOOM;
124         ar->v2d.minzoom = 0.00001f;
125         ar->v2d.maxzoom = 100000.0f;
126         ar->v2d.tot.xmin = -960.0f; /* 1920 width centered */
127         ar->v2d.tot.ymin = -540.0f; /* 1080 height centered */
128         ar->v2d.tot.xmax = 960.0f;
129         ar->v2d.tot.ymax = 540.0f;
130         ar->v2d.min[0] = 0.0f;
131         ar->v2d.min[1] = 0.0f;
132         ar->v2d.max[0] = 12000.0f;
133         ar->v2d.max[1] = 12000.0f;
134         ar->v2d.cur = ar->v2d.tot;
135         ar->v2d.align = V2D_ALIGN_FREE; // (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_NEG_Y);
136         ar->v2d.keeptot = V2D_KEEPTOT_FREE;
137 }
138
139 static void area_add_window_regions(ScrArea *sa, SpaceLink *sl, ListBase *lb)
140 {
141         ARegion *ar;
142         ARegion *ar_main;
143
144         if (sl) {
145                 /* first channels for ipo action nla... */
146                 switch (sl->spacetype) {
147                         case SPACE_IPO:
148                                 ar = MEM_callocN(sizeof(ARegion), "area region from do_versions");
149                                 BLI_addtail(lb, ar);
150                                 ar->regiontype = RGN_TYPE_CHANNELS;
151                                 ar->alignment = RGN_ALIGN_LEFT;
152                                 ar->v2d.scroll = (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM);
153
154                                 /* for some reason, this doesn't seem to go auto like for NLA... */
155                                 ar = MEM_callocN(sizeof(ARegion), "area region from do_versions");
156                                 BLI_addtail(lb, ar);
157                                 ar->regiontype = RGN_TYPE_UI;
158                                 ar->alignment = RGN_ALIGN_RIGHT;
159                                 ar->v2d.scroll = V2D_SCROLL_RIGHT;
160                                 ar->v2d.flag = RGN_FLAG_HIDDEN;
161                                 break;
162
163                         case SPACE_ACTION:
164                                 ar = MEM_callocN(sizeof(ARegion), "area region from do_versions");
165                                 BLI_addtail(lb, ar);
166                                 ar->regiontype = RGN_TYPE_CHANNELS;
167                                 ar->alignment = RGN_ALIGN_LEFT;
168                                 ar->v2d.scroll = V2D_SCROLL_BOTTOM;
169                                 ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
170                                 break;
171
172                         case SPACE_NLA:
173                                 ar = MEM_callocN(sizeof(ARegion), "area region from do_versions");
174                                 BLI_addtail(lb, ar);
175                                 ar->regiontype = RGN_TYPE_CHANNELS;
176                                 ar->alignment = RGN_ALIGN_LEFT;
177                                 ar->v2d.scroll = V2D_SCROLL_BOTTOM;
178                                 ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
179
180                                 /* for some reason, some files still don't get this auto */
181                                 ar = MEM_callocN(sizeof(ARegion), "area region from do_versions");
182                                 BLI_addtail(lb, ar);
183                                 ar->regiontype = RGN_TYPE_UI;
184                                 ar->alignment = RGN_ALIGN_RIGHT;
185                                 ar->v2d.scroll = V2D_SCROLL_RIGHT;
186                                 ar->v2d.flag = RGN_FLAG_HIDDEN;
187                                 break;
188
189                         case SPACE_NODE:
190                                 ar = MEM_callocN(sizeof(ARegion), "nodetree area for node");
191                                 BLI_addtail(lb, ar);
192                                 ar->regiontype = RGN_TYPE_UI;
193                                 ar->alignment = RGN_ALIGN_LEFT;
194                                 ar->v2d.scroll = (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM);
195                                 ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
196                                 /* temporarily hide it */
197                                 ar->flag = RGN_FLAG_HIDDEN;
198                                 break;
199                         case SPACE_FILE:
200                                 ar = MEM_callocN(sizeof(ARegion), "nodetree area for node");
201                                 BLI_addtail(lb, ar);
202                                 ar->regiontype = RGN_TYPE_CHANNELS;
203                                 ar->alignment = RGN_ALIGN_LEFT;
204
205                                 ar = MEM_callocN(sizeof(ARegion), "ui area for file");
206                                 BLI_addtail(lb, ar);
207                                 ar->regiontype = RGN_TYPE_UI;
208                                 ar->alignment = RGN_ALIGN_TOP;
209                                 break;
210                         case SPACE_SEQ:
211                                 ar_main = (ARegion*) lb->first;
212                                 for (; ar_main; ar_main = ar_main->next) {
213                                         if (ar_main->regiontype == RGN_TYPE_WINDOW)
214                                                 break;
215                                 }
216                                 ar = MEM_callocN(sizeof(ARegion), "preview area for sequencer");
217                                 BLI_insertlinkbefore(lb, ar_main, ar);
218                                 sequencer_init_preview_region(ar);
219                                 break;
220                         case SPACE_VIEW3D:
221                                 /* toolbar */
222                                 ar = MEM_callocN(sizeof(ARegion), "toolbar for view3d");
223
224                                 BLI_addtail(lb, ar);
225                                 ar->regiontype = RGN_TYPE_TOOLS;
226                                 ar->alignment = RGN_ALIGN_LEFT;
227                                 ar->flag = RGN_FLAG_HIDDEN;
228
229                                 /* tool properties */
230                                 ar = MEM_callocN(sizeof(ARegion), "tool properties for view3d");
231
232                                 BLI_addtail(lb, ar);
233                                 ar->regiontype = RGN_TYPE_TOOL_PROPS;
234                                 ar->alignment = RGN_ALIGN_BOTTOM|RGN_SPLIT_PREV;
235                                 ar->flag = RGN_FLAG_HIDDEN;
236
237                                 /* buttons/list view */
238                                 ar = MEM_callocN(sizeof(ARegion), "buttons for view3d");
239
240                                 BLI_addtail(lb, ar);
241                                 ar->regiontype = RGN_TYPE_UI;
242                                 ar->alignment = RGN_ALIGN_RIGHT;
243                                 ar->flag = RGN_FLAG_HIDDEN;
244 #if 0
245                         case SPACE_BUTS:
246                                 /* context UI region */
247                                 ar = MEM_callocN(sizeof(ARegion), "area region from do_versions");
248                                 BLI_addtail(lb, ar);
249                                 ar->regiontype = RGN_TYPE_UI;
250                                 ar->alignment = RGN_ALIGN_RIGHT;
251
252                                 break;
253 #endif
254                 }
255         }
256
257         /* main region */
258         ar = MEM_callocN(sizeof(ARegion), "area region from do_versions");
259
260         BLI_addtail(lb, ar);
261         ar->winrct = sa->totrct;
262
263         ar->regiontype = RGN_TYPE_WINDOW;
264
265         if (sl) {
266                 /* if active spacetype has view2d data, copy that over to main region */
267                 /* and we split view3d */
268                 switch (sl->spacetype) {
269                         case SPACE_VIEW3D:
270                                 blo_do_versions_view3d_split_250((View3D *)sl, lb);
271                                 break;
272
273                         case SPACE_OUTLINER:
274                                 {
275                                         SpaceOops *soops = (SpaceOops *)sl;
276
277                                         memcpy(&ar->v2d, &soops->v2d, sizeof(View2D));
278
279                                         ar->v2d.scroll &= ~V2D_SCROLL_LEFT;
280                                         ar->v2d.scroll |= (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM);
281                                         ar->v2d.align = (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_POS_Y);
282                                         ar->v2d.keepzoom |= (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_KEEPASPECT);
283                                         ar->v2d.keeptot = V2D_KEEPTOT_STRICT;
284                                         ar->v2d.minzoom = ar->v2d.maxzoom = 1.0f;
285                                         //ar->v2d.flag |= V2D_IS_INITIALISED;
286                                 }
287                                 break;
288                         case SPACE_IPO:
289                                 {
290                                         SpaceIpo *sipo = (SpaceIpo *)sl;
291                                         memcpy(&ar->v2d, &sipo->v2d, sizeof(View2D));
292
293                                         /* init mainarea view2d */
294                                         ar->v2d.scroll |= (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
295                                         ar->v2d.scroll |= (V2D_SCROLL_LEFT|V2D_SCROLL_SCALE_VERTICAL);
296
297                                         ar->v2d.min[0] = FLT_MIN;
298                                         ar->v2d.min[1] = FLT_MIN;
299
300                                         ar->v2d.max[0] = MAXFRAMEF;
301                                         ar->v2d.max[1] = FLT_MAX;
302
303                                         //ar->v2d.flag |= V2D_IS_INITIALISED;
304                                         break;
305                                 }
306                         case SPACE_NLA:
307                                 {
308                                         SpaceNla *snla = (SpaceNla *)sl;
309                                         memcpy(&ar->v2d, &snla->v2d, sizeof(View2D));
310
311                                         ar->v2d.tot.ymin = (float)(-sa->winy) / 3.0f;
312                                         ar->v2d.tot.ymax = 0.0f;
313
314                                         ar->v2d.scroll |= (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
315                                         ar->v2d.scroll |= (V2D_SCROLL_RIGHT);
316                                         ar->v2d.align = V2D_ALIGN_NO_POS_Y;
317                                         ar->v2d.flag |= V2D_VIEWSYNC_AREA_VERTICAL;
318                                         break;
319                                 }
320                         case SPACE_ACTION:
321                                 {
322                                         SpaceAction *saction = (SpaceAction *) sl;
323
324                                         /* we totally reinit the view for the Action Editor, as some old instances had some weird cruft set */
325                                         ar->v2d.tot.xmin = -20.0f;
326                                         ar->v2d.tot.ymin = (float)(-sa->winy) / 3.0f;
327                                         ar->v2d.tot.xmax = (float)((sa->winx > 120) ? (sa->winx) : 120);
328                                         ar->v2d.tot.ymax = 0.0f;
329
330                                         ar->v2d.cur = ar->v2d.tot;
331
332                                         ar->v2d.min[0] = 0.0f;
333                                         ar->v2d.min[1] = 0.0f;
334
335                                         ar->v2d.max[0] = MAXFRAMEF;
336                                         ar->v2d.max[1] = FLT_MAX;
337
338                                         ar->v2d.minzoom = 0.01f;
339                                         ar->v2d.maxzoom = 50;
340                                         ar->v2d.scroll = (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
341                                         ar->v2d.scroll |= (V2D_SCROLL_RIGHT);
342                                         ar->v2d.keepzoom = V2D_LOCKZOOM_Y;
343                                         ar->v2d.align = V2D_ALIGN_NO_POS_Y;
344                                         ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
345
346                                         /* for old files with ShapeKey editors open + an action set, clear the action as
347                                          * it doesn't make sense in the new system (i.e. violates concept that ShapeKey edit
348                                          * only shows ShapeKey-rooted actions only)
349                                          */
350                                         if (saction->mode == SACTCONT_SHAPEKEY)
351                                                 saction->action = NULL;
352                                         break;
353                                 }
354                         case SPACE_SEQ:
355                                 {
356                                         SpaceSeq *sseq = (SpaceSeq *)sl;
357                                         memcpy(&ar->v2d, &sseq->v2d, sizeof(View2D));
358
359                                         ar->v2d.scroll |= (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
360                                         ar->v2d.scroll |= (V2D_SCROLL_LEFT|V2D_SCROLL_SCALE_VERTICAL);
361                                         ar->v2d.align = V2D_ALIGN_NO_NEG_Y;
362                                         ar->v2d.flag |= V2D_IS_INITIALISED;
363                                         break;
364                                 }
365                         case SPACE_NODE:
366                                 {
367                                         SpaceNode *snode = (SpaceNode *)sl;
368                                         memcpy(&ar->v2d, &snode->v2d, sizeof(View2D));
369
370                                         ar->v2d.scroll = (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM);
371                                         ar->v2d.keepzoom = V2D_LIMITZOOM|V2D_KEEPASPECT;
372                                         break;
373                                 }
374                         case SPACE_BUTS:
375                                 {
376                                         SpaceButs *sbuts = (SpaceButs *)sl;
377                                         memcpy(&ar->v2d, &sbuts->v2d, sizeof(View2D));
378
379                                         ar->v2d.scroll |= (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM);
380                                         break;
381                                 }
382                         case SPACE_FILE:
383                                 {
384                                         // SpaceFile *sfile = (SpaceFile *)sl;
385                                         ar->v2d.tot.xmin = ar->v2d.tot.ymin = 0;
386                                         ar->v2d.tot.xmax = ar->winx;
387                                         ar->v2d.tot.ymax = ar->winy;
388                                         ar->v2d.cur = ar->v2d.tot;
389                                         ar->regiontype = RGN_TYPE_WINDOW;
390                                         ar->v2d.scroll = (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM);
391                                         ar->v2d.align = (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_POS_Y);
392                                         ar->v2d.keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_LIMITZOOM|V2D_KEEPASPECT);
393                                         break;
394                                 }
395                         case SPACE_TEXT:
396                                 {
397                                         SpaceText *st = (SpaceText *)sl;
398                                         st->flags |= ST_FIND_WRAP;
399                                 }
400                                 //case SPACE_XXX: // FIXME... add other ones
401                                 //      memcpy(&ar->v2d, &((SpaceXxx *)sl)->v2d, sizeof(View2D));
402                                 //      break;
403                 }
404         }
405 }
406
407 static void do_versions_windowmanager_2_50(bScreen *screen)
408 {
409         ScrArea *sa;
410         SpaceLink *sl;
411
412         /* add regions */
413         for (sa = screen->areabase.first; sa; sa = sa->next) {
414                 /* we keep headertype variable to convert old files only */
415                 if (sa->headertype)
416                         area_add_header_region(sa, &sa->regionbase);
417
418                 area_add_window_regions(sa, sa->spacedata.first, &sa->regionbase);
419
420                 /* space imageselect is deprecated */
421                 for (sl = sa->spacedata.first; sl; sl = sl->next) {
422                         if (sl->spacetype == SPACE_IMASEL)
423                                 sl->spacetype = SPACE_EMPTY;    /* spacedata then matches */
424                 }
425
426                 /* space sound is deprecated */
427                 for (sl = sa->spacedata.first; sl; sl = sl->next) {
428                         if (sl->spacetype == SPACE_SOUND)
429                                 sl->spacetype = SPACE_EMPTY;    /* spacedata then matches */
430                 }
431
432                 /* pushed back spaces also need regions! */
433                 if (sa->spacedata.first) {
434                         sl = sa->spacedata.first;
435                         for (sl = sl->next; sl; sl = sl->next) {
436                                 if (sa->headertype)
437                                         area_add_header_region(sa, &sl->regionbase);
438                                 area_add_window_regions(sa, sl, &sl->regionbase);
439                         }
440                 }
441         }
442 }
443
444 static void versions_gpencil_add_main(ListBase *lb, ID *id, const char *name)
445 {
446         BLI_addtail(lb, id);
447         id->us = 1;
448         id->flag = LIB_FAKEUSER;
449         *( (short *)id->name )= ID_GD;
450
451         new_id(lb, id, name);
452         /* alphabetic insertion: is in new_id */
453
454         if (G.debug & G_DEBUG)
455                 printf("Converted GPencil to ID: %s\n", id->name + 2);
456 }
457
458 static void do_versions_gpencil_2_50(Main *main, bScreen *screen)
459 {
460         ScrArea *sa;
461         SpaceLink *sl;
462
463         /* add regions */
464         for (sa = screen->areabase.first; sa; sa = sa->next) {
465                 for (sl = sa->spacedata.first; sl; sl = sl->next) {
466                         if (sl->spacetype == SPACE_VIEW3D) {
467                                 View3D *v3d = (View3D*) sl;
468                                 if (v3d->gpd) {
469                                         versions_gpencil_add_main(&main->gpencil, (ID *)v3d->gpd, "GPencil View3D");
470                                         v3d->gpd = NULL;
471                                 }
472                         }
473                         else if (sl->spacetype == SPACE_NODE) {
474                                 SpaceNode *snode = (SpaceNode *) sl;
475                                 if (snode->gpd) {
476                                         versions_gpencil_add_main(&main->gpencil, (ID *)snode->gpd, "GPencil Node");
477                                         snode->gpd = NULL;
478                                 }
479                         }
480                         else if (sl->spacetype == SPACE_SEQ) {
481                                 SpaceSeq *sseq = (SpaceSeq *) sl;
482                                 if (sseq->gpd) {
483                                         versions_gpencil_add_main(&main->gpencil, (ID *)sseq->gpd, "GPencil Node");
484                                         sseq->gpd = NULL;
485                                 }
486                         }
487                         else if (sl->spacetype == SPACE_IMAGE) {
488                                 SpaceImage *sima = (SpaceImage *) sl;
489 #if 0                   /* see comment on r28002 */
490                                 if (sima->gpd) {
491                                         versions_gpencil_add_main(&main->gpencil, (ID *)sima->gpd, "GPencil Image");
492                                         sima->gpd = NULL;
493                                 }
494 #else
495                                 sima->gpd = NULL;
496 #endif
497                         }
498                 }
499         }
500 }
501
502 static void do_version_mdef_250(Main *main)
503 {
504         Object *ob;
505         ModifierData *md;
506         MeshDeformModifierData *mmd;
507
508         for (ob = main->object.first; ob; ob = ob->id.next) {
509                 for (md = ob->modifiers.first; md; md = md->next) {
510                         if (md->type == eModifierType_MeshDeform) {
511                                 mmd = (MeshDeformModifierData*) md;
512
513                                 if (mmd->bindcos) {
514                                         /* make bindcos NULL in order to trick older versions
515                                          * into thinking that the mesh was not bound yet */
516                                         mmd->bindcagecos = mmd->bindcos;
517                                         mmd->bindcos = NULL;
518
519                                         modifier_mdef_compact_influences(md);
520                                 }
521                         }
522                 }
523         }
524 }
525
526 static void do_version_constraints_radians_degrees_250(ListBase *lb)
527 {
528         bConstraint *con;
529
530         for (con = lb->first; con; con = con->next) {
531                 if (con->type == CONSTRAINT_TYPE_KINEMATIC) {
532                         bKinematicConstraint *data = con->data;
533                         data->poleangle *= (float)(M_PI / 180.0);
534                 }
535                 else if (con->type == CONSTRAINT_TYPE_ROTLIMIT) {
536                         bRotLimitConstraint *data = con->data;
537
538                         data->xmin *= (float)(M_PI / 180.0);
539                         data->xmax *= (float)(M_PI / 180.0);
540                         data->ymin *= (float)(M_PI / 180.0);
541                         data->ymax *= (float)(M_PI / 180.0);
542                         data->zmin *= (float)(M_PI / 180.0);
543                         data->zmax *= (float)(M_PI / 180.0);
544                 }
545         }
546 }
547
548 /* NOTE: this version patch is intended for versions < 2.52.2, but was initially introduced in 2.27 already */
549 static void do_versions_seq_unique_name_all_strips(Scene *sce, ListBase *seqbasep)
550 {
551         Sequence * seq = seqbasep->first;
552
553         while (seq) {
554                 BKE_sequence_base_unique_name_recursive(&sce->ed->seqbase, seq);
555                 if (seq->seqbase.first) {
556                         do_versions_seq_unique_name_all_strips(sce, &seq->seqbase);
557                 }
558                 seq = seq->next;
559         }
560 }
561
562 static void do_version_bone_roll_256(Bone *bone)
563 {
564         Bone *child;
565         float submat[3][3];
566
567         copy_m3_m4(submat, bone->arm_mat);
568         mat3_to_vec_roll(submat, NULL, &bone->arm_roll);
569
570         for (child = bone->childbase.first; child; child = child->next)
571                 do_version_bone_roll_256(child);
572 }
573
574 /* deprecated, only keep this for readfile.c */
575 /* XXX Deprecated function to add a socket in ntree->inputs/ntree->outputs list
576  * (previously called node_group_add_socket). This function has been superseded
577  * by the implementation of proxy nodes. It is still necessary though
578  * for do_versions of pre-2.56.2 code (r35033), so later proxy nodes
579  * can be generated consistently from ntree socket lists.
580  */
581 static bNodeSocket *do_versions_node_group_add_socket_2_56_2(bNodeTree *ngroup, const char *name, int type, int in_out)
582 {
583 //      bNodeSocketType *stype = ntreeGetSocketType(type);
584         bNodeSocket *gsock = MEM_callocN(sizeof(bNodeSocket), "bNodeSocket");
585         
586         BLI_strncpy(gsock->name, name, sizeof(gsock->name));
587         gsock->type = type;
588
589         gsock->next = gsock->prev = NULL;
590         gsock->new_sock = NULL;
591         gsock->link = NULL;
592         /* assign new unique index */
593         gsock->own_index = ngroup->cur_index++;
594         gsock->limit = (in_out==SOCK_IN ? 0xFFF : 1);
595         
596 //      if (stype->value_structsize > 0)
597 //              gsock->default_value = MEM_callocN(stype->value_structsize, "default socket value");
598         
599         BLI_addtail(in_out==SOCK_IN ? &ngroup->inputs : &ngroup->outputs, gsock);
600         
601         ngroup->update |= (in_out==SOCK_IN ? NTREE_UPDATE_GROUP_IN : NTREE_UPDATE_GROUP_OUT);
602         
603         return gsock;
604 }
605
606 /* Create default_value structs for node sockets from the internal bNodeStack value.
607  * These structs were used from 2.59.2 on, but are replaced in the subsequent do_versions for custom nodes
608  * by generic ID property values. This conversion happened _after_ do_versions originally due to messy type initialization
609  * for node sockets. Now created here intermediately for convenience and to keep do_versions consistent.
610  *
611  * Node compatibility code is gross ...
612  */
613 static void do_versions_socket_default_value_259(bNodeSocket *sock)
614 {
615         bNodeSocketValueFloat *valfloat;
616         bNodeSocketValueVector *valvector;
617         bNodeSocketValueRGBA *valrgba;
618         
619         if (sock->default_value)
620                 return;
621         
622         switch (sock->type) {
623                 case SOCK_FLOAT:
624                         valfloat = sock->default_value = MEM_callocN(sizeof(bNodeSocketValueFloat), "default socket value");
625                         valfloat->value = sock->ns.vec[0];
626                         valfloat->min = sock->ns.min;
627                         valfloat->max = sock->ns.max;
628                         valfloat->subtype = PROP_NONE;
629                         break;
630                 case SOCK_VECTOR:
631                         valvector = sock->default_value = MEM_callocN(sizeof(bNodeSocketValueVector), "default socket value");
632                         copy_v3_v3(valvector->value, sock->ns.vec);
633                         valvector->min = sock->ns.min;
634                         valvector->max = sock->ns.max;
635                         valvector->subtype = PROP_NONE;
636                         break;
637                 case SOCK_RGBA:
638                         valrgba = sock->default_value = MEM_callocN(sizeof(bNodeSocketValueRGBA), "default socket value");
639                         copy_v4_v4(valrgba->value, sock->ns.vec);
640                         break;
641         }
642 }
643
644 void blo_do_versions_250(FileData *fd, Library *lib, Main *main)
645 {
646         /* WATCH IT!!!: pointers from libdata have not been converted */
647
648         if (main->versionfile < 250) {
649                 bScreen *screen;
650                 Scene *scene;
651                 Base *base;
652                 Material *ma;
653                 Camera *cam;
654                 Mesh *me;
655                 Curve *cu;
656                 Scene *sce;
657                 Tex *tx;
658                 ParticleSettings *part;
659                 Object *ob;
660                 //PTCacheID *pid;
661                 //ListBase pidlist;
662
663                 bSound *sound;
664                 Sequence *seq;
665
666                 for (sound = main->sound.first; sound; sound = sound->id.next) {
667                         if (sound->newpackedfile) {
668                                 sound->packedfile = sound->newpackedfile;
669                                 sound->newpackedfile = NULL;
670                         }
671                 }
672
673                 for (scene = main->scene.first; scene; scene = scene->id.next) {
674                         if (scene->ed && scene->ed->seqbasep) {
675                                 SEQ_BEGIN (scene->ed, seq)
676                                 {
677                                         if (seq->type == SEQ_TYPE_SOUND_HD) {
678                                                 char str[FILE_MAX];
679                                                 BLI_join_dirfile(str, sizeof(str), seq->strip->dir, seq->strip->stripdata->name);
680                                                 BLI_path_abs(str, main->name);
681                                                 seq->sound = BKE_sound_new_file(main, str);
682                                         }
683 #define SEQ_USE_PROXY_CUSTOM_DIR (1 << 19)
684 #define SEQ_USE_PROXY_CUSTOM_FILE (1 << 21)
685                                         /* don't know, if anybody used that this way, but just in case, upgrade to new way... */
686                                         if ((seq->flag & SEQ_USE_PROXY_CUSTOM_FILE) &&
687                                            !(seq->flag & SEQ_USE_PROXY_CUSTOM_DIR))
688                                         {
689                                                 BLI_snprintf(seq->strip->proxy->dir, FILE_MAXDIR, "%s/BL_proxy", seq->strip->dir);
690                                         }
691 #undef SEQ_USE_PROXY_CUSTOM_DIR
692 #undef SEQ_USE_PROXY_CUSTOM_FILE
693                                 }
694                                 SEQ_END
695                         }
696                 }
697
698                 for (screen = main->screen.first; screen; screen = screen->id.next) {
699                         do_versions_windowmanager_2_50(screen);
700                         do_versions_gpencil_2_50(main, screen);
701                 }
702
703                 /* shader, composite and texture node trees have id.name empty, put something in
704                  * to have them show in RNA viewer and accessible otherwise.
705                  */
706                 for (ma = main->mat.first; ma; ma = ma->id.next) {
707                         if (ma->nodetree && ma->nodetree->id.name[0] == '\0')
708                                 strcpy(ma->nodetree->id.name, "NTShader Nodetree");
709                 }
710
711                 /* and composite trees */
712                 for (sce = main->scene.first; sce; sce = sce->id.next) {
713                         if (sce->nodetree && sce->nodetree->id.name[0] == '\0')
714                                 strcpy(sce->nodetree->id.name, "NTCompositing Nodetree");
715
716                         /* move to cameras */
717                         if (sce->r.mode & R_PANORAMA) {
718                                 for (base = sce->base.first; base; base = base->next) {
719                                         ob = blo_do_versions_newlibadr(fd, lib, base->object);
720
721                                         if (ob->type == OB_CAMERA && !ob->id.lib) {
722                                                 cam = blo_do_versions_newlibadr(fd, lib, ob->data);
723                                                 cam->flag |= CAM_PANORAMA;
724                                         }
725                                 }
726
727                                 sce->r.mode &= ~R_PANORAMA;
728                         }
729                 }
730
731                 /* and texture trees */
732                 for (tx = main->tex.first; tx; tx = tx->id.next) {
733                         bNode *node;
734
735                         if (tx->nodetree) {
736                                 if (tx->nodetree->id.name[0] == '\0')
737                                         strcpy(tx->nodetree->id.name, "NTTexture Nodetree");
738
739                                 /* which_output 0 is now "not specified" */
740                                 for (node = tx->nodetree->nodes.first; node; node = node->next)
741                                         if (node->type == TEX_NODE_OUTPUT)
742                                                 node->custom1++;
743                         }
744                 }
745
746                 /* copy standard draw flag to meshes(used to be global, is not available here) */
747                 for (me = main->mesh.first; me; me = me->id.next) {
748                         me->drawflag = ME_DRAWEDGES|ME_DRAWFACES|ME_DRAWCREASES;
749                 }
750
751                 /* particle draw and render types */
752                 for (part = main->particle.first; part; part = part->id.next) {
753                         if (part->draw_as) {
754                                 if (part->draw_as == PART_DRAW_DOT) {
755                                         part->ren_as = PART_DRAW_HALO;
756                                         part->draw_as = PART_DRAW_REND;
757                                 }
758                                 else if (part->draw_as <= PART_DRAW_AXIS) {
759                                         part->ren_as = PART_DRAW_HALO;
760                                 }
761                                 else {
762                                         part->ren_as = part->draw_as;
763                                         part->draw_as = PART_DRAW_REND;
764                                 }
765                         }
766                         part->path_end = 1.0f;
767                         part->clength = 1.0f;
768                 }
769
770                 /* set old pointcaches to have disk cache flag */
771                 for (ob = main->object.first; ob; ob = ob->id.next) {
772
773                         //BKE_ptcache_ids_from_object(&pidlist, ob);
774
775                         //for (pid = pidlist.first; pid; pid = pid->next)
776                         //      pid->cache->flag |= PTCACHE_DISK_CACHE;
777
778                         //BLI_freelistN(&pidlist);
779                 }
780
781                 /* type was a mixed flag & enum. move the 2d flag elsewhere */
782                 for (cu = main->curve.first; cu; cu = cu->id.next) {
783                         Nurb *nu;
784
785                         for (nu = cu->nurb.first; nu; nu = nu->next) {
786                                 nu->flag |= (nu->type & CU_2D);
787                                 nu->type &= CU_TYPE;
788                         }
789                 }
790         }
791
792         if (main->versionfile < 250 || (main->versionfile == 250 && main->subversionfile < 1)) {
793                 Object *ob;
794                 Tex *tex;
795                 Scene *sce;
796                 ToolSettings *ts;
797                 //PTCacheID *pid;
798                 //ListBase pidlist;
799
800                 for (ob = main->object.first; ob; ob = ob->id.next) {
801                         //BKE_ptcache_ids_from_object(&pidlist, ob);
802
803                         //for (pid = pidlist.first; pid; pid = pid->next) {
804                         //      if (BLI_listbase_is_empty(pid->ptcaches))
805                         //              pid->ptcaches->first = pid->ptcaches->last = pid->cache;
806                         //}
807
808                         //BLI_freelistN(&pidlist);
809
810                         if (ob->type == OB_MESH) {
811                                 Mesh *me = blo_do_versions_newlibadr(fd, lib, ob->data);
812                                 void *olddata = ob->data;
813                                 ob->data = me;
814
815                                 /* XXX - library meshes crash on loading most yoFrankie levels,
816                                  * the multires pointer gets invalid -  Campbell */
817                                 if (me && me->id.lib == NULL && me->mr && me->mr->level_count > 1) {
818                                         multires_load_old(ob, me);
819                                 }
820
821                                 ob->data = olddata;
822                         }
823
824                         if (ob->totcol && ob->matbits == NULL) {
825                                 int a;
826
827                                 ob->matbits = MEM_calloc_arrayN(ob->totcol, sizeof(char), "ob->matbits");
828                                 for (a = 0; a < ob->totcol; a++)
829                                         ob->matbits[a] = (ob->colbits & (1<<a)) != 0;
830                         }
831                 }
832
833                 /* texture filter */
834                 for (tex = main->tex.first; tex; tex = tex->id.next) {
835                         if (tex->afmax == 0)
836                                 tex->afmax = 8;
837                 }
838
839                 for (sce = main->scene.first; sce; sce = sce->id.next) {
840                         ts = sce->toolsettings;
841                         if (!ts->uv_selectmode || ts->vgroup_weight == 0.0f) {
842                                 ts->selectmode = SCE_SELECT_VERTEX;
843
844                                 /* autokeying - setting should be taken from the user-prefs
845                                  * but the userprefs version may not have correct flags set
846                                  * (i.e. will result in blank box when enabled)
847                                  */
848                                 ts->autokey_mode = U.autokey_mode;
849                                 if (ts->autokey_mode == 0)
850                                         ts->autokey_mode = 2; /* 'add/replace' but not on */
851                                 ts->uv_selectmode = UV_SELECT_VERTEX;
852                                 ts->vgroup_weight = 1.0f;
853                         }
854                 }
855         }
856
857         if (main->versionfile < 250 || (main->versionfile == 250 && main->subversionfile < 2)) {
858                 Object *ob;
859
860                 for (ob = main->object.first; ob; ob = ob->id.next) {
861                         if (ob->flag & 8192) // OB_POSEMODE = 8192
862                                 ob->mode |= OB_MODE_POSE;
863                 }
864         }
865
866         if (main->versionfile < 250 || (main->versionfile == 250 && main->subversionfile < 4)) {
867                 Scene *sce;
868                 Object *ob;
869                 ParticleSettings *part;
870                 bool do_gravity = false;
871
872                 for (sce = main->scene.first; sce; sce = sce->id.next)
873                         if (sce->unit.scale_length == 0.0f)
874                                 sce->unit.scale_length = 1.0f;
875
876                 for (ob = main->object.first; ob; ob = ob->id.next) {
877                         /* fluid-sim stuff */
878                         FluidsimModifierData *fluidmd = (FluidsimModifierData *) modifiers_findByType(ob, eModifierType_Fluidsim);
879                         if (fluidmd)
880                                 fluidmd->fss->fmd = fluidmd;
881
882                         /* rotation modes were added, but old objects would now default to being 'quaternion based' */
883                         ob->rotmode = ROT_MODE_EUL;
884                 }
885
886                 for (sce = main->scene.first; sce; sce = sce->id.next) {
887                         if (sce->audio.main == 0.0f)
888                                 sce->audio.main = 1.0f;
889
890                         sce->r.ffcodecdata.audio_mixrate = sce->audio.mixrate;
891                         sce->r.ffcodecdata.audio_volume = sce->audio.main;
892                         sce->audio.distance_model = 2;
893                         sce->audio.doppler_factor = 1.0f;
894                         sce->audio.speed_of_sound = 343.3f;
895                 }
896
897                 /* Add default gravity to scenes */
898                 for (sce = main->scene.first; sce; sce = sce->id.next) {
899                         if ((sce->physics_settings.flag & PHYS_GLOBAL_GRAVITY) == 0 &&
900                             is_zero_v3(sce->physics_settings.gravity))
901                         {
902                                 sce->physics_settings.gravity[0] = sce->physics_settings.gravity[1] = 0.0f;
903                                 sce->physics_settings.gravity[2] = -9.81f;
904                                 sce->physics_settings.flag = PHYS_GLOBAL_GRAVITY;
905                                 do_gravity = true;
906                         }
907                 }
908
909                 /* Assign proper global gravity weights for dynamics (only z-coordinate is taken into account) */
910                 if (do_gravity) {
911                         for (part = main->particle.first; part; part = part->id.next)
912                                 part->effector_weights->global_gravity = part->acc[2]/-9.81f;
913                 }
914
915                 for (ob = main->object.first; ob; ob = ob->id.next) {
916                         ModifierData *md;
917
918                         if (do_gravity) {
919                                 for (md = ob->modifiers.first; md; md = md->next) {
920                                         ClothModifierData *clmd = (ClothModifierData *) modifiers_findByType(ob, eModifierType_Cloth);
921                                         if (clmd)
922                                                 clmd->sim_parms->effector_weights->global_gravity = clmd->sim_parms->gravity[2]/-9.81f;
923                                 }
924
925                                 if (ob->soft)
926                                         ob->soft->effector_weights->global_gravity = ob->soft->grav/9.81f;
927                         }
928
929                         /* Normal wind shape is plane */
930                         if (ob->pd) {
931                                 if (ob->pd->forcefield == PFIELD_WIND)
932                                         ob->pd->shape = PFIELD_SHAPE_PLANE;
933
934                                 if (ob->pd->flag & PFIELD_PLANAR)
935                                         ob->pd->shape = PFIELD_SHAPE_PLANE;
936                                 else if (ob->pd->flag & PFIELD_SURFACE)
937                                         ob->pd->shape = PFIELD_SHAPE_SURFACE;
938
939                                 ob->pd->flag |= PFIELD_DO_LOCATION;
940                         }
941                 }
942         }
943
944         if (main->versionfile < 250 || (main->versionfile == 250 && main->subversionfile < 6)) {
945                 Object *ob;
946
947                 /* New variables for axis-angle rotations and/or quaternion rotations were added, and need proper initialization */
948                 for (ob = main->object.first; ob; ob = ob->id.next) {
949                         /* new variables for all objects */
950                         ob->quat[0] = 1.0f;
951                         ob->rotAxis[1] = 1.0f;
952
953                         /* bones */
954                         if (ob->pose) {
955                                 bPoseChannel *pchan;
956
957                                 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
958                                         /* just need to initalise rotation axis properly... */
959                                         pchan->rotAxis[1] = 1.0f;
960                                 }
961                         }
962                 }
963         }
964
965         if (main->versionfile < 250 || (main->versionfile == 250 && main->subversionfile < 7)) {
966                 Mesh *me;
967                 Nurb *nu;
968                 Lattice *lt;
969                 Curve *cu;
970                 Key *key;
971                 const float *data;
972                 int a, tot;
973
974                 /* shape keys are no longer applied to the mesh itself, but rather
975                  * to the derivedmesh/displist, so here we ensure that the basis
976                  * shape key is always set in the mesh coordinates. */
977                 for (me = main->mesh.first; me; me = me->id.next) {
978                         if ((key = blo_do_versions_newlibadr(fd, lib, me->key)) && key->refkey) {
979                                 data = key->refkey->data;
980                                 tot = MIN2(me->totvert, key->refkey->totelem);
981
982                                 for (a = 0; a < tot; a++, data += 3)
983                                         copy_v3_v3(me->mvert[a].co, data);
984                         }
985                 }
986
987                 for (lt = main->latt.first; lt; lt = lt->id.next) {
988                         if ((key = blo_do_versions_newlibadr(fd, lib, lt->key)) && key->refkey) {
989                                 data = key->refkey->data;
990                                 tot = MIN2(lt->pntsu*lt->pntsv*lt->pntsw, key->refkey->totelem);
991
992                                 for (a = 0; a < tot; a++, data += 3)
993                                         copy_v3_v3(lt->def[a].vec, data);
994                         }
995                 }
996
997                 for (cu = main->curve.first; cu; cu = cu->id.next) {
998                         if ((key = blo_do_versions_newlibadr(fd, lib, cu->key)) && key->refkey) {
999                                 data = key->refkey->data;
1000
1001                                 for (nu = cu->nurb.first; nu; nu = nu->next) {
1002                                         if (nu->bezt) {
1003                                                 BezTriple *bezt = nu->bezt;
1004
1005                                                 for (a = 0; a < nu->pntsu; a++, bezt++) {
1006                                                         copy_v3_v3(bezt->vec[0], data); data+=3;
1007                                                         copy_v3_v3(bezt->vec[1], data); data+=3;
1008                                                         copy_v3_v3(bezt->vec[2], data); data+=3;
1009                                                         bezt->alfa = *data; data++;
1010                                                 }
1011                                         }
1012                                         else if (nu->bp) {
1013                                                 BPoint *bp = nu->bp;
1014
1015                                                 for (a = 0; a < nu->pntsu*nu->pntsv; a++, bp++) {
1016                                                         copy_v3_v3(bp->vec, data); data += 3;
1017                                                         bp->alfa = *data; data++;
1018                                                 }
1019                                         }
1020                                 }
1021                         }
1022                 }
1023         }
1024
1025         if (main->versionfile < 250 || (main->versionfile == 250 && main->subversionfile < 8)) {
1026                 {
1027                         Scene *sce = main->scene.first;
1028                         while (sce) {
1029                                 if (sce->r.frame_step == 0)
1030                                         sce->r.frame_step = 1;
1031
1032                                 if (sce->ed && sce->ed->seqbase.first) {
1033                                         do_versions_seq_unique_name_all_strips(sce, &sce->ed->seqbase);
1034                                 }
1035
1036                                 sce = sce->id.next;
1037                         }
1038                 }
1039
1040                 {
1041                         /* ensure all nodes have unique names */
1042                         bNodeTree *ntree = main->nodetree.first;
1043                         while (ntree) {
1044                                 bNode *node = ntree->nodes.first;
1045
1046                                 while (node) {
1047                                         nodeUniqueName(ntree, node);
1048                                         node = node->next;
1049                                 }
1050
1051                                 ntree = ntree->id.next;
1052                         }
1053                 }
1054
1055                 {
1056                         Object *ob = main->object.first;
1057                         while (ob) {
1058                                 /* shaded mode disabled for now */
1059                                 if (ob->dt == OB_MATERIAL)
1060                                         ob->dt = OB_TEXTURE;
1061                                 ob = ob->id.next;
1062                         }
1063                 }
1064
1065                 {
1066                         bScreen *screen;
1067                         ScrArea *sa;
1068                         SpaceLink *sl;
1069
1070                         for (screen = main->screen.first; screen; screen = screen->id.next) {
1071                                 for (sa = screen->areabase.first; sa; sa = sa->next) {
1072                                         for (sl = sa->spacedata.first; sl; sl = sl->next) {
1073                                                 if (sl->spacetype == SPACE_VIEW3D) {
1074                                                         View3D *v3d = (View3D *) sl;
1075                                                         if (v3d->drawtype == OB_MATERIAL)
1076                                                                 v3d->drawtype = OB_SOLID;
1077                                                 }
1078                                         }
1079                                 }
1080                         }
1081                 }
1082
1083                 /* only convert old 2.50 files with color management */
1084                 if (main->versionfile == 250) {
1085                         Scene *sce = main->scene.first;
1086                         Material *ma = main->mat.first;
1087                         Tex *tex = main->tex.first;
1088                         int i, convert = 0;
1089
1090                         /* convert to new color management system:
1091                          * while previously colors were stored as srgb,
1092                          * now they are stored as linear internally,
1093                          * with screen gamma correction in certain places in the UI. */
1094
1095                         /* don't know what scene is active, so we'll convert if any scene has it enabled... */
1096                         while (sce) {
1097                                 if (sce->r.color_mgt_flag & R_COLOR_MANAGEMENT)
1098                                         convert = 1;
1099                                 sce = sce->id.next;
1100                         }
1101
1102                         if (convert) {
1103                                 while (ma) {
1104                                         srgb_to_linearrgb_v3_v3(&ma->r, &ma->r);
1105                                         srgb_to_linearrgb_v3_v3(&ma->specr, &ma->specr);
1106                                         ma = ma->id.next;
1107                                 }
1108
1109                                 while (tex) {
1110                                         if (tex->coba) {
1111                                                 ColorBand *band = (ColorBand *)tex->coba;
1112                                                 for (i = 0; i < band->tot; i++) {
1113                                                         CBData *data = band->data + i;
1114                                                         srgb_to_linearrgb_v3_v3(&data->r, &data->r);
1115                                                 }
1116                                         }
1117                                         tex = tex->id.next;
1118                                 }
1119                         }
1120                 }
1121         }
1122
1123         if (main->versionfile < 250 || (main->versionfile == 250 && main->subversionfile < 9)) {
1124                 Scene *sce;
1125                 Mesh *me;
1126                 Object *ob;
1127
1128                 for (sce = main->scene.first; sce; sce = sce->id.next)
1129                         if (!sce->toolsettings->particle.selectmode)
1130                                 sce->toolsettings->particle.selectmode = SCE_SELECT_PATH;
1131
1132                 if (main->versionfile == 250 && main->subversionfile > 1) {
1133                         for (me = main->mesh.first; me; me = me->id.next)
1134                                 multires_load_old_250(me);
1135
1136                         for (ob = main->object.first; ob; ob = ob->id.next) {
1137                                 MultiresModifierData *mmd = (MultiresModifierData *) modifiers_findByType(ob, eModifierType_Multires);
1138
1139                                 if (mmd) {
1140                                         mmd->totlvl--;
1141                                         mmd->lvl--;
1142                                         mmd->sculptlvl = mmd->lvl;
1143                                         mmd->renderlvl = mmd->lvl;
1144                                 }
1145                         }
1146                 }
1147         }
1148
1149         if (main->versionfile < 250 || (main->versionfile == 250 && main->subversionfile < 10)) {
1150                 Object *ob;
1151
1152                 /* properly initialize hair clothsim data on old files */
1153                 for (ob = main->object.first; ob; ob = ob->id.next) {
1154                         ModifierData *md;
1155                         for (md = ob->modifiers.first; md; md = md->next) {
1156                                 if (md->type == eModifierType_Cloth) {
1157                                         ClothModifierData *clmd = (ClothModifierData *)md;
1158                                         if (clmd->sim_parms->velocity_smooth < 0.01f)
1159                                                 clmd->sim_parms->velocity_smooth = 0.f;
1160                                 }
1161                         }
1162                 }
1163         }
1164
1165         /* fix bad area setup in subversion 10 */
1166         if (main->versionfile == 250 && main->subversionfile == 10) {
1167                 /* fix for new view type in sequencer */
1168                 bScreen *screen;
1169                 ScrArea *sa;
1170                 SpaceLink *sl;
1171
1172                 /* remove all preview window in wrong spaces */
1173                 for (screen = main->screen.first; screen; screen = screen->id.next) {
1174                         for (sa = screen->areabase.first; sa; sa = sa->next) {
1175                                 for (sl = sa->spacedata.first; sl; sl = sl->next) {
1176                                         if (sl->spacetype != SPACE_SEQ) {
1177                                                 ARegion *ar;
1178                                                 ListBase *regionbase;
1179
1180                                                 if (sl == sa->spacedata.first) {
1181                                                         regionbase = &sa->regionbase;
1182                                                 }
1183                                                 else {
1184                                                         regionbase = &sl->regionbase;
1185                                                 }
1186
1187                                                 for (ar = regionbase->first; ar; ar = ar->next) {
1188                                                         if (ar->regiontype == RGN_TYPE_PREVIEW)
1189                                                                 break;
1190                                                 }
1191
1192                                                 if (ar && (ar->regiontype == RGN_TYPE_PREVIEW)) {
1193                                                         SpaceType *st = BKE_spacetype_from_id(SPACE_SEQ);
1194                                                         BKE_area_region_free(st, ar);
1195                                                         BLI_freelinkN(regionbase, ar);
1196                                                 }
1197                                         }
1198                                 }
1199                         }
1200                 }
1201         }
1202
1203         if (main->versionfile < 250 || (main->versionfile == 250 && main->subversionfile < 11)) {
1204                 {
1205                         /* fix for new view type in sequencer */
1206                         bScreen *screen;
1207                         ScrArea *sa;
1208                         SpaceLink *sl;
1209
1210                         for (screen = main->screen.first; screen; screen = screen->id.next) {
1211                                 for (sa = screen->areabase.first; sa; sa = sa->next) {
1212                                         for (sl = sa->spacedata.first; sl; sl = sl->next) {
1213                                                 if (sl->spacetype == SPACE_SEQ) {
1214                                                         ARegion *ar;
1215                                                         ARegion *ar_main;
1216                                                         ListBase *regionbase;
1217                                                         SpaceSeq *sseq = (SpaceSeq *)sl;
1218
1219                                                         if (sl == sa->spacedata.first) {
1220                                                                 regionbase = &sa->regionbase;
1221                                                         }
1222                                                         else {
1223                                                                 regionbase = &sl->regionbase;
1224                                                         }
1225
1226                                                         if (sseq->view == 0)
1227                                                                 sseq->view = SEQ_VIEW_SEQUENCE;
1228                                                         if (sseq->mainb == 0)
1229                                                                 sseq->mainb = SEQ_DRAW_IMG_IMBUF;
1230
1231                                                         ar_main = (ARegion*)regionbase->first;
1232                                                         for (; ar_main; ar_main = ar_main->next) {
1233                                                                 if (ar_main->regiontype == RGN_TYPE_WINDOW)
1234                                                                         break;
1235                                                         }
1236                                                         ar = MEM_callocN(sizeof(ARegion), "preview area for sequencer");
1237                                                         BLI_insertlinkbefore(regionbase, ar_main, ar);
1238                                                         sequencer_init_preview_region(ar);
1239                                                 }
1240                                         }
1241                                 }
1242                         }
1243                 }
1244         }
1245
1246         if (main->versionfile < 250 || (main->versionfile == 250 && main->subversionfile < 12)) {
1247                 Object *ob;
1248                 Brush *brush;
1249
1250                 /* anim viz changes */
1251                 for (ob = main->object.first; ob; ob = ob->id.next) {
1252                         /* initialize object defaults */
1253                         animviz_settings_init(&ob->avs);
1254
1255                         /* if armature, copy settings for pose from armature data
1256                          * performing initialization where appropriate
1257                          */
1258                         if (ob->pose && ob->data) {
1259                                 bArmature *arm = blo_do_versions_newlibadr(fd, lib, ob->data);
1260                                 if (arm) { /* XXX - why does this fail in some cases? */
1261                                         bAnimVizSettings *avs = &ob->pose->avs;
1262
1263                                         /* ghosting settings ---------------- */
1264                                         /* ranges */
1265                                         avs->ghost_bc = avs->ghost_ac = arm->ghostep;
1266
1267                                         avs->ghost_sf = arm->ghostsf;
1268                                         avs->ghost_ef = arm->ghostef;
1269                                         if ((avs->ghost_sf == avs->ghost_ef) && (avs->ghost_sf == 0)) {
1270                                                 avs->ghost_sf = 1;
1271                                                 avs->ghost_ef = 100;
1272                                         }
1273
1274                                         /* type */
1275                                         if (arm->ghostep == 0)
1276                                                 avs->ghost_type = GHOST_TYPE_NONE;
1277                                         else
1278                                                 avs->ghost_type = arm->ghosttype + 1;
1279
1280                                         /* stepsize */
1281                                         avs->ghost_step = arm->ghostsize;
1282                                         if (avs->ghost_step == 0)
1283                                                 avs->ghost_step = 1;
1284
1285                                         /* path settings --------------------- */
1286                                         /* ranges */
1287                                         avs->path_bc = arm->pathbc;
1288                                         avs->path_ac = arm->pathac;
1289                                         if ((avs->path_bc == avs->path_ac) && (avs->path_bc == 0))
1290                                                 avs->path_bc = avs->path_ac = 10;
1291
1292                                         avs->path_sf = arm->pathsf;
1293                                         avs->path_ef = arm->pathef;
1294                                         if ((avs->path_sf == avs->path_ef) && (avs->path_sf == 0)) {
1295                                                 avs->path_sf = 1;
1296                                                 avs->path_ef = 250;
1297                                         }
1298
1299                                         /* flags */
1300                                         if (arm->pathflag & ARM_PATH_FNUMS)
1301                                                 avs->path_viewflag |= MOTIONPATH_VIEW_FNUMS;
1302                                         if (arm->pathflag & ARM_PATH_KFRAS)
1303                                                 avs->path_viewflag |= MOTIONPATH_VIEW_KFRAS;
1304                                         if (arm->pathflag & ARM_PATH_KFNOS)
1305                                                 avs->path_viewflag |= MOTIONPATH_VIEW_KFNOS;
1306
1307                                         /* bake flags */
1308                                         if (arm->pathflag & ARM_PATH_HEADS)
1309                                                 avs->path_bakeflag |= MOTIONPATH_BAKE_HEADS;
1310
1311                                         /* type */
1312                                         if (arm->pathflag & ARM_PATH_ACFRA)
1313                                                 avs->path_type = MOTIONPATH_TYPE_ACFRA;
1314
1315                                         /* stepsize */
1316                                         avs->path_step = arm->pathsize;
1317                                         if (avs->path_step == 0)
1318                                                 avs->path_step = 1;
1319                                 }
1320                                 else
1321                                         animviz_settings_init(&ob->pose->avs);
1322                         }
1323                 }
1324
1325                 /* brush texture changes */
1326                 for (brush = main->brush.first; brush; brush = brush->id.next) {
1327                         BKE_texture_mtex_default(&brush->mtex);
1328                         BKE_texture_mtex_default(&brush->mask_mtex);
1329                 }
1330         }
1331
1332         if (main->versionfile < 250 || (main->versionfile == 250 && main->subversionfile < 13)) {
1333                 /* NOTE: if you do more conversion, be sure to do it outside of this and
1334                  * increase subversion again, otherwise it will not be correct */
1335                 Object *ob;
1336
1337                 /* convert degrees to radians for internal use */
1338                 for (ob = main->object.first; ob; ob = ob->id.next) {
1339                         bPoseChannel *pchan;
1340
1341                         do_version_constraints_radians_degrees_250(&ob->constraints);
1342
1343                         if (ob->pose) {
1344                                 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
1345                                         pchan->limitmin[0] *= (float)(M_PI / 180.0);
1346                                         pchan->limitmin[1] *= (float)(M_PI / 180.0);
1347                                         pchan->limitmin[2] *= (float)(M_PI / 180.0);
1348                                         pchan->limitmax[0] *= (float)(M_PI / 180.0);
1349                                         pchan->limitmax[1] *= (float)(M_PI / 180.0);
1350                                         pchan->limitmax[2] *= (float)(M_PI / 180.0);
1351
1352                                         do_version_constraints_radians_degrees_250(&pchan->constraints);
1353                                 }
1354                         }
1355                 }
1356         }
1357
1358         if (main->versionfile < 250 || (main->versionfile == 250 && main->subversionfile < 14)) {
1359                 /* fix for bad View2D extents for Animation Editors */
1360                 bScreen *screen;
1361                 ScrArea *sa;
1362                 SpaceLink *sl;
1363
1364                 for (screen = main->screen.first; screen; screen = screen->id.next) {
1365                         for (sa = screen->areabase.first; sa; sa = sa->next) {
1366                                 for (sl = sa->spacedata.first; sl; sl = sl->next) {
1367                                         ListBase *regionbase;
1368                                         ARegion *ar;
1369
1370                                         if (sl == sa->spacedata.first)
1371                                                 regionbase = &sa->regionbase;
1372                                         else
1373                                                 regionbase = &sl->regionbase;
1374
1375                                         if (ELEM(sl->spacetype, SPACE_ACTION, SPACE_NLA)) {
1376                                                 for (ar = (ARegion*) regionbase->first; ar; ar = ar->next) {
1377                                                         if (ar->regiontype == RGN_TYPE_WINDOW) {
1378                                                                 ar->v2d.cur.ymax = ar->v2d.tot.ymax = 0.0f;
1379                                                                 ar->v2d.cur.ymin = ar->v2d.tot.ymin = (float)(-sa->winy) / 3.0f;
1380                                                         }
1381                                                 }
1382                                         }
1383                                 }
1384                         }
1385                 }
1386         }
1387
1388         if (main->versionfile < 250 || (main->versionfile == 250 && main->subversionfile < 17)) {
1389                 Scene *sce;
1390                 Sequence *seq;
1391
1392                 /* initialize to sane default so toggling on border shows something */
1393                 for (sce = main->scene.first; sce; sce = sce->id.next) {
1394                         if (sce->r.border.xmin == 0.0f && sce->r.border.ymin == 0.0f &&
1395                             sce->r.border.xmax == 0.0f && sce->r.border.ymax == 0.0f)
1396                         {
1397                                 sce->r.border.xmin = 0.0f;
1398                                 sce->r.border.ymin = 0.0f;
1399                                 sce->r.border.xmax = 1.0f;
1400                                 sce->r.border.ymax = 1.0f;
1401                         }
1402
1403                         if ((sce->r.ffcodecdata.flags & FFMPEG_MULTIPLEX_AUDIO) == 0)
1404                                 sce->r.ffcodecdata.audio_codec = 0x0; // CODEC_ID_NONE
1405
1406                         SEQ_BEGIN (sce->ed, seq)
1407                         {
1408                                 seq->volume = 1.0f;
1409                         }
1410                         SEQ_END
1411                 }
1412
1413                 /* particle brush strength factor was changed from int to float */
1414                 for (sce = main->scene.first; sce; sce = sce->id.next) {
1415                         ParticleEditSettings *pset = &sce->toolsettings->particle;
1416                         int a;
1417
1418                         for (a = 0; a < ARRAY_SIZE(pset->brush); a++)
1419                                 pset->brush[a].strength /= 100.0f;
1420                 }
1421
1422                 /* sequencer changes */
1423                 {
1424                         bScreen *screen;
1425                         ScrArea *sa;
1426                         SpaceLink *sl;
1427
1428                         for (screen = main->screen.first; screen; screen = screen->id.next) {
1429                                 for (sa = screen->areabase.first; sa; sa = sa->next) {
1430                                         for (sl = sa->spacedata.first; sl; sl = sl->next) {
1431                                                 if (sl->spacetype == SPACE_SEQ) {
1432                                                         ARegion *ar_preview;
1433                                                         ListBase *regionbase;
1434
1435                                                         if (sl == sa->spacedata.first) {
1436                                                                 regionbase = &sa->regionbase;
1437                                                         }
1438                                                         else {
1439                                                                 regionbase = &sl->regionbase;
1440                                                         }
1441
1442                                                         ar_preview = (ARegion*) regionbase->first;
1443                                                         for (; ar_preview; ar_preview = ar_preview->next) {
1444                                                                 if (ar_preview->regiontype == RGN_TYPE_PREVIEW)
1445                                                                         break;
1446                                                         }
1447                                                         if (ar_preview && (ar_preview->regiontype == RGN_TYPE_PREVIEW)) {
1448                                                                 sequencer_init_preview_region(ar_preview);
1449                                                         }
1450                                                 }
1451                                         }
1452                                 }
1453                         }
1454                 } /* sequencer changes */
1455         }
1456
1457         if (main->versionfile <= 251) { /* 2.5.1 had no subversions */
1458                 bScreen *sc;
1459
1460                 /* Blender 2.5.2 - subversion 0 introduced a new setting: V3D_RENDER_OVERRIDE.
1461                  * This bit was used in the past for V3D_TRANSFORM_SNAP, which is now deprecated.
1462                  * Here we clear it for old files so they don't come in with V3D_RENDER_OVERRIDE set,
1463                  * which would cause cameras, lamps, etc to become invisible */
1464                 for (sc = main->screen.first; sc; sc = sc->id.next) {
1465                         ScrArea *sa;
1466                         for (sa = sc->areabase.first; sa; sa = sa->next) {
1467                                 SpaceLink *sl;
1468                                 for (sl = sa->spacedata.first; sl; sl = sl->next) {
1469                                         if (sl->spacetype == SPACE_VIEW3D) {
1470                                                 View3D *v3d = (View3D *)sl;
1471                                                 v3d->flag2 &= ~V3D_RENDER_OVERRIDE;
1472                                         }
1473                                 }
1474                         }
1475                 }
1476         }
1477
1478         if (main->versionfile < 252 || (main->versionfile == 252 && main->subversionfile < 1)) {
1479                 Brush *brush;
1480                 Object *ob;
1481                 Scene *scene;
1482                 bNodeTree *ntree;
1483
1484                 for (brush = main->brush.first; brush; brush = brush->id.next) {
1485                         if (brush->curve)
1486                                 brush->curve->preset = CURVE_PRESET_SMOOTH;
1487                 }
1488
1489                 /* properly initialize active flag for fluidsim modifiers */
1490                 for (ob = main->object.first; ob; ob = ob->id.next) {
1491                         ModifierData *md;
1492                         for (md = ob->modifiers.first; md; md = md->next) {
1493                                 if (md->type == eModifierType_Fluidsim) {
1494                                         FluidsimModifierData *fmd = (FluidsimModifierData *) md;
1495                                         fmd->fss->flag |= OB_FLUIDSIM_ACTIVE;
1496                                         fmd->fss->flag |= OB_FLUIDSIM_OVERRIDE_TIME;
1497                                 }
1498                         }
1499                 }
1500
1501                 /* adjustment to color balance node values */
1502                 for (scene = main->scene.first; scene; scene = scene->id.next) {
1503                         if (scene->nodetree) {
1504                                 bNode *node = scene->nodetree->nodes.first;
1505
1506                                 while (node) {
1507                                         if (node->type == CMP_NODE_COLORBALANCE) {
1508                                                 NodeColorBalance *n = (NodeColorBalance *) node->storage;
1509                                                 n->lift[0] += 1.f;
1510                                                 n->lift[1] += 1.f;
1511                                                 n->lift[2] += 1.f;
1512                                         }
1513                                         node = node->next;
1514                                 }
1515                         }
1516                 }
1517                 /* check inside node groups too */
1518                 for (ntree = main->nodetree.first; ntree; ntree = ntree->id.next) {
1519                         bNode *node = ntree->nodes.first;
1520
1521                         while (node) {
1522                                 if (node->type == CMP_NODE_COLORBALANCE) {
1523                                         NodeColorBalance *n = (NodeColorBalance *) node->storage;
1524                                         n->lift[0] += 1.f;
1525                                         n->lift[1] += 1.f;
1526                                         n->lift[2] += 1.f;
1527                                 }
1528
1529                                 node = node->next;
1530                         }
1531                 }
1532         }
1533
1534         /* old-track -> constraints (this time we're really doing it!) */
1535         if (main->versionfile < 252 || (main->versionfile == 252 && main->subversionfile < 2)) {
1536                 Object *ob;
1537
1538                 for (ob = main->object.first; ob; ob = ob->id.next)
1539                         blo_do_version_old_trackto_to_constraints(ob);
1540         }
1541
1542         if (main->versionfile < 252 || (main->versionfile == 252 && main->subversionfile < 5)) {
1543                 bScreen *sc;
1544
1545                 /* Image editor scopes */
1546                 for (sc = main->screen.first; sc; sc = sc->id.next) {
1547                         ScrArea *sa;
1548
1549                         for (sa = sc->areabase.first; sa; sa = sa->next) {
1550                                 SpaceLink *sl;
1551
1552                                 for (sl = sa->spacedata.first; sl; sl = sl->next) {
1553                                         if (sl->spacetype == SPACE_IMAGE) {
1554                                                 SpaceImage *sima = (SpaceImage *) sl;
1555                                                 scopes_new(&sima->scopes);
1556                                         }
1557                                 }
1558                         }
1559                 }
1560         }
1561
1562         if (main->versionfile < 253) {
1563                 Object *ob;
1564                 Scene *scene;
1565                 bScreen *sc;
1566                 Tex *tex;
1567                 Brush *brush;
1568
1569                 for (sc = main->screen.first; sc; sc = sc->id.next) {
1570                         ScrArea *sa;
1571                         for (sa = sc->areabase.first; sa; sa = sa->next) {
1572                                 SpaceLink *sl;
1573
1574                                 for (sl = sa->spacedata.first; sl; sl = sl->next) {
1575                                         if (sl->spacetype == SPACE_NODE) {
1576                                                 SpaceNode *snode = (SpaceNode *) sl;
1577                                                 ListBase *regionbase;
1578                                                 ARegion *ar;
1579
1580                                                 if (sl == sa->spacedata.first)
1581                                                         regionbase = &sa->regionbase;
1582                                                 else
1583                                                         regionbase = &sl->regionbase;
1584
1585                                                 if (snode->v2d.minzoom > 0.09f)
1586                                                         snode->v2d.minzoom = 0.09f;
1587                                                 if (snode->v2d.maxzoom < 2.31f)
1588                                                         snode->v2d.maxzoom = 2.31f;
1589
1590                                                 for (ar = regionbase->first; ar; ar = ar->next) {
1591                                                         if (ar->regiontype == RGN_TYPE_WINDOW) {
1592                                                                 if (ar->v2d.minzoom > 0.09f)
1593                                                                         ar->v2d.minzoom = 0.09f;
1594                                                                 if (ar->v2d.maxzoom < 2.31f)
1595                                                                         ar->v2d.maxzoom = 2.31f;
1596                                                         }
1597                                                 }
1598                                         }
1599                                 }
1600                         }
1601                 }
1602
1603                 do_version_mdef_250(main);
1604
1605                 /* parent type to modifier */
1606                 for (ob = main->object.first; ob; ob = ob->id.next) {
1607                         if (ob->parent) {
1608                                 Object *parent = (Object *) blo_do_versions_newlibadr(fd, lib, ob->parent);
1609                                 if (parent) { /* parent may not be in group */
1610                                         if (parent->type == OB_ARMATURE && ob->partype == PARSKEL) {
1611                                                 ArmatureModifierData *amd;
1612                                                 bArmature *arm = (bArmature *) blo_do_versions_newlibadr(fd, lib, parent->data);
1613
1614                                                 amd = (ArmatureModifierData*) modifier_new(eModifierType_Armature);
1615                                                 amd->object = ob->parent;
1616                                                 BLI_addtail((ListBase*)&ob->modifiers, amd);
1617                                                 amd->deformflag = arm->deformflag;
1618                                                 ob->partype = PAROBJECT;
1619                                         }
1620                                         else if (parent->type == OB_LATTICE && ob->partype == PARSKEL) {
1621                                                 LatticeModifierData *lmd;
1622
1623                                                 lmd = (LatticeModifierData*) modifier_new(eModifierType_Lattice);
1624                                                 lmd->object = ob->parent;
1625                                                 BLI_addtail((ListBase*)&ob->modifiers, lmd);
1626                                                 ob->partype = PAROBJECT;
1627                                         }
1628                                         else if (parent->type == OB_CURVE && ob->partype == PARCURVE) {
1629                                                 CurveModifierData *cmd;
1630
1631                                                 cmd = (CurveModifierData*) modifier_new(eModifierType_Curve);
1632                                                 cmd->object = ob->parent;
1633                                                 BLI_addtail((ListBase*)&ob->modifiers, cmd);
1634                                                 ob->partype = PAROBJECT;
1635                                         }
1636                                 }
1637                         }
1638                 }
1639
1640                 /* initialize scene active layer */
1641                 for (scene = main->scene.first; scene; scene = scene->id.next) {
1642                         int i;
1643                         for (i = 0; i < 20; i++) {
1644                                 if (scene->lay & (1<<i)) {
1645                                         scene->layact = 1<<i;
1646                                         break;
1647                                 }
1648                         }
1649                 }
1650
1651                 for (tex = main->tex.first; tex; tex = tex->id.next) {
1652                         /* if youre picky, this isn't correct until we do a version bump
1653                          * since you could set saturation to be 0.0*/
1654                         if (tex->saturation == 0.0f)
1655                                 tex->saturation = 1.0f;
1656                 }
1657
1658                 {
1659                         Curve *cu;
1660                         for (cu = main->curve.first; cu; cu = cu->id.next) {
1661                                 cu->smallcaps_scale = 0.75f;
1662                         }
1663                 }
1664
1665                 for (scene = main->scene.first; scene; scene = scene->id.next) {
1666                         if (scene) {
1667                                 Sequence *seq;
1668                                 SEQ_BEGIN (scene->ed, seq)
1669                                 {
1670                                         if (seq->sat == 0.0f) {
1671                                                 seq->sat = 1.0f;
1672                                         }
1673                                 }
1674                                 SEQ_END
1675                         }
1676                 }
1677
1678                 /* GSOC 2010 Sculpt - New settings for Brush */
1679
1680                 for (brush = main->brush.first; brush; brush = brush->id.next) {
1681                         /* Sanity Check */
1682
1683                         /* infinite number of dabs */
1684                         if (brush->spacing == 0)
1685                                 brush->spacing = 10;
1686
1687                         /* will have no effect */
1688                         if (brush->alpha == 0)
1689                                 brush->alpha = 0.5f;
1690
1691                         /* bad radius */
1692                         if (brush->unprojected_radius == 0)
1693                                 brush->unprojected_radius = 0.125f;
1694
1695                         /* unusable size */
1696                         if (brush->size == 0)
1697                                 brush->size = 35;
1698
1699                         /* can't see overlay */
1700                         if (brush->texture_overlay_alpha == 0)
1701                                 brush->texture_overlay_alpha = 33;
1702
1703                         /* same as draw brush */
1704                         if (brush->crease_pinch_factor == 0)
1705                                 brush->crease_pinch_factor = 0.5f;
1706
1707                         /* will sculpt no vertexes */
1708                         if (brush->plane_trim == 0)
1709                                 brush->plane_trim = 0.5f;
1710
1711                         /* same as smooth stroke off */
1712                         if (brush->smooth_stroke_radius == 0)
1713                                 brush->smooth_stroke_radius = 75;
1714
1715                         /* will keep cursor in one spot */
1716                         if (brush->smooth_stroke_radius == 1)
1717                                 brush->smooth_stroke_factor = 0.9f;
1718
1719                         /* same as dots */
1720                         if (brush->rate == 0)
1721                                 brush->rate = 0.1f;
1722
1723                         /* New Settings */
1724                         if (main->versionfile < 252 || (main->versionfile == 252 && main->subversionfile < 5)) {
1725                                 brush->flag |= BRUSH_SPACE_ATTEN; // explicitly enable adaptive space
1726
1727                                 /* spacing was originally in pixels, convert it to percentage for new version
1728                                  * size should not be zero due to sanity check above
1729                                  */
1730                                 brush->spacing = (int)(100 * ((float)brush->spacing) / ((float) brush->size));
1731
1732                                 if (brush->add_col[0] == 0 &&
1733                                         brush->add_col[1] == 0 &&
1734                                         brush->add_col[2] == 0)
1735                                 {
1736                                         brush->add_col[0] = 1.00f;
1737                                         brush->add_col[1] = 0.39f;
1738                                         brush->add_col[2] = 0.39f;
1739                                 }
1740
1741                                 if (brush->sub_col[0] == 0 &&
1742                                         brush->sub_col[1] == 0 &&
1743                                         brush->sub_col[2] == 0)
1744                                 {
1745                                         brush->sub_col[0] = 0.39f;
1746                                         brush->sub_col[1] = 0.39f;
1747                                         brush->sub_col[2] = 1.00f;
1748                                 }
1749                         }
1750                 }
1751         }
1752
1753         /* GSOC Sculpt 2010 - Sanity check on Sculpt/Paint settings */
1754         if (main->versionfile < 253) {
1755                 Scene *sce;
1756                 for (sce = main->scene.first; sce; sce = sce->id.next) {
1757                         if (sce->toolsettings->sculpt_paint_unified_alpha == 0)
1758                                 sce->toolsettings->sculpt_paint_unified_alpha = 0.5f;
1759
1760                         if (sce->toolsettings->sculpt_paint_unified_unprojected_radius == 0)
1761                                 sce->toolsettings->sculpt_paint_unified_unprojected_radius = 0.125f;
1762
1763                         if (sce->toolsettings->sculpt_paint_unified_size == 0)
1764                                 sce->toolsettings->sculpt_paint_unified_size = 35;
1765                 }
1766         }
1767
1768         if (main->versionfile < 253 || (main->versionfile == 253 && main->subversionfile < 1)) {
1769                 Object *ob;
1770
1771                 for (ob = main->object.first; ob; ob = ob->id.next) {
1772                         ModifierData *md;
1773
1774                         for (md = ob->modifiers.first; md; md = md->next) {
1775                                 if (md->type == eModifierType_Smoke) {
1776                                         SmokeModifierData *smd = (SmokeModifierData *)md;
1777
1778                                         if ((smd->type & MOD_SMOKE_TYPE_DOMAIN) && smd->domain) {
1779                                                 smd->domain->vorticity = 2.0f;
1780                                                 smd->domain->time_scale = 1.0f;
1781
1782                                                 if (!(smd->domain->flags & (1<<4)))
1783                                                         continue;
1784
1785                                                 /* delete old MOD_SMOKE_INITVELOCITY flag */
1786                                                 smd->domain->flags &= ~(1<<4);
1787
1788                                                 /* for now just add it to all flow objects in the scene */
1789                                                 {
1790                                                         Object *ob2;
1791                                                         for (ob2 = main->object.first; ob2; ob2 = ob2->id.next) {
1792                                                                 ModifierData *md2;
1793                                                                 for (md2 = ob2->modifiers.first; md2; md2 = md2->next) {
1794                                                                         if (md2->type == eModifierType_Smoke) {
1795                                                                                 SmokeModifierData *smd2 = (SmokeModifierData *)md2;
1796
1797                                                                                 if ((smd2->type & MOD_SMOKE_TYPE_FLOW) && smd2->flow) {
1798                                                                                         smd2->flow->flags |= MOD_SMOKE_FLOW_INITVELOCITY;
1799                                                                                 }
1800                                                                         }
1801                                                                 }
1802                                                         }
1803                                                 }
1804
1805                                         }
1806                                         else if ((smd->type & MOD_SMOKE_TYPE_FLOW) && smd->flow) {
1807                                                 smd->flow->vel_multi = 1.0f;
1808                                         }
1809                                 }
1810                         }
1811                 }
1812         }
1813
1814         if (main->versionfile < 255 || (main->versionfile == 255 && main->subversionfile < 1)) {
1815                 Brush *br;
1816                 ParticleSettings *part;
1817                 bScreen *sc;
1818
1819                 for (br = main->brush.first; br; br = br->id.next) {
1820                         if (br->ob_mode == 0)
1821                                 br->ob_mode = OB_MODE_ALL_PAINT;
1822                 }
1823
1824                 for (part = main->particle.first; part; part = part->id.next) {
1825                         if (part->boids)
1826                                 part->boids->pitch = 1.0f;
1827
1828                         part->flag &= ~PART_HAIR_REGROW; /* this was a deprecated flag before */
1829                         part->kink_amp_clump = 1.f; /* keep old files looking similar */
1830                 }
1831
1832                 for (sc = main->screen.first; sc; sc = sc->id.next) {
1833                         ScrArea *sa;
1834                         for (sa = sc->areabase.first; sa; sa = sa->next) {
1835                                 SpaceLink *sl;
1836                                 for (sl = sa->spacedata.first; sl; sl = sl->next) {
1837                                         if (sl->spacetype == SPACE_INFO) {
1838                                                 SpaceInfo *sinfo = (SpaceInfo *) sl;
1839                                                 ARegion *ar;
1840
1841                                                 sinfo->rpt_mask = INFO_RPT_OP;
1842
1843                                                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
1844                                                         if (ar->regiontype == RGN_TYPE_WINDOW) {
1845                                                                 ar->v2d.scroll = (V2D_SCROLL_RIGHT);
1846                                                                 ar->v2d.align = V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_NEG_Y; /* align bottom left */
1847                                                                 ar->v2d.keepofs = V2D_LOCKOFS_X;
1848                                                                 ar->v2d.keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_LIMITZOOM|V2D_KEEPASPECT);
1849                                                                 ar->v2d.keeptot = V2D_KEEPTOT_BOUNDS;
1850                                                                 ar->v2d.minzoom = ar->v2d.maxzoom = 1.0f;
1851                                                         }
1852                                                 }
1853                                         }
1854                                 }
1855                         }
1856                 }
1857         }
1858
1859         if (main->versionfile < 255 || (main->versionfile == 255 && main->subversionfile < 3)) {
1860                 Object *ob;
1861
1862                 /* ocean res is now squared, reset old ones - will be massive */
1863                 for (ob = main->object.first; ob; ob = ob->id.next) {
1864                         ModifierData *md;
1865                         for (md = ob->modifiers.first; md; md = md->next) {
1866                                 if (md->type == eModifierType_Ocean) {
1867                                         OceanModifierData *omd = (OceanModifierData *)md;
1868                                         omd->resolution = 7;
1869                                         omd->oceancache = NULL;
1870                                 }
1871                         }
1872                 }
1873         }
1874
1875         if (main->versionfile < 256) {
1876                 bScreen *sc;
1877                 ScrArea *sa;
1878                 Key *key;
1879
1880                 /* Fix for sample line scope initializing with no height */
1881                 for (sc = main->screen.first; sc; sc = sc->id.next) {
1882                         sa = sc->areabase.first;
1883                         while (sa) {
1884                                 SpaceLink *sl;
1885                                 for (sl = sa->spacedata.first; sl; sl = sl->next) {
1886                                         if (sl->spacetype == SPACE_IMAGE) {
1887                                                 SpaceImage *sima = (SpaceImage *) sl;
1888                                                 if (sima->sample_line_hist.height == 0)
1889                                                         sima->sample_line_hist.height = 100;
1890                                         }
1891                                 }
1892                                 sa = sa->next;
1893                         }
1894                 }
1895
1896                 /* old files could have been saved with slidermin = slidermax = 0.0, but the UI in
1897                  * 2.4x would never reveal this to users as a dummy value always ended up getting used
1898                  * instead
1899                  */
1900                 for (key = main->key.first; key; key = key->id.next) {
1901                         KeyBlock *kb;
1902
1903                         for (kb = key->block.first; kb; kb = kb->next) {
1904                                 if (IS_EQF(kb->slidermin, kb->slidermax) && IS_EQF(kb->slidermax, 0.0f))
1905                                         kb->slidermax = kb->slidermin + 1.0f;
1906                         }
1907                 }
1908         }
1909
1910         if (main->versionfile < 256 || (main->versionfile == 256 && main->subversionfile < 1)) {
1911                 /* fix for bones that didn't have arm_roll before */
1912                 bArmature *arm;
1913                 Bone *bone;
1914                 Object *ob;
1915
1916                 for (arm = main->armature.first; arm; arm = arm->id.next)
1917                         for (bone = arm->bonebase.first; bone; bone = bone->next)
1918                                 do_version_bone_roll_256(bone);
1919
1920                 /* fix for objects which have zero dquat's
1921                  * since this is multiplied with the quat rather than added */
1922                 for (ob = main->object.first; ob; ob = ob->id.next) {
1923                         if (is_zero_v4(ob->dquat)) {
1924                                 unit_qt(ob->dquat);
1925                         }
1926                         if (is_zero_v3(ob->drotAxis) && ob->drotAngle == 0.0f) {
1927                                 unit_axis_angle(ob->drotAxis, &ob->drotAngle);
1928                         }
1929                 }
1930         }
1931
1932         if (main->versionfile < 256 || (main->versionfile == 256 && main->subversionfile < 2)) {
1933                 bNodeTree *ntree;
1934                 bNode *node;
1935                 bNodeSocket *sock, *gsock;
1936                 bNodeLink *link;
1937                 
1938                 /* node sockets are not exposed automatically any more,
1939                  * this mimics the old behavior by adding all unlinked sockets to groups.
1940                  */
1941                 for (ntree=main->nodetree.first; ntree; ntree=ntree->id.next) {
1942                         /* this adds copies and links from all unlinked internal sockets to group inputs/outputs. */
1943                         
1944                         /* first make sure the own_index for new sockets is valid */
1945                         for (node=ntree->nodes.first; node; node=node->next) {
1946                                 for (sock = node->inputs.first; sock; sock = sock->next)
1947                                         if (sock->own_index >= ntree->cur_index)
1948                                                 ntree->cur_index = sock->own_index+1;
1949                                 for (sock = node->outputs.first; sock; sock = sock->next)
1950                                         if (sock->own_index >= ntree->cur_index)
1951                                                 ntree->cur_index = sock->own_index+1;
1952                         }
1953                         
1954                         /* add ntree->inputs/ntree->outputs sockets for all unlinked sockets in the group tree. */
1955                         for (node=ntree->nodes.first; node; node=node->next) {
1956                                 for (sock = node->inputs.first; sock; sock = sock->next) {
1957                                         if (!sock->link && !nodeSocketIsHidden(sock)) {
1958                                                 
1959                                                 gsock = do_versions_node_group_add_socket_2_56_2(ntree, sock->name, sock->type, SOCK_IN);
1960                                                 
1961                                                 /* initialize the default socket value */
1962                                                 copy_v4_v4(gsock->ns.vec, sock->ns.vec);
1963                                                 
1964                                                 /* XXX nodeAddLink does not work with incomplete (node==NULL) links any longer,
1965                                                  * have to create these directly here. These links are updated again in subsequent do_version!
1966                                                  */
1967                                                 link = MEM_callocN(sizeof(bNodeLink), "link");
1968                                                 BLI_addtail(&ntree->links, link);
1969                                                 link->fromnode = NULL;
1970                                                 link->fromsock = gsock;
1971                                                 link->tonode = node;
1972                                                 link->tosock = sock;
1973                                                 ntree->update |= NTREE_UPDATE_LINKS;
1974                                                 
1975                                                 sock->link = link;
1976                                         }
1977                                 }
1978                                 for (sock = node->outputs.first; sock; sock = sock->next) {
1979                                         if (nodeCountSocketLinks(ntree, sock)==0 && !nodeSocketIsHidden(sock)) {
1980                                                 gsock = do_versions_node_group_add_socket_2_56_2(ntree, sock->name, sock->type, SOCK_OUT);
1981                                                 
1982                                                 /* initialize the default socket value */
1983                                                 copy_v4_v4(gsock->ns.vec, sock->ns.vec);
1984                                                 
1985                                                 /* XXX nodeAddLink does not work with incomplete (node==NULL) links any longer,
1986                                                  * have to create these directly here. These links are updated again in subsequent do_version!
1987                                                  */
1988                                                 link = MEM_callocN(sizeof(bNodeLink), "link");
1989                                                 BLI_addtail(&ntree->links, link);
1990                                                 link->fromnode = node;
1991                                                 link->fromsock = sock;
1992                                                 link->tonode = NULL;
1993                                                 link->tosock = gsock;
1994                                                 ntree->update |= NTREE_UPDATE_LINKS;
1995                                                 
1996                                                 gsock->link = link;
1997                                         }
1998                                 }
1999                         }
2000                         
2001                         /* XXX The external group node sockets needs to adjust their own_index to point at
2002                          * associated ntree inputs/outputs internal sockets. However, this can only happen
2003                          * after lib-linking (needs access to internal node group tree)!
2004                          * Setting a temporary flag here, actual do_versions happens in lib_verify_nodetree.
2005                          */
2006                         ntree->flag |= NTREE_DO_VERSIONS_GROUP_EXPOSE_2_56_2;
2007                 }
2008         }
2009
2010         if (main->versionfile < 256 || (main->versionfile == 256 && main->subversionfile < 3)) {
2011                 bScreen *sc;
2012                 Brush *brush;
2013                 Object *ob;
2014                 ParticleSettings *part;
2015
2016                 /* redraws flag in SpaceTime has been moved to Screen level */
2017                 for (sc = main->screen.first; sc; sc = sc->id.next) {
2018                         if (sc->redraws_flag == 0) {
2019                                 /* just initialize to default? */
2020                                 /* XXX: we could also have iterated through areas, and taken them from the first timeline available... */
2021                                 sc->redraws_flag = TIME_ALL_3D_WIN|TIME_ALL_ANIM_WIN;
2022                         }
2023                 }
2024
2025                 for (brush = main->brush.first; brush; brush = brush->id.next) {
2026                         if (brush->height == 0)
2027                                 brush->height = 0.4f;
2028                 }
2029
2030                 /* replace 'rim material' option for in offset*/
2031                 for (ob = main->object.first; ob; ob = ob->id.next) {
2032                         ModifierData *md;
2033                         for (md = ob->modifiers.first; md; md = md->next) {
2034                                 if (md->type == eModifierType_Solidify) {
2035                                         SolidifyModifierData *smd = (SolidifyModifierData *) md;
2036                                         if (smd->flag & MOD_SOLIDIFY_RIM_MATERIAL) {
2037                                                 smd->mat_ofs_rim = 1;
2038                                                 smd->flag &= ~MOD_SOLIDIFY_RIM_MATERIAL;
2039                                         }
2040                                 }
2041                         }
2042                 }
2043
2044                 /* particle draw color from material */
2045                 for (part = main->particle.first; part; part = part->id.next) {
2046                         if (part->draw & PART_DRAW_MAT_COL)
2047                                 part->draw_col = PART_DRAW_COL_MAT;
2048                 }
2049         }
2050
2051         if (main->versionfile < 256 || (main->versionfile == 256 && main->subversionfile < 6)) {
2052                 Mesh *me;
2053
2054                 for (me = main->mesh.first; me; me = me->id.next)
2055                         BKE_mesh_calc_normals_tessface(me->mvert, me->totvert, me->mface, me->totface, NULL);
2056         }
2057
2058         if (main->versionfile < 256 || (main->versionfile == 256 && main->subversionfile < 2)) {
2059                 /* update blur area sizes from 0..1 range to 0..100 percentage */
2060                 Scene *scene;
2061                 bNode *node;
2062                 for (scene = main->scene.first; scene; scene = scene->id.next)
2063                         if (scene->nodetree)
2064                                 for (node = scene->nodetree->nodes.first; node; node = node->next)
2065                                         if (node->type == CMP_NODE_BLUR) {
2066                                                 NodeBlurData *nbd = node->storage;
2067                                                 nbd->percentx *= 100.0f;
2068                                                 nbd->percenty *= 100.0f;
2069                                         }
2070         }
2071
2072         if (main->versionfile < 258 || (main->versionfile == 258 && main->subversionfile < 1)) {
2073                 /* screen view2d settings were not properly initialized [#27164]
2074                  * v2d->scroll caused the bug but best reset other values too which are in old blend files only.
2075                  * need to make less ugly - possibly an iterator? */
2076                 bScreen *screen;
2077
2078                 for (screen = main->screen.first; screen; screen = screen->id.next) {
2079                         ScrArea *sa;
2080                         /* add regions */
2081                         for (sa = screen->areabase.first; sa; sa = sa->next) {
2082                                 SpaceLink *sl = sa->spacedata.first;
2083                                 if (sl->spacetype == SPACE_IMAGE) {
2084                                         ARegion *ar;
2085                                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
2086                                                 if (ar->regiontype == RGN_TYPE_WINDOW) {
2087                                                         View2D *v2d = &ar->v2d;
2088                                                         v2d->minzoom = v2d->maxzoom = v2d->scroll = v2d->keeptot = v2d->keepzoom = v2d->keepofs = v2d->align = 0;
2089                                                 }
2090                                         }
2091                                 }
2092
2093                                 for (sl = sa->spacedata.first; sl; sl = sl->next) {
2094                                         if (sl->spacetype == SPACE_IMAGE) {
2095                                                 ARegion *ar;
2096                                                 for (ar = sl->regionbase.first; ar; ar = ar->next) {
2097                                                         if (ar->regiontype == RGN_TYPE_WINDOW) {
2098                                                                 View2D *v2d = &ar->v2d;
2099                                                                 v2d->minzoom = v2d->maxzoom = v2d->scroll = v2d->keeptot = v2d->keepzoom = v2d->keepofs = v2d->align = 0;
2100                                                         }
2101                                                 }
2102                                         }
2103                                 }
2104                         }
2105                 }
2106
2107                 {
2108                         ParticleSettings *part;
2109                         for (part = main->particle.first; part; part = part->id.next) {
2110                                 /* Initialize particle billboard scale */
2111                                 part->bb_size[0] = part->bb_size[1] = 1.0f;
2112                         }
2113                 }
2114         }
2115
2116         if (main->versionfile < 259 || (main->versionfile == 259 && main->subversionfile < 1)) {
2117                 {
2118                         Scene *scene;
2119                         Sequence *seq;
2120
2121                         for (scene = main->scene.first; scene; scene = scene->id.next) {
2122                                 scene->r.ffcodecdata.audio_channels = 2;
2123                                 scene->audio.volume = 1.0f;
2124                                 SEQ_BEGIN (scene->ed, seq)
2125                                 {
2126                                         seq->pitch = 1.0f;
2127                                 }
2128                                 SEQ_END
2129                         }
2130                 }
2131
2132                 {
2133                         bScreen *screen;
2134                         for (screen = main->screen.first; screen; screen = screen->id.next) {
2135                                 ScrArea *sa;
2136
2137                                 /* add regions */
2138                                 for (sa = screen->areabase.first; sa; sa = sa->next) {
2139                                         SpaceLink *sl = sa->spacedata.first;
2140                                         if (sl->spacetype == SPACE_SEQ) {
2141                                                 ARegion *ar;
2142                                                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
2143                                                         if (ar->regiontype == RGN_TYPE_WINDOW) {
2144                                                                 if (ar->v2d.min[1] == 4.0f)
2145                                                                         ar->v2d.min[1] = 0.5f;
2146                                                         }
2147                                                 }
2148                                         }
2149                                         for (sl = sa->spacedata.first; sl; sl = sl->next) {
2150                                                 if (sl->spacetype == SPACE_SEQ) {
2151                                                         ARegion *ar;
2152                                                         for (ar = sl->regionbase.first; ar; ar = ar->next) {
2153                                                                 if (ar->regiontype == RGN_TYPE_WINDOW) {
2154                                                                         if (ar->v2d.min[1] == 4.0f)
2155                                                                                 ar->v2d.min[1] = 0.5f;
2156                                                                 }
2157                                                         }
2158                                                 }
2159                                         }
2160                                 }
2161                         }
2162                 }
2163
2164                 {
2165                         /* Make "auto-clamped" handles a per-keyframe setting instead of per-FCurve
2166                          *
2167                          * We're only patching F-Curves in Actions here, since it is assumed that most
2168                          * drivers out there won't be using this (and if they are, they're in the minority).
2169                          * While we should aim to fix everything ideally, in practice it's far too hard
2170                          * to get to every animdata block, not to mention the performance hit that'd have
2171                          */
2172                         bAction *act;
2173                         FCurve *fcu;
2174
2175                         for (act = main->action.first; act; act = act->id.next) {
2176                                 for (fcu = act->curves.first; fcu; fcu = fcu->next) {
2177                                         BezTriple *bezt;
2178                                         unsigned int i = 0;
2179
2180                                         /* only need to touch curves that had this flag set */
2181                                         if ((fcu->flag & FCURVE_AUTO_HANDLES) == 0)
2182                                                 continue;
2183                                         if ((fcu->totvert == 0) || (fcu->bezt == NULL))
2184                                                 continue;
2185
2186                                         /* only change auto-handles to auto-clamped */
2187                                         for (bezt = fcu->bezt; i < fcu->totvert; i++, bezt++) {
2188                                                 if (bezt->h1 == HD_AUTO)
2189                                                         bezt->h1 = HD_AUTO_ANIM;
2190                                                 if (bezt->h2 == HD_AUTO)
2191                                                         bezt->h2 = HD_AUTO_ANIM;
2192                                         }
2193
2194                                         fcu->flag &= ~FCURVE_AUTO_HANDLES;
2195                                 }
2196                         }
2197                 }
2198         }
2199
2200         if (main->versionfile < 259 || (main->versionfile == 259 && main->subversionfile < 2)) {
2201                 {
2202                         /* Convert default socket values from bNodeStack */
2203                         FOREACH_NODETREE(main, ntree, id) {
2204                                 bNode *node;
2205                                 bNodeSocket *sock;
2206                                 
2207                                 for (node=ntree->nodes.first; node; node=node->next) {
2208                                         for (sock = node->inputs.first; sock; sock = sock->next)
2209                                                 do_versions_socket_default_value_259(sock);
2210                                         for (sock = node->outputs.first; sock; sock = sock->next)
2211                                                 do_versions_socket_default_value_259(sock);
2212                                 }
2213                                 
2214                                 for (sock = ntree->inputs.first; sock; sock = sock->next)
2215                                         do_versions_socket_default_value_259(sock);
2216                                 for (sock = ntree->outputs.first; sock; sock = sock->next)
2217                                         do_versions_socket_default_value_259(sock);
2218                                 
2219                                 ntree->update |= NTREE_UPDATE;
2220                         }
2221                         FOREACH_NODETREE_END
2222                 }
2223
2224                 {
2225                         /* Initialize group tree nodetypes.
2226                          * These are used to distinguish tree types and
2227                          * associate them with specific node types for polling.
2228                          */
2229                         bNodeTree *ntree;
2230                         /* all node trees in main->nodetree are considered groups */
2231                         for (ntree = main->nodetree.first; ntree; ntree = ntree->id.next)
2232                                 ntree->nodetype = NODE_GROUP;
2233                 }
2234         }
2235
2236         if (main->versionfile < 259 || (main->versionfile == 259 && main->subversionfile < 4)) {
2237                 {
2238                         /* Adaptive time step for particle systems */
2239                         ParticleSettings *part;
2240                         for (part = main->particle.first; part; part = part->id.next) {
2241                                 part->courant_target = 0.2f;
2242                                 part->time_flag &= ~PART_TIME_AUTOSF;
2243                         }
2244                 }
2245         }
2246 }