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