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