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