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