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