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