2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): 2008,2009 Joshua Leung (IPO System cleanup, Animation System Recode)
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/blenkernel/intern/ipo.c
35 * This file is no longer used to provide tools for the deprecated IPO system. Instead, it
36 * is only used to house the conversion code to the new system.
38 * -- Joshua Leung, Jan 2009
46 /* since we have versioning code here */
47 #define DNA_DEPRECATED_ALLOW
49 #include "DNA_actuator_types.h"
50 #include "DNA_anim_types.h"
51 #include "DNA_constraint_types.h"
52 #include "DNA_camera_types.h"
53 #include "DNA_lamp_types.h"
54 #include "DNA_ipo_types.h"
55 #include "DNA_key_types.h"
56 #include "DNA_material_types.h"
57 #include "DNA_nla_types.h"
58 #include "DNA_sequence_types.h"
59 #include "DNA_scene_types.h"
60 #include "DNA_world_types.h"
61 #include "DNA_object_types.h"
63 #include "BLI_blenlib.h"
64 #include "BLI_dynstr.h"
65 #include "BLI_string_utils.h"
66 #include "BLI_utildefines.h"
68 #include "BLT_translation.h"
71 #include "BKE_animsys.h"
72 #include "BKE_action.h"
73 #include "BKE_fcurve.h"
74 #include "BKE_global.h"
76 #include "BKE_library.h"
79 #include "BKE_sequencer.h"
81 #include "MEM_guardedalloc.h"
84 # include "BLI_math_base.h" /* M_PI */
87 /* *************************************************** */
88 /* Old-Data Freeing Tools */
90 /* Free data from old IPO-Blocks (those which haven't been converted), but not IPO block itself */
91 // XXX this shouldn't be necessary anymore, but may occur while not all data is converted yet
92 void BKE_ipo_free(Ipo *ipo)
97 for (icu = ipo->curve.first; icu; icu = icn) {
101 if (icu->bezt) MEM_freeN(icu->bezt);
102 if (icu->bp) MEM_freeN(icu->bp);
103 if (icu->driver) MEM_freeN(icu->driver);
105 BLI_freelinkN(&ipo->curve, icu);
108 if (G.debug & G_DEBUG)
109 printf("Freed %d (Unconverted) Ipo-Curves from IPO '%s'\n", n, ipo->id.name + 2);
112 /* *************************************************** */
113 /* ADRCODE to RNA-Path Conversion Code - Special (Bitflags) */
115 /* Mapping Table for bitflag <-> RNA path */
116 typedef struct AdrBit2Path {
122 /* ----------------- */
123 /* Mapping Tables to use bits <-> RNA paths */
126 static AdrBit2Path ob_layer_bits[] = {
127 {(1 << 0), "layers", 0},
128 {(1 << 1), "layers", 1},
129 {(1 << 2), "layers", 2},
130 {(1 << 3), "layers", 3},
131 {(1 << 4), "layers", 4},
132 {(1 << 5), "layers", 5},
133 {(1 << 6), "layers", 6},
134 {(1 << 7), "layers", 7},
135 {(1 << 8), "layers", 8},
136 {(1 << 9), "layers", 9},
137 {(1 << 10), "layers", 10},
138 {(1 << 11), "layers", 11},
139 {(1 << 12), "layers", 12},
140 {(1 << 13), "layers", 13},
141 {(1 << 14), "layers", 14},
142 {(1 << 15), "layers", 15},
143 {(1 << 16), "layers", 16},
144 {(1 << 17), "layers", 17},
145 {(1 << 18), "layers", 18},
146 {(1 << 19), "layers", 19}
150 static AdrBit2Path ma_mode_bits[] = {
151 // {MA_TRACEBLE, "traceable", 0},
152 // {MA_SHADOW, "shadow", 0},
153 // {MA_SHLESS, "shadeless", 0},
155 {MA_RAYTRANSP, "transparency", 0},
156 {MA_RAYMIRROR, "raytrace_mirror.enabled", 0},
157 // {MA_HALO, "type", MA_TYPE_HALO}
160 /* ----------------- */
162 /* quick macro for returning the appropriate array for adrcode_bitmaps_to_paths() */
163 #define RET_ABP(items) \
165 *tot = sizeof(items) / sizeof(AdrBit2Path); \
169 /* This function checks if a Blocktype+Adrcode combo, returning a mapping table */
170 static AdrBit2Path *adrcode_bitmaps_to_paths(int blocktype, int adrcode, int *tot)
173 if ((blocktype == ID_OB) && (adrcode == OB_LAY)) {
174 RET_ABP(ob_layer_bits);
176 else if ((blocktype == ID_MA) && (adrcode == MA_MODE)) {
177 RET_ABP(ma_mode_bits);
179 // XXX TODO: add other types...
186 /* *************************************************** */
187 /* ADRCODE to RNA-Path Conversion Code - Standard */
190 static const char *ob_adrcodes_to_paths(int adrcode, int *array_index)
192 /* set array index like this in-case nothing sets it correctly */
195 /* result depends on adrcode */
198 *array_index = 0; return "location";
200 *array_index = 1; return "location";
202 *array_index = 2; return "location";
204 *array_index = 0; return "delta_location";
206 *array_index = 1; return "delta_location";
208 *array_index = 2; return "delta_location";
211 *array_index = 0; return "rotation_euler";
213 *array_index = 1; return "rotation_euler";
215 *array_index = 2; return "rotation_euler";
217 *array_index = 0; return "delta_rotation_euler";
219 *array_index = 1; return "delta_rotation_euler";
221 *array_index = 2; return "delta_rotation_euler";
224 *array_index = 0; return "scale";
226 *array_index = 1; return "scale";
228 *array_index = 2; return "scale";
230 *array_index = 0; return "delta_scale";
232 *array_index = 1; return "delta_scale";
234 *array_index = 2; return "delta_scale";
236 *array_index = 0; return "color";
238 *array_index = 1; return "color";
240 *array_index = 2; return "color";
242 *array_index = 3; return "color";
245 if (ob->pd) poin = &(ob->pd->f_strength);
248 if (ob->pd) poin = &(ob->pd->f_power);
251 if (ob->pd) poin = &(ob->pd->pdef_damp);
254 if (ob->pd) poin = &(ob->pd->pdef_rdamp);
257 if (ob->pd) poin = &(ob->pd->pdef_perm);
260 if (ob->pd) poin = &(ob->pd->maxdist);
269 * NOTE: pchan name comes from 'actname' added earlier...
271 static const char *pchan_adrcodes_to_paths(int adrcode, int *array_index)
273 /* set array index like this in-case nothing sets it correctly */
276 /* result depends on adrcode */
279 *array_index = 0; return "rotation_quaternion";
281 *array_index = 1; return "rotation_quaternion";
283 *array_index = 2; return "rotation_quaternion";
285 *array_index = 3; return "rotation_quaternion";
288 *array_index = 0; return "rotation_euler";
290 *array_index = 1; return "rotation_euler";
292 *array_index = 2; return "rotation_euler";
295 *array_index = 0; return "location";
297 *array_index = 1; return "location";
299 *array_index = 2; return "location";
302 *array_index = 0; return "scale";
304 *array_index = 1; return "scale";
306 *array_index = 2; return "scale";
309 /* for debugging only */
310 printf("ERROR: unmatched PoseChannel setting (code %d)\n", adrcode);
314 /* Constraint types */
315 static const char *constraint_adrcodes_to_paths(int adrcode, int *array_index)
317 /* set array index like this in-case nothing sets it correctly */
320 /* result depends on adrcode */
324 case CO_HEADTAIL: // XXX this needs to be wrapped in RNA.. probably then this path will be invalid
325 return "data.head_tail";
332 * NOTE: as we don't have access to the keyblock where the data comes from (for now),
333 * we'll just use numerical indices for now...
335 static char *shapekey_adrcodes_to_paths(ID *id, int adrcode, int *UNUSED(array_index))
337 static char buf[128];
339 /* block will be attached to ID_KE block... */
341 /* adrcode=0 was the misnamed "speed" curve (now "evaluation time") */
342 BLI_strncpy(buf, "eval_time", sizeof(buf));
345 /* Find the name of the ShapeKey (i.e. KeyBlock) to look for */
346 Key *key = (Key *)id;
347 KeyBlock *kb = BKE_keyblock_from_key(key, adrcode);
349 /* setting that we alter is the "value" (i.e. keyblock.curval) */
351 /* Use the keyblock name, escaped, so that path lookups for this will work */
352 BLI_snprintf(buf, sizeof(buf), "key_blocks[\"%s\"].value", kb->name);
355 /* Fallback - Use the adrcode as index directly, so that this can be manually fixed */
356 BLI_snprintf(buf, sizeof(buf), "key_blocks[%d].value", adrcode);
362 /* MTex (Texture Slot) types */
363 static const char *mtex_adrcodes_to_paths(int adrcode, int *UNUSED(array_index))
365 const char *base = NULL, *prop = NULL;
366 static char buf[128];
368 /* base part of path */
369 if (adrcode & MA_MAP1) base = "textures[0]";
370 else if (adrcode & MA_MAP2) base = "textures[1]";
371 else if (adrcode & MA_MAP3) base = "textures[2]";
372 else if (adrcode & MA_MAP4) base = "textures[3]";
373 else if (adrcode & MA_MAP5) base = "textures[4]";
374 else if (adrcode & MA_MAP6) base = "textures[5]";
375 else if (adrcode & MA_MAP7) base = "textures[6]";
376 else if (adrcode & MA_MAP8) base = "textures[7]";
377 else if (adrcode & MA_MAP9) base = "textures[8]";
378 else if (adrcode & MA_MAP10) base = "textures[9]";
379 else if (adrcode & MA_MAP11) base = "textures[10]";
380 else if (adrcode & MA_MAP12) base = "textures[11]";
381 else if (adrcode & MA_MAP13) base = "textures[12]";
382 else if (adrcode & MA_MAP14) base = "textures[13]";
383 else if (adrcode & MA_MAP15) base = "textures[14]";
384 else if (adrcode & MA_MAP16) base = "textures[15]";
385 else if (adrcode & MA_MAP17) base = "textures[16]";
386 else if (adrcode & MA_MAP18) base = "textures[17]";
388 /* property identifier for path */
389 adrcode = (adrcode & (MA_MAP1 - 1));
391 #if 0 // XXX these are not wrapped in RNA yet!
393 poin = &(mtex->ofs[0]); break;
395 poin = &(mtex->ofs[1]); break;
397 poin = &(mtex->ofs[2]); break;
399 poin = &(mtex->size[0]); break;
401 poin = &(mtex->size[1]); break;
403 poin = &(mtex->size[2]); break;
405 poin = &(mtex->r); break;
407 poin = &(mtex->g); break;
409 poin = &(mtex->b); break;
411 poin = &(mtex->def_var); break;
413 poin = &(mtex->colfac); break;
415 poin = &(mtex->norfac); break;
417 poin = &(mtex->varfac); break;
420 prop = "warp_factor"; break;
423 /* only build and return path if there's a property */
425 BLI_snprintf(buf, 128, "%s.%s", base, prop);
433 static const char *texture_adrcodes_to_paths(int adrcode, int *array_index)
435 /* set array index like this in-case nothing sets it correctly */
438 /* result depends on adrcode */
445 case TE_NDEPTH: // XXX texture RNA undefined
446 //poin= &(tex->noisedepth); *type= IPO_SHORT; break;
448 case TE_NTYPE: // XXX texture RNA undefined
449 //poin= &(tex->noisetype); *type= IPO_SHORT; break;
453 return "noise_basis";
455 return "noise_basis"; // XXX this is not yet defined in RNA...
459 *array_index = 0; return "feature_weights";
461 *array_index = 1; return "feature_weights";
463 *array_index = 2; return "feature_weights";
465 *array_index = 3; return "feature_weights";
467 return "minkovsky_exponent";
469 return "distance_metric";
473 /* distorted noise / voronoi */
475 return "noise_intensity";
477 /* distorted noise */
479 return "distortion_amount";
482 case TE_MG_TYP: // XXX texture RNA undefined
483 // poin= &(tex->stype); *type= IPO_SHORT; break;
486 return "highest_dimension";
497 *array_index = 0; return "rgb_factor";
499 *array_index = 1; return "rgb_factor";
501 *array_index = 2; return "rgb_factor";
513 static const char *material_adrcodes_to_paths(int adrcode, int *array_index)
515 /* set array index like this in-case nothing sets it correctly */
518 /* result depends on adrcode */
521 *array_index = 0; return "diffuse_color";
523 *array_index = 1; return "diffuse_color";
525 *array_index = 2; return "diffuse_color";
528 *array_index = 0; return "specular_color";
530 *array_index = 1; return "specular_color";
532 *array_index = 2; return "specular_color";
535 *array_index = 0; return "mirror_color";
537 *array_index = 1; return "mirror_color";
539 *array_index = 2; return "mirror_color";
545 return "diffuse_intensity";
554 return "specular_intensity";
557 return "specular_hardness";
560 return "specular_opacity";
569 return "translucency";
572 return "raytrace_mirror.reflect";
575 return "raytrace_mirror.fresnel";
578 return "raytrace_mirror.fresnel_factor";
581 return "raytrace_transparency.fresnel";
584 return "raytrace_transparency.fresnel_factor";
589 default: /* for now, we assume that the others were MTex channels */
590 return mtex_adrcodes_to_paths(adrcode, array_index);
597 static const char *camera_adrcodes_to_paths(int adrcode, int *array_index)
599 /* set array index like this in-case nothing sets it correctly */
602 /* result depends on adrcode */
605 #if 0 // XXX this cannot be resolved easily... perhaps we assume camera is perspective (works for most cases...
606 if (ca->type == CAM_ORTHO)
607 return "ortho_scale";
610 #else // XXX lazy hack for now...
612 #endif // XXX this cannot be resolved easily
619 #if 0 // XXX these are not defined in RNA
621 poin = &(ca->YF_aperture); break;
623 poin = &(ca->YF_dofdist); break;
624 #endif // XXX these are not defined in RNA
632 /* unrecognised adrcode, or not-yet-handled ones! */
637 static const char *lamp_adrcodes_to_paths(int adrcode, int *array_index)
639 /* set array index like this in-case nothing sets it correctly */
642 /* result depends on adrcode */
648 *array_index = 0; return "color";
650 *array_index = 1; return "color";
652 *array_index = 2; return "color";
663 return "linear_attenuation";
665 return "quadratic_attenuation";
668 return "halo_intensity";
670 default: /* for now, we assume that the others were MTex channels */
671 return mtex_adrcodes_to_paths(adrcode, array_index);
674 /* unrecognised adrcode, or not-yet-handled ones! */
679 static const char *sound_adrcodes_to_paths(int adrcode, int *array_index)
681 /* set array index like this in-case nothing sets it correctly */
684 /* result depends on adrcode */
690 /* XXX Joshua -- I had wrapped panning in rna, but someone commented out, calling it "unused" */
696 return "attenuation";
699 /* unrecognised adrcode, or not-yet-handled ones! */
704 static const char *world_adrcodes_to_paths(int adrcode, int *array_index)
706 /* set array index like this in-case nothing sets it correctly */
709 /* result depends on adrcode */
712 *array_index = 0; return "horizon_color";
714 *array_index = 1; return "horizon_color";
716 *array_index = 2; return "horizon_color";
718 *array_index = 0; return "zenith_color";
720 *array_index = 1; return "zenith_color";
722 *array_index = 2; return "zenith_color";
728 return "mist.intensity";
734 return "mist.height";
736 default: /* for now, we assume that the others were MTex channels */
737 return mtex_adrcodes_to_paths(adrcode, array_index);
744 static const char *particle_adrcodes_to_paths(int adrcode, int *array_index)
746 /* set array index like this in-case nothing sets it correctly */
749 /* result depends on adrcode */
752 return "settings.clump_factor";
754 return "settings.angular_velocity_factor";
756 return "settings.particle_size";
758 return "settings.drag_factor";
760 return "settings.brownian_factor";
762 return "settings.damp_factor";
764 return "settings.length";
766 *array_index = 0; return "settings.acceleration";
768 *array_index = 1; return "settings.acceleration";
770 *array_index = 2; return "settings.acceleration";
772 return "settings.kink_amplitude";
774 return "settings.kink_frequency";
775 case PART_KINK_SHAPE:
776 return "settings.kink_shape";
778 return "settings.billboard_tilt";
780 /* PartDeflect needs to be sorted out properly in rna_object_force;
781 * If anyone else works on this, but is unfamiliar, these particular
782 * settings reference the particles of the system themselves
783 * being used as forces -- it will use the same rna structure
784 * as the similar object forces */
787 if (part->pd) poin = &(part->pd->f_strength);
790 if (part->pd) poin = &(part->pd->f_power);
793 if (part->pd) poin = &(part->pd->maxdist);
796 if (part->pd2) poin = &(part->pd2->f_strength);
799 if (part->pd2) poin = &(part->pd2->f_power);
802 if (part->pd2) poin = &(part->pd2->maxdist);
813 /* Allocate memory for RNA-path for some property given a blocktype, adrcode, and 'root' parts of path
815 * - id - the datablock that the curve's IPO block is attached to and/or which the new paths will start from
816 * - blocktype, adrcode - determines setting to get
817 * - actname, constname,seq - used to build path
819 * - array_index - index in property's array (if applicable) to use
820 * - return - the allocated path...
822 static char *get_rna_access(ID *id, int blocktype, int adrcode, char actname[], char constname[], Sequence *seq, int *array_index)
824 DynStr *path = BLI_dynstr_new();
825 const char *propname = NULL;
830 /* hack: if constname is set, we can only be dealing with an Constraint curve */
834 /* get property name based on blocktype */
836 case ID_OB: /* object */
837 propname = ob_adrcodes_to_paths(adrcode, &dummy_index);
840 case ID_PO: /* pose channel */
841 propname = pchan_adrcodes_to_paths(adrcode, &dummy_index);
844 case ID_KE: /* shapekeys */
845 propname = shapekey_adrcodes_to_paths(id, adrcode, &dummy_index);
848 case ID_CO: /* constraint */
849 propname = constraint_adrcodes_to_paths(adrcode, &dummy_index);
852 case ID_TE: /* texture */
853 propname = texture_adrcodes_to_paths(adrcode, &dummy_index);
856 case ID_MA: /* material */
857 propname = material_adrcodes_to_paths(adrcode, &dummy_index);
860 case ID_CA: /* camera */
861 propname = camera_adrcodes_to_paths(adrcode, &dummy_index);
864 case ID_LA: /* lamp */
865 propname = lamp_adrcodes_to_paths(adrcode, &dummy_index);
868 case ID_SO: /* sound */
869 propname = sound_adrcodes_to_paths(adrcode, &dummy_index);
872 case ID_WO: /* world */
873 propname = world_adrcodes_to_paths(adrcode, &dummy_index);
876 case ID_PA: /* particle */
877 propname = particle_adrcodes_to_paths(adrcode, &dummy_index);
880 case ID_CU: /* curve */
881 /* this used to be a 'dummy' curve which got evaluated on the fly...
882 * now we've got real var for this!
884 propname = "eval_time";
887 /* XXX problematic blocktypes */
888 case ID_SEQ: /* sequencer strip */
892 propname = "effect_fader";
895 propname = "speed_fader";
897 case SEQ_FAC_OPACITY:
898 propname = "blend_opacity";
901 // poin= &(seq->facf0); // XXX this doesn't seem to be included anywhere in sequencer RNA...
906 /* special case for rotdiff drivers... we don't need a property for this... */
909 /* TODO... add other blocktypes... */
911 printf("IPO2ANIMATO WARNING: No path for blocktype %d, adrcode %d yet\n", blocktype, adrcode);
915 /* check if any property found
916 * - blocktype < 0 is special case for a specific type of driver, where we don't need a property name...
918 if ((propname == NULL) && (blocktype > 0)) {
919 /* nothing was found, so exit */
923 BLI_dynstr_free(path);
929 *array_index = dummy_index;
932 /* 'buf' _must_ be initialized in this block */
933 /* append preceding bits to path */
934 /* note, strings are not escapted and they should be! */
935 if ((actname && actname[0]) && (constname && constname[0])) {
936 /* Constraint in Pose-Channel */
937 BLI_snprintf(buf, sizeof(buf), "pose.bones[\"%s\"].constraints[\"%s\"]", actname, constname);
939 else if (actname && actname[0]) {
940 if ((blocktype == ID_OB) && STREQ(actname, "Object")) {
941 /* Actionified "Object" IPO's... no extra path stuff needed */
942 buf[0] = '\0'; /* empty string */
944 else if ((blocktype == ID_KE) && STREQ(actname, "Shape")) {
945 /* Actionified "Shape" IPO's - these are forced onto object level via the action container there... */
946 strcpy(buf, "data.shape_keys");
950 BLI_snprintf(buf, sizeof(buf), "pose.bones[\"%s\"]", actname);
953 else if (constname && constname[0]) {
954 /* Constraint in Object */
955 BLI_snprintf(buf, sizeof(buf), "constraints[\"%s\"]", constname);
958 /* Sequence names in Scene */
959 BLI_snprintf(buf, sizeof(buf), "sequence_editor.sequences_all[\"%s\"]", seq->name + 2);
962 buf[0] = '\0'; /* empty string */
965 BLI_dynstr_append(path, buf);
967 /* need to add dot before property if there was anything precceding this */
969 BLI_dynstr_append(path, ".");
971 /* now write name of property */
972 BLI_dynstr_append(path, propname);
974 /* if there was no array index pointer provided, add it to the path */
975 if (array_index == NULL) {
976 BLI_snprintf(buf, sizeof(buf), "[\"%d\"]", dummy_index);
977 BLI_dynstr_append(path, buf);
980 /* convert to normal MEM_malloc'd string */
981 rpath = BLI_dynstr_get_cstring(path);
982 BLI_dynstr_free(path);
988 /* *************************************************** */
989 /* Conversion Utilities */
991 /* Convert adrcodes to driver target transform channel types */
992 static short adrcode_to_dtar_transchan(short adrcode)
996 return DTAR_TRANSCHAN_LOCX;
998 return DTAR_TRANSCHAN_LOCY;
1000 return DTAR_TRANSCHAN_LOCZ;
1003 return DTAR_TRANSCHAN_ROTX;
1005 return DTAR_TRANSCHAN_ROTY;
1007 return DTAR_TRANSCHAN_ROTZ;
1010 return DTAR_TRANSCHAN_SCALEX;
1012 return DTAR_TRANSCHAN_SCALEX;
1014 return DTAR_TRANSCHAN_SCALEX;
1021 /* Convert IpoDriver to ChannelDriver - will free the old data (i.e. the old driver) */
1022 static ChannelDriver *idriver_to_cdriver(IpoDriver *idriver)
1024 ChannelDriver *cdriver;
1026 /* allocate memory for new driver */
1027 cdriver = MEM_callocN(sizeof(ChannelDriver), "ChannelDriver");
1029 /* if 'pydriver', just copy data across */
1030 if (idriver->type == IPO_DRIVER_TYPE_PYTHON) {
1031 /* PyDriver only requires the expression to be copied */
1032 // FIXME: expression will be useless due to API changes, but at least not totally lost
1033 cdriver->type = DRIVER_TYPE_PYTHON;
1034 if (idriver->name[0])
1035 BLI_strncpy(cdriver->expression, idriver->name, sizeof(cdriver->expression));
1038 DriverVar *dvar = NULL;
1039 DriverTarget *dtar = NULL;
1041 /* this should be ok for all types here... */
1042 cdriver->type = DRIVER_TYPE_AVERAGE;
1044 /* what to store depends on the 'blocktype' - object or posechannel */
1045 if (idriver->blocktype == ID_AR) { /* PoseChannel */
1046 if (idriver->adrcode == OB_ROT_DIFF) {
1047 /* Rotational Difference requires a special type of variable */
1048 dvar = driver_add_new_variable(cdriver);
1049 driver_change_variable_type(dvar, DVAR_TYPE_ROT_DIFF);
1051 /* first bone target */
1052 dtar = &dvar->targets[0];
1053 dtar->id = (ID *)idriver->ob;
1054 dtar->idtype = ID_OB;
1055 if (idriver->name[0])
1056 BLI_strncpy(dtar->pchan_name, idriver->name, sizeof(dtar->pchan_name));
1058 /* second bone target (name was stored in same var as the first one) */
1059 dtar = &dvar->targets[1];
1060 dtar->id = (ID *)idriver->ob;
1061 dtar->idtype = ID_OB;
1062 if (idriver->name[0]) // xxx... for safety
1063 BLI_strncpy(dtar->pchan_name, idriver->name + DRIVER_NAME_OFFS, sizeof(dtar->pchan_name));
1066 /* only a single variable, of type 'transform channel' */
1067 dvar = driver_add_new_variable(cdriver);
1068 driver_change_variable_type(dvar, DVAR_TYPE_TRANSFORM_CHAN);
1070 /* only requires a single target */
1071 dtar = &dvar->targets[0];
1072 dtar->id = (ID *)idriver->ob;
1073 dtar->idtype = ID_OB;
1074 if (idriver->name[0])
1075 BLI_strncpy(dtar->pchan_name, idriver->name, sizeof(dtar->pchan_name));
1076 dtar->transChan = adrcode_to_dtar_transchan(idriver->adrcode);
1077 dtar->flag |= DTAR_FLAG_LOCALSPACE; /* old drivers took local space */
1081 /* only a single variable, of type 'transform channel' */
1082 dvar = driver_add_new_variable(cdriver);
1083 driver_change_variable_type(dvar, DVAR_TYPE_TRANSFORM_CHAN);
1085 /* only requires single target */
1086 dtar = &dvar->targets[0];
1087 dtar->id = (ID *)idriver->ob;
1088 dtar->idtype = ID_OB;
1089 dtar->transChan = adrcode_to_dtar_transchan(idriver->adrcode);
1093 /* return the new one */
1097 /* Add F-Curve to the correct list
1098 * - grpname is needed to be used as group name where relevant, and is usually derived from actname
1100 static void fcurve_add_to_list(ListBase *groups, ListBase *list, FCurve *fcu, char *grpname, int muteipo)
1102 /* If we're adding to an action, we will have groups to write to... */
1103 if (groups && grpname) {
1104 /* wrap the pointers given into a dummy action that we pass to the API func
1105 * and extract the resultant lists...
1108 bActionGroup *agrp = NULL;
1110 /* init the temp action */
1111 memset(&tmp_act, 0, sizeof(bAction)); // XXX only enable this line if we get errors
1112 tmp_act.groups.first = groups->first;
1113 tmp_act.groups.last = groups->last;
1114 tmp_act.curves.first = list->first;
1115 tmp_act.curves.last = list->last;
1116 /* ... xxx, the other vars don't need to be filled in */
1118 /* get the group to use */
1119 agrp = BKE_action_group_find_name(&tmp_act, grpname);
1120 /* no matching group, so add one */
1122 /* Add a new group, and make it active */
1123 agrp = MEM_callocN(sizeof(bActionGroup), "bActionGroup");
1125 agrp->flag = AGRP_SELECTED;
1126 if (muteipo) agrp->flag |= AGRP_MUTED;
1128 BLI_strncpy(agrp->name, grpname, sizeof(agrp->name));
1130 BLI_addtail(&tmp_act.groups, agrp);
1131 BLI_uniquename(&tmp_act.groups, agrp, DATA_("Group"), '.', offsetof(bActionGroup, name),
1132 sizeof(agrp->name));
1135 /* add F-Curve to group */
1136 /* WARNING: this func should only need to look at the stuff we initialized, if not, things may crash */
1137 action_groups_add_channel(&tmp_act, agrp, fcu);
1139 if (agrp->flag & AGRP_MUTED) /* flush down */
1140 fcu->flag |= FCURVE_MUTED;
1142 /* set the output lists based on the ones in the temp action */
1143 groups->first = tmp_act.groups.first;
1144 groups->last = tmp_act.groups.last;
1145 list->first = tmp_act.curves.first;
1146 list->last = tmp_act.curves.last;
1149 /* simply add the F-Curve to the end of the given list */
1150 BLI_addtail(list, fcu);
1154 /* Convert IPO-Curve to F-Curve (including Driver data), and free any of the old data that
1155 * is not relevant, BUT do not free the IPO-Curve itself...
1156 * actname: name of Action-Channel (if applicable) that IPO-Curve's IPO-block belonged to
1157 * constname: name of Constraint-Channel (if applicable) that IPO-Curve's IPO-block belonged to
1158 * seq: sequencer-strip (if applicable) that IPO-Curve's IPO-block belonged to
1160 static void icu_to_fcurves(ID *id, ListBase *groups, ListBase *list, IpoCurve *icu, char *actname, char *constname, Sequence *seq, int muteipo)
1166 /* allocate memory for a new F-Curve */
1167 fcu = MEM_callocN(sizeof(FCurve), "FCurve");
1169 /* convert driver */
1171 fcu->driver = idriver_to_cdriver(icu->driver);
1174 if (icu->flag & IPO_VISIBLE) fcu->flag |= FCURVE_VISIBLE;
1175 if (icu->flag & IPO_SELECT) fcu->flag |= FCURVE_SELECTED;
1176 if (icu->flag & IPO_ACTIVE) fcu->flag |= FCURVE_ACTIVE;
1177 if (icu->flag & IPO_MUTE) fcu->flag |= FCURVE_MUTED;
1178 if (icu->flag & IPO_PROTECT) fcu->flag |= FCURVE_PROTECTED;
1180 /* set extrapolation */
1181 switch (icu->extrap) {
1182 case IPO_HORIZ: /* constant extrapolation */
1183 case IPO_DIR: /* linear extrapolation */
1185 /* just copy, as the new defines match the old ones... */
1186 fcu->extend = icu->extrap;
1189 case IPO_CYCL: /* cyclic extrapolation */
1190 case IPO_CYCLX: /* cyclic extrapolation + offset */
1192 /* Add a new FModifier (Cyclic) instead of setting extend value
1193 * as that's the new equivalent of that option.
1195 FModifier *fcm = add_fmodifier(&fcu->modifiers, FMODIFIER_TYPE_CYCLES, fcu);
1196 FMod_Cycles *data = (FMod_Cycles *)fcm->data;
1198 /* if 'offset' one is in use, set appropriate settings */
1199 if (icu->extrap == IPO_CYCLX)
1200 data->before_mode = data->after_mode = FCM_EXTRAPOLATE_CYCLIC_OFFSET;
1202 data->before_mode = data->after_mode = FCM_EXTRAPOLATE_CYCLIC;
1209 /* get adrcode <-> bitflags mapping to handle nasty bitflag curves? */
1210 abp = adrcode_bitmaps_to_paths(icu->blocktype, icu->adrcode, &totbits);
1211 if (abp && totbits) {
1215 if (G.debug & G_DEBUG) printf("\tconvert bitflag ipocurve, totbits = %d\n", totbits);
1217 /* add the 'only int values' flag */
1218 fcu->flag |= (FCURVE_INT_VALUES | FCURVE_DISCRETE_VALUES);
1220 /* for each bit we have to remap + check for:
1221 * 1) we need to make copy the existing F-Curve data (fcu -> fcurve),
1222 * except for the last one which will use the original
1223 * 2) copy the relevant path info across
1224 * 3) filter the keyframes for the flag of interest
1226 for (b = 0; b < totbits; b++, abp++) {
1229 /* make a copy of existing base-data if not the last curve */
1230 if (b < (totbits - 1))
1231 fcurve = copy_fcurve(fcu);
1236 fcurve->rna_path = BLI_strdup(abp->path);
1237 fcurve->array_index = abp->array_index;
1239 /* convert keyframes
1240 * - beztriples and bpoints are mutually exclusive, so we won't have both at the same time
1241 * - beztriples are more likely to be encountered as they are keyframes (the other type wasn't used yet)
1243 fcurve->totvert = icu->totvert;
1246 BezTriple *dst, *src;
1248 /* allocate new array for keyframes/beztriples */
1249 fcurve->bezt = MEM_callocN(sizeof(BezTriple) * fcurve->totvert, "BezTriples");
1251 /* loop through copying all BezTriples individually, as we need to modify a few things */
1252 for (dst = fcurve->bezt, src = icu->bezt, i = 0; i < fcurve->totvert; i++, dst++, src++) {
1253 /* firstly, copy BezTriple data */
1256 /* interpolation can only be constant... */
1257 dst->ipo = BEZT_IPO_CONST;
1259 /* 'hide' flag is now used for keytype - only 'keyframes' existed before */
1260 dst->hide = BEZT_KEYTYPE_KEYFRAME;
1262 /* auto-handles - per curve to per handle */
1263 if (icu->flag & IPO_AUTO_HORIZ) {
1264 if (dst->h1 == HD_AUTO) dst->h1 = HD_AUTO_ANIM;
1265 if (dst->h2 == HD_AUTO) dst->h2 = HD_AUTO_ANIM;
1268 /* correct values, by checking if the flag of interest is set */
1269 if ( ((int)(dst->vec[1][1])) & (abp->bit) )
1270 dst->vec[0][1] = dst->vec[1][1] = dst->vec[2][1] = 1.0f;
1272 dst->vec[0][1] = dst->vec[1][1] = dst->vec[2][1] = 0.0f;
1276 /* TODO: need to convert from BPoint type to the more compact FPoint type... but not priority, since no data used this */
1281 /* add new F-Curve to list */
1282 fcurve_add_to_list(groups, list, fcurve, actname, muteipo);
1289 * - we will need to set the 'disabled' flag if no path is able to be made (for now)
1291 fcu->rna_path = get_rna_access(id, icu->blocktype, icu->adrcode, actname, constname, seq, &fcu->array_index);
1292 if (fcu->rna_path == NULL)
1293 fcu->flag |= FCURVE_DISABLED;
1295 /* convert keyframes
1296 * - beztriples and bpoints are mutually exclusive, so we won't have both at the same time
1297 * - beztriples are more likely to be encountered as they are keyframes (the other type wasn't used yet)
1299 fcu->totvert = icu->totvert;
1302 BezTriple *dst, *src;
1304 /* allocate new array for keyframes/beztriples */
1305 fcu->bezt = MEM_callocN(sizeof(BezTriple) * fcu->totvert, "BezTriples");
1307 /* loop through copying all BezTriples individually, as we need to modify a few things */
1308 for (dst = fcu->bezt, src = icu->bezt, i = 0; i < fcu->totvert; i++, dst++, src++) {
1309 /* firstly, copy BezTriple data */
1312 /* now copy interpolation from curve (if not already set) */
1313 if (icu->ipo != IPO_MIXED)
1314 dst->ipo = icu->ipo;
1316 /* 'hide' flag is now used for keytype - only 'keyframes' existed before */
1317 dst->hide = BEZT_KEYTYPE_KEYFRAME;
1319 /* auto-handles - per curve to per handle */
1320 if (icu->flag & IPO_AUTO_HORIZ) {
1321 if (dst->h1 == HD_AUTO) dst->h1 = HD_AUTO_ANIM;
1322 if (dst->h2 == HD_AUTO) dst->h2 = HD_AUTO_ANIM;
1325 /* correct values for euler rotation curves
1326 * - they were degrees/10
1327 * - we need radians for RNA to do the right thing
1329 if ( ((icu->blocktype == ID_OB) && ELEM(icu->adrcode, OB_ROT_X, OB_ROT_Y, OB_ROT_Z)) ||
1330 ((icu->blocktype == ID_PO) && ELEM(icu->adrcode, AC_EUL_X, AC_EUL_Y, AC_EUL_Z)) )
1332 const float fac = (float)M_PI / 18.0f; //10.0f * M_PI/180.0f;
1334 dst->vec[0][1] *= fac;
1335 dst->vec[1][1] *= fac;
1336 dst->vec[2][1] *= fac;
1339 /* correct values for path speed curves
1340 * - their values were 0-1
1341 * - we now need as 'frames'
1343 if ( (id) && (icu->blocktype == GS(id->name)) &&
1344 (fcu->rna_path && STREQ(fcu->rna_path, "eval_time")) )
1346 Curve *cu = (Curve *)id;
1348 dst->vec[0][1] *= cu->pathlen;
1349 dst->vec[1][1] *= cu->pathlen;
1350 dst->vec[2][1] *= cu->pathlen;
1353 /* correct times for rotation drivers
1354 * - need to go from degrees to radians...
1355 * - there's only really 1 target to worry about
1356 * - were also degrees/10
1358 if (fcu->driver && fcu->driver->variables.first) {
1359 DriverVar *dvar = fcu->driver->variables.first;
1360 DriverTarget *dtar = &dvar->targets[0];
1362 if (ELEM(dtar->transChan, DTAR_TRANSCHAN_ROTX, DTAR_TRANSCHAN_ROTY, DTAR_TRANSCHAN_ROTZ)) {
1363 const float fac = (float)M_PI / 18.0f;
1365 dst->vec[0][0] *= fac;
1366 dst->vec[1][0] *= fac;
1367 dst->vec[2][0] *= fac;
1371 /* correct values for sequencer curves, that were not locked to frame */
1372 if (seq && (seq->flag & SEQ_IPO_FRAME_LOCKED) == 0) {
1373 const float mul = (seq->enddisp - seq->startdisp) / 100.0f;
1374 const float offset = seq->startdisp;
1376 dst->vec[0][0] *= mul;
1377 dst->vec[0][0] += offset;
1379 dst->vec[1][0] *= mul;
1380 dst->vec[1][0] += offset;
1382 dst->vec[2][0] *= mul;
1383 dst->vec[2][0] += offset;
1388 /* TODO: need to convert from BPoint type to the more compact FPoint type... but not priority, since no data used this */
1393 /* add new F-Curve to list */
1394 fcurve_add_to_list(groups, list, fcu, actname, muteipo);
1398 /* ------------------------- */
1400 /* Convert IPO-block (i.e. all its IpoCurves) to the new system.
1401 * This does not assume that any ID or AnimData uses it, but does assume that
1402 * it is given two lists, which it will perform driver/animation-data separation.
1404 static void ipo_to_animato(ID *id, Ipo *ipo, char actname[], char constname[], Sequence *seq, ListBase *animgroups, ListBase *anim, ListBase *drivers)
1409 if (ELEM(NULL, ipo, anim, drivers))
1412 if (G.debug & G_DEBUG) printf("ipo_to_animato\n");
1414 /* validate actname and constname
1415 * - clear actname if it was one of the generic <builtin> ones (i.e. 'Object', or 'Shapes')
1416 * - actname can then be used to assign F-Curves in Action to Action Groups
1417 * (i.e. thus keeping the benefits that used to be provided by Action Channels for grouping
1418 * F-Curves for bones). This may be added later... for now let's just dump without them...
1421 if ((ipo->blocktype == ID_OB) && STREQ(actname, "Object"))
1423 else if ((ipo->blocktype == ID_OB) && STREQ(actname, "Shape"))
1427 /* loop over IPO-Curves, freeing as we progress */
1428 for (icu = ipo->curve.first; icu; icu = icu->next) {
1429 /* Since an IPO-Curve may end up being made into many F-Curves (i.e. bitflag curves),
1430 * we figure out the best place to put the channel, then tell the curve-converter to just dump there
1433 /* Blender 2.4x allowed empty drivers, but we don't now, since they cause more trouble than they're worth */
1434 if ((icu->driver->ob) || (icu->driver->type == IPO_DRIVER_TYPE_PYTHON)) {
1435 icu_to_fcurves(id, NULL, drivers, icu, actname, constname, seq, ipo->muteipo);
1438 MEM_freeN(icu->driver);
1443 icu_to_fcurves(id, animgroups, anim, icu, actname, constname, seq, ipo->muteipo);
1446 /* if this IPO block doesn't have any users after this one, free... */
1447 id_us_min(&ipo->id);
1448 if (ID_REAL_USERS(ipo) <= 0) {
1451 for (icu = ipo->curve.first; icu; icu = icn) {
1456 MEM_freeN(icu->driver);
1458 /* free old data of curve now that it's no longer needed for converting any more curves */
1459 if (icu->bezt) MEM_freeN(icu->bezt);
1460 if (icu->bp) MEM_freeN(icu->bezt);
1462 /* free this IPO-Curve */
1463 BLI_freelinkN(&ipo->curve, icu);
1468 /* Convert Action-block to new system, separating animation and drivers
1469 * New curves may not be converted directly into the given Action (i.e. for Actions linked
1470 * to Objects, where ob->ipo and ob->action need to be combined).
1471 * NOTE: we need to be careful here, as same data-structs are used for new system too!
1473 static void action_to_animato(ID *id, bAction *act, ListBase *groups, ListBase *curves, ListBase *drivers)
1475 bActionChannel *achan, *achann;
1476 bConstraintChannel *conchan, *conchann;
1478 /* only continue if there are Action Channels (indicating unconverted data) */
1479 if (BLI_listbase_is_empty(&act->chanbase))
1482 /* get rid of all Action Groups */
1483 // XXX this is risky if there's some old + some new data in the Action...
1484 if (act->groups.first)
1485 BLI_freelistN(&act->groups);
1487 /* loop through Action-Channels, converting data, freeing as we go */
1488 for (achan = act->chanbase.first; achan; achan = achann) {
1489 /* get pointer to next Action Channel */
1490 achann = achan->next;
1492 /* convert Action Channel's IPO data */
1494 ipo_to_animato(id, achan->ipo, achan->name, NULL, NULL, groups, curves, drivers);
1495 id_us_min(&achan->ipo->id);
1499 /* convert constraint channel IPO-data */
1500 for (conchan = achan->constraintChannels.first; conchan; conchan = conchann) {
1501 /* get pointer to next Constraint Channel */
1502 conchann = conchan->next;
1504 /* convert Constraint Channel's IPO data */
1506 ipo_to_animato(id, conchan->ipo, achan->name, conchan->name, NULL, groups, curves, drivers);
1507 id_us_min(&conchan->ipo->id);
1508 conchan->ipo = NULL;
1511 /* free Constraint Channel */
1512 BLI_freelinkN(&achan->constraintChannels, conchan);
1515 /* free Action Channel */
1516 BLI_freelinkN(&act->chanbase, achan);
1521 /* ------------------------- */
1523 /* Convert IPO-block (i.e. all its IpoCurves) for some ID to the new system
1524 * This assumes that AnimData has been added already. Separation of drivers
1525 * from animation data is accomplished here too...
1527 static void ipo_to_animdata(ID *id, Ipo *ipo, char actname[], char constname[], Sequence *seq)
1529 AnimData *adt = BKE_animdata_from_id(id);
1530 ListBase anim = {NULL, NULL};
1531 ListBase drivers = {NULL, NULL};
1534 if (ELEM(NULL, id, ipo))
1537 printf("ERROR ipo_to_animdata(): adt invalid\n");
1541 if (G.debug & G_DEBUG) {
1542 printf("ipo to animdata - ID:%s, IPO:%s, actname:%s constname:%s seqname:%s curves:%d\n",
1543 id->name + 2, ipo->id.name + 2, (actname) ? actname : "<None>", (constname) ? constname : "<None>", (seq) ? (seq->name + 2) : "<None>",
1544 BLI_listbase_count(&ipo->curve));
1547 /* Convert curves to animato system (separated into separate lists of F-Curves for animation and drivers),
1548 * and the try to put these lists in the right places, but do not free the lists here
1550 // XXX there shouldn't be any need for the groups, so don't supply pointer for that now...
1551 ipo_to_animato(id, ipo, actname, constname, seq, NULL, &anim, &drivers);
1553 /* deal with animation first */
1555 if (G.debug & G_DEBUG) printf("\thas anim\n");
1556 /* try to get action */
1557 if (adt->action == NULL) {
1558 char nameBuf[MAX_ID_NAME];
1560 BLI_snprintf(nameBuf, sizeof(nameBuf), "CDA:%s", ipo->id.name + 2);
1562 adt->action = BKE_action_add(G.main, nameBuf);
1563 if (G.debug & G_DEBUG) printf("\t\tadded new action - '%s'\n", nameBuf);
1566 /* add F-Curves to action */
1567 BLI_movelisttolist(&adt->action->curves, &anim);
1570 /* deal with drivers */
1571 if (drivers.first) {
1572 if (G.debug & G_DEBUG) printf("\thas drivers\n");
1573 /* add drivers to end of driver stack */
1574 BLI_movelisttolist(&adt->drivers, &drivers);
1578 /* Convert Action-block to new system
1579 * NOTE: we need to be careful here, as same data-structs are used for new system too!
1581 static void action_to_animdata(ID *id, bAction *act)
1583 AnimData *adt = BKE_animdata_from_id(id);
1585 /* only continue if there are Action Channels (indicating unconverted data) */
1586 if (ELEM(NULL, adt, act->chanbase.first))
1589 /* check if we need to set this Action as the AnimData's action */
1590 if (adt->action == NULL) {
1591 /* set this Action as AnimData's Action */
1592 if (G.debug & G_DEBUG) printf("act_to_adt - set adt action to act\n");
1596 /* convert Action data */
1597 action_to_animato(id, act, &adt->action->groups, &adt->action->curves, &adt->drivers);
1600 /* ------------------------- */
1603 * - NLA group duplicators info
1604 * - NLA curve/stride modifiers... */
1606 /* Convert NLA-Strip to new system */
1607 static void nlastrips_to_animdata(ID *id, ListBase *strips)
1609 AnimData *adt = BKE_animdata_from_id(id);
1610 NlaTrack *nlt = NULL;
1612 bActionStrip *as, *asn;
1614 /* for each one of the original strips, convert to a new strip and free the old... */
1615 for (as = strips->first; as; as = asn) {
1618 /* this old strip is only worth something if it had an action... */
1620 /* convert Action data (if not yet converted), storing the results in the same Action */
1621 action_to_animato(id, as->act, &as->act->groups, &as->act->curves, &adt->drivers);
1623 /* create a new-style NLA-strip which references this Action, then copy over relevant settings */
1625 /* init a new strip, and assign the action to it
1626 * - no need to muck around with the user-counts, since this is just
1627 * passing over the ref to the new owner, not creating an additional ref
1629 strip = MEM_callocN(sizeof(NlaStrip), "NlaStrip");
1630 strip->act = as->act;
1633 strip->start = as->start;
1634 strip->end = as->end;
1635 strip->actstart = as->actstart;
1636 strip->actend = as->actend;
1639 strip->repeat = as->repeat;
1640 strip->scale = as->scale;
1641 if (as->flag & ACTSTRIP_LOCK_ACTION) strip->flag |= NLASTRIP_FLAG_SYNC_LENGTH;
1644 strip->blendin = as->blendin;
1645 strip->blendout = as->blendout;
1646 strip->blendmode = (as->mode == ACTSTRIPMODE_ADD) ? NLASTRIP_MODE_ADD : NLASTRIP_MODE_REPLACE;
1647 if (as->flag & ACTSTRIP_AUTO_BLENDS) strip->flag |= NLASTRIP_FLAG_AUTO_BLENDS;
1649 /* assorted setting flags */
1650 if (as->flag & ACTSTRIP_SELECT) strip->flag |= NLASTRIP_FLAG_SELECT;
1651 if (as->flag & ACTSTRIP_ACTIVE) strip->flag |= NLASTRIP_FLAG_ACTIVE;
1653 if (as->flag & ACTSTRIP_MUTE) strip->flag |= NLASTRIP_FLAG_MUTED;
1654 if (as->flag & ACTSTRIP_REVERSE) strip->flag |= NLASTRIP_FLAG_REVERSE;
1656 /* by default, we now always extrapolate, while in the past this was optional */
1657 if ((as->flag & ACTSTRIP_HOLDLASTFRAME) == 0)
1658 strip->extendmode = NLASTRIP_EXTEND_NOTHING;
1661 /* try to add this strip to the current NLA-Track (i.e. the 'last' one on the stack atm) */
1662 if (BKE_nlatrack_add_strip(nlt, strip) == 0) {
1663 /* trying to add to the current failed (no space),
1664 * so add a new track to the stack, and add to that...
1666 nlt = BKE_nlatrack_add(adt, NULL);
1667 BKE_nlatrack_add_strip(nlt, strip);
1670 /* ensure that strip has a name */
1671 BKE_nlastrip_validate_name(adt, strip);
1675 // FIXME: for now, we just free them...
1676 if (as->modifiers.first)
1677 BLI_freelistN(&as->modifiers);
1679 /* free the old strip */
1680 BLI_freelinkN(strips, as);
1684 /* *************************************************** */
1685 /* External API - Only Called from do_versions() */
1687 /* Called from do_versions() in readfile.c to convert the old 'IPO/adrcode' system
1688 * to the new 'Animato/RNA' system.
1690 * The basic method used here, is to loop over datablocks which have IPO-data, and
1691 * add those IPO's to new AnimData blocks as Actions.
1692 * Action/NLA data only works well for Objects, so these only need to be checked for there.
1694 * Data that has been converted should be freed immediately, which means that it is immediately
1695 * clear which datablocks have yet to be converted, and also prevent freeing errors when we exit.
1697 // XXX currently done after all file reading...
1698 void do_versions_ipos_to_animato(Main *main)
1700 ListBase drivers = {NULL, NULL};
1704 printf("Argh! Main is NULL in do_versions_ipos_to_animato()\n");
1708 /* only convert if version is right */
1709 if (main->versionfile >= 250) {
1710 printf("WARNING: Animation data too new to convert (Version %d)\n", main->versionfile);
1713 else if (G.debug & G_DEBUG)
1714 printf("INFO: Converting to Animato...\n");
1716 /* ----------- Animation Attached to Data -------------- */
1719 for (id = main->object.first; id; id = id->next) {
1720 Object *ob = (Object *)id;
1721 bPoseChannel *pchan;
1723 bConstraintChannel *conchan, *conchann;
1725 if (G.debug & G_DEBUG) printf("\tconverting ob %s\n", id->name + 2);
1727 /* check if object has any animation data */
1728 if (ob->nlastrips.first) {
1729 /* Add AnimData block */
1730 BKE_animdata_add_id(id);
1732 /* IPO first to take into any non-NLA'd Object Animation */
1734 ipo_to_animdata(id, ob->ipo, NULL, NULL, NULL);
1736 id_us_min(&ob->ipo->id);
1740 /* Action is skipped since it'll be used by some strip in the NLA anyway,
1741 * causing errors with evaluation in the new evaluation pipeline
1744 id_us_min(&ob->action->id);
1749 nlastrips_to_animdata(id, &ob->nlastrips);
1751 else if ((ob->ipo) || (ob->action)) {
1752 /* Add AnimData block */
1753 AnimData *adt = BKE_animdata_add_id(id);
1755 /* Action first - so that Action name get conserved */
1757 action_to_animdata(id, ob->action);
1759 /* only decrease usercount if this Action isn't now being used by AnimData */
1760 if (ob->action != adt->action) {
1761 id_us_min(&ob->action->id);
1768 ipo_to_animdata(id, ob->ipo, NULL, NULL, NULL);
1769 id_us_min(&ob->ipo->id);
1773 /* If we have any empty action actuators, assume they were
1774 * converted IPO Actuators using the object IPO */
1776 bActionActuator *aa;
1778 for (act = ob->actuators.first; act; act = act->next) {
1779 /* Any actuators set to ACT_IPO at this point are actually Action Actuators that
1780 * need this converted IPO to finish converting the actuator. */
1781 if (act->type == ACT_IPO) {
1782 aa = (bActionActuator *)act->data;
1783 aa->act = ob->adt->action;
1784 act->type = ACT_ACTION;
1791 /* check PoseChannels for constraints with local data */
1793 /* Verify if there's AnimData block */
1794 BKE_animdata_add_id(id);
1796 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
1797 for (con = pchan->constraints.first; con; con = con->next) {
1798 /* if constraint has own IPO, convert add these to Object
1799 * (NOTE: they're most likely to be drivers too)
1802 /* although this was the constraint's local IPO, we still need to provide pchan + con
1803 * so that drivers can be added properly...
1805 ipo_to_animdata(id, con->ipo, pchan->name, con->name, NULL);
1806 id_us_min(&con->ipo->id);
1813 /* check constraints for local IPO's */
1814 for (con = ob->constraints.first; con; con = con->next) {
1815 /* if constraint has own IPO, convert add these to Object
1816 * (NOTE: they're most likely to be drivers too)
1819 /* Verify if there's AnimData block, just in case */
1820 BKE_animdata_add_id(id);
1822 /* although this was the constraint's local IPO, we still need to provide con
1823 * so that drivers can be added properly...
1825 ipo_to_animdata(id, con->ipo, NULL, con->name, NULL);
1826 id_us_min(&con->ipo->id);
1830 /* check for Action Constraint */
1831 // XXX do we really want to do this here?
1834 /* check constraint channels - we need to remove them anyway... */
1835 if (ob->constraintChannels.first) {
1836 /* Verify if there's AnimData block */
1837 BKE_animdata_add_id(id);
1839 for (conchan = ob->constraintChannels.first; conchan; conchan = conchann) {
1840 /* get pointer to next Constraint Channel */
1841 conchann = conchan->next;
1843 /* convert Constraint Channel's IPO data */
1845 ipo_to_animdata(id, conchan->ipo, NULL, conchan->name, NULL);
1846 id_us_min(&conchan->ipo->id);
1847 conchan->ipo = NULL;
1850 /* free Constraint Channel */
1851 BLI_freelinkN(&ob->constraintChannels, conchan);
1855 /* object's action will always be object-rooted */
1857 AnimData *adt = BKE_animdata_from_id(id);
1858 if (adt && adt->action)
1859 adt->action->idroot = ID_OB;
1864 for (id = main->key.first; id; id = id->next) {
1865 Key *key = (Key *)id;
1867 if (G.debug & G_DEBUG) printf("\tconverting key %s\n", id->name + 2);
1869 /* we're only interested in the IPO
1870 * NOTE: for later, it might be good to port these over to Object instead, as many of these
1871 * are likely to be drivers, but it's hard to trace that from here, so move this to Ob loop?
1874 /* Add AnimData block */
1875 AnimData *adt = BKE_animdata_add_id(id);
1877 /* Convert Shapekey data... */
1878 ipo_to_animdata(id, key->ipo, NULL, NULL, NULL);
1881 adt->action->idroot = key->ipo->blocktype;
1883 id_us_min(&key->ipo->id);
1889 for (id = main->mat.first; id; id = id->next) {
1890 Material *ma = (Material *)id;
1892 if (G.debug & G_DEBUG) printf("\tconverting material %s\n", id->name + 2);
1894 /* we're only interested in the IPO */
1896 /* Add AnimData block */
1897 AnimData *adt = BKE_animdata_add_id(id);
1899 /* Convert Material data... */
1900 ipo_to_animdata(id, ma->ipo, NULL, NULL, NULL);
1903 adt->action->idroot = ma->ipo->blocktype;
1905 id_us_min(&ma->ipo->id);
1911 for (id = main->world.first; id; id = id->next) {
1912 World *wo = (World *)id;
1914 if (G.debug & G_DEBUG) printf("\tconverting world %s\n", id->name + 2);
1916 /* we're only interested in the IPO */
1918 /* Add AnimData block */
1919 AnimData *adt = BKE_animdata_add_id(id);
1921 /* Convert World data... */
1922 ipo_to_animdata(id, wo->ipo, NULL, NULL, NULL);
1925 adt->action->idroot = wo->ipo->blocktype;
1927 id_us_min(&wo->ipo->id);
1932 /* sequence strips */
1933 for (id = main->scene.first; id; id = id->next) {
1934 Scene *scene = (Scene *)id;
1935 Editing *ed = scene->ed;
1936 if (ed && ed->seqbasep) {
1939 AnimData *adt = BKE_animdata_add_id(id);
1943 IpoCurve *icu = (seq->ipo) ? seq->ipo->curve.first : NULL;
1944 short adrcode = SEQ_FAC1;
1946 if (G.debug & G_DEBUG)
1947 printf("\tconverting sequence strip %s\n", seq->name + 2);
1949 if (ELEM(NULL, seq->ipo, icu)) {
1950 seq->flag |= SEQ_USE_EFFECT_DEFAULT_FADE;
1954 /* patch adrcode, so that we can map
1955 * to different DNA variables later
1958 switch (seq->type) {
1959 case SEQ_TYPE_IMAGE:
1961 case SEQ_TYPE_SCENE:
1962 case SEQ_TYPE_MOVIE:
1963 case SEQ_TYPE_COLOR:
1964 adrcode = SEQ_FAC_OPACITY;
1966 case SEQ_TYPE_SPEED:
1967 adrcode = SEQ_FAC_SPEED;
1970 icu->adrcode = adrcode;
1973 ipo_to_animdata((ID *)scene, seq->ipo, NULL, NULL, seq);
1976 adt->action->idroot = ID_SCE; /* scene-rooted */
1978 id_us_min(&seq->ipo->id);
1987 for (id = main->tex.first; id; id = id->next) {
1988 Tex *te = (Tex *)id;
1990 if (G.debug & G_DEBUG) printf("\tconverting texture %s\n", id->name + 2);
1992 /* we're only interested in the IPO */
1994 /* Add AnimData block */
1995 AnimData *adt = BKE_animdata_add_id(id);
1997 /* Convert Texture data... */
1998 ipo_to_animdata(id, te->ipo, NULL, NULL, NULL);
2001 adt->action->idroot = te->ipo->blocktype;
2003 id_us_min(&te->ipo->id);
2009 for (id = main->camera.first; id; id = id->next) {
2010 Camera *ca = (Camera *)id;
2012 if (G.debug & G_DEBUG) printf("\tconverting camera %s\n", id->name + 2);
2014 /* we're only interested in the IPO */
2016 /* Add AnimData block */
2017 AnimData *adt = BKE_animdata_add_id(id);
2019 /* Convert Camera data... */
2020 ipo_to_animdata(id, ca->ipo, NULL, NULL, NULL);
2023 adt->action->idroot = ca->ipo->blocktype;
2025 id_us_min(&ca->ipo->id);
2031 for (id = main->lamp.first; id; id = id->next) {
2032 Lamp *la = (Lamp *)id;
2034 if (G.debug & G_DEBUG) printf("\tconverting lamp %s\n", id->name + 2);
2036 /* we're only interested in the IPO */
2038 /* Add AnimData block */
2039 AnimData *adt = BKE_animdata_add_id(id);
2041 /* Convert Lamp data... */
2042 ipo_to_animdata(id, la->ipo, NULL, NULL, NULL);
2045 adt->action->idroot = la->ipo->blocktype;
2047 id_us_min(&la->ipo->id);
2053 for (id = main->curve.first; id; id = id->next) {
2054 Curve *cu = (Curve *)id;
2056 if (G.debug & G_DEBUG) printf("\tconverting curve %s\n", id->name + 2);
2058 /* we're only interested in the IPO */
2060 /* Add AnimData block */
2061 AnimData *adt = BKE_animdata_add_id(id);
2063 /* Convert Curve data... */
2064 ipo_to_animdata(id, cu->ipo, NULL, NULL, NULL);
2067 adt->action->idroot = cu->ipo->blocktype;
2069 id_us_min(&cu->ipo->id);
2074 /* --------- Unconverted Animation Data ------------------ */
2075 /* For Animation data which may not be directly connected (i.e. not linked) to any other
2076 * data, we need to perform a separate pass to make sure that they are converted to standalone
2077 * Actions which may then be able to be reused. This does mean that we will be going over data that's
2078 * already been converted, but there are no problems with that.
2080 * The most common case for this will be Action Constraints, or IPO's with Fake-Users.
2081 * We collect all drivers that were found into a temporary collection, and free them in one go, as they're
2082 * impossible to resolve.
2086 for (id = main->action.first; id; id = id->next) {
2087 bAction *act = (bAction *)id;
2089 if (G.debug & G_DEBUG) printf("\tconverting action %s\n", id->name + 2);
2091 /* if old action, it will be object-only... */
2092 if (act->chanbase.first)
2093 act->idroot = ID_OB;
2095 /* be careful! some of the actions we encounter will be converted ones... */
2096 action_to_animato(NULL, act, &act->groups, &act->curves, &drivers);
2100 for (id = main->ipo.first; id; id = id->next) {
2101 Ipo *ipo = (Ipo *)id;
2103 if (G.debug & G_DEBUG) printf("\tconverting ipo %s\n", id->name + 2);
2105 /* most likely this IPO has already been processed, so check if any curves left to convert */
2106 if (ipo->curve.first) {
2109 /* add a new action for this, and convert all data into that action */
2110 new_act = BKE_action_add(main, id->name + 2);
2111 ipo_to_animato(NULL, ipo, NULL, NULL, NULL, NULL, &new_act->curves, &drivers);
2112 new_act->idroot = ipo->blocktype;
2115 /* clear fake-users, and set user-count to zero to make sure it is cleared on file-save */
2117 ipo->id.flag &= ~LIB_FAKEUSER;
2120 /* free unused drivers from actions + ipos */
2121 free_fcurves(&drivers);
2123 if (G.debug & G_DEBUG)
2124 printf("INFO: Animato convert done\n");