Assorted fixes:
[blender.git] / source / blender / blenkernel / intern / ipo.c
1 /* ipo.c
2  * 
3  * $Id$
4  *
5  * ***** BEGIN GPL LICENSE BLOCK *****
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  *
21  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
22  * All rights reserved.
23  *
24  * The Original Code is: all of this file.
25  *
26  * Contributor(s): 2008,2009 Joshua Leung (IPO System cleanup, Animation System Recode)
27  *
28  * ***** END GPL LICENSE BLOCK *****
29  */
30
31 /* NOTE:
32  *
33  * This file is no longer used to provide tools for the depreceated IPO system. Instead, it
34  * is only used to house the conversion code to the new system.
35  *
36  * -- Joshua Leung, Jan 2009
37  */
38  
39 #include <math.h>
40 #include <stdio.h>
41 #include <string.h>
42
43 #ifdef HAVE_CONFIG_H
44 #include <config.h>
45 #endif
46
47 #include "MEM_guardedalloc.h"
48
49 #include "DNA_anim_types.h"
50 #include "DNA_action_types.h"
51 #include "DNA_armature_types.h"
52 #include "DNA_constraint_types.h"
53 #include "DNA_curve_types.h"
54 #include "DNA_camera_types.h"
55 #include "DNA_lamp_types.h"
56 #include "DNA_ipo_types.h"
57 #include "DNA_key_types.h"
58 #include "DNA_material_types.h"
59 #include "DNA_mesh_types.h"
60 #include "DNA_object_types.h"
61 #include "DNA_object_force.h"
62 #include "DNA_particle_types.h"
63 #include "DNA_sequence_types.h"
64 #include "DNA_scene_types.h"
65 #include "DNA_sound_types.h"
66 #include "DNA_texture_types.h"
67 #include "DNA_view3d_types.h"
68 #include "DNA_world_types.h"
69
70 #include "BLI_blenlib.h"
71 #include "BLI_arithb.h"
72 #include "BLI_dynstr.h"
73
74 #include "BKE_utildefines.h"
75
76 #include "BKE_animsys.h"
77 #include "BKE_action.h"
78 #include "BKE_blender.h"
79 #include "BKE_curve.h"
80 #include "BKE_constraint.h"
81 #include "BKE_fcurve.h"
82 #include "BKE_global.h"
83 #include "BKE_ipo.h"
84 #include "BKE_library.h"
85 #include "BKE_main.h"
86 #include "BKE_mesh.h"
87 #include "BKE_object.h"
88
89
90
91 /* *************************************************** */
92 /* Old-Data Freeing Tools */
93
94 /* Free data from old IPO-Blocks (those which haven't been converted), but not IPO block itself */
95 // XXX this shouldn't be necessary anymore, but may occur while not all data is converted yet
96 void free_ipo (Ipo *ipo)
97 {
98         IpoCurve *icu, *icn;
99         int n= 0;
100         
101         for (icu= ipo->curve.first; icu; icu= icn) {
102                 icn= icu->next;
103                 n++;
104                 
105                 if (icu->bezt) MEM_freeN(icu->bezt);
106                 if (icu->bp) MEM_freeN(icu->bp);
107                 if (icu->driver) MEM_freeN(icu->driver);
108                 
109                 BLI_freelinkN(&ipo->curve, icu);
110         }
111         
112         if (G.f & G_DEBUG)
113                 printf("Freed %d (Unconverted) Ipo-Curves from IPO '%s' \n", n, ipo->id.name+2);
114 }
115
116 /* *************************************************** */
117 /* ADRCODE to RNA-Path Conversion Code  - Special (Bitflags) */
118
119 /* Mapping Table for bitflag <-> RNA path */
120 typedef struct AdrBit2Path {
121         int bit;
122         char *path;
123         int array_index;
124 } AdrBit2Path;
125
126 /* ----------------- */
127 /* Mapping Tables to use bits <-> RNA paths */
128
129 /* Object layers */
130 static AdrBit2Path ob_layer_bits[]= {
131         {(1<<0), "layer", 0},
132         {(1<<1), "layer", 1},
133         {(1<<2), "layer", 2},
134         {(1<<3), "layer", 3},
135         {(1<<4), "layer", 4},
136         {(1<<5), "layer", 5},
137         {(1<<6), "layer", 6},
138         {(1<<7), "layer", 7},
139         {(1<<8), "layer", 8},
140         {(1<<9), "layer", 9},
141         {(1<<10), "layer", 10},
142         {(1<<11), "layer", 11},
143         {(1<<12), "layer", 12},
144         {(1<<13), "layer", 13},
145         {(1<<14), "layer", 14},
146         {(1<<15), "layer", 15},
147         {(1<<16), "layer", 16},
148         {(1<<17), "layer", 17},
149         {(1<<18), "layer", 18},
150         {(1<<19), "layer", 19},
151         {(1<<20), "layer", 20}
152 };
153
154 /* ----------------- */
155
156 /* This function checks if a Blocktype+Adrcode combo, returning a mapping table */
157 static AdrBit2Path *adrcode_bitmaps_to_paths (int blocktype, int adrcode, int *tot)
158 {
159         /* Object layers */
160         if ((blocktype == ID_OB) && (adrcode == OB_LAY)) {
161                 *tot= sizeof(ob_layer_bits)/sizeof(AdrBit2Path);
162                 return ob_layer_bits;
163         }
164         else if ((blocktype == ID_MA) && (adrcode == MA_MODE)) {
165                 // XXX to be added...
166         }
167         // XXX TODO: add other types...
168         
169         /* Normal curve */
170         return NULL;
171 }
172
173 /* This function makes a copy of a path stored in AdrBit2Path entry, and makes a guardedalloc copy */
174 static char *adrcode_bitmap_path_copy (const char *abp_path)
175 {
176         char *path;
177         int len;
178         
179         /* copy the path */
180         len= strlen(abp_path) + 1; // XXX is this safe?
181         path= MEM_callocN(len, "Bitflag IPO-Curve RNA-Path");
182         memcpy(path, abp_path, len);
183         
184         return path;
185 }
186
187 /* *************************************************** */
188 /* ADRCODE to RNA-Path Conversion Code  - Standard */
189
190 /* Object types */
191 static char *ob_adrcodes_to_paths (int adrcode, int *array_index)
192 {
193         /* set array index like this in-case nothing sets it correctly  */
194         *array_index= 0;
195         
196         /* result depends on adrcode */
197         switch (adrcode) {
198                 case OB_LOC_X:
199                         *array_index= 0; return "location";
200                 case OB_LOC_Y:
201                         *array_index= 1; return "location";
202                 case OB_LOC_Z:
203                         *array_index= 2; return "location";
204                 case OB_DLOC_X:
205                         *array_index= 0; return "delta_location";
206                 case OB_DLOC_Y:
207                         *array_index= 1; return "delta_location";
208                 case OB_DLOC_Z:
209                         *array_index= 2; return "delta_location";
210                 
211                 case OB_ROT_X:
212                         *array_index= 0; return "rotation";
213                 case OB_ROT_Y:
214                         *array_index= 1; return "rotation";
215                 case OB_ROT_Z:
216                         *array_index= 2; return "rotation";
217                 case OB_DROT_X:
218                         *array_index= 0; return "delta_rotation";
219                 case OB_DROT_Y:
220                         *array_index= 1; return "delta_rotation";
221                 case OB_DROT_Z:
222                         *array_index= 2; return "delta_rotation";
223                         
224                 case OB_SIZE_X:
225                         *array_index= 0; return "scale";
226                 case OB_SIZE_Y:
227                         *array_index= 1; return "scale";
228                 case OB_SIZE_Z:
229                         *array_index= 2; return "scale";
230                 case OB_DSIZE_X:
231                         *array_index= 0; return "delta_scale";
232                 case OB_DSIZE_Y:
233                         *array_index= 1; return "delta_scale";
234                 case OB_DSIZE_Z:
235                         *array_index= 2; return "delta_scale";
236         
237 #if 0   
238                 case OB_COL_R:  
239                         poin= &(ob->col[0]); break;
240                 case OB_COL_G:
241                         poin= &(ob->col[1]); break;
242                 case OB_COL_B:
243                         poin= &(ob->col[2]); break;
244                 case OB_COL_A:
245                         poin= &(ob->col[3]); break;
246                         
247                 case OB_PD_FSTR:
248                         if (ob->pd) poin= &(ob->pd->f_strength);
249                         break;
250                 case OB_PD_FFALL:
251                         if (ob->pd) poin= &(ob->pd->f_power);
252                         break;
253                 case OB_PD_SDAMP:
254                         if (ob->pd) poin= &(ob->pd->pdef_damp);
255                         break;
256                 case OB_PD_RDAMP:
257                         if (ob->pd) poin= &(ob->pd->pdef_rdamp);
258                         break;
259                 case OB_PD_PERM:
260                         if (ob->pd) poin= &(ob->pd->pdef_perm);
261                         break;
262                 case OB_PD_FMAXD:
263                         if (ob->pd) poin= &(ob->pd->maxdist);
264                         break;
265 #endif
266         }
267         
268         return NULL;
269 }
270
271 /* PoseChannel types 
272  * NOTE: pchan name comes from 'actname' added earlier... 
273  */
274 static char *pchan_adrcodes_to_paths (int adrcode, int *array_index)
275 {
276         /* set array index like this in-case nothing sets it correctly  */
277         *array_index= 0;
278         
279         /* result depends on adrcode */
280         switch (adrcode) {
281                 case AC_QUAT_W:
282                         *array_index= 0; return "rotation";
283                 case AC_QUAT_X:
284                         *array_index= 1; return "rotation";
285                 case AC_QUAT_Y:
286                         *array_index= 2; return "rotation";
287                 case AC_QUAT_Z:
288                         *array_index= 3; return "rotation";
289                         
290                 case AC_EUL_X:
291                         *array_index= 0; return "euler_rotation";
292                 case AC_EUL_Y:
293                         *array_index= 1; return "euler_rotation";
294                 case AC_EUL_Z:
295                         *array_index= 2; return "euler_rotation";
296                         
297                 case -1: // XXX special case for rotation drivers... until eulers are added...
298                         *array_index= 0; return "rotation";
299                         
300                 case AC_LOC_X:
301                         *array_index= 0; return "location";
302                 case AC_LOC_Y:
303                         *array_index= 1; return "location";
304                 case AC_LOC_Z:
305                         *array_index= 2; return "location";
306                 
307                 case AC_SIZE_X:
308                         *array_index= 0; return "scale";
309                 case AC_SIZE_Y:
310                         *array_index= 1; return "scale";
311                 case AC_SIZE_Z:
312                         *array_index= 2; return "scale";
313         }
314         
315         /* for debugging only */
316         printf("ERROR: unmatched PoseChannel setting (code %d) \n", adrcode);
317         return NULL;
318 }
319
320 /* Constraint types */
321 static char *constraint_adrcodes_to_paths (int adrcode, int *array_index)
322 {
323         /* set array index like this in-case nothing sets it correctly  */
324         *array_index= 0;
325         
326         /* result depends on adrcode */
327         switch (adrcode) {
328                 case CO_ENFORCE:
329                         return "influence";
330                 case CO_HEADTAIL:       // XXX this needs to be wrapped in RNA.. probably then this path will be invalid
331                         return "data.head_tail";
332         }
333         
334         return NULL;
335 }
336
337 /* ShapeKey types 
338  * NOTE: as we don't have access to the keyblock where the data comes from (for now), 
339  *              we'll just use numerical indicies for now... 
340  */
341 static char *shapekey_adrcodes_to_paths (int adrcode, int *array_index)
342 {
343         static char buf[128];
344         
345         /* block will be attached to ID_KE block, and setting that we alter is the 'value' (which sets keyblock.curval) */
346         // XXX adrcode 0 was dummy 'speed' curve 
347         if (adrcode == 0) 
348                 sprintf(buf, "speed");
349         else
350                 sprintf(buf, "keys[%d].value", adrcode);
351         return buf;
352 }
353
354 /* MTex (Texture Slot) types */
355 static char *mtex_adrcodes_to_paths (int adrcode, int *array_index)
356 {
357         char *base=NULL, *prop=NULL;
358         static char buf[128];
359         
360         /* base part of path */
361         if (adrcode & MA_MAP1) base= "textures[0]";
362         else if (adrcode & MA_MAP2) base= "textures[1]";
363         else if (adrcode & MA_MAP3) base= "textures[2]";
364         else if (adrcode & MA_MAP4) base= "textures[3]";
365         else if (adrcode & MA_MAP5) base= "textures[4]";
366         else if (adrcode & MA_MAP6) base= "textures[5]";
367         else if (adrcode & MA_MAP7) base= "textures[6]";
368         else if (adrcode & MA_MAP8) base= "textures[7]";
369         else if (adrcode & MA_MAP9) base= "textures[8]";
370         else if (adrcode & MA_MAP10) base= "textures[9]";
371         else if (adrcode & MA_MAP11) base= "textures[10]";
372         else if (adrcode & MA_MAP12) base= "textures[11]";
373         else if (adrcode & MA_MAP13) base= "textures[12]";
374         else if (adrcode & MA_MAP14) base= "textures[13]";
375         else if (adrcode & MA_MAP15) base= "textures[14]";
376         else if (adrcode & MA_MAP16) base= "textures[15]";
377         else if (adrcode & MA_MAP17) base= "textures[16]";
378         else if (adrcode & MA_MAP18) base= "textures[17]";
379                 
380         /* property identifier for path */
381         adrcode= (adrcode & (MA_MAP1-1));
382         switch (adrcode) {
383 #if 0 // XXX these are not wrapped in RNA yet!
384                 case MAP_OFS_X:
385                         poin= &(mtex->ofs[0]); break;
386                 case MAP_OFS_Y:
387                         poin= &(mtex->ofs[1]); break;
388                 case MAP_OFS_Z:
389                         poin= &(mtex->ofs[2]); break;
390                 case MAP_SIZE_X:
391                         poin= &(mtex->size[0]); break;
392                 case MAP_SIZE_Y:
393                         poin= &(mtex->size[1]); break;
394                 case MAP_SIZE_Z:
395                         poin= &(mtex->size[2]); break;
396                 case MAP_R:
397                         poin= &(mtex->r); break;
398                 case MAP_G:
399                         poin= &(mtex->g); break;
400                 case MAP_B:
401                         poin= &(mtex->b); break;
402                 case MAP_DVAR:
403                         poin= &(mtex->def_var); break;
404                 case MAP_COLF:
405                         poin= &(mtex->colfac); break;
406                 case MAP_NORF:
407                         poin= &(mtex->norfac); break;
408                 case MAP_VARF:
409                         poin= &(mtex->varfac); break;
410 #endif
411                 case MAP_DISP:
412                         prop= "warp_factor"; break;
413         }
414         
415         /* only build and return path if there's a property */
416         if (prop) {
417                 BLI_snprintf(buf, 128, "%s.%s", base, prop);
418                 return buf;
419         }
420         else
421                 return NULL;
422 }
423
424 /* Texture types */
425 static char *texture_adrcodes_to_paths (int adrcode, int *array_index)
426 {
427         /* set array index like this in-case nothing sets it correctly  */
428         *array_index= 0;
429         
430         /* result depends on adrcode */
431         switch (adrcode) {
432                 case TE_NSIZE:
433                         return "noise_size";
434                 case TE_TURB:
435                         return "turbulence";
436                         
437                 case TE_NDEPTH: // XXX texture RNA undefined
438                         //poin= &(tex->noisedepth); *type= IPO_SHORT; break;
439                         break;
440                 case TE_NTYPE: // XXX texture RNA undefined
441                         //poin= &(tex->noisetype); *type= IPO_SHORT; break;
442                         break;
443                         
444                 case TE_N_BAS1:
445                         return "noise_basis";
446                 case TE_N_BAS2:
447                         return "noise_basis"; // XXX this is not yet defined in RNA...
448                 
449                         /* voronoi */
450                 case TE_VNW1:
451                         *array_index= 0; return "feature_weights";
452                 case TE_VNW2:
453                         *array_index= 1; return "feature_weights";
454                 case TE_VNW3:
455                         *array_index= 2; return "feature_weights";
456                 case TE_VNW4:
457                         *array_index= 3; return "feature_weights";
458                 case TE_VNMEXP:
459                         return "minkovsky_exponent";
460                 case TE_VN_DISTM:
461                         return "distance_metric";
462                 case TE_VN_COLT:
463                         return "color_type";
464                 
465                         /* distorted noise / voronoi */
466                 case TE_ISCA:
467                         return "noise_intensity";
468                         
469                         /* distorted noise */
470                 case TE_DISTA:
471                         return "distortion_amount";
472                 
473                         /* musgrave */
474                 case TE_MG_TYP: // XXX texture RNA undefined
475                 //      poin= &(tex->stype); *type= IPO_SHORT; break;
476                         break;
477                 case TE_MGH:
478                         return "highest_dimension";
479                 case TE_MG_LAC:
480                         return "lacunarity";
481                 case TE_MG_OCT:
482                         return "octaves";
483                 case TE_MG_OFF:
484                         return "offset";
485                 case TE_MG_GAIN:
486                         return "gain";
487                         
488                 case TE_COL_R:
489                         *array_index= 0; return "rgb_factor";
490                 case TE_COL_G:
491                         *array_index= 1; return "rgb_factor";
492                 case TE_COL_B:
493                         *array_index= 2; return "rgb_factor";
494                         
495                 case TE_BRIGHT:
496                         return "brightness";
497                 case TE_CONTRA:
498                         return "constrast";
499         }
500         
501         return NULL;
502 }
503
504 /* Material Types */
505 static char *material_adrcodes_to_paths (int adrcode, int *array_index)
506 {
507         /* set array index like this in-case nothing sets it correctly  */
508         *array_index= 0;
509         
510         /* result depends on adrcode */
511         switch (adrcode) {
512                 case MA_COL_R:
513                         *array_index= 0; return "diffuse_color";
514                 case MA_COL_G:
515                         *array_index= 1; return "diffuse_color";
516                 case MA_COL_B:
517                         *array_index= 2; return "diffuse_color";
518                         
519                 case MA_SPEC_R:
520                         *array_index= 0; return "specular_color";
521                 case MA_SPEC_G:
522                         *array_index= 1; return "specular_color";
523                 case MA_SPEC_B:
524                         *array_index= 2; return "specular_color";
525                         
526                 case MA_MIR_R:
527                         *array_index= 0; return "mirror_color";
528                 case MA_MIR_G:
529                         *array_index= 1; return "mirror_color";
530                 case MA_MIR_B:
531                         *array_index= 2; return "mirror_color";
532                         
533                 case MA_ALPHA:
534                         return "alpha";
535                         
536                 case MA_REF:
537                         return "diffuse_reflection";
538                 
539                 
540                 default: /* for now, we assume that the others were MTex channels */
541                         return mtex_adrcodes_to_paths(adrcode, array_index);
542         }
543         
544         return NULL;
545         
546 #if 0
547         case MA_EMIT:
548                 poin= &(ma->emit); break;
549         case MA_AMB:
550                 poin= &(ma->amb); break;
551         case MA_SPEC:
552                 poin= &(ma->spec); break;
553         case MA_HARD:
554                 poin= &(ma->har); *type= IPO_SHORT; break;
555         case MA_SPTR:
556                 poin= &(ma->spectra); break;
557         case MA_IOR:
558                 poin= &(ma->ang); break;
559         case MA_HASIZE:
560                 poin= &(ma->hasize); break;
561         case MA_TRANSLU:
562                 poin= &(ma->translucency); break;
563         case MA_RAYM:
564                 poin= &(ma->ray_mirror); break;
565         case MA_FRESMIR:
566                 poin= &(ma->fresnel_mir); break;
567         case MA_FRESMIRI:
568                 poin= &(ma->fresnel_mir_i); break;
569         case MA_FRESTRA:
570                 poin= &(ma->fresnel_tra); break;
571         case MA_FRESTRAI:
572                 poin= &(ma->fresnel_tra_i); break;
573         case MA_ADD:
574                 poin= &(ma->add); break;
575 #endif
576         
577 }
578
579 /* Camera Types */
580 static char *camera_adrcodes_to_paths (int adrcode, int *array_index)
581 {
582         /* set array index like this in-case nothing sets it correctly  */
583         *array_index= 0;
584         
585         /* result depends on adrcode */
586         switch (adrcode) {
587                 case CAM_LENS:
588 #if 0 // XXX this cannot be resolved easily... perhaps we assume camera is perspective (works for most cases...
589                         if (ca->type == CAM_ORTHO)
590                                 return "ortho_scale";
591                         else
592                                 return "lens"; 
593 #endif // XXX this cannot be resolved easily
594                         break;
595                         
596                 case CAM_STA:
597                         return "clip_start";
598                 case CAM_END:
599                         return "clip_end";
600                         
601 #if 0 // XXX these are not defined in RNA
602                 case CAM_YF_APERT:
603                         poin= &(ca->YF_aperture); break;
604                 case CAM_YF_FDIST:
605                         poin= &(ca->YF_dofdist); break;
606 #endif // XXX these are not defined in RNA
607                         
608                 case CAM_SHIFT_X:
609                         return "shift_x";
610                 case CAM_SHIFT_Y:
611                         return "shift_y";
612         }
613         
614         /* unrecognised adrcode, or not-yet-handled ones! */
615         return NULL;
616 }
617
618 /* Lamp Types */
619 static char *lamp_adrcodes_to_paths (int adrcode, int *array_index)
620 {
621         /* set array index like this in-case nothing sets it correctly  */
622         *array_index= 0;
623         
624         /* result depends on adrcode */
625         switch (adrcode) {
626                 case LA_ENERGY:
627                         return "energy";
628                         
629                 case LA_COL_R:
630                         *array_index= 0;  return "color";
631                 case LA_COL_G:
632                         *array_index= 1;  return "color";
633                 case LA_COL_B:
634                         *array_index= 2;  return "color";
635                         
636                 case LA_DIST:
637                         return "distance";
638                 
639                 case LA_SPOTSI:
640                         return "spot_size";
641                 case LA_SPOTBL:
642                         return "spot_blend";
643                         
644                 case LA_QUAD1:
645                         return "linear_attenuation";
646                 case LA_QUAD2:
647                         return "quadratic_attenuation";
648                         
649                 case LA_HALOINT:
650                         return "halo_intensity";
651                         
652                 default: /* for now, we assume that the others were MTex channels */
653                         return mtex_adrcodes_to_paths(adrcode, array_index);
654         }
655         
656         /* unrecognised adrcode, or not-yet-handled ones! */
657         return NULL;
658 }
659
660 /* ------- */
661
662 /* Allocate memory for RNA-path for some property given a blocktype, adrcode, and 'root' parts of path
663  *      Input:
664  *              - blocktype, adrcode    - determines setting to get
665  *              - actname, constname    - used to build path
666  *      Output:
667  *              - array_index                   - index in property's array (if applicable) to use
668  *              - return                                - the allocated path...
669  */
670 char *get_rna_access (int blocktype, int adrcode, char actname[], char constname[], int *array_index)
671 {
672         DynStr *path= BLI_dynstr_new();
673         char *propname=NULL, *rpath=NULL;
674         char buf[512];
675         int dummy_index= 0;
676         
677         /* get property name based on blocktype */
678         switch (blocktype) {
679                 case ID_OB: /* object */
680                         propname= ob_adrcodes_to_paths(adrcode, &dummy_index);
681                         break;
682                 
683                 case ID_PO: /* pose channel */
684                         propname= pchan_adrcodes_to_paths(adrcode, &dummy_index);
685                         break;
686                         
687                 case ID_KE: /* shapekeys */
688                         propname= shapekey_adrcodes_to_paths(adrcode, &dummy_index);
689                         break;
690                         
691                 case ID_CO: /* constraint */
692                         propname= constraint_adrcodes_to_paths(adrcode, &dummy_index);
693                         break;
694                         
695                 case ID_TE: /* texture */
696                         propname= texture_adrcodes_to_paths(adrcode, &dummy_index);
697                         break;
698                         
699                 case ID_MA: /* material */
700                         propname= material_adrcodes_to_paths(adrcode, &dummy_index);
701                         break;
702                         
703                 case ID_CA: /* camera */
704                         propname= camera_adrcodes_to_paths(adrcode, &dummy_index);
705                         break;
706                         
707                 case ID_LA: /* lamp */
708                         propname= lamp_adrcodes_to_paths(adrcode, &dummy_index);
709                         break;
710                         
711                 /* XXX problematic blocktypes */
712                 case ID_CU: /* curve */
713                         propname= "speed"; // XXX this was a 'dummy curve' that didn't really correspond to any real var...
714                         break;
715                         
716                 case ID_SEQ: /* sequencer strip */
717                         //SEQ_FAC1:
718                         //      poin= &(seq->facf0); // XXX this doesn't seem to be included anywhere in sequencer RNA...
719                         break;
720                         
721                 /* special hacks */
722                 case -1:
723                         /* special case for rotdiff drivers... we don't need a property for this... */
724                         break;
725                         
726                 // TODO... add other blocktypes...
727                 default:
728                         printf("IPO2ANIMATO WARNING: No path for blocktype %d, adrcode %d yet \n", blocktype, adrcode);
729                         break;
730         }
731         
732         /* check if any property found 
733          *      - blocktype < 0 is special case for a specific type of driver, where we don't need a property name...
734          */
735         if ((propname == NULL) && (blocktype > 0)) {
736                 /* nothing was found, so exit */
737                 if (array_index) 
738                         *array_index= 0;
739                         
740                 BLI_dynstr_free(path);
741                 
742                 return NULL;
743         }
744         else {
745                 if (array_index)
746                         *array_index= dummy_index;
747         }
748         
749         /* append preceeding bits to path */
750         if ((actname && actname[0]) && (constname && constname[0])) {
751                 /* Constraint in Pose-Channel */
752                 sprintf(buf, "pose.pose_channels[\"%s\"].constraints[\"%s\"]", actname, constname);
753         }
754         else if (actname && actname[0]) {
755                 /* Pose-Channel */
756                 sprintf(buf, "pose.pose_channels[\"%s\"]", actname);
757         }
758         else if (constname && constname[0]) {
759                 /* Constraint in Object */
760                 sprintf(buf, "constraints[\"%s\"]", constname);
761         }
762         else
763                 strcpy(buf, ""); /* empty string */
764         BLI_dynstr_append(path, buf);
765         
766         /* append property to path (only if applicable) */
767         if (blocktype > 0) {
768                 /* need to add dot before property if there was anything precceding this */
769                 if (buf[0])
770                         BLI_dynstr_append(path, ".");
771                 
772                 /* now write name of property */
773                 BLI_dynstr_append(path, propname);
774         }
775         
776         /* convert to normal MEM_malloc'd string */
777         rpath= BLI_dynstr_get_cstring(path);
778         BLI_dynstr_free(path);
779         
780         /* return path... */
781         return rpath;
782 }
783
784 /* *************************************************** */
785 /* Conversion Utilities */
786
787 /* Convert IpoDriver to ChannelDriver - will free the old data (i.e. the old driver) */
788 static ChannelDriver *idriver_to_cdriver (IpoDriver *idriver)
789 {
790         ChannelDriver *cdriver;
791         
792         /* allocate memory for new driver */
793         cdriver= MEM_callocN(sizeof(ChannelDriver), "ChannelDriver");
794         
795         /* if 'pydriver', just copy data across */
796         if (idriver->type == IPO_DRIVER_TYPE_PYTHON) {
797                 /* PyDriver only requires the expression to be copied */
798                 cdriver->type = DRIVER_TYPE_PYTHON;
799                 strcpy(cdriver->expression, idriver->name); // XXX is this safe? 
800         }
801         else {
802                 /* what to store depends on the 'blocktype' (ID_OB or ID_PO - object or posechannel) */
803                 if (idriver->blocktype == ID_AR) {
804                         /* ID_PO */
805                         if (idriver->adrcode == OB_ROT_DIFF) {
806                                 if (G.f & G_DEBUG) printf("idriver_to_cdriver - rotdiff %p \n", idriver->ob);
807                                 /* Rotational Difference is a special type of driver now... */
808                                 cdriver->type= DRIVER_TYPE_ROTDIFF;
809                                 
810                                 /* driver must use bones from same armature... */
811                                 cdriver->id= cdriver->id2= (ID *)idriver->ob;
812                                 
813                                 /* paths for the two targets get the pointers to the relevant Pose-Channels 
814                                  *      - return pointers to Pose-Channels not rotation channels, as calculation code is picky
815                                  *      - old bone names were stored in same var, in idriver->name
816                                  *
817                                  *      - we use several hacks here - blocktype == -1 specifies that no property needs to be found, and
818                                  *        providing a name for 'actname' will automatically imply Pose-Channel with name 'actname'
819                                  */
820                                 cdriver->rna_path= get_rna_access(-1, -1, idriver->name, NULL, NULL);
821                                 cdriver->rna_path2= get_rna_access(-1, -1, idriver->name+DRIVER_NAME_OFFS, NULL, NULL);
822                         }
823                         else {
824                                 if (G.f & G_DEBUG) printf("idriver_to_cdriver - arm  %p \n", idriver->ob);
825                                 /* 'standard' driver */
826                                 cdriver->type= DRIVER_TYPE_CHANNEL;
827                                 cdriver->id= (ID *)idriver->ob;
828                                 
829                                 switch (idriver->adrcode) {
830                                         case OB_LOC_X:  /* x,y,z location are quite straightforward */
831                                                 cdriver->rna_path= get_rna_access(ID_PO, AC_LOC_X, idriver->name, NULL, &cdriver->array_index);
832                                                 break;
833                                         case OB_LOC_Y:
834                                                 cdriver->rna_path= get_rna_access(ID_PO, AC_LOC_Y, idriver->name, NULL, &cdriver->array_index);
835                                                 break;
836                                         case OB_LOC_Z:
837                                                 cdriver->rna_path= get_rna_access(ID_PO, AC_LOC_Z, idriver->name, NULL, &cdriver->array_index);
838                                                 break;
839                                                 
840                                         case OB_SIZE_X: /* x,y,z scaling are also quite straightforward */
841                                                 cdriver->rna_path= get_rna_access(ID_PO, AC_SIZE_X, idriver->name, NULL, &cdriver->array_index);
842                                                 break;
843                                         case OB_SIZE_Y:
844                                                 cdriver->rna_path= get_rna_access(ID_PO, AC_SIZE_Y, idriver->name, NULL, &cdriver->array_index);
845                                                 break;
846                                         case OB_SIZE_Z:
847                                                 cdriver->rna_path= get_rna_access(ID_PO, AC_SIZE_Z, idriver->name, NULL, &cdriver->array_index);
848                                                 break;  
849                                                 
850                                         case OB_ROT_X:  /* rotation - we need to be careful with this... XXX (another reason why we need eulers) */     
851                                         case OB_ROT_Y:
852                                         case OB_ROT_Z:
853                                         {
854                                                 // XXX this is not yet a 1:1 map, since we'd need euler rotations to make this work nicely (unless we make some hacks)
855                                                 // XXX -1 here is a special hack...
856                                                 cdriver->rna_path= get_rna_access(ID_PO, -1, idriver->name, NULL, NULL);
857                                                 cdriver->array_index= idriver->adrcode - OB_ROT_X;
858                                         }
859                                                 break;
860                                 }
861                         }
862                 }
863                 else {
864                         /* ID_OB */
865                         if (G.f & G_DEBUG) printf("idriver_to_cdriver  - ob %p \n", idriver->ob);
866                         cdriver->type= DRIVER_TYPE_CHANNEL;
867                         cdriver->id= (ID *)idriver->ob;
868                         cdriver->rna_path= get_rna_access(ID_OB, idriver->adrcode, NULL, NULL, &cdriver->array_index);
869                 }
870         }
871         
872         if (G.f & G_DEBUG) printf("\tcdriver -> id = %p \n", cdriver->id);
873         
874         /* free old driver */
875         MEM_freeN(idriver);
876         
877         /* return the new one */
878         return cdriver;
879 }
880
881 /* Convert IPO-Curve to F-Curve (including Driver data), and free any of the old data that 
882  * is not relevant, BUT do not free the IPO-Curve itself...
883  *      actname: name of Action-Channel (if applicable) that IPO-Curve's IPO-block belonged to
884  *      constname: name of Constraint-Channel (if applicable) that IPO-Curve's IPO-block belonged to
885  */
886 static void icu_to_fcurves (ListBase *list, IpoCurve *icu, char *actname, char *constname)
887 {
888         AdrBit2Path *abp;
889         FCurve *fcu;
890         int i=0, totbits;
891         
892         /* allocate memory for a new F-Curve */
893         fcu= MEM_callocN(sizeof(FCurve), "FCurve");
894         
895         /* convert driver - will free the old one... */
896         if (icu->driver) {
897                 fcu->driver= idriver_to_cdriver(icu->driver);
898                 icu->driver= NULL;
899         }
900         
901         /* copy flags */
902         if (icu->flag & IPO_VISIBLE) fcu->flag |= FCURVE_VISIBLE;
903         if (icu->flag & IPO_SELECT) fcu->flag |= FCURVE_SELECTED;
904         if (icu->flag & IPO_ACTIVE) fcu->flag |= FCURVE_ACTIVE;
905         if (icu->flag & IPO_MUTE) fcu->flag |= FCURVE_MUTED;
906         if (icu->flag & IPO_PROTECT) fcu->flag |= FCURVE_PROTECTED;
907         if (icu->flag & IPO_AUTO_HORIZ) fcu->flag |= FCURVE_AUTO_HANDLES;
908         
909         /* set extrapolation */
910         switch (icu->extrap) {
911                 case IPO_HORIZ: /* constant extrapolation */
912                 case IPO_DIR: /* linear extrapolation */
913                 {
914                         /* just copy, as the new defines match the old ones... */
915                         fcu->extend= icu->extrap;
916                 }
917                         break;
918                         
919                 case IPO_CYCL: /* cyclic extrapolation */
920                 case IPO_CYCLX: /* cyclic extrapolation + offset */
921                 {
922                         /* Add a new FModifier (Cyclic) instead of setting extend value 
923                          * as that's the new equivilant of that option. 
924                          */
925                         FModifier *fcm= fcurve_add_modifier(fcu, FMODIFIER_TYPE_CYCLES);
926                         FMod_Cycles *data= (FMod_Cycles *)fcm->data;
927                         
928                         /* if 'offset' one is in use, set appropriate settings */
929                         if (icu->extrap == IPO_CYCLX)
930                                 data->before_mode= data->after_mode= FCM_EXTRAPOLATE_CYCLIC_OFFSET;
931                         else
932                                 data->before_mode= data->after_mode= FCM_EXTRAPOLATE_CYCLIC;
933                 }
934                         break;
935         }
936         
937         /* -------- */
938         
939         /* get adrcode <-> bitflags mapping to handle nasty bitflag curves? */
940         abp= adrcode_bitmaps_to_paths(icu->blocktype, icu->adrcode, &totbits);
941         if (abp && totbits) {
942                 FCurve *fcurve;
943                 int b;
944                 
945                 if (G.f & G_DEBUG) printf("\tconvert bitflag ipocurve, totbits = %d \n", totbits);
946                 
947                 /* add the 'only int values' flag */
948                 fcu->flag |= FCURVE_INT_VALUES;         
949                 
950                 /* for each bit we have to remap + check for:
951                  * 1) we need to make copy the existing F-Curve data (fcu -> fcurve),
952                  *        except for the last one which will use the original 
953                  * 2) copy the relevant path info across
954                  * 3) filter the keyframes for the flag of interest
955                  */
956                 for (b=0; b < totbits; b++, abp++) {
957                         /* make a copy of existing base-data if not the last curve */
958                         if (b < (totbits-1))
959                                 fcurve= copy_fcurve(fcu);
960                         else
961                                 fcurve= fcu;
962                                 
963                         /* set path */
964                         fcurve->rna_path= BLI_strdupn(abp->path, strlen(abp->path));
965                         fcurve->array_index= abp->array_index;
966                         
967                         /* convert keyframes 
968                          *      - beztriples and bpoints are mutually exclusive, so we won't have both at the same time
969                          *      - beztriples are more likely to be encountered as they are keyframes (the other type wasn't used yet)
970                          */
971                         fcurve->totvert= icu->totvert;
972                         
973                         if (icu->bezt) {
974                                 BezTriple *dst, *src;
975                                 
976                                 /* allocate new array for keyframes/beztriples */
977                                 fcurve->bezt= MEM_callocN(sizeof(BezTriple)*fcurve->totvert, "BezTriples");
978                                 
979                                 /* loop through copying all BezTriples individually, as we need to modify a few things */
980                                 for (dst=fcurve->bezt, src=icu->bezt; i < fcurve->totvert; i++, dst++, src++) {
981                                         /* firstly, copy BezTriple data */
982                                         *dst= *src;
983                                         
984                                         /* interpolation can only be constant... */
985                                         dst->ipo= BEZT_IPO_CONST;
986                                         
987                                         /* correct values, by checking if the flag of interest is set */
988                                         if ( ((int)(dst->vec[1][1])) & (abp->bit) )
989                                                 dst->vec[0][1]= dst->vec[1][1]= dst->vec[2][1] = 1.0f;
990                                         else
991                                                 dst->vec[0][1]= dst->vec[1][1]= dst->vec[2][1] = 0.0f;
992                                 }
993                         }
994                         else if (icu->bp) {
995                                 /* TODO: need to convert from BPoint type to the more compact FPoint type... but not priority, since no data used this */
996                                 //BPoint *bp;
997                                 //FPoint *fpt;
998                         }
999                         
1000                         /* add new F-Curve to list */
1001                         BLI_addtail(list, fcurve);
1002                 }
1003                 
1004                 /* free old data of curve now that it's no longer needed for converting any more curves */
1005                 if (icu->bezt) MEM_freeN(icu->bezt);
1006                 if (icu->bp) MEM_freeN(icu->bezt);
1007         }
1008         else {
1009                 /* get rna-path
1010                  *      - we will need to set the 'disabled' flag if no path is able to be made (for now)
1011                  */
1012                 fcu->rna_path= get_rna_access(icu->blocktype, icu->adrcode, actname, constname, &fcu->array_index);
1013                 if (fcu->rna_path == NULL)
1014                         fcu->flag |= FCURVE_DISABLED;
1015                 
1016                 /* convert keyframes 
1017                  *      - beztriples and bpoints are mutually exclusive, so we won't have both at the same time
1018                  *      - beztriples are more likely to be encountered as they are keyframes (the other type wasn't used yet)
1019                  */
1020                 fcu->totvert= icu->totvert;
1021                 
1022                 if (icu->bezt) {
1023                         BezTriple *dst, *src;
1024                         
1025                         /* allocate new array for keyframes/beztriples */
1026                         fcu->bezt= MEM_callocN(sizeof(BezTriple)*fcu->totvert, "BezTriples");
1027                         
1028                         /* loop through copying all BezTriples individually, as we need to modify a few things */
1029                         for (dst=fcu->bezt, src=icu->bezt; i < fcu->totvert; i++, dst++, src++) {
1030                                 /* firstly, copy BezTriple data */
1031                                 *dst= *src;
1032                                 
1033                                 /* now copy interpolation from curve (if not already set) */
1034                                 if (icu->ipo != IPO_MIXED)
1035                                         dst->ipo= icu->ipo;
1036                                         
1037                                 /* correct values for euler rotation curves - they were degrees/10 */
1038                                 // XXX for now, just make them into radians as RNA sets/reads directly in that form
1039                                 if ( ((icu->blocktype == ID_OB) && ELEM3(icu->adrcode, OB_ROT_X, OB_ROT_Y, OB_ROT_Z)) ||
1040                                          ((icu->blocktype == ID_PO) && ELEM3(icu->adrcode, AC_EUL_X, AC_EUL_Y, AC_EUL_Z)) )
1041                                 {
1042                                         const float fac= (float)M_PI / 18.0f; //10.0f * M_PI/180.0f;
1043                                         
1044                                         dst->vec[0][1] *= fac;
1045                                         dst->vec[1][1] *= fac;
1046                                         dst->vec[2][1] *= fac;
1047                                 }
1048                         }
1049                         
1050                         /* free this data now */
1051                         MEM_freeN(icu->bezt);
1052                 }
1053                 else if (icu->bp) {
1054                         /* TODO: need to convert from BPoint type to the more compact FPoint type... but not priority, since no data used this */
1055                         //BPoint *bp;
1056                         //FPoint *fpt;
1057                 }
1058                 
1059                 /* add new F-Curve to list */
1060                 BLI_addtail(list, fcu);
1061         }
1062 }
1063
1064 /* ------------------------- */
1065
1066 /* Convert IPO-block (i.e. all its IpoCurves) to the new system.
1067  * This does not assume that any ID or AnimData uses it, but does assume that
1068  * it is given two lists, which it will perform driver/animation-data separation.
1069  */
1070 static void ipo_to_animato (Ipo *ipo, char actname[], char constname[], ListBase *anim, ListBase *drivers)
1071 {
1072         IpoCurve *icu, *icn;
1073         
1074         /* sanity check */
1075         if (ELEM3(NULL, ipo, anim, drivers))
1076                 return;
1077                 
1078         if (G.f & G_DEBUG) printf("ipo_to_animato \n");
1079                 
1080         /* validate actname and constname 
1081          *      - clear actname if it was one of the generic <builtin> ones (i.e. 'Object', or 'Shapes')
1082          *      - actname can then be used to assign F-Curves in Action to Action Groups 
1083          *        (i.e. thus keeping the benefits that used to be provided by Action Channels for grouping
1084          *              F-Curves for bones). This may be added later... for now let's just dump without them...
1085          */
1086         if (actname) {
1087                 if ((ipo->blocktype == ID_OB) && (strcmp(actname, "Object") == 0))
1088                         actname= NULL;
1089                 else if ((ipo->blocktype == ID_OB) && (strcmp(actname, "Shape") == 0))
1090                         actname= NULL;
1091         }
1092         
1093         /* loop over IPO-Curves, freeing as we progress */
1094         for (icu= ipo->curve.first; icu; icu= icn) {
1095                 /* get link to next (for later) */
1096                 icn= icu->next;
1097                 
1098                 /* Since an IPO-Curve may end up being made into many F-Curves (i.e. bitflag curves), 
1099                  * we figure out the best place to put the channel, then tell the curve-converter to just dump there
1100                  */
1101                 if (icu->driver)
1102                         icu_to_fcurves(drivers, icu, actname, constname);
1103                 else
1104                         icu_to_fcurves(anim, icu, actname, constname);
1105                 
1106                 /* free this IpoCurve now that it's been converted */
1107                 BLI_freelinkN(&ipo->curve, icu);
1108         }
1109 }
1110
1111 /* Convert Action-block to new system, separating animation and drivers
1112  * New curves may not be converted directly into the given Action (i.e. for Actions linked
1113  * to Objects, where ob->ipo and ob->action need to be combined).
1114  * NOTE: we need to be careful here, as same data-structs are used for new system too!
1115  */
1116 static void action_to_animato (bAction *act, ListBase *curves, ListBase *drivers)
1117 {
1118         bActionChannel *achan, *achann;
1119         bConstraintChannel *conchan, *conchann;
1120         
1121         /* only continue if there are Action Channels (indicating unconverted data) */
1122         if (act->chanbase.first == NULL)
1123                 return;
1124                 
1125         /* get rid of all Action Groups */
1126         // XXX this is risky if there's some old + some new data in the Action...
1127         if (act->groups.first) 
1128                 BLI_freelistN(&act->groups);
1129         
1130         /* loop through Action-Channels, converting data, freeing as we go */
1131         for (achan= act->chanbase.first; achan; achan= achann) {
1132                 /* get pointer to next Action Channel */
1133                 achann= achan->next;
1134                 
1135                 /* convert Action Channel's IPO data */
1136                 if (achan->ipo) {
1137                         ipo_to_animato(achan->ipo, achan->name, NULL, curves, drivers);
1138                         achan->ipo->id.us--;
1139                         achan->ipo= NULL;
1140                 }
1141                 
1142                 /* convert constraint channel IPO-data */
1143                 for (conchan= achan->constraintChannels.first; conchan; conchan= conchann) {
1144                         /* get pointer to next Constraint Channel */
1145                         conchann= conchan->next;
1146                         
1147                         /* convert Constraint Channel's IPO data */
1148                         if (conchan->ipo) {
1149                                 ipo_to_animato(conchan->ipo, achan->name, conchan->name, curves, drivers);
1150                                 conchan->ipo->id.us--;
1151                                 conchan->ipo= NULL;
1152                         }
1153                         
1154                         /* free Constraint Channel */
1155                         BLI_freelinkN(&achan->constraintChannels, conchan);
1156                 }
1157                 
1158                 /* free Action Channel */
1159                 BLI_freelinkN(&act->chanbase, achan);
1160         }
1161 }
1162
1163
1164 /* ------------------------- */
1165
1166 /* Convert IPO-block (i.e. all its IpoCurves) for some ID to the new system
1167  * This assumes that AnimData has been added already. Separation of drivers
1168  * from animation data is accomplished here too...
1169  */
1170 static void ipo_to_animdata (ID *id, Ipo *ipo, char actname[], char constname[])
1171 {
1172         AnimData *adt= BKE_animdata_from_id(id);
1173         ListBase anim = {NULL, NULL};
1174         ListBase drivers = {NULL, NULL};
1175         
1176         /* sanity check */
1177         if ELEM(NULL, id, ipo)
1178                 return;
1179         if (adt == NULL) {
1180                 printf("ERROR ipo_to_animdata(): adt invalid \n");
1181                 return;
1182         }
1183         
1184         if (G.f & G_DEBUG) {
1185                 printf("ipo to animdata - ID:%s, IPO:%s, actname:%s constname:%s  curves:%d \n", 
1186                         id->name+2, ipo->id.name+2, (actname)?actname:"<None>", (constname)?constname:"<None>", 
1187                         BLI_countlist(&ipo->curve));
1188         }
1189         
1190         /* Convert curves to animato system (separated into separate lists of F-Curves for animation and drivers),
1191          * and the try to put these lists in the right places, but do not free the lists here
1192          */
1193         ipo_to_animato(ipo, actname, constname, &anim, &drivers);
1194         
1195         /* deal with animation first */
1196         if (anim.first) {
1197                 if (G.f & G_DEBUG) printf("\thas anim \n");
1198                 /* try to get action */
1199                 if (adt->action == NULL) {
1200                         adt->action= add_empty_action("ConvData_Action"); // XXX we need a better name for this
1201                         if (G.f & G_DEBUG) printf("\t\tadded new action \n");
1202                 }
1203                 
1204                 /* add F-Curves to action */
1205                 addlisttolist(&adt->action->curves, &anim);
1206         }
1207         
1208         /* deal with drivers */
1209         if (drivers.first) {
1210                 if (G.f & G_DEBUG) printf("\thas drivers \n");
1211                 /* add drivers to end of driver stack */
1212                 addlisttolist(&adt->drivers, &drivers);
1213         }
1214 }
1215
1216 /* Convert Action-block to new system
1217  * NOTE: we need to be careful here, as same data-structs are used for new system too!
1218  */
1219 static void action_to_animdata (ID *id, bAction *act)
1220 {
1221         AnimData *adt= BKE_animdata_from_id(id);
1222         
1223         /* only continue if there are Action Channels (indicating unconverted data) */
1224         if (ELEM(NULL, adt, act->chanbase.first))
1225                 return;
1226         
1227         /* check if we need to set this Action as the AnimData's action */
1228         if (adt->action == NULL) {
1229                 /* set this Action as AnimData's Action */
1230                 if (G.f & G_DEBUG) printf("act_to_adt - set adt action to act \n");
1231                 adt->action= act;
1232         }
1233         
1234         /* convert Action data */
1235         action_to_animato(act, &adt->action->curves, &adt->drivers);
1236 }
1237
1238 /* *************************************************** */
1239 /* External API - Only Called from do_versions() */
1240
1241 /* Called from do_versions() in readfile.c to convert the old 'IPO/adrcode' system
1242  * to the new 'Animato/RNA' system.
1243  *
1244  * The basic method used here, is to loop over datablocks which have IPO-data, and 
1245  * add those IPO's to new AnimData blocks as Actions. 
1246  * Action/NLA data only works well for Objects, so these only need to be checked for there.
1247  *  
1248  * Data that has been converted should be freed immediately, which means that it is immediately
1249  * clear which datablocks have yet to be converted, and also prevent freeing errors when we exit.
1250  */
1251 // XXX currently done after all file reading... 
1252 void do_versions_ipos_to_animato(Main *main)
1253 {
1254         ListBase drivers = {NULL, NULL};
1255         ID *id;
1256         AnimData *adt;
1257         
1258         if (main == NULL) {
1259                 printf("Argh! Main is NULL in do_versions_ipos_to_animato() \n");
1260                 return;
1261         }
1262                 
1263         /* only convert if version is right */
1264         // XXX???
1265         if (main->versionfile >= 250) {
1266                 printf("WARNING: Animation data too new to convert (Version %d) \n", main->versionfile);
1267                 return;
1268         }
1269         else
1270                 printf("INFO: Converting to Animato... \n"); // xxx debug
1271                 
1272         /* ----------- Animation Attached to Data -------------- */
1273         
1274         /* objects */
1275         for (id= main->object.first; id; id= id->next) {
1276                 Object *ob= (Object *)id;
1277                 bPoseChannel *pchan;
1278                 bConstraint *con;
1279                 bConstraintChannel *conchan, *conchann;
1280                 
1281                 if (G.f & G_DEBUG) printf("\tconverting ob %s \n", id->name+2);
1282                 
1283                 /* check if object has any animation data */
1284                 if ((ob->ipo) || (ob->action) || (ob->nlastrips.first)) {
1285                         /* Add AnimData block */
1286                         adt= BKE_id_add_animdata(id);
1287                         
1288                         /* IPO first */
1289                         if (ob->ipo) {
1290                                 ipo_to_animdata(id, ob->ipo, NULL, NULL);
1291                                 ob->ipo->id.us--;
1292                                 ob->ipo= NULL;
1293                         }
1294                         
1295                         /* now Action */
1296                         if (ob->action) {
1297                                 action_to_animdata(id, ob->action);
1298                                 
1299                                 /* only decrease usercount if this Action isn't now being used by AnimData */
1300                                 if (ob->action != adt->action) {
1301                                         ob->action->id.us--;
1302                                         ob->action= NULL;
1303                                 }
1304                         }
1305                         
1306                         /* finally NLA */
1307                         // XXX todo... for now, new NLA code not hooked up yet, so keep old stuff (but not for too long!)
1308                 }
1309                 
1310                 /* check PoseChannels for constraints with local data */
1311                 if (ob->pose) {
1312                         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1313                                 for (con= pchan->constraints.first; con; con= con->next) {
1314                                         /* if constraint has own IPO, convert add these to Object 
1315                                          * (NOTE: they're most likely to be drivers too) 
1316                                          */
1317                                         if (con->ipo) {
1318                                                 /* although this was the constraint's local IPO, we still need to provide pchan + con 
1319                                                  * so that drivers can be added properly...
1320                                                  */
1321                                                 ipo_to_animdata(id, con->ipo, pchan->name, con->name);
1322                                                 con->ipo->id.us--;
1323                                                 con->ipo= NULL;
1324                                         }
1325                                 }
1326                         }
1327                 }
1328                 
1329                 /* check constraints for local IPO's */
1330                 for (con= ob->constraints.first; con; con= con->next) {
1331                         /* if constraint has own IPO, convert add these to Object 
1332                          * (NOTE: they're most likely to be drivers too) 
1333                          */
1334                         if (con->ipo) {
1335                                 /* although this was the constraint's local IPO, we still need to provide con 
1336                                  * so that drivers can be added properly...
1337                                  */
1338                                 ipo_to_animdata(id, con->ipo, NULL, con->name);
1339                                 con->ipo->id.us--;
1340                                 con->ipo= NULL;
1341                         }
1342                          
1343                         /* check for Action Constraint */
1344                         // XXX do we really want to do this here?
1345                 }
1346                 
1347                 /* check constraint channels - we need to remove them anyway... */
1348                 for (conchan= ob->constraintChannels.first; conchan; conchan= conchann) {
1349                         /* get pointer to next Constraint Channel */
1350                         conchann= conchan->next;
1351                         
1352                         /* convert Constraint Channel's IPO data */
1353                         if (conchan->ipo) {
1354                                 ipo_to_animdata(id, conchan->ipo, NULL, conchan->name);
1355                                 conchan->ipo->id.us--;
1356                                 conchan->ipo= NULL;
1357                         }
1358                         
1359                         /* free Constraint Channel */
1360                         BLI_freelinkN(&ob->constraintChannels, conchan);
1361                 }
1362         }
1363         
1364         /* shapekeys */
1365         for (id= main->key.first; id; id= id->next) {
1366                 Key *key= (Key *)id;
1367                 
1368                 if (G.f & G_DEBUG) printf("\tconverting key %s \n", id->name+2);
1369                 
1370                 /* we're only interested in the IPO 
1371                  * NOTE: for later, it might be good to port these over to Object instead, as many of these
1372                  * are likely to be drivers, but it's hard to trace that from here, so move this to Ob loop?
1373                  */
1374                 if (key->ipo) {
1375                         /* Add AnimData block */
1376                         adt= BKE_id_add_animdata(id);
1377                         
1378                         /* Convert Shapekey data... */
1379                         ipo_to_animdata(id, key->ipo, NULL, NULL);
1380                         key->ipo->id.us--;
1381                         key->ipo= NULL;
1382                 }
1383         }
1384         
1385         /* materials */
1386         for (id= main->mat.first; id; id= id->next) {
1387                 Material *ma= (Material *)id;
1388                 
1389                 if (G.f & G_DEBUG) printf("\tconverting material %s \n", id->name+2);
1390                 
1391                 /* we're only interested in the IPO */
1392                 if (ma->ipo) {
1393                         /* Add AnimData block */
1394                         adt= BKE_id_add_animdata(id);
1395                         
1396                         /* Convert Material data... */
1397                         ipo_to_animdata(id, ma->ipo, NULL, NULL);
1398                         ma->ipo->id.us--;
1399                         ma->ipo= NULL;
1400                 }
1401         }
1402         
1403         /* textures */
1404         for (id= main->tex.first; id; id= id->next) {
1405                 Tex *te= (Tex *)id;
1406                 
1407                 if (G.f & G_DEBUG) printf("\tconverting texture %s \n", id->name+2);
1408                 
1409                 /* we're only interested in the IPO */
1410                 if (te->ipo) {
1411                         /* Add AnimData block */
1412                         adt= BKE_id_add_animdata(id);
1413                         
1414                         /* Convert Texture data... */
1415                         ipo_to_animdata(id, te->ipo, NULL, NULL);
1416                         te->ipo->id.us--;
1417                         te->ipo= NULL;
1418                 }
1419         }
1420         
1421         /* cameras */
1422         for (id= main->camera.first; id; id= id->next) {
1423                 Camera *ca= (Camera *)id;
1424                 
1425                 if (G.f & G_DEBUG) printf("\tconverting camera %s \n", id->name+2);
1426                 
1427                 /* we're only interested in the IPO */
1428                 if (ca->ipo) {
1429                         /* Add AnimData block */
1430                         adt= BKE_id_add_animdata(id);
1431                         
1432                         /* Convert Camera data... */
1433                         ipo_to_animdata(id, ca->ipo, NULL, NULL);
1434                         ca->ipo->id.us--;
1435                         ca->ipo= NULL;
1436                 }
1437         }
1438         
1439         /* lamps */
1440         for (id= main->lamp.first; id; id= id->next) {
1441                 Lamp *la= (Lamp *)id;
1442                 
1443                 if (G.f & G_DEBUG) printf("\tconverting lamp %s \n", id->name+2);
1444                 
1445                 /* we're only interested in the IPO */
1446                 if (la->ipo) {
1447                         /* Add AnimData block */
1448                         adt= BKE_id_add_animdata(id);
1449                         
1450                         /* Convert Lamp data... */
1451                         ipo_to_animdata(id, la->ipo, NULL, NULL);
1452                         la->ipo->id.us--;
1453                         la->ipo= NULL;
1454                 }
1455         }
1456         
1457         /* --------- Unconverted Animation Data ------------------ */
1458         /* For Animation data which may not be directly connected (i.e. not linked) to any other 
1459          * data, we need to perform a separate pass to make sure that they are converted to standalone
1460          * Actions which may then be able to be reused. This does mean that we will be going over data that's
1461          * already been converted, but there are no problems with that.
1462          *
1463          * The most common case for this will be Action Constraints, or IPO's with Fake-Users. 
1464          * We collect all drivers that were found into a temporary collection, and free them in one go, as they're 
1465          * impossible to resolve.
1466          */
1467         
1468         /* actions */
1469         for (id= main->action.first; id; id= id->next) {
1470                 bAction *act= (bAction *)id;
1471                 
1472                 if (G.f & G_DEBUG) printf("\tconverting action %s \n", id->name+2);
1473                 
1474                 /* be careful! some of the actions we encounter will be converted ones... */
1475                 action_to_animato(act, &act->curves, &drivers);
1476         }
1477         
1478         /* ipo's */
1479         for (id= main->ipo.first; id; id= id->next) {
1480                 Ipo *ipo= (Ipo *)id;
1481                 
1482                 if (G.f & G_DEBUG) printf("\tconverting ipo %s \n", id->name+2);
1483                 
1484                 /* most likely this IPO has already been processed, so check if any curves left to convert */
1485                 if (ipo->curve.first) {
1486                         bAction *new_act;
1487                         
1488                         /* add a new action for this, and convert all data into that action */
1489                         new_act= add_empty_action("ConvIPO_Action"); // XXX need a better name...
1490                         ipo_to_animato(ipo, NULL, NULL, &new_act->curves, &drivers);
1491                 }
1492                 
1493                 /* clear fake-users, and set user-count to zero to make sure it is cleared on file-save */
1494                 ipo->id.us= 0;
1495                 ipo->id.flag &= ~LIB_FAKEUSER;
1496         }
1497         
1498         /* free unused drivers from actions + ipos */
1499         free_fcurves(&drivers);
1500         
1501         printf("INFO: animato convert done \n"); // xxx debug
1502 }
1503
1504
1505
1506 #if 0 // XXX old animation system
1507
1508 /* ***************************** IPO - DataAPI ********************************* */
1509
1510 // !!!!!!!!!!!!!!!!!!!!!!!!!!!! FIXME - BAD CRUFT WARNING !!!!!!!!!!!!!!!!!!!!!!!
1511
1512 /* These functions here should be replaced eventually by the Data API, as this is 
1513  * inflexible duplication...
1514  */
1515
1516 /* --------------------- Get Pointer API ----------------------------- */ 
1517
1518
1519 /* GS reads the memory pointed at in a specific ordering. There are,
1520  * however two definitions for it. I have jotted them down here, both,
1521  * but I think the first one is actually used. The thing is that
1522  * big-endian systems might read this the wrong way round. OTOH, we
1523  * constructed the IDs that are read out with this macro explicitly as
1524  * well. I expect we'll sort it out soon... */
1525
1526 /* from blendef: */
1527 #define GS(a)   (*((short *)(a)))
1528
1529 /* from misc_util: flip the bytes from x  */
1530 /*  #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
1531
1532
1533 /* general function to get pointer to source/destination data  */
1534 void *get_ipo_poin (ID *id, IpoCurve *icu, int *type)
1535 {
1536         void *poin= NULL;
1537         MTex *mtex= NULL;
1538
1539         /* most channels will have float data, but those with other types will override this */
1540         *type= IPO_FLOAT;
1541
1542         /* data is divided into 'blocktypes' based on ID-codes */
1543         switch (GS(id->name)) {
1544                 case ID_WO: /* world channels -----------------------------  */
1545                 {
1546                         World *wo= (World *)id;
1547                         
1548                         switch (icu->adrcode) {
1549                         case WO_HOR_R:
1550                                 poin= &(wo->horr); break;
1551                         case WO_HOR_G:
1552                                 poin= &(wo->horg); break;
1553                         case WO_HOR_B:
1554                                 poin= &(wo->horb); break;
1555                         case WO_ZEN_R:
1556                                 poin= &(wo->zenr); break;
1557                         case WO_ZEN_G:
1558                                 poin= &(wo->zeng); break;
1559                         case WO_ZEN_B:
1560                                 poin= &(wo->zenb); break;
1561                         
1562                         case WO_EXPOS:
1563                                 poin= &(wo->exposure); break;
1564                         
1565                         case WO_MISI:
1566                                 poin= &(wo->misi); break;
1567                         case WO_MISTDI:
1568                                 poin= &(wo->mistdist); break;
1569                         case WO_MISTSTA:
1570                                 poin= &(wo->miststa); break;
1571                         case WO_MISTHI:
1572                                 poin= &(wo->misthi); break;
1573                         
1574                         case WO_STAR_R:
1575                                 poin= &(wo->starr); break;
1576                         case WO_STAR_G:
1577                                 poin= &(wo->starg); break;
1578                         case WO_STAR_B:
1579                                 poin= &(wo->starb); break;
1580                         
1581                         case WO_STARDIST:
1582                                 poin= &(wo->stardist); break;
1583                         case WO_STARSIZE:
1584                                 poin= &(wo->starsize); break;
1585                         }
1586                         
1587                         if (poin == NULL) {
1588                                 if (icu->adrcode & MA_MAP1) mtex= wo->mtex[0];
1589                                 else if (icu->adrcode & MA_MAP2) mtex= wo->mtex[1];
1590                                 else if (icu->adrcode & MA_MAP3) mtex= wo->mtex[2];
1591                                 else if (icu->adrcode & MA_MAP4) mtex= wo->mtex[3];
1592                                 else if (icu->adrcode & MA_MAP5) mtex= wo->mtex[4];
1593                                 else if (icu->adrcode & MA_MAP6) mtex= wo->mtex[5];
1594                                 else if (icu->adrcode & MA_MAP7) mtex= wo->mtex[6];
1595                                 else if (icu->adrcode & MA_MAP8) mtex= wo->mtex[7];
1596                                 else if (icu->adrcode & MA_MAP9) mtex= wo->mtex[8];
1597                                 else if (icu->adrcode & MA_MAP10) mtex= wo->mtex[9];
1598                                 else if (icu->adrcode & MA_MAP11) mtex= wo->mtex[10];
1599                                 else if (icu->adrcode & MA_MAP12) mtex= wo->mtex[11];
1600                                 else if (icu->adrcode & MA_MAP13) mtex= wo->mtex[12];
1601                                 else if (icu->adrcode & MA_MAP14) mtex= wo->mtex[13];
1602                                 else if (icu->adrcode & MA_MAP15) mtex= wo->mtex[14];
1603                                 else if (icu->adrcode & MA_MAP16) mtex= wo->mtex[15];
1604                                 else if (icu->adrcode & MA_MAP17) mtex= wo->mtex[16];
1605                                 else if (icu->adrcode & MA_MAP18) mtex= wo->mtex[17];
1606                                 
1607                                 if (mtex)
1608                                         poin= give_mtex_poin(mtex, (icu->adrcode & (MA_MAP1-1)));
1609                         }
1610                 }
1611                         break;
1612                 case ID_SO: /* sound channels -----------------------------  */
1613                 {
1614                         bSound *snd= (bSound *)id;
1615                         
1616                         switch (icu->adrcode) {
1617                         case SND_VOLUME:
1618                                 poin= &(snd->volume); break;
1619                         case SND_PITCH:
1620                                 poin= &(snd->pitch); break;
1621                         case SND_PANNING:
1622                                 poin= &(snd->panning); break;
1623                         case SND_ATTEN:
1624                                 poin= &(snd->attenuation); break;
1625                         }
1626                 }
1627                         break;
1628                 case ID_PA: /* particle channels -----------------------------  */
1629                 {
1630                         ParticleSettings *part= (ParticleSettings *)id;
1631                         
1632                         switch (icu->adrcode) {
1633                         case PART_EMIT_FREQ:
1634                         case PART_EMIT_LIFE:
1635                         case PART_EMIT_VEL:
1636                         case PART_EMIT_AVE:
1637                         case PART_EMIT_SIZE:
1638                                 poin= NULL; 
1639                                 break;
1640                         
1641                         case PART_CLUMP:
1642                                 poin= &(part->clumpfac); break;
1643                         case PART_AVE:
1644                                 poin= &(part->avefac); break;
1645                         case PART_SIZE:
1646                                 poin= &(part->size); break;
1647                         case PART_DRAG:
1648                                 poin= &(part->dragfac); break;
1649                         case PART_BROWN:
1650                                 poin= &(part->brownfac); break;
1651                         case PART_DAMP:
1652                                 poin= &(part->dampfac); break;
1653                         case PART_LENGTH:
1654                                 poin= &(part->length); break;
1655                         case PART_GRAV_X:
1656                                 poin= &(part->acc[0]); break;
1657                         case PART_GRAV_Y:
1658                                 poin= &(part->acc[1]); break;
1659                         case PART_GRAV_Z:
1660                                 poin= &(part->acc[2]); break;
1661                         case PART_KINK_AMP:
1662                                 poin= &(part->kink_amp); break;
1663                         case PART_KINK_FREQ:
1664                                 poin= &(part->kink_freq); break;
1665                         case PART_KINK_SHAPE:
1666                                 poin= &(part->kink_shape); break;
1667                         case PART_BB_TILT:
1668                                 poin= &(part->bb_tilt); break;
1669                                 
1670                         case PART_PD_FSTR:
1671                                 if (part->pd) poin= &(part->pd->f_strength);
1672                                 break;
1673                         case PART_PD_FFALL:
1674                                 if (part->pd) poin= &(part->pd->f_power);
1675                                 break;
1676                         case PART_PD_FMAXD:
1677                                 if (part->pd) poin= &(part->pd->maxdist);
1678                                 break;
1679                         case PART_PD2_FSTR:
1680                                 if (part->pd2) poin= &(part->pd2->f_strength);
1681                                 break;
1682                         case PART_PD2_FFALL:
1683                                 if (part->pd2) poin= &(part->pd2->f_power);
1684                                 break;
1685                         case PART_PD2_FMAXD:
1686                                 if (part->pd2) poin= &(part->pd2->maxdist);
1687                                 break;
1688                         }
1689                 }
1690                         break;
1691         }
1692
1693         /* return pointer */
1694         return poin;
1695 }
1696
1697
1698 #endif // XXX old animation system