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