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