migrated NDOF code from soc-2010-merwin, SpaceNavigator now works on Mac 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_reflection";
522                 
523                 case MA_EMIT:
524                         return "emit";
525                 
526                 case MA_AMB:
527                         return "ambient";
528                 
529                 case MA_SPEC:
530                         return "specular_reflection";
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_fac";
555                         
556                 case MA_FRESTRA:
557                         return "raytrace_transparency.fresnel";
558                         
559                 case MA_FRESTRAI:
560                         return "raytrace_transparency.fresnel_fac";
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         if (icu->flag & IPO_AUTO_HORIZ) fcu->flag |= FCURVE_AUTO_HANDLES;
1161         
1162         /* set extrapolation */
1163         switch (icu->extrap) {
1164                 case IPO_HORIZ: /* constant extrapolation */
1165                 case IPO_DIR: /* linear extrapolation */
1166                 {
1167                         /* just copy, as the new defines match the old ones... */
1168                         fcu->extend= icu->extrap;
1169                 }
1170                         break;
1171                         
1172                 case IPO_CYCL: /* cyclic extrapolation */
1173                 case IPO_CYCLX: /* cyclic extrapolation + offset */
1174                 {
1175                         /* Add a new FModifier (Cyclic) instead of setting extend value 
1176                          * as that's the new equivilant of that option. 
1177                          */
1178                         FModifier *fcm= add_fmodifier(&fcu->modifiers, FMODIFIER_TYPE_CYCLES);
1179                         FMod_Cycles *data= (FMod_Cycles *)fcm->data;
1180                         
1181                         /* if 'offset' one is in use, set appropriate settings */
1182                         if (icu->extrap == IPO_CYCLX)
1183                                 data->before_mode= data->after_mode= FCM_EXTRAPOLATE_CYCLIC_OFFSET;
1184                         else
1185                                 data->before_mode= data->after_mode= FCM_EXTRAPOLATE_CYCLIC;
1186                 }
1187                         break;
1188         }
1189         
1190         /* -------- */
1191         
1192         /* get adrcode <-> bitflags mapping to handle nasty bitflag curves? */
1193         abp= adrcode_bitmaps_to_paths(icu->blocktype, icu->adrcode, &totbits);
1194         if (abp && totbits) {
1195                 FCurve *fcurve;
1196                 int b;
1197                 
1198                 if (G.f & G_DEBUG) printf("\tconvert bitflag ipocurve, totbits = %d \n", totbits);
1199                 
1200                 /* add the 'only int values' flag */
1201                 fcu->flag |= (FCURVE_INT_VALUES|FCURVE_DISCRETE_VALUES);                
1202                 
1203                 /* for each bit we have to remap + check for:
1204                  * 1) we need to make copy the existing F-Curve data (fcu -> fcurve),
1205                  *        except for the last one which will use the original 
1206                  * 2) copy the relevant path info across
1207                  * 3) filter the keyframes for the flag of interest
1208                  */
1209                 for (b=0; b < totbits; b++, abp++) {
1210                         unsigned int i=0;
1211                         
1212                         /* make a copy of existing base-data if not the last curve */
1213                         if (b < (totbits-1))
1214                                 fcurve= copy_fcurve(fcu);
1215                         else
1216                                 fcurve= fcu;
1217                                 
1218                         /* set path */
1219                         fcurve->rna_path= BLI_strdup(abp->path);
1220                         fcurve->array_index= abp->array_index;
1221                         
1222                         /* convert keyframes 
1223                          *      - beztriples and bpoints are mutually exclusive, so we won't have both at the same time
1224                          *      - beztriples are more likely to be encountered as they are keyframes (the other type wasn't used yet)
1225                          */
1226                         fcurve->totvert= icu->totvert;
1227                         
1228                         if (icu->bezt) {
1229                                 BezTriple *dst, *src;
1230                                 
1231                                 /* allocate new array for keyframes/beztriples */
1232                                 fcurve->bezt= MEM_callocN(sizeof(BezTriple)*fcurve->totvert, "BezTriples");
1233                                 
1234                                 /* loop through copying all BezTriples individually, as we need to modify a few things */
1235                                 for (dst=fcurve->bezt, src=icu->bezt, i=0; i < fcurve->totvert; i++, dst++, src++) {
1236                                         /* firstly, copy BezTriple data */
1237                                         *dst= *src;
1238                                         
1239                                         /* interpolation can only be constant... */
1240                                         dst->ipo= BEZT_IPO_CONST;
1241                                         
1242                                         /* 'hide' flag is now used for keytype - only 'keyframes' existed before */
1243                                         dst->hide= BEZT_KEYTYPE_KEYFRAME;
1244                                         
1245                                         /* correct values, by checking if the flag of interest is set */
1246                                         if ( ((int)(dst->vec[1][1])) & (abp->bit) )
1247                                                 dst->vec[0][1]= dst->vec[1][1]= dst->vec[2][1] = 1.0f;
1248                                         else
1249                                                 dst->vec[0][1]= dst->vec[1][1]= dst->vec[2][1] = 0.0f;
1250                                 }
1251                         }
1252                         else if (icu->bp) {
1253                                 /* TODO: need to convert from BPoint type to the more compact FPoint type... but not priority, since no data used this */
1254                                 //BPoint *bp;
1255                                 //FPoint *fpt;
1256                         }
1257                         
1258                         /* add new F-Curve to list */
1259                         fcurve_add_to_list(groups, list, fcurve, actname, muteipo);
1260                 }
1261         }
1262         else {
1263                 unsigned int i=0;
1264                 
1265                 /* get rna-path
1266                  *      - we will need to set the 'disabled' flag if no path is able to be made (for now)
1267                  */
1268                 fcu->rna_path= get_rna_access(icu->blocktype, icu->adrcode, actname, constname, seq, &fcu->array_index);
1269                 if (fcu->rna_path == NULL)
1270                         fcu->flag |= FCURVE_DISABLED;
1271                 
1272                 /* convert keyframes 
1273                  *      - beztriples and bpoints are mutually exclusive, so we won't have both at the same time
1274                  *      - beztriples are more likely to be encountered as they are keyframes (the other type wasn't used yet)
1275                  */
1276                 fcu->totvert= icu->totvert;
1277                 
1278                 if (icu->bezt) {
1279                         BezTriple *dst, *src;
1280                         
1281                         /* allocate new array for keyframes/beztriples */
1282                         fcu->bezt= MEM_callocN(sizeof(BezTriple)*fcu->totvert, "BezTriples");
1283                         
1284                         /* loop through copying all BezTriples individually, as we need to modify a few things */
1285                         for (dst=fcu->bezt, src=icu->bezt, i=0; i < fcu->totvert; i++, dst++, src++) {
1286                                 /* firstly, copy BezTriple data */
1287                                 *dst= *src;
1288                                 
1289                                 /* now copy interpolation from curve (if not already set) */
1290                                 if (icu->ipo != IPO_MIXED)
1291                                         dst->ipo= icu->ipo;
1292                                         
1293                                 /* 'hide' flag is now used for keytype - only 'keyframes' existed before */
1294                                 dst->hide= BEZT_KEYTYPE_KEYFRAME;
1295                                         
1296                                 /* correct values for euler rotation curves 
1297                                  *      - they were degrees/10 
1298                                  *      - we need radians for RNA to do the right thing
1299                                  */
1300                                 if ( ((icu->blocktype == ID_OB) && ELEM3(icu->adrcode, OB_ROT_X, OB_ROT_Y, OB_ROT_Z)) ||
1301                                          ((icu->blocktype == ID_PO) && ELEM3(icu->adrcode, AC_EUL_X, AC_EUL_Y, AC_EUL_Z)) )
1302                                 {
1303                                         const float fac= (float)M_PI / 18.0f; //10.0f * M_PI/180.0f;
1304                                         
1305                                         dst->vec[0][1] *= fac;
1306                                         dst->vec[1][1] *= fac;
1307                                         dst->vec[2][1] *= fac;
1308                                 }
1309                                 
1310                                 /* correct values for path speed curves 
1311                                  *      - their values were 0-1
1312                                  *      - we now need as 'frames'
1313                                  */
1314                                 if ( (id) && (icu->blocktype == GS(id->name)) && 
1315                                          (fcu->rna_path && strcmp(fcu->rna_path, "eval_time")==0) )
1316                                 {
1317                                         Curve *cu = (Curve *)id;
1318                                         
1319                                         dst->vec[0][1] *= cu->pathlen;
1320                                         dst->vec[1][1] *= cu->pathlen;
1321                                         dst->vec[2][1] *= cu->pathlen;
1322                                 }
1323                                 
1324                                 /* correct times for rotation drivers 
1325                                  *      - need to go from degrees to radians...
1326                                  *      - there's only really 1 target to worry about 
1327                                  *  - were also degrees/10
1328                                  */
1329                                 if (fcu->driver && fcu->driver->variables.first) {
1330                                         DriverVar *dvar= fcu->driver->variables.first;
1331                                         DriverTarget *dtar= &dvar->targets[0];
1332                                         
1333                                         if (ELEM3(dtar->transChan, DTAR_TRANSCHAN_ROTX, DTAR_TRANSCHAN_ROTY, DTAR_TRANSCHAN_ROTZ)) {
1334                                                 const float fac= (float)M_PI / 18.0f;
1335                                                 
1336                                                 dst->vec[0][0] *= fac;
1337                                                 dst->vec[1][0] *= fac;
1338                                                 dst->vec[2][0] *= fac;
1339                                         }
1340                                 }
1341                                 
1342                                 /* correct values for sequencer curves, that were not locked to frame */
1343                                 if (seq && (seq->flag & SEQ_IPO_FRAME_LOCKED) == 0) {
1344                                         double mul= (seq->enddisp-seq->startdisp)/100.0f;
1345                                         double offset= seq->startdisp;
1346                                         
1347                                         dst->vec[0][0] *= mul;
1348                                         dst->vec[0][0] += offset;
1349                                         
1350                                         dst->vec[1][0] *= mul;
1351                                         dst->vec[1][0] += offset;
1352                                         
1353                                         dst->vec[2][0] *= mul;
1354                                         dst->vec[2][0] += offset;
1355                                 }
1356                         }
1357                 }
1358                 else if (icu->bp) {
1359                         /* TODO: need to convert from BPoint type to the more compact FPoint type... but not priority, since no data used this */
1360                         //BPoint *bp;
1361                         //FPoint *fpt;
1362                 }
1363                 
1364                 /* add new F-Curve to list */
1365                 fcurve_add_to_list(groups, list, fcu, actname, muteipo);
1366         }
1367 }
1368
1369 /* ------------------------- */
1370
1371 /* Convert IPO-block (i.e. all its IpoCurves) to the new system.
1372  * This does not assume that any ID or AnimData uses it, but does assume that
1373  * it is given two lists, which it will perform driver/animation-data separation.
1374  */
1375 static void ipo_to_animato (ID *id, Ipo *ipo, char actname[], char constname[], Sequence *seq, ListBase *animgroups, ListBase *anim, ListBase *drivers)
1376 {
1377         IpoCurve *icu;
1378         
1379         /* sanity check */
1380         if (ELEM3(NULL, ipo, anim, drivers))
1381                 return;
1382                 
1383         if (G.f & G_DEBUG) printf("ipo_to_animato \n");
1384                 
1385         /* validate actname and constname 
1386          *      - clear actname if it was one of the generic <builtin> ones (i.e. 'Object', or 'Shapes')
1387          *      - actname can then be used to assign F-Curves in Action to Action Groups 
1388          *        (i.e. thus keeping the benefits that used to be provided by Action Channels for grouping
1389          *              F-Curves for bones). This may be added later... for now let's just dump without them...
1390          */
1391         if (actname) {
1392                 if ((ipo->blocktype == ID_OB) && (strcmp(actname, "Object") == 0))
1393                         actname= NULL;
1394                 else if ((ipo->blocktype == ID_OB) && (strcmp(actname, "Shape") == 0))
1395                         actname= NULL;
1396         }
1397         
1398         /* loop over IPO-Curves, freeing as we progress */
1399         for (icu= ipo->curve.first; icu; icu= icu->next) {
1400                 /* Since an IPO-Curve may end up being made into many F-Curves (i.e. bitflag curves), 
1401                  * we figure out the best place to put the channel, then tell the curve-converter to just dump there
1402                  */
1403                 if (icu->driver) {
1404                         /* Blender 2.4x allowed empty drivers, but we don't now, since they cause more trouble than they're worth */
1405                         if ((icu->driver->ob) || (icu->driver->type == IPO_DRIVER_TYPE_PYTHON)) {
1406                                 icu_to_fcurves(id, NULL, drivers, icu, actname, constname, seq, ipo->muteipo);
1407                         }
1408                         else {
1409                                 MEM_freeN(icu->driver);
1410                                 icu->driver= NULL;
1411                         }
1412                 }
1413                 else
1414                         icu_to_fcurves(id, animgroups, anim, icu, actname, constname, seq, ipo->muteipo);
1415         }
1416         
1417         /* if this IPO block doesn't have any users after this one, free... */
1418         ipo->id.us--;
1419         if (ID_REAL_USERS(ipo) <= 0) {
1420                 IpoCurve *icn;
1421                 
1422                 for (icu= ipo->curve.first; icu; icu= icn) {
1423                         icn= icu->next;
1424                         
1425                         /* free driver */
1426                         if (icu->driver)
1427                                 MEM_freeN(icu->driver);
1428                                 
1429                         /* free old data of curve now that it's no longer needed for converting any more curves */
1430                         if (icu->bezt) MEM_freeN(icu->bezt);
1431                         if (icu->bp) MEM_freeN(icu->bezt);
1432                         
1433                         /* free this IPO-Curve */
1434                         BLI_freelinkN(&ipo->curve, icu);
1435                 }
1436         }
1437 }
1438
1439 /* Convert Action-block to new system, separating animation and drivers
1440  * New curves may not be converted directly into the given Action (i.e. for Actions linked
1441  * to Objects, where ob->ipo and ob->action need to be combined).
1442  * NOTE: we need to be careful here, as same data-structs are used for new system too!
1443  */
1444 static void action_to_animato (ID *id, bAction *act, ListBase *groups, ListBase *curves, ListBase *drivers)
1445 {
1446         bActionChannel *achan, *achann;
1447         bConstraintChannel *conchan, *conchann;
1448         
1449         /* only continue if there are Action Channels (indicating unconverted data) */
1450         if (act->chanbase.first == NULL)
1451                 return;
1452                 
1453         /* get rid of all Action Groups */
1454         // XXX this is risky if there's some old + some new data in the Action...
1455         if (act->groups.first) 
1456                 BLI_freelistN(&act->groups);
1457         
1458         /* loop through Action-Channels, converting data, freeing as we go */
1459         for (achan= act->chanbase.first; achan; achan= achann) {
1460                 /* get pointer to next Action Channel */
1461                 achann= achan->next;
1462                 
1463                 /* convert Action Channel's IPO data */
1464                 if (achan->ipo) {
1465                         ipo_to_animato(id, achan->ipo, achan->name, NULL, NULL, groups, curves, drivers);
1466                         achan->ipo->id.us--;
1467                         achan->ipo= NULL;
1468                 }
1469                 
1470                 /* convert constraint channel IPO-data */
1471                 for (conchan= achan->constraintChannels.first; conchan; conchan= conchann) {
1472                         /* get pointer to next Constraint Channel */
1473                         conchann= conchan->next;
1474                         
1475                         /* convert Constraint Channel's IPO data */
1476                         if (conchan->ipo) {
1477                                 ipo_to_animato(id, conchan->ipo, achan->name, conchan->name, NULL, groups, curves, drivers);
1478                                 conchan->ipo->id.us--;
1479                                 conchan->ipo= NULL;
1480                         }
1481                         
1482                         /* free Constraint Channel */
1483                         BLI_freelinkN(&achan->constraintChannels, conchan);
1484                 }
1485                 
1486                 /* free Action Channel */
1487                 BLI_freelinkN(&act->chanbase, achan);
1488         }
1489 }
1490
1491
1492 /* ------------------------- */
1493
1494 /* Convert IPO-block (i.e. all its IpoCurves) for some ID to the new system
1495  * This assumes that AnimData has been added already. Separation of drivers
1496  * from animation data is accomplished here too...
1497  */
1498 static void ipo_to_animdata (ID *id, Ipo *ipo, char actname[], char constname[], Sequence *seq)
1499 {
1500         AnimData *adt= BKE_animdata_from_id(id);
1501         ListBase anim = {NULL, NULL};
1502         ListBase drivers = {NULL, NULL};
1503         
1504         /* sanity check */
1505         if ELEM(NULL, id, ipo)
1506                 return;
1507         if (adt == NULL) {
1508                 printf("ERROR ipo_to_animdata(): adt invalid \n");
1509                 return;
1510         }
1511         
1512         if (G.f & G_DEBUG) {
1513                 printf("ipo to animdata - ID:%s, IPO:%s, actname:%s constname:%s seqname:%s  curves:%d \n", 
1514                        id->name+2, ipo->id.name+2, (actname)?actname:"<None>", (constname)?constname:"<None>", (seq)?(seq->name+2):"<None>",
1515                         BLI_countlist(&ipo->curve));
1516         }
1517         
1518         /* Convert curves to animato system (separated into separate lists of F-Curves for animation and drivers),
1519          * and the try to put these lists in the right places, but do not free the lists here
1520          */
1521         // XXX there shouldn't be any need for the groups, so don't supply pointer for that now... 
1522         ipo_to_animato(id, ipo, actname, constname, seq, NULL, &anim, &drivers);
1523         
1524         /* deal with animation first */
1525         if (anim.first) {
1526                 if (G.f & G_DEBUG) printf("\thas anim \n");
1527                 /* try to get action */
1528                 if (adt->action == NULL) {
1529                         char nameBuf[MAX_ID_NAME];
1530                         
1531                         BLI_snprintf(nameBuf, sizeof(nameBuf), "CDA:%s", ipo->id.name+2);
1532                         
1533                         adt->action= add_empty_action(nameBuf);
1534                         if (G.f & G_DEBUG) printf("\t\tadded new action - '%s' \n", nameBuf);
1535                 }
1536                 
1537                 /* add F-Curves to action */
1538                 BLI_movelisttolist(&adt->action->curves, &anim);
1539         }
1540         
1541         /* deal with drivers */
1542         if (drivers.first) {
1543                 if (G.f & G_DEBUG) printf("\thas drivers \n");
1544                 /* add drivers to end of driver stack */
1545                 BLI_movelisttolist(&adt->drivers, &drivers);
1546         }
1547 }
1548
1549 /* Convert Action-block to new system
1550  * NOTE: we need to be careful here, as same data-structs are used for new system too!
1551  */
1552 static void action_to_animdata (ID *id, bAction *act)
1553 {
1554         AnimData *adt= BKE_animdata_from_id(id);
1555         
1556         /* only continue if there are Action Channels (indicating unconverted data) */
1557         if (ELEM(NULL, adt, act->chanbase.first))
1558                 return;
1559         
1560         /* check if we need to set this Action as the AnimData's action */
1561         if (adt->action == NULL) {
1562                 /* set this Action as AnimData's Action */
1563                 if (G.f & G_DEBUG) printf("act_to_adt - set adt action to act \n");
1564                 adt->action= act;
1565         }
1566         
1567         /* convert Action data */
1568         action_to_animato(id, act, &adt->action->groups, &adt->action->curves, &adt->drivers);
1569 }
1570
1571 /* ------------------------- */
1572
1573 // TODO:
1574 //      - NLA group duplicators info
1575 //      - NLA curve/stride modifiers...
1576
1577 /* Convert NLA-Strip to new system */
1578 static void nlastrips_to_animdata (ID *id, ListBase *strips)
1579 {
1580         AnimData *adt= BKE_animdata_from_id(id);
1581         NlaTrack *nlt = NULL;
1582         NlaStrip *strip;
1583         bActionStrip *as, *asn;
1584         
1585         /* for each one of the original strips, convert to a new strip and free the old... */
1586         for (as= strips->first; as; as= asn) {
1587                 asn= as->next;
1588                 
1589                 /* this old strip is only worth something if it had an action... */
1590                 if (as->act) {
1591                         /* convert Action data (if not yet converted), storing the results in the same Action */
1592                         action_to_animato(id, as->act, &as->act->groups, &as->act->curves, &adt->drivers);
1593                         
1594                         /* create a new-style NLA-strip which references this Action, then copy over relevant settings */
1595                         {
1596                                 /* init a new strip, and assign the action to it 
1597                                  *      - no need to muck around with the user-counts, since this is just 
1598                                  *        passing over the ref to the new owner, not creating an additional ref
1599                                  */
1600                                 strip= MEM_callocN(sizeof(NlaStrip), "NlaStrip");
1601                                 strip->act= as->act;
1602                                 
1603                                         /* endpoints */
1604                                 strip->start= as->start;
1605                                 strip->end= as->end;
1606                                 strip->actstart= as->actstart;
1607                                 strip->actend= as->actend;
1608                                 
1609                                         /* action reuse */
1610                                 strip->repeat= as->repeat;
1611                                 strip->scale= as->scale;
1612                                 if (as->flag & ACTSTRIP_LOCK_ACTION)    strip->flag |= NLASTRIP_FLAG_SYNC_LENGTH;
1613                                 
1614                                         /* blending */
1615                                 strip->blendin= as->blendin;
1616                                 strip->blendout= as->blendout;
1617                                 strip->blendmode= (as->mode==ACTSTRIPMODE_ADD) ? NLASTRIP_MODE_ADD : NLASTRIP_MODE_REPLACE;
1618                                 if (as->flag & ACTSTRIP_AUTO_BLENDS)    strip->flag |= NLASTRIP_FLAG_AUTO_BLENDS;
1619                                         
1620                                         /* assorted setting flags */
1621                                 if (as->flag & ACTSTRIP_SELECT)                 strip->flag |= NLASTRIP_FLAG_SELECT;
1622                                 if (as->flag & ACTSTRIP_ACTIVE)                 strip->flag |= NLASTRIP_FLAG_ACTIVE;
1623                                 
1624                                 if (as->flag & ACTSTRIP_MUTE)                   strip->flag |= NLASTRIP_FLAG_MUTED;
1625                                 if (as->flag & ACTSTRIP_REVERSE)                strip->flag |= NLASTRIP_FLAG_REVERSE;
1626                                 
1627                                         /* by default, we now always extrapolate, while in the past this was optional */
1628                                 if ((as->flag & ACTSTRIP_HOLDLASTFRAME)==0) 
1629                                         strip->extendmode= NLASTRIP_EXTEND_NOTHING;
1630                         }       
1631                         
1632                         /* try to add this strip to the current NLA-Track (i.e. the 'last' one on the stack atm) */
1633                         if (BKE_nlatrack_add_strip(nlt, strip) == 0) {
1634                                 /* trying to add to the current failed (no space), 
1635                                  * so add a new track to the stack, and add to that...
1636                                  */
1637                                 nlt= add_nlatrack(adt, NULL);
1638                                 BKE_nlatrack_add_strip(nlt, strip);
1639                         }
1640                 }
1641                 
1642                 /* modifiers */
1643                 // FIXME: for now, we just free them...
1644                 if (as->modifiers.first)
1645                         BLI_freelistN(&as->modifiers);
1646                 
1647                 /* free the old strip */
1648                 BLI_freelinkN(strips, as);
1649         }
1650 }
1651
1652 /* *************************************************** */
1653 /* External API - Only Called from do_versions() */
1654
1655 /* Called from do_versions() in readfile.c to convert the old 'IPO/adrcode' system
1656  * to the new 'Animato/RNA' system.
1657  *
1658  * The basic method used here, is to loop over datablocks which have IPO-data, and 
1659  * add those IPO's to new AnimData blocks as Actions. 
1660  * Action/NLA data only works well for Objects, so these only need to be checked for there.
1661  *  
1662  * Data that has been converted should be freed immediately, which means that it is immediately
1663  * clear which datablocks have yet to be converted, and also prevent freeing errors when we exit.
1664  */
1665 // XXX currently done after all file reading... 
1666 void do_versions_ipos_to_animato(Main *main)
1667 {
1668         ListBase drivers = {NULL, NULL};
1669         ID *id;
1670         
1671         if (main == NULL) {
1672                 printf("Argh! Main is NULL in do_versions_ipos_to_animato() \n");
1673                 return;
1674         }
1675                 
1676         /* only convert if version is right */
1677         if (main->versionfile >= 250) {
1678                 printf("WARNING: Animation data too new to convert (Version %d) \n", main->versionfile);
1679                 return;
1680         }
1681         else if (G.f & G_DEBUG)
1682                 printf("INFO: Converting to Animato... \n");
1683                 
1684         /* ----------- Animation Attached to Data -------------- */
1685         
1686         /* objects */
1687         for (id= main->object.first; id; id= id->next) {
1688                 Object *ob= (Object *)id;
1689                 bPoseChannel *pchan;
1690                 bConstraint *con;
1691                 bConstraintChannel *conchan, *conchann;
1692                 
1693                 if (G.f & G_DEBUG) printf("\tconverting ob %s \n", id->name+2);
1694                 
1695                 /* check if object has any animation data */
1696                 if (ob->nlastrips.first) {
1697                         /* Add AnimData block */
1698                         BKE_id_add_animdata(id);
1699                         
1700                         /* IPO first to take into any non-NLA'd Object Animation */
1701                         if (ob->ipo) {
1702                                 ipo_to_animdata(id, ob->ipo, NULL, NULL, NULL);
1703                                 
1704                                 ob->ipo->id.us--;
1705                                 ob->ipo= NULL;
1706                         }
1707                         
1708                         /* Action is skipped since it'll be used by some strip in the NLA anyway, 
1709                          * causing errors with evaluation in the new evaluation pipeline
1710                          */
1711                         if (ob->action) {
1712                                 ob->action->id.us--;
1713                                 ob->action= NULL;
1714                         }
1715                         
1716                         /* finally NLA */
1717                         nlastrips_to_animdata(id, &ob->nlastrips);
1718                 }
1719                 else if ((ob->ipo) || (ob->action)) {
1720                         /* Add AnimData block */
1721                         AnimData *adt= BKE_id_add_animdata(id);
1722                         
1723                         /* Action first - so that Action name get conserved */
1724                         if (ob->action) {
1725                                 action_to_animdata(id, ob->action);
1726                                 
1727                                 /* only decrease usercount if this Action isn't now being used by AnimData */
1728                                 if (ob->action != adt->action) {
1729                                         ob->action->id.us--;
1730                                         ob->action= NULL;
1731                                 }
1732                         }
1733                         
1734                         /* IPO second... */
1735                         if (ob->ipo) {
1736                                 ipo_to_animdata(id, ob->ipo, NULL, NULL, NULL);
1737                                 ob->ipo->id.us--;
1738                                 ob->ipo= NULL;
1739                         }
1740                 }
1741                 
1742                 /* check PoseChannels for constraints with local data */
1743                 if (ob->pose) {
1744                         /* Verify if there's AnimData block */
1745                         BKE_id_add_animdata(id);
1746                         
1747                         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1748                                 for (con= pchan->constraints.first; con; con= con->next) {
1749                                         /* if constraint has own IPO, convert add these to Object 
1750                                          * (NOTE: they're most likely to be drivers too) 
1751                                          */
1752                                         if (con->ipo) {
1753                                                 /* although this was the constraint's local IPO, we still need to provide pchan + con 
1754                                                  * so that drivers can be added properly...
1755                                                  */
1756                                                 ipo_to_animdata(id, con->ipo, pchan->name, con->name, NULL);
1757                                                 con->ipo->id.us--;
1758                                                 con->ipo= NULL;
1759                                         }
1760                                 }
1761                         }
1762                 }
1763                 
1764                 /* check constraints for local IPO's */
1765                 for (con= ob->constraints.first; con; con= con->next) {
1766                         /* if constraint has own IPO, convert add these to Object 
1767                          * (NOTE: they're most likely to be drivers too) 
1768                          */
1769                         if (con->ipo) {
1770                                 /* Verify if there's AnimData block, just in case */
1771                                 BKE_id_add_animdata(id);
1772                                 
1773                                 /* although this was the constraint's local IPO, we still need to provide con 
1774                                  * so that drivers can be added properly...
1775                                  */
1776                                 ipo_to_animdata(id, con->ipo, NULL, con->name, NULL);
1777                                 con->ipo->id.us--;
1778                                 con->ipo= NULL;
1779                         }
1780                          
1781                         /* check for Action Constraint */
1782                         // XXX do we really want to do this here?
1783                 }
1784                 
1785                 /* check constraint channels - we need to remove them anyway... */
1786                 if (ob->constraintChannels.first) {
1787                         /* Verify if there's AnimData block */
1788                         BKE_id_add_animdata(id);
1789                         
1790                         for (conchan= ob->constraintChannels.first; conchan; conchan= conchann) {
1791                                 /* get pointer to next Constraint Channel */
1792                                 conchann= conchan->next;
1793                                 
1794                                 /* convert Constraint Channel's IPO data */
1795                                 if (conchan->ipo) {
1796                                         ipo_to_animdata(id, conchan->ipo, NULL, conchan->name, NULL);
1797                                         conchan->ipo->id.us--;
1798                                         conchan->ipo= NULL;
1799                                 }
1800                                 
1801                                 /* free Constraint Channel */
1802                                 BLI_freelinkN(&ob->constraintChannels, conchan);
1803                         }
1804                 }
1805                 
1806                 /* object's action will always be object-rooted */
1807                 {
1808                         AnimData *adt= BKE_animdata_from_id(id);
1809                         if (adt && adt->action)
1810                                 adt->action->idroot = ID_OB;
1811                 }
1812         }
1813         
1814         /* shapekeys */
1815         for (id= main->key.first; id; id= id->next) {
1816                 Key *key= (Key *)id;
1817                 
1818                 if (G.f & G_DEBUG) printf("\tconverting key %s \n", id->name+2);
1819                 
1820                 /* we're only interested in the IPO 
1821                  * NOTE: for later, it might be good to port these over to Object instead, as many of these
1822                  * are likely to be drivers, but it's hard to trace that from here, so move this to Ob loop?
1823                  */
1824                 if (key->ipo) {
1825                         /* Add AnimData block */
1826                         AnimData *adt= BKE_id_add_animdata(id);
1827                         
1828                         /* Convert Shapekey data... */
1829                         ipo_to_animdata(id, key->ipo, NULL, NULL, NULL);
1830                         
1831                         if (adt->action)
1832                                 adt->action->idroot = key->ipo->blocktype;
1833                         
1834                         key->ipo->id.us--;
1835                         key->ipo= NULL;
1836                 }
1837         }
1838         
1839         /* materials */
1840         for (id= main->mat.first; id; id= id->next) {
1841                 Material *ma= (Material *)id;
1842                 
1843                 if (G.f & G_DEBUG) printf("\tconverting material %s \n", id->name+2);
1844                 
1845                 /* we're only interested in the IPO */
1846                 if (ma->ipo) {
1847                         /* Add AnimData block */
1848                         AnimData *adt= BKE_id_add_animdata(id);
1849                         
1850                         /* Convert Material data... */
1851                         ipo_to_animdata(id, ma->ipo, NULL, NULL, NULL);
1852                         
1853                         if (adt->action)
1854                                 adt->action->idroot = ma->ipo->blocktype;
1855                         
1856                         ma->ipo->id.us--;
1857                         ma->ipo= NULL;
1858                 }
1859         }
1860         
1861         /* worlds */
1862         for (id= main->world.first; id; id= id->next) {
1863                 World *wo= (World *)id;
1864                 
1865                 if (G.f & G_DEBUG) printf("\tconverting world %s \n", id->name+2);
1866                 
1867                 /* we're only interested in the IPO */
1868                 if (wo->ipo) {
1869                         /* Add AnimData block */
1870                         AnimData *adt= BKE_id_add_animdata(id);
1871                         
1872                         /* Convert World data... */
1873                         ipo_to_animdata(id, wo->ipo, NULL, NULL, NULL);
1874                         
1875                         if (adt->action)
1876                                 adt->action->idroot = wo->ipo->blocktype;
1877                         
1878                         wo->ipo->id.us--;
1879                         wo->ipo= NULL;
1880                 }
1881         }
1882         
1883         /* sequence strips */
1884         for (id= main->scene.first; id; id= id->next) {
1885                 Scene *scene = (Scene *)id;
1886                 Editing * ed = scene->ed;
1887                 if (ed && ed->seqbasep) {
1888                         Sequence * seq;
1889                         
1890                         AnimData *adt= BKE_id_add_animdata(id);
1891                         
1892                         SEQ_BEGIN(ed, seq) {
1893                                 IpoCurve *icu = (seq->ipo) ? seq->ipo->curve.first : NULL;
1894                                 short adrcode = SEQ_FAC1;
1895                                 
1896                                 if (G.f & G_DEBUG) 
1897                                         printf("\tconverting sequence strip %s \n", seq->name+2);
1898                                 
1899                                 if (ELEM(NULL, seq->ipo, icu)) {
1900                                         seq->flag |= SEQ_USE_EFFECT_DEFAULT_FADE;
1901                                         continue;
1902                                 }
1903                                 
1904                                 /* patch adrcode, so that we can map
1905                                    to different DNA variables later 
1906                                    (semi-hack (tm) )
1907                                 */
1908                                 switch (seq->type) {
1909                                         case SEQ_IMAGE:
1910                                         case SEQ_META:
1911                                         case SEQ_SCENE:
1912                                         case SEQ_MOVIE:
1913                                         case SEQ_COLOR:
1914                                                 adrcode = SEQ_FAC_OPACITY;
1915                                                 break;
1916                                         case SEQ_SPEED:
1917                                                 adrcode = SEQ_FAC_SPEED;
1918                                                 break;
1919                                 }
1920                                 icu->adrcode = adrcode;
1921                                 
1922                                 /* convert IPO */
1923                                 ipo_to_animdata((ID *)scene, seq->ipo, NULL, NULL, seq);
1924                                 
1925                                 if (adt->action)
1926                                         adt->action->idroot = ID_SCE; /* scene-rooted */
1927                                 
1928                                 seq->ipo->id.us--;
1929                                 seq->ipo = NULL;
1930                         }
1931                         SEQ_END
1932                 }
1933         }
1934
1935
1936         /* textures */
1937         for (id= main->tex.first; id; id= id->next) {
1938                 Tex *te= (Tex *)id;
1939                 
1940                 if (G.f & G_DEBUG) printf("\tconverting texture %s \n", id->name+2);
1941                 
1942                 /* we're only interested in the IPO */
1943                 if (te->ipo) {
1944                         /* Add AnimData block */
1945                         AnimData *adt= BKE_id_add_animdata(id);
1946                         
1947                         /* Convert Texture data... */
1948                         ipo_to_animdata(id, te->ipo, NULL, NULL, NULL);
1949                         
1950                         if (adt->action)
1951                                 adt->action->idroot = te->ipo->blocktype;
1952                         
1953                         te->ipo->id.us--;
1954                         te->ipo= NULL;
1955                 }
1956         }
1957         
1958         /* cameras */
1959         for (id= main->camera.first; id; id= id->next) {
1960                 Camera *ca= (Camera *)id;
1961                 
1962                 if (G.f & G_DEBUG) printf("\tconverting camera %s \n", id->name+2);
1963                 
1964                 /* we're only interested in the IPO */
1965                 if (ca->ipo) {
1966                         /* Add AnimData block */
1967                         AnimData *adt= BKE_id_add_animdata(id);
1968                         
1969                         /* Convert Camera data... */
1970                         ipo_to_animdata(id, ca->ipo, NULL, NULL, NULL);
1971                         
1972                         if (adt->action)
1973                                 adt->action->idroot = ca->ipo->blocktype;
1974                         
1975                         ca->ipo->id.us--;
1976                         ca->ipo= NULL;
1977                 }
1978         }
1979         
1980         /* lamps */
1981         for (id= main->lamp.first; id; id= id->next) {
1982                 Lamp *la= (Lamp *)id;
1983                 
1984                 if (G.f & G_DEBUG) printf("\tconverting lamp %s \n", id->name+2);
1985                 
1986                 /* we're only interested in the IPO */
1987                 if (la->ipo) {
1988                         /* Add AnimData block */
1989                         AnimData *adt= BKE_id_add_animdata(id);
1990                         
1991                         /* Convert Lamp data... */
1992                         ipo_to_animdata(id, la->ipo, NULL, NULL, NULL);
1993                         
1994                         if (adt->action)
1995                                 adt->action->idroot = la->ipo->blocktype;
1996                         
1997                         la->ipo->id.us--;
1998                         la->ipo= NULL;
1999                 }
2000         }
2001         
2002         /* curves */
2003         for (id= main->curve.first; id; id= id->next) {
2004                 Curve *cu= (Curve *)id;
2005                 
2006                 if (G.f & G_DEBUG) printf("\tconverting curve %s \n", id->name+2);
2007                 
2008                 /* we're only interested in the IPO */
2009                 if (cu->ipo) {
2010                         /* Add AnimData block */
2011                         AnimData *adt= BKE_id_add_animdata(id);
2012                         
2013                         /* Convert Curve data... */
2014                         ipo_to_animdata(id, cu->ipo, NULL, NULL, NULL);
2015                         
2016                         if (adt->action)
2017                                 adt->action->idroot = cu->ipo->blocktype;
2018                         
2019                         cu->ipo->id.us--;
2020                         cu->ipo= NULL;
2021                 }
2022         }
2023         
2024         /* --------- Unconverted Animation Data ------------------ */
2025         /* For Animation data which may not be directly connected (i.e. not linked) to any other 
2026          * data, we need to perform a separate pass to make sure that they are converted to standalone
2027          * Actions which may then be able to be reused. This does mean that we will be going over data that's
2028          * already been converted, but there are no problems with that.
2029          *
2030          * The most common case for this will be Action Constraints, or IPO's with Fake-Users. 
2031          * We collect all drivers that were found into a temporary collection, and free them in one go, as they're 
2032          * impossible to resolve.
2033          */
2034         
2035         /* actions */
2036         for (id= main->action.first; id; id= id->next) {
2037                 bAction *act= (bAction *)id;
2038                 
2039                 if (G.f & G_DEBUG) printf("\tconverting action %s \n", id->name+2);
2040                 
2041                 /* if old action, it will be object-only... */
2042                 if (act->chanbase.first)
2043                         act->idroot = ID_OB;
2044                 
2045                 /* be careful! some of the actions we encounter will be converted ones... */
2046                 action_to_animato(NULL, act, &act->groups, &act->curves, &drivers);
2047         }
2048         
2049         /* ipo's */
2050         for (id= main->ipo.first; id; id= id->next) {
2051                 Ipo *ipo= (Ipo *)id;
2052                 
2053                 if (G.f & G_DEBUG) printf("\tconverting ipo %s \n", id->name+2);
2054                 
2055                 /* most likely this IPO has already been processed, so check if any curves left to convert */
2056                 if (ipo->curve.first) {
2057                         bAction *new_act;
2058                         
2059                         /* add a new action for this, and convert all data into that action */
2060                         new_act= add_empty_action("ConvIPO_Action"); // XXX need a better name...
2061                         ipo_to_animato(NULL, ipo, NULL, NULL, NULL, NULL, &new_act->curves, &drivers);
2062                         new_act->idroot = ipo->blocktype;
2063                 }
2064                 
2065                 /* clear fake-users, and set user-count to zero to make sure it is cleared on file-save */
2066                 ipo->id.us= 0;
2067                 ipo->id.flag &= ~LIB_FAKEUSER;
2068         }
2069         
2070         /* free unused drivers from actions + ipos */
2071         free_fcurves(&drivers);
2072         
2073         if (G.f & G_DEBUG)
2074                 printf("INFO: Animato convert done \n");
2075 }
2076