svn merge -r 20373:20515 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_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         
239 #if 0   
240                 case OB_COL_R:  
241                         poin= &(ob->col[0]); break;
242                 case OB_COL_G:
243                         poin= &(ob->col[1]); break;
244                 case OB_COL_B:
245                         poin= &(ob->col[2]); break;
246                 case OB_COL_A:
247                         poin= &(ob->col[3]); break;
248                         
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: // XXX special case for rotation drivers... until eulers are added...
300                         *array_index= 0; return "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 "specularity";
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         /* get property name based on blocktype */
831         switch (blocktype) {
832                 case ID_OB: /* object */
833                         propname= ob_adrcodes_to_paths(adrcode, &dummy_index);
834                         break;
835                 
836                 case ID_PO: /* pose channel */
837                         propname= pchan_adrcodes_to_paths(adrcode, &dummy_index);
838                         break;
839                         
840                 case ID_KE: /* shapekeys */
841                         propname= shapekey_adrcodes_to_paths(adrcode, &dummy_index);
842                         break;
843                         
844                 case ID_CO: /* constraint */
845                         propname= constraint_adrcodes_to_paths(adrcode, &dummy_index);
846                         break;
847                         
848                 case ID_TE: /* texture */
849                         propname= texture_adrcodes_to_paths(adrcode, &dummy_index);
850                         break;
851                         
852                 case ID_MA: /* material */
853                         propname= material_adrcodes_to_paths(adrcode, &dummy_index);
854                         break;
855                         
856                 case ID_CA: /* camera */
857                         propname= camera_adrcodes_to_paths(adrcode, &dummy_index);
858                         break;
859                         
860                 case ID_LA: /* lamp */
861                         propname= lamp_adrcodes_to_paths(adrcode, &dummy_index);
862                         break;
863                 
864                 case ID_SO: /* sound */
865                         propname= sound_adrcodes_to_paths(adrcode, &dummy_index);
866                 
867                 case ID_WO: /* world */
868                         propname= world_adrcodes_to_paths(adrcode, &dummy_index);
869
870                 case ID_PA: /* particle */
871                         propname= particle_adrcodes_to_paths(adrcode, &dummy_index);
872                         
873                 /* XXX problematic blocktypes */
874                 case ID_CU: /* curve */
875                         propname= "speed"; // XXX this was a 'dummy curve' that didn't really correspond to any real var...
876                         break;
877                         
878                 case ID_SEQ: /* sequencer strip */
879                         //SEQ_FAC1:
880                         //      poin= &(seq->facf0); // XXX this doesn't seem to be included anywhere in sequencer RNA...
881                         break;
882                         
883                 /* special hacks */
884                 case -1:
885                         /* special case for rotdiff drivers... we don't need a property for this... */
886                         break;
887                         
888                 // TODO... add other blocktypes...
889                 default:
890                         printf("IPO2ANIMATO WARNING: No path for blocktype %d, adrcode %d yet \n", blocktype, adrcode);
891                         break;
892         }
893         
894         /* check if any property found 
895          *      - blocktype < 0 is special case for a specific type of driver, where we don't need a property name...
896          */
897         if ((propname == NULL) && (blocktype > 0)) {
898                 /* nothing was found, so exit */
899                 if (array_index) 
900                         *array_index= 0;
901                         
902                 BLI_dynstr_free(path);
903                 
904                 return NULL;
905         }
906         else {
907                 if (array_index)
908                         *array_index= dummy_index;
909         }
910         
911         /* append preceeding bits to path */
912         if ((actname && actname[0]) && (constname && constname[0])) {
913                 /* Constraint in Pose-Channel */
914                 sprintf(buf, "pose.pose_channels[\"%s\"].constraints[\"%s\"]", actname, constname);
915         }
916         else if (actname && actname[0]) {
917                 /* Pose-Channel */
918                 sprintf(buf, "pose.pose_channels[\"%s\"]", actname);
919         }
920         else if (constname && constname[0]) {
921                 /* Constraint in Object */
922                 sprintf(buf, "constraints[\"%s\"]", constname);
923         }
924         else
925                 strcpy(buf, ""); /* empty string */
926         BLI_dynstr_append(path, buf);
927         
928         /* append property to path (only if applicable) */
929         if (blocktype > 0) {
930                 /* need to add dot before property if there was anything precceding this */
931                 if (buf[0])
932                         BLI_dynstr_append(path, ".");
933                 
934                 /* now write name of property */
935                 BLI_dynstr_append(path, propname);
936         }
937         
938         /* convert to normal MEM_malloc'd string */
939         rpath= BLI_dynstr_get_cstring(path);
940         BLI_dynstr_free(path);
941         
942         /* return path... */
943         return rpath;
944 }
945
946 /* *************************************************** */
947 /* Conversion Utilities */
948
949 /* Convert IpoDriver to ChannelDriver - will free the old data (i.e. the old driver) */
950 static ChannelDriver *idriver_to_cdriver (IpoDriver *idriver)
951 {
952         ChannelDriver *cdriver;
953         DriverTarget *dtar=NULL, *dtar2=NULL;
954         
955         /* allocate memory for new driver */
956         cdriver= MEM_callocN(sizeof(ChannelDriver), "ChannelDriver");
957         
958         /* if 'pydriver', just copy data across */
959         if (idriver->type == IPO_DRIVER_TYPE_PYTHON) {
960                 /* PyDriver only requires the expression to be copied */
961                 // TODO: but the expression will be useless...
962                 cdriver->type = DRIVER_TYPE_PYTHON;
963                 strcpy(cdriver->expression, idriver->name); // XXX is this safe? 
964         }
965         else {
966                 /* what to store depends on the 'blocktype' (ID_OB or ID_PO - object or posechannel) */
967                 if (idriver->blocktype == ID_AR) {
968                         /* ID_PO */
969                         if (idriver->adrcode == OB_ROT_DIFF) {
970                                 /* Rotational Difference is a special type of driver now... */
971                                 cdriver->type= DRIVER_TYPE_ROTDIFF;
972                                 
973                                 /* make 2 driver targets */
974                                 dtar= driver_add_new_target(cdriver);
975                                 dtar2= driver_add_new_target(cdriver);
976                                 
977                                 /* driver must use bones from same armature... */
978                                 dtar->id= dtar2->id= (ID *)idriver->ob;
979                                 
980                                 /* paths for the two targets get the pointers to the relevant Pose-Channels 
981                                  *      - return pointers to Pose-Channels not rotation channels, as calculation code is picky
982                                  *      - old bone names were stored in same var, in idriver->name
983                                  *
984                                  *      - we use several hacks here - blocktype == -1 specifies that no property needs to be found, and
985                                  *        providing a name for 'actname' will automatically imply Pose-Channel with name 'actname'
986                                  */
987                                 dtar->rna_path= get_rna_access(-1, -1, idriver->name, NULL, NULL);
988                                 dtar2->rna_path= get_rna_access(-1, -1, idriver->name+DRIVER_NAME_OFFS, NULL, NULL);
989                         }
990                         else {
991                                 /* 'standard' driver */
992                                 cdriver->type= DRIVER_TYPE_AVERAGE;
993                                 
994                                 /* make 1 driver target */
995                                 dtar= driver_add_new_target(cdriver);
996                                 dtar->id= (ID *)idriver->ob;
997                                 
998                                 switch (idriver->adrcode) {
999                                         case OB_LOC_X:  /* x,y,z location are quite straightforward */
1000                                                 dtar->rna_path= get_rna_access(ID_PO, AC_LOC_X, idriver->name, NULL, &dtar->array_index);
1001                                                 break;
1002                                         case OB_LOC_Y:
1003                                                 dtar->rna_path= get_rna_access(ID_PO, AC_LOC_Y, idriver->name, NULL, &dtar->array_index);
1004                                                 break;
1005                                         case OB_LOC_Z:
1006                                                 dtar->rna_path= get_rna_access(ID_PO, AC_LOC_Z, idriver->name, NULL, &dtar->array_index);
1007                                                 break;
1008                                                 
1009                                         case OB_SIZE_X: /* x,y,z scaling are also quite straightforward */
1010                                                 dtar->rna_path= get_rna_access(ID_PO, AC_SIZE_X, idriver->name, NULL, &dtar->array_index);
1011                                                 break;
1012                                         case OB_SIZE_Y:
1013                                                 dtar->rna_path= get_rna_access(ID_PO, AC_SIZE_Y, idriver->name, NULL, &dtar->array_index);
1014                                                 break;
1015                                         case OB_SIZE_Z:
1016                                                 dtar->rna_path= get_rna_access(ID_PO, AC_SIZE_Z, idriver->name, NULL, &dtar->array_index);
1017                                                 break;  
1018                                                 
1019                                         case OB_ROT_X:  /* rotation - we need to be careful with this... XXX (another reason why we need eulers) */     
1020                                         case OB_ROT_Y:
1021                                         case OB_ROT_Z:
1022                                         {
1023                                                 // 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)
1024                                                 // XXX -1 here is a special hack...
1025                                                 dtar->rna_path= get_rna_access(ID_PO, -1, idriver->name, NULL, NULL);
1026                                                 dtar->array_index= idriver->adrcode - OB_ROT_X;
1027                                         }
1028                                                 break;
1029                                 }
1030                         }
1031                 }
1032                 else {
1033                         /* ID_OB */
1034                         cdriver->type= DRIVER_TYPE_AVERAGE;
1035                         
1036                         /* make 1 driver target */
1037                         dtar= driver_add_new_target(cdriver);
1038                         
1039                         dtar->id= (ID *)idriver->ob;
1040                         dtar->rna_path= get_rna_access(ID_OB, idriver->adrcode, NULL, NULL, &dtar->array_index);
1041                 }
1042         }
1043         
1044         
1045         /* free old driver */
1046         MEM_freeN(idriver);
1047         
1048         /* return the new one */
1049         return cdriver;
1050 }
1051
1052 /* Add F-Curve to the correct list 
1053  *      - grpname is needed to be used as group name where relevant, and is usually derived from actname
1054  */
1055 static void fcurve_add_to_list (ListBase *groups, ListBase *list, FCurve *fcu, char *grpname)
1056 {
1057         /* If we're adding to an action, we will have groups to write to... */
1058         if (groups && grpname) {
1059                 /* wrap the pointers given into a dummy action that we pass to the API func
1060                  * and extract the resultant lists...
1061                  */
1062                 bAction tmp_act;
1063                 bActionGroup *agrp= NULL;
1064                 
1065                 /* init the temp action */
1066                 //memset(&tmp_act, 0, sizeof(bAction)); // XXX only enable this line if we get errors
1067                 tmp_act.groups.first= groups->first;
1068                 tmp_act.groups.last= groups->last;
1069                 tmp_act.curves.first= list->first;
1070                 tmp_act.curves.last= list->last;
1071                 /* ... xxx, the other vars don't need to be filled in */
1072                 
1073                 /* get the group to use */
1074                 agrp= action_groups_find_named(&tmp_act, grpname);
1075                 if (agrp == NULL) {
1076                         /* no matching group, so add one */
1077                         if (agrp == NULL) {
1078                                 /* Add a new group, and make it active */
1079                                 agrp= MEM_callocN(sizeof(bActionGroup), "bActionGroup");
1080                                 
1081                                 agrp->flag = AGRP_SELECTED;
1082                                 BLI_snprintf(agrp->name, 64, grpname);
1083                                 
1084                                 BLI_addtail(&tmp_act.groups, agrp);
1085                                 BLI_uniquename(&tmp_act.groups, agrp, "Group", '.', offsetof(bActionGroup, name), 64);
1086                         }
1087                 }
1088                 
1089                 /* add F-Curve to group */
1090                 /* WARNING: this func should only need to look at the stuff we initialised, if not, things may crash */
1091                 action_groups_add_channel(&tmp_act, agrp, fcu);
1092                 
1093                 /* set the output lists based on the ones in the temp action */
1094                 groups->first= tmp_act.groups.first;
1095                 groups->last= tmp_act.groups.last;
1096                 list->first= tmp_act.curves.first;
1097                 list->last= tmp_act.curves.last;
1098         }
1099         else {
1100                 /* simply add the F-Curve to the end of the given list */
1101                 BLI_addtail(list, fcu);
1102         }
1103 }
1104
1105 /* Convert IPO-Curve to F-Curve (including Driver data), and free any of the old data that 
1106  * is not relevant, BUT do not free the IPO-Curve itself...
1107  *      actname: name of Action-Channel (if applicable) that IPO-Curve's IPO-block belonged to
1108  *      constname: name of Constraint-Channel (if applicable) that IPO-Curve's IPO-block belonged to
1109  */
1110 static void icu_to_fcurves (ListBase *groups, ListBase *list, IpoCurve *icu, char *actname, char *constname)
1111 {
1112         AdrBit2Path *abp;
1113         FCurve *fcu;
1114         int i=0, totbits;
1115         
1116         /* allocate memory for a new F-Curve */
1117         fcu= MEM_callocN(sizeof(FCurve), "FCurve");
1118         
1119         /* convert driver - will free the old one... */
1120         if (icu->driver) {
1121                 fcu->driver= idriver_to_cdriver(icu->driver);
1122                 icu->driver= NULL;
1123         }
1124         
1125         /* copy flags */
1126         if (icu->flag & IPO_VISIBLE) fcu->flag |= FCURVE_VISIBLE;
1127         if (icu->flag & IPO_SELECT) fcu->flag |= FCURVE_SELECTED;
1128         if (icu->flag & IPO_ACTIVE) fcu->flag |= FCURVE_ACTIVE;
1129         if (icu->flag & IPO_MUTE) fcu->flag |= FCURVE_MUTED;
1130         if (icu->flag & IPO_PROTECT) fcu->flag |= FCURVE_PROTECTED;
1131         if (icu->flag & IPO_AUTO_HORIZ) fcu->flag |= FCURVE_AUTO_HANDLES;
1132         
1133         /* set extrapolation */
1134         switch (icu->extrap) {
1135                 case IPO_HORIZ: /* constant extrapolation */
1136                 case IPO_DIR: /* linear extrapolation */
1137                 {
1138                         /* just copy, as the new defines match the old ones... */
1139                         fcu->extend= icu->extrap;
1140                 }
1141                         break;
1142                         
1143                 case IPO_CYCL: /* cyclic extrapolation */
1144                 case IPO_CYCLX: /* cyclic extrapolation + offset */
1145                 {
1146                         /* Add a new FModifier (Cyclic) instead of setting extend value 
1147                          * as that's the new equivilant of that option. 
1148                          */
1149                         FModifier *fcm= fcurve_add_modifier(fcu, FMODIFIER_TYPE_CYCLES);
1150                         FMod_Cycles *data= (FMod_Cycles *)fcm->data;
1151                         
1152                         /* if 'offset' one is in use, set appropriate settings */
1153                         if (icu->extrap == IPO_CYCLX)
1154                                 data->before_mode= data->after_mode= FCM_EXTRAPOLATE_CYCLIC_OFFSET;
1155                         else
1156                                 data->before_mode= data->after_mode= FCM_EXTRAPOLATE_CYCLIC;
1157                 }
1158                         break;
1159         }
1160         
1161         /* -------- */
1162         
1163         /* get adrcode <-> bitflags mapping to handle nasty bitflag curves? */
1164         abp= adrcode_bitmaps_to_paths(icu->blocktype, icu->adrcode, &totbits);
1165         if (abp && totbits) {
1166                 FCurve *fcurve;
1167                 int b;
1168                 
1169                 if (G.f & G_DEBUG) printf("\tconvert bitflag ipocurve, totbits = %d \n", totbits);
1170                 
1171                 /* add the 'only int values' flag */
1172                 fcu->flag |= (FCURVE_INT_VALUES|FCURVE_DISCRETE_VALUES);                
1173                 
1174                 /* for each bit we have to remap + check for:
1175                  * 1) we need to make copy the existing F-Curve data (fcu -> fcurve),
1176                  *        except for the last one which will use the original 
1177                  * 2) copy the relevant path info across
1178                  * 3) filter the keyframes for the flag of interest
1179                  */
1180                 for (b=0; b < totbits; b++, abp++) {
1181                         /* make a copy of existing base-data if not the last curve */
1182                         if (b < (totbits-1))
1183                                 fcurve= copy_fcurve(fcu);
1184                         else
1185                                 fcurve= fcu;
1186                                 
1187                         /* set path */
1188                         fcurve->rna_path= BLI_strdupn(abp->path, strlen(abp->path));
1189                         fcurve->array_index= abp->array_index;
1190                         
1191                         /* convert keyframes 
1192                          *      - beztriples and bpoints are mutually exclusive, so we won't have both at the same time
1193                          *      - beztriples are more likely to be encountered as they are keyframes (the other type wasn't used yet)
1194                          */
1195                         fcurve->totvert= icu->totvert;
1196                         
1197                         if (icu->bezt) {
1198                                 BezTriple *dst, *src;
1199                                 
1200                                 /* allocate new array for keyframes/beztriples */
1201                                 fcurve->bezt= MEM_callocN(sizeof(BezTriple)*fcurve->totvert, "BezTriples");
1202                                 
1203                                 /* loop through copying all BezTriples individually, as we need to modify a few things */
1204                                 for (dst=fcurve->bezt, src=icu->bezt; i < fcurve->totvert; i++, dst++, src++) {
1205                                         /* firstly, copy BezTriple data */
1206                                         *dst= *src;
1207                                         
1208                                         /* interpolation can only be constant... */
1209                                         dst->ipo= BEZT_IPO_CONST;
1210                                         
1211                                         /* correct values, by checking if the flag of interest is set */
1212                                         if ( ((int)(dst->vec[1][1])) & (abp->bit) )
1213                                                 dst->vec[0][1]= dst->vec[1][1]= dst->vec[2][1] = 1.0f;
1214                                         else
1215                                                 dst->vec[0][1]= dst->vec[1][1]= dst->vec[2][1] = 0.0f;
1216                                 }
1217                         }
1218                         else if (icu->bp) {
1219                                 /* TODO: need to convert from BPoint type to the more compact FPoint type... but not priority, since no data used this */
1220                                 //BPoint *bp;
1221                                 //FPoint *fpt;
1222                         }
1223                         
1224                         /* add new F-Curve to list */
1225                         fcurve_add_to_list(groups, list, fcurve, actname);
1226                 }
1227                 
1228                 /* free old data of curve now that it's no longer needed for converting any more curves */
1229                 if (icu->bezt) MEM_freeN(icu->bezt);
1230                 if (icu->bp) MEM_freeN(icu->bezt);
1231         }
1232         else {
1233                 /* get rna-path
1234                  *      - we will need to set the 'disabled' flag if no path is able to be made (for now)
1235                  */
1236                 fcu->rna_path= get_rna_access(icu->blocktype, icu->adrcode, actname, constname, &fcu->array_index);
1237                 if (fcu->rna_path == NULL)
1238                         fcu->flag |= FCURVE_DISABLED;
1239                 
1240                 /* convert keyframes 
1241                  *      - beztriples and bpoints are mutually exclusive, so we won't have both at the same time
1242                  *      - beztriples are more likely to be encountered as they are keyframes (the other type wasn't used yet)
1243                  */
1244                 fcu->totvert= icu->totvert;
1245                 
1246                 if (icu->bezt) {
1247                         BezTriple *dst, *src;
1248                         
1249                         /* allocate new array for keyframes/beztriples */
1250                         fcu->bezt= MEM_callocN(sizeof(BezTriple)*fcu->totvert, "BezTriples");
1251                         
1252                         /* loop through copying all BezTriples individually, as we need to modify a few things */
1253                         for (dst=fcu->bezt, src=icu->bezt; i < fcu->totvert; i++, dst++, src++) {
1254                                 /* firstly, copy BezTriple data */
1255                                 *dst= *src;
1256                                 
1257                                 /* now copy interpolation from curve (if not already set) */
1258                                 if (icu->ipo != IPO_MIXED)
1259                                         dst->ipo= icu->ipo;
1260                                         
1261                                 /* correct values for euler rotation curves - they were degrees/10 */
1262                                 // XXX for now, just make them into radians as RNA sets/reads directly in that form
1263                                 if ( ((icu->blocktype == ID_OB) && ELEM3(icu->adrcode, OB_ROT_X, OB_ROT_Y, OB_ROT_Z)) ||
1264                                          ((icu->blocktype == ID_PO) && ELEM3(icu->adrcode, AC_EUL_X, AC_EUL_Y, AC_EUL_Z)) )
1265                                 {
1266                                         const float fac= (float)M_PI / 18.0f; //10.0f * M_PI/180.0f;
1267                                         
1268                                         dst->vec[0][1] *= fac;
1269                                         dst->vec[1][1] *= fac;
1270                                         dst->vec[2][1] *= fac;
1271                                 }
1272                         }
1273                         
1274                         /* free this data now */
1275                         MEM_freeN(icu->bezt);
1276                 }
1277                 else if (icu->bp) {
1278                         /* TODO: need to convert from BPoint type to the more compact FPoint type... but not priority, since no data used this */
1279                         //BPoint *bp;
1280                         //FPoint *fpt;
1281                 }
1282                 
1283                 /* add new F-Curve to list */
1284                 fcurve_add_to_list(groups, list, fcu, actname);
1285         }
1286 }
1287
1288 /* ------------------------- */
1289
1290 /* Convert IPO-block (i.e. all its IpoCurves) to the new system.
1291  * This does not assume that any ID or AnimData uses it, but does assume that
1292  * it is given two lists, which it will perform driver/animation-data separation.
1293  */
1294 static void ipo_to_animato (Ipo *ipo, char actname[], char constname[], ListBase *animgroups, ListBase *anim, ListBase *drivers)
1295 {
1296         IpoCurve *icu, *icn;
1297         
1298         /* sanity check */
1299         if (ELEM3(NULL, ipo, anim, drivers))
1300                 return;
1301                 
1302         if (G.f & G_DEBUG) printf("ipo_to_animato \n");
1303                 
1304         /* validate actname and constname 
1305          *      - clear actname if it was one of the generic <builtin> ones (i.e. 'Object', or 'Shapes')
1306          *      - actname can then be used to assign F-Curves in Action to Action Groups 
1307          *        (i.e. thus keeping the benefits that used to be provided by Action Channels for grouping
1308          *              F-Curves for bones). This may be added later... for now let's just dump without them...
1309          */
1310         if (actname) {
1311                 if ((ipo->blocktype == ID_OB) && (strcmp(actname, "Object") == 0))
1312                         actname= NULL;
1313                 else if ((ipo->blocktype == ID_OB) && (strcmp(actname, "Shape") == 0))
1314                         actname= NULL;
1315         }
1316         
1317         /* loop over IPO-Curves, freeing as we progress */
1318         for (icu= ipo->curve.first; icu; icu= icn) {
1319                 /* get link to next (for later) */
1320                 icn= icu->next;
1321                 
1322                 /* Since an IPO-Curve may end up being made into many F-Curves (i.e. bitflag curves), 
1323                  * we figure out the best place to put the channel, then tell the curve-converter to just dump there
1324                  */
1325                 if (icu->driver) {
1326                         /* Blender 2.4x allowed empty drivers, but we don't now, since they cause more trouble than they're worth */
1327                         if ((icu->driver->ob) || (icu->driver->type == IPO_DRIVER_TYPE_PYTHON))
1328                                 icu_to_fcurves(NULL, drivers, icu, actname, constname);
1329                         else
1330                                 MEM_freeN(icu->driver);
1331                 }
1332                 else
1333                         icu_to_fcurves(animgroups, anim, icu, actname, constname);
1334                 
1335                 /* free this IpoCurve now that it's been converted */
1336                 BLI_freelinkN(&ipo->curve, icu);
1337         }
1338 }
1339
1340 /* Convert Action-block to new system, separating animation and drivers
1341  * New curves may not be converted directly into the given Action (i.e. for Actions linked
1342  * to Objects, where ob->ipo and ob->action need to be combined).
1343  * NOTE: we need to be careful here, as same data-structs are used for new system too!
1344  */
1345 static void action_to_animato (bAction *act, ListBase *groups, ListBase *curves, ListBase *drivers)
1346 {
1347         bActionChannel *achan, *achann;
1348         bConstraintChannel *conchan, *conchann;
1349         
1350         /* only continue if there are Action Channels (indicating unconverted data) */
1351         if (act->chanbase.first == NULL)
1352                 return;
1353                 
1354         /* get rid of all Action Groups */
1355         // XXX this is risky if there's some old + some new data in the Action...
1356         if (act->groups.first) 
1357                 BLI_freelistN(&act->groups);
1358         
1359         /* loop through Action-Channels, converting data, freeing as we go */
1360         for (achan= act->chanbase.first; achan; achan= achann) {
1361                 /* get pointer to next Action Channel */
1362                 achann= achan->next;
1363                 
1364                 /* convert Action Channel's IPO data */
1365                 if (achan->ipo) {
1366                         ipo_to_animato(achan->ipo, achan->name, NULL, groups, curves, drivers);
1367                         achan->ipo->id.us--;
1368                         achan->ipo= NULL;
1369                 }
1370                 
1371                 /* convert constraint channel IPO-data */
1372                 for (conchan= achan->constraintChannels.first; conchan; conchan= conchann) {
1373                         /* get pointer to next Constraint Channel */
1374                         conchann= conchan->next;
1375                         
1376                         /* convert Constraint Channel's IPO data */
1377                         if (conchan->ipo) {
1378                                 ipo_to_animato(conchan->ipo, achan->name, conchan->name, groups, curves, drivers);
1379                                 conchan->ipo->id.us--;
1380                                 conchan->ipo= NULL;
1381                         }
1382                         
1383                         /* free Constraint Channel */
1384                         BLI_freelinkN(&achan->constraintChannels, conchan);
1385                 }
1386                 
1387                 /* free Action Channel */
1388                 BLI_freelinkN(&act->chanbase, achan);
1389         }
1390 }
1391
1392
1393 /* ------------------------- */
1394
1395 /* Convert IPO-block (i.e. all its IpoCurves) for some ID to the new system
1396  * This assumes that AnimData has been added already. Separation of drivers
1397  * from animation data is accomplished here too...
1398  */
1399 static void ipo_to_animdata (ID *id, Ipo *ipo, char actname[], char constname[])
1400 {
1401         AnimData *adt= BKE_animdata_from_id(id);
1402         ListBase anim = {NULL, NULL};
1403         ListBase drivers = {NULL, NULL};
1404         
1405         /* sanity check */
1406         if ELEM(NULL, id, ipo)
1407                 return;
1408         if (adt == NULL) {
1409                 printf("ERROR ipo_to_animdata(): adt invalid \n");
1410                 return;
1411         }
1412         
1413         if (G.f & G_DEBUG) {
1414                 printf("ipo to animdata - ID:%s, IPO:%s, actname:%s constname:%s  curves:%d \n", 
1415                         id->name+2, ipo->id.name+2, (actname)?actname:"<None>", (constname)?constname:"<None>", 
1416                         BLI_countlist(&ipo->curve));
1417         }
1418         
1419         /* Convert curves to animato system (separated into separate lists of F-Curves for animation and drivers),
1420          * and the try to put these lists in the right places, but do not free the lists here
1421          */
1422         // XXX there shouldn't be any need for the groups, so don't supply pointer for that now... 
1423         ipo_to_animato(ipo, actname, constname, NULL, &anim, &drivers);
1424         
1425         /* deal with animation first */
1426         if (anim.first) {
1427                 if (G.f & G_DEBUG) printf("\thas anim \n");
1428                 /* try to get action */
1429                 if (adt->action == NULL) {
1430                         adt->action= add_empty_action("ConvData_Action"); // XXX we need a better name for this
1431                         if (G.f & G_DEBUG) printf("\t\tadded new action \n");
1432                 }
1433                 
1434                 /* add F-Curves to action */
1435                 addlisttolist(&adt->action->curves, &anim);
1436         }
1437         
1438         /* deal with drivers */
1439         if (drivers.first) {
1440                 if (G.f & G_DEBUG) printf("\thas drivers \n");
1441                 /* add drivers to end of driver stack */
1442                 addlisttolist(&adt->drivers, &drivers);
1443         }
1444 }
1445
1446 /* Convert Action-block to new system
1447  * NOTE: we need to be careful here, as same data-structs are used for new system too!
1448  */
1449 static void action_to_animdata (ID *id, bAction *act)
1450 {
1451         AnimData *adt= BKE_animdata_from_id(id);
1452         
1453         /* only continue if there are Action Channels (indicating unconverted data) */
1454         if (ELEM(NULL, adt, act->chanbase.first))
1455                 return;
1456         
1457         /* check if we need to set this Action as the AnimData's action */
1458         if (adt->action == NULL) {
1459                 /* set this Action as AnimData's Action */
1460                 if (G.f & G_DEBUG) printf("act_to_adt - set adt action to act \n");
1461                 adt->action= act;
1462         }
1463         
1464         /* convert Action data */
1465         action_to_animato(act, &adt->action->groups, &adt->action->curves, &adt->drivers);
1466 }
1467
1468 /* *************************************************** */
1469 /* External API - Only Called from do_versions() */
1470
1471 /* Called from do_versions() in readfile.c to convert the old 'IPO/adrcode' system
1472  * to the new 'Animato/RNA' system.
1473  *
1474  * The basic method used here, is to loop over datablocks which have IPO-data, and 
1475  * add those IPO's to new AnimData blocks as Actions. 
1476  * Action/NLA data only works well for Objects, so these only need to be checked for there.
1477  *  
1478  * Data that has been converted should be freed immediately, which means that it is immediately
1479  * clear which datablocks have yet to be converted, and also prevent freeing errors when we exit.
1480  */
1481 // XXX currently done after all file reading... 
1482 void do_versions_ipos_to_animato(Main *main)
1483 {
1484         ListBase drivers = {NULL, NULL};
1485         ID *id;
1486         AnimData *adt;
1487         
1488         if (main == NULL) {
1489                 printf("Argh! Main is NULL in do_versions_ipos_to_animato() \n");
1490                 return;
1491         }
1492                 
1493         /* only convert if version is right */
1494         // XXX???
1495         if (main->versionfile >= 250) {
1496                 printf("WARNING: Animation data too new to convert (Version %d) \n", main->versionfile);
1497                 return;
1498         }
1499         else
1500                 printf("INFO: Converting to Animato... \n"); // xxx debug
1501                 
1502         /* ----------- Animation Attached to Data -------------- */
1503         
1504         /* objects */
1505         for (id= main->object.first; id; id= id->next) {
1506                 Object *ob= (Object *)id;
1507                 bPoseChannel *pchan;
1508                 bConstraint *con;
1509                 bConstraintChannel *conchan, *conchann;
1510                 
1511                 if (G.f & G_DEBUG) printf("\tconverting ob %s \n", id->name+2);
1512                 
1513                 /* check if object has any animation data */
1514                 if ((ob->ipo) || (ob->action) || (ob->nlastrips.first)) {
1515                         /* Add AnimData block */
1516                         adt= BKE_id_add_animdata(id);
1517                         
1518                         /* IPO first */
1519                         if (ob->ipo) {
1520                                 ipo_to_animdata(id, ob->ipo, NULL, NULL);
1521                                 ob->ipo->id.us--;
1522                                 ob->ipo= NULL;
1523                         }
1524                         
1525                         /* now Action */
1526                         if (ob->action) {
1527                                 action_to_animdata(id, ob->action);
1528                                 
1529                                 /* only decrease usercount if this Action isn't now being used by AnimData */
1530                                 if (ob->action != adt->action) {
1531                                         ob->action->id.us--;
1532                                         ob->action= NULL;
1533                                 }
1534                         }
1535                         
1536                         /* finally NLA */
1537                         // XXX todo... for now, new NLA code not hooked up yet, so keep old stuff (but not for too long!)
1538                 }
1539                 
1540                 /* check PoseChannels for constraints with local data */
1541                 if (ob->pose) {
1542                         /* Verify if there's AnimData block */
1543                         BKE_id_add_animdata(id);
1544                         
1545                         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1546                                 for (con= pchan->constraints.first; con; con= con->next) {
1547                                         /* if constraint has own IPO, convert add these to Object 
1548                                          * (NOTE: they're most likely to be drivers too) 
1549                                          */
1550                                         if (con->ipo) {
1551                                                 /* although this was the constraint's local IPO, we still need to provide pchan + con 
1552                                                  * so that drivers can be added properly...
1553                                                  */
1554                                                 ipo_to_animdata(id, con->ipo, pchan->name, con->name);
1555                                                 con->ipo->id.us--;
1556                                                 con->ipo= NULL;
1557                                         }
1558                                 }
1559                         }
1560                 }
1561                 
1562                 /* check constraints for local IPO's */
1563                 for (con= ob->constraints.first; con; con= con->next) {
1564                         /* if constraint has own IPO, convert add these to Object 
1565                          * (NOTE: they're most likely to be drivers too) 
1566                          */
1567                         if (con->ipo) {
1568                                 /* Verify if there's AnimData block, just in case */
1569                                 BKE_id_add_animdata(id);
1570                                 
1571                                 /* although this was the constraint's local IPO, we still need to provide con 
1572                                  * so that drivers can be added properly...
1573                                  */
1574                                 ipo_to_animdata(id, con->ipo, NULL, con->name);
1575                                 con->ipo->id.us--;
1576                                 con->ipo= NULL;
1577                         }
1578                          
1579                         /* check for Action Constraint */
1580                         // XXX do we really want to do this here?
1581                 }
1582                 
1583                 /* check constraint channels - we need to remove them anyway... */
1584                 if (ob->constraintChannels.first) {
1585                         /* Verify if there's AnimData block */
1586                         BKE_id_add_animdata(id);
1587                         
1588                         for (conchan= ob->constraintChannels.first; conchan; conchan= conchann) {
1589                                 /* get pointer to next Constraint Channel */
1590                                 conchann= conchan->next;
1591                                 
1592                                 /* convert Constraint Channel's IPO data */
1593                                 if (conchan->ipo) {
1594                                         ipo_to_animdata(id, conchan->ipo, NULL, conchan->name);
1595                                         conchan->ipo->id.us--;
1596                                         conchan->ipo= NULL;
1597                                 }
1598                                 
1599                                 /* free Constraint Channel */
1600                                 BLI_freelinkN(&ob->constraintChannels, conchan);
1601                         }
1602                 }
1603         }
1604         
1605         /* shapekeys */
1606         for (id= main->key.first; id; id= id->next) {
1607                 Key *key= (Key *)id;
1608                 
1609                 if (G.f & G_DEBUG) printf("\tconverting key %s \n", id->name+2);
1610                 
1611                 /* we're only interested in the IPO 
1612                  * NOTE: for later, it might be good to port these over to Object instead, as many of these
1613                  * are likely to be drivers, but it's hard to trace that from here, so move this to Ob loop?
1614                  */
1615                 if (key->ipo) {
1616                         /* Add AnimData block */
1617                         adt= BKE_id_add_animdata(id);
1618                         
1619                         /* Convert Shapekey data... */
1620                         ipo_to_animdata(id, key->ipo, NULL, NULL);
1621                         key->ipo->id.us--;
1622                         key->ipo= NULL;
1623                 }
1624         }
1625         
1626         /* materials */
1627         for (id= main->mat.first; id; id= id->next) {
1628                 Material *ma= (Material *)id;
1629                 
1630                 if (G.f & G_DEBUG) printf("\tconverting material %s \n", id->name+2);
1631                 
1632                 /* we're only interested in the IPO */
1633                 if (ma->ipo) {
1634                         /* Add AnimData block */
1635                         adt= BKE_id_add_animdata(id);
1636                         
1637                         /* Convert Material data... */
1638                         ipo_to_animdata(id, ma->ipo, NULL, NULL);
1639                         ma->ipo->id.us--;
1640                         ma->ipo= NULL;
1641                 }
1642         }
1643         
1644         /* textures */
1645         for (id= main->tex.first; id; id= id->next) {
1646                 Tex *te= (Tex *)id;
1647                 
1648                 if (G.f & G_DEBUG) printf("\tconverting texture %s \n", id->name+2);
1649                 
1650                 /* we're only interested in the IPO */
1651                 if (te->ipo) {
1652                         /* Add AnimData block */
1653                         adt= BKE_id_add_animdata(id);
1654                         
1655                         /* Convert Texture data... */
1656                         ipo_to_animdata(id, te->ipo, NULL, NULL);
1657                         te->ipo->id.us--;
1658                         te->ipo= NULL;
1659                 }
1660         }
1661         
1662         /* cameras */
1663         for (id= main->camera.first; id; id= id->next) {
1664                 Camera *ca= (Camera *)id;
1665                 
1666                 if (G.f & G_DEBUG) printf("\tconverting camera %s \n", id->name+2);
1667                 
1668                 /* we're only interested in the IPO */
1669                 if (ca->ipo) {
1670                         /* Add AnimData block */
1671                         adt= BKE_id_add_animdata(id);
1672                         
1673                         /* Convert Camera data... */
1674                         ipo_to_animdata(id, ca->ipo, NULL, NULL);
1675                         ca->ipo->id.us--;
1676                         ca->ipo= NULL;
1677                 }
1678         }
1679         
1680         /* lamps */
1681         for (id= main->lamp.first; id; id= id->next) {
1682                 Lamp *la= (Lamp *)id;
1683                 
1684                 if (G.f & G_DEBUG) printf("\tconverting lamp %s \n", id->name+2);
1685                 
1686                 /* we're only interested in the IPO */
1687                 if (la->ipo) {
1688                         /* Add AnimData block */
1689                         adt= BKE_id_add_animdata(id);
1690                         
1691                         /* Convert Lamp data... */
1692                         ipo_to_animdata(id, la->ipo, NULL, NULL);
1693                         la->ipo->id.us--;
1694                         la->ipo= NULL;
1695                 }
1696         }
1697         
1698         /* --------- Unconverted Animation Data ------------------ */
1699         /* For Animation data which may not be directly connected (i.e. not linked) to any other 
1700          * data, we need to perform a separate pass to make sure that they are converted to standalone
1701          * Actions which may then be able to be reused. This does mean that we will be going over data that's
1702          * already been converted, but there are no problems with that.
1703          *
1704          * The most common case for this will be Action Constraints, or IPO's with Fake-Users. 
1705          * We collect all drivers that were found into a temporary collection, and free them in one go, as they're 
1706          * impossible to resolve.
1707          */
1708         
1709         /* actions */
1710         for (id= main->action.first; id; id= id->next) {
1711                 bAction *act= (bAction *)id;
1712                 
1713                 if (G.f & G_DEBUG) printf("\tconverting action %s \n", id->name+2);
1714                 
1715                 /* be careful! some of the actions we encounter will be converted ones... */
1716                 action_to_animato(act, &act->groups, &act->curves, &drivers);
1717         }
1718         
1719         /* ipo's */
1720         for (id= main->ipo.first; id; id= id->next) {
1721                 Ipo *ipo= (Ipo *)id;
1722                 
1723                 if (G.f & G_DEBUG) printf("\tconverting ipo %s \n", id->name+2);
1724                 
1725                 /* most likely this IPO has already been processed, so check if any curves left to convert */
1726                 if (ipo->curve.first) {
1727                         bAction *new_act;
1728                         
1729                         /* add a new action for this, and convert all data into that action */
1730                         new_act= add_empty_action("ConvIPO_Action"); // XXX need a better name...
1731                         ipo_to_animato(ipo, NULL, NULL, NULL, &new_act->curves, &drivers);
1732                 }
1733                 
1734                 /* clear fake-users, and set user-count to zero to make sure it is cleared on file-save */
1735                 ipo->id.us= 0;
1736                 ipo->id.flag &= ~LIB_FAKEUSER;
1737         }
1738         
1739         /* free unused drivers from actions + ipos */
1740         free_fcurves(&drivers);
1741         
1742         printf("INFO: Animato convert done \n"); // xxx debug
1743 }