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