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