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