Merge with trunk r37677
[blender.git] / source / blender / collada / AnimationImporter.cpp
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Nathan Letwory.
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file blender/collada/AnimationImporter.cpp
26  *  \ingroup collada
27  */
28
29 #include <stddef.h>
30
31 /* COLLADABU_ASSERT, may be able to remove later */
32 #include "COLLADABUPlatform.h"
33
34 #include "DNA_armature_types.h"
35
36 #include "ED_keyframing.h"
37
38 #include "BLI_listbase.h"
39 #include "BLI_math.h"
40 #include "BLI_path_util.h"
41 #include "BLI_string.h"
42
43 #include "BKE_action.h"
44 #include "BKE_armature.h"
45 #include "BKE_fcurve.h"
46 #include "BKE_object.h"
47
48 #include "MEM_guardedalloc.h"
49
50 #include "collada_utils.h"
51 #include "AnimationImporter.h"
52 #include "ArmatureImporter.h"
53
54 #include <algorithm>
55
56 // first try node name, if not available (since is optional), fall back to original id
57 template<class T>
58 static const char *bc_get_joint_name(T *node)
59 {
60         const std::string& id = node->getName();
61         return id.size() ? id.c_str() : node->getOriginalId().c_str();
62 }
63
64 FCurve *AnimationImporter::create_fcurve(int array_index, const char *rna_path)
65 {
66         FCurve *fcu = (FCurve*)MEM_callocN(sizeof(FCurve), "FCurve");
67         
68         fcu->flag = (FCURVE_VISIBLE|FCURVE_AUTO_HANDLES|FCURVE_SELECTED);
69         fcu->rna_path = BLI_strdupn(rna_path, strlen(rna_path));
70         fcu->array_index = array_index;
71         return fcu;
72 }
73         
74 void AnimationImporter::create_bezt(FCurve *fcu, float frame, float output)
75 {
76         BezTriple bez;
77         memset(&bez, 0, sizeof(BezTriple));
78         bez.vec[1][0] = frame;
79         bez.vec[1][1] = output;
80         bez.ipo = U.ipo_new; /* use default interpolation mode here... */
81         bez.f1 = bez.f2 = bez.f3 = SELECT;
82         bez.h1 = bez.h2 = HD_AUTO;
83         insert_bezt_fcurve(fcu, &bez, 0);
84         calchandles_fcurve(fcu);
85 }
86
87 // create one or several fcurves depending on the number of parameters being animated
88 void AnimationImporter::animation_to_fcurves(COLLADAFW::AnimationCurve *curve)
89 {
90         COLLADAFW::FloatOrDoubleArray& input = curve->getInputValues();
91         COLLADAFW::FloatOrDoubleArray& output = curve->getOutputValues();
92    
93         if( curve->getInterpolationType() == COLLADAFW::AnimationCurve::INTERPOLATION_BEZIER ) {
94         COLLADAFW::FloatOrDoubleArray& intan = curve->getInTangentValues();
95     COLLADAFW::FloatOrDoubleArray& outtan = curve->getOutTangentValues();
96         }
97         float fps = (float)FPS;
98         size_t dim = curve->getOutDimension();
99         unsigned int i;
100         
101         std::vector<FCurve*>& fcurves = curve_map[curve->getUniqueId()];
102
103         switch (dim) {
104         case 1: // X, Y, Z or angle
105         case 3: // XYZ
106         case 16: // matrix
107                 {
108                         for (i = 0; i < dim; i++ ) {
109                                 FCurve *fcu = (FCurve*)MEM_callocN(sizeof(FCurve), "FCurve");
110                         
111                                 fcu->flag = (FCURVE_VISIBLE|FCURVE_AUTO_HANDLES|FCURVE_SELECTED);
112                                 // fcu->rna_path = BLI_strdupn(path, strlen(path));
113                                 fcu->array_index = 0;
114                                 fcu->totvert = curve->getKeyCount();
115                         
116                                 // create beztriple for each key
117                                 for (unsigned int j = 0; j < curve->getKeyCount(); j++) {
118                                         BezTriple bez;
119                                         memset(&bez, 0, sizeof(BezTriple));
120
121                                         
122                                         // input, output
123                                         bez.vec[1][0] = bc_get_float_value(input, j) * fps; 
124                                         bez.vec[1][1] = bc_get_float_value(output, j * dim + i);
125
126
127                                         if( curve->getInterpolationType() == COLLADAFW::AnimationCurve::INTERPOLATION_BEZIER ) 
128                                         {
129                                                 COLLADAFW::FloatOrDoubleArray& intan = curve->getInTangentValues();
130                         COLLADAFW::FloatOrDoubleArray& outtan = curve->getOutTangentValues();
131
132                                                 // intangent
133                                                  bez.vec[0][0] = bc_get_float_value(intan, (j * 2 * dim ) + (2 * i)) * fps;
134                                                  bez.vec[0][1] = bc_get_float_value(intan, (j * 2 * dim )+ (2 * i) + 1);
135
136                                                  // outtangent
137                                                  bez.vec[2][0] = bc_get_float_value(outtan, (j * 2 * dim ) + (2 * i)) * fps;
138                                                  bez.vec[2][1] = bc_get_float_value(outtan, (j * 2 * dim )+ (2 * i) + 1);
139                                              bez.ipo = BEZT_IPO_BEZ;
140                                                  //bez.h1 = bez.h2 = HD_AUTO;   
141                                         }
142                                         else 
143                                         {
144                                                 bez.h1 = bez.h2 = HD_AUTO; 
145                                                 bez.ipo = BEZT_IPO_LIN;
146                                         }
147                                         // bez.ipo = U.ipo_new; /* use default interpolation mode here... */
148                                         bez.f1 = bez.f2 = bez.f3 = SELECT;
149                                         
150                                         insert_bezt_fcurve(fcu, &bez, 0);
151                                 }
152
153                                 calchandles_fcurve(fcu);
154
155                                 fcurves.push_back(fcu);
156                         }
157                 }
158                 break;
159         default:
160                 fprintf(stderr, "Output dimension of %d is not yet supported (animation id = %s)\n", (int)dim, curve->getOriginalId().c_str());
161         }
162
163         for (std::vector<FCurve*>::iterator it = fcurves.begin(); it != fcurves.end(); it++)
164                 unused_curves.push_back(*it);
165 }
166
167
168 void AnimationImporter::fcurve_deg_to_rad(FCurve *cu)
169 {
170         for (unsigned int i = 0; i < cu->totvert; i++) {
171                 // TODO convert handles too
172                 cu->bezt[i].vec[1][1] *= M_PI / 180.0f;
173                 cu->bezt[i].vec[0][1] *= M_PI / 180.0f;
174                 cu->bezt[i].vec[2][1] *= M_PI / 180.0f;
175                 cu->bezt[i].vec[1][0];
176         }
177 }
178
179 void AnimationImporter::add_fcurves_to_object(Object *ob, std::vector<FCurve*>& curves, char *rna_path, int array_index, Animation *animated)
180 {
181         bAction *act;
182         
183         if (!ob->adt || !ob->adt->action) act = verify_adt_action((ID*)&ob->id, 1);
184         else act = ob->adt->action;
185         
186         std::vector<FCurve*>::iterator it;
187         int i;
188
189 #if 0
190         char *p = strstr(rna_path, "rotation_euler");
191         bool is_rotation = p && *(p + strlen("rotation_euler")) == '\0';
192
193         // convert degrees to radians for rotation
194         if (is_rotation)
195                 fcurve_deg_to_rad(fcu);
196 #endif
197         
198         for (it = curves.begin(), i = 0; it != curves.end(); it++, i++) {
199                 FCurve *fcu = *it;
200                 fcu->rna_path = BLI_strdupn(rna_path, strlen(rna_path));
201                 
202                 if (array_index == -1) fcu->array_index = i;
203                 else fcu->array_index = array_index;
204         
205                 if (ob->type == OB_ARMATURE) {
206                         bActionGroup *grp = NULL;
207                         const char *bone_name = bc_get_joint_name(animated->node);
208                         
209                         if (bone_name) {
210                                 /* try to find group */
211                                 grp = action_groups_find_named(act, bone_name);
212                                 
213                                 /* no matching groups, so add one */
214                                 if (grp == NULL) {
215                                         /* Add a new group, and make it active */
216                                         grp = (bActionGroup*)MEM_callocN(sizeof(bActionGroup), "bActionGroup");
217                                         
218                                         grp->flag = AGRP_SELECTED;
219                                         BLI_strncpy(grp->name, bone_name, sizeof(grp->name));
220                                         
221                                         BLI_addtail(&act->groups, grp);
222                                         BLI_uniquename(&act->groups, grp, "Group", '.', offsetof(bActionGroup, name), 64);
223                                 }
224                                 
225                                 /* add F-Curve to group */
226                                 action_groups_add_channel(act, grp, fcu);
227                                 
228                         }
229 #if 0
230                         if (is_rotation) {
231                                 fcurves_actionGroup_map[grp].push_back(fcu);
232                         }
233 #endif
234                 }
235                 else {
236                         BLI_addtail(&act->curves, fcu);
237                 }
238
239                 // curve is used, so remove it from unused_curves
240                 unused_curves.erase(std::remove(unused_curves.begin(), unused_curves.end(), fcu), unused_curves.end());
241         }
242 }
243
244 AnimationImporter::AnimationImporter(UnitConverter *conv, ArmatureImporter *arm, Scene *scene) :
245                 TransformReader(conv), armature_importer(arm), scene(scene) { }
246
247 AnimationImporter::~AnimationImporter()
248 {
249         // free unused FCurves
250         for (std::vector<FCurve*>::iterator it = unused_curves.begin(); it != unused_curves.end(); it++)
251                 free_fcurve(*it);
252
253         if (unused_curves.size())
254                 fprintf(stderr, "removed %d unused curves\n", (int)unused_curves.size());
255 }
256
257 bool AnimationImporter::write_animation(const COLLADAFW::Animation* anim) 
258 {
259         if (anim->getAnimationType() == COLLADAFW::Animation::ANIMATION_CURVE) {
260                 COLLADAFW::AnimationCurve *curve = (COLLADAFW::AnimationCurve*)anim;
261                 
262                 // XXX Don't know if it's necessary
263                 // Should we check outPhysicalDimension?
264                 if (curve->getInPhysicalDimension() != COLLADAFW::PHYSICAL_DIMENSION_TIME) {
265                         fprintf(stderr, "Inputs physical dimension is not time. \n");
266                         return true;
267                 }
268
269                 // a curve can have mixed interpolation type,
270                 // in this case curve->getInterpolationTypes returns a list of interpolation types per key
271                 COLLADAFW::AnimationCurve::InterpolationType interp = curve->getInterpolationType();
272
273                 if (interp != COLLADAFW::AnimationCurve::INTERPOLATION_MIXED) {
274                         switch (interp) {
275                         case COLLADAFW::AnimationCurve::INTERPOLATION_LINEAR:
276                         case COLLADAFW::AnimationCurve::INTERPOLATION_BEZIER:
277                                 animation_to_fcurves(curve);
278                                 break;
279                         default:
280                                 // TODO there're also CARDINAL, HERMITE, BSPLINE and STEP types
281                                 fprintf(stderr, "CARDINAL, HERMITE, BSPLINE and STEP anim interpolation types not supported yet.\n");
282                                 break;
283                         }
284                 }
285                 else {
286                         // not supported yet
287                         fprintf(stderr, "MIXED anim interpolation type is not supported yet.\n");
288                 }
289         }
290         else {
291                 fprintf(stderr, "FORMULA animation type is not supported yet.\n");
292         }
293         
294         return true;
295 }
296         
297 // called on post-process stage after writeVisualScenes
298 bool AnimationImporter::write_animation_list(const COLLADAFW::AnimationList* animlist) 
299 {
300         const COLLADAFW::UniqueId& animlist_id = animlist->getUniqueId();
301     
302         animlist_map[animlist_id] = animlist;
303     
304 #if 0
305
306         // should not happen
307         if (uid_animated_map.find(animlist_id) == uid_animated_map.end()) {
308                 return true;
309         }
310
311         // for bones rna_path is like: pose.bones["bone-name"].rotation
312         
313         // what does this AnimationList animate?
314         Animation& animated = uid_animated_map[animlist_id];
315         Object *ob = animated.ob;
316     
317         char rna_path[100];
318         char joint_path[100];
319         bool is_joint = false;
320
321         // if ob is NULL, it should be a JOINT
322         if (!ob) {
323                 
324                 ob = armature_importer->get_armature_for_joint(animated.node);
325
326                 if (!ob) {
327 //                      fprintf(stderr, "Cannot find armature for node %s\n", get_joint_name(animated.node));
328                         return true;
329                 }
330
331                 armature_importer->get_rna_path_for_joint(animated.node, joint_path, sizeof(joint_path));
332
333                 is_joint = true;
334         }
335         printf("object for animlist: %s found\n", animlist->getUniqueId().toAscii().c_str());
336         const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings();
337
338         switch (animated.tm->getTransformationType()) {
339         case COLLADAFW::Transformation::TRANSLATE:
340         case COLLADAFW::Transformation::SCALE:
341                 {
342                         bool loc = animated.tm->getTransformationType() == COLLADAFW::Transformation::TRANSLATE;
343                         if (is_joint)
344                                 BLI_snprintf(rna_path, sizeof(rna_path), "%s.%s", joint_path, loc ? "location" : "scale");
345                         else
346                                 BLI_strncpy(rna_path, loc ? "location" : "scale", sizeof(rna_path));
347
348                         for (int i = 0; i < bindings.getCount(); i++) {
349                                 const COLLADAFW::AnimationList::AnimationBinding& binding = bindings[i];
350                                 COLLADAFW::UniqueId anim_uid = binding.animation;
351
352                                 if (curve_map.find(anim_uid) == curve_map.end()) {
353                                         fprintf(stderr, "Cannot find FCurve by animation UID.\n");
354                                         continue;
355                                 }
356
357                                 std::vector<FCurve*>& fcurves = curve_map[anim_uid];
358                                 
359                                 switch (binding.animationClass) {
360                                 case COLLADAFW::AnimationList::POSITION_X:
361                                         add_fcurves_to_object(ob, fcurves, rna_path, 0, &animated);
362                                         break;
363                                 case COLLADAFW::AnimationList::POSITION_Y:
364                                         add_fcurves_to_object(ob, fcurves, rna_path, 1, &animated);
365                                         break;
366                                 case COLLADAFW::AnimationList::POSITION_Z:
367                                         add_fcurves_to_object(ob, fcurves, rna_path, 2, &animated);
368                                         break;
369                                 case COLLADAFW::AnimationList::POSITION_XYZ:
370                                         add_fcurves_to_object(ob, fcurves, rna_path, -1, &animated);
371                                         break;
372                                 default:
373                                         fprintf(stderr, "AnimationClass %d is not supported for %s.\n",
374                                                         binding.animationClass, loc ? "TRANSLATE" : "SCALE");
375                                 }
376                         }
377                 }
378                 break;
379         case COLLADAFW::Transformation::ROTATE:
380                 {
381                         if (is_joint)
382                                 BLI_snprintf(rna_path, sizeof(rna_path), "%s.rotation_euler", joint_path);
383                         else
384                                 BLI_strncpy(rna_path, "rotation_euler", sizeof(rna_path));
385
386                         COLLADAFW::Rotate* rot = (COLLADAFW::Rotate*)animated.tm;
387                         COLLADABU::Math::Vector3& axis = rot->getRotationAxis();
388                         
389                         for (int i = 0; i < bindings.getCount(); i++) {
390                                 const COLLADAFW::AnimationList::AnimationBinding& binding = bindings[i];
391                                 COLLADAFW::UniqueId anim_uid = binding.animation;
392
393                                 if (curve_map.find(anim_uid) == curve_map.end()) {
394                                         fprintf(stderr, "Cannot find FCurve by animation UID.\n");
395                                         continue;
396                                 }
397
398                                 std::vector<FCurve*>& fcurves = curve_map[anim_uid];
399
400                                 switch (binding.animationClass) {
401                                 case COLLADAFW::AnimationList::ANGLE:
402                                         if (COLLADABU::Math::Vector3::UNIT_X == axis) {
403                                                 add_fcurves_to_object(ob, fcurves, rna_path, 0, &animated);
404                                         }
405                                         else if (COLLADABU::Math::Vector3::UNIT_Y == axis) {
406                                                 add_fcurves_to_object(ob, fcurves, rna_path, 1, &animated);
407                                         }
408                                         else if (COLLADABU::Math::Vector3::UNIT_Z == axis) {
409                                                 add_fcurves_to_object(ob, fcurves, rna_path, 2, &animated);
410                                         }
411                                         break;
412                                 case COLLADAFW::AnimationList::AXISANGLE:
413                                         // TODO convert axis-angle to quat? or XYZ?
414                                 default:
415                                         fprintf(stderr, "AnimationClass %d is not supported for ROTATE transformation.\n",
416                                                         binding.animationClass);
417                                 }
418                         }
419                 }
420                 break;
421         case COLLADAFW::Transformation::MATRIX:
422         case COLLADAFW::Transformation::SKEW:
423         case COLLADAFW::Transformation::LOOKAT:
424                 fprintf(stderr, "Animation of MATRIX, SKEW and LOOKAT transformations is not supported yet.\n");
425                 break;
426         }
427 #endif
428         
429         return true;
430 }
431
432 // \todo refactor read_node_transform to not automatically apply anything,
433 // but rather return the transform matrix, so caller can do with it what is
434 // necessary. Same for \ref get_node_mat
435 void AnimationImporter::read_node_transform(COLLADAFW::Node *node, Object *ob)
436 {
437         float mat[4][4];
438         TransformReader::get_node_mat(mat, node, &uid_animated_map, ob);
439         if (ob) {
440                 copy_m4_m4(ob->obmat, mat);
441                 object_apply_mat4(ob, ob->obmat, 0, 0);
442         }
443 }
444
445 #if 0
446 virtual void AnimationImporter::change_eul_to_quat(Object *ob, bAction *act)
447 {
448         bActionGroup *grp;
449         int i;
450         
451         for (grp = (bActionGroup*)act->groups.first; grp; grp = grp->next) {
452
453                 FCurve *eulcu[3] = {NULL, NULL, NULL};
454                 
455                 if (fcurves_actionGroup_map.find(grp) == fcurves_actionGroup_map.end())
456                         continue;
457
458                 std::vector<FCurve*> &rot_fcurves = fcurves_actionGroup_map[grp];
459                 
460                 if (rot_fcurves.size() > 3) continue;
461
462                 for (i = 0; i < rot_fcurves.size(); i++)
463                         eulcu[rot_fcurves[i]->array_index] = rot_fcurves[i];
464
465                 char joint_path[100];
466                 char rna_path[100];
467
468                 BLI_snprintf(joint_path, sizeof(joint_path), "pose.bones[\"%s\"]", grp->name);
469                 BLI_snprintf(rna_path, sizeof(rna_path), "%s.rotation_quaternion", joint_path);
470
471                 FCurve *quatcu[4] = {
472                         create_fcurve(0, rna_path),
473                         create_fcurve(1, rna_path),
474                         create_fcurve(2, rna_path),
475                         create_fcurve(3, rna_path)
476                 };
477
478                 bPoseChannel *chan = get_pose_channel(ob->pose, grp->name);
479
480                 float m4[4][4], irest[3][3];
481                 invert_m4_m4(m4, chan->bone->arm_mat);
482                 copy_m3_m4(irest, m4);
483
484                 for (i = 0; i < 3; i++) {
485
486                         FCurve *cu = eulcu[i];
487
488                         if (!cu) continue;
489
490                         for (int j = 0; j < cu->totvert; j++) {
491                                 float frame = cu->bezt[j].vec[1][0];
492
493                                 float eul[3] = {
494                                         eulcu[0] ? evaluate_fcurve(eulcu[0], frame) : 0.0f,
495                                         eulcu[1] ? evaluate_fcurve(eulcu[1], frame) : 0.0f,
496                                         eulcu[2] ? evaluate_fcurve(eulcu[2], frame) : 0.0f
497                                 };
498
499                                 // make eul relative to bone rest pose
500                                 float rot[3][3], rel[3][3], quat[4];
501
502                                 /*eul_to_mat3(rot, eul);
503
504                                 mul_m3_m3m3(rel, irest, rot);
505
506                                 mat3_to_quat(quat, rel);
507                                 */
508
509                                 eul_to_quat(quat, eul);
510
511                                 for (int k = 0; k < 4; k++)
512                                         create_bezt(quatcu[k], frame, quat[k]);
513                         }
514                 }
515
516                 // now replace old Euler curves
517
518                 for (i = 0; i < 3; i++) {
519                         if (!eulcu[i]) continue;
520
521                         action_groups_remove_channel(act, eulcu[i]);
522                         free_fcurve(eulcu[i]);
523                 }
524
525                 chan->rotmode = ROT_MODE_QUAT;
526
527                 for (i = 0; i < 4; i++)
528                         action_groups_add_channel(act, grp, quatcu[i]);
529         }
530
531         bPoseChannel *pchan;
532         for (pchan = (bPoseChannel*)ob->pose->chanbase.first; pchan; pchan = pchan->next) {
533                 pchan->rotmode = ROT_MODE_QUAT;
534         }
535 }
536 #endif
537
538
539 //sets the rna_path and array index to curve
540 void AnimationImporter::modify_fcurve(std::vector<FCurve*>* curves , char* rna_path , int array_index )
541 {   
542         std::vector<FCurve*>::iterator it;
543         int i;
544         for (it = curves->begin(), i = 0; it != curves->end(); it++, i++) {
545                 FCurve *fcu = *it;
546                 fcu->rna_path = BLI_strdupn(rna_path, strlen(rna_path));
547                 
548                 if (array_index == -1) fcu->array_index = i;
549                 else fcu->array_index = array_index;
550
551                 unused_curves.erase(std::remove(unused_curves.begin(), unused_curves.end(), fcu), unused_curves.end());
552         }
553 }
554
555 void AnimationImporter::find_frames( std::vector<float>* frames , std::vector<FCurve*>* curves)
556 {
557         std::vector<FCurve*>::iterator iter;
558                 for (iter = curves->begin(); iter != curves->end(); iter++) {
559                         FCurve *fcu = *iter;
560         
561                 for (unsigned int k = 0; k < fcu->totvert; k++) {
562                         //get frame value from bezTriple
563                         float fra = fcu->bezt[k].vec[1][0];
564                         //if frame already not added add frame to frames
565                         if (std::find(frames->begin(), frames->end(), fra) == frames->end())
566                                 frames->push_back(fra);
567                                                         
568                 }
569                 }
570 }
571
572 //creates the rna_paths and array indices of fcurves from animations using transformation and bound animation class of each animation.
573 void AnimationImporter:: Assign_transform_animations(std::vector<float>* frames, 
574                                                                                                          COLLADAFW::Transformation * transform , 
575                                                                                                          const COLLADAFW::AnimationList::AnimationBinding * binding,
576                                                                                                          std::vector<FCurve*>* curves, bool is_joint, char * joint_path)
577 {
578         COLLADAFW::Transformation::TransformationType tm_type = transform->getTransformationType();
579         bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX;
580         bool is_rotation = tm_type  == COLLADAFW::Transformation::ROTATE;
581         
582         //to check if the no of curves are valid
583         bool xyz = ((tm_type == COLLADAFW::Transformation::TRANSLATE ||tm_type  == COLLADAFW::Transformation::SCALE) && binding->animationClass == COLLADAFW::AnimationList::POSITION_XYZ);
584                         
585         
586         if (!((!xyz && curves->size() == 1) || (xyz && curves->size() == 3) || is_matrix)) {
587                 fprintf(stderr, "expected %d curves, got %d\n", xyz ? 3 : 1, (int)curves->size());
588                 return;
589         }
590     
591         //find key frames of the animation and accumulates them to frames of the transformation.
592         find_frames (frames , curves );
593
594         char rna_path[100];
595         //char joint_path[100];
596         
597                                                 
598         switch (tm_type) {
599                 case COLLADAFW::Transformation::TRANSLATE:
600                 case COLLADAFW::Transformation::SCALE:
601                         {
602                                 bool loc = tm_type == COLLADAFW::Transformation::TRANSLATE;
603                                 if (is_joint)
604                                         BLI_snprintf(rna_path, sizeof(rna_path), "%s.%s", joint_path, loc ? "location" : "scale");
605                                 else
606                                         BLI_strncpy(rna_path, loc ? "location" : "scale", sizeof(rna_path));
607
608                                 switch (binding->animationClass) {
609                                         case COLLADAFW::AnimationList::POSITION_X:
610                                                 modify_fcurve(curves, rna_path, 0 );
611                                                 break;
612                                         case COLLADAFW::AnimationList::POSITION_Y:
613                                                 modify_fcurve(curves, rna_path, 1 );
614                                                 break;
615                                         case COLLADAFW::AnimationList::POSITION_Z:
616                                                 modify_fcurve(curves, rna_path, 2 );
617                                                 break;
618                                         case COLLADAFW::AnimationList::POSITION_XYZ:
619                                                 modify_fcurve(curves, rna_path, -1 );
620                                                 break;
621                                         default:
622                                                 fprintf(stderr, "AnimationClass %d is not supported for %s.\n",
623                                                                 binding->animationClass, loc ? "TRANSLATE" : "SCALE");
624                                         }
625                 break;
626                         }
627                 
628                 
629                 case COLLADAFW::Transformation::ROTATE:
630                         {
631                                 if (is_joint)
632                                         BLI_snprintf(rna_path, sizeof(rna_path), "%s.rotation_euler", joint_path);
633                                 else
634                                         BLI_strncpy(rna_path, "rotation_euler", sizeof(rna_path));
635                 std::vector<FCurve*>::iterator iter;
636                                 for (iter = curves->begin(); iter != curves->end(); iter++) {
637                                         FCurve* fcu = *iter;
638                                         
639                                         //if transform is rotation the fcurves values must be turned in to radian.
640                                         if (is_rotation)
641                                                 fcurve_deg_to_rad(fcu);          
642                                 }                                       
643                                 COLLADAFW::Rotate* rot = (COLLADAFW::Rotate*)transform;
644                                 COLLADABU::Math::Vector3& axis = rot->getRotationAxis();
645                         
646                                 switch (binding->animationClass) {
647                                         case COLLADAFW::AnimationList::ANGLE:
648                                                 if (COLLADABU::Math::Vector3::UNIT_X == axis) {
649                                                         modify_fcurve(curves, rna_path, 0 );
650                                                 }
651                                                 else if (COLLADABU::Math::Vector3::UNIT_Y == axis) {
652                                                         modify_fcurve(curves, rna_path, 1 );
653                                                 }
654                                                 else if (COLLADABU::Math::Vector3::UNIT_Z == axis) {
655                                                         modify_fcurve(curves, rna_path, 2 );
656                                                 }
657                                                 break;
658                                         case COLLADAFW::AnimationList::AXISANGLE:
659                                                 // TODO convert axis-angle to quat? or XYZ?
660                                         default:
661                                                 fprintf(stderr, "AnimationClass %d is not supported for ROTATE transformation.\n",
662                                                                 binding->animationClass);
663                                         }
664                         break;
665                         }
666                         
667                 case COLLADAFW::Transformation::MATRIX:
668                 case COLLADAFW::Transformation::SKEW:
669                 case COLLADAFW::Transformation::LOOKAT:
670                         fprintf(stderr, "Animation of MATRIX, SKEW and LOOKAT transformations is not supported yet.\n");
671                         break;
672                 }
673         
674 }
675
676 void AnimationImporter::translate_Animations_NEW ( COLLADAFW::Node * node , 
677                                                                                                    std::map<COLLADAFW::UniqueId, COLLADAFW::Node*>& root_map,
678                                                                                                    std::map<COLLADAFW::UniqueId, Object*>& object_map )
679 {
680         bool is_joint = node->getType() == COLLADAFW::Node::JOINT;
681         
682         COLLADAFW::Node *root = root_map.find(node->getUniqueId()) == root_map.end() ? node : root_map[node->getUniqueId()];
683         Object *ob = is_joint ? armature_importer->get_armature_for_joint(root) : object_map[node->getUniqueId()];
684         
685         const char *bone_name = is_joint ? bc_get_joint_name(node) : NULL;
686     
687         if ( ! is_object_animated(node) ) return ;  
688
689     char joint_path[200];
690
691         if ( is_joint ) 
692         armature_importer->get_rna_path_for_joint(node, joint_path, sizeof(joint_path));
693         
694         bAction * act;
695         bActionGroup *grp = NULL;
696     
697         if (!ob->adt || !ob->adt->action) act = verify_adt_action((ID*)&ob->id, 1);
698                     else act = ob->adt->action;
699                                 //Get the list of animation curves of the object
700     
701         ListBase *AnimCurves = &(act->curves);
702         
703         if (!ob) {
704                 fprintf(stderr, "cannot find Object for Node with id=\"%s\"\n", node->getOriginalId().c_str());
705                 return;
706         }
707
708         
709         /*float irest_dae[4][4];
710         float rest[4][4], irest[4][4];
711
712         if (is_joint) {
713                 get_joint_rest_mat(irest_dae, root, node);
714                 invert_m4(irest_dae);
715
716                 Bone *bone = get_named_bone((bArmature*)ob->data, bone_name);
717                 if (!bone) {
718                         fprintf(stderr, "cannot find bone \"%s\"\n", bone_name);
719                         return;
720                 }
721
722                 unit_m4(rest);
723                 copy_m4_m4(rest, bone->arm_mat);
724                 invert_m4_m4(irest, rest);
725         }*/
726     
727         const COLLADAFW::TransformationPointerArray& nodeTransforms = node->getTransformations();
728         
729         //for each transformation in node 
730         for (unsigned int i = 0; i < nodeTransforms.getCount(); i++) {
731                 COLLADAFW::Transformation *transform = nodeTransforms[i];
732                 COLLADAFW::Transformation::TransformationType tm_type = transform->getTransformationType();
733
734                 bool is_rotation = tm_type == COLLADAFW::Transformation::ROTATE;
735                 bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX;
736                         
737                 const COLLADAFW::UniqueId& listid = transform->getAnimationList();
738                 
739                 //might not be needed
740                 std::vector<float> frames;
741                         
742                 //check if transformation has animations    
743                 if (animlist_map.find(listid) == animlist_map.end()) continue ; 
744                 else 
745                         {
746                                 //transformation has animations
747                                 const COLLADAFW::AnimationList *animlist = animlist_map[listid];
748                                 const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings();
749                 //all the curves belonging to the current binding
750                                 std::vector<FCurve*> animcurves;    
751                                 for (unsigned int j = 0; j < bindings.getCount(); j++) {
752                                          animcurves = curve_map[bindings[j].animation];
753                                         //calculate rnapaths and array index of fcurves according to transformation and animation class
754                                          Assign_transform_animations(&frames,transform, &bindings[j], &animcurves, is_joint, joint_path ); 
755                                         
756                                          std::vector<FCurve*>::iterator iter;
757                                     //Add the curves of the current animation to the object
758                                                 for (iter = animcurves.begin(); iter != animcurves.end(); iter++) {
759                                                         FCurve * fcu = *iter;
760                                                          if (ob->type == OB_ARMATURE) 
761                                                                 add_bone_fcurve( ob, node , fcu );
762                                                          else 
763                                                          BLI_addtail(AnimCurves, fcu);  
764                                                 }                               
765                                 }
766                          std::sort(frames.begin(), frames.end());
767                         }
768                 if (is_rotation || is_matrix) {
769                         if (is_joint) 
770                         {
771                                 bPoseChannel *chan = get_pose_channel(ob->pose, bone_name);
772                                 chan->rotmode = ROT_MODE_EUL;
773                         }
774                         else 
775                         {
776                                 ob->rotmode = ROT_MODE_EUL;
777                         }
778                 }
779         }
780         
781 }
782
783 //Check if object is animated by checking if animlist_map holds the animlist_id of node transforms
784 bool AnimationImporter::is_object_animated ( const COLLADAFW::Node * node ) 
785 {
786         bool exists = false;
787         const COLLADAFW::TransformationPointerArray& nodeTransforms = node->getTransformations();
788         
789         //for each transformation in node 
790         for (unsigned int i = 0; i < nodeTransforms.getCount(); i++) {
791                 COLLADAFW::Transformation *transform = nodeTransforms[i];
792                 const COLLADAFW::UniqueId& listid = transform->getAnimationList();
793                 
794                 //check if transformation has animations    
795                 if (animlist_map.find(listid) == animlist_map.end()) continue ;
796                 else 
797                 {
798                         exists = true;
799                         break;
800                 }
801         }
802
803         return exists;
804 }
805
806 //XXX Is not used anymore.
807 void AnimationImporter::find_frames_old(std::vector<float> * frames, COLLADAFW::Node * node , COLLADAFW::Transformation::TransformationType tm_type)
808 {
809         bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX;
810         bool is_rotation = tm_type == COLLADAFW::Transformation::ROTATE;
811         // for each <rotate>, <translate>, etc. there is a separate Transformation
812         const COLLADAFW::TransformationPointerArray& nodeTransforms = node->getTransformations();
813
814         unsigned int i;
815         // find frames at which to sample plus convert all rotation keys to radians
816         for (i = 0; i < nodeTransforms.getCount(); i++) {
817                 COLLADAFW::Transformation *transform = nodeTransforms[i];
818                 COLLADAFW::Transformation::TransformationType nodeTmType = transform->getTransformationType();
819
820
821                 if (nodeTmType == tm_type) {
822                         //get animation bindings for the current transformation
823                         const COLLADAFW::UniqueId& listid = transform->getAnimationList();
824                         //if transform is animated its animlist must exist.
825                         if (animlist_map.find(listid) != animlist_map.end()) {
826                                 
827                                 const COLLADAFW::AnimationList *animlist = animlist_map[listid];
828                                 const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings();
829                 
830                                 if (bindings.getCount()) {
831                                         //for each AnimationBinding get the fcurves which animate the transform
832                                         for (unsigned int j = 0; j < bindings.getCount(); j++) {
833                                                 std::vector<FCurve*>& curves = curve_map[bindings[j].animation];
834                                                 bool xyz = ((nodeTmType == COLLADAFW::Transformation::TRANSLATE || nodeTmType == COLLADAFW::Transformation::SCALE) && bindings[j].animationClass == COLLADAFW::AnimationList::POSITION_XYZ);
835
836                                                 if ((!xyz && curves.size() == 1) || (xyz && curves.size() == 3) || is_matrix) {
837                                                         std::vector<FCurve*>::iterator iter;
838
839                                                         for (iter = curves.begin(); iter != curves.end(); iter++) {
840                                                                 FCurve *fcu = *iter;
841                                 
842                                                                 //if transform is rotation the fcurves values must be turned in to radian.
843                                                                 if (is_rotation)
844                                                                         fcurve_deg_to_rad(fcu);
845
846                                                                 for (unsigned int k = 0; k < fcu->totvert; k++) {
847                                                                         //get frame value from bezTriple
848                                                                         float fra = fcu->bezt[k].vec[1][0];
849                                                                         //if frame already not added add frame to frames
850                                                                         if (std::find(frames->begin(), frames->end(), fra) == frames->end())
851                                                                                 frames->push_back(fra);
852                                                                 }
853                                                         }
854                                                 }
855                                                 else {
856                                                         fprintf(stderr, "expected %d curves, got %d\n", xyz ? 3 : 1, (int)curves.size());
857                                                 }
858                                         }
859                                 }
860                         }
861                 }
862         }
863 }
864
865
866 // prerequisites:
867 // animlist_map - map animlist id -> animlist
868 // curve_map - map anim id -> curve(s)
869 Object *AnimationImporter::translate_animation(COLLADAFW::Node *node,
870                                                         std::map<COLLADAFW::UniqueId, Object*>& object_map,
871                                                         std::map<COLLADAFW::UniqueId, COLLADAFW::Node*>& root_map,
872                                                         COLLADAFW::Transformation::TransformationType tm_type,
873                                                         Object *par_job)
874 {
875         
876         bool is_rotation = tm_type == COLLADAFW::Transformation::ROTATE;
877         bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX;
878         bool is_joint = node->getType() == COLLADAFW::Node::JOINT;
879         
880         COLLADAFW::Node *root = root_map.find(node->getUniqueId()) == root_map.end() ? node : root_map[node->getUniqueId()];
881         Object *ob = is_joint ? armature_importer->get_armature_for_joint(node) : object_map[node->getUniqueId()];
882         const char *bone_name = is_joint ? bc_get_joint_name(node) : NULL;
883         if (!ob) {
884                 fprintf(stderr, "cannot find Object for Node with id=\"%s\"\n", node->getOriginalId().c_str());
885                 return NULL;
886         }
887
888         // frames at which to sample
889         std::vector<float> frames;
890         
891         find_frames_old(&frames, node , tm_type);
892         
893         unsigned int i;
894         
895         float irest_dae[4][4];
896         float rest[4][4], irest[4][4];
897
898         if (is_joint) {
899                 get_joint_rest_mat(irest_dae, root, node);
900                 invert_m4(irest_dae);
901
902                 Bone *bone = get_named_bone((bArmature*)ob->data, bone_name);
903                 if (!bone) {
904                         fprintf(stderr, "cannot find bone \"%s\"\n", bone_name);
905                         return NULL;
906                 }
907
908                 unit_m4(rest);
909                 copy_m4_m4(rest, bone->arm_mat);
910                 invert_m4_m4(irest, rest);
911         }
912
913         Object *job = NULL;
914
915 #ifdef ARMATURE_TEST
916         FCurve *job_curves[10];
917         job = get_joint_object(root, node, par_job);
918 #endif
919
920         if (frames.size() == 0)
921                 return job;
922
923         std::sort(frames.begin(), frames.end());
924
925         const char *tm_str = NULL;
926         switch (tm_type) {
927         case COLLADAFW::Transformation::ROTATE:
928                 tm_str = "rotation_quaternion";
929                 break;
930         case COLLADAFW::Transformation::SCALE:
931                 tm_str = "scale";
932                 break;
933         case COLLADAFW::Transformation::TRANSLATE:
934                 tm_str = "location";
935                 break;
936         case COLLADAFW::Transformation::MATRIX:
937                 break;
938         default:
939                 return job;
940         }
941
942         char rna_path[200];
943         char joint_path[200];
944
945         if (is_joint)
946                 armature_importer->get_rna_path_for_joint(node, joint_path, sizeof(joint_path));
947
948         // new curves
949         FCurve *newcu[10]; // if tm_type is matrix, then create 10 curves: 4 rot, 3 loc, 3 scale
950         unsigned int totcu = is_matrix ? 10 : (is_rotation ? 4 : 3);
951
952         for (i = 0; i < totcu; i++) {
953
954                 int axis = i;
955
956                 if (is_matrix) {
957                         if (i < 4) {
958                                 tm_str = "rotation_quaternion";
959                                 axis = i;
960                         }
961                         else if (i < 7) {
962                                 tm_str = "location";
963                                 axis = i - 4;
964                         }
965                         else {
966                                 tm_str = "scale";
967                                 axis = i - 7;
968                         }
969                 }
970
971                 if (is_joint)
972                         BLI_snprintf(rna_path, sizeof(rna_path), "%s.%s", joint_path, tm_str);
973                 else
974                         strcpy(rna_path, tm_str);
975                 newcu[i] = create_fcurve(axis, rna_path);
976
977 #ifdef ARMATURE_TEST
978                 if (is_joint)
979                         job_curves[i] = create_fcurve(axis, tm_str);
980 #endif
981         }
982
983         std::vector<float>::iterator it;
984
985         // sample values at each frame
986         for (it = frames.begin(); it != frames.end(); it++) {
987                 float fra = *it;
988
989                 float mat[4][4];
990                 float matfra[4][4];
991
992                 unit_m4(matfra);
993
994                 // calc object-space mat
995                 evaluate_transform_at_frame(matfra, node, fra);
996
997                 // for joints, we need a special matrix
998                 if (is_joint) {
999                         // special matrix: iR * M * iR_dae * R
1000                         // where R, iR are bone rest and inverse rest mats in world space (Blender bones),
1001                         // iR_dae is joint inverse rest matrix (DAE) and M is an evaluated joint world-space matrix (DAE)
1002                         float temp[4][4], par[4][4];
1003
1004                         // calc M
1005                         calc_joint_parent_mat_rest(par, NULL, root, node);
1006                         mul_m4_m4m4(temp, matfra, par);
1007
1008                         // evaluate_joint_world_transform_at_frame(temp, NULL, , node, fra);
1009
1010                         // calc special matrix
1011                         mul_serie_m4(mat, irest, temp, irest_dae, rest, NULL, NULL, NULL, NULL);
1012                 }
1013                 else {
1014                         copy_m4_m4(mat, matfra);
1015                 }
1016
1017                 float val[4], rot[4], loc[3], scale[3];
1018
1019                 switch (tm_type) {
1020                 case COLLADAFW::Transformation::ROTATE:
1021                         mat4_to_quat(val, mat);
1022                         break;
1023                 case COLLADAFW::Transformation::SCALE:
1024                         mat4_to_size(val, mat);
1025                         break;
1026                 case COLLADAFW::Transformation::TRANSLATE:
1027                         copy_v3_v3(val, mat[3]);
1028                         break;
1029                 case COLLADAFW::Transformation::MATRIX:
1030                         mat4_to_quat(rot, mat);
1031                         copy_v3_v3(loc, mat[3]);
1032                         mat4_to_size(scale, mat);
1033                         break;
1034                 default:
1035                         break;
1036                 }
1037
1038                 // add keys
1039                 for (i = 0; i < totcu; i++) {
1040                         if (is_matrix) {
1041                                 if (i < 4)
1042                                         add_bezt(newcu[i], fra, rot[i]);
1043                                 else if (i < 7)
1044                                         add_bezt(newcu[i], fra, loc[i - 4]);
1045                                 else
1046                                         add_bezt(newcu[i], fra, scale[i - 7]);
1047                         }
1048                         else {
1049                                 add_bezt(newcu[i], fra, val[i]);
1050                         }
1051                 }
1052
1053 #ifdef ARMATURE_TEST
1054                 if (is_joint) {
1055                         switch (tm_type) {
1056                         case COLLADAFW::Transformation::ROTATE:
1057                                 mat4_to_quat(val, matfra);
1058                                 break;
1059                         case COLLADAFW::Transformation::SCALE:
1060                                 mat4_to_size(val, matfra);
1061                                 break;
1062                         case COLLADAFW::Transformation::TRANSLATE:
1063                                 copy_v3_v3(val, matfra[3]);
1064                                 break;
1065                         case MATRIX:
1066                                 mat4_to_quat(rot, matfra);
1067                                 copy_v3_v3(loc, matfra[3]);
1068                                 mat4_to_size(scale, matfra);
1069                                 break;
1070                         default:
1071                                 break;
1072                         }
1073
1074                         for (i = 0; i < totcu; i++) {
1075                                 if (is_matrix) {
1076                                         if (i < 4)
1077                                                 add_bezt(job_curves[i], fra, rot[i]);
1078                                         else if (i < 7)
1079                                                 add_bezt(job_curves[i], fra, loc[i - 4]);
1080                                         else
1081                                                 add_bezt(job_curves[i], fra, scale[i - 7]);
1082                                 }
1083                                 else {
1084                                         add_bezt(job_curves[i], fra, val[i]);
1085                                 }
1086                         }
1087                 }
1088 #endif
1089         }
1090
1091         verify_adt_action((ID*)&ob->id, 1);
1092
1093         ListBase *curves = &ob->adt->action->curves;
1094
1095         // add curves
1096         for (i = 0; i < totcu; i++) {
1097                 if (is_joint)
1098                         add_bone_fcurve(ob, node, newcu[i]);
1099                 else
1100                         BLI_addtail(curves, newcu[i]);
1101
1102 #ifdef ARMATURE_TEST
1103                 if (is_joint)
1104                         BLI_addtail(&job->adt->action->curves, job_curves[i]);
1105 #endif
1106         }
1107
1108         if (is_rotation || is_matrix) {
1109                 if (is_joint) {
1110                         bPoseChannel *chan = get_pose_channel(ob->pose, bone_name);
1111                         chan->rotmode = ROT_MODE_QUAT;
1112                 }
1113                 else {
1114                         ob->rotmode = ROT_MODE_QUAT;
1115                 }
1116         }
1117
1118         return job;
1119 }
1120
1121 // internal, better make it private
1122 // warning: evaluates only rotation
1123 // prerequisites: animlist_map, curve_map
1124 void AnimationImporter::evaluate_transform_at_frame(float mat[4][4], COLLADAFW::Node *node, float fra)
1125 {
1126         const COLLADAFW::TransformationPointerArray& tms = node->getTransformations();
1127
1128         unit_m4(mat);
1129
1130         for (unsigned int i = 0; i < tms.getCount(); i++) {
1131                 COLLADAFW::Transformation *tm = tms[i];
1132                 COLLADAFW::Transformation::TransformationType type = tm->getTransformationType();
1133                 float m[4][4];
1134
1135                 unit_m4(m);
1136
1137                 std::string nodename = node->getName().size() ? node->getName() : node->getOriginalId();
1138                 if (!evaluate_animation(tm, m, fra, nodename.c_str())) {
1139                         switch (type) {
1140                         case COLLADAFW::Transformation::ROTATE:
1141                                 dae_rotate_to_mat4(tm, m);
1142                                 break;
1143                         case COLLADAFW::Transformation::TRANSLATE:
1144                                 dae_translate_to_mat4(tm, m);
1145                                 break;
1146                         case COLLADAFW::Transformation::SCALE:
1147                                 dae_scale_to_mat4(tm, m);
1148                                 break;
1149                         case COLLADAFW::Transformation::MATRIX:
1150                                 dae_matrix_to_mat4(tm, m);
1151                                 break;
1152                         default:
1153                                 fprintf(stderr, "unsupported transformation type %d\n", type);
1154                         }
1155                 }
1156
1157                 float temp[4][4];
1158                 copy_m4_m4(temp, mat);
1159
1160                 mul_m4_m4m4(mat, m, temp);
1161         }
1162 }
1163
1164 // return true to indicate that mat contains a sane value
1165 bool AnimationImporter::evaluate_animation(COLLADAFW::Transformation *tm, float mat[4][4], float fra, const char *node_id)
1166 {
1167         const COLLADAFW::UniqueId& listid = tm->getAnimationList();
1168         COLLADAFW::Transformation::TransformationType type = tm->getTransformationType();
1169
1170         if (type != COLLADAFW::Transformation::ROTATE &&
1171             type != COLLADAFW::Transformation::SCALE &&
1172             type != COLLADAFW::Transformation::TRANSLATE &&
1173             type != COLLADAFW::Transformation::MATRIX) {
1174                 fprintf(stderr, "animation of transformation %d is not supported yet\n", type);
1175                 return false;
1176         }
1177
1178         if (animlist_map.find(listid) == animlist_map.end())
1179                 return false;
1180
1181         const COLLADAFW::AnimationList *animlist = animlist_map[listid];
1182         const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings();
1183
1184         if (bindings.getCount()) {
1185                 float vec[3];
1186
1187                 bool is_scale = (type == COLLADAFW::Transformation::SCALE);
1188                 bool is_translate = (type == COLLADAFW::Transformation::TRANSLATE);
1189
1190                 if (type == COLLADAFW::Transformation::SCALE)
1191                         dae_scale_to_v3(tm, vec);
1192                 else if (type == COLLADAFW::Transformation::TRANSLATE)
1193                         dae_translate_to_v3(tm, vec);
1194
1195                 for (unsigned int j = 0; j < bindings.getCount(); j++) {
1196                         const COLLADAFW::AnimationList::AnimationBinding& binding = bindings[j];
1197                         std::vector<FCurve*>& curves = curve_map[binding.animation];
1198                         COLLADAFW::AnimationList::AnimationClass animclass = binding.animationClass;
1199                         char path[100];
1200
1201                         switch (type) {
1202                         case COLLADAFW::Transformation::ROTATE:
1203                                 BLI_snprintf(path, sizeof(path), "%s.rotate (binding %u)", node_id, j);
1204                                 break;
1205                         case COLLADAFW::Transformation::SCALE:
1206                                 BLI_snprintf(path, sizeof(path), "%s.scale (binding %u)", node_id, j);
1207                                 break;
1208                         case COLLADAFW::Transformation::TRANSLATE:
1209                                 BLI_snprintf(path, sizeof(path), "%s.translate (binding %u)", node_id, j);
1210                                 break;
1211                         case COLLADAFW::Transformation::MATRIX:
1212                                 BLI_snprintf(path, sizeof(path), "%s.matrix (binding %u)", node_id, j);
1213                                 break;
1214                         default:
1215                                 break;
1216                         }
1217
1218                         if (animclass == COLLADAFW::AnimationList::UNKNOWN_CLASS) {
1219                                 fprintf(stderr, "%s: UNKNOWN animation class\n", path);
1220                                 continue;
1221                         }
1222
1223                         if (type == COLLADAFW::Transformation::ROTATE) {
1224                                 if (curves.size() != 1) {
1225                                         fprintf(stderr, "expected 1 curve, got %d\n", (int)curves.size());
1226                                         return false;
1227                                 }
1228
1229                                 // TODO support other animclasses
1230                                 if (animclass != COLLADAFW::AnimationList::ANGLE) {
1231                                         fprintf(stderr, "%s: animation class %d is not supported yet\n", path, animclass);
1232                                         return false;
1233                                 }
1234
1235                                 COLLADABU::Math::Vector3& axis = ((COLLADAFW::Rotate*)tm)->getRotationAxis();
1236                                 float ax[3] = {axis[0], axis[1], axis[2]};
1237                                 float angle = evaluate_fcurve(curves[0], fra);
1238                                 axis_angle_to_mat4(mat, ax, angle);
1239
1240                                 return true;
1241                         }
1242                         else if (is_scale || is_translate) {
1243                                 bool is_xyz = animclass == COLLADAFW::AnimationList::POSITION_XYZ;
1244
1245                                 if ((!is_xyz && curves.size() != 1) || (is_xyz && curves.size() != 3)) {
1246                                         if (is_xyz)
1247                                                 fprintf(stderr, "%s: expected 3 curves, got %d\n", path, (int)curves.size());
1248                                         else
1249                                                 fprintf(stderr, "%s: expected 1 curve, got %d\n", path, (int)curves.size());
1250                                         return false;
1251                                 }
1252                                 
1253                                 switch (animclass) {
1254                                 case COLLADAFW::AnimationList::POSITION_X:
1255                                         vec[0] = evaluate_fcurve(curves[0], fra);
1256                                         break;
1257                                 case COLLADAFW::AnimationList::POSITION_Y:
1258                                         vec[1] = evaluate_fcurve(curves[0], fra);
1259                                         break;
1260                                 case COLLADAFW::AnimationList::POSITION_Z:
1261                                         vec[2] = evaluate_fcurve(curves[0], fra);
1262                                         break;
1263                                 case COLLADAFW::AnimationList::POSITION_XYZ:
1264                                         vec[0] = evaluate_fcurve(curves[0], fra);
1265                                         vec[1] = evaluate_fcurve(curves[1], fra);
1266                                         vec[2] = evaluate_fcurve(curves[2], fra);
1267                                         break;
1268                                 default:
1269                                         fprintf(stderr, "%s: animation class %d is not supported yet\n", path, animclass);
1270                                         break;
1271                                 }
1272                         }
1273                         else if (type == COLLADAFW::Transformation::MATRIX) {
1274                                 // for now, of matrix animation, support only the case when all values are packed into one animation
1275                                 if (curves.size() != 16) {
1276                                         fprintf(stderr, "%s: expected 16 curves, got %d\n", path, (int)curves.size());
1277                                         return false;
1278                                 }
1279
1280                                 COLLADABU::Math::Matrix4 matrix;
1281                                 int i = 0, j = 0;
1282
1283                                 for (std::vector<FCurve*>::iterator it = curves.begin(); it != curves.end(); it++) {
1284                                         matrix.setElement(i, j, evaluate_fcurve(*it, fra));
1285                                         j++;
1286                                         if (j == 4) {
1287                                                 i++;
1288                                                 j = 0;
1289                                         }
1290                                 }
1291
1292                                 COLLADAFW::Matrix tm(matrix);
1293                                 dae_matrix_to_mat4(&tm, mat);
1294
1295                                 return true;
1296                         }
1297                 }
1298
1299                 if (is_scale)
1300                         size_to_mat4(mat, vec);
1301                 else
1302                         copy_v3_v3(mat[3], vec);
1303
1304                 return is_scale || is_translate;
1305         }
1306
1307         return false;
1308 }
1309
1310 // gives a world-space mat of joint at rest position
1311 void AnimationImporter::get_joint_rest_mat(float mat[4][4], COLLADAFW::Node *root, COLLADAFW::Node *node)
1312 {
1313         // if bind mat is not available,
1314         // use "current" node transform, i.e. all those tms listed inside <node>
1315         if (!armature_importer->get_joint_bind_mat(mat, node)) {
1316                 float par[4][4], m[4][4];
1317
1318                 calc_joint_parent_mat_rest(par, NULL, root, node);
1319                 get_node_mat(m, node, NULL, NULL);
1320                 mul_m4_m4m4(mat, m, par);
1321         }
1322 }
1323
1324 // gives a world-space mat, end's mat not included
1325 bool AnimationImporter::calc_joint_parent_mat_rest(float mat[4][4], float par[4][4], COLLADAFW::Node *node, COLLADAFW::Node *end)
1326 {
1327         float m[4][4];
1328
1329         if (node == end) {
1330                 par ? copy_m4_m4(mat, par) : unit_m4(mat);
1331                 return true;
1332         }
1333
1334         // use bind matrix if available or calc "current" world mat
1335         if (!armature_importer->get_joint_bind_mat(m, node)) {
1336                 if (par) {
1337                         float temp[4][4];
1338                         get_node_mat(temp, node, NULL, NULL);
1339                         mul_m4_m4m4(m, temp, par);
1340                 }
1341                 else {
1342                         get_node_mat(m, node, NULL, NULL);
1343                 }
1344         }
1345
1346         COLLADAFW::NodePointerArray& children = node->getChildNodes();
1347         for (unsigned int i = 0; i < children.getCount(); i++) {
1348                 if (calc_joint_parent_mat_rest(mat, m, children[i], end))
1349                         return true;
1350         }
1351
1352         return false;
1353 }
1354
1355 #ifdef ARMATURE_TEST
1356 Object *AnimationImporter::get_joint_object(COLLADAFW::Node *root, COLLADAFW::Node *node, Object *par_job)
1357 {
1358         if (joint_objects.find(node->getUniqueId()) == joint_objects.end()) {
1359                 Object *job = add_object(scene, OB_EMPTY);
1360
1361                 rename_id((ID*)&job->id, (char*)get_joint_name(node));
1362
1363                 job->lay = object_in_scene(job, scene)->lay = 2;
1364
1365                 mul_v3_fl(job->size, 0.5f);
1366                 job->recalc |= OB_RECALC_OB;
1367
1368                 verify_adt_action((ID*)&job->id, 1);
1369
1370                 job->rotmode = ROT_MODE_QUAT;
1371
1372                 float mat[4][4];
1373                 get_joint_rest_mat(mat, root, node);
1374
1375                 if (par_job) {
1376                         float temp[4][4], ipar[4][4];
1377                         invert_m4_m4(ipar, par_job->obmat);
1378                         copy_m4_m4(temp, mat);
1379                         mul_m4_m4m4(mat, temp, ipar);
1380                 }
1381
1382                 TransformBase::decompose(mat, job->loc, NULL, job->quat, job->size);
1383
1384                 if (par_job) {
1385                         job->parent = par_job;
1386
1387                         par_job->recalc |= OB_RECALC_OB;
1388                         job->parsubstr[0] = 0;
1389                 }
1390
1391                 where_is_object(scene, job);
1392
1393                 // after parenting and layer change
1394                 DAG_scene_sort(CTX_data_main(C), scene);
1395
1396                 joint_objects[node->getUniqueId()] = job;
1397         }
1398
1399         return joint_objects[node->getUniqueId()];
1400 }
1401 #endif
1402
1403 #if 0
1404 // recursively evaluates joint tree until end is found, mat then is world-space matrix of end
1405 // mat must be identity on enter, node must be root
1406 bool AnimationImporter::evaluate_joint_world_transform_at_frame(float mat[4][4], float par[4][4], COLLADAFW::Node *node, COLLADAFW::Node *end, float fra)
1407 {
1408         float m[4][4];
1409         if (par) {
1410                 float temp[4][4];
1411                 evaluate_transform_at_frame(temp, node, node == end ? fra : 0.0f);
1412                 mul_m4_m4m4(m, temp, par);
1413         }
1414         else {
1415                 evaluate_transform_at_frame(m, node, node == end ? fra : 0.0f);
1416         }
1417
1418         if (node == end) {
1419                 copy_m4_m4(mat, m);
1420                 return true;
1421         }
1422         else {
1423                 COLLADAFW::NodePointerArray& children = node->getChildNodes();
1424                 for (int i = 0; i < children.getCount(); i++) {
1425                         if (evaluate_joint_world_transform_at_frame(mat, m, children[i], end, fra))
1426                                 return true;
1427                 }
1428         }
1429
1430         return false;
1431 }
1432 #endif
1433
1434 void AnimationImporter::add_bone_fcurve(Object *ob, COLLADAFW::Node *node, FCurve *fcu)
1435 {
1436         const char *bone_name = bc_get_joint_name(node);
1437         bAction *act = ob->adt->action;
1438                         
1439         /* try to find group */
1440         bActionGroup *grp = action_groups_find_named(act, bone_name);
1441
1442         /* no matching groups, so add one */
1443         if (grp == NULL) {
1444                 /* Add a new group, and make it active */
1445                 grp = (bActionGroup*)MEM_callocN(sizeof(bActionGroup), "bActionGroup");
1446                                         
1447                 grp->flag = AGRP_SELECTED;
1448                 BLI_strncpy(grp->name, bone_name, sizeof(grp->name));
1449                                         
1450                 BLI_addtail(&act->groups, grp);
1451                 BLI_uniquename(&act->groups, grp, "Group", '.', offsetof(bActionGroup, name), 64);
1452         }
1453                                 
1454         /* add F-Curve to group */
1455         action_groups_add_channel(act, grp, fcu);
1456 }
1457
1458 void AnimationImporter::add_bezt(FCurve *fcu, float fra, float value)
1459 {
1460         BezTriple bez;
1461         memset(&bez, 0, sizeof(BezTriple));
1462         bez.vec[1][0] = fra;
1463         bez.vec[1][1] = value;
1464         bez.ipo = U.ipo_new; /* use default interpolation mode here... */
1465         bez.f1 = bez.f2 = bez.f3 = SELECT;
1466         bez.h1 = bez.h2 = HD_AUTO;
1467         insert_bezt_fcurve(fcu, &bez, 0);
1468         calchandles_fcurve(fcu);
1469 }