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