Bugfix [#22069] Speed-Ipos are not imported correctly from 2.49 to 2.5
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 #include <stddef.h>
43
44 #include "MEM_guardedalloc.h"
45
46 #include "DNA_anim_types.h"
47 #include "DNA_constraint_types.h"
48 #include "DNA_camera_types.h"
49 #include "DNA_lamp_types.h"
50 #include "DNA_ipo_types.h"
51 #include "DNA_key_types.h"
52 #include "DNA_material_types.h"
53 #include "DNA_nla_types.h"
54 #include "DNA_sequence_types.h"
55 #include "DNA_scene_types.h"
56 #include "DNA_world_types.h"
57
58 #include "BLI_math.h" /* windows needs for M_PI */
59 #include "BLI_blenlib.h"
60 #include "BLI_dynstr.h"
61
62 #include "BKE_utildefines.h"
63
64 #include "BKE_animsys.h"
65 #include "BKE_action.h"
66 #include "BKE_fcurve.h"
67 #include "BKE_global.h"
68 #include "BKE_main.h"
69 #include "BKE_nla.h"
70
71
72
73 /* *************************************************** */
74 /* Old-Data Freeing Tools */
75
76 /* Free data from old IPO-Blocks (those which haven't been converted), but not IPO block itself */
77 // XXX this shouldn't be necessary anymore, but may occur while not all data is converted yet
78 void free_ipo (Ipo *ipo)
79 {
80         IpoCurve *icu, *icn;
81         int n= 0;
82         
83         for (icu= ipo->curve.first; icu; icu= icn) {
84                 icn= icu->next;
85                 n++;
86                 
87                 if (icu->bezt) MEM_freeN(icu->bezt);
88                 if (icu->bp) MEM_freeN(icu->bp);
89                 if (icu->driver) MEM_freeN(icu->driver);
90                 
91                 BLI_freelinkN(&ipo->curve, icu);
92         }
93         
94         if (G.f & G_DEBUG)
95                 printf("Freed %d (Unconverted) Ipo-Curves from IPO '%s' \n", n, ipo->id.name+2);
96 }
97
98 /* *************************************************** */
99 /* ADRCODE to RNA-Path Conversion Code  - Special (Bitflags) */
100
101 /* Mapping Table for bitflag <-> RNA path */
102 typedef struct AdrBit2Path {
103         int bit;
104         char *path;
105         int array_index;
106 } AdrBit2Path;
107
108 /* ----------------- */
109 /* Mapping Tables to use bits <-> RNA paths */
110
111 /* Object layers */
112 static AdrBit2Path ob_layer_bits[]= {
113         {(1<<0), "layer", 0},
114         {(1<<1), "layer", 1},
115         {(1<<2), "layer", 2},
116         {(1<<3), "layer", 3},
117         {(1<<4), "layer", 4},
118         {(1<<5), "layer", 5},
119         {(1<<6), "layer", 6},
120         {(1<<7), "layer", 7},
121         {(1<<8), "layer", 8},
122         {(1<<9), "layer", 9},
123         {(1<<10), "layer", 10},
124         {(1<<11), "layer", 11},
125         {(1<<12), "layer", 12},
126         {(1<<13), "layer", 13},
127         {(1<<14), "layer", 14},
128         {(1<<15), "layer", 15},
129         {(1<<16), "layer", 16},
130         {(1<<17), "layer", 17},
131         {(1<<18), "layer", 18},
132         {(1<<19), "layer", 19},
133         {(1<<20), "layer", 20}
134 };
135
136 /* Material mode */
137 static AdrBit2Path ma_mode_bits[]= {
138 //      {MA_TRACEBLE, "traceable", 0},
139 //      {MA_SHADOW, "shadow", 0},
140 //      {MA_SHLESS, "shadeless", 0},
141 //      ...
142         {MA_RAYTRANSP, "transparency", 0},
143         {MA_RAYMIRROR, "raytrace_mirror.enabled", 0},
144 //      {MA_HALO, "type", MA_TYPE_HALO}
145 };
146
147 /* ----------------- */
148
149 /* quick macro for returning the appropriate array for adrcode_bitmaps_to_paths() */
150 #define RET_ABP(items) \
151         { \
152                 *tot= sizeof(items)/sizeof(AdrBit2Path); \
153                 return items; \
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                 RET_ABP(ob_layer_bits)
162         else if ((blocktype == ID_MA) && (adrcode == MA_MODE))
163                 RET_ABP(ma_mode_bits)
164         // XXX TODO: add other types...
165         
166         /* Normal curve */
167         return NULL;
168 }
169
170 /* *************************************************** */
171 /* ADRCODE to RNA-Path Conversion Code  - Standard */
172
173 /* Object types */
174 static char *ob_adrcodes_to_paths (int adrcode, int *array_index)
175 {
176         /* set array index like this in-case nothing sets it correctly  */
177         *array_index= 0;
178         
179         /* result depends on adrcode */
180         switch (adrcode) {
181                 case OB_LOC_X:
182                         *array_index= 0; return "location";
183                 case OB_LOC_Y:
184                         *array_index= 1; return "location";
185                 case OB_LOC_Z:
186                         *array_index= 2; return "location";
187                 case OB_DLOC_X:
188                         *array_index= 0; return "delta_location";
189                 case OB_DLOC_Y:
190                         *array_index= 1; return "delta_location";
191                 case OB_DLOC_Z:
192                         *array_index= 2; return "delta_location";
193                 
194                 case OB_ROT_X:
195                         *array_index= 0; return "rotation_euler";
196                 case OB_ROT_Y:
197                         *array_index= 1; return "rotation_euler";
198                 case OB_ROT_Z:
199                         *array_index= 2; return "rotation_euler";
200                 case OB_DROT_X:
201                         *array_index= 0; return "delta_rotation_euler";
202                 case OB_DROT_Y:
203                         *array_index= 1; return "delta_rotation_euler";
204                 case OB_DROT_Z:
205                         *array_index= 2; return "delta_rotation_euler";
206                         
207                 case OB_SIZE_X:
208                         *array_index= 0; return "scale";
209                 case OB_SIZE_Y:
210                         *array_index= 1; return "scale";
211                 case OB_SIZE_Z:
212                         *array_index= 2; return "scale";
213                 case OB_DSIZE_X:
214                         *array_index= 0; return "delta_scale";
215                 case OB_DSIZE_Y:
216                         *array_index= 1; return "delta_scale";
217                 case OB_DSIZE_Z:
218                         *array_index= 2; return "delta_scale";
219                 case OB_COL_R:
220                         *array_index= 0; return "color";
221                 case OB_COL_G:
222                         *array_index= 1; return "color";
223                 case OB_COL_B:
224                         *array_index= 2; return "color";
225                 case OB_COL_A:
226                         *array_index= 3; return "color";
227 #if 0
228                 case OB_PD_FSTR:
229                         if (ob->pd) poin= &(ob->pd->f_strength);
230                         break;
231                 case OB_PD_FFALL:
232                         if (ob->pd) poin= &(ob->pd->f_power);
233                         break;
234                 case OB_PD_SDAMP:
235                         if (ob->pd) poin= &(ob->pd->pdef_damp);
236                         break;
237                 case OB_PD_RDAMP:
238                         if (ob->pd) poin= &(ob->pd->pdef_rdamp);
239                         break;
240                 case OB_PD_PERM:
241                         if (ob->pd) poin= &(ob->pd->pdef_perm);
242                         break;
243                 case OB_PD_FMAXD:
244                         if (ob->pd) poin= &(ob->pd->maxdist);
245                         break;
246 #endif
247         }
248         
249         return NULL;
250 }
251
252 /* PoseChannel types 
253  * NOTE: pchan name comes from 'actname' added earlier... 
254  */
255 static char *pchan_adrcodes_to_paths (int adrcode, int *array_index)
256 {
257         /* set array index like this in-case nothing sets it correctly  */
258         *array_index= 0;
259         
260         /* result depends on adrcode */
261         switch (adrcode) {
262                 case AC_QUAT_W:
263                         *array_index= 0; return "rotation_quaternion";
264                 case AC_QUAT_X:
265                         *array_index= 1; return "rotation_quaternion";
266                 case AC_QUAT_Y:
267                         *array_index= 2; return "rotation_quaternion";
268                 case AC_QUAT_Z:
269                         *array_index= 3; return "rotation_quaternion";
270                         
271                 case AC_EUL_X:
272                         *array_index= 0; return "rotation_euler";
273                 case AC_EUL_Y:
274                         *array_index= 1; return "rotation_euler";
275                 case AC_EUL_Z:
276                         *array_index= 2; return "rotation_euler";
277                 
278                 case AC_LOC_X:
279                         *array_index= 0; return "location";
280                 case AC_LOC_Y:
281                         *array_index= 1; return "location";
282                 case AC_LOC_Z:
283                         *array_index= 2; return "location";
284                 
285                 case AC_SIZE_X:
286                         *array_index= 0; return "scale";
287                 case AC_SIZE_Y:
288                         *array_index= 1; return "scale";
289                 case AC_SIZE_Z:
290                         *array_index= 2; return "scale";
291         }
292         
293         /* for debugging only */
294         printf("ERROR: unmatched PoseChannel setting (code %d) \n", adrcode);
295         return NULL;
296 }
297
298 /* Constraint types */
299 static char *constraint_adrcodes_to_paths (int adrcode, int *array_index)
300 {
301         /* set array index like this in-case nothing sets it correctly  */
302         *array_index= 0;
303         
304         /* result depends on adrcode */
305         switch (adrcode) {
306                 case CO_ENFORCE:
307                         return "influence";
308                 case CO_HEADTAIL:       // XXX this needs to be wrapped in RNA.. probably then this path will be invalid
309                         return "data.head_tail";
310         }
311         
312         return NULL;
313 }
314
315 /* ShapeKey types 
316  * NOTE: as we don't have access to the keyblock where the data comes from (for now), 
317  *              we'll just use numerical indicies for now... 
318  */
319 static char *shapekey_adrcodes_to_paths (int adrcode, int *array_index)
320 {
321         static char buf[128];
322         
323         /* block will be attached to ID_KE block, and setting that we alter is the 'value' (which sets keyblock.curval) */
324         // XXX adrcode 0 was dummy 'speed' curve 
325         if (adrcode == 0) 
326                 sprintf(buf, "speed");
327         else
328                 sprintf(buf, "keys[%d].value", adrcode);
329         return buf;
330 }
331
332 /* MTex (Texture Slot) types */
333 static char *mtex_adrcodes_to_paths (int adrcode, int *array_index)
334 {
335         char *base=NULL, *prop=NULL;
336         static char buf[128];
337         
338         /* base part of path */
339         if (adrcode & MA_MAP1) base= "textures[0]";
340         else if (adrcode & MA_MAP2) base= "textures[1]";
341         else if (adrcode & MA_MAP3) base= "textures[2]";
342         else if (adrcode & MA_MAP4) base= "textures[3]";
343         else if (adrcode & MA_MAP5) base= "textures[4]";
344         else if (adrcode & MA_MAP6) base= "textures[5]";
345         else if (adrcode & MA_MAP7) base= "textures[6]";
346         else if (adrcode & MA_MAP8) base= "textures[7]";
347         else if (adrcode & MA_MAP9) base= "textures[8]";
348         else if (adrcode & MA_MAP10) base= "textures[9]";
349         else if (adrcode & MA_MAP11) base= "textures[10]";
350         else if (adrcode & MA_MAP12) base= "textures[11]";
351         else if (adrcode & MA_MAP13) base= "textures[12]";
352         else if (adrcode & MA_MAP14) base= "textures[13]";
353         else if (adrcode & MA_MAP15) base= "textures[14]";
354         else if (adrcode & MA_MAP16) base= "textures[15]";
355         else if (adrcode & MA_MAP17) base= "textures[16]";
356         else if (adrcode & MA_MAP18) base= "textures[17]";
357                 
358         /* property identifier for path */
359         adrcode= (adrcode & (MA_MAP1-1));
360         switch (adrcode) {
361 #if 0 // XXX these are not wrapped in RNA yet!
362                 case MAP_OFS_X:
363                         poin= &(mtex->ofs[0]); break;
364                 case MAP_OFS_Y:
365                         poin= &(mtex->ofs[1]); break;
366                 case MAP_OFS_Z:
367                         poin= &(mtex->ofs[2]); break;
368                 case MAP_SIZE_X:
369                         poin= &(mtex->size[0]); break;
370                 case MAP_SIZE_Y:
371                         poin= &(mtex->size[1]); break;
372                 case MAP_SIZE_Z:
373                         poin= &(mtex->size[2]); break;
374                 case MAP_R:
375                         poin= &(mtex->r); break;
376                 case MAP_G:
377                         poin= &(mtex->g); break;
378                 case MAP_B:
379                         poin= &(mtex->b); break;
380                 case MAP_DVAR:
381                         poin= &(mtex->def_var); break;
382                 case MAP_COLF:
383                         poin= &(mtex->colfac); break;
384                 case MAP_NORF:
385                         poin= &(mtex->norfac); break;
386                 case MAP_VARF:
387                         poin= &(mtex->varfac); break;
388 #endif
389                 case MAP_DISP:
390                         prop= "warp_factor"; break;
391         }
392         
393         /* only build and return path if there's a property */
394         if (prop) {
395                 BLI_snprintf(buf, 128, "%s.%s", base, prop);
396                 return buf;
397         }
398         else
399                 return NULL;
400 }
401
402 /* Texture types */
403 static char *texture_adrcodes_to_paths (int adrcode, int *array_index)
404 {
405         /* set array index like this in-case nothing sets it correctly  */
406         *array_index= 0;
407         
408         /* result depends on adrcode */
409         switch (adrcode) {
410                 case TE_NSIZE:
411                         return "noise_size";
412                 case TE_TURB:
413                         return "turbulence";
414                         
415                 case TE_NDEPTH: // XXX texture RNA undefined
416                         //poin= &(tex->noisedepth); *type= IPO_SHORT; break;
417                         break;
418                 case TE_NTYPE: // XXX texture RNA undefined
419                         //poin= &(tex->noisetype); *type= IPO_SHORT; break;
420                         break;
421                         
422                 case TE_N_BAS1:
423                         return "noise_basis";
424                 case TE_N_BAS2:
425                         return "noise_basis"; // XXX this is not yet defined in RNA...
426                 
427                         /* voronoi */
428                 case TE_VNW1:
429                         *array_index= 0; return "feature_weights";
430                 case TE_VNW2:
431                         *array_index= 1; return "feature_weights";
432                 case TE_VNW3:
433                         *array_index= 2; return "feature_weights";
434                 case TE_VNW4:
435                         *array_index= 3; return "feature_weights";
436                 case TE_VNMEXP:
437                         return "minkovsky_exponent";
438                 case TE_VN_DISTM:
439                         return "distance_metric";
440                 case TE_VN_COLT:
441                         return "color_type";
442                 
443                         /* distorted noise / voronoi */
444                 case TE_ISCA:
445                         return "noise_intensity";
446                         
447                         /* distorted noise */
448                 case TE_DISTA:
449                         return "distortion_amount";
450                 
451                         /* musgrave */
452                 case TE_MG_TYP: // XXX texture RNA undefined
453                 //      poin= &(tex->stype); *type= IPO_SHORT; break;
454                         break;
455                 case TE_MGH:
456                         return "highest_dimension";
457                 case TE_MG_LAC:
458                         return "lacunarity";
459                 case TE_MG_OCT:
460                         return "octaves";
461                 case TE_MG_OFF:
462                         return "offset";
463                 case TE_MG_GAIN:
464                         return "gain";
465                         
466                 case TE_COL_R:
467                         *array_index= 0; return "rgb_factor";
468                 case TE_COL_G:
469                         *array_index= 1; return "rgb_factor";
470                 case TE_COL_B:
471                         *array_index= 2; return "rgb_factor";
472                         
473                 case TE_BRIGHT:
474                         return "brightness";
475                 case TE_CONTRA:
476                         return "constrast";
477         }
478         
479         return NULL;
480 }
481
482 /* Material Types */
483 static char *material_adrcodes_to_paths (int adrcode, int *array_index)
484 {
485         /* set array index like this in-case nothing sets it correctly  */
486         *array_index= 0;
487         
488         /* result depends on adrcode */
489         switch (adrcode) {
490                 case MA_COL_R:
491                         *array_index= 0; return "diffuse_color";
492                 case MA_COL_G:
493                         *array_index= 1; return "diffuse_color";
494                 case MA_COL_B:
495                         *array_index= 2; return "diffuse_color";
496                         
497                 case MA_SPEC_R:
498                         *array_index= 0; return "specular_color";
499                 case MA_SPEC_G:
500                         *array_index= 1; return "specular_color";
501                 case MA_SPEC_B:
502                         *array_index= 2; return "specular_color";
503                         
504                 case MA_MIR_R:
505                         *array_index= 0; return "mirror_color";
506                 case MA_MIR_G:
507                         *array_index= 1; return "mirror_color";
508                 case MA_MIR_B:
509                         *array_index= 2; return "mirror_color";
510                         
511                 case MA_ALPHA:
512                         return "alpha";
513                         
514                 case MA_REF:
515                         return "diffuse_reflection";
516                 
517                 case MA_EMIT:
518                         return "emit";
519                 
520                 case MA_AMB:
521                         return "ambient";
522                 
523                 case MA_SPEC:
524                         return "specular_reflection";
525                 
526                 case MA_HARD:
527                         return "specular_hardness";
528                         
529                 case MA_SPTR:
530                         return "specular_opacity";
531                         
532                 case MA_IOR:
533                         return "ior";
534                         
535                 case MA_HASIZE:
536                         return "halo.size";
537                         
538                 case MA_TRANSLU:
539                         return "translucency";
540                         
541                 case MA_RAYM:
542                         return "raytrace_mirror.reflect";
543                         
544                 case MA_FRESMIR:
545                         return "raytrace_mirror.fresnel";
546                         
547                 case MA_FRESMIRI:
548                         return "raytrace_mirror.fresnel_fac";
549                         
550                 case MA_FRESTRA:
551                         return "raytrace_transparency.fresnel";
552                         
553                 case MA_FRESTRAI:
554                         return "raytrace_transparency.fresnel_fac";
555                         
556                 case MA_ADD:
557                         return "halo.add";
558                 
559                 default: /* for now, we assume that the others were MTex channels */
560                         return mtex_adrcodes_to_paths(adrcode, array_index);
561         }
562         
563         return NULL;    
564 }
565
566 /* Camera Types */
567 static char *camera_adrcodes_to_paths (int adrcode, int *array_index)
568 {
569         /* set array index like this in-case nothing sets it correctly  */
570         *array_index= 0;
571         
572         /* result depends on adrcode */
573         switch (adrcode) {
574                 case CAM_LENS:
575 #if 0 // XXX this cannot be resolved easily... perhaps we assume camera is perspective (works for most cases...
576                         if (ca->type == CAM_ORTHO)
577                                 return "ortho_scale";
578                         else
579                                 return "lens"; 
580 #else // XXX lazy hack for now...
581                         return "lens";
582 #endif // XXX this cannot be resolved easily
583                         
584                 case CAM_STA:
585                         return "clip_start";
586                 case CAM_END:
587                         return "clip_end";
588                         
589 #if 0 // XXX these are not defined in RNA
590                 case CAM_YF_APERT:
591                         poin= &(ca->YF_aperture); break;
592                 case CAM_YF_FDIST:
593                         poin= &(ca->YF_dofdist); break;
594 #endif // XXX these are not defined in RNA
595                         
596                 case CAM_SHIFT_X:
597                         return "shift_x";
598                 case CAM_SHIFT_Y:
599                         return "shift_y";
600         }
601         
602         /* unrecognised adrcode, or not-yet-handled ones! */
603         return NULL;
604 }
605
606 /* Lamp Types */
607 static char *lamp_adrcodes_to_paths (int adrcode, int *array_index)
608 {
609         /* set array index like this in-case nothing sets it correctly  */
610         *array_index= 0;
611         
612         /* result depends on adrcode */
613         switch (adrcode) {
614                 case LA_ENERGY:
615                         return "energy";
616                         
617                 case LA_COL_R:
618                         *array_index= 0;  return "color";
619                 case LA_COL_G:
620                         *array_index= 1;  return "color";
621                 case LA_COL_B:
622                         *array_index= 2;  return "color";
623                         
624                 case LA_DIST:
625                         return "distance";
626                 
627                 case LA_SPOTSI:
628                         return "spot_size";
629                 case LA_SPOTBL:
630                         return "spot_blend";
631                         
632                 case LA_QUAD1:
633                         return "linear_attenuation";
634                 case LA_QUAD2:
635                         return "quadratic_attenuation";
636                         
637                 case LA_HALOINT:
638                         return "halo_intensity";
639                         
640                 default: /* for now, we assume that the others were MTex channels */
641                         return mtex_adrcodes_to_paths(adrcode, array_index);
642         }
643         
644         /* unrecognised adrcode, or not-yet-handled ones! */
645         return NULL;
646 }
647
648 /* Sound Types */
649 static char *sound_adrcodes_to_paths (int adrcode, int *array_index)
650 {
651         /* set array index like this in-case nothing sets it correctly  */
652         *array_index= 0;
653         
654         /* result depends on adrcode */
655         switch (adrcode) {
656                 case SND_VOLUME:
657                         return "volume";
658                 case SND_PITCH:
659                         return "pitch";
660         /* XXX Joshua -- I had wrapped panning in rna, but someone commented out, calling it "unused" */
661         /*      case SND_PANNING:
662                         return "panning"; */
663                 case SND_ATTEN:
664                         return "attenuation";
665         }
666         
667         /* unrecognised adrcode, or not-yet-handled ones! */
668         return NULL;
669 }
670
671 /* World Types */
672 static char *world_adrcodes_to_paths (int adrcode, int *array_index)
673 {
674         /* set array index like this in-case nothing sets it correctly  */
675         *array_index= 0;
676         
677         /* result depends on adrcode */
678         switch (adrcode) {
679                 case WO_HOR_R:
680                         *array_index= 0; return "horizon_color";
681                 case WO_HOR_G:
682                         *array_index= 1; return "horizon_color";
683                 case WO_HOR_B:
684                         *array_index= 2; return "horizon_color";
685                 case WO_ZEN_R:
686                         *array_index= 0; return "zenith_color";
687                 case WO_ZEN_G:
688                         *array_index= 1; return "zenith_color";
689                 case WO_ZEN_B:
690                         *array_index= 2; return "zenith_color";
691                 
692                 case WO_EXPOS:
693                         return "exposure";
694                 
695                 case WO_MISI:
696                         return "mist.intensity";
697                 case WO_MISTDI:
698                         return "mist.depth";
699                 case WO_MISTSTA:
700                         return "mist.start";
701                 case WO_MISTHI:
702                         return "mist.height";
703                 
704         /*      Star Color is unused -- recommend removal */
705         /*      case WO_STAR_R:
706                         *array_index= 0; return "stars.color";
707                 case WO_STAR_G:
708                         *array_index= 1; return "stars.color";
709                 case WO_STAR_B:
710                         *array_index= 2; return "stars.color"; */
711                 
712                 case WO_STARDIST:
713                         return "stars.min_distance";
714                 case WO_STARSIZE:
715                         return "stars.size";
716                 
717                 default: /* for now, we assume that the others were MTex channels */
718                         return mtex_adrcodes_to_paths(adrcode, array_index);
719                 }
720                 
721         return NULL;    
722 }
723
724 /* Particle Types */
725 static char *particle_adrcodes_to_paths (int adrcode, int *array_index)
726 {
727         /* set array index like this in-case nothing sets it correctly  */
728         *array_index= 0;
729         
730         /* result depends on adrcode */
731         switch (adrcode) {
732                 case PART_CLUMP:
733                         return "settings.clump_factor";
734                 case PART_AVE:
735                         return "settings.angular_velocity_factor";
736                 case PART_SIZE:
737                         return "settings.particle_size";
738                 case PART_DRAG:
739                         return "settings.drag_factor";
740                 case PART_BROWN:
741                         return "settings.brownian_factor";
742                 case PART_DAMP:
743                         return "settings.damp_factor";
744                 case PART_LENGTH:
745                         return "settings.length";
746                 case PART_GRAV_X:
747                         *array_index= 0; return "settings.acceleration";
748                 case PART_GRAV_Y:
749                         *array_index= 1; return "settings.acceleration";
750                 case PART_GRAV_Z:
751                         *array_index= 2; return "settings.acceleration";
752                 case PART_KINK_AMP:
753                         return "settings.kink_amplitude";
754                 case PART_KINK_FREQ:
755                         return "settings.kink_frequency";
756                 case PART_KINK_SHAPE:
757                         return "settings.kink_shape";
758                 case PART_BB_TILT:
759                         return "settings.billboard_tilt";
760                 
761                 /* PartDeflect needs to be sorted out properly in rna_object_force;
762                    If anyone else works on this, but is unfamiliar, these particular
763                         settings reference the particles of the system themselves
764                         being used as forces -- it will use the same rna structure
765                         as the similar object forces                            */
766                 /*case PART_PD_FSTR:
767                         if (part->pd) poin= &(part->pd->f_strength);
768                         break;
769                 case PART_PD_FFALL:
770                         if (part->pd) poin= &(part->pd->f_power);
771                         break;
772                 case PART_PD_FMAXD:
773                         if (part->pd) poin= &(part->pd->maxdist);
774                         break;
775                 case PART_PD2_FSTR:
776                         if (part->pd2) poin= &(part->pd2->f_strength);
777                         break;
778                 case PART_PD2_FFALL:
779                         if (part->pd2) poin= &(part->pd2->f_power);
780                         break;
781                 case PART_PD2_FMAXD:
782                         if (part->pd2) poin= &(part->pd2->maxdist);
783                         break;*/
784
785                 }
786                 
787         return NULL;    
788 }
789
790 /* ------- */
791
792 /* Allocate memory for RNA-path for some property given a blocktype, adrcode, and 'root' parts of path
793  *      Input:
794  *              - blocktype, adrcode    - determines setting to get
795  *              - actname, constname    - used to build path
796  *      Output:
797  *              - array_index                   - index in property's array (if applicable) to use
798  *              - return                                - the allocated path...
799  */
800 static char *get_rna_access (int blocktype, int adrcode, char actname[], char constname[], int *array_index)
801 {
802         DynStr *path= BLI_dynstr_new();
803         char *propname=NULL, *rpath=NULL;
804         char buf[512];
805         int dummy_index= 0;
806         
807         /* hack: if constname is set, we can only be dealing with an Constraint curve */
808         if (constname)
809                 blocktype= ID_CO;
810         
811         /* get property name based on blocktype */
812         switch (blocktype) {
813                 case ID_OB: /* object */
814                         propname= ob_adrcodes_to_paths(adrcode, &dummy_index);
815                         break;
816                 
817                 case ID_PO: /* pose channel */
818                         propname= pchan_adrcodes_to_paths(adrcode, &dummy_index);
819                         break;
820                         
821                 case ID_KE: /* shapekeys */
822                         propname= shapekey_adrcodes_to_paths(adrcode, &dummy_index);
823                         break;
824                         
825                 case ID_CO: /* constraint */
826                         propname= constraint_adrcodes_to_paths(adrcode, &dummy_index);  
827                         break;
828                         
829                 case ID_TE: /* texture */
830                         propname= texture_adrcodes_to_paths(adrcode, &dummy_index);
831                         break;
832                         
833                 case ID_MA: /* material */
834                         propname= material_adrcodes_to_paths(adrcode, &dummy_index);
835                         break;
836                         
837                 case ID_CA: /* camera */
838                         propname= camera_adrcodes_to_paths(adrcode, &dummy_index);
839                         break;
840                         
841                 case ID_LA: /* lamp */
842                         propname= lamp_adrcodes_to_paths(adrcode, &dummy_index);
843                         break;
844                 
845                 case ID_SO: /* sound */
846                         propname= sound_adrcodes_to_paths(adrcode, &dummy_index);
847                         break;
848                 
849                 case ID_WO: /* world */
850                         propname= world_adrcodes_to_paths(adrcode, &dummy_index);
851                         break;
852                 
853                 case ID_PA: /* particle */
854                         propname= particle_adrcodes_to_paths(adrcode, &dummy_index);
855                         break;
856                         
857                 case ID_CU: /* curve */
858                         /* this used to be a 'dummy' curve which got evaluated on the fly... 
859                          * now we've got real var for this!
860                          */
861                         propname= "eval_time";
862                         break;
863                 
864                 /* XXX problematic blocktypes */                
865                 case ID_SEQ: /* sequencer strip */
866                         //SEQ_FAC1:
867                         switch (adrcode) {
868                         case SEQ_FAC1:
869                                 propname= "effect_fader";
870                                 break;
871                         case SEQ_FAC_SPEED:
872                                 propname= "speed_fader";
873                                 break;
874                         case SEQ_FAC_OPACITY:
875                                 propname= "blend_opacity";
876                                 break;
877                         }
878                         //      poin= &(seq->facf0); // XXX this doesn't seem to be included anywhere in sequencer RNA...
879                         break;
880                         
881                 /* special hacks */
882                 case -1:
883                         /* special case for rotdiff drivers... we don't need a property for this... */
884                         break;
885                         
886                 // TODO... add other blocktypes...
887                 default:
888                         printf("IPO2ANIMATO WARNING: No path for blocktype %d, adrcode %d yet \n", blocktype, adrcode);
889                         break;
890         }
891         
892         /* check if any property found 
893          *      - blocktype < 0 is special case for a specific type of driver, where we don't need a property name...
894          */
895         if ((propname == NULL) && (blocktype > 0)) {
896                 /* nothing was found, so exit */
897                 if (array_index) 
898                         *array_index= 0;
899                         
900                 BLI_dynstr_free(path);
901                 
902                 return NULL;
903         }
904         else {
905                 if (array_index)
906                         *array_index= dummy_index;
907         }
908         
909         /* append preceeding bits to path */
910         if ((actname && actname[0]) && (constname && constname[0])) {
911                 /* Constraint in Pose-Channel */
912                 sprintf(buf, "pose.bones[\"%s\"].constraints[\"%s\"]", actname, constname);
913         }
914         else if (actname && actname[0]) {
915                 /* Pose-Channel */
916                 sprintf(buf, "pose.bones[\"%s\"]", actname);
917         }
918         else if (constname && constname[0]) {
919                 /* Constraint in Object */
920                 sprintf(buf, "constraints[\"%s\"]", constname);
921         }
922         else
923                 strcpy(buf, ""); /* empty string */
924         BLI_dynstr_append(path, buf);
925         
926         /* need to add dot before property if there was anything precceding this */
927         if (buf[0])
928                 BLI_dynstr_append(path, ".");
929         
930         /* now write name of property */
931         BLI_dynstr_append(path, propname);
932         
933         /* if there was no array index pointer provided, add it to the path */
934         if (array_index == NULL) {
935                 sprintf(buf, "[\"%d\"]", dummy_index);
936                 BLI_dynstr_append(path, buf);
937         }
938         
939         /* convert to normal MEM_malloc'd string */
940         rpath= BLI_dynstr_get_cstring(path);
941         BLI_dynstr_free(path);
942         
943         /* return path... */
944         return rpath;
945 }
946
947 /* *************************************************** */
948 /* Conversion Utilities */
949
950 /* Convert adrcodes to driver target transform channel types */
951 static short adrcode_to_dtar_transchan (short adrcode)
952 {
953         switch (adrcode) {
954                 case OB_LOC_X:  
955                         return DTAR_TRANSCHAN_LOCX;
956                 case OB_LOC_Y:
957                         return DTAR_TRANSCHAN_LOCY;
958                 case OB_LOC_Z:
959                         return DTAR_TRANSCHAN_LOCZ;
960                 
961                 case OB_ROT_X:  
962                         return DTAR_TRANSCHAN_ROTX;
963                 case OB_ROT_Y:
964                         return DTAR_TRANSCHAN_ROTY;
965                 case OB_ROT_Z:
966                         return DTAR_TRANSCHAN_ROTZ;
967                 
968                 case OB_SIZE_X: 
969                         return DTAR_TRANSCHAN_SCALEX;
970                 case OB_SIZE_Y:
971                         return DTAR_TRANSCHAN_SCALEX;
972                 case OB_SIZE_Z:
973                         return DTAR_TRANSCHAN_SCALEX;
974                         
975                 default:
976                         return 0;
977         }
978 }
979
980 /* Convert IpoDriver to ChannelDriver - will free the old data (i.e. the old driver) */
981 static ChannelDriver *idriver_to_cdriver (IpoDriver *idriver)
982 {
983         ChannelDriver *cdriver;
984         
985         /* allocate memory for new driver */
986         cdriver= MEM_callocN(sizeof(ChannelDriver), "ChannelDriver");
987         
988         /* if 'pydriver', just copy data across */
989         if (idriver->type == IPO_DRIVER_TYPE_PYTHON) {
990                 /* PyDriver only requires the expression to be copied */
991                 // FIXME: expression will be useless due to API changes, but at least not totally lost
992                 cdriver->type = DRIVER_TYPE_PYTHON;
993                 if (idriver->name[0])
994                         BLI_strncpy(cdriver->expression, idriver->name, sizeof(cdriver->expression));
995         }
996         else {
997                 DriverVar *dvar = NULL;
998                 DriverTarget *dtar = NULL;
999                 
1000                 /* this should be ok for all types here... */
1001                 cdriver->type= DRIVER_TYPE_AVERAGE;
1002                 
1003                 /* what to store depends on the 'blocktype' - object or posechannel */
1004                 if (idriver->blocktype == ID_AR) { /* PoseChannel */
1005                         if (idriver->adrcode == OB_ROT_DIFF) {
1006                                 /* Rotational Difference requires a special type of variable */
1007                                 dvar= driver_add_new_variable(cdriver);
1008                                 driver_change_variable_type(dvar, DVAR_TYPE_ROT_DIFF);
1009                                 
1010                                         /* first bone target */
1011                                 dtar= &dvar->targets[0];
1012                                 dtar->id= (ID *)idriver->ob;
1013                                 if (idriver->name[0])
1014                                         BLI_strncpy(dtar->pchan_name, idriver->name, 32);
1015                                 
1016                                         /* second bone target (name was stored in same var as the first one) */
1017                                 dtar= &dvar->targets[1];
1018                                 dtar->id= (ID *)idriver->ob;
1019                                 if (idriver->name[0]) // xxx... for safety
1020                                         BLI_strncpy(dtar->pchan_name, idriver->name+DRIVER_NAME_OFFS, 32);
1021                         }
1022                         else {
1023                                 /* only a single variable, of type 'transform channel' */
1024                                 dvar= driver_add_new_variable(cdriver);
1025                                 driver_change_variable_type(dvar, DVAR_TYPE_TRANSFORM_CHAN);
1026                                 
1027                                 /* only requires a single target */
1028                                 dtar= &dvar->targets[0];
1029                                 dtar->id= (ID *)idriver->ob;
1030                                 if (idriver->name[0])
1031                                         BLI_strncpy(dtar->pchan_name, idriver->name, 32);
1032                                 dtar->transChan= adrcode_to_dtar_transchan(idriver->adrcode);
1033                                 dtar->flag |= DTAR_FLAG_LOCALSPACE; /* old drivers took local space */
1034                         }
1035                 }
1036                 else { /* Object */
1037                         /* only a single variable, of type 'transform channel' */
1038                         dvar= driver_add_new_variable(cdriver);
1039                         driver_change_variable_type(dvar, DVAR_TYPE_TRANSFORM_CHAN);
1040                         
1041                                 /* only requires single target */
1042                         dtar= &dvar->targets[0];
1043                         dtar->id= (ID *)idriver->ob;
1044                         dtar->transChan= adrcode_to_dtar_transchan(idriver->adrcode);
1045                 }
1046         }
1047         
1048         /* return the new one */
1049         return cdriver;
1050 }
1051
1052 /* Add F-Curve to the correct list 
1053  *      - grpname is needed to be used as group name where relevant, and is usually derived from actname
1054  */
1055 static void fcurve_add_to_list (ListBase *groups, ListBase *list, FCurve *fcu, char *grpname, int muteipo)
1056 {
1057         /* If we're adding to an action, we will have groups to write to... */
1058         if (groups && grpname) {
1059                 /* wrap the pointers given into a dummy action that we pass to the API func
1060                  * and extract the resultant lists...
1061                  */
1062                 bAction tmp_act;
1063                 bActionGroup *agrp= NULL;
1064                 
1065                 /* init the temp action */
1066                 //memset(&tmp_act, 0, sizeof(bAction)); // XXX only enable this line if we get errors
1067                 tmp_act.groups.first= groups->first;
1068                 tmp_act.groups.last= groups->last;
1069                 tmp_act.curves.first= list->first;
1070                 tmp_act.curves.last= list->last;
1071                 /* ... xxx, the other vars don't need to be filled in */
1072                 
1073                 /* get the group to use */
1074                 agrp= action_groups_find_named(&tmp_act, grpname);
1075                 if (agrp == NULL) {
1076                         /* no matching group, so add one */
1077                         if (agrp == NULL) {
1078                                 /* Add a new group, and make it active */
1079                                 agrp= MEM_callocN(sizeof(bActionGroup), "bActionGroup");
1080                                 
1081                                 agrp->flag = AGRP_SELECTED;
1082                                 if(muteipo) agrp->flag |= AGRP_MUTED;
1083
1084                                 strncpy(agrp->name, grpname, sizeof(agrp->name));
1085                                 
1086                                 BLI_addtail(&tmp_act.groups, agrp);
1087                                 BLI_uniquename(&tmp_act.groups, agrp, "Group", '.', offsetof(bActionGroup, name), sizeof(agrp->name));
1088                         }
1089                 }
1090                 
1091                 /* add F-Curve to group */
1092                 /* WARNING: this func should only need to look at the stuff we initialised, if not, things may crash */
1093                 action_groups_add_channel(&tmp_act, agrp, fcu);
1094                 
1095                 if(agrp->flag & AGRP_MUTED) /* flush down */
1096                         fcu->flag |= FCURVE_MUTED;
1097
1098                 /* set the output lists based on the ones in the temp action */
1099                 groups->first= tmp_act.groups.first;
1100                 groups->last= tmp_act.groups.last;
1101                 list->first= tmp_act.curves.first;
1102                 list->last= tmp_act.curves.last;
1103         }
1104         else {
1105                 /* simply add the F-Curve to the end of the given list */
1106                 BLI_addtail(list, fcu);
1107         }
1108 }
1109
1110 /* Convert IPO-Curve to F-Curve (including Driver data), and free any of the old data that 
1111  * is not relevant, BUT do not free the IPO-Curve itself...
1112  *      actname: name of Action-Channel (if applicable) that IPO-Curve's IPO-block belonged to
1113  *      constname: name of Constraint-Channel (if applicable) that IPO-Curve's IPO-block belonged to
1114  */
1115 static void icu_to_fcurves (ID *id, ListBase *groups, ListBase *list, IpoCurve *icu, char *actname, char *constname, int muteipo)
1116 {
1117         AdrBit2Path *abp;
1118         FCurve *fcu;
1119         int i=0, totbits;
1120         
1121         /* allocate memory for a new F-Curve */
1122         fcu= MEM_callocN(sizeof(FCurve), "FCurve");
1123         
1124         /* convert driver */
1125         if (icu->driver)
1126                 fcu->driver= idriver_to_cdriver(icu->driver);
1127         
1128         /* copy flags */
1129         if (icu->flag & IPO_VISIBLE) fcu->flag |= FCURVE_VISIBLE;
1130         if (icu->flag & IPO_SELECT) fcu->flag |= FCURVE_SELECTED;
1131         if (icu->flag & IPO_ACTIVE) fcu->flag |= FCURVE_ACTIVE;
1132         if (icu->flag & IPO_MUTE) fcu->flag |= FCURVE_MUTED;
1133         if (icu->flag & IPO_PROTECT) fcu->flag |= FCURVE_PROTECTED;
1134         if (icu->flag & IPO_AUTO_HORIZ) fcu->flag |= FCURVE_AUTO_HANDLES;
1135         
1136         /* set extrapolation */
1137         switch (icu->extrap) {
1138                 case IPO_HORIZ: /* constant extrapolation */
1139                 case IPO_DIR: /* linear extrapolation */
1140                 {
1141                         /* just copy, as the new defines match the old ones... */
1142                         fcu->extend= icu->extrap;
1143                 }
1144                         break;
1145                         
1146                 case IPO_CYCL: /* cyclic extrapolation */
1147                 case IPO_CYCLX: /* cyclic extrapolation + offset */
1148                 {
1149                         /* Add a new FModifier (Cyclic) instead of setting extend value 
1150                          * as that's the new equivilant of that option. 
1151                          */
1152                         FModifier *fcm= add_fmodifier(&fcu->modifiers, FMODIFIER_TYPE_CYCLES);
1153                         FMod_Cycles *data= (FMod_Cycles *)fcm->data;
1154                         
1155                         /* if 'offset' one is in use, set appropriate settings */
1156                         if (icu->extrap == IPO_CYCLX)
1157                                 data->before_mode= data->after_mode= FCM_EXTRAPOLATE_CYCLIC_OFFSET;
1158                         else
1159                                 data->before_mode= data->after_mode= FCM_EXTRAPOLATE_CYCLIC;
1160                 }
1161                         break;
1162         }
1163         
1164         /* -------- */
1165         
1166         /* get adrcode <-> bitflags mapping to handle nasty bitflag curves? */
1167         abp= adrcode_bitmaps_to_paths(icu->blocktype, icu->adrcode, &totbits);
1168         if (abp && totbits) {
1169                 FCurve *fcurve;
1170                 int b;
1171                 
1172                 if (G.f & G_DEBUG) printf("\tconvert bitflag ipocurve, totbits = %d \n", totbits);
1173                 
1174                 /* add the 'only int values' flag */
1175                 fcu->flag |= (FCURVE_INT_VALUES|FCURVE_DISCRETE_VALUES);                
1176                 
1177                 /* for each bit we have to remap + check for:
1178                  * 1) we need to make copy the existing F-Curve data (fcu -> fcurve),
1179                  *        except for the last one which will use the original 
1180                  * 2) copy the relevant path info across
1181                  * 3) filter the keyframes for the flag of interest
1182                  */
1183                 for (b=0; b < totbits; b++, abp++) {
1184                         /* make a copy of existing base-data if not the last curve */
1185                         if (b < (totbits-1))
1186                                 fcurve= copy_fcurve(fcu);
1187                         else
1188                                 fcurve= fcu;
1189                                 
1190                         /* set path */
1191                         fcurve->rna_path= BLI_strdupn(abp->path, strlen(abp->path));
1192                         fcurve->array_index= abp->array_index;
1193                         
1194                         /* convert keyframes 
1195                          *      - beztriples and bpoints are mutually exclusive, so we won't have both at the same time
1196                          *      - beztriples are more likely to be encountered as they are keyframes (the other type wasn't used yet)
1197                          */
1198                         fcurve->totvert= icu->totvert;
1199                         
1200                         if (icu->bezt) {
1201                                 BezTriple *dst, *src;
1202                                 
1203                                 /* allocate new array for keyframes/beztriples */
1204                                 fcurve->bezt= MEM_callocN(sizeof(BezTriple)*fcurve->totvert, "BezTriples");
1205                                 
1206                                 /* loop through copying all BezTriples individually, as we need to modify a few things */
1207                                 for (dst=fcurve->bezt, src=icu->bezt; i < fcurve->totvert; i++, dst++, src++) {
1208                                         /* firstly, copy BezTriple data */
1209                                         *dst= *src;
1210                                         
1211                                         /* interpolation can only be constant... */
1212                                         dst->ipo= BEZT_IPO_CONST;
1213                                         
1214                                         /* 'hide' flag is now used for keytype - only 'keyframes' existed before */
1215                                         dst->hide= BEZT_KEYTYPE_KEYFRAME;
1216                                         
1217                                         /* correct values, by checking if the flag of interest is set */
1218                                         if ( ((int)(dst->vec[1][1])) & (abp->bit) )
1219                                                 dst->vec[0][1]= dst->vec[1][1]= dst->vec[2][1] = 1.0f;
1220                                         else
1221                                                 dst->vec[0][1]= dst->vec[1][1]= dst->vec[2][1] = 0.0f;
1222                                 }
1223                         }
1224                         else if (icu->bp) {
1225                                 /* TODO: need to convert from BPoint type to the more compact FPoint type... but not priority, since no data used this */
1226                                 //BPoint *bp;
1227                                 //FPoint *fpt;
1228                         }
1229                         
1230                         /* add new F-Curve to list */
1231                         fcurve_add_to_list(groups, list, fcurve, actname, muteipo);
1232                 }
1233         }
1234         else {
1235                 /* get rna-path
1236                  *      - we will need to set the 'disabled' flag if no path is able to be made (for now)
1237                  */
1238                 fcu->rna_path= get_rna_access(icu->blocktype, icu->adrcode, actname, constname, &fcu->array_index);
1239                 if (fcu->rna_path == NULL)
1240                         fcu->flag |= FCURVE_DISABLED;
1241                 
1242                 /* convert keyframes 
1243                  *      - beztriples and bpoints are mutually exclusive, so we won't have both at the same time
1244                  *      - beztriples are more likely to be encountered as they are keyframes (the other type wasn't used yet)
1245                  */
1246                 fcu->totvert= icu->totvert;
1247                 
1248                 if (icu->bezt) {
1249                         BezTriple *dst, *src;
1250                         
1251                         /* allocate new array for keyframes/beztriples */
1252                         fcu->bezt= MEM_callocN(sizeof(BezTriple)*fcu->totvert, "BezTriples");
1253                         
1254                         /* loop through copying all BezTriples individually, as we need to modify a few things */
1255                         for (dst=fcu->bezt, src=icu->bezt; i < fcu->totvert; i++, dst++, src++) {
1256                                 /* firstly, copy BezTriple data */
1257                                 *dst= *src;
1258                                 
1259                                 /* now copy interpolation from curve (if not already set) */
1260                                 if (icu->ipo != IPO_MIXED)
1261                                         dst->ipo= icu->ipo;
1262                                         
1263                                 /* 'hide' flag is now used for keytype - only 'keyframes' existed before */
1264                                 dst->hide= BEZT_KEYTYPE_KEYFRAME;
1265                                         
1266                                 /* correct values for euler rotation curves 
1267                                  *      - they were degrees/10 
1268                                  *      - we need radians for RNA to do the right thing
1269                                  */
1270                                 if ( ((icu->blocktype == ID_OB) && ELEM3(icu->adrcode, OB_ROT_X, OB_ROT_Y, OB_ROT_Z)) ||
1271                                          ((icu->blocktype == ID_PO) && ELEM3(icu->adrcode, AC_EUL_X, AC_EUL_Y, AC_EUL_Z)) )
1272                                 {
1273                                         const float fac= (float)M_PI / 18.0f; //10.0f * M_PI/180.0f;
1274                                         
1275                                         dst->vec[0][1] *= fac;
1276                                         dst->vec[1][1] *= fac;
1277                                         dst->vec[2][1] *= fac;
1278                                 }
1279                                 
1280                                 /* correct values for path speed curves 
1281                                  *      - their values were 0-1
1282                                  *      - we now need as 'frames'
1283                                  */
1284                                 if ( (id) && (icu->blocktype == GS(id->name)) && 
1285                                          (fcu->rna_path && strcmp(fcu->rna_path, "eval_time")==0) )
1286                                 {
1287                                         Curve *cu = (Curve *)id;
1288                                         
1289                                         dst->vec[0][1] *= cu->pathlen;
1290                                         dst->vec[1][1] *= cu->pathlen;
1291                                         dst->vec[2][1] *= cu->pathlen;
1292                                 }
1293                                 
1294                                 /* correct times for rotation drivers 
1295                                  *      - need to go from degrees to radians...
1296                                  *      - there's only really 1 target to worry about 
1297                                  */
1298                                 if (fcu->driver && fcu->driver->variables.first) {
1299                                         DriverVar *dvar= fcu->driver->variables.first;
1300                                         DriverTarget *dtar= &dvar->targets[0];
1301                                         
1302                                         if (ELEM3(dtar->transChan, DTAR_TRANSCHAN_ROTX, DTAR_TRANSCHAN_ROTY, DTAR_TRANSCHAN_ROTZ)) {
1303                                                 const float fac= (float)M_PI / 180.0f;
1304                                                 
1305                                                 dst->vec[0][0] *= fac;
1306                                                 dst->vec[1][0] *= fac;
1307                                                 dst->vec[2][0] *= fac;
1308                                         }
1309                                 }
1310                         }
1311                 }
1312                 else if (icu->bp) {
1313                         /* TODO: need to convert from BPoint type to the more compact FPoint type... but not priority, since no data used this */
1314                         //BPoint *bp;
1315                         //FPoint *fpt;
1316                 }
1317                 
1318                 /* add new F-Curve to list */
1319                 fcurve_add_to_list(groups, list, fcu, actname, muteipo);
1320         }
1321 }
1322
1323 /* ------------------------- */
1324
1325 /* Convert IPO-block (i.e. all its IpoCurves) to the new system.
1326  * This does not assume that any ID or AnimData uses it, but does assume that
1327  * it is given two lists, which it will perform driver/animation-data separation.
1328  */
1329 static void ipo_to_animato (ID *id, Ipo *ipo, char actname[], char constname[], ListBase *animgroups, ListBase *anim, ListBase *drivers)
1330 {
1331         IpoCurve *icu;
1332         
1333         /* sanity check */
1334         if (ELEM3(NULL, ipo, anim, drivers))
1335                 return;
1336                 
1337         if (G.f & G_DEBUG) printf("ipo_to_animato \n");
1338                 
1339         /* validate actname and constname 
1340          *      - clear actname if it was one of the generic <builtin> ones (i.e. 'Object', or 'Shapes')
1341          *      - actname can then be used to assign F-Curves in Action to Action Groups 
1342          *        (i.e. thus keeping the benefits that used to be provided by Action Channels for grouping
1343          *              F-Curves for bones). This may be added later... for now let's just dump without them...
1344          */
1345         if (actname) {
1346                 if ((ipo->blocktype == ID_OB) && (strcmp(actname, "Object") == 0))
1347                         actname= NULL;
1348                 else if ((ipo->blocktype == ID_OB) && (strcmp(actname, "Shape") == 0))
1349                         actname= NULL;
1350         }
1351         
1352         /* loop over IPO-Curves, freeing as we progress */
1353         for (icu= ipo->curve.first; icu; icu= icu->next) {
1354                 /* Since an IPO-Curve may end up being made into many F-Curves (i.e. bitflag curves), 
1355                  * we figure out the best place to put the channel, then tell the curve-converter to just dump there
1356                  */
1357                 if (icu->driver) {
1358                         /* Blender 2.4x allowed empty drivers, but we don't now, since they cause more trouble than they're worth */
1359                         if ((icu->driver->ob) || (icu->driver->type == IPO_DRIVER_TYPE_PYTHON)) {
1360                                 icu_to_fcurves(id, NULL, drivers, icu, actname, constname, ipo->muteipo);
1361                         }
1362                         else {
1363                                 MEM_freeN(icu->driver);
1364                                 icu->driver= NULL;
1365                         }
1366                 }
1367                 else
1368                         icu_to_fcurves(id, animgroups, anim, icu, actname, constname, ipo->muteipo);
1369         }
1370         
1371         /* if this IPO block doesn't have any users after this one, free... */
1372         ipo->id.us--;
1373         if ( (ipo->id.us == 0) || ((ipo->id.us == 1) && (ipo->id.flag & LIB_FAKEUSER)) ) 
1374         {
1375                 IpoCurve *icn;
1376                 
1377                 for (icu= ipo->curve.first; icu; icu= icn) {
1378                         icn= icu->next;
1379                         
1380                         /* free driver */
1381                         if (icu->driver)
1382                                 MEM_freeN(icu->driver);
1383                                 
1384                         /* free old data of curve now that it's no longer needed for converting any more curves */
1385                         if (icu->bezt) MEM_freeN(icu->bezt);
1386                         if (icu->bp) MEM_freeN(icu->bezt);
1387                         
1388                         /* free this IPO-Curve */
1389                         BLI_freelinkN(&ipo->curve, icu);
1390                 }
1391         }
1392 }
1393
1394 /* Convert Action-block to new system, separating animation and drivers
1395  * New curves may not be converted directly into the given Action (i.e. for Actions linked
1396  * to Objects, where ob->ipo and ob->action need to be combined).
1397  * NOTE: we need to be careful here, as same data-structs are used for new system too!
1398  */
1399 static void action_to_animato (ID *id, bAction *act, ListBase *groups, ListBase *curves, ListBase *drivers)
1400 {
1401         bActionChannel *achan, *achann;
1402         bConstraintChannel *conchan, *conchann;
1403         
1404         /* only continue if there are Action Channels (indicating unconverted data) */
1405         if (act->chanbase.first == NULL)
1406                 return;
1407                 
1408         /* get rid of all Action Groups */
1409         // XXX this is risky if there's some old + some new data in the Action...
1410         if (act->groups.first) 
1411                 BLI_freelistN(&act->groups);
1412         
1413         /* loop through Action-Channels, converting data, freeing as we go */
1414         for (achan= act->chanbase.first; achan; achan= achann) {
1415                 /* get pointer to next Action Channel */
1416                 achann= achan->next;
1417                 
1418                 /* convert Action Channel's IPO data */
1419                 if (achan->ipo) {
1420                         ipo_to_animato(id, achan->ipo, achan->name, NULL, groups, curves, drivers);
1421                         achan->ipo->id.us--;
1422                         achan->ipo= NULL;
1423                 }
1424                 
1425                 /* convert constraint channel IPO-data */
1426                 for (conchan= achan->constraintChannels.first; conchan; conchan= conchann) {
1427                         /* get pointer to next Constraint Channel */
1428                         conchann= conchan->next;
1429                         
1430                         /* convert Constraint Channel's IPO data */
1431                         if (conchan->ipo) {
1432                                 ipo_to_animato(id, conchan->ipo, achan->name, conchan->name, groups, curves, drivers);
1433                                 conchan->ipo->id.us--;
1434                                 conchan->ipo= NULL;
1435                         }
1436                         
1437                         /* free Constraint Channel */
1438                         BLI_freelinkN(&achan->constraintChannels, conchan);
1439                 }
1440                 
1441                 /* free Action Channel */
1442                 BLI_freelinkN(&act->chanbase, achan);
1443         }
1444 }
1445
1446
1447 /* ------------------------- */
1448
1449 /* Convert IPO-block (i.e. all its IpoCurves) for some ID to the new system
1450  * This assumes that AnimData has been added already. Separation of drivers
1451  * from animation data is accomplished here too...
1452  */
1453 static void ipo_to_animdata (ID *id, Ipo *ipo, char actname[], char constname[])
1454 {
1455         AnimData *adt= BKE_animdata_from_id(id);
1456         ListBase anim = {NULL, NULL};
1457         ListBase drivers = {NULL, NULL};
1458         
1459         /* sanity check */
1460         if ELEM(NULL, id, ipo)
1461                 return;
1462         if (adt == NULL) {
1463                 printf("ERROR ipo_to_animdata(): adt invalid \n");
1464                 return;
1465         }
1466         
1467         if (G.f & G_DEBUG) {
1468                 printf("ipo to animdata - ID:%s, IPO:%s, actname:%s constname:%s  curves:%d \n", 
1469                         id->name+2, ipo->id.name+2, (actname)?actname:"<None>", (constname)?constname:"<None>", 
1470                         BLI_countlist(&ipo->curve));
1471         }
1472         
1473         /* Convert curves to animato system (separated into separate lists of F-Curves for animation and drivers),
1474          * and the try to put these lists in the right places, but do not free the lists here
1475          */
1476         // XXX there shouldn't be any need for the groups, so don't supply pointer for that now... 
1477         ipo_to_animato(id, ipo, actname, constname, NULL, &anim, &drivers);
1478         
1479         /* deal with animation first */
1480         if (anim.first) {
1481                 if (G.f & G_DEBUG) printf("\thas anim \n");
1482                 /* try to get action */
1483                 if (adt->action == NULL) {
1484                         adt->action= add_empty_action("ConvData_Action"); // XXX we need a better name for this
1485                         if (G.f & G_DEBUG) printf("\t\tadded new action \n");
1486                 }
1487                 
1488                 /* add F-Curves to action */
1489                 addlisttolist(&adt->action->curves, &anim);
1490         }
1491         
1492         /* deal with drivers */
1493         if (drivers.first) {
1494                 if (G.f & G_DEBUG) printf("\thas drivers \n");
1495                 /* add drivers to end of driver stack */
1496                 addlisttolist(&adt->drivers, &drivers);
1497         }
1498 }
1499
1500 /* Convert Action-block to new system
1501  * NOTE: we need to be careful here, as same data-structs are used for new system too!
1502  */
1503 static void action_to_animdata (ID *id, bAction *act)
1504 {
1505         AnimData *adt= BKE_animdata_from_id(id);
1506         
1507         /* only continue if there are Action Channels (indicating unconverted data) */
1508         if (ELEM(NULL, adt, act->chanbase.first))
1509                 return;
1510         
1511         /* check if we need to set this Action as the AnimData's action */
1512         if (adt->action == NULL) {
1513                 /* set this Action as AnimData's Action */
1514                 if (G.f & G_DEBUG) printf("act_to_adt - set adt action to act \n");
1515                 adt->action= act;
1516         }
1517         
1518         /* convert Action data */
1519         action_to_animato(id, act, &adt->action->groups, &adt->action->curves, &adt->drivers);
1520 }
1521
1522 /* ------------------------- */
1523
1524 // TODO:
1525 //      - NLA group duplicators info
1526 //      - NLA curve/stride modifiers...
1527
1528 /* Convert NLA-Strip to new system */
1529 static void nlastrips_to_animdata (ID *id, ListBase *strips)
1530 {
1531         AnimData *adt= BKE_animdata_from_id(id);
1532         NlaTrack *nlt = NULL;
1533         NlaStrip *strip;
1534         bActionStrip *as, *asn;
1535         
1536         /* for each one of the original strips, convert to a new strip and free the old... */
1537         for (as= strips->first; as; as= asn) {
1538                 asn= as->next;
1539                 
1540                 /* this old strip is only worth something if it had an action... */
1541                 if (as->act) {
1542                         /* convert Action data (if not yet converted), storing the results in the same Action */
1543                         action_to_animato(id, as->act, &as->act->groups, &as->act->curves, &adt->drivers);
1544                         
1545                         /* create a new-style NLA-strip which references this Action, then copy over relevant settings */
1546                         {
1547                                 /* init a new strip, and assign the action to it 
1548                                  *      - no need to muck around with the user-counts, since this is just 
1549                                  *        passing over the ref to the new owner, not creating an additional ref
1550                                  */
1551                                 strip= MEM_callocN(sizeof(NlaStrip), "NlaStrip");
1552                                 strip->act= as->act;
1553                                 
1554                                         /* endpoints */
1555                                 strip->start= as->start;
1556                                 strip->end= as->end;
1557                                 strip->actstart= as->actstart;
1558                                 strip->actend= as->actend;
1559                                 
1560                                         /* action reuse */
1561                                 strip->repeat= as->repeat;
1562                                 strip->scale= as->scale;
1563                                 if (as->flag & ACTSTRIP_LOCK_ACTION)    strip->flag |= NLASTRIP_FLAG_SYNC_LENGTH;
1564                                 
1565                                         /* blending */
1566                                 strip->blendin= as->blendin;
1567                                 strip->blendout= as->blendout;
1568                                 strip->blendmode= (as->mode==ACTSTRIPMODE_ADD) ? NLASTRIP_MODE_ADD : NLASTRIP_MODE_REPLACE;
1569                                 if (as->flag & ACTSTRIP_AUTO_BLENDS)    strip->flag |= NLASTRIP_FLAG_AUTO_BLENDS;
1570                                         
1571                                         /* assorted setting flags */
1572                                 if (as->flag & ACTSTRIP_SELECT)                 strip->flag |= NLASTRIP_FLAG_SELECT;
1573                                 if (as->flag & ACTSTRIP_ACTIVE)                 strip->flag |= NLASTRIP_FLAG_ACTIVE;
1574                                 
1575                                 if (as->flag & ACTSTRIP_MUTE)                   strip->flag |= NLASTRIP_FLAG_MUTED;
1576                                 if (as->flag & ACTSTRIP_REVERSE)                strip->flag |= NLASTRIP_FLAG_REVERSE;
1577                                 
1578                                         /* by default, we now always extrapolate, while in the past this was optional */
1579                                 if ((as->flag & ACTSTRIP_HOLDLASTFRAME)==0) 
1580                                         strip->extendmode= NLASTRIP_EXTEND_NOTHING;
1581                         }       
1582                         
1583                         /* try to add this strip to the current NLA-Track (i.e. the 'last' one on the stack atm) */
1584                         if (BKE_nlatrack_add_strip(nlt, strip) == 0) {
1585                                 /* trying to add to the current failed (no space), 
1586                                  * so add a new track to the stack, and add to that...
1587                                  */
1588                                 nlt= add_nlatrack(adt, NULL);
1589                                 BKE_nlatrack_add_strip(nlt, strip);
1590                         }
1591                 }
1592                 
1593                 /* modifiers */
1594                 // FIXME: for now, we just free them...
1595                 if (as->modifiers.first)
1596                         BLI_freelistN(&as->modifiers);
1597                 
1598                 /* free the old strip */
1599                 BLI_freelinkN(strips, as);
1600         }
1601 }
1602
1603 /* *************************************************** */
1604 /* External API - Only Called from do_versions() */
1605
1606 /* Called from do_versions() in readfile.c to convert the old 'IPO/adrcode' system
1607  * to the new 'Animato/RNA' system.
1608  *
1609  * The basic method used here, is to loop over datablocks which have IPO-data, and 
1610  * add those IPO's to new AnimData blocks as Actions. 
1611  * Action/NLA data only works well for Objects, so these only need to be checked for there.
1612  *  
1613  * Data that has been converted should be freed immediately, which means that it is immediately
1614  * clear which datablocks have yet to be converted, and also prevent freeing errors when we exit.
1615  */
1616 // XXX currently done after all file reading... 
1617 void do_versions_ipos_to_animato(Main *main)
1618 {
1619         ListBase drivers = {NULL, NULL};
1620         ID *id;
1621         AnimData *adt;
1622         
1623         if (main == NULL) {
1624                 printf("Argh! Main is NULL in do_versions_ipos_to_animato() \n");
1625                 return;
1626         }
1627                 
1628         /* only convert if version is right */
1629         if (main->versionfile >= 250) {
1630                 printf("WARNING: Animation data too new to convert (Version %d) \n", main->versionfile);
1631                 return;
1632         }
1633         else if (G.f & G_DEBUG)
1634                 printf("INFO: Converting to Animato... \n");
1635                 
1636         /* ----------- Animation Attached to Data -------------- */
1637         
1638         /* objects */
1639         for (id= main->object.first; id; id= id->next) {
1640                 Object *ob= (Object *)id;
1641                 bPoseChannel *pchan;
1642                 bConstraint *con;
1643                 bConstraintChannel *conchan, *conchann;
1644                 
1645                 if (G.f & G_DEBUG) printf("\tconverting ob %s \n", id->name+2);
1646                 
1647                 /* check if object has any animation data */
1648                 if (ob->nlastrips.first) {
1649                         /* Add AnimData block */
1650                         adt= BKE_id_add_animdata(id);
1651                         
1652                         /* IPO first to take into any non-NLA'd Object Animation */
1653                         if (ob->ipo) {
1654                                 ipo_to_animdata(id, ob->ipo, NULL, NULL);
1655                                 
1656                                 ob->ipo->id.us--;
1657                                 ob->ipo= NULL;
1658                         }
1659                         
1660                         /* Action is skipped since it'll be used by some strip in the NLA anyway, 
1661                          * causing errors with evaluation in the new evaluation pipeline
1662                          */
1663                         if (ob->action) {
1664                                 ob->action->id.us--;
1665                                 ob->action= NULL;
1666                         }
1667                         
1668                         /* finally NLA */
1669                         nlastrips_to_animdata(id, &ob->nlastrips);
1670                 }
1671                 else if ((ob->ipo) || (ob->action)) {
1672                         /* Add AnimData block */
1673                         adt= BKE_id_add_animdata(id);
1674                         
1675                         /* Action first - so that Action name get conserved */
1676                         if (ob->action) {
1677                                 action_to_animdata(id, ob->action);
1678                                 
1679                                 /* only decrease usercount if this Action isn't now being used by AnimData */
1680                                 if (ob->action != adt->action) {
1681                                         ob->action->id.us--;
1682                                         ob->action= NULL;
1683                                 }
1684                         }
1685                         
1686                         /* IPO second... */
1687                         if (ob->ipo) {
1688                                 ipo_to_animdata(id, ob->ipo, NULL, NULL);
1689                                 ob->ipo->id.us--;
1690                                 ob->ipo= NULL;
1691                         }
1692                 }
1693                 
1694                 /* check PoseChannels for constraints with local data */
1695                 if (ob->pose) {
1696                         /* Verify if there's AnimData block */
1697                         BKE_id_add_animdata(id);
1698                         
1699                         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1700                                 for (con= pchan->constraints.first; con; con= con->next) {
1701                                         /* if constraint has own IPO, convert add these to Object 
1702                                          * (NOTE: they're most likely to be drivers too) 
1703                                          */
1704                                         if (con->ipo) {
1705                                                 /* although this was the constraint's local IPO, we still need to provide pchan + con 
1706                                                  * so that drivers can be added properly...
1707                                                  */
1708                                                 ipo_to_animdata(id, con->ipo, pchan->name, con->name);
1709                                                 con->ipo->id.us--;
1710                                                 con->ipo= NULL;
1711                                         }
1712                                 }
1713                         }
1714                 }
1715                 
1716                 /* check constraints for local IPO's */
1717                 for (con= ob->constraints.first; con; con= con->next) {
1718                         /* if constraint has own IPO, convert add these to Object 
1719                          * (NOTE: they're most likely to be drivers too) 
1720                          */
1721                         if (con->ipo) {
1722                                 /* Verify if there's AnimData block, just in case */
1723                                 BKE_id_add_animdata(id);
1724                                 
1725                                 /* although this was the constraint's local IPO, we still need to provide con 
1726                                  * so that drivers can be added properly...
1727                                  */
1728                                 ipo_to_animdata(id, con->ipo, NULL, con->name);
1729                                 con->ipo->id.us--;
1730                                 con->ipo= NULL;
1731                         }
1732                          
1733                         /* check for Action Constraint */
1734                         // XXX do we really want to do this here?
1735                 }
1736                 
1737                 /* check constraint channels - we need to remove them anyway... */
1738                 if (ob->constraintChannels.first) {
1739                         /* Verify if there's AnimData block */
1740                         BKE_id_add_animdata(id);
1741                         
1742                         for (conchan= ob->constraintChannels.first; conchan; conchan= conchann) {
1743                                 /* get pointer to next Constraint Channel */
1744                                 conchann= conchan->next;
1745                                 
1746                                 /* convert Constraint Channel's IPO data */
1747                                 if (conchan->ipo) {
1748                                         ipo_to_animdata(id, conchan->ipo, NULL, conchan->name);
1749                                         conchan->ipo->id.us--;
1750                                         conchan->ipo= NULL;
1751                                 }
1752                                 
1753                                 /* free Constraint Channel */
1754                                 BLI_freelinkN(&ob->constraintChannels, conchan);
1755                         }
1756                 }
1757         }
1758         
1759         /* shapekeys */
1760         for (id= main->key.first; id; id= id->next) {
1761                 Key *key= (Key *)id;
1762                 
1763                 if (G.f & G_DEBUG) printf("\tconverting key %s \n", id->name+2);
1764                 
1765                 /* we're only interested in the IPO 
1766                  * NOTE: for later, it might be good to port these over to Object instead, as many of these
1767                  * are likely to be drivers, but it's hard to trace that from here, so move this to Ob loop?
1768                  */
1769                 if (key->ipo) {
1770                         /* Add AnimData block */
1771                         adt= BKE_id_add_animdata(id);
1772                         
1773                         /* Convert Shapekey data... */
1774                         ipo_to_animdata(id, key->ipo, NULL, NULL);
1775                         key->ipo->id.us--;
1776                         key->ipo= NULL;
1777                 }
1778         }
1779         
1780         /* materials */
1781         for (id= main->mat.first; id; id= id->next) {
1782                 Material *ma= (Material *)id;
1783                 
1784                 if (G.f & G_DEBUG) printf("\tconverting material %s \n", id->name+2);
1785                 
1786                 /* we're only interested in the IPO */
1787                 if (ma->ipo) {
1788                         /* Add AnimData block */
1789                         adt= BKE_id_add_animdata(id);
1790                         
1791                         /* Convert Material data... */
1792                         ipo_to_animdata(id, ma->ipo, NULL, NULL);
1793                         ma->ipo->id.us--;
1794                         ma->ipo= NULL;
1795                 }
1796         }
1797         
1798         /* worlds */
1799         for (id= main->world.first; id; id= id->next) {
1800                 World *wo= (World *)id;
1801                 
1802                 if (G.f & G_DEBUG) printf("\tconverting world %s \n", id->name+2);
1803                 
1804                 /* we're only interested in the IPO */
1805                 if (wo->ipo) {
1806                         /* Add AnimData block */
1807                         adt= BKE_id_add_animdata(id);
1808                         
1809                         /* Convert World data... */
1810                         ipo_to_animdata(id, wo->ipo, NULL, NULL);
1811                         wo->ipo->id.us--;
1812                         wo->ipo= NULL;
1813                 }
1814         }
1815         
1816         /* sequence strips */
1817         for (id= main->scene.first; id; id= id->next) {
1818                 Scene *scene = (Scene *)id;
1819                 if (scene->ed && scene->ed->seqbasep) {
1820                         Sequence * seq;
1821                         
1822                         for(seq = scene->ed->seqbasep->first; seq; seq = seq->next) {
1823                                 IpoCurve *icu = (seq->ipo) ? seq->ipo->curve.first : NULL;
1824                                 short adrcode = SEQ_FAC1;
1825                                 
1826                                 if (G.f & G_DEBUG) 
1827                                         printf("\tconverting sequence strip %s \n", seq->name+2);
1828                                 
1829                                 if (ELEM(NULL, seq->ipo, icu)) {
1830                                         seq->flag |= SEQ_USE_EFFECT_DEFAULT_FADE;
1831                                         continue;
1832                                 }
1833                                 
1834                                 /* patch adrcode, so that we can map
1835                                    to different DNA variables later 
1836                                    (semi-hack (tm) )
1837                                 */
1838                                 switch(seq->type) {
1839                                         case SEQ_IMAGE:
1840                                         case SEQ_META:
1841                                         case SEQ_SCENE:
1842                                         case SEQ_MOVIE:
1843                                         case SEQ_COLOR:
1844                                                 adrcode = SEQ_FAC_OPACITY;
1845                                                 break;
1846                                         case SEQ_SPEED:
1847                                                 adrcode = SEQ_FAC_SPEED;
1848                                                 break;
1849                                 }
1850                                 icu->adrcode = adrcode;
1851                                 
1852                                 /* convert IPO */
1853                                 ipo_to_animdata((ID *)seq, seq->ipo, NULL, NULL);
1854                                 seq->ipo->id.us--;
1855                                 seq->ipo = NULL;
1856                         }
1857                 }
1858         }
1859
1860
1861         /* textures */
1862         for (id= main->tex.first; id; id= id->next) {
1863                 Tex *te= (Tex *)id;
1864                 
1865                 if (G.f & G_DEBUG) printf("\tconverting texture %s \n", id->name+2);
1866                 
1867                 /* we're only interested in the IPO */
1868                 if (te->ipo) {
1869                         /* Add AnimData block */
1870                         adt= BKE_id_add_animdata(id);
1871                         
1872                         /* Convert Texture data... */
1873                         ipo_to_animdata(id, te->ipo, NULL, NULL);
1874                         te->ipo->id.us--;
1875                         te->ipo= NULL;
1876                 }
1877         }
1878         
1879         /* cameras */
1880         for (id= main->camera.first; id; id= id->next) {
1881                 Camera *ca= (Camera *)id;
1882                 
1883                 if (G.f & G_DEBUG) printf("\tconverting camera %s \n", id->name+2);
1884                 
1885                 /* we're only interested in the IPO */
1886                 if (ca->ipo) {
1887                         /* Add AnimData block */
1888                         adt= BKE_id_add_animdata(id);
1889                         
1890                         /* Convert Camera data... */
1891                         ipo_to_animdata(id, ca->ipo, NULL, NULL);
1892                         ca->ipo->id.us--;
1893                         ca->ipo= NULL;
1894                 }
1895         }
1896         
1897         /* lamps */
1898         for (id= main->lamp.first; id; id= id->next) {
1899                 Lamp *la= (Lamp *)id;
1900                 
1901                 if (G.f & G_DEBUG) printf("\tconverting lamp %s \n", id->name+2);
1902                 
1903                 /* we're only interested in the IPO */
1904                 if (la->ipo) {
1905                         /* Add AnimData block */
1906                         adt= BKE_id_add_animdata(id);
1907                         
1908                         /* Convert Lamp data... */
1909                         ipo_to_animdata(id, la->ipo, NULL, NULL);
1910                         la->ipo->id.us--;
1911                         la->ipo= NULL;
1912                 }
1913         }
1914         
1915         /* curves */
1916         for (id= main->curve.first; id; id= id->next) {
1917                 Curve *cu= (Curve *)id;
1918                 
1919                 if (G.f & G_DEBUG) printf("\tconverting curve %s \n", id->name+2);
1920                 
1921                 /* we're only interested in the IPO */
1922                 if (cu->ipo) {
1923                         /* Add AnimData block */
1924                         adt= BKE_id_add_animdata(id);
1925                         
1926                         /* Convert Curve data... */
1927                         ipo_to_animdata(id, cu->ipo, NULL, NULL);
1928                         cu->ipo->id.us--;
1929                         cu->ipo= NULL;
1930                 }
1931         }
1932         
1933         /* --------- Unconverted Animation Data ------------------ */
1934         /* For Animation data which may not be directly connected (i.e. not linked) to any other 
1935          * data, we need to perform a separate pass to make sure that they are converted to standalone
1936          * Actions which may then be able to be reused. This does mean that we will be going over data that's
1937          * already been converted, but there are no problems with that.
1938          *
1939          * The most common case for this will be Action Constraints, or IPO's with Fake-Users. 
1940          * We collect all drivers that were found into a temporary collection, and free them in one go, as they're 
1941          * impossible to resolve.
1942          */
1943         
1944         /* actions */
1945         for (id= main->action.first; id; id= id->next) {
1946                 bAction *act= (bAction *)id;
1947                 
1948                 if (G.f & G_DEBUG) printf("\tconverting action %s \n", id->name+2);
1949                 
1950                 /* be careful! some of the actions we encounter will be converted ones... */
1951                 action_to_animato(NULL, act, &act->groups, &act->curves, &drivers);
1952         }
1953         
1954         /* ipo's */
1955         for (id= main->ipo.first; id; id= id->next) {
1956                 Ipo *ipo= (Ipo *)id;
1957                 
1958                 if (G.f & G_DEBUG) printf("\tconverting ipo %s \n", id->name+2);
1959                 
1960                 /* most likely this IPO has already been processed, so check if any curves left to convert */
1961                 if (ipo->curve.first) {
1962                         bAction *new_act;
1963                         
1964                         /* add a new action for this, and convert all data into that action */
1965                         new_act= add_empty_action("ConvIPO_Action"); // XXX need a better name...
1966                         ipo_to_animato(NULL, ipo, NULL, NULL, NULL, &new_act->curves, &drivers);
1967                 }
1968                 
1969                 /* clear fake-users, and set user-count to zero to make sure it is cleared on file-save */
1970                 ipo->id.us= 0;
1971                 ipo->id.flag &= ~LIB_FAKEUSER;
1972         }
1973         
1974         /* free unused drivers from actions + ipos */
1975         free_fcurves(&drivers);
1976         
1977         if (G.f & G_DEBUG)
1978                 printf("INFO: Animato convert done \n");
1979 }
1980