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