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