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