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