Durian Request: Drivers Recode
[blender.git] / source / blender / blenkernel / intern / ipo.c
1 /* ipo.c
2  * 
3  * $Id$
4  *
5  * ***** BEGIN GPL LICENSE BLOCK *****
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  *
21  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
22  * All rights reserved.
23  *
24  * The Original Code is: all of this file.
25  *
26  * Contributor(s): 2008,2009 Joshua Leung (IPO System cleanup, Animation System Recode)
27  *
28  * ***** END GPL LICENSE BLOCK *****
29  */
30
31 /* NOTE:
32  *
33  * This file is no longer used to provide tools for the depreceated IPO system. Instead, it
34  * is only used to house the conversion code to the new system.
35  *
36  * -- Joshua Leung, Jan 2009
37  */
38  
39 #include <math.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <stddef.h>
43
44 #ifdef HAVE_CONFIG_H
45 #include <config.h>
46 #endif
47
48 #include "MEM_guardedalloc.h"
49
50 #include "DNA_anim_types.h"
51 #include "DNA_action_types.h"
52 #include "DNA_armature_types.h"
53 #include "DNA_constraint_types.h"
54 #include "DNA_curve_types.h"
55 #include "DNA_camera_types.h"
56 #include "DNA_lamp_types.h"
57 #include "DNA_ipo_types.h"
58 #include "DNA_key_types.h"
59 #include "DNA_material_types.h"
60 #include "DNA_mesh_types.h"
61 #include "DNA_nla_types.h"
62 #include "DNA_object_types.h"
63 #include "DNA_object_force.h"
64 #include "DNA_particle_types.h"
65 #include "DNA_sequence_types.h"
66 #include "DNA_scene_types.h"
67 #include "DNA_sound_types.h"
68 #include "DNA_texture_types.h"
69 #include "DNA_view3d_types.h"
70 #include "DNA_world_types.h"
71
72 #include "BLI_blenlib.h"
73 #include "BLI_math.h"
74 #include "BLI_dynstr.h"
75
76 #include "BKE_utildefines.h"
77
78 #include "BKE_animsys.h"
79 #include "BKE_action.h"
80 #include "BKE_blender.h"
81 #include "BKE_curve.h"
82 #include "BKE_constraint.h"
83 #include "BKE_fcurve.h"
84 #include "BKE_global.h"
85 #include "BKE_ipo.h"
86 #include "BKE_library.h"
87 #include "BKE_main.h"
88 #include "BKE_mesh.h"
89 #include "BKE_nla.h"
90 #include "BKE_object.h"
91
92
93
94 /* *************************************************** */
95 /* Old-Data Freeing Tools */
96
97 /* Free data from old IPO-Blocks (those which haven't been converted), but not IPO block itself */
98 // XXX this shouldn't be necessary anymore, but may occur while not all data is converted yet
99 void free_ipo (Ipo *ipo)
100 {
101         IpoCurve *icu, *icn;
102         int n= 0;
103         
104         for (icu= ipo->curve.first; icu; icu= icn) {
105                 icn= icu->next;
106                 n++;
107                 
108                 if (icu->bezt) MEM_freeN(icu->bezt);
109                 if (icu->bp) MEM_freeN(icu->bp);
110                 if (icu->driver) MEM_freeN(icu->driver);
111                 
112                 BLI_freelinkN(&ipo->curve, icu);
113         }
114         
115         if (G.f & G_DEBUG)
116                 printf("Freed %d (Unconverted) Ipo-Curves from IPO '%s' \n", n, ipo->id.name+2);
117 }
118
119 /* *************************************************** */
120 /* ADRCODE to RNA-Path Conversion Code  - Special (Bitflags) */
121
122 /* Mapping Table for bitflag <-> RNA path */
123 typedef struct AdrBit2Path {
124         int bit;
125         char *path;
126         int array_index;
127 } AdrBit2Path;
128
129 /* ----------------- */
130 /* Mapping Tables to use bits <-> RNA paths */
131
132 /* Object layers */
133 static AdrBit2Path ob_layer_bits[]= {
134         {(1<<0), "layer", 0},
135         {(1<<1), "layer", 1},
136         {(1<<2), "layer", 2},
137         {(1<<3), "layer", 3},
138         {(1<<4), "layer", 4},
139         {(1<<5), "layer", 5},
140         {(1<<6), "layer", 6},
141         {(1<<7), "layer", 7},
142         {(1<<8), "layer", 8},
143         {(1<<9), "layer", 9},
144         {(1<<10), "layer", 10},
145         {(1<<11), "layer", 11},
146         {(1<<12), "layer", 12},
147         {(1<<13), "layer", 13},
148         {(1<<14), "layer", 14},
149         {(1<<15), "layer", 15},
150         {(1<<16), "layer", 16},
151         {(1<<17), "layer", 17},
152         {(1<<18), "layer", 18},
153         {(1<<19), "layer", 19},
154         {(1<<20), "layer", 20}
155 };
156
157 /* Material mode */
158 static AdrBit2Path ma_mode_bits[]= {
159 //      {MA_TRACEBLE, "traceable", 0},
160 //      {MA_SHADOW, "shadow", 0},
161 //      {MA_SHLESS, "shadeless", 0},
162 //      ...
163         {MA_RAYTRANSP, "transparency", 0},
164         {MA_RAYMIRROR, "raytrace_mirror.enabled", 0},
165 //      {MA_HALO, "type", MA_TYPE_HALO}
166 };
167
168 /* ----------------- */
169
170 /* quick macro for returning the appropriate array for adrcode_bitmaps_to_paths() */
171 #define RET_ABP(items) \
172         { \
173                 *tot= sizeof(items)/sizeof(AdrBit2Path); \
174                 return items; \
175         }
176
177 /* This function checks if a Blocktype+Adrcode combo, returning a mapping table */
178 static AdrBit2Path *adrcode_bitmaps_to_paths (int blocktype, int adrcode, int *tot)
179 {
180         /* Object layers */
181         if ((blocktype == ID_OB) && (adrcode == OB_LAY)) 
182                 RET_ABP(ob_layer_bits)
183         else if ((blocktype == ID_MA) && (adrcode == MA_MODE))
184                 RET_ABP(ma_mode_bits)
185         // XXX TODO: add other types...
186         
187         /* Normal curve */
188         return NULL;
189 }
190
191 /* *************************************************** */
192 /* ADRCODE to RNA-Path Conversion Code  - Standard */
193
194 /* Object types */
195 static char *ob_adrcodes_to_paths (int adrcode, int *array_index)
196 {
197         /* set array index like this in-case nothing sets it correctly  */
198         *array_index= 0;
199         
200         /* result depends on adrcode */
201         switch (adrcode) {
202                 case OB_LOC_X:
203                         *array_index= 0; return "location";
204                 case OB_LOC_Y:
205                         *array_index= 1; return "location";
206                 case OB_LOC_Z:
207                         *array_index= 2; return "location";
208                 case OB_DLOC_X:
209                         *array_index= 0; return "delta_location";
210                 case OB_DLOC_Y:
211                         *array_index= 1; return "delta_location";
212                 case OB_DLOC_Z:
213                         *array_index= 2; return "delta_location";
214                 
215                 case OB_ROT_X:
216                         *array_index= 0; return "rotation_euler";
217                 case OB_ROT_Y:
218                         *array_index= 1; return "rotation_euler";
219                 case OB_ROT_Z:
220                         *array_index= 2; return "rotation_euler";
221                 case OB_DROT_X:
222                         *array_index= 0; return "delta_rotation_euler";
223                 case OB_DROT_Y:
224                         *array_index= 1; return "delta_rotation_euler";
225                 case OB_DROT_Z:
226                         *array_index= 2; return "delta_rotation_euler";
227                         
228                 case OB_SIZE_X:
229                         *array_index= 0; return "scale";
230                 case OB_SIZE_Y:
231                         *array_index= 1; return "scale";
232                 case OB_SIZE_Z:
233                         *array_index= 2; return "scale";
234                 case OB_DSIZE_X:
235                         *array_index= 0; return "delta_scale";
236                 case OB_DSIZE_Y:
237                         *array_index= 1; return "delta_scale";
238                 case OB_DSIZE_Z:
239                         *array_index= 2; return "delta_scale";
240                 case OB_COL_R:
241                         *array_index= 0; return "color";
242                 case OB_COL_G:
243                         *array_index= 1; return "color";
244                 case OB_COL_B:
245                         *array_index= 2; return "color";
246                 case OB_COL_A:
247                         *array_index= 3; return "color";
248 #if 0
249                 case OB_PD_FSTR:
250                         if (ob->pd) poin= &(ob->pd->f_strength);
251                         break;
252                 case OB_PD_FFALL:
253                         if (ob->pd) poin= &(ob->pd->f_power);
254                         break;
255                 case OB_PD_SDAMP:
256                         if (ob->pd) poin= &(ob->pd->pdef_damp);
257                         break;
258                 case OB_PD_RDAMP:
259                         if (ob->pd) poin= &(ob->pd->pdef_rdamp);
260                         break;
261                 case OB_PD_PERM:
262                         if (ob->pd) poin= &(ob->pd->pdef_perm);
263                         break;
264                 case OB_PD_FMAXD:
265                         if (ob->pd) poin= &(ob->pd->maxdist);
266                         break;
267 #endif
268         }
269         
270         return NULL;
271 }
272
273 /* PoseChannel types 
274  * NOTE: pchan name comes from 'actname' added earlier... 
275  */
276 static char *pchan_adrcodes_to_paths (int adrcode, int *array_index)
277 {
278         /* set array index like this in-case nothing sets it correctly  */
279         *array_index= 0;
280         
281         /* result depends on adrcode */
282         switch (adrcode) {
283                 case AC_QUAT_W:
284                         *array_index= 0; return "rotation_quaternion";
285                 case AC_QUAT_X:
286                         *array_index= 1; return "rotation_quaternion";
287                 case AC_QUAT_Y:
288                         *array_index= 2; return "rotation_quaternion";
289                 case AC_QUAT_Z:
290                         *array_index= 3; return "rotation_quaternion";
291                         
292                 case AC_EUL_X:
293                         *array_index= 0; return "rotation_euler";
294                 case AC_EUL_Y:
295                         *array_index= 1; return "rotation_euler";
296                 case AC_EUL_Z:
297                         *array_index= 2; return "rotation_euler";
298                         
299                 case -1: /* special case for euler-rotations used by old drivers */
300                         *array_index= 0; return "rotation_euler";
301                         
302                 case AC_LOC_X:
303                         *array_index= 0; return "location";
304                 case AC_LOC_Y:
305                         *array_index= 1; return "location";
306                 case AC_LOC_Z:
307                         *array_index= 2; return "location";
308                 
309                 case AC_SIZE_X:
310                         *array_index= 0; return "scale";
311                 case AC_SIZE_Y:
312                         *array_index= 1; return "scale";
313                 case AC_SIZE_Z:
314                         *array_index= 2; return "scale";
315         }
316         
317         /* for debugging only */
318         printf("ERROR: unmatched PoseChannel setting (code %d) \n", adrcode);
319         return NULL;
320 }
321
322 /* Constraint types */
323 static char *constraint_adrcodes_to_paths (int adrcode, int *array_index)
324 {
325         /* set array index like this in-case nothing sets it correctly  */
326         *array_index= 0;
327         
328         /* result depends on adrcode */
329         switch (adrcode) {
330                 case CO_ENFORCE:
331                         return "influence";
332                 case CO_HEADTAIL:       // XXX this needs to be wrapped in RNA.. probably then this path will be invalid
333                         return "data.head_tail";
334         }
335         
336         return NULL;
337 }
338
339 /* ShapeKey types 
340  * NOTE: as we don't have access to the keyblock where the data comes from (for now), 
341  *              we'll just use numerical indicies for now... 
342  */
343 static char *shapekey_adrcodes_to_paths (int adrcode, int *array_index)
344 {
345         static char buf[128];
346         
347         /* block will be attached to ID_KE block, and setting that we alter is the 'value' (which sets keyblock.curval) */
348         // XXX adrcode 0 was dummy 'speed' curve 
349         if (adrcode == 0) 
350                 sprintf(buf, "speed");
351         else
352                 sprintf(buf, "keys[%d].value", adrcode);
353         return buf;
354 }
355
356 /* MTex (Texture Slot) types */
357 static char *mtex_adrcodes_to_paths (int adrcode, int *array_index)
358 {
359         char *base=NULL, *prop=NULL;
360         static char buf[128];
361         
362         /* base part of path */
363         if (adrcode & MA_MAP1) base= "textures[0]";
364         else if (adrcode & MA_MAP2) base= "textures[1]";
365         else if (adrcode & MA_MAP3) base= "textures[2]";
366         else if (adrcode & MA_MAP4) base= "textures[3]";
367         else if (adrcode & MA_MAP5) base= "textures[4]";
368         else if (adrcode & MA_MAP6) base= "textures[5]";
369         else if (adrcode & MA_MAP7) base= "textures[6]";
370         else if (adrcode & MA_MAP8) base= "textures[7]";
371         else if (adrcode & MA_MAP9) base= "textures[8]";
372         else if (adrcode & MA_MAP10) base= "textures[9]";
373         else if (adrcode & MA_MAP11) base= "textures[10]";
374         else if (adrcode & MA_MAP12) base= "textures[11]";
375         else if (adrcode & MA_MAP13) base= "textures[12]";
376         else if (adrcode & MA_MAP14) base= "textures[13]";
377         else if (adrcode & MA_MAP15) base= "textures[14]";
378         else if (adrcode & MA_MAP16) base= "textures[15]";
379         else if (adrcode & MA_MAP17) base= "textures[16]";
380         else if (adrcode & MA_MAP18) base= "textures[17]";
381                 
382         /* property identifier for path */
383         adrcode= (adrcode & (MA_MAP1-1));
384         switch (adrcode) {
385 #if 0 // XXX these are not wrapped in RNA yet!
386                 case MAP_OFS_X:
387                         poin= &(mtex->ofs[0]); break;
388                 case MAP_OFS_Y:
389                         poin= &(mtex->ofs[1]); break;
390                 case MAP_OFS_Z:
391                         poin= &(mtex->ofs[2]); break;
392                 case MAP_SIZE_X:
393                         poin= &(mtex->size[0]); break;
394                 case MAP_SIZE_Y:
395                         poin= &(mtex->size[1]); break;
396                 case MAP_SIZE_Z:
397                         poin= &(mtex->size[2]); break;
398                 case MAP_R:
399                         poin= &(mtex->r); break;
400                 case MAP_G:
401                         poin= &(mtex->g); break;
402                 case MAP_B:
403                         poin= &(mtex->b); break;
404                 case MAP_DVAR:
405                         poin= &(mtex->def_var); break;
406                 case MAP_COLF:
407                         poin= &(mtex->colfac); break;
408                 case MAP_NORF:
409                         poin= &(mtex->norfac); break;
410                 case MAP_VARF:
411                         poin= &(mtex->varfac); break;
412 #endif
413                 case MAP_DISP:
414                         prop= "warp_factor"; break;
415         }
416         
417         /* only build and return path if there's a property */
418         if (prop) {
419                 BLI_snprintf(buf, 128, "%s.%s", base, prop);
420                 return buf;
421         }
422         else
423                 return NULL;
424 }
425
426 /* Texture types */
427 static char *texture_adrcodes_to_paths (int adrcode, int *array_index)
428 {
429         /* set array index like this in-case nothing sets it correctly  */
430         *array_index= 0;
431         
432         /* result depends on adrcode */
433         switch (adrcode) {
434                 case TE_NSIZE:
435                         return "noise_size";
436                 case TE_TURB:
437                         return "turbulence";
438                         
439                 case TE_NDEPTH: // XXX texture RNA undefined
440                         //poin= &(tex->noisedepth); *type= IPO_SHORT; break;
441                         break;
442                 case TE_NTYPE: // XXX texture RNA undefined
443                         //poin= &(tex->noisetype); *type= IPO_SHORT; break;
444                         break;
445                         
446                 case TE_N_BAS1:
447                         return "noise_basis";
448                 case TE_N_BAS2:
449                         return "noise_basis"; // XXX this is not yet defined in RNA...
450                 
451                         /* voronoi */
452                 case TE_VNW1:
453                         *array_index= 0; return "feature_weights";
454                 case TE_VNW2:
455                         *array_index= 1; return "feature_weights";
456                 case TE_VNW3:
457                         *array_index= 2; return "feature_weights";
458                 case TE_VNW4:
459                         *array_index= 3; return "feature_weights";
460                 case TE_VNMEXP:
461                         return "minkovsky_exponent";
462                 case TE_VN_DISTM:
463                         return "distance_metric";
464                 case TE_VN_COLT:
465                         return "color_type";
466                 
467                         /* distorted noise / voronoi */
468                 case TE_ISCA:
469                         return "noise_intensity";
470                         
471                         /* distorted noise */
472                 case TE_DISTA:
473                         return "distortion_amount";
474                 
475                         /* musgrave */
476                 case TE_MG_TYP: // XXX texture RNA undefined
477                 //      poin= &(tex->stype); *type= IPO_SHORT; break;
478                         break;
479                 case TE_MGH:
480                         return "highest_dimension";
481                 case TE_MG_LAC:
482                         return "lacunarity";
483                 case TE_MG_OCT:
484                         return "octaves";
485                 case TE_MG_OFF:
486                         return "offset";
487                 case TE_MG_GAIN:
488                         return "gain";
489                         
490                 case TE_COL_R:
491                         *array_index= 0; return "rgb_factor";
492                 case TE_COL_G:
493                         *array_index= 1; return "rgb_factor";
494                 case TE_COL_B:
495                         *array_index= 2; return "rgb_factor";
496                         
497                 case TE_BRIGHT:
498                         return "brightness";
499                 case TE_CONTRA:
500                         return "constrast";
501         }
502         
503         return NULL;
504 }
505
506 /* Material Types */
507 static char *material_adrcodes_to_paths (int adrcode, int *array_index)
508 {
509         /* set array index like this in-case nothing sets it correctly  */
510         *array_index= 0;
511         
512         /* result depends on adrcode */
513         switch (adrcode) {
514                 case MA_COL_R:
515                         *array_index= 0; return "diffuse_color";
516                 case MA_COL_G:
517                         *array_index= 1; return "diffuse_color";
518                 case MA_COL_B:
519                         *array_index= 2; return "diffuse_color";
520                         
521                 case MA_SPEC_R:
522                         *array_index= 0; return "specular_color";
523                 case MA_SPEC_G:
524                         *array_index= 1; return "specular_color";
525                 case MA_SPEC_B:
526                         *array_index= 2; return "specular_color";
527                         
528                 case MA_MIR_R:
529                         *array_index= 0; return "mirror_color";
530                 case MA_MIR_G:
531                         *array_index= 1; return "mirror_color";
532                 case MA_MIR_B:
533                         *array_index= 2; return "mirror_color";
534                         
535                 case MA_ALPHA:
536                         return "alpha";
537                         
538                 case MA_REF:
539                         return "diffuse_reflection";
540                 
541                 case MA_EMIT:
542                         return "emit";
543                 
544                 case MA_AMB:
545                         return "ambient";
546                 
547                 case MA_SPEC:
548                         return "specular_reflection";
549                 
550                 case MA_HARD:
551                         return "specular_hardness";
552                         
553                 case MA_SPTR:
554                         return "specular_opacity";
555                         
556                 case MA_IOR:
557                         return "ior";
558                         
559                 case MA_HASIZE:
560                         return "halo.size";
561                         
562                 case MA_TRANSLU:
563                         return "translucency";
564                         
565                 case MA_RAYM:
566                         return "raytrace_mirror.reflect";
567                         
568                 case MA_FRESMIR:
569                         return "raytrace_mirror.fresnel";
570                         
571                 case MA_FRESMIRI:
572                         return "raytrace_mirror.fresnel_fac";
573                         
574                 case MA_FRESTRA:
575                         return "raytrace_transparency.fresnel";
576                         
577                 case MA_FRESTRAI:
578                         return "raytrace_transparency.fresnel_fac";
579                         
580                 case MA_ADD:
581                         return "halo.add";
582                 
583                 default: /* for now, we assume that the others were MTex channels */
584                         return mtex_adrcodes_to_paths(adrcode, array_index);
585         }
586         
587         return NULL;    
588 }
589
590 /* Camera Types */
591 static char *camera_adrcodes_to_paths (int adrcode, int *array_index)
592 {
593         /* set array index like this in-case nothing sets it correctly  */
594         *array_index= 0;
595         
596         /* result depends on adrcode */
597         switch (adrcode) {
598                 case CAM_LENS:
599 #if 0 // XXX this cannot be resolved easily... perhaps we assume camera is perspective (works for most cases...
600                         if (ca->type == CAM_ORTHO)
601                                 return "ortho_scale";
602                         else
603                                 return "lens"; 
604 #else // XXX lazy hack for now...
605                         return "lens";
606 #endif // XXX this cannot be resolved easily
607                         
608                 case CAM_STA:
609                         return "clip_start";
610                 case CAM_END:
611                         return "clip_end";
612                         
613 #if 0 // XXX these are not defined in RNA
614                 case CAM_YF_APERT:
615                         poin= &(ca->YF_aperture); break;
616                 case CAM_YF_FDIST:
617                         poin= &(ca->YF_dofdist); break;
618 #endif // XXX these are not defined in RNA
619                         
620                 case CAM_SHIFT_X:
621                         return "shift_x";
622                 case CAM_SHIFT_Y:
623                         return "shift_y";
624         }
625         
626         /* unrecognised adrcode, or not-yet-handled ones! */
627         return NULL;
628 }
629
630 /* Lamp Types */
631 static char *lamp_adrcodes_to_paths (int adrcode, int *array_index)
632 {
633         /* set array index like this in-case nothing sets it correctly  */
634         *array_index= 0;
635         
636         /* result depends on adrcode */
637         switch (adrcode) {
638                 case LA_ENERGY:
639                         return "energy";
640                         
641                 case LA_COL_R:
642                         *array_index= 0;  return "color";
643                 case LA_COL_G:
644                         *array_index= 1;  return "color";
645                 case LA_COL_B:
646                         *array_index= 2;  return "color";
647                         
648                 case LA_DIST:
649                         return "distance";
650                 
651                 case LA_SPOTSI:
652                         return "spot_size";
653                 case LA_SPOTBL:
654                         return "spot_blend";
655                         
656                 case LA_QUAD1:
657                         return "linear_attenuation";
658                 case LA_QUAD2:
659                         return "quadratic_attenuation";
660                         
661                 case LA_HALOINT:
662                         return "halo_intensity";
663                         
664                 default: /* for now, we assume that the others were MTex channels */
665                         return mtex_adrcodes_to_paths(adrcode, array_index);
666         }
667         
668         /* unrecognised adrcode, or not-yet-handled ones! */
669         return NULL;
670 }
671
672 /* Sound Types */
673 static char *sound_adrcodes_to_paths (int adrcode, int *array_index)
674 {
675         /* set array index like this in-case nothing sets it correctly  */
676         *array_index= 0;
677         
678         /* result depends on adrcode */
679         switch (adrcode) {
680                 case SND_VOLUME:
681                         return "volume";
682                 case SND_PITCH:
683                         return "pitch";
684         /* XXX Joshua -- I had wrapped panning in rna, but someone commented out, calling it "unused" */
685         /*      case SND_PANNING:
686                         return "panning"; */
687                 case SND_ATTEN:
688                         return "attenuation";
689         }
690         
691         /* unrecognised adrcode, or not-yet-handled ones! */
692         return NULL;
693 }
694
695 /* World Types */
696 static char *world_adrcodes_to_paths (int adrcode, int *array_index)
697 {
698         /* set array index like this in-case nothing sets it correctly  */
699         *array_index= 0;
700         
701         /* result depends on adrcode */
702         switch (adrcode) {
703                 case WO_HOR_R:
704                         *array_index= 0; return "horizon_color";
705                 case WO_HOR_G:
706                         *array_index= 1; return "horizon_color";
707                 case WO_HOR_B:
708                         *array_index= 2; return "horizon_color";
709                 case WO_ZEN_R:
710                         *array_index= 0; return "zenith_color";
711                 case WO_ZEN_G:
712                         *array_index= 1; return "zenith_color";
713                 case WO_ZEN_B:
714                         *array_index= 2; return "zenith_color";
715                 
716                 case WO_EXPOS:
717                         return "exposure";
718                 
719                 case WO_MISI:
720                         return "mist.intensity";
721                 case WO_MISTDI:
722                         return "mist.depth";
723                 case WO_MISTSTA:
724                         return "mist.start";
725                 case WO_MISTHI:
726                         return "mist.height";
727                 
728         /*      Star Color is unused -- recommend removal */
729         /*      case WO_STAR_R:
730                         *array_index= 0; return "stars.color";
731                 case WO_STAR_G:
732                         *array_index= 1; return "stars.color";
733                 case WO_STAR_B:
734                         *array_index= 2; return "stars.color"; */
735                 
736                 case WO_STARDIST:
737                         return "stars.min_distance";
738                 case WO_STARSIZE:
739                         return "stars.size";
740                 
741                 default: /* for now, we assume that the others were MTex channels */
742                         return mtex_adrcodes_to_paths(adrcode, array_index);
743                 }
744                 
745         return NULL;    
746 }
747
748 /* Particle Types */
749 static char *particle_adrcodes_to_paths (int adrcode, int *array_index)
750 {
751         /* set array index like this in-case nothing sets it correctly  */
752         *array_index= 0;
753         
754         /* result depends on adrcode */
755         switch (adrcode) {
756                 case PART_CLUMP:
757                         return "settings.clump_factor";
758                 case PART_AVE:
759                         return "settings.angular_velocity_factor";
760                 case PART_SIZE:
761                         return "settings.particle_size";
762                 case PART_DRAG:
763                         return "settings.drag_factor";
764                 case PART_BROWN:
765                         return "settings.brownian_factor";
766                 case PART_DAMP:
767                         return "settings.damp_factor";
768                 case PART_LENGTH:
769                         return "settings.length";
770                 case PART_GRAV_X:
771                         *array_index= 0; return "settings.acceleration";
772                 case PART_GRAV_Y:
773                         *array_index= 1; return "settings.acceleration";
774                 case PART_GRAV_Z:
775                         *array_index= 2; return "settings.acceleration";
776                 case PART_KINK_AMP:
777                         return "settings.kink_amplitude";
778                 case PART_KINK_FREQ:
779                         return "settings.kink_frequency";
780                 case PART_KINK_SHAPE:
781                         return "settings.kink_shape";
782                 case PART_BB_TILT:
783                         return "settings.billboard_tilt";
784                 
785                 /* PartDeflect needs to be sorted out properly in rna_object_force;
786                    If anyone else works on this, but is unfamiliar, these particular
787                         settings reference the particles of the system themselves
788                         being used as forces -- it will use the same rna structure
789                         as the similar object forces                            */
790                 /*case PART_PD_FSTR:
791                         if (part->pd) poin= &(part->pd->f_strength);
792                         break;
793                 case PART_PD_FFALL:
794                         if (part->pd) poin= &(part->pd->f_power);
795                         break;
796                 case PART_PD_FMAXD:
797                         if (part->pd) poin= &(part->pd->maxdist);
798                         break;
799                 case PART_PD2_FSTR:
800                         if (part->pd2) poin= &(part->pd2->f_strength);
801                         break;
802                 case PART_PD2_FFALL:
803                         if (part->pd2) poin= &(part->pd2->f_power);
804                         break;
805                 case PART_PD2_FMAXD:
806                         if (part->pd2) poin= &(part->pd2->maxdist);
807                         break;*/
808
809                 }
810                 
811         return NULL;    
812 }
813
814 /* ------- */
815
816 /* Allocate memory for RNA-path for some property given a blocktype, adrcode, and 'root' parts of path
817  *      Input:
818  *              - blocktype, adrcode    - determines setting to get
819  *              - actname, constname    - used to build path
820  *      Output:
821  *              - array_index                   - index in property's array (if applicable) to use
822  *              - return                                - the allocated path...
823  */
824 static char *get_rna_access (int blocktype, int adrcode, char actname[], char constname[], int *array_index)
825 {
826         DynStr *path= BLI_dynstr_new();
827         char *propname=NULL, *rpath=NULL;
828         char buf[512];
829         int dummy_index= 0;
830         
831         /* hack: if constname is set, we can only be dealing with an Constraint curve */
832         if (constname)
833                 blocktype= ID_CO;
834         
835         /* get property name based on blocktype */
836         switch (blocktype) {
837                 case ID_OB: /* object */
838                         propname= ob_adrcodes_to_paths(adrcode, &dummy_index);
839                         break;
840                 
841                 case ID_PO: /* pose channel */
842                         propname= pchan_adrcodes_to_paths(adrcode, &dummy_index);
843                         break;
844                         
845                 case ID_KE: /* shapekeys */
846                         propname= shapekey_adrcodes_to_paths(adrcode, &dummy_index);
847                         break;
848                         
849                 case ID_CO: /* constraint */
850                         propname= constraint_adrcodes_to_paths(adrcode, &dummy_index);  
851                         break;
852                         
853                 case ID_TE: /* texture */
854                         propname= texture_adrcodes_to_paths(adrcode, &dummy_index);
855                         break;
856                         
857                 case ID_MA: /* material */
858                         propname= material_adrcodes_to_paths(adrcode, &dummy_index);
859                         break;
860                         
861                 case ID_CA: /* camera */
862                         propname= camera_adrcodes_to_paths(adrcode, &dummy_index);
863                         break;
864                         
865                 case ID_LA: /* lamp */
866                         propname= lamp_adrcodes_to_paths(adrcode, &dummy_index);
867                         break;
868                 
869                 case ID_SO: /* sound */
870                         propname= sound_adrcodes_to_paths(adrcode, &dummy_index);
871                 
872                 case ID_WO: /* world */
873                         propname= world_adrcodes_to_paths(adrcode, &dummy_index);
874
875                 case ID_PA: /* particle */
876                         propname= particle_adrcodes_to_paths(adrcode, &dummy_index);
877                         
878                 /* XXX problematic blocktypes */
879                 case ID_CU: /* curve */
880                         /* this used to be a 'dummy' curve which got evaluated on the fly... 
881                          * now we've got real var for this!
882                          */
883                         propname= "eval_time";
884                         break;
885                         
886                 case ID_SEQ: /* sequencer strip */
887                         //SEQ_FAC1:
888                         switch (adrcode) {
889                         case SEQ_FAC1:
890                                 propname= "effect_fader";
891                                 break;
892                         case SEQ_FAC_SPEED:
893                                 propname= "speed_fader";
894                                 break;
895                         case SEQ_FAC_OPACITY:
896                                 propname= "blend_opacity";
897                                 break;
898                         }
899                         //      poin= &(seq->facf0); // XXX this doesn't seem to be included anywhere in sequencer RNA...
900                         break;
901                         
902                 /* special hacks */
903                 case -1:
904                         /* special case for rotdiff drivers... we don't need a property for this... */
905                         break;
906                         
907                 // TODO... add other blocktypes...
908                 default:
909                         printf("IPO2ANIMATO WARNING: No path for blocktype %d, adrcode %d yet \n", blocktype, adrcode);
910                         break;
911         }
912         
913         /* check if any property found 
914          *      - blocktype < 0 is special case for a specific type of driver, where we don't need a property name...
915          */
916         if ((propname == NULL) && (blocktype > 0)) {
917                 /* nothing was found, so exit */
918                 if (array_index) 
919                         *array_index= 0;
920                         
921                 BLI_dynstr_free(path);
922                 
923                 return NULL;
924         }
925         else {
926                 if (array_index)
927                         *array_index= dummy_index;
928         }
929         
930         /* append preceeding bits to path */
931         if ((actname && actname[0]) && (constname && constname[0])) {
932                 /* Constraint in Pose-Channel */
933                 sprintf(buf, "pose.bones[\"%s\"].constraints[\"%s\"]", actname, constname);
934         }
935         else if (actname && actname[0]) {
936                 /* Pose-Channel */
937                 sprintf(buf, "pose.bones[\"%s\"]", actname);
938         }
939         else if (constname && constname[0]) {
940                 /* Constraint in Object */
941                 sprintf(buf, "constraints[\"%s\"]", constname);
942         }
943         else
944                 strcpy(buf, ""); /* empty string */
945         BLI_dynstr_append(path, buf);
946         
947         /* append property to path (only if applicable) */
948         if (blocktype > 0) {
949                 /* need to add dot before property if there was anything precceding this */
950                 if (buf[0])
951                         BLI_dynstr_append(path, ".");
952                 
953                 /* now write name of property */
954                 BLI_dynstr_append(path, propname);
955         }
956         
957         /* convert to normal MEM_malloc'd string */
958         rpath= BLI_dynstr_get_cstring(path);
959         BLI_dynstr_free(path);
960         
961         /* return path... */
962         return rpath;
963 }
964
965 /* *************************************************** */
966 /* Conversion Utilities */
967
968 /* Convert IpoDriver to ChannelDriver - will free the old data (i.e. the old driver) */
969 static ChannelDriver *idriver_to_cdriver (IpoDriver *idriver)
970 {
971         ChannelDriver *cdriver;
972         
973         /* allocate memory for new driver */
974         cdriver= MEM_callocN(sizeof(ChannelDriver), "ChannelDriver");
975         
976         /* if 'pydriver', just copy data across */
977         if (idriver->type == IPO_DRIVER_TYPE_PYTHON) {
978                 /* PyDriver only requires the expression to be copied */
979                 // TODO: but the expression will be useless...
980                 cdriver->type = DRIVER_TYPE_PYTHON;
981                 strcpy(cdriver->expression, idriver->name); // XXX is this safe? 
982         }
983 #if 0 // XXX needs changes for the new system
984         else {
985                 DriverTarget *dtar=NULL, *dtar2=NULL;
986                 
987                 /* what to store depends on the 'blocktype' (ID_OB or ID_PO - object or posechannel) */
988                 if (idriver->blocktype == ID_AR) {
989                         /* ID_PO */
990                         if (idriver->adrcode == OB_ROT_DIFF) {
991                                 /* Rotational Difference is a special type of driver now... */
992                                 cdriver->type= DRIVER_TYPE_ROTDIFF;
993                                 
994                                 /* make 2 driver targets */
995                                 dtar= driver_add_new_target(cdriver);
996                                 dtar2= driver_add_new_target(cdriver);
997                                 
998                                 /* driver must use bones from same armature... */
999                                 dtar->id= dtar2->id= (ID *)idriver->ob;
1000                                 
1001                                 /* paths for the two targets get the pointers to the relevant Pose-Channels 
1002                                  *      - return pointers to Pose-Channels not rotation channels, as calculation code is picky
1003                                  *      - old bone names were stored in same var, in idriver->name
1004                                  *
1005                                  *      - we use several hacks here - blocktype == -1 specifies that no property needs to be found, and
1006                                  *        providing a name for 'actname' will automatically imply Pose-Channel with name 'actname'
1007                                  */
1008                                 dtar->rna_path= get_rna_access(-1, -1, idriver->name, NULL, NULL);
1009                                 dtar2->rna_path= get_rna_access(-1, -1, idriver->name+DRIVER_NAME_OFFS, NULL, NULL);
1010                         }
1011                         else {
1012                                 /* 'standard' driver */
1013                                 cdriver->type= DRIVER_TYPE_AVERAGE;
1014                                 
1015                                 /* make 1 driver target */
1016                                 dtar= driver_add_new_target(cdriver);
1017                                 dtar->id= (ID *)idriver->ob;
1018                                 
1019                                 switch (idriver->adrcode) {
1020                                         case OB_LOC_X:  /* x,y,z location are quite straightforward */
1021                                                 dtar->rna_path= get_rna_access(ID_PO, AC_LOC_X, idriver->name, NULL, &dtar->array_index);
1022                                                 break;
1023                                         case OB_LOC_Y:
1024                                                 dtar->rna_path= get_rna_access(ID_PO, AC_LOC_Y, idriver->name, NULL, &dtar->array_index);
1025                                                 break;
1026                                         case OB_LOC_Z:
1027                                                 dtar->rna_path= get_rna_access(ID_PO, AC_LOC_Z, idriver->name, NULL, &dtar->array_index);
1028                                                 break;
1029                                                 
1030                                         case OB_SIZE_X: /* x,y,z scaling are also quite straightforward */
1031                                                 dtar->rna_path= get_rna_access(ID_PO, AC_SIZE_X, idriver->name, NULL, &dtar->array_index);
1032                                                 break;
1033                                         case OB_SIZE_Y:
1034                                                 dtar->rna_path= get_rna_access(ID_PO, AC_SIZE_Y, idriver->name, NULL, &dtar->array_index);
1035                                                 break;
1036                                         case OB_SIZE_Z:
1037                                                 dtar->rna_path= get_rna_access(ID_PO, AC_SIZE_Z, idriver->name, NULL, &dtar->array_index);
1038                                                 break;  
1039                                                 
1040                                         case OB_ROT_X:  /* rotation - we need to be careful with this... */     
1041                                         case OB_ROT_Y:
1042                                         case OB_ROT_Z:
1043                                         {
1044                                                 /* -1 here, not rotation code, since old system didn't have eulers */
1045                                                 dtar->rna_path= get_rna_access(ID_PO, -1, idriver->name, NULL, NULL);
1046                                                 dtar->array_index= idriver->adrcode - OB_ROT_X;
1047                                         }
1048                                                 break;
1049                                 }
1050                         }
1051                 }
1052                 else {
1053                         /* ID_OB */
1054                         cdriver->type= DRIVER_TYPE_AVERAGE;
1055                         
1056                         /* make 1 driver target */
1057                         dtar= driver_add_new_target(cdriver);
1058                         
1059                         dtar->id= (ID *)idriver->ob;
1060                         dtar->rna_path= get_rna_access(ID_OB, idriver->adrcode, NULL, NULL, &dtar->array_index);
1061                 }
1062         }
1063 #endif // XXX fixme
1064         
1065         /* return the new one */
1066         return cdriver;
1067 }
1068
1069 /* Add F-Curve to the correct list 
1070  *      - grpname is needed to be used as group name where relevant, and is usually derived from actname
1071  */
1072 static void fcurve_add_to_list (ListBase *groups, ListBase *list, FCurve *fcu, char *grpname)
1073 {
1074         /* If we're adding to an action, we will have groups to write to... */
1075         if (groups && grpname) {
1076                 /* wrap the pointers given into a dummy action that we pass to the API func
1077                  * and extract the resultant lists...
1078                  */
1079                 bAction tmp_act;
1080                 bActionGroup *agrp= NULL;
1081                 
1082                 /* init the temp action */
1083                 //memset(&tmp_act, 0, sizeof(bAction)); // XXX only enable this line if we get errors
1084                 tmp_act.groups.first= groups->first;
1085                 tmp_act.groups.last= groups->last;
1086                 tmp_act.curves.first= list->first;
1087                 tmp_act.curves.last= list->last;
1088                 /* ... xxx, the other vars don't need to be filled in */
1089                 
1090                 /* get the group to use */
1091                 agrp= action_groups_find_named(&tmp_act, grpname);
1092                 if (agrp == NULL) {
1093                         /* no matching group, so add one */
1094                         if (agrp == NULL) {
1095                                 /* Add a new group, and make it active */
1096                                 agrp= MEM_callocN(sizeof(bActionGroup), "bActionGroup");
1097                                 
1098                                 agrp->flag = AGRP_SELECTED;
1099                                 BLI_snprintf(agrp->name, 64, grpname);
1100                                 
1101                                 BLI_addtail(&tmp_act.groups, agrp);
1102                                 BLI_uniquename(&tmp_act.groups, agrp, "Group", '.', offsetof(bActionGroup, name), 64);
1103                         }
1104                 }
1105                 
1106                 /* add F-Curve to group */
1107                 /* WARNING: this func should only need to look at the stuff we initialised, if not, things may crash */
1108                 action_groups_add_channel(&tmp_act, agrp, fcu);
1109                 
1110                 /* set the output lists based on the ones in the temp action */
1111                 groups->first= tmp_act.groups.first;
1112                 groups->last= tmp_act.groups.last;
1113                 list->first= tmp_act.curves.first;
1114                 list->last= tmp_act.curves.last;
1115         }
1116         else {
1117                 /* simply add the F-Curve to the end of the given list */
1118                 BLI_addtail(list, fcu);
1119         }
1120 }
1121
1122 /* Convert IPO-Curve to F-Curve (including Driver data), and free any of the old data that 
1123  * is not relevant, BUT do not free the IPO-Curve itself...
1124  *      actname: name of Action-Channel (if applicable) that IPO-Curve's IPO-block belonged to
1125  *      constname: name of Constraint-Channel (if applicable) that IPO-Curve's IPO-block belonged to
1126  */
1127 static void icu_to_fcurves (ListBase *groups, ListBase *list, IpoCurve *icu, char *actname, char *constname)
1128 {
1129         AdrBit2Path *abp;
1130         FCurve *fcu;
1131         int i=0, totbits;
1132         
1133         /* allocate memory for a new F-Curve */
1134         fcu= MEM_callocN(sizeof(FCurve), "FCurve");
1135         
1136         /* convert driver */
1137         if (icu->driver)
1138                 fcu->driver= idriver_to_cdriver(icu->driver);
1139         
1140         /* copy flags */
1141         if (icu->flag & IPO_VISIBLE) fcu->flag |= FCURVE_VISIBLE;
1142         if (icu->flag & IPO_SELECT) fcu->flag |= FCURVE_SELECTED;
1143         if (icu->flag & IPO_ACTIVE) fcu->flag |= FCURVE_ACTIVE;
1144         if (icu->flag & IPO_MUTE) fcu->flag |= FCURVE_MUTED;
1145         if (icu->flag & IPO_PROTECT) fcu->flag |= FCURVE_PROTECTED;
1146         if (icu->flag & IPO_AUTO_HORIZ) fcu->flag |= FCURVE_AUTO_HANDLES;
1147         
1148         /* set extrapolation */
1149         switch (icu->extrap) {
1150                 case IPO_HORIZ: /* constant extrapolation */
1151                 case IPO_DIR: /* linear extrapolation */
1152                 {
1153                         /* just copy, as the new defines match the old ones... */
1154                         fcu->extend= icu->extrap;
1155                 }
1156                         break;
1157                         
1158                 case IPO_CYCL: /* cyclic extrapolation */
1159                 case IPO_CYCLX: /* cyclic extrapolation + offset */
1160                 {
1161                         /* Add a new FModifier (Cyclic) instead of setting extend value 
1162                          * as that's the new equivilant of that option. 
1163                          */
1164                         FModifier *fcm= add_fmodifier(&fcu->modifiers, FMODIFIER_TYPE_CYCLES);
1165                         FMod_Cycles *data= (FMod_Cycles *)fcm->data;
1166                         
1167                         /* if 'offset' one is in use, set appropriate settings */
1168                         if (icu->extrap == IPO_CYCLX)
1169                                 data->before_mode= data->after_mode= FCM_EXTRAPOLATE_CYCLIC_OFFSET;
1170                         else
1171                                 data->before_mode= data->after_mode= FCM_EXTRAPOLATE_CYCLIC;
1172                 }
1173                         break;
1174         }
1175         
1176         /* -------- */
1177         
1178         /* get adrcode <-> bitflags mapping to handle nasty bitflag curves? */
1179         abp= adrcode_bitmaps_to_paths(icu->blocktype, icu->adrcode, &totbits);
1180         if (abp && totbits) {
1181                 FCurve *fcurve;
1182                 int b;
1183                 
1184                 if (G.f & G_DEBUG) printf("\tconvert bitflag ipocurve, totbits = %d \n", totbits);
1185                 
1186                 /* add the 'only int values' flag */
1187                 fcu->flag |= (FCURVE_INT_VALUES|FCURVE_DISCRETE_VALUES);                
1188                 
1189                 /* for each bit we have to remap + check for:
1190                  * 1) we need to make copy the existing F-Curve data (fcu -> fcurve),
1191                  *        except for the last one which will use the original 
1192                  * 2) copy the relevant path info across
1193                  * 3) filter the keyframes for the flag of interest
1194                  */
1195                 for (b=0; b < totbits; b++, abp++) {
1196                         /* make a copy of existing base-data if not the last curve */
1197                         if (b < (totbits-1))
1198                                 fcurve= copy_fcurve(fcu);
1199                         else
1200                                 fcurve= fcu;
1201                                 
1202                         /* set path */
1203                         fcurve->rna_path= BLI_strdupn(abp->path, strlen(abp->path));
1204                         fcurve->array_index= abp->array_index;
1205                         
1206                         /* convert keyframes 
1207                          *      - beztriples and bpoints are mutually exclusive, so we won't have both at the same time
1208                          *      - beztriples are more likely to be encountered as they are keyframes (the other type wasn't used yet)
1209                          */
1210                         fcurve->totvert= icu->totvert;
1211                         
1212                         if (icu->bezt) {
1213                                 BezTriple *dst, *src;
1214                                 
1215                                 /* allocate new array for keyframes/beztriples */
1216                                 fcurve->bezt= MEM_callocN(sizeof(BezTriple)*fcurve->totvert, "BezTriples");
1217                                 
1218                                 /* loop through copying all BezTriples individually, as we need to modify a few things */
1219                                 for (dst=fcurve->bezt, src=icu->bezt; i < fcurve->totvert; i++, dst++, src++) {
1220                                         /* firstly, copy BezTriple data */
1221                                         *dst= *src;
1222                                         
1223                                         /* interpolation can only be constant... */
1224                                         dst->ipo= BEZT_IPO_CONST;
1225                                         
1226                                         /* 'hide' flag is now used for keytype - only 'keyframes' existed before */
1227                                         dst->hide= BEZT_KEYTYPE_KEYFRAME;
1228                                         
1229                                         /* correct values, by checking if the flag of interest is set */
1230                                         if ( ((int)(dst->vec[1][1])) & (abp->bit) )
1231                                                 dst->vec[0][1]= dst->vec[1][1]= dst->vec[2][1] = 1.0f;
1232                                         else
1233                                                 dst->vec[0][1]= dst->vec[1][1]= dst->vec[2][1] = 0.0f;
1234                                 }
1235                         }
1236                         else if (icu->bp) {
1237                                 /* TODO: need to convert from BPoint type to the more compact FPoint type... but not priority, since no data used this */
1238                                 //BPoint *bp;
1239                                 //FPoint *fpt;
1240                         }
1241                         
1242                         /* add new F-Curve to list */
1243                         fcurve_add_to_list(groups, list, fcurve, actname);
1244                 }
1245         }
1246         else {
1247                 /* get rna-path
1248                  *      - we will need to set the 'disabled' flag if no path is able to be made (for now)
1249                  */
1250                 fcu->rna_path= get_rna_access(icu->blocktype, icu->adrcode, actname, constname, &fcu->array_index);
1251                 if (fcu->rna_path == NULL)
1252                         fcu->flag |= FCURVE_DISABLED;
1253                 
1254                 /* convert keyframes 
1255                  *      - beztriples and bpoints are mutually exclusive, so we won't have both at the same time
1256                  *      - beztriples are more likely to be encountered as they are keyframes (the other type wasn't used yet)
1257                  */
1258                 fcu->totvert= icu->totvert;
1259                 
1260                 if (icu->bezt) {
1261                         BezTriple *dst, *src;
1262                         
1263                         /* allocate new array for keyframes/beztriples */
1264                         fcu->bezt= MEM_callocN(sizeof(BezTriple)*fcu->totvert, "BezTriples");
1265                         
1266                         /* loop through copying all BezTriples individually, as we need to modify a few things */
1267                         for (dst=fcu->bezt, src=icu->bezt; i < fcu->totvert; i++, dst++, src++) {
1268                                 /* firstly, copy BezTriple data */
1269                                 *dst= *src;
1270                                 
1271                                 /* now copy interpolation from curve (if not already set) */
1272                                 if (icu->ipo != IPO_MIXED)
1273                                         dst->ipo= icu->ipo;
1274                                         
1275                                 /* 'hide' flag is now used for keytype - only 'keyframes' existed before */
1276                                 dst->hide= BEZT_KEYTYPE_KEYFRAME;
1277                                         
1278                                 /* correct values for euler rotation curves - they were degrees/10 */
1279                                 // XXX for now, just make them into radians as RNA sets/reads directly in that form
1280                                 if ( ((icu->blocktype == ID_OB) && ELEM3(icu->adrcode, OB_ROT_X, OB_ROT_Y, OB_ROT_Z)) ||
1281                                          ((icu->blocktype == ID_PO) && ELEM3(icu->adrcode, AC_EUL_X, AC_EUL_Y, AC_EUL_Z)) )
1282                                 {
1283                                         const float fac= (float)M_PI / 18.0f; //10.0f * M_PI/180.0f;
1284                                         
1285                                         dst->vec[0][1] *= fac;
1286                                         dst->vec[1][1] *= fac;
1287                                         dst->vec[2][1] *= fac;
1288                                 }
1289                                 
1290                                 /* correct times for rotation drivers 
1291                                  *      - need to go from degrees to radians...
1292                                  *      - there's only really 1 target to worry about 
1293                                  */
1294                                 if (fcu->driver && fcu->driver->variables.first) {
1295                                         DriverVar *dvar= fcu->driver->variables.first;
1296                                         DriverTarget *dtar= &dvar->targets[0];
1297                                         
1298                                         /* since drivers could only be for objects, we should just check for 'rotation' being 
1299                                          * in the name of the path given
1300                                          *      - WARNING: this will break if we encounter a bone or object explictly named in that way...
1301                                          */
1302                                         if ((dtar && dtar->rna_path) && strstr(dtar->rna_path, "rotation")) {
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);
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 (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(NULL, drivers, icu, actname, constname);
1361                         }
1362                         else {
1363                                 MEM_freeN(icu->driver);
1364                                 icu->driver= NULL;
1365                         }
1366                 }
1367                 else
1368                         icu_to_fcurves(animgroups, anim, icu, actname, constname);
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 (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(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(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(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(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(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         Scene *scene;
1623         
1624         if (main == NULL) {
1625                 printf("Argh! Main is NULL in do_versions_ipos_to_animato() \n");
1626                 return;
1627         }
1628                 
1629         /* only convert if version is right */
1630         // XXX???
1631         if (main->versionfile >= 250) {
1632                 printf("WARNING: Animation data too new to convert (Version %d) \n", main->versionfile);
1633                 return;
1634         }
1635         else
1636                 printf("INFO: Converting to Animato... \n"); // xxx debug
1637                 
1638         /* ----------- Animation Attached to Data -------------- */
1639         
1640         /* objects */
1641         for (id= main->object.first; id; id= id->next) {
1642                 Object *ob= (Object *)id;
1643                 bPoseChannel *pchan;
1644                 bConstraint *con;
1645                 bConstraintChannel *conchan, *conchann;
1646                 
1647                 if (G.f & G_DEBUG) printf("\tconverting ob %s \n", id->name+2);
1648                 
1649                 /* check if object has any animation data */
1650                 if (ob->nlastrips.first) {
1651                         /* Add AnimData block */
1652                         adt= BKE_id_add_animdata(id);
1653                         
1654                         /* IPO first to take into any non-NLA'd Object Animation */
1655                         if (ob->ipo) {
1656                                 ipo_to_animdata(id, ob->ipo, NULL, NULL);
1657                                 
1658                                 ob->ipo->id.us--;
1659                                 ob->ipo= NULL;
1660                         }
1661                         
1662                         /* Action is skipped since it'll be used by some strip in the NLA anyway, 
1663                          * causing errors with evaluation in the new evaluation pipeline
1664                          */
1665                         if (ob->action) {
1666                                 ob->action->id.us--;
1667                                 ob->action= NULL;
1668                         }
1669                         
1670                         /* finally NLA */
1671                         nlastrips_to_animdata(id, &ob->nlastrips);
1672                 }
1673                 else if ((ob->ipo) || (ob->action)) {
1674                         /* Add AnimData block */
1675                         adt= BKE_id_add_animdata(id);
1676                         
1677                         /* Action first - so that Action name get conserved */
1678                         if (ob->action) {
1679                                 action_to_animdata(id, ob->action);
1680                                 
1681                                 /* only decrease usercount if this Action isn't now being used by AnimData */
1682                                 if (ob->action != adt->action) {
1683                                         ob->action->id.us--;
1684                                         ob->action= NULL;
1685                                 }
1686                         }
1687                         
1688                         /* IPO second... */
1689                         if (ob->ipo) {
1690                                 ipo_to_animdata(id, ob->ipo, NULL, NULL);
1691                                 ob->ipo->id.us--;
1692                                 ob->ipo= NULL;
1693                         }
1694                 }
1695                 
1696                 /* check PoseChannels for constraints with local data */
1697                 if (ob->pose) {
1698                         /* Verify if there's AnimData block */
1699                         BKE_id_add_animdata(id);
1700                         
1701                         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1702                                 for (con= pchan->constraints.first; con; con= con->next) {
1703                                         /* if constraint has own IPO, convert add these to Object 
1704                                          * (NOTE: they're most likely to be drivers too) 
1705                                          */
1706                                         if (con->ipo) {
1707                                                 /* although this was the constraint's local IPO, we still need to provide pchan + con 
1708                                                  * so that drivers can be added properly...
1709                                                  */
1710                                                 ipo_to_animdata(id, con->ipo, pchan->name, con->name);
1711                                                 con->ipo->id.us--;
1712                                                 con->ipo= NULL;
1713                                         }
1714                                 }
1715                         }
1716                 }
1717                 
1718                 /* check constraints for local IPO's */
1719                 for (con= ob->constraints.first; con; con= con->next) {
1720                         /* if constraint has own IPO, convert add these to Object 
1721                          * (NOTE: they're most likely to be drivers too) 
1722                          */
1723                         if (con->ipo) {
1724                                 /* Verify if there's AnimData block, just in case */
1725                                 BKE_id_add_animdata(id);
1726                                 
1727                                 /* although this was the constraint's local IPO, we still need to provide con 
1728                                  * so that drivers can be added properly...
1729                                  */
1730                                 ipo_to_animdata(id, con->ipo, NULL, con->name);
1731                                 con->ipo->id.us--;
1732                                 con->ipo= NULL;
1733                         }
1734                          
1735                         /* check for Action Constraint */
1736                         // XXX do we really want to do this here?
1737                 }
1738                 
1739                 /* check constraint channels - we need to remove them anyway... */
1740                 if (ob->constraintChannels.first) {
1741                         /* Verify if there's AnimData block */
1742                         BKE_id_add_animdata(id);
1743                         
1744                         for (conchan= ob->constraintChannels.first; conchan; conchan= conchann) {
1745                                 /* get pointer to next Constraint Channel */
1746                                 conchann= conchan->next;
1747                                 
1748                                 /* convert Constraint Channel's IPO data */
1749                                 if (conchan->ipo) {
1750                                         ipo_to_animdata(id, conchan->ipo, NULL, conchan->name);
1751                                         conchan->ipo->id.us--;
1752                                         conchan->ipo= NULL;
1753                                 }
1754                                 
1755                                 /* free Constraint Channel */
1756                                 BLI_freelinkN(&ob->constraintChannels, conchan);
1757                         }
1758                 }
1759         }
1760         
1761         /* shapekeys */
1762         for (id= main->key.first; id; id= id->next) {
1763                 Key *key= (Key *)id;
1764                 
1765                 if (G.f & G_DEBUG) printf("\tconverting key %s \n", id->name+2);
1766                 
1767                 /* we're only interested in the IPO 
1768                  * NOTE: for later, it might be good to port these over to Object instead, as many of these
1769                  * are likely to be drivers, but it's hard to trace that from here, so move this to Ob loop?
1770                  */
1771                 if (key->ipo) {
1772                         /* Add AnimData block */
1773                         adt= BKE_id_add_animdata(id);
1774                         
1775                         /* Convert Shapekey data... */
1776                         ipo_to_animdata(id, key->ipo, NULL, NULL);
1777                         key->ipo->id.us--;
1778                         key->ipo= NULL;
1779                 }
1780         }
1781         
1782         /* materials */
1783         for (id= main->mat.first; id; id= id->next) {
1784                 Material *ma= (Material *)id;
1785                 
1786                 if (G.f & G_DEBUG) printf("\tconverting material %s \n", id->name+2);
1787                 
1788                 /* we're only interested in the IPO */
1789                 if (ma->ipo) {
1790                         /* Add AnimData block */
1791                         adt= BKE_id_add_animdata(id);
1792                         
1793                         /* Convert Material data... */
1794                         ipo_to_animdata(id, ma->ipo, NULL, NULL);
1795                         ma->ipo->id.us--;
1796                         ma->ipo= NULL;
1797                 }
1798         }
1799         
1800         /* sequence strips */
1801         for(scene = main->scene.first; scene; scene = scene->id.next) {
1802                 if(scene->ed && scene->ed->seqbasep) {
1803                         Sequence * seq;
1804
1805                         for(seq = scene->ed->seqbasep->first; 
1806                             seq; seq = seq->next) {
1807                                 short adrcode = SEQ_FAC1;
1808
1809                                 if (G.f & G_DEBUG) 
1810                                         printf("\tconverting sequence strip %s \n", seq->name+2);
1811
1812                                 if (!seq->ipo || !seq->ipo->curve.first) {
1813                                         seq->flag |= 
1814                                                 SEQ_USE_EFFECT_DEFAULT_FADE;
1815                                         continue;
1816                                 }
1817                                 
1818                                 /* patch adrcode, so that we can map
1819                                    to different DNA variables later 
1820                                    (semi-hack (tm) )
1821                                 */
1822                                 switch(seq->type) {
1823                                 case SEQ_IMAGE:
1824                                 case SEQ_META:
1825                                 case SEQ_SCENE:
1826                                 case SEQ_MOVIE:
1827                                 case SEQ_COLOR:
1828                                         adrcode = SEQ_FAC_OPACITY;
1829                                         break;
1830                                 case SEQ_SPEED:
1831                                         adrcode = SEQ_FAC_SPEED;
1832                                         break;
1833                                 }
1834                                 ((IpoCurve*) seq->ipo->curve.first)
1835                                         ->adrcode = adrcode;
1836                                 ipo_to_animdata((ID*) seq, seq->ipo,
1837                                                 NULL, NULL);
1838                                 seq->ipo->id.us--;
1839                                 seq->ipo = NULL;
1840                         }
1841                 }
1842         }
1843
1844
1845         /* textures */
1846         for (id= main->tex.first; id; id= id->next) {
1847                 Tex *te= (Tex *)id;
1848                 
1849                 if (G.f & G_DEBUG) printf("\tconverting texture %s \n", id->name+2);
1850                 
1851                 /* we're only interested in the IPO */
1852                 if (te->ipo) {
1853                         /* Add AnimData block */
1854                         adt= BKE_id_add_animdata(id);
1855                         
1856                         /* Convert Texture data... */
1857                         ipo_to_animdata(id, te->ipo, NULL, NULL);
1858                         te->ipo->id.us--;
1859                         te->ipo= NULL;
1860                 }
1861         }
1862         
1863         /* cameras */
1864         for (id= main->camera.first; id; id= id->next) {
1865                 Camera *ca= (Camera *)id;
1866                 
1867                 if (G.f & G_DEBUG) printf("\tconverting camera %s \n", id->name+2);
1868                 
1869                 /* we're only interested in the IPO */
1870                 if (ca->ipo) {
1871                         /* Add AnimData block */
1872                         adt= BKE_id_add_animdata(id);
1873                         
1874                         /* Convert Camera data... */
1875                         ipo_to_animdata(id, ca->ipo, NULL, NULL);
1876                         ca->ipo->id.us--;
1877                         ca->ipo= NULL;
1878                 }
1879         }
1880         
1881         /* lamps */
1882         for (id= main->lamp.first; id; id= id->next) {
1883                 Lamp *la= (Lamp *)id;
1884                 
1885                 if (G.f & G_DEBUG) printf("\tconverting lamp %s \n", id->name+2);
1886                 
1887                 /* we're only interested in the IPO */
1888                 if (la->ipo) {
1889                         /* Add AnimData block */
1890                         adt= BKE_id_add_animdata(id);
1891                         
1892                         /* Convert Lamp data... */
1893                         ipo_to_animdata(id, la->ipo, NULL, NULL);
1894                         la->ipo->id.us--;
1895                         la->ipo= NULL;
1896                 }
1897         }
1898         
1899         /* --------- Unconverted Animation Data ------------------ */
1900         /* For Animation data which may not be directly connected (i.e. not linked) to any other 
1901          * data, we need to perform a separate pass to make sure that they are converted to standalone
1902          * Actions which may then be able to be reused. This does mean that we will be going over data that's
1903          * already been converted, but there are no problems with that.
1904          *
1905          * The most common case for this will be Action Constraints, or IPO's with Fake-Users. 
1906          * We collect all drivers that were found into a temporary collection, and free them in one go, as they're 
1907          * impossible to resolve.
1908          */
1909         
1910         /* actions */
1911         for (id= main->action.first; id; id= id->next) {
1912                 bAction *act= (bAction *)id;
1913                 
1914                 if (G.f & G_DEBUG) printf("\tconverting action %s \n", id->name+2);
1915                 
1916                 /* be careful! some of the actions we encounter will be converted ones... */
1917                 action_to_animato(act, &act->groups, &act->curves, &drivers);
1918         }
1919         
1920         /* ipo's */
1921         for (id= main->ipo.first; id; id= id->next) {
1922                 Ipo *ipo= (Ipo *)id;
1923                 
1924                 if (G.f & G_DEBUG) printf("\tconverting ipo %s \n", id->name+2);
1925                 
1926                 /* most likely this IPO has already been processed, so check if any curves left to convert */
1927                 if (ipo->curve.first) {
1928                         bAction *new_act;
1929                         
1930                         /* add a new action for this, and convert all data into that action */
1931                         new_act= add_empty_action("ConvIPO_Action"); // XXX need a better name...
1932                         ipo_to_animato(ipo, NULL, NULL, NULL, &new_act->curves, &drivers);
1933                 }
1934                 
1935                 /* clear fake-users, and set user-count to zero to make sure it is cleared on file-save */
1936                 ipo->id.us= 0;
1937                 ipo->id.flag &= ~LIB_FAKEUSER;
1938         }
1939         
1940         /* free unused drivers from actions + ipos */
1941         free_fcurves(&drivers);
1942         
1943         printf("INFO: Animato convert done \n"); // xxx debug
1944 }
1945