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