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