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 * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Nathan Letwory, Sukhitha Jayathilake.
20 * ***** END GPL LICENSE BLOCK *****
23 /** \file blender/collada/AnimationImporter.cpp
29 /* COLLADABU_ASSERT, may be able to remove later */
30 #include "COLLADABUPlatform.h"
32 #include "DNA_armature_types.h"
34 #include "ED_keyframing.h"
36 #include "BLI_listbase.h"
38 #include "BLI_path_util.h"
39 #include "BLI_string.h"
41 #include "BKE_action.h"
42 #include "BKE_armature.h"
43 #include "BKE_fcurve.h"
44 #include "BKE_object.h"
46 #include "MEM_guardedalloc.h"
48 #include "collada_utils.h"
49 #include "AnimationImporter.h"
50 #include "ArmatureImporter.h"
51 #include "MaterialExporter.h"
55 // first try node name, if not available (since is optional), fall back to original id
57 static const char *bc_get_joint_name(T *node)
59 const std::string& id = node->getName();
60 return id.size() ? id.c_str() : node->getOriginalId().c_str();
63 FCurve *AnimationImporter::create_fcurve(int array_index, const char *rna_path)
65 FCurve *fcu = (FCurve *)MEM_callocN(sizeof(FCurve), "FCurve");
66 fcu->flag = (FCURVE_VISIBLE | FCURVE_AUTO_HANDLES | FCURVE_SELECTED);
67 fcu->rna_path = BLI_strdupn(rna_path, strlen(rna_path));
68 fcu->array_index = array_index;
72 void AnimationImporter::create_bezt(FCurve *fcu, float frame, float output)
75 memset(&bez, 0, sizeof(BezTriple));
76 bez.vec[1][0] = frame;
77 bez.vec[1][1] = output;
78 bez.ipo = U.ipo_new; /* use default interpolation mode here... */
79 bez.f1 = bez.f2 = bez.f3 = SELECT;
80 bez.h1 = bez.h2 = HD_AUTO;
81 insert_bezt_fcurve(fcu, &bez, 0);
82 calchandles_fcurve(fcu);
85 // create one or several fcurves depending on the number of parameters being animated
86 void AnimationImporter::animation_to_fcurves(COLLADAFW::AnimationCurve *curve)
88 COLLADAFW::FloatOrDoubleArray& input = curve->getInputValues();
89 COLLADAFW::FloatOrDoubleArray& output = curve->getOutputValues();
91 float fps = (float)FPS;
92 size_t dim = curve->getOutDimension();
95 std::vector<FCurve *>& fcurves = curve_map[curve->getUniqueId()];
98 case 1: // X, Y, Z or angle
103 for (i = 0; i < dim; i++) {
104 FCurve *fcu = (FCurve *)MEM_callocN(sizeof(FCurve), "FCurve");
106 fcu->flag = (FCURVE_VISIBLE | FCURVE_AUTO_HANDLES | FCURVE_SELECTED);
107 // fcu->rna_path = BLI_strdupn(path, strlen(path));
108 fcu->array_index = 0;
109 fcu->totvert = curve->getKeyCount();
111 // create beztriple for each key
112 for (unsigned int j = 0; j < curve->getKeyCount(); j++) {
114 memset(&bez, 0, sizeof(BezTriple));
118 bez.vec[1][0] = bc_get_float_value(input, j) * fps;
119 bez.vec[1][1] = bc_get_float_value(output, j * dim + i);
122 if (curve->getInterpolationType() == COLLADAFW::AnimationCurve::INTERPOLATION_BEZIER ||
123 curve->getInterpolationType() == COLLADAFW::AnimationCurve::INTERPOLATION_STEP)
125 COLLADAFW::FloatOrDoubleArray& intan = curve->getInTangentValues();
126 COLLADAFW::FloatOrDoubleArray& outtan = curve->getOutTangentValues();
129 bez.vec[0][0] = bc_get_float_value(intan, (j * 2 * dim) + (2 * i)) * fps;
130 bez.vec[0][1] = bc_get_float_value(intan, (j * 2 * dim) + (2 * i) + 1);
133 bez.vec[2][0] = bc_get_float_value(outtan, (j * 2 * dim) + (2 * i)) * fps;
134 bez.vec[2][1] = bc_get_float_value(outtan, (j * 2 * dim) + (2 * i) + 1);
135 if (curve->getInterpolationType() == COLLADAFW::AnimationCurve::INTERPOLATION_BEZIER)
136 bez.ipo = BEZT_IPO_BEZ;
138 bez.ipo = BEZT_IPO_CONST;
139 //bez.h1 = bez.h2 = HD_AUTO;
142 bez.h1 = bez.h2 = HD_AUTO;
143 bez.ipo = BEZT_IPO_LIN;
145 // bez.ipo = U.ipo_new; /* use default interpolation mode here... */
146 bez.f1 = bez.f2 = bez.f3 = SELECT;
148 insert_bezt_fcurve(fcu, &bez, 0);
151 calchandles_fcurve(fcu);
153 fcurves.push_back(fcu);
158 fprintf(stderr, "Output dimension of %d is not yet supported (animation id = %s)\n", (int)dim, curve->getOriginalId().c_str());
161 for (std::vector<FCurve *>::iterator it = fcurves.begin(); it != fcurves.end(); it++)
162 unused_curves.push_back(*it);
166 void AnimationImporter::fcurve_deg_to_rad(FCurve *cu)
168 for (unsigned int i = 0; i < cu->totvert; i++) {
169 // TODO convert handles too
170 cu->bezt[i].vec[1][1] *= DEG2RADF(1.0f);
171 cu->bezt[i].vec[0][1] *= DEG2RADF(1.0f);
172 cu->bezt[i].vec[2][1] *= DEG2RADF(1.0f);
177 void AnimationImporter::add_fcurves_to_object(Object *ob, std::vector<FCurve *>& curves, char *rna_path, int array_index, Animation *animated)
181 if (!ob->adt || !ob->adt->action) act = verify_adt_action((ID *)&ob->id, 1);
182 else act = ob->adt->action;
184 std::vector<FCurve *>::iterator it;
188 char *p = strstr(rna_path, "rotation_euler");
189 bool is_rotation = p && *(p + strlen("rotation_euler")) == '\0';
191 // convert degrees to radians for rotation
193 fcurve_deg_to_rad(fcu);
196 for (it = curves.begin(), i = 0; it != curves.end(); it++, i++) {
198 fcu->rna_path = BLI_strdupn(rna_path, strlen(rna_path));
200 if (array_index == -1) fcu->array_index = i;
201 else fcu->array_index = array_index;
203 if (ob->type == OB_ARMATURE) {
204 bActionGroup *grp = NULL;
205 const char *bone_name = bc_get_joint_name(animated->node);
208 /* try to find group */
209 grp = BKE_action_group_find_name(act, bone_name);
211 /* no matching groups, so add one */
213 /* Add a new group, and make it active */
214 grp = (bActionGroup *)MEM_callocN(sizeof(bActionGroup), "bActionGroup");
216 grp->flag = AGRP_SELECTED;
217 BLI_strncpy(grp->name, bone_name, sizeof(grp->name));
219 BLI_addtail(&act->groups, grp);
220 BLI_uniquename(&act->groups, grp, "Group", '.', offsetof(bActionGroup, name), 64);
223 /* add F-Curve to group */
224 action_groups_add_channel(act, grp, fcu);
229 fcurves_actionGroup_map[grp].push_back(fcu);
234 BLI_addtail(&act->curves, fcu);
237 // curve is used, so remove it from unused_curves
238 unused_curves.erase(std::remove(unused_curves.begin(), unused_curves.end(), fcu), unused_curves.end());
242 AnimationImporter::AnimationImporter(UnitConverter *conv, ArmatureImporter *arm, Scene *scene) :
243 TransformReader(conv), armature_importer(arm), scene(scene) {
246 AnimationImporter::~AnimationImporter()
248 // free unused FCurves
249 for (std::vector<FCurve *>::iterator it = unused_curves.begin(); it != unused_curves.end(); it++)
252 if (unused_curves.size())
253 fprintf(stderr, "removed %d unused curves\n", (int)unused_curves.size());
256 bool AnimationImporter::write_animation(const COLLADAFW::Animation *anim)
258 if (anim->getAnimationType() == COLLADAFW::Animation::ANIMATION_CURVE) {
259 COLLADAFW::AnimationCurve *curve = (COLLADAFW::AnimationCurve *)anim;
261 // XXX Don't know if it's necessary
262 // Should we check outPhysicalDimension?
263 if (curve->getInPhysicalDimension() != COLLADAFW::PHYSICAL_DIMENSION_TIME) {
264 fprintf(stderr, "Inputs physical dimension is not time.\n");
268 // a curve can have mixed interpolation type,
269 // in this case curve->getInterpolationTypes returns a list of interpolation types per key
270 COLLADAFW::AnimationCurve::InterpolationType interp = curve->getInterpolationType();
272 if (interp != COLLADAFW::AnimationCurve::INTERPOLATION_MIXED) {
274 case COLLADAFW::AnimationCurve::INTERPOLATION_LINEAR:
275 case COLLADAFW::AnimationCurve::INTERPOLATION_BEZIER:
276 case COLLADAFW::AnimationCurve::INTERPOLATION_STEP:
277 animation_to_fcurves(curve);
280 // TODO there're also CARDINAL, HERMITE, BSPLINE and STEP types
281 fprintf(stderr, "CARDINAL, HERMITE and BSPLINE anim interpolation types not supported yet.\n");
287 fprintf(stderr, "MIXED anim interpolation type is not supported yet.\n");
291 fprintf(stderr, "FORMULA animation type is not supported yet.\n");
297 // called on post-process stage after writeVisualScenes
298 bool AnimationImporter::write_animation_list(const COLLADAFW::AnimationList *animlist)
300 const COLLADAFW::UniqueId& animlist_id = animlist->getUniqueId();
302 animlist_map[animlist_id] = animlist;
307 if (uid_animated_map.find(animlist_id) == uid_animated_map.end()) {
311 // for bones rna_path is like: pose.bones["bone-name"].rotation
319 // \todo refactor read_node_transform to not automatically apply anything,
320 // but rather return the transform matrix, so caller can do with it what is
321 // necessary. Same for \ref get_node_mat
322 void AnimationImporter::read_node_transform(COLLADAFW::Node *node, Object *ob)
325 TransformReader::get_node_mat(mat, node, &uid_animated_map, ob);
327 copy_m4_m4(ob->obmat, mat);
328 BKE_object_apply_mat4(ob, ob->obmat, 0, 0);
333 virtual void AnimationImporter::change_eul_to_quat(Object *ob, bAction *act)
338 for (grp = (bActionGroup *)act->groups.first; grp; grp = grp->next) {
340 FCurve *eulcu[3] = {NULL, NULL, NULL};
342 if (fcurves_actionGroup_map.find(grp) == fcurves_actionGroup_map.end())
345 std::vector<FCurve *> &rot_fcurves = fcurves_actionGroup_map[grp];
347 if (rot_fcurves.size() > 3) continue;
349 for (i = 0; i < rot_fcurves.size(); i++)
350 eulcu[rot_fcurves[i]->array_index] = rot_fcurves[i];
352 char joint_path[100];
355 BLI_snprintf(joint_path, sizeof(joint_path), "pose.bones[\"%s\"]", grp->name);
356 BLI_snprintf(rna_path, sizeof(rna_path), "%s.rotation_quaternion", joint_path);
358 FCurve *quatcu[4] = {
359 create_fcurve(0, rna_path),
360 create_fcurve(1, rna_path),
361 create_fcurve(2, rna_path),
362 create_fcurve(3, rna_path)
365 bPoseChannel *chan = BKE_pose_channel_find_name(ob->pose, grp->name);
367 float m4[4][4], irest[3][3];
368 invert_m4_m4(m4, chan->bone->arm_mat);
369 copy_m3_m4(irest, m4);
371 for (i = 0; i < 3; i++) {
373 FCurve *cu = eulcu[i];
377 for (int j = 0; j < cu->totvert; j++) {
378 float frame = cu->bezt[j].vec[1][0];
381 eulcu[0] ? evaluate_fcurve(eulcu[0], frame) : 0.0f,
382 eulcu[1] ? evaluate_fcurve(eulcu[1], frame) : 0.0f,
383 eulcu[2] ? evaluate_fcurve(eulcu[2], frame) : 0.0f
386 // make eul relative to bone rest pose
387 float rot[3][3], rel[3][3], quat[4];
389 /*eul_to_mat3(rot, eul);
391 mul_m3_m3m3(rel, irest, rot);
393 mat3_to_quat(quat, rel);
396 eul_to_quat(quat, eul);
398 for (int k = 0; k < 4; k++)
399 create_bezt(quatcu[k], frame, quat[k]);
403 // now replace old Euler curves
405 for (i = 0; i < 3; i++) {
406 if (!eulcu[i]) continue;
408 action_groups_remove_channel(act, eulcu[i]);
409 free_fcurve(eulcu[i]);
412 chan->rotmode = ROT_MODE_QUAT;
414 for (i = 0; i < 4; i++)
415 action_groups_add_channel(act, grp, quatcu[i]);
419 for (pchan = (bPoseChannel *)ob->pose->chanbase.first; pchan; pchan = pchan->next) {
420 pchan->rotmode = ROT_MODE_QUAT;
426 //sets the rna_path and array index to curve
427 void AnimationImporter::modify_fcurve(std::vector<FCurve *> *curves, const char *rna_path, int array_index)
429 std::vector<FCurve *>::iterator it;
431 for (it = curves->begin(), i = 0; it != curves->end(); it++, i++) {
433 fcu->rna_path = BLI_strdup(rna_path);
435 if (array_index == -1) fcu->array_index = i;
436 else fcu->array_index = array_index;
438 unused_curves.erase(std::remove(unused_curves.begin(), unused_curves.end(), fcu), unused_curves.end());
442 void AnimationImporter::unused_fcurve(std::vector<FCurve *> *curves)
444 // when an error happens and we can't actually use curve remove it from unused_curves
445 std::vector<FCurve *>::iterator it;
446 for (it = curves->begin(); it != curves->end(); it++) {
448 unused_curves.erase(std::remove(unused_curves.begin(), unused_curves.end(), fcu), unused_curves.end());
452 void AnimationImporter::find_frames(std::vector<float> *frames, std::vector<FCurve *> *curves)
454 std::vector<FCurve *>::iterator iter;
455 for (iter = curves->begin(); iter != curves->end(); iter++) {
458 for (unsigned int k = 0; k < fcu->totvert; k++) {
459 //get frame value from bezTriple
460 float fra = fcu->bezt[k].vec[1][0];
461 //if frame already not added add frame to frames
462 if (std::find(frames->begin(), frames->end(), fra) == frames->end())
463 frames->push_back(fra);
469 //creates the rna_paths and array indices of fcurves from animations using transformation and bound animation class of each animation.
470 void AnimationImporter:: Assign_transform_animations(COLLADAFW::Transformation *transform,
471 const COLLADAFW::AnimationList::AnimationBinding *binding,
472 std::vector<FCurve *> *curves, bool is_joint, char *joint_path)
474 COLLADAFW::Transformation::TransformationType tm_type = transform->getTransformationType();
475 bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX;
476 bool is_rotation = tm_type == COLLADAFW::Transformation::ROTATE;
478 //to check if the no of curves are valid
479 bool xyz = ((tm_type == COLLADAFW::Transformation::TRANSLATE || tm_type == COLLADAFW::Transformation::SCALE) && binding->animationClass == COLLADAFW::AnimationList::POSITION_XYZ);
482 if (!((!xyz && curves->size() == 1) || (xyz && curves->size() == 3) || is_matrix)) {
483 fprintf(stderr, "expected %d curves, got %d\n", xyz ? 3 : 1, (int)curves->size());
490 case COLLADAFW::Transformation::TRANSLATE:
491 case COLLADAFW::Transformation::SCALE:
493 bool loc = tm_type == COLLADAFW::Transformation::TRANSLATE;
495 BLI_snprintf(rna_path, sizeof(rna_path), "%s.%s", joint_path, loc ? "location" : "scale");
497 BLI_strncpy(rna_path, loc ? "location" : "scale", sizeof(rna_path));
499 switch (binding->animationClass) {
500 case COLLADAFW::AnimationList::POSITION_X:
501 modify_fcurve(curves, rna_path, 0);
503 case COLLADAFW::AnimationList::POSITION_Y:
504 modify_fcurve(curves, rna_path, 1);
506 case COLLADAFW::AnimationList::POSITION_Z:
507 modify_fcurve(curves, rna_path, 2);
509 case COLLADAFW::AnimationList::POSITION_XYZ:
510 modify_fcurve(curves, rna_path, -1);
513 unused_fcurve(curves);
514 fprintf(stderr, "AnimationClass %d is not supported for %s.\n",
515 binding->animationClass, loc ? "TRANSLATE" : "SCALE");
521 case COLLADAFW::Transformation::ROTATE:
524 BLI_snprintf(rna_path, sizeof(rna_path), "%s.rotation_euler", joint_path);
526 BLI_strncpy(rna_path, "rotation_euler", sizeof(rna_path));
527 std::vector<FCurve *>::iterator iter;
528 for (iter = curves->begin(); iter != curves->end(); iter++) {
531 //if transform is rotation the fcurves values must be turned in to radian.
533 fcurve_deg_to_rad(fcu);
535 COLLADAFW::Rotate *rot = (COLLADAFW::Rotate *)transform;
536 COLLADABU::Math::Vector3& axis = rot->getRotationAxis();
538 switch (binding->animationClass) {
539 case COLLADAFW::AnimationList::ANGLE:
540 if (COLLADABU::Math::Vector3::UNIT_X == axis) {
541 modify_fcurve(curves, rna_path, 0);
543 else if (COLLADABU::Math::Vector3::UNIT_Y == axis) {
544 modify_fcurve(curves, rna_path, 1);
546 else if (COLLADABU::Math::Vector3::UNIT_Z == axis) {
547 modify_fcurve(curves, rna_path, 2);
550 unused_fcurve(curves);
552 case COLLADAFW::AnimationList::AXISANGLE:
553 // TODO convert axis-angle to quat? or XYZ?
555 unused_fcurve(curves);
556 fprintf(stderr, "AnimationClass %d is not supported for ROTATE transformation.\n",
557 binding->animationClass);
562 case COLLADAFW::Transformation::MATRIX:
564 COLLADAFW::Matrix* mat = (COLLADAFW::Matrix*)transform;
565 COLLADABU::Math::Matrix4 mat4 = mat->getMatrix();
566 switch (binding->animationClass) {
567 case COLLADAFW::AnimationList::TRANSFORM:
571 unused_fcurve(curves);
573 case COLLADAFW::Transformation::SKEW:
574 case COLLADAFW::Transformation::LOOKAT:
575 unused_fcurve(curves);
576 fprintf(stderr, "Animation of SKEW and LOOKAT transformations is not supported yet.\n");
582 //creates the rna_paths and array indices of fcurves from animations using color and bound animation class of each animation.
583 void AnimationImporter:: Assign_color_animations(const COLLADAFW::UniqueId& listid, ListBase *AnimCurves, const char *anim_type)
586 BLI_strncpy(rna_path, anim_type, sizeof(rna_path));
588 const COLLADAFW::AnimationList *animlist = animlist_map[listid];
589 const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings();
590 //all the curves belonging to the current binding
591 std::vector<FCurve *> animcurves;
592 for (unsigned int j = 0; j < bindings.getCount(); j++) {
593 animcurves = curve_map[bindings[j].animation];
595 switch (bindings[j].animationClass) {
596 case COLLADAFW::AnimationList::COLOR_R:
597 modify_fcurve(&animcurves, rna_path, 0);
599 case COLLADAFW::AnimationList::COLOR_G:
600 modify_fcurve(&animcurves, rna_path, 1);
602 case COLLADAFW::AnimationList::COLOR_B:
603 modify_fcurve(&animcurves, rna_path, 2);
605 case COLLADAFW::AnimationList::COLOR_RGB:
606 case COLLADAFW::AnimationList::COLOR_RGBA: // to do-> set intensity
607 modify_fcurve(&animcurves, rna_path, -1);
611 unused_fcurve(&animcurves);
612 fprintf(stderr, "AnimationClass %d is not supported for %s.\n",
613 bindings[j].animationClass, "COLOR");
616 std::vector<FCurve *>::iterator iter;
617 //Add the curves of the current animation to the object
618 for (iter = animcurves.begin(); iter != animcurves.end(); iter++) {
620 BLI_addtail(AnimCurves, fcu);
627 void AnimationImporter:: Assign_float_animations(const COLLADAFW::UniqueId& listid, ListBase *AnimCurves, const char *anim_type)
630 if (animlist_map.find(listid) == animlist_map.end()) {
634 //anim_type has animations
635 const COLLADAFW::AnimationList *animlist = animlist_map[listid];
636 const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings();
637 //all the curves belonging to the current binding
638 std::vector<FCurve *> animcurves;
639 for (unsigned int j = 0; j < bindings.getCount(); j++) {
640 animcurves = curve_map[bindings[j].animation];
642 BLI_strncpy(rna_path, anim_type, sizeof(rna_path));
643 modify_fcurve(&animcurves, rna_path, 0);
644 std::vector<FCurve *>::iterator iter;
645 //Add the curves of the current animation to the object
646 for (iter = animcurves.begin(); iter != animcurves.end(); iter++) {
648 BLI_addtail(AnimCurves, fcu);
656 * Lens animations must be stored in COLLADA by using FOV,
657 * while blender internally uses focal length.
658 * The imported animation curves must be converted appropriately.
660 void AnimationImporter::Assign_lens_animations(const COLLADAFW::UniqueId& listid, ListBase *AnimCurves, const double aspect, Camera *cam, const char *anim_type, int fov_type)
663 if (animlist_map.find(listid) == animlist_map.end()) {
667 //anim_type has animations
668 const COLLADAFW::AnimationList *animlist = animlist_map[listid];
669 const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings();
670 //all the curves belonging to the current binding
671 std::vector<FCurve *> animcurves;
672 for (unsigned int j = 0; j < bindings.getCount(); j++) {
673 animcurves = curve_map[bindings[j].animation];
675 BLI_strncpy(rna_path, anim_type, sizeof(rna_path));
677 modify_fcurve(&animcurves, rna_path, 0);
678 std::vector<FCurve *>::iterator iter;
679 //Add the curves of the current animation to the object
680 for (iter = animcurves.begin(); iter != animcurves.end(); iter++) {
683 for (unsigned int i = 0; i < fcu->totvert; i++) {
685 double input_fov = fcu->bezt[i].vec[1][1];
686 double xfov = (fov_type == CAMERA_YFOV) ? aspect * input_fov : input_fov;
688 // fov is in degrees, cam->lens is in millimiters
689 double fov = fov_to_focallength(DEG2RADF(input_fov), cam->sensor_x);
691 fcu->bezt[i].vec[1][1] = fov;
694 BLI_addtail(AnimCurves, fcu);
700 void AnimationImporter::apply_matrix_curves(Object *ob, std::vector<FCurve *>& animcurves, COLLADAFW::Node *root, COLLADAFW::Node *node,
701 COLLADAFW::Transformation *tm)
703 bool is_joint = node->getType() == COLLADAFW::Node::JOINT;
704 const char *bone_name = is_joint ? bc_get_joint_name(node) : NULL;
705 char joint_path[200];
707 armature_importer->get_rna_path_for_joint(node, joint_path, sizeof(joint_path));
709 std::vector<float> frames;
710 find_frames(&frames, &animcurves);
712 float irest_dae[4][4];
713 float rest[4][4], irest[4][4];
716 get_joint_rest_mat(irest_dae, root, node);
717 invert_m4(irest_dae);
719 Bone *bone = BKE_armature_find_bone_name((bArmature *)ob->data, bone_name);
721 fprintf(stderr, "cannot find bone \"%s\"\n", bone_name);
726 copy_m4_m4(rest, bone->arm_mat);
727 invert_m4_m4(irest, rest);
729 // new curves to assign matrix transform animation
730 FCurve *newcu[10]; // if tm_type is matrix, then create 10 curves: 4 rot, 3 loc, 3 scale
731 unsigned int totcu = 10;
732 const char *tm_str = NULL;
734 for (int i = 0; i < totcu; i++) {
739 tm_str = "rotation_quaternion";
753 BLI_snprintf(rna_path, sizeof(rna_path), "%s.%s", joint_path, tm_str);
755 BLI_strncpy(rna_path, tm_str, sizeof(rna_path));
756 newcu[i] = create_fcurve(axis, rna_path);
757 newcu[i]->totvert = frames.size();
760 if (frames.size() == 0)
763 std::sort(frames.begin(), frames.end());
765 std::vector<float>::iterator it;
767 // sample values at each frame
768 for (it = frames.begin(); it != frames.end(); it++) {
776 // calc object-space mat
777 evaluate_transform_at_frame(matfra, node, fra);
780 // for joints, we need a special matrix
782 // special matrix: iR * M * iR_dae * R
783 // where R, iR are bone rest and inverse rest mats in world space (Blender bones),
784 // iR_dae is joint inverse rest matrix (DAE) and M is an evaluated joint world-space matrix (DAE)
785 float temp[4][4], par[4][4];
788 calc_joint_parent_mat_rest(par, NULL, root, node);
789 mult_m4_m4m4(temp, par, matfra);
791 // evaluate_joint_world_transform_at_frame(temp, NULL, node, fra);
793 // calc special matrix
794 mul_serie_m4(mat, irest, temp, irest_dae, rest, NULL, NULL, NULL, NULL);
797 copy_m4_m4(mat, matfra);
800 float rot[4], loc[3], scale[3];
802 mat4_to_quat(rot, mat);
803 /*for ( int i = 0 ; i < 4 ; i ++ )
805 rot[i] = RAD2DEGF(rot[i]);
807 copy_v3_v3(loc, mat[3]);
808 mat4_to_size(scale, mat);
811 for (int i = 0; i < totcu; i++) {
813 add_bezt(newcu[i], fra, rot[i]);
815 add_bezt(newcu[i], fra, loc[i - 4]);
817 add_bezt(newcu[i], fra, scale[i - 7]);
820 verify_adt_action((ID *)&ob->id, 1);
822 ListBase *curves = &ob->adt->action->curves;
825 for (int i = 0; i < totcu; i++) {
827 add_bone_fcurve(ob, node, newcu[i]);
829 BLI_addtail(curves, newcu[i]);
833 bPoseChannel *chan = BKE_pose_channel_find_name(ob->pose, bone_name);
834 chan->rotmode = ROT_MODE_QUAT;
837 ob->rotmode = ROT_MODE_QUAT;
845 * This function returns the aspet ration from the Collada camera.
847 * Note:COLLADA allows to specify either XFov, or YFov alone.
848 * In tghat case the aspect ratio can be determined from
849 * the viewport aspect ratio (which is 1:1 ?)
850 * XXX: check this: its probably wrong!
851 * If both values are specified, then the aspect ration is simply xfov/yfov
852 * and if aspect ratio is efined, then .. well then its that one.
854 static const double get_aspect_ratio(const COLLADAFW::Camera *camera)
856 double aspect = camera->getAspectRatio().getValue();
860 const double yfov = camera->getYFov().getValue();
863 aspect=1; // assume yfov and xfov are equal
866 const double xfov = camera->getXFov().getValue();
870 aspect = xfov / yfov;
877 void AnimationImporter::translate_Animations(COLLADAFW::Node *node,
878 std::map<COLLADAFW::UniqueId, COLLADAFW::Node *>& root_map,
879 std::multimap<COLLADAFW::UniqueId, Object *>& object_map,
880 std::map<COLLADAFW::UniqueId, const COLLADAFW::Object *> FW_object_map)
882 AnimationImporter::AnimMix *animType = get_animation_type(node, FW_object_map);
884 bool is_joint = node->getType() == COLLADAFW::Node::JOINT;
885 COLLADAFW::UniqueId uid = node->getUniqueId();
886 COLLADAFW::Node *root = root_map.find(uid) == root_map.end() ? node : root_map[uid];
890 ob = armature_importer->get_armature_for_joint(root);
892 ob = object_map.find(uid) == object_map.end() ? NULL : object_map.find(uid)->second;
895 fprintf(stderr, "cannot find Object for Node with id=\"%s\"\n", node->getOriginalId().c_str());
901 if ( (animType->transform) != 0) {
902 /* const char *bone_name = is_joint ? bc_get_joint_name(node) : NULL; */ /* UNUSED */
903 char joint_path[200];
906 armature_importer->get_rna_path_for_joint(node, joint_path, sizeof(joint_path));
909 if (!ob->adt || !ob->adt->action) act = verify_adt_action((ID *)&ob->id, 1);
910 else act = ob->adt->action;
912 //Get the list of animation curves of the object
913 ListBase *AnimCurves = &(act->curves);
915 const COLLADAFW::TransformationPointerArray& nodeTransforms = node->getTransformations();
917 //for each transformation in node
918 for (unsigned int i = 0; i < nodeTransforms.getCount(); i++) {
919 COLLADAFW::Transformation *transform = nodeTransforms[i];
920 COLLADAFW::Transformation::TransformationType tm_type = transform->getTransformationType();
922 bool is_rotation = tm_type == COLLADAFW::Transformation::ROTATE;
923 bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX;
925 const COLLADAFW::UniqueId& listid = transform->getAnimationList();
927 //check if transformation has animations
928 if (animlist_map.find(listid) == animlist_map.end()) {
932 //transformation has animations
933 const COLLADAFW::AnimationList *animlist = animlist_map[listid];
934 const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings();
935 //all the curves belonging to the current binding
936 std::vector<FCurve *> animcurves;
937 for (unsigned int j = 0; j < bindings.getCount(); j++) {
938 animcurves = curve_map[bindings[j].animation];
940 apply_matrix_curves(ob, animcurves, root, node, transform);
946 add_bone_animation_sampled(ob, animcurves, root, node, transform);
949 //calculate rnapaths and array index of fcurves according to transformation and animation class
950 Assign_transform_animations(transform, &bindings[j], &animcurves, is_joint, joint_path);
952 std::vector<FCurve *>::iterator iter;
953 //Add the curves of the current animation to the object
954 for (iter = animcurves.begin(); iter != animcurves.end(); iter++) {
957 BLI_addtail(AnimCurves, fcu);
964 if (is_rotation && !is_joint) {
965 ob->rotmode = ROT_MODE_EUL;
970 if ((animType->light) != 0) {
971 Lamp *lamp = (Lamp *) ob->data;
973 if (!lamp->adt || !lamp->adt->action) act = verify_adt_action((ID *)&lamp->id, 1);
974 else act = lamp->adt->action;
976 ListBase *AnimCurves = &(act->curves);
977 const COLLADAFW::InstanceLightPointerArray& nodeLights = node->getInstanceLights();
979 for (unsigned int i = 0; i < nodeLights.getCount(); i++) {
980 const COLLADAFW::Light *light = (COLLADAFW::Light *) FW_object_map[nodeLights[i]->getInstanciatedObjectId()];
982 if ((animType->light & LIGHT_COLOR) != 0) {
983 const COLLADAFW::Color *col = &(light->getColor());
984 const COLLADAFW::UniqueId& listid = col->getAnimationList();
986 Assign_color_animations(listid, AnimCurves, "color");
988 if ((animType->light & LIGHT_FOA) != 0) {
989 const COLLADAFW::AnimatableFloat *foa = &(light->getFallOffAngle());
990 const COLLADAFW::UniqueId& listid = foa->getAnimationList();
992 Assign_float_animations(listid, AnimCurves, "spot_size");
994 if ( (animType->light & LIGHT_FOE) != 0) {
995 const COLLADAFW::AnimatableFloat *foe = &(light->getFallOffExponent());
996 const COLLADAFW::UniqueId& listid = foe->getAnimationList();
998 Assign_float_animations(listid, AnimCurves, "spot_blend");
1004 if (animType->camera != 0) {
1005 Camera *cam = (Camera *) ob->data;
1006 if (!cam->adt || !cam->adt->action)
1007 act = verify_adt_action((ID *)&cam->id, 1);
1009 act = cam->adt->action;
1011 ListBase *AnimCurves = &(act->curves);
1012 const COLLADAFW::InstanceCameraPointerArray& nodeCameras = node->getInstanceCameras();
1014 for (unsigned int i = 0; i < nodeCameras.getCount(); i++) {
1015 const COLLADAFW::Camera *camera = (COLLADAFW::Camera *) FW_object_map[nodeCameras[i]->getInstanciatedObjectId()];
1017 if ((animType->camera & CAMERA_XFOV) != 0) {
1018 const COLLADAFW::AnimatableFloat *xfov = &(camera->getXFov());
1019 const COLLADAFW::UniqueId& listid = xfov->getAnimationList();
1020 double aspect = get_aspect_ratio(camera);
1021 Assign_lens_animations(listid, AnimCurves, aspect, cam, "lens", CAMERA_XFOV);
1024 else if ((animType->camera & CAMERA_YFOV) != 0) {
1025 const COLLADAFW::AnimatableFloat *yfov = &(camera->getYFov());
1026 const COLLADAFW::UniqueId& listid = yfov->getAnimationList();
1027 double aspect = get_aspect_ratio(camera);
1028 Assign_lens_animations(listid, AnimCurves, aspect, cam, "lens", CAMERA_YFOV);
1031 else if ((animType->camera & CAMERA_XMAG) != 0) {
1032 const COLLADAFW::AnimatableFloat *xmag = &(camera->getXMag());
1033 const COLLADAFW::UniqueId& listid = xmag->getAnimationList();
1034 Assign_float_animations(listid, AnimCurves, "ortho_scale");
1037 else if ((animType->camera & CAMERA_YMAG) != 0) {
1038 const COLLADAFW::AnimatableFloat *ymag = &(camera->getYMag());
1039 const COLLADAFW::UniqueId& listid = ymag->getAnimationList();
1040 Assign_float_animations(listid, AnimCurves, "ortho_scale");
1043 if ((animType->camera & CAMERA_ZFAR) != 0) {
1044 const COLLADAFW::AnimatableFloat *zfar = &(camera->getFarClippingPlane());
1045 const COLLADAFW::UniqueId& listid = zfar->getAnimationList();
1046 Assign_float_animations(listid, AnimCurves, "clip_end");
1049 if ((animType->camera & CAMERA_ZNEAR) != 0) {
1050 const COLLADAFW::AnimatableFloat *znear = &(camera->getNearClippingPlane());
1051 const COLLADAFW::UniqueId& listid = znear->getAnimationList();
1052 Assign_float_animations(listid, AnimCurves, "clip_start");
1057 if (animType->material != 0) {
1058 Material *ma = give_current_material(ob, 1);
1059 if (!ma->adt || !ma->adt->action) act = verify_adt_action((ID *)&ma->id, 1);
1060 else act = ma->adt->action;
1062 ListBase *AnimCurves = &(act->curves);
1064 const COLLADAFW::InstanceGeometryPointerArray& nodeGeoms = node->getInstanceGeometries();
1065 for (unsigned int i = 0; i < nodeGeoms.getCount(); i++) {
1066 const COLLADAFW::MaterialBindingArray& matBinds = nodeGeoms[i]->getMaterialBindings();
1067 for (unsigned int j = 0; j < matBinds.getCount(); j++) {
1068 const COLLADAFW::UniqueId & matuid = matBinds[j].getReferencedMaterial();
1069 const COLLADAFW::Effect *ef = (COLLADAFW::Effect *) (FW_object_map[matuid]);
1070 if (ef != NULL) { /* can be NULL [#28909] */
1071 const COLLADAFW::CommonEffectPointerArray& commonEffects = ef->getCommonEffects();
1072 COLLADAFW::EffectCommon *efc = commonEffects[0];
1073 if ((animType->material & MATERIAL_SHININESS) != 0) {
1074 const COLLADAFW::FloatOrParam *shin = &(efc->getShininess());
1075 const COLLADAFW::UniqueId& listid = shin->getAnimationList();
1076 Assign_float_animations(listid, AnimCurves, "specular_hardness");
1079 if ((animType->material & MATERIAL_IOR) != 0) {
1080 const COLLADAFW::FloatOrParam *ior = &(efc->getIndexOfRefraction());
1081 const COLLADAFW::UniqueId& listid = ior->getAnimationList();
1082 Assign_float_animations(listid, AnimCurves, "raytrace_transparency.ior");
1085 if ((animType->material & MATERIAL_SPEC_COLOR) != 0) {
1086 const COLLADAFW::ColorOrTexture *cot = &(efc->getSpecular());
1087 const COLLADAFW::UniqueId& listid = cot->getColor().getAnimationList();
1088 Assign_color_animations(listid, AnimCurves, "specular_color");
1091 if ((animType->material & MATERIAL_DIFF_COLOR) != 0) {
1092 const COLLADAFW::ColorOrTexture *cot = &(efc->getDiffuse());
1093 const COLLADAFW::UniqueId& listid = cot->getColor().getAnimationList();
1094 Assign_color_animations(listid, AnimCurves, "diffuse_color");
1102 void AnimationImporter::add_bone_animation_sampled(Object *ob, std::vector<FCurve *>& animcurves, COLLADAFW::Node *root, COLLADAFW::Node *node, COLLADAFW::Transformation *tm)
1104 const char *bone_name = bc_get_joint_name(node);
1105 char joint_path[200];
1106 armature_importer->get_rna_path_for_joint(node, joint_path, sizeof(joint_path));
1108 std::vector<float> frames;
1109 find_frames(&frames, &animcurves);
1111 // convert degrees to radians
1112 if (tm->getTransformationType() == COLLADAFW::Transformation::ROTATE) {
1114 std::vector<FCurve *>::iterator iter;
1115 for (iter = animcurves.begin(); iter != animcurves.end(); iter++) {
1116 FCurve *fcu = *iter;
1118 fcurve_deg_to_rad(fcu);
1123 float irest_dae[4][4];
1124 float rest[4][4], irest[4][4];
1126 get_joint_rest_mat(irest_dae, root, node);
1127 invert_m4(irest_dae);
1129 Bone *bone = BKE_armature_find_bone_name((bArmature *)ob->data, bone_name);
1131 fprintf(stderr, "cannot find bone \"%s\"\n", bone_name);
1136 copy_m4_m4(rest, bone->arm_mat);
1137 invert_m4_m4(irest, rest);
1139 // new curves to assign matrix transform animation
1140 FCurve *newcu[10]; // if tm_type is matrix, then create 10 curves: 4 rot, 3 loc, 3 scale
1141 unsigned int totcu = 10;
1142 const char *tm_str = NULL;
1144 for (int i = 0; i < totcu; i++) {
1149 tm_str = "rotation_quaternion";
1153 tm_str = "location";
1162 BLI_snprintf(rna_path, sizeof(rna_path), "%s.%s", joint_path, tm_str);
1164 newcu[i] = create_fcurve(axis, rna_path);
1165 newcu[i]->totvert = frames.size();
1168 if (frames.size() == 0)
1171 std::sort(frames.begin(), frames.end());
1173 std::vector<float>::iterator it;
1175 // sample values at each frame
1176 for (it = frames.begin(); it != frames.end(); it++) {
1184 // calc object-space mat
1185 evaluate_transform_at_frame(matfra, node, fra);
1188 // for joints, we need a special matrix
1189 // special matrix: iR * M * iR_dae * R
1190 // where R, iR are bone rest and inverse rest mats in world space (Blender bones),
1191 // iR_dae is joint inverse rest matrix (DAE) and M is an evaluated joint world-space matrix (DAE)
1192 float temp[4][4], par[4][4];
1196 calc_joint_parent_mat_rest(par, NULL, root, node);
1197 mult_m4_m4m4(temp, par, matfra);
1199 // evaluate_joint_world_transform_at_frame(temp, NULL,, node, fra);
1201 // calc special matrix
1202 mul_serie_m4(mat, irest, temp, irest_dae, rest, NULL, NULL, NULL, NULL);
1204 float rot[4], loc[3], scale[3];
1206 mat4_to_quat(rot, mat);
1207 copy_v3_v3(loc, mat[3]);
1208 mat4_to_size(scale, mat);
1211 for (int i = 0; i < totcu; i++) {
1213 add_bezt(newcu[i], fra, rot[i]);
1215 add_bezt(newcu[i], fra, loc[i - 4]);
1217 add_bezt(newcu[i], fra, scale[i - 7]);
1220 verify_adt_action((ID *)&ob->id, 1);
1223 for (int i = 0; i < totcu; i++) {
1224 add_bone_fcurve(ob, node, newcu[i]);
1227 bPoseChannel *chan = BKE_pose_channel_find_name(ob->pose, bone_name);
1228 chan->rotmode = ROT_MODE_QUAT;
1233 //Check if object is animated by checking if animlist_map holds the animlist_id of node transforms
1234 AnimationImporter::AnimMix *AnimationImporter::get_animation_type(const COLLADAFW::Node *node,
1235 std::map<COLLADAFW::UniqueId, const COLLADAFW::Object *> FW_object_map)
1237 AnimMix *types = new AnimMix();
1239 const COLLADAFW::TransformationPointerArray& nodeTransforms = node->getTransformations();
1241 //for each transformation in node
1242 for (unsigned int i = 0; i < nodeTransforms.getCount(); i++) {
1243 COLLADAFW::Transformation *transform = nodeTransforms[i];
1244 const COLLADAFW::UniqueId& listid = transform->getAnimationList();
1246 //check if transformation has animations
1247 if (animlist_map.find(listid) == animlist_map.end()) {
1251 types->transform = types->transform | NODE_TRANSFORM;
1255 const COLLADAFW::InstanceLightPointerArray& nodeLights = node->getInstanceLights();
1257 for (unsigned int i = 0; i < nodeLights.getCount(); i++) {
1258 const COLLADAFW::Light *light = (COLLADAFW::Light *) FW_object_map[nodeLights[i]->getInstanciatedObjectId()];
1259 types->light = setAnimType(&(light->getColor()), (types->light), LIGHT_COLOR);
1260 types->light = setAnimType(&(light->getFallOffAngle()), (types->light), LIGHT_FOA);
1261 types->light = setAnimType(&(light->getFallOffExponent()), (types->light), LIGHT_FOE);
1263 if (types->light != 0) break;
1267 const COLLADAFW::InstanceCameraPointerArray& nodeCameras = node->getInstanceCameras();
1268 for (unsigned int i = 0; i < nodeCameras.getCount(); i++) {
1269 const COLLADAFW::Camera *camera = (COLLADAFW::Camera *) FW_object_map[nodeCameras[i]->getInstanciatedObjectId()];
1270 if ( camera == NULL ) {
1271 // Can happen if the node refers to an unknown camera.
1275 const bool is_perspective_type = camera->getCameraType() == COLLADAFW::Camera::PERSPECTIVE;
1278 const COLLADAFW::Animatable *mag;
1279 const COLLADAFW::UniqueId listid = camera->getYMag().getAnimationList();
1280 if (animlist_map.find(listid) != animlist_map.end()) {
1281 mag = &(camera->getYMag());
1282 addition = (is_perspective_type) ? CAMERA_YFOV: CAMERA_YMAG;
1285 mag = &(camera->getXMag());
1286 addition = (is_perspective_type) ? CAMERA_XFOV: CAMERA_XMAG;
1288 types->camera = setAnimType(mag, (types->camera), addition);
1290 types->camera = setAnimType(&(camera->getFarClippingPlane()), (types->camera), CAMERA_ZFAR);
1291 types->camera = setAnimType(&(camera->getNearClippingPlane()), (types->camera), CAMERA_ZNEAR);
1293 if (types->camera != 0) break;
1297 const COLLADAFW::InstanceGeometryPointerArray& nodeGeoms = node->getInstanceGeometries();
1298 for (unsigned int i = 0; i < nodeGeoms.getCount(); i++) {
1299 const COLLADAFW::MaterialBindingArray& matBinds = nodeGeoms[i]->getMaterialBindings();
1300 for (unsigned int j = 0; j < matBinds.getCount(); j++) {
1301 const COLLADAFW::UniqueId & matuid = matBinds[j].getReferencedMaterial();
1302 const COLLADAFW::Effect *ef = (COLLADAFW::Effect *) (FW_object_map[matuid]);
1303 if (ef != NULL) { /* can be NULL [#28909] */
1304 const COLLADAFW::CommonEffectPointerArray& commonEffects = ef->getCommonEffects();
1305 if (!commonEffects.empty()) {
1306 COLLADAFW::EffectCommon *efc = commonEffects[0];
1307 types->material = setAnimType(&(efc->getShininess()), (types->material), MATERIAL_SHININESS);
1308 types->material = setAnimType(&(efc->getSpecular().getColor()), (types->material), MATERIAL_SPEC_COLOR);
1309 types->material = setAnimType(&(efc->getDiffuse().getColor()), (types->material), MATERIAL_DIFF_COLOR);
1310 // types->material = setAnimType(&(efc->get()), (types->material), MATERIAL_TRANSPARENCY);
1311 types->material = setAnimType(&(efc->getIndexOfRefraction()), (types->material), MATERIAL_IOR);
1319 int AnimationImporter::setAnimType(const COLLADAFW::Animatable *prop, int types, int addition)
1322 const COLLADAFW::UniqueId& listid = prop->getAnimationList();
1323 if (animlist_map.find(listid) != animlist_map.end())
1324 anim_type = types | addition;
1331 // Is not used anymore.
1332 void AnimationImporter::find_frames_old(std::vector<float> *frames, COLLADAFW::Node *node, COLLADAFW::Transformation::TransformationType tm_type)
1334 bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX;
1335 bool is_rotation = tm_type == COLLADAFW::Transformation::ROTATE;
1336 // for each <rotate>, <translate>, etc. there is a separate Transformation
1337 const COLLADAFW::TransformationPointerArray& nodeTransforms = node->getTransformations();
1340 // find frames at which to sample plus convert all rotation keys to radians
1341 for (i = 0; i < nodeTransforms.getCount(); i++) {
1342 COLLADAFW::Transformation *transform = nodeTransforms[i];
1343 COLLADAFW::Transformation::TransformationType nodeTmType = transform->getTransformationType();
1346 if (nodeTmType == tm_type) {
1347 //get animation bindings for the current transformation
1348 const COLLADAFW::UniqueId& listid = transform->getAnimationList();
1349 //if transform is animated its animlist must exist.
1350 if (animlist_map.find(listid) != animlist_map.end()) {
1352 const COLLADAFW::AnimationList *animlist = animlist_map[listid];
1353 const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings();
1355 if (bindings.getCount()) {
1356 //for each AnimationBinding get the fcurves which animate the transform
1357 for (unsigned int j = 0; j < bindings.getCount(); j++) {
1358 std::vector<FCurve *>& curves = curve_map[bindings[j].animation];
1359 bool xyz = ((nodeTmType == COLLADAFW::Transformation::TRANSLATE || nodeTmType == COLLADAFW::Transformation::SCALE) && bindings[j].animationClass == COLLADAFW::AnimationList::POSITION_XYZ);
1361 if ((!xyz && curves.size() == 1) || (xyz && curves.size() == 3) || is_matrix) {
1362 std::vector<FCurve *>::iterator iter;
1364 for (iter = curves.begin(); iter != curves.end(); iter++) {
1365 FCurve *fcu = *iter;
1367 //if transform is rotation the fcurves values must be turned in to radian.
1369 fcurve_deg_to_rad(fcu);
1371 for (unsigned int k = 0; k < fcu->totvert; k++) {
1372 //get frame value from bezTriple
1373 float fra = fcu->bezt[k].vec[1][0];
1374 //if frame already not added add frame to frames
1375 if (std::find(frames->begin(), frames->end(), fra) == frames->end())
1376 frames->push_back(fra);
1381 fprintf(stderr, "expected %d curves, got %d\n", xyz ? 3 : 1, (int)curves.size());
1393 // animlist_map - map animlist id -> animlist
1394 // curve_map - map anim id -> curve(s)
1395 Object *AnimationImporter::translate_animation_OLD(COLLADAFW::Node *node,
1396 std::map<COLLADAFW::UniqueId, Object *>& object_map,
1397 std::map<COLLADAFW::UniqueId, COLLADAFW::Node *>& root_map,
1398 COLLADAFW::Transformation::TransformationType tm_type,
1402 bool is_rotation = tm_type == COLLADAFW::Transformation::ROTATE;
1403 bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX;
1404 bool is_joint = node->getType() == COLLADAFW::Node::JOINT;
1406 COLLADAFW::Node *root = root_map.find(node->getUniqueId()) == root_map.end() ? node : root_map[node->getUniqueId()];
1407 Object *ob = is_joint ? armature_importer->get_armature_for_joint(node) : object_map[node->getUniqueId()];
1408 const char *bone_name = is_joint ? bc_get_joint_name(node) : NULL;
1410 fprintf(stderr, "cannot find Object for Node with id=\"%s\"\n", node->getOriginalId().c_str());
1414 // frames at which to sample
1415 std::vector<float> frames;
1417 find_frames_old(&frames, node, tm_type);
1421 float irest_dae[4][4];
1422 float rest[4][4], irest[4][4];
1425 get_joint_rest_mat(irest_dae, root, node);
1426 invert_m4(irest_dae);
1428 Bone *bone = BKE_armature_find_bone_name((bArmature *)ob->data, bone_name);
1430 fprintf(stderr, "cannot find bone \"%s\"\n", bone_name);
1435 copy_m4_m4(rest, bone->arm_mat);
1436 invert_m4_m4(irest, rest);
1441 #ifdef ARMATURE_TEST
1442 FCurve *job_curves[10];
1443 job = get_joint_object(root, node, par_job);
1446 if (frames.size() == 0)
1449 std::sort(frames.begin(), frames.end());
1451 const char *tm_str = NULL;
1453 case COLLADAFW::Transformation::ROTATE:
1454 tm_str = "rotation_quaternion";
1456 case COLLADAFW::Transformation::SCALE:
1459 case COLLADAFW::Transformation::TRANSLATE:
1460 tm_str = "location";
1462 case COLLADAFW::Transformation::MATRIX:
1469 char joint_path[200];
1472 armature_importer->get_rna_path_for_joint(node, joint_path, sizeof(joint_path));
1475 FCurve *newcu[10]; // if tm_type is matrix, then create 10 curves: 4 rot, 3 loc, 3 scale
1476 unsigned int totcu = is_matrix ? 10 : (is_rotation ? 4 : 3);
1478 for (i = 0; i < totcu; i++) {
1484 tm_str = "rotation_quaternion";
1488 tm_str = "location";
1498 BLI_snprintf(rna_path, sizeof(rna_path), "%s.%s", joint_path, tm_str);
1500 BLI_strncpy(rna_path, tm_str, sizeof(rna_path));
1501 newcu[i] = create_fcurve(axis, rna_path);
1503 #ifdef ARMATURE_TEST
1505 job_curves[i] = create_fcurve(axis, tm_str);
1509 std::vector<float>::iterator it;
1511 // sample values at each frame
1512 for (it = frames.begin(); it != frames.end(); it++) {
1520 // calc object-space mat
1521 evaluate_transform_at_frame(matfra, node, fra);
1523 // for joints, we need a special matrix
1525 // special matrix: iR * M * iR_dae * R
1526 // where R, iR are bone rest and inverse rest mats in world space (Blender bones),
1527 // iR_dae is joint inverse rest matrix (DAE) and M is an evaluated joint world-space matrix (DAE)
1528 float temp[4][4], par[4][4];
1531 calc_joint_parent_mat_rest(par, NULL, root, node);
1532 mult_m4_m4m4(temp, par, matfra);
1534 // evaluate_joint_world_transform_at_frame(temp, NULL,, node, fra);
1536 // calc special matrix
1537 mul_serie_m4(mat, irest, temp, irest_dae, rest, NULL, NULL, NULL, NULL);
1540 copy_m4_m4(mat, matfra);
1543 float val[4], rot[4], loc[3], scale[3];
1546 case COLLADAFW::Transformation::ROTATE:
1547 mat4_to_quat(val, mat);
1549 case COLLADAFW::Transformation::SCALE:
1550 mat4_to_size(val, mat);
1552 case COLLADAFW::Transformation::TRANSLATE:
1553 copy_v3_v3(val, mat[3]);
1555 case COLLADAFW::Transformation::MATRIX:
1556 mat4_to_quat(rot, mat);
1557 copy_v3_v3(loc, mat[3]);
1558 mat4_to_size(scale, mat);
1565 for (i = 0; i < totcu; i++) {
1568 add_bezt(newcu[i], fra, rot[i]);
1570 add_bezt(newcu[i], fra, loc[i - 4]);
1572 add_bezt(newcu[i], fra, scale[i - 7]);
1575 add_bezt(newcu[i], fra, val[i]);
1579 #ifdef ARMATURE_TEST
1582 case COLLADAFW::Transformation::ROTATE:
1583 mat4_to_quat(val, matfra);
1585 case COLLADAFW::Transformation::SCALE:
1586 mat4_to_size(val, matfra);
1588 case COLLADAFW::Transformation::TRANSLATE:
1589 copy_v3_v3(val, matfra[3]);
1592 mat4_to_quat(rot, matfra);
1593 copy_v3_v3(loc, matfra[3]);
1594 mat4_to_size(scale, matfra);
1600 for (i = 0; i < totcu; i++) {
1603 add_bezt(job_curves[i], fra, rot[i]);
1605 add_bezt(job_curves[i], fra, loc[i - 4]);
1607 add_bezt(job_curves[i], fra, scale[i - 7]);
1610 add_bezt(job_curves[i], fra, val[i]);
1617 verify_adt_action((ID *)&ob->id, 1);
1619 ListBase *curves = &ob->adt->action->curves;
1622 for (i = 0; i < totcu; i++) {
1624 add_bone_fcurve(ob, node, newcu[i]);
1626 BLI_addtail(curves, newcu[i]);
1628 #ifdef ARMATURE_TEST
1630 BLI_addtail(&job->adt->action->curves, job_curves[i]);
1634 if (is_rotation || is_matrix) {
1636 bPoseChannel *chan = BKE_pose_channel_find_name(ob->pose, bone_name);
1637 chan->rotmode = ROT_MODE_QUAT;
1640 ob->rotmode = ROT_MODE_QUAT;
1647 // internal, better make it private
1648 // warning: evaluates only rotation and only assigns matrix transforms now
1649 // prerequisites: animlist_map, curve_map
1650 void AnimationImporter::evaluate_transform_at_frame(float mat[4][4], COLLADAFW::Node *node, float fra)
1652 const COLLADAFW::TransformationPointerArray& tms = node->getTransformations();
1656 for (unsigned int i = 0; i < tms.getCount(); i++) {
1657 COLLADAFW::Transformation *tm = tms[i];
1658 COLLADAFW::Transformation::TransformationType type = tm->getTransformationType();
1663 std::string nodename = node->getName().size() ? node->getName() : node->getOriginalId();
1664 if (!evaluate_animation(tm, m, fra, nodename.c_str())) {
1666 case COLLADAFW::Transformation::ROTATE:
1667 dae_rotate_to_mat4(tm, m);
1669 case COLLADAFW::Transformation::TRANSLATE:
1670 dae_translate_to_mat4(tm, m);
1672 case COLLADAFW::Transformation::SCALE:
1673 dae_scale_to_mat4(tm, m);
1675 case COLLADAFW::Transformation::MATRIX:
1676 dae_matrix_to_mat4(tm, m);
1679 fprintf(stderr, "unsupported transformation type %d\n", type);
1681 // dae_matrix_to_mat4(tm, m);
1686 copy_m4_m4(temp, mat);
1688 mult_m4_m4m4(mat, temp, m);
1692 // return true to indicate that mat contains a sane value
1693 bool AnimationImporter::evaluate_animation(COLLADAFW::Transformation *tm, float mat[4][4], float fra, const char *node_id)
1695 const COLLADAFW::UniqueId& listid = tm->getAnimationList();
1696 COLLADAFW::Transformation::TransformationType type = tm->getTransformationType();
1698 if (type != COLLADAFW::Transformation::ROTATE &&
1699 type != COLLADAFW::Transformation::SCALE &&
1700 type != COLLADAFW::Transformation::TRANSLATE &&
1701 type != COLLADAFW::Transformation::MATRIX) {
1702 fprintf(stderr, "animation of transformation %d is not supported yet\n", type);
1706 if (animlist_map.find(listid) == animlist_map.end())
1709 const COLLADAFW::AnimationList *animlist = animlist_map[listid];
1710 const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings();
1712 if (bindings.getCount()) {
1715 bool is_scale = (type == COLLADAFW::Transformation::SCALE);
1716 bool is_translate = (type == COLLADAFW::Transformation::TRANSLATE);
1719 dae_scale_to_v3(tm, vec);
1720 else if (is_translate)
1721 dae_translate_to_v3(tm, vec);
1723 for (unsigned int j = 0; j < bindings.getCount(); j++) {
1724 const COLLADAFW::AnimationList::AnimationBinding& binding = bindings[j];
1725 std::vector<FCurve *>& curves = curve_map[binding.animation];
1726 COLLADAFW::AnimationList::AnimationClass animclass = binding.animationClass;
1730 case COLLADAFW::Transformation::ROTATE:
1731 BLI_snprintf(path, sizeof(path), "%s.rotate (binding %u)", node_id, j);
1733 case COLLADAFW::Transformation::SCALE:
1734 BLI_snprintf(path, sizeof(path), "%s.scale (binding %u)", node_id, j);
1736 case COLLADAFW::Transformation::TRANSLATE:
1737 BLI_snprintf(path, sizeof(path), "%s.translate (binding %u)", node_id, j);
1739 case COLLADAFW::Transformation::MATRIX:
1740 BLI_snprintf(path, sizeof(path), "%s.matrix (binding %u)", node_id, j);
1746 if (animclass == COLLADAFW::AnimationList::UNKNOWN_CLASS) {
1747 fprintf(stderr, "%s: UNKNOWN animation class\n", path);
1751 if (type == COLLADAFW::Transformation::ROTATE) {
1752 if (curves.size() != 1) {
1753 fprintf(stderr, "expected 1 curve, got %d\n", (int)curves.size());
1757 // TODO support other animclasses
1758 if (animclass != COLLADAFW::AnimationList::ANGLE) {
1759 fprintf(stderr, "%s: animation class %d is not supported yet\n", path, animclass);
1763 COLLADABU::Math::Vector3& axis = ((COLLADAFW::Rotate *)tm)->getRotationAxis();
1765 float ax[3] = {(float)axis[0], (float)axis[1], (float)axis[2]};
1766 float angle = evaluate_fcurve(curves[0], fra);
1767 axis_angle_to_mat4(mat, ax, angle);
1771 else if (is_scale || is_translate) {
1772 bool is_xyz = animclass == COLLADAFW::AnimationList::POSITION_XYZ;
1774 if ((!is_xyz && curves.size() != 1) || (is_xyz && curves.size() != 3)) {
1776 fprintf(stderr, "%s: expected 3 curves, got %d\n", path, (int)curves.size());
1778 fprintf(stderr, "%s: expected 1 curve, got %d\n", path, (int)curves.size());
1782 switch (animclass) {
1783 case COLLADAFW::AnimationList::POSITION_X:
1784 vec[0] = evaluate_fcurve(curves[0], fra);
1786 case COLLADAFW::AnimationList::POSITION_Y:
1787 vec[1] = evaluate_fcurve(curves[0], fra);
1789 case COLLADAFW::AnimationList::POSITION_Z:
1790 vec[2] = evaluate_fcurve(curves[0], fra);
1792 case COLLADAFW::AnimationList::POSITION_XYZ:
1793 vec[0] = evaluate_fcurve(curves[0], fra);
1794 vec[1] = evaluate_fcurve(curves[1], fra);
1795 vec[2] = evaluate_fcurve(curves[2], fra);
1798 fprintf(stderr, "%s: animation class %d is not supported yet\n", path, animclass);
1802 else if (type == COLLADAFW::Transformation::MATRIX) {
1803 // for now, of matrix animation, support only the case when all values are packed into one animation
1804 if (curves.size() != 16) {
1805 fprintf(stderr, "%s: expected 16 curves, got %d\n", path, (int)curves.size());
1809 COLLADABU::Math::Matrix4 matrix;
1812 for (std::vector<FCurve *>::iterator it = curves.begin(); it != curves.end(); it++) {
1813 matrix.setElement(i, j, evaluate_fcurve(*it, fra));
1819 unused_curves.erase(std::remove(unused_curves.begin(), unused_curves.end(), *it), unused_curves.end());
1822 COLLADAFW::Matrix tm(matrix);
1823 dae_matrix_to_mat4(&tm, mat);
1825 std::vector<FCurve *>::iterator it;
1832 size_to_mat4(mat, vec);
1834 copy_v3_v3(mat[3], vec);
1836 return is_scale || is_translate;
1842 // gives a world-space mat of joint at rest position
1843 void AnimationImporter::get_joint_rest_mat(float mat[4][4], COLLADAFW::Node *root, COLLADAFW::Node *node)
1845 // if bind mat is not available,
1846 // use "current" node transform, i.e. all those tms listed inside <node>
1847 if (!armature_importer->get_joint_bind_mat(mat, node)) {
1848 float par[4][4], m[4][4];
1850 calc_joint_parent_mat_rest(par, NULL, root, node);
1851 get_node_mat(m, node, NULL, NULL);
1852 mult_m4_m4m4(mat, par, m);
1856 // gives a world-space mat, end's mat not included
1857 bool AnimationImporter::calc_joint_parent_mat_rest(float mat[4][4], float par[4][4], COLLADAFW::Node *node, COLLADAFW::Node *end)
1862 par ? copy_m4_m4(mat, par) : unit_m4(mat);
1866 // use bind matrix if available or calc "current" world mat
1867 if (!armature_importer->get_joint_bind_mat(m, node)) {
1870 get_node_mat(temp, node, NULL, NULL);
1871 mult_m4_m4m4(m, par, temp);
1874 get_node_mat(m, node, NULL, NULL);
1878 COLLADAFW::NodePointerArray& children = node->getChildNodes();
1879 for (unsigned int i = 0; i < children.getCount(); i++) {
1880 if (calc_joint_parent_mat_rest(mat, m, children[i], end))
1887 #ifdef ARMATURE_TEST
1888 Object *AnimationImporter::get_joint_object(COLLADAFW::Node *root, COLLADAFW::Node *node, Object *par_job)
1890 if (joint_objects.find(node->getUniqueId()) == joint_objects.end()) {
1891 Object *job = bc_add_object(scene, OB_EMPTY, (char *)get_joint_name(node));
1893 job->lay = BKE_scene_base_find(scene, job)->lay = 2;
1895 mul_v3_fl(job->size, 0.5f);
1896 job->recalc |= OB_RECALC_OB;
1898 verify_adt_action((ID *)&job->id, 1);
1900 job->rotmode = ROT_MODE_QUAT;
1903 get_joint_rest_mat(mat, root, node);
1906 float temp[4][4], ipar[4][4];
1907 invert_m4_m4(ipar, par_job->obmat);
1908 copy_m4_m4(temp, mat);
1909 mult_m4_m4m4(mat, ipar, temp);
1912 TransformBase::decompose(mat, job->loc, NULL, job->quat, job->size);
1915 job->parent = par_job;
1917 par_job->recalc |= OB_RECALC_OB;
1918 job->parsubstr[0] = 0;
1921 BKE_object_where_is_calc(scene, job);
1923 // after parenting and layer change
1924 DAG_scene_sort(CTX_data_main(C), scene);
1926 joint_objects[node->getUniqueId()] = job;
1929 return joint_objects[node->getUniqueId()];
1934 // recursively evaluates joint tree until end is found, mat then is world-space matrix of end
1935 // mat must be identity on enter, node must be root
1936 bool AnimationImporter::evaluate_joint_world_transform_at_frame(float mat[4][4], float par[4][4], COLLADAFW::Node *node, COLLADAFW::Node *end, float fra)
1941 evaluate_transform_at_frame(temp, node, node == end ? fra : 0.0f);
1942 mult_m4_m4m4(m, par, temp);
1945 evaluate_transform_at_frame(m, node, node == end ? fra : 0.0f);
1953 COLLADAFW::NodePointerArray& children = node->getChildNodes();
1954 for (int i = 0; i < children.getCount(); i++) {
1955 if (evaluate_joint_world_transform_at_frame(mat, m, children[i], end, fra))
1964 void AnimationImporter::add_bone_fcurve(Object *ob, COLLADAFW::Node *node, FCurve *fcu)
1966 const char *bone_name = bc_get_joint_name(node);
1967 bAction *act = ob->adt->action;
1969 /* try to find group */
1970 bActionGroup *grp = BKE_action_group_find_name(act, bone_name);
1972 /* no matching groups, so add one */
1974 /* Add a new group, and make it active */
1975 grp = (bActionGroup *)MEM_callocN(sizeof(bActionGroup), "bActionGroup");
1977 grp->flag = AGRP_SELECTED;
1978 BLI_strncpy(grp->name, bone_name, sizeof(grp->name));
1980 BLI_addtail(&act->groups, grp);
1981 BLI_uniquename(&act->groups, grp, "Group", '.', offsetof(bActionGroup, name), 64);
1984 /* add F-Curve to group */
1985 action_groups_add_channel(act, grp, fcu);
1988 void AnimationImporter::add_bezt(FCurve *fcu, float fra, float value)
1990 //float fps = (float)FPS;
1992 memset(&bez, 0, sizeof(BezTriple));
1993 bez.vec[1][0] = fra;
1994 bez.vec[1][1] = value;
1995 bez.ipo = BEZT_IPO_LIN; /* use default interpolation mode here... */
1996 bez.f1 = bez.f2 = bez.f3 = SELECT;
1997 bez.h1 = bez.h2 = HD_AUTO;
1998 insert_bezt_fcurve(fcu, &bez, 0);
1999 calchandles_fcurve(fcu);