Merge branch 'master' into blender2.8
[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 static void do_version_bone_roll_256(Bone *bone)
549 {
550         Bone *child;
551         float submat[3][3];
552
553         copy_m3_m4(submat, bone->arm_mat);
554         mat3_to_vec_roll(submat, NULL, &bone->arm_roll);
555
556         for (child = bone->childbase.first; child; child = child->next)
557                 do_version_bone_roll_256(child);
558 }
559
560 /* deprecated, only keep this for readfile.c */
561 /* XXX Deprecated function to add a socket in ntree->inputs/ntree->outputs list
562  * (previously called node_group_add_socket). This function has been superseded
563  * by the implementation of proxy nodes. It is still necessary though
564  * for do_versions of pre-2.56.2 code (r35033), so later proxy nodes
565  * can be generated consistently from ntree socket lists.
566  */
567 static bNodeSocket *do_versions_node_group_add_socket_2_56_2(bNodeTree *ngroup, const char *name, int type, int in_out)
568 {
569 //      bNodeSocketType *stype = ntreeGetSocketType(type);
570         bNodeSocket *gsock = MEM_callocN(sizeof(bNodeSocket), "bNodeSocket");
571
572         BLI_strncpy(gsock->name, name, sizeof(gsock->name));
573         gsock->type = type;
574
575         gsock->next = gsock->prev = NULL;
576         gsock->new_sock = NULL;
577         gsock->link = NULL;
578         /* assign new unique index */
579         gsock->own_index = ngroup->cur_index++;
580         gsock->limit = (in_out==SOCK_IN ? 0xFFF : 1);
581
582 //      if (stype->value_structsize > 0)
583 //              gsock->default_value = MEM_callocN(stype->value_structsize, "default socket value");
584
585         BLI_addtail(in_out==SOCK_IN ? &ngroup->inputs : &ngroup->outputs, gsock);
586
587         ngroup->update |= (in_out==SOCK_IN ? NTREE_UPDATE_GROUP_IN : NTREE_UPDATE_GROUP_OUT);
588
589         return gsock;
590 }
591
592 /* Create default_value structs for node sockets from the internal bNodeStack value.
593  * These structs were used from 2.59.2 on, but are replaced in the subsequent do_versions for custom nodes
594  * by generic ID property values. This conversion happened _after_ do_versions originally due to messy type initialization
595  * for node sockets. Now created here intermediately for convenience and to keep do_versions consistent.
596  *
597  * Node compatibility code is gross ...
598  */
599 static void do_versions_socket_default_value_259(bNodeSocket *sock)
600 {
601         bNodeSocketValueFloat *valfloat;
602         bNodeSocketValueVector *valvector;
603         bNodeSocketValueRGBA *valrgba;
604
605         if (sock->default_value)
606                 return;
607
608         switch (sock->type) {
609                 case SOCK_FLOAT:
610                         valfloat = sock->default_value = MEM_callocN(sizeof(bNodeSocketValueFloat), "default socket value");
611                         valfloat->value = sock->ns.vec[0];
612                         valfloat->min = sock->ns.min;
613                         valfloat->max = sock->ns.max;
614                         valfloat->subtype = PROP_NONE;
615                         break;
616                 case SOCK_VECTOR:
617                         valvector = sock->default_value = MEM_callocN(sizeof(bNodeSocketValueVector), "default socket value");
618                         copy_v3_v3(valvector->value, sock->ns.vec);
619                         valvector->min = sock->ns.min;
620                         valvector->max = sock->ns.max;
621                         valvector->subtype = PROP_NONE;
622                         break;
623                 case SOCK_RGBA:
624                         valrgba = sock->default_value = MEM_callocN(sizeof(bNodeSocketValueRGBA), "default socket value");
625                         copy_v4_v4(valrgba->value, sock->ns.vec);
626                         break;
627         }
628 }
629
630 void blo_do_versions_250(FileData *fd, Library *lib, Main *bmain)
631 {
632         /* WATCH IT!!!: pointers from libdata have not been converted */
633
634         if (bmain->versionfile < 250) {
635                 bScreen *screen;
636                 Scene *scene;
637                 Base *base;
638                 Material *ma;
639                 Camera *cam;
640                 Mesh *me;
641                 Curve *cu;
642                 Scene *sce;
643                 Tex *tx;
644                 ParticleSettings *part;
645                 Object *ob;
646                 //PTCacheID *pid;
647                 //ListBase pidlist;
648
649                 bSound *sound;
650                 Sequence *seq;
651
652                 for (sound = bmain->sound.first; sound; sound = sound->id.next) {
653                         if (sound->newpackedfile) {
654                                 sound->packedfile = sound->newpackedfile;
655                                 sound->newpackedfile = NULL;
656                         }
657                 }
658
659                 for (scene = bmain->scene.first; scene; scene = scene->id.next) {
660                         if (scene->ed && scene->ed->seqbasep) {
661                                 SEQ_BEGIN (scene->ed, seq)
662                                 {
663                                         if (seq->type == SEQ_TYPE_SOUND_HD) {
664                                                 char str[FILE_MAX];
665                                                 BLI_join_dirfile(str, sizeof(str), seq->strip->dir, seq->strip->stripdata->name);
666                                                 BLI_path_abs(str, BKE_main_blendfile_path(bmain));
667                                                 seq->sound = BKE_sound_new_file(bmain, str);
668                                         }
669 #define SEQ_USE_PROXY_CUSTOM_DIR (1 << 19)
670 #define SEQ_USE_PROXY_CUSTOM_FILE (1 << 21)
671                                         /* don't know, if anybody used that this way, but just in case, upgrade to new way... */
672                                         if ((seq->flag & SEQ_USE_PROXY_CUSTOM_FILE) &&
673                                            !(seq->flag & SEQ_USE_PROXY_CUSTOM_DIR))
674                                         {
675                                                 BLI_snprintf(seq->strip->proxy->dir, FILE_MAXDIR, "%s/BL_proxy", seq->strip->dir);
676                                         }
677 #undef SEQ_USE_PROXY_CUSTOM_DIR
678 #undef SEQ_USE_PROXY_CUSTOM_FILE
679                                 }
680                                 SEQ_END
681                         }
682                 }
683
684                 for (screen = bmain->screen.first; screen; screen = screen->id.next) {
685                         do_versions_windowmanager_2_50(screen);
686                         do_versions_gpencil_2_50(bmain, screen);
687                 }
688
689                 /* shader, composite and texture node trees have id.name empty, put something in
690                  * to have them show in RNA viewer and accessible otherwise.
691                  */
692                 for (ma = bmain->mat.first; ma; ma = ma->id.next) {
693                         if (ma->nodetree && ma->nodetree->id.name[0] == '\0')
694                                 strcpy(ma->nodetree->id.name, "NTShader Nodetree");
695                 }
696
697                 /* and composite trees */
698                 for (sce = bmain->scene.first; sce; sce = sce->id.next) {
699                         if (sce->nodetree && sce->nodetree->id.name[0] == '\0')
700                                 strcpy(sce->nodetree->id.name, "NTCompositing Nodetree");
701
702                         /* move to cameras */
703                         if (sce->r.mode & R_PANORAMA) {
704                                 for (base = sce->base.first; base; base = base->next) {
705                                         ob = blo_do_versions_newlibadr(fd, lib, base->object);
706
707                                         if (ob->type == OB_CAMERA && !ob->id.lib) {
708                                                 cam = blo_do_versions_newlibadr(fd, lib, ob->data);
709                                                 cam->flag |= CAM_PANORAMA;
710                                         }
711                                 }
712
713                                 sce->r.mode &= ~R_PANORAMA;
714                         }
715                 }
716
717                 /* and texture trees */
718                 for (tx = bmain->tex.first; tx; tx = tx->id.next) {
719                         bNode *node;
720
721                         if (tx->nodetree) {
722                                 if (tx->nodetree->id.name[0] == '\0')
723                                         strcpy(tx->nodetree->id.name, "NTTexture Nodetree");
724
725                                 /* which_output 0 is now "not specified" */
726                                 for (node = tx->nodetree->nodes.first; node; node = node->next)
727                                         if (node->type == TEX_NODE_OUTPUT)
728                                                 node->custom1++;
729                         }
730                 }
731
732                 /* copy standard draw flag to meshes(used to be global, is not available here) */
733                 for (me = bmain->mesh.first; me; me = me->id.next) {
734                         me->drawflag = ME_DRAWEDGES|ME_DRAWFACES|ME_DRAWCREASES;
735                 }
736
737                 /* particle draw and render types */
738                 for (part = bmain->particle.first; part; part = part->id.next) {
739                         if (part->draw_as) {
740                                 if (part->draw_as == PART_DRAW_DOT) {
741                                         part->ren_as = PART_DRAW_HALO;
742                                         part->draw_as = PART_DRAW_REND;
743                                 }
744                                 else if (part->draw_as <= PART_DRAW_AXIS) {
745                                         part->ren_as = PART_DRAW_HALO;
746                                 }
747                                 else {
748                                         part->ren_as = part->draw_as;
749                                         part->draw_as = PART_DRAW_REND;
750                                 }
751                         }
752                         part->path_end = 1.0f;
753                         part->clength = 1.0f;
754                 }
755
756                 /* set old pointcaches to have disk cache flag */
757                 for (ob = bmain->object.first; ob; ob = ob->id.next) {
758
759                         //BKE_ptcache_ids_from_object(&pidlist, ob);
760
761                         //for (pid = pidlist.first; pid; pid = pid->next)
762                         //      pid->cache->flag |= PTCACHE_DISK_CACHE;
763
764                         //BLI_freelistN(&pidlist);
765                 }
766
767                 /* type was a mixed flag & enum. move the 2d flag elsewhere */
768                 for (cu = bmain->curve.first; cu; cu = cu->id.next) {
769                         Nurb *nu;
770
771                         for (nu = cu->nurb.first; nu; nu = nu->next) {
772                                 nu->flag |= (nu->type & CU_2D);
773                                 nu->type &= CU_TYPE;
774                         }
775                 }
776         }
777
778         if (bmain->versionfile < 250 || (bmain->versionfile == 250 && bmain->subversionfile < 1)) {
779                 Object *ob;
780                 Tex *tex;
781                 Scene *sce;
782                 ToolSettings *ts;
783                 //PTCacheID *pid;
784                 //ListBase pidlist;
785
786                 for (ob = bmain->object.first; ob; ob = ob->id.next) {
787                         //BKE_ptcache_ids_from_object(&pidlist, ob);
788
789                         //for (pid = pidlist.first; pid; pid = pid->next) {
790                         //      if (BLI_listbase_is_empty(pid->ptcaches))
791                         //              pid->ptcaches->first = pid->ptcaches->last = pid->cache;
792                         //}
793
794                         //BLI_freelistN(&pidlist);
795
796                         if (ob->type == OB_MESH) {
797                                 Mesh *me = blo_do_versions_newlibadr(fd, lib, ob->data);
798                                 void *olddata = ob->data;
799                                 ob->data = me;
800
801                                 /* XXX - library meshes crash on loading most yoFrankie levels,
802                                  * the multires pointer gets invalid -  Campbell */
803                                 if (me && me->id.lib == NULL && me->mr && me->mr->level_count > 1) {
804                                         multires_load_old(ob, me);
805                                 }
806
807                                 ob->data = olddata;
808                         }
809
810                         if (ob->totcol && ob->matbits == NULL) {
811                                 int a;
812
813                                 ob->matbits = MEM_calloc_arrayN(ob->totcol, sizeof(char), "ob->matbits");
814                                 for (a = 0; a < ob->totcol; a++)
815                                         ob->matbits[a] = (ob->colbits & (1<<a)) != 0;
816                         }
817                 }
818
819                 /* texture filter */
820                 for (tex = bmain->tex.first; tex; tex = tex->id.next) {
821                         if (tex->afmax == 0)
822                                 tex->afmax = 8;
823                 }
824
825                 for (sce = bmain->scene.first; sce; sce = sce->id.next) {
826                         ts = sce->toolsettings;
827                         if (!ts->uv_selectmode || ts->vgroup_weight == 0.0f) {
828                                 ts->selectmode = SCE_SELECT_VERTEX;
829
830                                 /* autokeying - setting should be taken from the user-prefs
831                                  * but the userprefs version may not have correct flags set
832                                  * (i.e. will result in blank box when enabled)
833                                  */
834                                 ts->autokey_mode = U.autokey_mode;
835                                 if (ts->autokey_mode == 0)
836                                         ts->autokey_mode = 2; /* 'add/replace' but not on */
837                                 ts->uv_selectmode = UV_SELECT_VERTEX;
838                                 ts->vgroup_weight = 1.0f;
839                         }
840                 }
841         }
842
843         if (bmain->versionfile < 250 || (bmain->versionfile == 250 && bmain->subversionfile < 2)) {
844                 Object *ob;
845
846                 for (ob = bmain->object.first; ob; ob = ob->id.next) {
847                         if (ob->flag & 8192) // OB_POSEMODE = 8192
848                                 ob->mode |= OB_MODE_POSE;
849                 }
850         }
851
852         if (bmain->versionfile < 250 || (bmain->versionfile == 250 && bmain->subversionfile < 4)) {
853                 Scene *sce;
854                 Object *ob;
855                 ParticleSettings *part;
856                 bool do_gravity = false;
857
858                 for (sce = bmain->scene.first; sce; sce = sce->id.next)
859                         if (sce->unit.scale_length == 0.0f)
860                                 sce->unit.scale_length = 1.0f;
861
862                 for (ob = bmain->object.first; ob; ob = ob->id.next) {
863                         /* fluid-sim stuff */
864                         FluidsimModifierData *fluidmd = (FluidsimModifierData *) modifiers_findByType(ob, eModifierType_Fluidsim);
865                         if (fluidmd)
866                                 fluidmd->fss->fmd = fluidmd;
867
868                         /* rotation modes were added, but old objects would now default to being 'quaternion based' */
869                         ob->rotmode = ROT_MODE_EUL;
870                 }
871
872                 for (sce = bmain->scene.first; sce; sce = sce->id.next) {
873                         if (sce->audio.main == 0.0f)
874                                 sce->audio.main = 1.0f;
875
876                         sce->r.ffcodecdata.audio_mixrate = sce->audio.mixrate;
877                         sce->r.ffcodecdata.audio_volume = sce->audio.main;
878                         sce->audio.distance_model = 2;
879                         sce->audio.doppler_factor = 1.0f;
880                         sce->audio.speed_of_sound = 343.3f;
881                 }
882
883                 /* Add default gravity to scenes */
884                 for (sce = bmain->scene.first; sce; sce = sce->id.next) {
885                         if ((sce->physics_settings.flag & PHYS_GLOBAL_GRAVITY) == 0 &&
886                             is_zero_v3(sce->physics_settings.gravity))
887                         {
888                                 sce->physics_settings.gravity[0] = sce->physics_settings.gravity[1] = 0.0f;
889                                 sce->physics_settings.gravity[2] = -9.81f;
890                                 sce->physics_settings.flag = PHYS_GLOBAL_GRAVITY;
891                                 do_gravity = true;
892                         }
893                 }
894
895                 /* Assign proper global gravity weights for dynamics (only z-coordinate is taken into account) */
896                 if (do_gravity) {
897                         for (part = bmain->particle.first; part; part = part->id.next)
898                                 part->effector_weights->global_gravity = part->acc[2]/-9.81f;
899                 }
900
901                 for (ob = bmain->object.first; ob; ob = ob->id.next) {
902                         ModifierData *md;
903
904                         if (do_gravity) {
905                                 for (md = ob->modifiers.first; md; md = md->next) {
906                                         ClothModifierData *clmd = (ClothModifierData *) modifiers_findByType(ob, eModifierType_Cloth);
907                                         if (clmd)
908                                                 clmd->sim_parms->effector_weights->global_gravity = clmd->sim_parms->gravity[2]/-9.81f;
909                                 }
910
911                                 if (ob->soft)
912                                         ob->soft->effector_weights->global_gravity = ob->soft->grav/9.81f;
913                         }
914
915                         /* Normal wind shape is plane */
916                         if (ob->pd) {
917                                 if (ob->pd->forcefield == PFIELD_WIND)
918                                         ob->pd->shape = PFIELD_SHAPE_PLANE;
919
920                                 if (ob->pd->flag & PFIELD_PLANAR)
921                                         ob->pd->shape = PFIELD_SHAPE_PLANE;
922                                 else if (ob->pd->flag & PFIELD_SURFACE)
923                                         ob->pd->shape = PFIELD_SHAPE_SURFACE;
924
925                                 ob->pd->flag |= PFIELD_DO_LOCATION;
926                         }
927                 }
928         }
929
930         if (bmain->versionfile < 250 || (bmain->versionfile == 250 && bmain->subversionfile < 6)) {
931                 Object *ob;
932
933                 /* New variables for axis-angle rotations and/or quaternion rotations were added, and need proper initialization */
934                 for (ob = bmain->object.first; ob; ob = ob->id.next) {
935                         /* new variables for all objects */
936                         ob->quat[0] = 1.0f;
937                         ob->rotAxis[1] = 1.0f;
938
939                         /* bones */
940                         if (ob->pose) {
941                                 bPoseChannel *pchan;
942
943                                 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
944                                         /* just need to initalise rotation axis properly... */
945                                         pchan->rotAxis[1] = 1.0f;
946                                 }
947                         }
948                 }
949         }
950
951         if (bmain->versionfile < 250 || (bmain->versionfile == 250 && bmain->subversionfile < 7)) {
952                 Mesh *me;
953                 Nurb *nu;
954                 Lattice *lt;
955                 Curve *cu;
956                 Key *key;
957                 const float *data;
958                 int a, tot;
959
960                 /* shape keys are no longer applied to the mesh itself, but rather
961                  * to the derivedmesh/displist, so here we ensure that the basis
962                  * shape key is always set in the mesh coordinates. */
963                 for (me = bmain->mesh.first; me; me = me->id.next) {
964                         if ((key = blo_do_versions_newlibadr(fd, lib, me->key)) && key->refkey) {
965                                 data = key->refkey->data;
966                                 tot = MIN2(me->totvert, key->refkey->totelem);
967
968                                 for (a = 0; a < tot; a++, data += 3)
969                                         copy_v3_v3(me->mvert[a].co, data);
970                         }
971                 }
972
973                 for (lt = bmain->latt.first; lt; lt = lt->id.next) {
974                         if ((key = blo_do_versions_newlibadr(fd, lib, lt->key)) && key->refkey) {
975                                 data = key->refkey->data;
976                                 tot = MIN2(lt->pntsu*lt->pntsv*lt->pntsw, key->refkey->totelem);
977
978                                 for (a = 0; a < tot; a++, data += 3)
979                                         copy_v3_v3(lt->def[a].vec, data);
980                         }
981                 }
982
983                 for (cu = bmain->curve.first; cu; cu = cu->id.next) {
984                         if ((key = blo_do_versions_newlibadr(fd, lib, cu->key)) && key->refkey) {
985                                 data = key->refkey->data;
986
987                                 for (nu = cu->nurb.first; nu; nu = nu->next) {
988                                         if (nu->bezt) {
989                                                 BezTriple *bezt = nu->bezt;
990
991                                                 for (a = 0; a < nu->pntsu; a++, bezt++) {
992                                                         copy_v3_v3(bezt->vec[0], data); data+=3;
993                                                         copy_v3_v3(bezt->vec[1], data); data+=3;
994                                                         copy_v3_v3(bezt->vec[2], data); data+=3;
995                                                         bezt->alfa = *data; data++;
996                                                 }
997                                         }
998                                         else if (nu->bp) {
999                                                 BPoint *bp = nu->bp;
1000
1001                                                 for (a = 0; a < nu->pntsu*nu->pntsv; a++, bp++) {
1002                                                         copy_v3_v3(bp->vec, data); data += 3;
1003                                                         bp->alfa = *data; data++;
1004                                                 }
1005                                         }
1006                                 }
1007                         }
1008                 }
1009         }
1010
1011         if (bmain->versionfile < 250 || (bmain->versionfile == 250 && bmain->subversionfile < 8)) {
1012                 {
1013                         Scene *sce = bmain->scene.first;
1014                         while (sce) {
1015                                 if (sce->r.frame_step == 0)
1016                                         sce->r.frame_step = 1;
1017
1018                                 sce = sce->id.next;
1019                         }
1020                 }
1021
1022                 {
1023                         /* ensure all nodes have unique names */
1024                         bNodeTree *ntree = bmain->nodetree.first;
1025                         while (ntree) {
1026                                 bNode *node = ntree->nodes.first;
1027
1028                                 while (node) {
1029                                         nodeUniqueName(ntree, node);
1030                                         node = node->next;
1031                                 }
1032
1033                                 ntree = ntree->id.next;
1034                         }
1035                 }
1036
1037                 {
1038                         Object *ob = bmain->object.first;
1039                         while (ob) {
1040                                 /* shaded mode disabled for now */
1041                                 if (ob->dt == OB_MATERIAL)
1042                                         ob->dt = OB_TEXTURE;
1043                                 ob = ob->id.next;
1044                         }
1045                 }
1046
1047                 {
1048                         bScreen *screen;
1049                         ScrArea *sa;
1050                         SpaceLink *sl;
1051
1052                         for (screen = bmain->screen.first; screen; screen = screen->id.next) {
1053                                 for (sa = screen->areabase.first; sa; sa = sa->next) {
1054                                         for (sl = sa->spacedata.first; sl; sl = sl->next) {
1055                                                 if (sl->spacetype == SPACE_VIEW3D) {
1056                                                         View3D *v3d = (View3D *) sl;
1057                                                         if (v3d->drawtype == OB_MATERIAL)
1058                                                                 v3d->drawtype = OB_SOLID;
1059                                                 }
1060                                         }
1061                                 }
1062                         }
1063                 }
1064
1065                 /* only convert old 2.50 files with color management */
1066                 if (bmain->versionfile == 250) {
1067                         Scene *sce = bmain->scene.first;
1068                         Material *ma = bmain->mat.first;
1069                         Tex *tex = bmain->tex.first;
1070                         int i, convert = 0;
1071
1072                         /* convert to new color management system:
1073                          * while previously colors were stored as srgb,
1074                          * now they are stored as linear internally,
1075                          * with screen gamma correction in certain places in the UI. */
1076
1077                         /* don't know what scene is active, so we'll convert if any scene has it enabled... */
1078                         while (sce) {
1079                                 if (sce->r.color_mgt_flag & R_COLOR_MANAGEMENT)
1080                                         convert = 1;
1081                                 sce = sce->id.next;
1082                         }
1083
1084                         if (convert) {
1085                                 while (ma) {
1086                                         srgb_to_linearrgb_v3_v3(&ma->r, &ma->r);
1087                                         srgb_to_linearrgb_v3_v3(&ma->specr, &ma->specr);
1088                                         ma = ma->id.next;
1089                                 }
1090
1091                                 while (tex) {
1092                                         if (tex->coba) {
1093                                                 ColorBand *band = (ColorBand *)tex->coba;
1094                                                 for (i = 0; i < band->tot; i++) {
1095                                                         CBData *data = band->data + i;
1096                                                         srgb_to_linearrgb_v3_v3(&data->r, &data->r);
1097                                                 }
1098                                         }
1099                                         tex = tex->id.next;
1100                                 }
1101                         }
1102                 }
1103         }
1104
1105         if (bmain->versionfile < 250 || (bmain->versionfile == 250 && bmain->subversionfile < 9)) {
1106                 Scene *sce;
1107                 Mesh *me;
1108                 Object *ob;
1109
1110                 for (sce = bmain->scene.first; sce; sce = sce->id.next)
1111                         if (!sce->toolsettings->particle.selectmode)
1112                                 sce->toolsettings->particle.selectmode = SCE_SELECT_PATH;
1113
1114                 if (bmain->versionfile == 250 && bmain->subversionfile > 1) {
1115                         for (me = bmain->mesh.first; me; me = me->id.next)
1116                                 multires_load_old_250(me);
1117
1118                         for (ob = bmain->object.first; ob; ob = ob->id.next) {
1119                                 MultiresModifierData *mmd = (MultiresModifierData *) modifiers_findByType(ob, eModifierType_Multires);
1120
1121                                 if (mmd) {
1122                                         mmd->totlvl--;
1123                                         mmd->lvl--;
1124                                         mmd->sculptlvl = mmd->lvl;
1125                                         mmd->renderlvl = mmd->lvl;
1126                                 }
1127                         }
1128                 }
1129         }
1130
1131         if (bmain->versionfile < 250 || (bmain->versionfile == 250 && bmain->subversionfile < 10)) {
1132                 Object *ob;
1133
1134                 /* properly initialize hair clothsim data on old files */
1135                 for (ob = bmain->object.first; ob; ob = ob->id.next) {
1136                         ModifierData *md;
1137                         for (md = ob->modifiers.first; md; md = md->next) {
1138                                 if (md->type == eModifierType_Cloth) {
1139                                         ClothModifierData *clmd = (ClothModifierData *)md;
1140                                         if (clmd->sim_parms->velocity_smooth < 0.01f)
1141                                                 clmd->sim_parms->velocity_smooth = 0.f;
1142                                 }
1143                         }
1144                 }
1145         }
1146
1147         /* fix bad area setup in subversion 10 */
1148         if (bmain->versionfile == 250 && bmain->subversionfile == 10) {
1149                 /* fix for new view type in sequencer */
1150                 bScreen *screen;
1151                 ScrArea *sa;
1152                 SpaceLink *sl;
1153
1154                 /* remove all preview window in wrong spaces */
1155                 for (screen = bmain->screen.first; screen; screen = screen->id.next) {
1156                         for (sa = screen->areabase.first; sa; sa = sa->next) {
1157                                 for (sl = sa->spacedata.first; sl; sl = sl->next) {
1158                                         if (sl->spacetype != SPACE_SEQ) {
1159                                                 ARegion *ar;
1160                                                 ListBase *regionbase;
1161
1162                                                 if (sl == sa->spacedata.first) {
1163                                                         regionbase = &sa->regionbase;
1164                                                 }
1165                                                 else {
1166                                                         regionbase = &sl->regionbase;
1167                                                 }
1168
1169                                                 for (ar = regionbase->first; ar; ar = ar->next) {
1170                                                         if (ar->regiontype == RGN_TYPE_PREVIEW)
1171                                                                 break;
1172                                                 }
1173
1174                                                 if (ar && (ar->regiontype == RGN_TYPE_PREVIEW)) {
1175                                                         SpaceType *st = BKE_spacetype_from_id(SPACE_SEQ);
1176                                                         BKE_area_region_free(st, ar);
1177                                                         BLI_freelinkN(regionbase, ar);
1178                                                 }
1179                                         }
1180                                 }
1181                         }
1182                 }
1183         }
1184
1185         if (bmain->versionfile < 250 || (bmain->versionfile == 250 && bmain->subversionfile < 11)) {
1186                 {
1187                         /* fix for new view type in sequencer */
1188                         bScreen *screen;
1189                         ScrArea *sa;
1190                         SpaceLink *sl;
1191
1192                         for (screen = bmain->screen.first; screen; screen = screen->id.next) {
1193                                 for (sa = screen->areabase.first; sa; sa = sa->next) {
1194                                         for (sl = sa->spacedata.first; sl; sl = sl->next) {
1195                                                 if (sl->spacetype == SPACE_SEQ) {
1196                                                         ARegion *ar;
1197                                                         ARegion *ar_main;
1198                                                         ListBase *regionbase;
1199                                                         SpaceSeq *sseq = (SpaceSeq *)sl;
1200
1201                                                         if (sl == sa->spacedata.first) {
1202                                                                 regionbase = &sa->regionbase;
1203                                                         }
1204                                                         else {
1205                                                                 regionbase = &sl->regionbase;
1206                                                         }
1207
1208                                                         if (sseq->view == 0)
1209                                                                 sseq->view = SEQ_VIEW_SEQUENCE;
1210                                                         if (sseq->mainb == 0)
1211                                                                 sseq->mainb = SEQ_DRAW_IMG_IMBUF;
1212
1213                                                         ar_main = (ARegion*)regionbase->first;
1214                                                         for (; ar_main; ar_main = ar_main->next) {
1215                                                                 if (ar_main->regiontype == RGN_TYPE_WINDOW)
1216                                                                         break;
1217                                                         }
1218                                                         ar = MEM_callocN(sizeof(ARegion), "preview area for sequencer");
1219                                                         BLI_insertlinkbefore(regionbase, ar_main, ar);
1220                                                         sequencer_init_preview_region(ar);
1221                                                 }
1222                                         }
1223                                 }
1224                         }
1225                 }
1226         }
1227
1228         if (bmain->versionfile < 250 || (bmain->versionfile == 250 && bmain->subversionfile < 12)) {
1229                 Object *ob;
1230                 Brush *brush;
1231
1232                 /* anim viz changes */
1233                 for (ob = bmain->object.first; ob; ob = ob->id.next) {
1234                         /* initialize object defaults */
1235                         animviz_settings_init(&ob->avs);
1236
1237                         /* if armature, copy settings for pose from armature data
1238                          * performing initialization where appropriate
1239                          */
1240                         if (ob->pose && ob->data) {
1241                                 bArmature *arm = blo_do_versions_newlibadr(fd, lib, ob->data);
1242                                 if (arm) { /* XXX - why does this fail in some cases? */
1243                                         bAnimVizSettings *avs = &ob->pose->avs;
1244
1245                                         /* ghosting settings ---------------- */
1246                                         /* ranges */
1247                                         avs->ghost_bc = avs->ghost_ac = arm->ghostep;
1248
1249                                         avs->ghost_sf = arm->ghostsf;
1250                                         avs->ghost_ef = arm->ghostef;
1251                                         if ((avs->ghost_sf == avs->ghost_ef) && (avs->ghost_sf == 0)) {
1252                                                 avs->ghost_sf = 1;
1253                                                 avs->ghost_ef = 100;
1254                                         }
1255
1256                                         /* type */
1257                                         if (arm->ghostep == 0)
1258                                                 avs->ghost_type = GHOST_TYPE_NONE;
1259                                         else
1260                                                 avs->ghost_type = arm->ghosttype + 1;
1261
1262                                         /* stepsize */
1263                                         avs->ghost_step = arm->ghostsize;
1264                                         if (avs->ghost_step == 0)
1265                                                 avs->ghost_step = 1;
1266
1267                                         /* path settings --------------------- */
1268                                         /* ranges */
1269                                         avs->path_bc = arm->pathbc;
1270                                         avs->path_ac = arm->pathac;
1271                                         if ((avs->path_bc == avs->path_ac) && (avs->path_bc == 0))
1272                                                 avs->path_bc = avs->path_ac = 10;
1273
1274                                         avs->path_sf = arm->pathsf;
1275                                         avs->path_ef = arm->pathef;
1276                                         if ((avs->path_sf == avs->path_ef) && (avs->path_sf == 0)) {
1277                                                 avs->path_sf = 1;
1278                                                 avs->path_ef = 250;
1279                                         }
1280
1281                                         /* flags */
1282                                         if (arm->pathflag & ARM_PATH_FNUMS)
1283                                                 avs->path_viewflag |= MOTIONPATH_VIEW_FNUMS;
1284                                         if (arm->pathflag & ARM_PATH_KFRAS)
1285                                                 avs->path_viewflag |= MOTIONPATH_VIEW_KFRAS;
1286                                         if (arm->pathflag & ARM_PATH_KFNOS)
1287                                                 avs->path_viewflag |= MOTIONPATH_VIEW_KFNOS;
1288
1289                                         /* bake flags */
1290                                         if (arm->pathflag & ARM_PATH_HEADS)
1291                                                 avs->path_bakeflag |= MOTIONPATH_BAKE_HEADS;
1292
1293                                         /* type */
1294                                         if (arm->pathflag & ARM_PATH_ACFRA)
1295                                                 avs->path_type = MOTIONPATH_TYPE_ACFRA;
1296
1297                                         /* stepsize */
1298                                         avs->path_step = arm->pathsize;
1299                                         if (avs->path_step == 0)
1300                                                 avs->path_step = 1;
1301                                 }
1302                                 else
1303                                         animviz_settings_init(&ob->pose->avs);
1304                         }
1305                 }
1306
1307                 /* brush texture changes */
1308                 for (brush = bmain->brush.first; brush; brush = brush->id.next) {
1309                         BKE_texture_mtex_default(&brush->mtex);
1310                         BKE_texture_mtex_default(&brush->mask_mtex);
1311                 }
1312         }
1313
1314         if (bmain->versionfile < 250 || (bmain->versionfile == 250 && bmain->subversionfile < 13)) {
1315                 /* NOTE: if you do more conversion, be sure to do it outside of this and
1316                  * increase subversion again, otherwise it will not be correct */
1317                 Object *ob;
1318
1319                 /* convert degrees to radians for internal use */
1320                 for (ob = bmain->object.first; ob; ob = ob->id.next) {
1321                         bPoseChannel *pchan;
1322
1323                         do_version_constraints_radians_degrees_250(&ob->constraints);
1324
1325                         if (ob->pose) {
1326                                 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
1327                                         pchan->limitmin[0] *= (float)(M_PI / 180.0);
1328                                         pchan->limitmin[1] *= (float)(M_PI / 180.0);
1329                                         pchan->limitmin[2] *= (float)(M_PI / 180.0);
1330                                         pchan->limitmax[0] *= (float)(M_PI / 180.0);
1331                                         pchan->limitmax[1] *= (float)(M_PI / 180.0);
1332                                         pchan->limitmax[2] *= (float)(M_PI / 180.0);
1333
1334                                         do_version_constraints_radians_degrees_250(&pchan->constraints);
1335                                 }
1336                         }
1337                 }
1338         }
1339
1340         if (bmain->versionfile < 250 || (bmain->versionfile == 250 && bmain->subversionfile < 14)) {
1341                 /* fix for bad View2D extents for Animation Editors */
1342                 bScreen *screen;
1343                 ScrArea *sa;
1344                 SpaceLink *sl;
1345
1346                 for (screen = bmain->screen.first; screen; screen = screen->id.next) {
1347                         for (sa = screen->areabase.first; sa; sa = sa->next) {
1348                                 for (sl = sa->spacedata.first; sl; sl = sl->next) {
1349                                         ListBase *regionbase;
1350                                         ARegion *ar;
1351
1352                                         if (sl == sa->spacedata.first)
1353                                                 regionbase = &sa->regionbase;
1354                                         else
1355                                                 regionbase = &sl->regionbase;
1356
1357                                         if (ELEM(sl->spacetype, SPACE_ACTION, SPACE_NLA)) {
1358                                                 for (ar = (ARegion*) regionbase->first; ar; ar = ar->next) {
1359                                                         if (ar->regiontype == RGN_TYPE_WINDOW) {
1360                                                                 ar->v2d.cur.ymax = ar->v2d.tot.ymax = 0.0f;
1361                                                                 ar->v2d.cur.ymin = ar->v2d.tot.ymin = (float)(-sa->winy) / 3.0f;
1362                                                         }
1363                                                 }
1364                                         }
1365                                 }
1366                         }
1367                 }
1368         }
1369
1370         if (bmain->versionfile < 250 || (bmain->versionfile == 250 && bmain->subversionfile < 17)) {
1371                 Scene *sce;
1372                 Sequence *seq;
1373
1374                 /* initialize to sane default so toggling on border shows something */
1375                 for (sce = bmain->scene.first; sce; sce = sce->id.next) {
1376                         if (sce->r.border.xmin == 0.0f && sce->r.border.ymin == 0.0f &&
1377                             sce->r.border.xmax == 0.0f && sce->r.border.ymax == 0.0f)
1378                         {
1379                                 sce->r.border.xmin = 0.0f;
1380                                 sce->r.border.ymin = 0.0f;
1381                                 sce->r.border.xmax = 1.0f;
1382                                 sce->r.border.ymax = 1.0f;
1383                         }
1384
1385                         if ((sce->r.ffcodecdata.flags & FFMPEG_MULTIPLEX_AUDIO) == 0)
1386                                 sce->r.ffcodecdata.audio_codec = 0x0; // CODEC_ID_NONE
1387
1388                         SEQ_BEGIN (sce->ed, seq)
1389                         {
1390                                 seq->volume = 1.0f;
1391                         }
1392                         SEQ_END
1393                 }
1394
1395                 /* particle brush strength factor was changed from int to float */
1396                 for (sce = bmain->scene.first; sce; sce = sce->id.next) {
1397                         ParticleEditSettings *pset = &sce->toolsettings->particle;
1398                         int a;
1399
1400                         for (a = 0; a < ARRAY_SIZE(pset->brush); a++)
1401                                 pset->brush[a].strength /= 100.0f;
1402                 }
1403
1404                 /* sequencer changes */
1405                 {
1406                         bScreen *screen;
1407                         ScrArea *sa;
1408                         SpaceLink *sl;
1409
1410                         for (screen = bmain->screen.first; screen; screen = screen->id.next) {
1411                                 for (sa = screen->areabase.first; sa; sa = sa->next) {
1412                                         for (sl = sa->spacedata.first; sl; sl = sl->next) {
1413                                                 if (sl->spacetype == SPACE_SEQ) {
1414                                                         ARegion *ar_preview;
1415                                                         ListBase *regionbase;
1416
1417                                                         if (sl == sa->spacedata.first) {
1418                                                                 regionbase = &sa->regionbase;
1419                                                         }
1420                                                         else {
1421                                                                 regionbase = &sl->regionbase;
1422                                                         }
1423
1424                                                         ar_preview = (ARegion*) regionbase->first;
1425                                                         for (; ar_preview; ar_preview = ar_preview->next) {
1426                                                                 if (ar_preview->regiontype == RGN_TYPE_PREVIEW)
1427                                                                         break;
1428                                                         }
1429                                                         if (ar_preview && (ar_preview->regiontype == RGN_TYPE_PREVIEW)) {
1430                                                                 sequencer_init_preview_region(ar_preview);
1431                                                         }
1432                                                 }
1433                                         }
1434                                 }
1435                         }
1436                 } /* sequencer changes */
1437         }
1438
1439         if (bmain->versionfile <= 251) {        /* 2.5.1 had no subversions */
1440                 bScreen *sc;
1441
1442                 /* Blender 2.5.2 - subversion 0 introduced a new setting: V3D_RENDER_OVERRIDE.
1443                  * This bit was used in the past for V3D_TRANSFORM_SNAP, which is now deprecated.
1444                  * Here we clear it for old files so they don't come in with V3D_RENDER_OVERRIDE set,
1445                  * which would cause cameras, lamps, etc to become invisible */
1446                 for (sc = bmain->screen.first; sc; sc = sc->id.next) {
1447                         ScrArea *sa;
1448                         for (sa = sc->areabase.first; sa; sa = sa->next) {
1449                                 SpaceLink *sl;
1450                                 for (sl = sa->spacedata.first; sl; sl = sl->next) {
1451                                         if (sl->spacetype == SPACE_VIEW3D) {
1452                                                 View3D *v3d = (View3D *)sl;
1453                                                 v3d->flag2 &= ~V3D_RENDER_OVERRIDE;
1454                                         }
1455                                 }
1456                         }
1457                 }
1458         }
1459
1460         if (bmain->versionfile < 252 || (bmain->versionfile == 252 && bmain->subversionfile < 1)) {
1461                 Brush *brush;
1462                 Object *ob;
1463                 Scene *scene;
1464                 bNodeTree *ntree;
1465
1466                 for (brush = bmain->brush.first; brush; brush = brush->id.next) {
1467                         if (brush->curve)
1468                                 brush->curve->preset = CURVE_PRESET_SMOOTH;
1469                 }
1470
1471                 /* properly initialize active flag for fluidsim modifiers */
1472                 for (ob = bmain->object.first; ob; ob = ob->id.next) {
1473                         ModifierData *md;
1474                         for (md = ob->modifiers.first; md; md = md->next) {
1475                                 if (md->type == eModifierType_Fluidsim) {
1476                                         FluidsimModifierData *fmd = (FluidsimModifierData *) md;
1477                                         fmd->fss->flag |= OB_FLUIDSIM_ACTIVE;
1478                                         fmd->fss->flag |= OB_FLUIDSIM_OVERRIDE_TIME;
1479                                 }
1480                         }
1481                 }
1482
1483                 /* adjustment to color balance node values */
1484                 for (scene = bmain->scene.first; scene; scene = scene->id.next) {
1485                         if (scene->nodetree) {
1486                                 bNode *node = scene->nodetree->nodes.first;
1487
1488                                 while (node) {
1489                                         if (node->type == CMP_NODE_COLORBALANCE) {
1490                                                 NodeColorBalance *n = (NodeColorBalance *) node->storage;
1491                                                 n->lift[0] += 1.f;
1492                                                 n->lift[1] += 1.f;
1493                                                 n->lift[2] += 1.f;
1494                                         }
1495                                         node = node->next;
1496                                 }
1497                         }
1498                 }
1499                 /* check inside node groups too */
1500                 for (ntree = bmain->nodetree.first; ntree; ntree = ntree->id.next) {
1501                         bNode *node = ntree->nodes.first;
1502
1503                         while (node) {
1504                                 if (node->type == CMP_NODE_COLORBALANCE) {
1505                                         NodeColorBalance *n = (NodeColorBalance *) node->storage;
1506                                         n->lift[0] += 1.f;
1507                                         n->lift[1] += 1.f;
1508                                         n->lift[2] += 1.f;
1509                                 }
1510
1511                                 node = node->next;
1512                         }
1513                 }
1514         }
1515
1516         /* old-track -> constraints (this time we're really doing it!) */
1517         if (bmain->versionfile < 252 || (bmain->versionfile == 252 && bmain->subversionfile < 2)) {
1518                 Object *ob;
1519
1520                 for (ob = bmain->object.first; ob; ob = ob->id.next)
1521                         blo_do_version_old_trackto_to_constraints(ob);
1522         }
1523
1524         if (bmain->versionfile < 252 || (bmain->versionfile == 252 && bmain->subversionfile < 5)) {
1525                 bScreen *sc;
1526
1527                 /* Image editor scopes */
1528                 for (sc = bmain->screen.first; sc; sc = sc->id.next) {
1529                         ScrArea *sa;
1530
1531                         for (sa = sc->areabase.first; sa; sa = sa->next) {
1532                                 SpaceLink *sl;
1533
1534                                 for (sl = sa->spacedata.first; sl; sl = sl->next) {
1535                                         if (sl->spacetype == SPACE_IMAGE) {
1536                                                 SpaceImage *sima = (SpaceImage *) sl;
1537                                                 scopes_new(&sima->scopes);
1538                                         }
1539                                 }
1540                         }
1541                 }
1542         }
1543
1544         if (bmain->versionfile < 253) {
1545                 Object *ob;
1546                 Scene *scene;
1547                 bScreen *sc;
1548                 Tex *tex;
1549                 Brush *brush;
1550
1551                 for (sc = bmain->screen.first; sc; sc = sc->id.next) {
1552                         ScrArea *sa;
1553                         for (sa = sc->areabase.first; sa; sa = sa->next) {
1554                                 SpaceLink *sl;
1555
1556                                 for (sl = sa->spacedata.first; sl; sl = sl->next) {
1557                                         if (sl->spacetype == SPACE_NODE) {
1558                                                 SpaceNode *snode = (SpaceNode *) sl;
1559                                                 ListBase *regionbase;
1560                                                 ARegion *ar;
1561
1562                                                 if (sl == sa->spacedata.first)
1563                                                         regionbase = &sa->regionbase;
1564                                                 else
1565                                                         regionbase = &sl->regionbase;
1566
1567                                                 if (snode->v2d.minzoom > 0.09f)
1568                                                         snode->v2d.minzoom = 0.09f;
1569                                                 if (snode->v2d.maxzoom < 2.31f)
1570                                                         snode->v2d.maxzoom = 2.31f;
1571
1572                                                 for (ar = regionbase->first; ar; ar = ar->next) {
1573                                                         if (ar->regiontype == RGN_TYPE_WINDOW) {
1574                                                                 if (ar->v2d.minzoom > 0.09f)
1575                                                                         ar->v2d.minzoom = 0.09f;
1576                                                                 if (ar->v2d.maxzoom < 2.31f)
1577                                                                         ar->v2d.maxzoom = 2.31f;
1578                                                         }
1579                                                 }
1580                                         }
1581                                 }
1582                         }
1583                 }
1584
1585                 do_version_mdef_250(bmain);
1586
1587                 /* parent type to modifier */
1588                 for (ob = bmain->object.first; ob; ob = ob->id.next) {
1589                         if (ob->parent) {
1590                                 Object *parent = (Object *) blo_do_versions_newlibadr(fd, lib, ob->parent);
1591                                 if (parent) { /* parent may not be in group */
1592                                         if (parent->type == OB_ARMATURE && ob->partype == PARSKEL) {
1593                                                 ArmatureModifierData *amd;
1594                                                 bArmature *arm = (bArmature *) blo_do_versions_newlibadr(fd, lib, parent->data);
1595
1596                                                 amd = (ArmatureModifierData*) modifier_new(eModifierType_Armature);
1597                                                 amd->object = ob->parent;
1598                                                 BLI_addtail((ListBase*)&ob->modifiers, amd);
1599                                                 amd->deformflag = arm->deformflag;
1600                                                 ob->partype = PAROBJECT;
1601                                         }
1602                                         else if (parent->type == OB_LATTICE && ob->partype == PARSKEL) {
1603                                                 LatticeModifierData *lmd;
1604
1605                                                 lmd = (LatticeModifierData*) modifier_new(eModifierType_Lattice);
1606                                                 lmd->object = ob->parent;
1607                                                 BLI_addtail((ListBase*)&ob->modifiers, lmd);
1608                                                 ob->partype = PAROBJECT;
1609                                         }
1610                                         else if (parent->type == OB_CURVE && ob->partype == PARCURVE) {
1611                                                 CurveModifierData *cmd;
1612
1613                                                 cmd = (CurveModifierData*) modifier_new(eModifierType_Curve);
1614                                                 cmd->object = ob->parent;
1615                                                 BLI_addtail((ListBase*)&ob->modifiers, cmd);
1616                                                 ob->partype = PAROBJECT;
1617                                         }
1618                                 }
1619                         }
1620                 }
1621
1622                 /* initialize scene active layer */
1623                 for (scene = bmain->scene.first; scene; scene = scene->id.next) {
1624                         int i;
1625                         for (i = 0; i < 20; i++) {
1626                                 if (scene->lay & (1<<i)) {
1627                                         scene->layact = 1<<i;
1628                                         break;
1629                                 }
1630                         }
1631                 }
1632
1633                 for (tex = bmain->tex.first; tex; tex = tex->id.next) {
1634                         /* if youre picky, this isn't correct until we do a version bump
1635                          * since you could set saturation to be 0.0*/
1636                         if (tex->saturation == 0.0f)
1637                                 tex->saturation = 1.0f;
1638                 }
1639
1640                 {
1641                         Curve *cu;
1642                         for (cu = bmain->curve.first; cu; cu = cu->id.next) {
1643                                 cu->smallcaps_scale = 0.75f;
1644                         }
1645                 }
1646
1647                 for (scene = bmain->scene.first; scene; scene = scene->id.next) {
1648                         if (scene) {
1649                                 Sequence *seq;
1650                                 SEQ_BEGIN (scene->ed, seq)
1651                                 {
1652                                         if (seq->sat == 0.0f) {
1653                                                 seq->sat = 1.0f;
1654                                         }
1655                                 }
1656                                 SEQ_END
1657                         }
1658                 }
1659
1660                 /* GSOC 2010 Sculpt - New settings for Brush */
1661
1662                 for (brush = bmain->brush.first; brush; brush = brush->id.next) {
1663                         /* Sanity Check */
1664
1665                         /* infinite number of dabs */
1666                         if (brush->spacing == 0)
1667                                 brush->spacing = 10;
1668
1669                         /* will have no effect */
1670                         if (brush->alpha == 0)
1671                                 brush->alpha = 0.5f;
1672
1673                         /* bad radius */
1674                         if (brush->unprojected_radius == 0)
1675                                 brush->unprojected_radius = 0.125f;
1676
1677                         /* unusable size */
1678                         if (brush->size == 0)
1679                                 brush->size = 35;
1680
1681                         /* can't see overlay */
1682                         if (brush->texture_overlay_alpha == 0)
1683                                 brush->texture_overlay_alpha = 33;
1684
1685                         /* same as draw brush */
1686                         if (brush->crease_pinch_factor == 0)
1687                                 brush->crease_pinch_factor = 0.5f;
1688
1689                         /* will sculpt no vertexes */
1690                         if (brush->plane_trim == 0)
1691                                 brush->plane_trim = 0.5f;
1692
1693                         /* same as smooth stroke off */
1694                         if (brush->smooth_stroke_radius == 0)
1695                                 brush->smooth_stroke_radius = 75;
1696
1697                         /* will keep cursor in one spot */
1698                         if (brush->smooth_stroke_radius == 1)
1699                                 brush->smooth_stroke_factor = 0.9f;
1700
1701                         /* same as dots */
1702                         if (brush->rate == 0)
1703                                 brush->rate = 0.1f;
1704
1705                         /* New Settings */
1706                         if (bmain->versionfile < 252 || (bmain->versionfile == 252 && bmain->subversionfile < 5)) {
1707                                 brush->flag |= BRUSH_SPACE_ATTEN; // explicitly enable adaptive space
1708
1709                                 /* spacing was originally in pixels, convert it to percentage for new version
1710                                  * size should not be zero due to sanity check above
1711                                  */
1712                                 brush->spacing = (int)(100 * ((float)brush->spacing) / ((float) brush->size));
1713
1714                                 if (brush->add_col[0] == 0 &&
1715                                         brush->add_col[1] == 0 &&
1716                                         brush->add_col[2] == 0)
1717                                 {
1718                                         brush->add_col[0] = 1.00f;
1719                                         brush->add_col[1] = 0.39f;
1720                                         brush->add_col[2] = 0.39f;
1721                                 }
1722
1723                                 if (brush->sub_col[0] == 0 &&
1724                                         brush->sub_col[1] == 0 &&
1725                                         brush->sub_col[2] == 0)
1726                                 {
1727                                         brush->sub_col[0] = 0.39f;
1728                                         brush->sub_col[1] = 0.39f;
1729                                         brush->sub_col[2] = 1.00f;
1730                                 }
1731                         }
1732                 }
1733         }
1734
1735         /* GSOC Sculpt 2010 - Sanity check on Sculpt/Paint settings */
1736         if (bmain->versionfile < 253) {
1737                 Scene *sce;
1738                 for (sce = bmain->scene.first; sce; sce = sce->id.next) {
1739                         if (sce->toolsettings->sculpt_paint_unified_alpha == 0)
1740                                 sce->toolsettings->sculpt_paint_unified_alpha = 0.5f;
1741
1742                         if (sce->toolsettings->sculpt_paint_unified_unprojected_radius == 0)
1743                                 sce->toolsettings->sculpt_paint_unified_unprojected_radius = 0.125f;
1744
1745                         if (sce->toolsettings->sculpt_paint_unified_size == 0)
1746                                 sce->toolsettings->sculpt_paint_unified_size = 35;
1747                 }
1748         }
1749
1750         if (bmain->versionfile < 253 || (bmain->versionfile == 253 && bmain->subversionfile < 1)) {
1751                 Object *ob;
1752
1753                 for (ob = bmain->object.first; ob; ob = ob->id.next) {
1754                         ModifierData *md;
1755
1756                         for (md = ob->modifiers.first; md; md = md->next) {
1757                                 if (md->type == eModifierType_Smoke) {
1758                                         SmokeModifierData *smd = (SmokeModifierData *)md;
1759
1760                                         if ((smd->type & MOD_SMOKE_TYPE_DOMAIN) && smd->domain) {
1761                                                 smd->domain->vorticity = 2.0f;
1762                                                 smd->domain->time_scale = 1.0f;
1763
1764                                                 if (!(smd->domain->flags & (1<<4)))
1765                                                         continue;
1766
1767                                                 /* delete old MOD_SMOKE_INITVELOCITY flag */
1768                                                 smd->domain->flags &= ~(1<<4);
1769
1770                                                 /* for now just add it to all flow objects in the scene */
1771                                                 {
1772                                                         Object *ob2;
1773                                                         for (ob2 = bmain->object.first; ob2; ob2 = ob2->id.next) {
1774                                                                 ModifierData *md2;
1775                                                                 for (md2 = ob2->modifiers.first; md2; md2 = md2->next) {
1776                                                                         if (md2->type == eModifierType_Smoke) {
1777                                                                                 SmokeModifierData *smd2 = (SmokeModifierData *)md2;
1778
1779                                                                                 if ((smd2->type & MOD_SMOKE_TYPE_FLOW) && smd2->flow) {
1780                                                                                         smd2->flow->flags |= MOD_SMOKE_FLOW_INITVELOCITY;
1781                                                                                 }
1782                                                                         }
1783                                                                 }
1784                                                         }
1785                                                 }
1786
1787                                         }
1788                                         else if ((smd->type & MOD_SMOKE_TYPE_FLOW) && smd->flow) {
1789                                                 smd->flow->vel_multi = 1.0f;
1790                                         }
1791                                 }
1792                         }
1793                 }
1794         }
1795
1796         if (bmain->versionfile < 255 || (bmain->versionfile == 255 && bmain->subversionfile < 1)) {
1797                 Brush *br;
1798                 ParticleSettings *part;
1799                 bScreen *sc;
1800
1801                 for (br = bmain->brush.first; br; br = br->id.next) {
1802                         if (br->ob_mode == 0)
1803                                 br->ob_mode = OB_MODE_ALL_PAINT;
1804                 }
1805
1806                 for (part = bmain->particle.first; part; part = part->id.next) {
1807                         if (part->boids)
1808                                 part->boids->pitch = 1.0f;
1809
1810                         part->flag &= ~PART_HAIR_REGROW; /* this was a deprecated flag before */
1811                         part->kink_amp_clump = 1.f; /* keep old files looking similar */
1812                 }
1813
1814                 for (sc = bmain->screen.first; sc; sc = sc->id.next) {
1815                         ScrArea *sa;
1816                         for (sa = sc->areabase.first; sa; sa = sa->next) {
1817                                 SpaceLink *sl;
1818                                 for (sl = sa->spacedata.first; sl; sl = sl->next) {
1819                                         if (sl->spacetype == SPACE_INFO) {
1820                                                 SpaceInfo *sinfo = (SpaceInfo *) sl;
1821                                                 ARegion *ar;
1822
1823                                                 sinfo->rpt_mask = INFO_RPT_OP;
1824
1825                                                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
1826                                                         if (ar->regiontype == RGN_TYPE_WINDOW) {
1827                                                                 ar->v2d.scroll = (V2D_SCROLL_RIGHT);
1828                                                                 ar->v2d.align = V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_NEG_Y; /* align bottom left */
1829                                                                 ar->v2d.keepofs = V2D_LOCKOFS_X;
1830                                                                 ar->v2d.keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_LIMITZOOM|V2D_KEEPASPECT);
1831                                                                 ar->v2d.keeptot = V2D_KEEPTOT_BOUNDS;
1832                                                                 ar->v2d.minzoom = ar->v2d.maxzoom = 1.0f;
1833                                                         }
1834                                                 }
1835                                         }
1836                                 }
1837                         }
1838                 }
1839         }
1840
1841         if (bmain->versionfile < 255 || (bmain->versionfile == 255 && bmain->subversionfile < 3)) {
1842                 Object *ob;
1843
1844                 /* ocean res is now squared, reset old ones - will be massive */
1845                 for (ob = bmain->object.first; ob; ob = ob->id.next) {
1846                         ModifierData *md;
1847                         for (md = ob->modifiers.first; md; md = md->next) {
1848                                 if (md->type == eModifierType_Ocean) {
1849                                         OceanModifierData *omd = (OceanModifierData *)md;
1850                                         omd->resolution = 7;
1851                                         omd->oceancache = NULL;
1852                                 }
1853                         }
1854                 }
1855         }
1856
1857         if (bmain->versionfile < 256) {
1858                 bScreen *sc;
1859                 ScrArea *sa;
1860                 Key *key;
1861
1862                 /* Fix for sample line scope initializing with no height */
1863                 for (sc = bmain->screen.first; sc; sc = sc->id.next) {
1864                         sa = sc->areabase.first;
1865                         while (sa) {
1866                                 SpaceLink *sl;
1867                                 for (sl = sa->spacedata.first; sl; sl = sl->next) {
1868                                         if (sl->spacetype == SPACE_IMAGE) {
1869                                                 SpaceImage *sima = (SpaceImage *) sl;
1870                                                 if (sima->sample_line_hist.height == 0)
1871                                                         sima->sample_line_hist.height = 100;
1872                                         }
1873                                 }
1874                                 sa = sa->next;
1875                         }
1876                 }
1877
1878                 /* old files could have been saved with slidermin = slidermax = 0.0, but the UI in
1879                  * 2.4x would never reveal this to users as a dummy value always ended up getting used
1880                  * instead
1881                  */
1882                 for (key = bmain->key.first; key; key = key->id.next) {
1883                         KeyBlock *kb;
1884
1885                         for (kb = key->block.first; kb; kb = kb->next) {
1886                                 if (IS_EQF(kb->slidermin, kb->slidermax) && IS_EQF(kb->slidermax, 0.0f))
1887                                         kb->slidermax = kb->slidermin + 1.0f;
1888                         }
1889                 }
1890         }
1891
1892         if (bmain->versionfile < 256 || (bmain->versionfile == 256 && bmain->subversionfile < 1)) {
1893                 /* fix for bones that didn't have arm_roll before */
1894                 bArmature *arm;
1895                 Bone *bone;
1896                 Object *ob;
1897
1898                 for (arm = bmain->armature.first; arm; arm = arm->id.next)
1899                         for (bone = arm->bonebase.first; bone; bone = bone->next)
1900                                 do_version_bone_roll_256(bone);
1901
1902                 /* fix for objects which have zero dquat's
1903                  * since this is multiplied with the quat rather than added */
1904                 for (ob = bmain->object.first; ob; ob = ob->id.next) {
1905                         if (is_zero_v4(ob->dquat)) {
1906                                 unit_qt(ob->dquat);
1907                         }
1908                         if (is_zero_v3(ob->drotAxis) && ob->drotAngle == 0.0f) {
1909                                 unit_axis_angle(ob->drotAxis, &ob->drotAngle);
1910                         }
1911                 }
1912         }
1913
1914         if (bmain->versionfile < 256 || (bmain->versionfile == 256 && bmain->subversionfile < 2)) {
1915                 bNodeTree *ntree;
1916                 bNode *node;
1917                 bNodeSocket *sock, *gsock;
1918                 bNodeLink *link;
1919
1920                 /* node sockets are not exposed automatically any more,
1921                  * this mimics the old behavior by adding all unlinked sockets to groups.
1922                  */
1923                 for (ntree=bmain->nodetree.first; ntree; ntree=ntree->id.next) {
1924                         /* this adds copies and links from all unlinked internal sockets to group inputs/outputs. */
1925
1926                         /* first make sure the own_index for new sockets is valid */
1927                         for (node=ntree->nodes.first; node; node=node->next) {
1928                                 for (sock = node->inputs.first; sock; sock = sock->next)
1929                                         if (sock->own_index >= ntree->cur_index)
1930                                                 ntree->cur_index = sock->own_index+1;
1931                                 for (sock = node->outputs.first; sock; sock = sock->next)
1932                                         if (sock->own_index >= ntree->cur_index)
1933                                                 ntree->cur_index = sock->own_index+1;
1934                         }
1935
1936                         /* add ntree->inputs/ntree->outputs sockets for all unlinked sockets in the group tree. */
1937                         for (node=ntree->nodes.first; node; node=node->next) {
1938                                 for (sock = node->inputs.first; sock; sock = sock->next) {
1939                                         if (!sock->link && !nodeSocketIsHidden(sock)) {
1940
1941                                                 gsock = do_versions_node_group_add_socket_2_56_2(ntree, sock->name, sock->type, SOCK_IN);
1942
1943                                                 /* initialize the default socket value */
1944                                                 copy_v4_v4(gsock->ns.vec, sock->ns.vec);
1945
1946                                                 /* XXX nodeAddLink does not work with incomplete (node==NULL) links any longer,
1947                                                  * have to create these directly here. These links are updated again in subsequent do_version!
1948                                                  */
1949                                                 link = MEM_callocN(sizeof(bNodeLink), "link");
1950                                                 BLI_addtail(&ntree->links, link);
1951                                                 link->fromnode = NULL;
1952                                                 link->fromsock = gsock;
1953                                                 link->tonode = node;
1954                                                 link->tosock = sock;
1955                                                 ntree->update |= NTREE_UPDATE_LINKS;
1956
1957                                                 sock->link = link;
1958                                         }
1959                                 }
1960                                 for (sock = node->outputs.first; sock; sock = sock->next) {
1961                                         if (nodeCountSocketLinks(ntree, sock)==0 && !nodeSocketIsHidden(sock)) {
1962                                                 gsock = do_versions_node_group_add_socket_2_56_2(ntree, sock->name, sock->type, SOCK_OUT);
1963
1964                                                 /* initialize the default socket value */
1965                                                 copy_v4_v4(gsock->ns.vec, sock->ns.vec);
1966
1967                                                 /* XXX nodeAddLink does not work with incomplete (node==NULL) links any longer,
1968                                                  * have to create these directly here. These links are updated again in subsequent do_version!
1969                                                  */
1970                                                 link = MEM_callocN(sizeof(bNodeLink), "link");
1971                                                 BLI_addtail(&ntree->links, link);
1972                                                 link->fromnode = node;
1973                                                 link->fromsock = sock;
1974                                                 link->tonode = NULL;
1975                                                 link->tosock = gsock;
1976                                                 ntree->update |= NTREE_UPDATE_LINKS;
1977
1978                                                 gsock->link = link;
1979                                         }
1980                                 }
1981                         }
1982
1983                         /* XXX The external group node sockets needs to adjust their own_index to point at
1984                          * associated ntree inputs/outputs internal sockets. However, this can only happen
1985                          * after lib-linking (needs access to internal node group tree)!
1986                          * Setting a temporary flag here, actual do_versions happens in lib_verify_nodetree.
1987                          */
1988                         ntree->flag |= NTREE_DO_VERSIONS_GROUP_EXPOSE_2_56_2;
1989                 }
1990         }
1991
1992         if (bmain->versionfile < 256 || (bmain->versionfile == 256 && bmain->subversionfile < 3)) {
1993                 bScreen *sc;
1994                 Brush *brush;
1995                 Object *ob;
1996                 ParticleSettings *part;
1997
1998                 /* redraws flag in SpaceTime has been moved to Screen level */
1999                 for (sc = bmain->screen.first; sc; sc = sc->id.next) {
2000                         if (sc->redraws_flag == 0) {
2001                                 /* just initialize to default? */
2002                                 /* XXX: we could also have iterated through areas, and taken them from the first timeline available... */
2003                                 sc->redraws_flag = TIME_ALL_3D_WIN|TIME_ALL_ANIM_WIN;
2004                         }
2005                 }
2006
2007                 for (brush = bmain->brush.first; brush; brush = brush->id.next) {
2008                         if (brush->height == 0)
2009                                 brush->height = 0.4f;
2010                 }
2011
2012                 /* replace 'rim material' option for in offset*/
2013                 for (ob = bmain->object.first; ob; ob = ob->id.next) {
2014                         ModifierData *md;
2015                         for (md = ob->modifiers.first; md; md = md->next) {
2016                                 if (md->type == eModifierType_Solidify) {
2017                                         SolidifyModifierData *smd = (SolidifyModifierData *) md;
2018                                         if (smd->flag & MOD_SOLIDIFY_RIM_MATERIAL) {
2019                                                 smd->mat_ofs_rim = 1;
2020                                                 smd->flag &= ~MOD_SOLIDIFY_RIM_MATERIAL;
2021                                         }
2022                                 }
2023                         }
2024                 }
2025
2026                 /* particle draw color from material */
2027                 for (part = bmain->particle.first; part; part = part->id.next) {
2028                         if (part->draw & PART_DRAW_MAT_COL)
2029                                 part->draw_col = PART_DRAW_COL_MAT;
2030                 }
2031         }
2032
2033         if (bmain->versionfile < 256 || (bmain->versionfile == 256 && bmain->subversionfile < 6)) {
2034                 Mesh *me;
2035
2036                 for (me = bmain->mesh.first; me; me = me->id.next)
2037                         BKE_mesh_calc_normals_tessface(me->mvert, me->totvert, me->mface, me->totface, NULL);
2038         }
2039
2040         if (bmain->versionfile < 256 || (bmain->versionfile == 256 && bmain->subversionfile < 2)) {
2041                 /* update blur area sizes from 0..1 range to 0..100 percentage */
2042                 Scene *scene;
2043                 bNode *node;
2044                 for (scene = bmain->scene.first; scene; scene = scene->id.next)
2045                         if (scene->nodetree)
2046                                 for (node = scene->nodetree->nodes.first; node; node = node->next)
2047                                         if (node->type == CMP_NODE_BLUR) {
2048                                                 NodeBlurData *nbd = node->storage;
2049                                                 nbd->percentx *= 100.0f;
2050                                                 nbd->percenty *= 100.0f;
2051                                         }
2052         }
2053
2054         if (bmain->versionfile < 258 || (bmain->versionfile == 258 && bmain->subversionfile < 1)) {
2055                 /* screen view2d settings were not properly initialized [#27164]
2056                  * v2d->scroll caused the bug but best reset other values too which are in old blend files only.
2057                  * need to make less ugly - possibly an iterator? */
2058                 bScreen *screen;
2059
2060                 for (screen = bmain->screen.first; screen; screen = screen->id.next) {
2061                         ScrArea *sa;
2062                         /* add regions */
2063                         for (sa = screen->areabase.first; sa; sa = sa->next) {
2064                                 SpaceLink *sl = sa->spacedata.first;
2065                                 if (sl->spacetype == SPACE_IMAGE) {
2066                                         ARegion *ar;
2067                                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
2068                                                 if (ar->regiontype == RGN_TYPE_WINDOW) {
2069                                                         View2D *v2d = &ar->v2d;
2070                                                         v2d->minzoom = v2d->maxzoom = v2d->scroll = v2d->keeptot = v2d->keepzoom = v2d->keepofs = v2d->align = 0;
2071                                                 }
2072                                         }
2073                                 }
2074
2075                                 for (sl = sa->spacedata.first; sl; sl = sl->next) {
2076                                         if (sl->spacetype == SPACE_IMAGE) {
2077                                                 ARegion *ar;
2078                                                 for (ar = sl->regionbase.first; ar; ar = ar->next) {
2079                                                         if (ar->regiontype == RGN_TYPE_WINDOW) {
2080                                                                 View2D *v2d = &ar->v2d;
2081                                                                 v2d->minzoom = v2d->maxzoom = v2d->scroll = v2d->keeptot = v2d->keepzoom = v2d->keepofs = v2d->align = 0;
2082                                                         }
2083                                                 }
2084                                         }
2085                                 }
2086                         }
2087                 }
2088
2089                 {
2090                         ParticleSettings *part;
2091                         for (part = bmain->particle.first; part; part = part->id.next) {
2092                                 /* Initialize particle billboard scale */
2093                                 part->bb_size[0] = part->bb_size[1] = 1.0f;
2094                         }
2095                 }
2096         }
2097
2098         if (bmain->versionfile < 259 || (bmain->versionfile == 259 && bmain->subversionfile < 1)) {
2099                 {
2100                         Scene *scene;
2101                         Sequence *seq;
2102
2103                         for (scene = bmain->scene.first; scene; scene = scene->id.next) {
2104                                 scene->r.ffcodecdata.audio_channels = 2;
2105                                 scene->audio.volume = 1.0f;
2106                                 SEQ_BEGIN (scene->ed, seq)
2107                                 {
2108                                         seq->pitch = 1.0f;
2109                                 }
2110                                 SEQ_END
2111                         }
2112                 }
2113
2114                 {
2115                         bScreen *screen;
2116                         for (screen = bmain->screen.first; screen; screen = screen->id.next) {
2117                                 ScrArea *sa;
2118
2119                                 /* add regions */
2120                                 for (sa = screen->areabase.first; sa; sa = sa->next) {
2121                                         SpaceLink *sl = sa->spacedata.first;
2122                                         if (sl->spacetype == SPACE_SEQ) {
2123                                                 ARegion *ar;
2124                                                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
2125                                                         if (ar->regiontype == RGN_TYPE_WINDOW) {
2126                                                                 if (ar->v2d.min[1] == 4.0f)
2127                                                                         ar->v2d.min[1] = 0.5f;
2128                                                         }
2129                                                 }
2130                                         }
2131                                         for (sl = sa->spacedata.first; sl; sl = sl->next) {
2132                                                 if (sl->spacetype == SPACE_SEQ) {
2133                                                         ARegion *ar;
2134                                                         for (ar = sl->regionbase.first; ar; ar = ar->next) {
2135                                                                 if (ar->regiontype == RGN_TYPE_WINDOW) {
2136                                                                         if (ar->v2d.min[1] == 4.0f)
2137                                                                                 ar->v2d.min[1] = 0.5f;
2138                                                                 }
2139                                                         }
2140                                                 }
2141                                         }
2142                                 }
2143                         }
2144                 }
2145
2146                 {
2147                         /* Make "auto-clamped" handles a per-keyframe setting instead of per-FCurve
2148                          *
2149                          * We're only patching F-Curves in Actions here, since it is assumed that most
2150                          * drivers out there won't be using this (and if they are, they're in the minority).
2151                          * While we should aim to fix everything ideally, in practice it's far too hard
2152                          * to get to every animdata block, not to mention the performance hit that'd have
2153                          */
2154                         bAction *act;
2155                         FCurve *fcu;
2156
2157                         for (act = bmain->action.first; act; act = act->id.next) {
2158                                 for (fcu = act->curves.first; fcu; fcu = fcu->next) {
2159                                         BezTriple *bezt;
2160                                         unsigned int i = 0;
2161
2162                                         /* only need to touch curves that had this flag set */
2163                                         if ((fcu->flag & FCURVE_AUTO_HANDLES) == 0)
2164                                                 continue;
2165                                         if ((fcu->totvert == 0) || (fcu->bezt == NULL))
2166                                                 continue;
2167
2168                                         /* only change auto-handles to auto-clamped */
2169                                         for (bezt = fcu->bezt; i < fcu->totvert; i++, bezt++) {
2170                                                 if (bezt->h1 == HD_AUTO)
2171                                                         bezt->h1 = HD_AUTO_ANIM;
2172                                                 if (bezt->h2 == HD_AUTO)
2173                                                         bezt->h2 = HD_AUTO_ANIM;
2174                                         }
2175
2176                                         fcu->flag &= ~FCURVE_AUTO_HANDLES;
2177                                 }
2178                         }
2179                 }
2180         }
2181
2182         if (bmain->versionfile < 259 || (bmain->versionfile == 259 && bmain->subversionfile < 2)) {
2183                 {
2184                         /* Convert default socket values from bNodeStack */
2185                         FOREACH_NODETREE(bmain, ntree, id) {
2186                                 bNode *node;
2187                                 bNodeSocket *sock;
2188
2189                                 for (node=ntree->nodes.first; node; node=node->next) {
2190                                         for (sock = node->inputs.first; sock; sock = sock->next)
2191                                                 do_versions_socket_default_value_259(sock);
2192                                         for (sock = node->outputs.first; sock; sock = sock->next)
2193                                                 do_versions_socket_default_value_259(sock);
2194                                 }
2195
2196                                 for (sock = ntree->inputs.first; sock; sock = sock->next)
2197                                         do_versions_socket_default_value_259(sock);
2198                                 for (sock = ntree->outputs.first; sock; sock = sock->next)
2199                                         do_versions_socket_default_value_259(sock);
2200
2201                                 ntree->update |= NTREE_UPDATE;
2202                         }
2203                         FOREACH_NODETREE_END
2204                 }
2205
2206                 {
2207                         /* Initialize group tree nodetypes.
2208                          * These are used to distinguish tree types and
2209                          * associate them with specific node types for polling.
2210                          */
2211                         bNodeTree *ntree;
2212                         /* all node trees in bmain->nodetree are considered groups */
2213                         for (ntree = bmain->nodetree.first; ntree; ntree = ntree->id.next)
2214                                 ntree->nodetype = NODE_GROUP;
2215                 }
2216         }
2217
2218         if (bmain->versionfile < 259 || (bmain->versionfile == 259 && bmain->subversionfile < 4)) {
2219                 {
2220                         /* Adaptive time step for particle systems */
2221                         ParticleSettings *part;
2222                         for (part = bmain->particle.first; part; part = part->id.next) {
2223                                 part->courant_target = 0.2f;
2224                                 part->time_flag &= ~PART_TIME_AUTOSF;
2225                         }
2226                 }
2227         }
2228 }