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