Silencing MSVC warnings in code that I maintain
[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         printf("Freed %d (Unconverted) Ipo-Curves from IPO '%s' \n", n, ipo->id.name+2);
113 }
114
115 /* *************************************************** */
116 /* ADRCODE to RNA-Path Conversion Code  - Special (Bitflags) */
117
118 /* Mapping Table for bitflag <-> RNA path */
119 typedef struct AdrBit2Path {
120         int bit;
121         char *path;
122         int array_index;
123 } AdrBit2Path;
124
125 /* ----------------- */
126 /* Mapping Tables to use bits <-> RNA paths */
127
128 /* Object layers */
129 static AdrBit2Path ob_layer_bits[]= {
130         {(1<<0), "layer", 0},
131         {(1<<1), "layer", 1},
132         {(1<<2), "layer", 2},
133         {(1<<3), "layer", 3},
134         {(1<<4), "layer", 4},
135         {(1<<5), "layer", 5},
136         {(1<<6), "layer", 6},
137         {(1<<7), "layer", 7},
138         {(1<<8), "layer", 8},
139         {(1<<9), "layer", 9},
140         {(1<<10), "layer", 10},
141         {(1<<11), "layer", 11},
142         {(1<<12), "layer", 12},
143         {(1<<13), "layer", 13},
144         {(1<<14), "layer", 14},
145         {(1<<15), "layer", 15},
146         {(1<<16), "layer", 16},
147         {(1<<17), "layer", 17},
148         {(1<<18), "layer", 18},
149         {(1<<19), "layer", 19},
150         {(1<<20), "layer", 20}
151 };
152
153 /* ----------------- */
154
155 /* This function checks if a Blocktype+Adrcode combo, returning a mapping table */
156 static AdrBit2Path *adrcode_bitmaps_to_paths (int blocktype, int adrcode, int *tot)
157 {
158         /* Object layers */
159         if ((blocktype == ID_OB) && (adrcode == OB_LAY)) {
160                 *tot= sizeof(ob_layer_bits)/sizeof(AdrBit2Path);
161                 return ob_layer_bits;
162         }
163         else if ((blocktype == ID_MA) && (adrcode == MA_MODE)) {
164                 // XXX to be added...
165         }
166         // XXX TODO: add other types...
167         
168         /* Normal curve */
169         return NULL;
170 }
171
172 /* This function makes a copy of a path stored in AdrBit2Path entry, and makes a guardedalloc copy */
173 static char *adrcode_bitmap_path_copy (const char *abp_path)
174 {
175         char *path;
176         int len;
177         
178         /* copy the path */
179         len= strlen(abp_path) + 1; // XXX is this safe?
180         path= MEM_callocN(len, "Bitflag IPO-Curve RNA-Path");
181         memcpy(path, abp_path, len);
182         
183         return path;
184 }
185
186 /* *************************************************** */
187 /* ADRCODE to RNA-Path Conversion Code  - Standard */
188
189 /* Object types */
190 static char *ob_adrcodes_to_paths (int adrcode, int *array_index)
191 {
192         /* set array index like this in-case nothing sets it correctly  */
193         *array_index= 0;
194         
195         /* result depends on adrcode */
196         switch (adrcode) {
197                 case OB_LOC_X:
198                         *array_index= 0; return "location";
199                 case OB_LOC_Y:
200                         *array_index= 1; return "location";
201                 case OB_LOC_Z:
202                         *array_index= 2; return "location";
203                 case OB_DLOC_X:
204                         *array_index= 0; return "delta_location";
205                 case OB_DLOC_Y:
206                         *array_index= 1; return "delta_location";
207                 case OB_DLOC_Z:
208                         *array_index= 2; return "delta_location";
209                 
210                 case OB_ROT_X:
211                         *array_index= 0; return "rotation";
212                 case OB_ROT_Y:
213                         *array_index= 1; return "rotation";
214                 case OB_ROT_Z:
215                         *array_index= 2; return "rotation";
216                 case OB_DROT_X:
217                         *array_index= 0; return "delta_rotation";
218                 case OB_DROT_Y:
219                         *array_index= 1; return "delta_rotation";
220                 case OB_DROT_Z:
221                         *array_index= 2; return "delta_rotation";
222                         
223                 case OB_SIZE_X:
224                         *array_index= 0; return "scale";
225                 case OB_SIZE_Y:
226                         *array_index= 1; return "scale";
227                 case OB_SIZE_Z:
228                         *array_index= 2; return "scale";
229                 case OB_DSIZE_X:
230                         *array_index= 0; return "delta_scale";
231                 case OB_DSIZE_Y:
232                         *array_index= 1; return "delta_scale";
233                 case OB_DSIZE_Z:
234                         *array_index= 2; return "delta_scale";
235         
236 #if 0   
237                 case OB_COL_R:  
238                         poin= &(ob->col[0]); break;
239                 case OB_COL_G:
240                         poin= &(ob->col[1]); break;
241                 case OB_COL_B:
242                         poin= &(ob->col[2]); break;
243                 case OB_COL_A:
244                         poin= &(ob->col[3]); break;
245                         
246                 case OB_PD_FSTR:
247                         if (ob->pd) poin= &(ob->pd->f_strength);
248                         break;
249                 case OB_PD_FFALL:
250                         if (ob->pd) poin= &(ob->pd->f_power);
251                         break;
252                 case OB_PD_SDAMP:
253                         if (ob->pd) poin= &(ob->pd->pdef_damp);
254                         break;
255                 case OB_PD_RDAMP:
256                         if (ob->pd) poin= &(ob->pd->pdef_rdamp);
257                         break;
258                 case OB_PD_PERM:
259                         if (ob->pd) poin= &(ob->pd->pdef_perm);
260                         break;
261                 case OB_PD_FMAXD:
262                         if (ob->pd) poin= &(ob->pd->maxdist);
263                         break;
264 #endif
265         }
266         
267         return NULL;
268 }
269
270 /* PoseChannel types 
271  * NOTE: pchan name comes from 'actname' added earlier... 
272  */
273 static char *pchan_adrcodes_to_paths (int adrcode, int *array_index)
274 {
275         /* set array index like this in-case nothing sets it correctly  */
276         *array_index= 0;
277         
278         /* result depends on adrcode */
279         switch (adrcode) {
280                 case AC_QUAT_W:
281                         *array_index= 0; return "rotation";
282                 case AC_QUAT_X:
283                         *array_index= 1; return "rotation";
284                 case AC_QUAT_Y:
285                         *array_index= 2; return "rotation";
286                 case AC_QUAT_Z:
287                         *array_index= 3; return "rotation";
288                         
289 #if 0 // XXX these were not 'official' channels (i.e. not in bf-releases)... these will need separate wrapping to work...
290                 case AC_EUL_X:
291                         *array_index= 0; return "rotation";
292                 case AC_EUL_Y:
293                         *array_index= 1; return "rotation";
294                 case AC_EUL_Z:
295                         *array_index= 2; return "rotation";
296 #endif 
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                                 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                                 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                         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         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                 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= MEM_dupallocN(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 object 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                                         const float fac= (float)M_PI / 18.0f; //10.0f * M_PI/180.0f;
1016                                         
1017                                         dst->vec[0][1] *= fac;
1018                                         dst->vec[1][1] *= fac;
1019                                         dst->vec[2][1] *= fac;
1020                                 }
1021                         }
1022                         
1023                         /* free this data now */
1024                         MEM_freeN(icu->bezt);
1025                 }
1026                 else if (icu->bp) {
1027                         /* TODO: need to convert from BPoint type to the more compact FPoint type... but not priority, since no data used this */
1028                         //BPoint *bp;
1029                         //FPoint *fpt;
1030                 }
1031                 
1032                 /* add new F-Curve to list */
1033                 BLI_addtail(list, fcu);
1034         }
1035 }
1036
1037 /* ------------------------- */
1038
1039 /* Convert IPO-block (i.e. all its IpoCurves) to the new system.
1040  * This does not assume that any ID or AnimData uses it, but does assume that
1041  * it is given two lists, which it will perform driver/animation-data separation.
1042  */
1043 static void ipo_to_animato (Ipo *ipo, char actname[], char constname[], ListBase *anim, ListBase *drivers)
1044 {
1045         IpoCurve *icu, *icn;
1046         
1047         /* sanity check */
1048         if (ELEM3(NULL, ipo, anim, drivers))
1049                 return;
1050                 
1051         printf("ipo_to_animato \n");
1052                 
1053         /* validate actname and constname 
1054          *      - clear actname if it was one of the generic <builtin> ones (i.e. 'Object', or 'Shapes')
1055          *      - actname can then be used to assign F-Curves in Action to Action Groups 
1056          *        (i.e. thus keeping the benefits that used to be provided by Action Channels for grouping
1057          *              F-Curves for bones). This may be added later... for now let's just dump without them...
1058          */
1059         if (actname) {
1060                 if ((ipo->blocktype == ID_OB) && (strcmp(actname, "Object") == 0))
1061                         actname= NULL;
1062                 else if ((ipo->blocktype == ID_OB) && (strcmp(actname, "Shape") == 0))
1063                         actname= NULL;
1064         }
1065         
1066         /* loop over IPO-Curves, freeing as we progress */
1067         for (icu= ipo->curve.first; icu; icu= icn) {
1068                 /* get link to next (for later) */
1069                 icn= icu->next;
1070                 
1071                 /* Since an IPO-Curve may end up being made into many F-Curves (i.e. bitflag curves), 
1072                  * we figure out the best place to put the channel, then tell the curve-converter to just dump there
1073                  */
1074                 if (icu->driver)
1075                         icu_to_fcurves(drivers, icu, actname, constname);
1076                 else
1077                         icu_to_fcurves(anim, icu, actname, constname);
1078                 
1079                 /* free this IpoCurve now that it's been converted */
1080                 BLI_freelinkN(&ipo->curve, icu);
1081         }
1082 }
1083
1084 /* Convert Action-block to new system, separating animation and drivers
1085  * New curves may not be converted directly into the given Action (i.e. for Actions linked
1086  * to Objects, where ob->ipo and ob->action need to be combined).
1087  * NOTE: we need to be careful here, as same data-structs are used for new system too!
1088  */
1089 static void action_to_animato (bAction *act, ListBase *curves, ListBase *drivers)
1090 {
1091         bActionChannel *achan, *achann;
1092         bConstraintChannel *conchan, *conchann;
1093         
1094         /* only continue if there are Action Channels (indicating unconverted data) */
1095         if (act->chanbase.first == NULL)
1096                 return;
1097                 
1098         /* get rid of all Action Groups */
1099         // XXX this is risky if there's some old + some new data in the Action...
1100         if (act->groups.first) 
1101                 BLI_freelistN(&act->groups);
1102         
1103         /* loop through Action-Channels, converting data, freeing as we go */
1104         for (achan= act->chanbase.first; achan; achan= achann) {
1105                 /* get pointer to next Action Channel */
1106                 achann= achan->next;
1107                 
1108                 /* convert Action Channel's IPO data */
1109                 if (achan->ipo) {
1110                         ipo_to_animato(achan->ipo, achan->name, NULL, curves, drivers);
1111                         achan->ipo->id.us--;
1112                         achan->ipo= NULL;
1113                 }
1114                 
1115                 /* convert constraint channel IPO-data */
1116                 for (conchan= achan->constraintChannels.first; conchan; conchan= conchann) {
1117                         /* get pointer to next Constraint Channel */
1118                         conchann= conchan->next;
1119                         
1120                         /* convert Constraint Channel's IPO data */
1121                         if (conchan->ipo) {
1122                                 ipo_to_animato(conchan->ipo, achan->name, conchan->name, curves, drivers);
1123                                 conchan->ipo->id.us--;
1124                                 conchan->ipo= NULL;
1125                         }
1126                         
1127                         /* free Constraint Channel */
1128                         BLI_freelinkN(&achan->constraintChannels, conchan);
1129                 }
1130                 
1131                 /* free Action Channel */
1132                 BLI_freelinkN(&act->chanbase, achan);
1133         }
1134 }
1135
1136
1137 /* ------------------------- */
1138
1139 /* Convert IPO-block (i.e. all its IpoCurves) for some ID to the new system
1140  * This assumes that AnimData has been added already. Separation of drivers
1141  * from animation data is accomplished here too...
1142  */
1143 static void ipo_to_animdata (ID *id, Ipo *ipo, char actname[], char constname[])
1144 {
1145         AnimData *adt= BKE_animdata_from_id(id);
1146         ListBase anim = {NULL, NULL};
1147         ListBase drivers = {NULL, NULL};
1148         
1149         /* sanity check */
1150         if ELEM(NULL, id, ipo)
1151                 return;
1152         if (adt == NULL) {
1153                 printf("ERROR ipo_to_animdata(): adt invalid \n");
1154                 return;
1155         }
1156         
1157         printf("ipo to animdata - ID:%s, IPO:%s, actname:%s constname:%s  curves:%d \n", 
1158                 id->name+2, ipo->id.name+2, (actname)?actname:"<None>", (constname)?constname:"<None>", 
1159                 BLI_countlist(&ipo->curve));
1160         
1161         /* Convert curves to animato system (separated into separate lists of F-Curves for animation and drivers),
1162          * and the try to put these lists in the right places, but do not free the lists here
1163          */
1164         ipo_to_animato(ipo, actname, constname, &anim, &drivers);
1165         
1166         /* deal with animation first */
1167         if (anim.first) {
1168                 printf("\thas anim \n");
1169                 /* try to get action */
1170                 if (adt->action == NULL) {
1171                         adt->action= add_empty_action("ConvData_Action"); // XXX we need a better name for this
1172                         printf("\t\tadded new action \n");
1173                 }
1174                 
1175                 /* add F-Curves to action */
1176                 addlisttolist(&adt->action->curves, &anim);
1177         }
1178         
1179         /* deal with drivers */
1180         if (drivers.first) {
1181                 printf("\thas drivers \n");
1182                 /* add drivers to end of driver stack */
1183                 addlisttolist(&adt->drivers, &drivers);
1184         }
1185 }
1186
1187 /* Convert Action-block to new system
1188  * NOTE: we need to be careful here, as same data-structs are used for new system too!
1189  */
1190 static void action_to_animdata (ID *id, bAction *act)
1191 {
1192         AnimData *adt= BKE_animdata_from_id(id);
1193         
1194         /* only continue if there are Action Channels (indicating unconverted data) */
1195         if (ELEM(NULL, adt, act->chanbase.first))
1196                 return;
1197         
1198         /* check if we need to set this Action as the AnimData's action */
1199         if (adt->action == NULL) {
1200                 /* set this Action as AnimData's Action */
1201                 printf("act_to_adt - set adt action to act \n");
1202                 adt->action= act;
1203         }
1204         
1205         /* convert Action data */
1206         action_to_animato(act, &adt->action->curves, &adt->drivers);
1207 }
1208
1209 /* *************************************************** */
1210 /* External API - Only Called from do_versions() */
1211
1212 /* Called from do_versions() in readfile.c to convert the old 'IPO/adrcode' system
1213  * to the new 'Animato/RNA' system.
1214  *
1215  * The basic method used here, is to loop over datablocks which have IPO-data, and 
1216  * add those IPO's to new AnimData blocks as Actions. 
1217  * Action/NLA data only works well for Objects, so these only need to be checked for there.
1218  *  
1219  * Data that has been converted should be freed immediately, which means that it is immediately
1220  * clear which datablocks have yet to be converted, and also prevent freeing errors when we exit.
1221  */
1222 // XXX currently done after all file reading... 
1223 void do_versions_ipos_to_animato(Main *main)
1224 {
1225         ListBase drivers = {NULL, NULL};
1226         ID *id;
1227         AnimData *adt;
1228         
1229         if (main == NULL) {
1230                 printf("Argh! Main is NULL in do_versions_ipos_to_animato() \n");
1231                 return;
1232         }
1233                 
1234         /* only convert if version is right */
1235         // XXX???
1236         if (main->versionfile >= 250) {
1237                 printf("WARNING: Animation data too new to convert (Version %d) \n", main->versionfile);
1238                 return;
1239         }
1240         else
1241                 printf("INFO: Converting to Animato... \n"); // xxx debug
1242                 
1243         /* ----------- Animation Attached to Data -------------- */
1244         
1245         /* objects */
1246         for (id= main->object.first; id; id= id->next) {
1247                 Object *ob= (Object *)id;
1248                 bPoseChannel *pchan;
1249                 bConstraint *con;
1250                 bConstraintChannel *conchan, *conchann;
1251                 
1252                 printf("\tconverting ob %s \n", id->name+2);
1253                 
1254                 /* check if object has any animation data */
1255                 if ((ob->ipo) || (ob->action) || (ob->nlastrips.first)) {
1256                         /* Add AnimData block */
1257                         adt= BKE_id_add_animdata(id);
1258                         
1259                         /* IPO first */
1260                         if (ob->ipo) {
1261                                 ipo_to_animdata(id, ob->ipo, NULL, NULL);
1262                                 ob->ipo->id.us--;
1263                                 ob->ipo= NULL;
1264                         }
1265                         
1266                         /* now Action */
1267                         if (ob->action) {
1268                                 action_to_animdata(id, ob->action);
1269                                 
1270                                 /* only decrease usercount if this Action isn't now being used by AnimData */
1271                                 if (ob->action != adt->action) {
1272                                         ob->action->id.us--;
1273                                         ob->action= NULL;
1274                                 }
1275                         }
1276                         
1277                         /* finally NLA */
1278                         // XXX todo... for now, new NLA code not hooked up yet, so keep old stuff (but not for too long!)
1279                 }
1280                 
1281                 /* check PoseChannels for constraints with local data */
1282                 if (ob->pose) {
1283                         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1284                                 for (con= pchan->constraints.first; con; con= con->next) {
1285                                         /* if constraint has own IPO, convert add these to Object 
1286                                          * (NOTE: they're most likely to be drivers too) 
1287                                          */
1288                                         if (con->ipo) {
1289                                                 /* although this was the constraint's local IPO, we still need to provide pchan + con 
1290                                                  * so that drivers can be added properly...
1291                                                  */
1292                                                 ipo_to_animdata(id, con->ipo, pchan->name, con->name);
1293                                                 con->ipo->id.us--;
1294                                                 con->ipo= NULL;
1295                                         }
1296                                 }
1297                         }
1298                 }
1299                 
1300                 /* check constraints for local IPO's */
1301                 for (con= ob->constraints.first; con; con= con->next) {
1302                         /* if constraint has own IPO, convert add these to Object 
1303                          * (NOTE: they're most likely to be drivers too) 
1304                          */
1305                         if (con->ipo) {
1306                                 /* although this was the constraint's local IPO, we still need to provide con 
1307                                  * so that drivers can be added properly...
1308                                  */
1309                                 ipo_to_animdata(id, con->ipo, NULL, con->name);
1310                                 con->ipo->id.us--;
1311                                 con->ipo= NULL;
1312                         }
1313                          
1314                         /* check for Action Constraint */
1315                         // XXX do we really want to do this here?
1316                 }
1317                 
1318                 /* check constraint channels - we need to remove them anyway... */
1319                 for (conchan= ob->constraintChannels.first; conchan; conchan= conchann) {
1320                         /* get pointer to next Constraint Channel */
1321                         conchann= conchan->next;
1322                         
1323                         /* convert Constraint Channel's IPO data */
1324                         if (conchan->ipo) {
1325                                 ipo_to_animdata(id, conchan->ipo, NULL, conchan->name);
1326                                 conchan->ipo->id.us--;
1327                                 conchan->ipo= NULL;
1328                         }
1329                         
1330                         /* free Constraint Channel */
1331                         BLI_freelinkN(&ob->constraintChannels, conchan);
1332                 }
1333         }
1334         
1335         /* shapekeys */
1336         for (id= main->key.first; id; id= id->next) {
1337                 Key *key= (Key *)id;
1338                 
1339                 printf("\tconverting key %s \n", id->name+2);
1340                 
1341                 /* we're only interested in the IPO 
1342                  * NOTE: for later, it might be good to port these over to Object instead, as many of these
1343                  * are likely to be drivers, but it's hard to trace that from here, so move this to Ob loop?
1344                  */
1345                 if (key->ipo) {
1346                         /* Add AnimData block */
1347                         adt= BKE_id_add_animdata(id);
1348                         
1349                         /* Convert Shapekey data... */
1350                         ipo_to_animdata(id, key->ipo, NULL, NULL);
1351                         key->ipo->id.us--;
1352                         key->ipo= NULL;
1353                 }
1354         }
1355         
1356         /* materials */
1357         for (id= main->mat.first; id; id= id->next) {
1358                 Material *ma= (Material *)id;
1359                 
1360                 printf("\tconverting material %s \n", id->name+2);
1361                 
1362                 /* we're only interested in the IPO */
1363                 if (ma->ipo) {
1364                         /* Add AnimData block */
1365                         adt= BKE_id_add_animdata(id);
1366                         
1367                         /* Convert Material data... */
1368                         ipo_to_animdata(id, ma->ipo, NULL, NULL);
1369                         ma->ipo->id.us--;
1370                         ma->ipo= NULL;
1371                 }
1372         }
1373         
1374         /* textures */
1375         for (id= main->tex.first; id; id= id->next) {
1376                 Tex *te= (Tex *)id;
1377                 
1378                 printf("\tconverting texture %s \n", id->name+2);
1379                 
1380                 /* we're only interested in the IPO */
1381                 if (te->ipo) {
1382                         /* Add AnimData block */
1383                         adt= BKE_id_add_animdata(id);
1384                         
1385                         /* Convert Texture data... */
1386                         ipo_to_animdata(id, te->ipo, NULL, NULL);
1387                         te->ipo->id.us--;
1388                         te->ipo= NULL;
1389                 }
1390         }
1391         
1392         /* cameras */
1393         for (id= main->camera.first; id; id= id->next) {
1394                 Camera *ca= (Camera *)id;
1395                 
1396                 printf("\tconverting camera %s \n", id->name+2);
1397                 
1398                 /* we're only interested in the IPO */
1399                 if (ca->ipo) {
1400                         /* Add AnimData block */
1401                         adt= BKE_id_add_animdata(id);
1402                         
1403                         /* Convert Camera data... */
1404                         ipo_to_animdata(id, ca->ipo, NULL, NULL);
1405                         ca->ipo->id.us--;
1406                         ca->ipo= NULL;
1407                 }
1408         }
1409         
1410         /* lamps */
1411         for (id= main->lamp.first; id; id= id->next) {
1412                 Lamp *la= (Lamp *)id;
1413                 
1414                 printf("\tconverting lamp %s \n", id->name+2);
1415                 
1416                 /* we're only interested in the IPO */
1417                 if (la->ipo) {
1418                         /* Add AnimData block */
1419                         adt= BKE_id_add_animdata(id);
1420                         
1421                         /* Convert Lamp data... */
1422                         ipo_to_animdata(id, la->ipo, NULL, NULL);
1423                         la->ipo->id.us--;
1424                         la->ipo= NULL;
1425                 }
1426         }
1427         
1428         /* --------- Unconverted Animation Data ------------------ */
1429         /* For Animation data which may not be directly connected (i.e. not linked) to any other 
1430          * data, we need to perform a separate pass to make sure that they are converted to standalone
1431          * Actions which may then be able to be reused. This does mean that we will be going over data that's
1432          * already been converted, but there are no problems with that.
1433          *
1434          * The most common case for this will be Action Constraints, or IPO's with Fake-Users. 
1435          * We collect all drivers that were found into a temporary collection, and free them in one go, as they're 
1436          * impossible to resolve.
1437          */
1438         
1439         /* actions */
1440         for (id= main->action.first; id; id= id->next) {
1441                 bAction *act= (bAction *)id;
1442                 
1443                 printf("\tconverting action %s \n", id->name+2);
1444                 
1445                 /* be careful! some of the actions we encounter will be converted ones... */
1446                 action_to_animato(act, &act->curves, &drivers);
1447         }
1448         
1449         /* ipo's */
1450         for (id= main->ipo.first; id; id= id->next) {
1451                 Ipo *ipo= (Ipo *)id;
1452                 
1453                 printf("\tconverting ipo %s \n", id->name+2);
1454                 
1455                 /* most likely this IPO has already been processed, so check if any curves left to convert */
1456                 if (ipo->curve.first) {
1457                         bAction *new_act;
1458                         
1459                         /* add a new action for this, and convert all data into that action */
1460                         new_act= add_empty_action("ConvIPO_Action"); // XXX need a better name...
1461                         ipo_to_animato(ipo, NULL, NULL, &new_act->curves, &drivers);
1462                 }
1463                 
1464                 /* clear fake-users, and set user-count to zero to make sure it is cleared on file-save */
1465                 ipo->id.us= 0;
1466                 ipo->id.flag &= ~LIB_FAKEUSER;
1467         }
1468         
1469         /* free unused drivers from actions + ipos */
1470         free_fcurves(&drivers);
1471         
1472         printf("INFO: animato convert done \n"); // xxx debug
1473 }
1474
1475
1476
1477 #if 0 // XXX old animation system
1478
1479 /* ***************************** IPO - DataAPI ********************************* */
1480
1481 // !!!!!!!!!!!!!!!!!!!!!!!!!!!! FIXME - BAD CRUFT WARNING !!!!!!!!!!!!!!!!!!!!!!!
1482
1483 /* These functions here should be replaced eventually by the Data API, as this is 
1484  * inflexible duplication...
1485  */
1486
1487 /* --------------------- Get Pointer API ----------------------------- */ 
1488
1489 /* get texture-slot/mapping channel */
1490 void *give_mtex_poin (MTex *mtex, int adrcode)
1491 {
1492         void *poin= NULL;
1493         
1494         switch (adrcode) {
1495         case MAP_OFS_X:
1496                 poin= &(mtex->ofs[0]); break;
1497         case MAP_OFS_Y:
1498                 poin= &(mtex->ofs[1]); break;
1499         case MAP_OFS_Z:
1500                 poin= &(mtex->ofs[2]); break;
1501         case MAP_SIZE_X:
1502                 poin= &(mtex->size[0]); break;
1503         case MAP_SIZE_Y:
1504                 poin= &(mtex->size[1]); break;
1505         case MAP_SIZE_Z:
1506                 poin= &(mtex->size[2]); break;
1507         case MAP_R:
1508                 poin= &(mtex->r); break;
1509         case MAP_G:
1510                 poin= &(mtex->g); break;
1511         case MAP_B:
1512                 poin= &(mtex->b); break;
1513         case MAP_DVAR:
1514                 poin= &(mtex->def_var); break;
1515         case MAP_COLF:
1516                 poin= &(mtex->colfac); break;
1517         case MAP_NORF:
1518                 poin= &(mtex->norfac); break;
1519         case MAP_VARF:
1520                 poin= &(mtex->varfac); break;
1521         case MAP_DISP:
1522                 poin= &(mtex->dispfac); break;
1523         }
1524         
1525         /* return pointer */
1526         return poin;
1527 }
1528
1529 /* GS reads the memory pointed at in a specific ordering. There are,
1530  * however two definitions for it. I have jotted them down here, both,
1531  * but I think the first one is actually used. The thing is that
1532  * big-endian systems might read this the wrong way round. OTOH, we
1533  * constructed the IDs that are read out with this macro explicitly as
1534  * well. I expect we'll sort it out soon... */
1535
1536 /* from blendef: */
1537 #define GS(a)   (*((short *)(a)))
1538
1539 /* from misc_util: flip the bytes from x  */
1540 /*  #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
1541
1542
1543 /* general function to get pointer to source/destination data  */
1544 void *get_ipo_poin (ID *id, IpoCurve *icu, int *type)
1545 {
1546         void *poin= NULL;
1547         MTex *mtex= NULL;
1548
1549         /* most channels will have float data, but those with other types will override this */
1550         *type= IPO_FLOAT;
1551
1552         /* data is divided into 'blocktypes' based on ID-codes */
1553         switch (GS(id->name)) {
1554                 case ID_WO: /* world channels -----------------------------  */
1555                 {
1556                         World *wo= (World *)id;
1557                         
1558                         switch (icu->adrcode) {
1559                         case WO_HOR_R:
1560                                 poin= &(wo->horr); break;
1561                         case WO_HOR_G:
1562                                 poin= &(wo->horg); break;
1563                         case WO_HOR_B:
1564                                 poin= &(wo->horb); break;
1565                         case WO_ZEN_R:
1566                                 poin= &(wo->zenr); break;
1567                         case WO_ZEN_G:
1568                                 poin= &(wo->zeng); break;
1569                         case WO_ZEN_B:
1570                                 poin= &(wo->zenb); break;
1571                         
1572                         case WO_EXPOS:
1573                                 poin= &(wo->exposure); break;
1574                         
1575                         case WO_MISI:
1576                                 poin= &(wo->misi); break;
1577                         case WO_MISTDI:
1578                                 poin= &(wo->mistdist); break;
1579                         case WO_MISTSTA:
1580                                 poin= &(wo->miststa); break;
1581                         case WO_MISTHI:
1582                                 poin= &(wo->misthi); break;
1583                         
1584                         case WO_STAR_R:
1585                                 poin= &(wo->starr); break;
1586                         case WO_STAR_G:
1587                                 poin= &(wo->starg); break;
1588                         case WO_STAR_B:
1589                                 poin= &(wo->starb); break;
1590                         
1591                         case WO_STARDIST:
1592                                 poin= &(wo->stardist); break;
1593                         case WO_STARSIZE:
1594                                 poin= &(wo->starsize); break;
1595                         }
1596                         
1597                         if (poin == NULL) {
1598                                 if (icu->adrcode & MA_MAP1) mtex= wo->mtex[0];
1599                                 else if (icu->adrcode & MA_MAP2) mtex= wo->mtex[1];
1600                                 else if (icu->adrcode & MA_MAP3) mtex= wo->mtex[2];
1601                                 else if (icu->adrcode & MA_MAP4) mtex= wo->mtex[3];
1602                                 else if (icu->adrcode & MA_MAP5) mtex= wo->mtex[4];
1603                                 else if (icu->adrcode & MA_MAP6) mtex= wo->mtex[5];
1604                                 else if (icu->adrcode & MA_MAP7) mtex= wo->mtex[6];
1605                                 else if (icu->adrcode & MA_MAP8) mtex= wo->mtex[7];
1606                                 else if (icu->adrcode & MA_MAP9) mtex= wo->mtex[8];
1607                                 else if (icu->adrcode & MA_MAP10) mtex= wo->mtex[9];
1608                                 else if (icu->adrcode & MA_MAP11) mtex= wo->mtex[10];
1609                                 else if (icu->adrcode & MA_MAP12) mtex= wo->mtex[11];
1610                                 else if (icu->adrcode & MA_MAP13) mtex= wo->mtex[12];
1611                                 else if (icu->adrcode & MA_MAP14) mtex= wo->mtex[13];
1612                                 else if (icu->adrcode & MA_MAP15) mtex= wo->mtex[14];
1613                                 else if (icu->adrcode & MA_MAP16) mtex= wo->mtex[15];
1614                                 else if (icu->adrcode & MA_MAP17) mtex= wo->mtex[16];
1615                                 else if (icu->adrcode & MA_MAP18) mtex= wo->mtex[17];
1616                                 
1617                                 if (mtex)
1618                                         poin= give_mtex_poin(mtex, (icu->adrcode & (MA_MAP1-1)));
1619                         }
1620                 }
1621                         break;
1622                 case ID_SO: /* sound channels -----------------------------  */
1623                 {
1624                         bSound *snd= (bSound *)id;
1625                         
1626                         switch (icu->adrcode) {
1627                         case SND_VOLUME:
1628                                 poin= &(snd->volume); break;
1629                         case SND_PITCH:
1630                                 poin= &(snd->pitch); break;
1631                         case SND_PANNING:
1632                                 poin= &(snd->panning); break;
1633                         case SND_ATTEN:
1634                                 poin= &(snd->attenuation); break;
1635                         }
1636                 }
1637                         break;
1638                 case ID_PA: /* particle channels -----------------------------  */
1639                 {
1640                         ParticleSettings *part= (ParticleSettings *)id;
1641                         
1642                         switch (icu->adrcode) {
1643                         case PART_EMIT_FREQ:
1644                         case PART_EMIT_LIFE:
1645                         case PART_EMIT_VEL:
1646                         case PART_EMIT_AVE:
1647                         case PART_EMIT_SIZE:
1648                                 poin= NULL; 
1649                                 break;
1650                         
1651                         case PART_CLUMP:
1652                                 poin= &(part->clumpfac); break;
1653                         case PART_AVE:
1654                                 poin= &(part->avefac); break;
1655                         case PART_SIZE:
1656                                 poin= &(part->size); break;
1657                         case PART_DRAG:
1658                                 poin= &(part->dragfac); break;
1659                         case PART_BROWN:
1660                                 poin= &(part->brownfac); break;
1661                         case PART_DAMP:
1662                                 poin= &(part->dampfac); break;
1663                         case PART_LENGTH:
1664                                 poin= &(part->length); break;
1665                         case PART_GRAV_X:
1666                                 poin= &(part->acc[0]); break;
1667                         case PART_GRAV_Y:
1668                                 poin= &(part->acc[1]); break;
1669                         case PART_GRAV_Z:
1670                                 poin= &(part->acc[2]); break;
1671                         case PART_KINK_AMP:
1672                                 poin= &(part->kink_amp); break;
1673                         case PART_KINK_FREQ:
1674                                 poin= &(part->kink_freq); break;
1675                         case PART_KINK_SHAPE:
1676                                 poin= &(part->kink_shape); break;
1677                         case PART_BB_TILT:
1678                                 poin= &(part->bb_tilt); break;
1679                                 
1680                         case PART_PD_FSTR:
1681                                 if (part->pd) poin= &(part->pd->f_strength);
1682                                 break;
1683                         case PART_PD_FFALL:
1684                                 if (part->pd) poin= &(part->pd->f_power);
1685                                 break;
1686                         case PART_PD_FMAXD:
1687                                 if (part->pd) poin= &(part->pd->maxdist);
1688                                 break;
1689                         case PART_PD2_FSTR:
1690                                 if (part->pd2) poin= &(part->pd2->f_strength);
1691                                 break;
1692                         case PART_PD2_FFALL:
1693                                 if (part->pd2) poin= &(part->pd2->f_power);
1694                                 break;
1695                         case PART_PD2_FMAXD:
1696                                 if (part->pd2) poin= &(part->pd2->maxdist);
1697                                 break;
1698                         }
1699                 }
1700                         break;
1701         }
1702
1703         /* return pointer */
1704         return poin;
1705 }
1706
1707
1708 #endif // XXX old animation system