transform matrix animation import fix.
[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 #include "MaterialExporter.h"
54
55 #include <algorithm>
56
57 // first try node name, if not available (since is optional), fall back to original id
58 template<class T>
59 static const char *bc_get_joint_name(T *node)
60 {
61         const std::string& id = node->getName();
62         return id.size() ? id.c_str() : node->getOriginalId().c_str();
63 }
64
65 FCurve *AnimationImporter::create_fcurve(int array_index, const char *rna_path)
66 {
67         FCurve *fcu = (FCurve*)MEM_callocN(sizeof(FCurve), "FCurve");
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
98         float fps = (float)FPS;
99         size_t dim = curve->getOutDimension();
100         unsigned int i;
101         
102         std::vector<FCurve*>& fcurves = curve_map[curve->getUniqueId()];
103
104         switch (dim) {
105         case 1: // X, Y, Z or angle
106         case 3: // XYZ
107         case 4:
108         case 16: // matrix
109                 {
110                         for (i = 0; i < dim; i++ ) {
111                                 FCurve *fcu = (FCurve*)MEM_callocN(sizeof(FCurve), "FCurve");
112                         
113                                 fcu->flag = (FCURVE_VISIBLE|FCURVE_AUTO_HANDLES|FCURVE_SELECTED);
114                                 // fcu->rna_path = BLI_strdupn(path, strlen(path));
115                                 fcu->array_index = 0;
116                                 fcu->totvert = curve->getKeyCount();
117                         
118                                 // create beztriple for each key
119                                 for (unsigned int j = 0; j < curve->getKeyCount(); j++) {
120                                         BezTriple bez;
121                                         memset(&bez, 0, sizeof(BezTriple));
122
123                                         
124                                         // input, output
125                                         bez.vec[1][0] = bc_get_float_value(input, j) * fps; 
126                                         bez.vec[1][1] = bc_get_float_value(output, j * dim + i);
127
128
129                                         if( curve->getInterpolationType() == COLLADAFW::AnimationCurve::INTERPOLATION_BEZIER ) 
130                                         {
131                                                 COLLADAFW::FloatOrDoubleArray& intan = curve->getInTangentValues();
132                         COLLADAFW::FloatOrDoubleArray& outtan = curve->getOutTangentValues();
133
134                                                 // intangent
135                                                  bez.vec[0][0] = bc_get_float_value(intan, (j * 2 * dim ) + (2 * i)) * fps;
136                                                  bez.vec[0][1] = bc_get_float_value(intan, (j * 2 * dim )+ (2 * i) + 1);
137
138                                                  // outtangent
139                                                  bez.vec[2][0] = bc_get_float_value(outtan, (j * 2 * dim ) + (2 * i)) * fps;
140                                                  bez.vec[2][1] = bc_get_float_value(outtan, (j * 2 * dim )+ (2 * i) + 1);
141                                              bez.ipo = BEZT_IPO_BEZ;
142                                                  //bez.h1 = bez.h2 = HD_AUTO;   
143                                         }
144                                         else 
145                                         {
146                                                 bez.h1 = bez.h2 = HD_AUTO; 
147                                                 bez.ipo = BEZT_IPO_LIN;
148                                         }
149                                         // bez.ipo = U.ipo_new; /* use default interpolation mode here... */
150                                         bez.f1 = bez.f2 = bez.f3 = SELECT;
151                                         
152                                         insert_bezt_fcurve(fcu, &bez, 0);
153                                 }
154
155                                 calchandles_fcurve(fcu);
156
157                                 fcurves.push_back(fcu);
158                         }
159                 }
160                 break;
161         default:
162                 fprintf(stderr, "Output dimension of %d is not yet supported (animation id = %s)\n", (int)dim, curve->getOriginalId().c_str());
163         }
164
165         for (std::vector<FCurve*>::iterator it = fcurves.begin(); it != fcurves.end(); it++)
166                 unused_curves.push_back(*it);
167 }
168
169
170 void AnimationImporter::fcurve_deg_to_rad(FCurve *cu)
171 {
172         for (unsigned int i = 0; i < cu->totvert; i++) {
173                 // TODO convert handles too
174                 cu->bezt[i].vec[1][1] *= M_PI / 180.0f;
175                 cu->bezt[i].vec[0][1] *= M_PI / 180.0f;
176                 cu->bezt[i].vec[2][1] *= M_PI / 180.0f;
177                 cu->bezt[i].vec[1][0];
178         }
179 }
180
181 void AnimationImporter::add_fcurves_to_object(Object *ob, std::vector<FCurve*>& curves, char *rna_path, int array_index, Animation *animated)
182 {
183         bAction *act;
184         
185         if (!ob->adt || !ob->adt->action) act = verify_adt_action((ID*)&ob->id, 1);
186         else act = ob->adt->action;
187         
188         std::vector<FCurve*>::iterator it;
189         int i;
190
191 #if 0
192         char *p = strstr(rna_path, "rotation_euler");
193         bool is_rotation = p && *(p + strlen("rotation_euler")) == '\0';
194
195         // convert degrees to radians for rotation
196         if (is_rotation)
197                 fcurve_deg_to_rad(fcu);
198 #endif
199         
200         for (it = curves.begin(), i = 0; it != curves.end(); it++, i++) {
201                 FCurve *fcu = *it;
202                 fcu->rna_path = BLI_strdupn(rna_path, strlen(rna_path));
203                 
204                 if (array_index == -1) fcu->array_index = i;
205                 else fcu->array_index = array_index;
206         
207                 if (ob->type == OB_ARMATURE) {
208                         bActionGroup *grp = NULL;
209                         const char *bone_name = bc_get_joint_name(animated->node);
210                         
211                         if (bone_name) {
212                                 /* try to find group */
213                                 grp = action_groups_find_named(act, bone_name);
214                                 
215                                 /* no matching groups, so add one */
216                                 if (grp == NULL) {
217                                         /* Add a new group, and make it active */
218                                         grp = (bActionGroup*)MEM_callocN(sizeof(bActionGroup), "bActionGroup");
219                                         
220                                         grp->flag = AGRP_SELECTED;
221                                         BLI_strncpy(grp->name, bone_name, sizeof(grp->name));
222                                         
223                                         BLI_addtail(&act->groups, grp);
224                                         BLI_uniquename(&act->groups, grp, "Group", '.', offsetof(bActionGroup, name), 64);
225                                 }
226                                 
227                                 /* add F-Curve to group */
228                                 action_groups_add_channel(act, grp, fcu);
229                                 
230                         }
231 #if 0
232                         if (is_rotation) {
233                                 fcurves_actionGroup_map[grp].push_back(fcu);
234                         }
235 #endif
236                 }
237                 else {
238                         BLI_addtail(&act->curves, fcu);
239                 }
240
241                 // curve is used, so remove it from unused_curves
242                 unused_curves.erase(std::remove(unused_curves.begin(), unused_curves.end(), fcu), unused_curves.end());
243         }
244 }
245
246 AnimationImporter::AnimationImporter(UnitConverter *conv, ArmatureImporter *arm, Scene *scene) :
247                 TransformReader(conv), armature_importer(arm), scene(scene) { }
248
249 AnimationImporter::~AnimationImporter()
250 {
251         // free unused FCurves
252         for (std::vector<FCurve*>::iterator it = unused_curves.begin(); it != unused_curves.end(); it++)
253                 free_fcurve(*it);
254
255         if (unused_curves.size())
256                 fprintf(stderr, "removed %d unused curves\n", (int)unused_curves.size());
257 }
258
259 bool AnimationImporter::write_animation(const COLLADAFW::Animation* anim) 
260 {
261         if (anim->getAnimationType() == COLLADAFW::Animation::ANIMATION_CURVE) {
262                 COLLADAFW::AnimationCurve *curve = (COLLADAFW::AnimationCurve*)anim;
263                 
264                 // XXX Don't know if it's necessary
265                 // Should we check outPhysicalDimension?
266                 if (curve->getInPhysicalDimension() != COLLADAFW::PHYSICAL_DIMENSION_TIME) {
267                         fprintf(stderr, "Inputs physical dimension is not time. \n");
268                         return true;
269                 }
270
271                 // a curve can have mixed interpolation type,
272                 // in this case curve->getInterpolationTypes returns a list of interpolation types per key
273                 COLLADAFW::AnimationCurve::InterpolationType interp = curve->getInterpolationType();
274
275                 if (interp != COLLADAFW::AnimationCurve::INTERPOLATION_MIXED) {
276                         switch (interp) {
277                         case COLLADAFW::AnimationCurve::INTERPOLATION_LINEAR:
278                         case COLLADAFW::AnimationCurve::INTERPOLATION_BEZIER:
279                                 animation_to_fcurves(curve);
280                                 break;
281                         default:
282                                 // TODO there're also CARDINAL, HERMITE, BSPLINE and STEP types
283                                 fprintf(stderr, "CARDINAL, HERMITE, BSPLINE and STEP anim interpolation types not supported yet.\n");
284                                 break;
285                         }
286                 }
287                 else {
288                         // not supported yet
289                         fprintf(stderr, "MIXED anim interpolation type is not supported yet.\n");
290                 }
291         }
292         else {
293                 fprintf(stderr, "FORMULA animation type is not supported yet.\n");
294         }
295         
296         return true;
297 }
298         
299 // called on post-process stage after writeVisualScenes
300 bool AnimationImporter::write_animation_list(const COLLADAFW::AnimationList* animlist) 
301 {
302         const COLLADAFW::UniqueId& animlist_id = animlist->getUniqueId();
303     
304         animlist_map[animlist_id] = animlist;
305     
306 #if 0
307
308         // should not happen
309         if (uid_animated_map.find(animlist_id) == uid_animated_map.end()) {
310                 return true;
311         }
312
313         // for bones rna_path is like: pose.bones["bone-name"].rotation
314         
315         // what does this AnimationList animate?
316         Animation& animated = uid_animated_map[animlist_id];
317         Object *ob = animated.ob;
318     
319         char rna_path[100];
320         char joint_path[100];
321         bool is_joint = false;
322
323         // if ob is NULL, it should be a JOINT
324         if (!ob) {
325                 
326                 ob = armature_importer->get_armature_for_joint(animated.node);
327
328                 if (!ob) {
329 //                      fprintf(stderr, "Cannot find armature for node %s\n", get_joint_name(animated.node));
330                         return true;
331                 }
332
333                 armature_importer->get_rna_path_for_joint(animated.node, joint_path, sizeof(joint_path));
334
335                 is_joint = true;
336         }
337         printf("object for animlist: %s found\n", animlist->getUniqueId().toAscii().c_str());
338         const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings();
339
340         switch (animated.tm->getTransformationType()) {
341         case COLLADAFW::Transformation::TRANSLATE:
342         case COLLADAFW::Transformation::SCALE:
343                 {
344                         bool loc = animated.tm->getTransformationType() == COLLADAFW::Transformation::TRANSLATE;
345                         if (is_joint)
346                                 BLI_snprintf(rna_path, sizeof(rna_path), "%s.%s", joint_path, loc ? "location" : "scale");
347                         else
348                                 BLI_strncpy(rna_path, loc ? "location" : "scale", sizeof(rna_path));
349
350                         for (int i = 0; i < bindings.getCount(); i++) {
351                                 const COLLADAFW::AnimationList::AnimationBinding& binding = bindings[i];
352                                 COLLADAFW::UniqueId anim_uid = binding.animation;
353
354                                 if (curve_map.find(anim_uid) == curve_map.end()) {
355                                         fprintf(stderr, "Cannot find FCurve by animation UID.\n");
356                                         continue;
357                                 }
358
359                                 std::vector<FCurve*>& fcurves = curve_map[anim_uid];
360                                 
361                                 switch (binding.animationClass) {
362                                 case COLLADAFW::AnimationList::POSITION_X:
363                                         add_fcurves_to_object(ob, fcurves, rna_path, 0, &animated);
364                                         break;
365                                 case COLLADAFW::AnimationList::POSITION_Y:
366                                         add_fcurves_to_object(ob, fcurves, rna_path, 1, &animated);
367                                         break;
368                                 case COLLADAFW::AnimationList::POSITION_Z:
369                                         add_fcurves_to_object(ob, fcurves, rna_path, 2, &animated);
370                                         break;
371                                 case COLLADAFW::AnimationList::POSITION_XYZ:
372                                         add_fcurves_to_object(ob, fcurves, rna_path, -1, &animated);
373                                         break;
374                                 default:
375                                         fprintf(stderr, "AnimationClass %d is not supported for %s.\n",
376                                                         binding.animationClass, loc ? "TRANSLATE" : "SCALE");
377                                 }
378                         }
379                 }
380                 break;
381         case COLLADAFW::Transformation::ROTATE:
382                 {
383                         if (is_joint)
384                                 BLI_snprintf(rna_path, sizeof(rna_path), "%s.rotation_euler", joint_path);
385                         else
386                                 BLI_strncpy(rna_path, "rotation_euler", sizeof(rna_path));
387
388                         COLLADAFW::Rotate* rot = (COLLADAFW::Rotate*)animated.tm;
389                         COLLADABU::Math::Vector3& axis = rot->getRotationAxis();
390                         
391                         for (int i = 0; i < bindings.getCount(); i++) {
392                                 const COLLADAFW::AnimationList::AnimationBinding& binding = bindings[i];
393                                 COLLADAFW::UniqueId anim_uid = binding.animation;
394
395                                 if (curve_map.find(anim_uid) == curve_map.end()) {
396                                         fprintf(stderr, "Cannot find FCurve by animation UID.\n");
397                                         continue;
398                                 }
399
400                                 std::vector<FCurve*>& fcurves = curve_map[anim_uid];
401
402                                 switch (binding.animationClass) {
403                                 case COLLADAFW::AnimationList::ANGLE:
404                                         if (COLLADABU::Math::Vector3::UNIT_X == axis) {
405                                                 add_fcurves_to_object(ob, fcurves, rna_path, 0, &animated);
406                                         }
407                                         else if (COLLADABU::Math::Vector3::UNIT_Y == axis) {
408                                                 add_fcurves_to_object(ob, fcurves, rna_path, 1, &animated);
409                                         }
410                                         else if (COLLADABU::Math::Vector3::UNIT_Z == axis) {
411                                                 add_fcurves_to_object(ob, fcurves, rna_path, 2, &animated);
412                                         }
413                                         break;
414                                 case COLLADAFW::AnimationList::AXISANGLE:
415                                         // TODO convert axis-angle to quat? or XYZ?
416                                 default:
417                                         fprintf(stderr, "AnimationClass %d is not supported for ROTATE transformation.\n",
418                                                         binding.animationClass);
419                                 }
420                         }
421                 }
422                 break;
423         case COLLADAFW::Transformation::MATRIX:
424         case COLLADAFW::Transformation::SKEW:
425         case COLLADAFW::Transformation::LOOKAT:
426                 fprintf(stderr, "Animation of MATRIX, SKEW and LOOKAT transformations is not supported yet.\n");
427                 break;
428         }
429 #endif
430         
431         return true;
432 }
433
434 // \todo refactor read_node_transform to not automatically apply anything,
435 // but rather return the transform matrix, so caller can do with it what is
436 // necessary. Same for \ref get_node_mat
437 void AnimationImporter::read_node_transform(COLLADAFW::Node *node, Object *ob)
438 {
439         float mat[4][4];
440         TransformReader::get_node_mat(mat, node, &uid_animated_map, ob);
441         if (ob) {
442                 copy_m4_m4(ob->obmat, mat);
443                 object_apply_mat4(ob, ob->obmat, 0, 0);
444         }
445 }
446
447 #if 0
448 virtual void AnimationImporter::change_eul_to_quat(Object *ob, bAction *act)
449 {
450         bActionGroup *grp;
451         int i;
452         
453         for (grp = (bActionGroup*)act->groups.first; grp; grp = grp->next) {
454
455                 FCurve *eulcu[3] = {NULL, NULL, NULL};
456                 
457                 if (fcurves_actionGroup_map.find(grp) == fcurves_actionGroup_map.end())
458                         continue;
459
460                 std::vector<FCurve*> &rot_fcurves = fcurves_actionGroup_map[grp];
461                 
462                 if (rot_fcurves.size() > 3) continue;
463
464                 for (i = 0; i < rot_fcurves.size(); i++)
465                         eulcu[rot_fcurves[i]->array_index] = rot_fcurves[i];
466
467                 char joint_path[100];
468                 char rna_path[100];
469
470                 BLI_snprintf(joint_path, sizeof(joint_path), "pose.bones[\"%s\"]", grp->name);
471                 BLI_snprintf(rna_path, sizeof(rna_path), "%s.rotation_quaternion", joint_path);
472
473                 FCurve *quatcu[4] = {
474                         create_fcurve(0, rna_path),
475                         create_fcurve(1, rna_path),
476                         create_fcurve(2, rna_path),
477                         create_fcurve(3, rna_path)
478                 };
479
480                 bPoseChannel *chan = get_pose_channel(ob->pose, grp->name);
481
482                 float m4[4][4], irest[3][3];
483                 invert_m4_m4(m4, chan->bone->arm_mat);
484                 copy_m3_m4(irest, m4);
485
486                 for (i = 0; i < 3; i++) {
487
488                         FCurve *cu = eulcu[i];
489
490                         if (!cu) continue;
491
492                         for (int j = 0; j < cu->totvert; j++) {
493                                 float frame = cu->bezt[j].vec[1][0];
494
495                                 float eul[3] = {
496                                         eulcu[0] ? evaluate_fcurve(eulcu[0], frame) : 0.0f,
497                                         eulcu[1] ? evaluate_fcurve(eulcu[1], frame) : 0.0f,
498                                         eulcu[2] ? evaluate_fcurve(eulcu[2], frame) : 0.0f
499                                 };
500
501                                 // make eul relative to bone rest pose
502                                 float rot[3][3], rel[3][3], quat[4];
503
504                                 /*eul_to_mat3(rot, eul);
505
506                                 mul_m3_m3m3(rel, irest, rot);
507
508                                 mat3_to_quat(quat, rel);
509                                 */
510
511                                 eul_to_quat(quat, eul);
512
513                                 for (int k = 0; k < 4; k++)
514                                         create_bezt(quatcu[k], frame, quat[k]);
515                         }
516                 }
517
518                 // now replace old Euler curves
519
520                 for (i = 0; i < 3; i++) {
521                         if (!eulcu[i]) continue;
522
523                         action_groups_remove_channel(act, eulcu[i]);
524                         free_fcurve(eulcu[i]);
525                 }
526
527                 chan->rotmode = ROT_MODE_QUAT;
528
529                 for (i = 0; i < 4; i++)
530                         action_groups_add_channel(act, grp, quatcu[i]);
531         }
532
533         bPoseChannel *pchan;
534         for (pchan = (bPoseChannel*)ob->pose->chanbase.first; pchan; pchan = pchan->next) {
535                 pchan->rotmode = ROT_MODE_QUAT;
536         }
537 }
538 #endif
539
540
541 //sets the rna_path and array index to curve
542 void AnimationImporter::modify_fcurve(std::vector<FCurve*>* curves , char* rna_path , int array_index )
543 {   
544         std::vector<FCurve*>::iterator it;
545         int i;
546         for (it = curves->begin(), i = 0; it != curves->end(); it++, i++) {
547                 FCurve *fcu = *it;
548                 fcu->rna_path = BLI_strdupn(rna_path, strlen(rna_path));
549                 
550                 if (array_index == -1) fcu->array_index = i;
551                 else fcu->array_index = array_index;
552
553                 unused_curves.erase(std::remove(unused_curves.begin(), unused_curves.end(), fcu), unused_curves.end());
554         }
555 }
556
557 void AnimationImporter::find_frames( std::vector<float>* frames , std::vector<FCurve*>* curves)
558 {
559         std::vector<FCurve*>::iterator iter;
560                 for (iter = curves->begin(); iter != curves->end(); iter++) {
561                         FCurve *fcu = *iter;
562         
563                 for (unsigned int k = 0; k < fcu->totvert; k++) {
564                         //get frame value from bezTriple
565                         float fra = fcu->bezt[k].vec[1][0];
566                         //if frame already not added add frame to frames
567                         if (std::find(frames->begin(), frames->end(), fra) == frames->end())
568                                 frames->push_back(fra);
569                                                         
570                 }
571                 }
572 }
573
574 //creates the rna_paths and array indices of fcurves from animations using transformation and bound animation class of each animation.
575 void AnimationImporter:: Assign_transform_animations(COLLADAFW::Transformation * transform , 
576                                                                                                          const COLLADAFW::AnimationList::AnimationBinding * binding,
577                                                                                                          std::vector<FCurve*>* curves, bool is_joint, char * joint_path)
578 {
579         COLLADAFW::Transformation::TransformationType tm_type = transform->getTransformationType();
580         bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX;
581         bool is_rotation = tm_type  == COLLADAFW::Transformation::ROTATE;
582         
583         //to check if the no of curves are valid
584         bool xyz = ((tm_type == COLLADAFW::Transformation::TRANSLATE ||tm_type  == COLLADAFW::Transformation::SCALE) && binding->animationClass == COLLADAFW::AnimationList::POSITION_XYZ);
585                         
586         
587         if (!((!xyz && curves->size() == 1) || (xyz && curves->size() == 3) || is_matrix)) {
588                 fprintf(stderr, "expected %d curves, got %d\n", xyz ? 3 : 1, (int)curves->size());
589                 return;
590         }
591         
592         char rna_path[100];
593                                                         
594         switch (tm_type) {
595                 case COLLADAFW::Transformation::TRANSLATE:
596                 case COLLADAFW::Transformation::SCALE:
597                         {
598                                 bool loc = tm_type == COLLADAFW::Transformation::TRANSLATE;
599                                 if (is_joint)
600                                         BLI_snprintf(rna_path, sizeof(rna_path), "%s.%s", joint_path, loc ? "location" : "scale");
601                                 else
602                                         BLI_strncpy(rna_path, loc ? "location" : "scale", sizeof(rna_path));
603
604                                 switch (binding->animationClass) {
605                                         case COLLADAFW::AnimationList::POSITION_X:
606                                                 modify_fcurve(curves, rna_path, 0 );
607                                                 break;
608                                         case COLLADAFW::AnimationList::POSITION_Y:
609                                                 modify_fcurve(curves, rna_path, 1 );
610                                                 break;
611                                         case COLLADAFW::AnimationList::POSITION_Z:
612                                                 modify_fcurve(curves, rna_path, 2 );
613                                                 break;
614                                         case COLLADAFW::AnimationList::POSITION_XYZ:
615                                                 modify_fcurve(curves, rna_path, -1 );
616                                                 break;
617                                         default:
618                                                 fprintf(stderr, "AnimationClass %d is not supported for %s.\n",
619                                                                 binding->animationClass, loc ? "TRANSLATE" : "SCALE");
620                                         }
621                 break;
622                         }
623                 
624                 
625                 case COLLADAFW::Transformation::ROTATE:
626                         {
627                                 if (is_joint)
628                                         BLI_snprintf(rna_path, sizeof(rna_path), "%s.rotation_euler", joint_path);
629                                 else
630                                         BLI_strncpy(rna_path, "rotation_euler", sizeof(rna_path));
631                 std::vector<FCurve*>::iterator iter;
632                                 for (iter = curves->begin(); iter != curves->end(); iter++) {
633                                         FCurve* fcu = *iter;
634                                         
635                                         //if transform is rotation the fcurves values must be turned in to radian.
636                                         if (is_rotation)
637                                                 fcurve_deg_to_rad(fcu);          
638                                 }                                       
639                                 COLLADAFW::Rotate* rot = (COLLADAFW::Rotate*)transform;
640                                 COLLADABU::Math::Vector3& axis = rot->getRotationAxis();
641                         
642                                 switch (binding->animationClass) {
643                                         case COLLADAFW::AnimationList::ANGLE:
644                                                 if (COLLADABU::Math::Vector3::UNIT_X == axis) {
645                                                         modify_fcurve(curves, rna_path, 0 );
646                                                 }
647                                                 else if (COLLADABU::Math::Vector3::UNIT_Y == axis) {
648                                                         modify_fcurve(curves, rna_path, 1 );
649                                                 }
650                                                 else if (COLLADABU::Math::Vector3::UNIT_Z == axis) {
651                                                         modify_fcurve(curves, rna_path, 2 );
652                                                 }
653                                                 break;
654                                         case COLLADAFW::AnimationList::AXISANGLE:
655                                                 // TODO convert axis-angle to quat? or XYZ?
656                                         default:
657                                                 fprintf(stderr, "AnimationClass %d is not supported for ROTATE transformation.\n",
658                                                                 binding->animationClass);
659                                         }
660                         break;
661                         }
662                         
663                 case COLLADAFW::Transformation::MATRIX:
664                         /*{
665                                 COLLADAFW::Matrix* mat = (COLLADAFW::Matrix*)transform;
666                                 COLLADABU::Math::Matrix4 mat4 = mat->getMatrix();
667                                 switch (binding->animationClass) {
668                                         case COLLADAFW::AnimationList::TRANSFORM:
669                                         
670                                 }
671                         }*/
672                 case COLLADAFW::Transformation::SKEW:
673                 case COLLADAFW::Transformation::LOOKAT:
674                         fprintf(stderr, "Animation of MATRIX, SKEW and LOOKAT transformations is not supported yet.\n");
675                         break;
676                 }
677         
678 }
679
680 void AnimationImporter:: Assign_color_animations(const COLLADAFW::UniqueId& listid, ListBase *AnimCurves ,char * anim_type)
681 {
682         char rna_path[100];
683         BLI_strncpy(rna_path,anim_type, sizeof(rna_path));
684     
685         const COLLADAFW::AnimationList *animlist = animlist_map[listid];
686         const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings();
687                                 //all the curves belonging to the current binding
688         std::vector<FCurve*> animcurves;    
689         for (unsigned int j = 0; j < bindings.getCount(); j++) {
690                  animcurves = curve_map[bindings[j].animation];
691                 //calculate rnapaths and array index of fcurves according to transformation and animation class
692                  //Assign_color_animations( &bindings[j], &animcurves); 
693                 
694             switch (bindings[j].animationClass) {
695                 case COLLADAFW::AnimationList::COLOR_R:
696                         modify_fcurve(&animcurves, rna_path, 0 );
697                         break;
698                 case COLLADAFW::AnimationList::COLOR_G:
699                         modify_fcurve(&animcurves, rna_path, 1 );
700                         break;
701                 case COLLADAFW::AnimationList::COLOR_B:
702                         modify_fcurve(&animcurves, rna_path, 2 );
703                         break;
704                 case COLLADAFW::AnimationList::COLOR_RGB:
705                 case COLLADAFW::AnimationList::COLOR_RGBA:
706                         modify_fcurve(&animcurves, rna_path, -1 );
707                         break;
708                         
709                 default:
710                         fprintf(stderr, "AnimationClass %d is not supported for %s.\n",
711                                         bindings[j].animationClass, "COLOR" );
712                 }
713
714                  std::vector<FCurve*>::iterator iter;
715                 //Add the curves of the current animation to the object
716                 for (iter = animcurves.begin(); iter != animcurves.end(); iter++) {
717                         FCurve * fcu = *iter;
718                         BLI_addtail(AnimCurves, fcu);   
719                 }                               
720         }
721
722         
723 }
724
725 void AnimationImporter:: Assign_float_animations(const COLLADAFW::UniqueId& listid, ListBase *AnimCurves, char * anim_type)
726 {
727         char rna_path[100];
728         if (animlist_map.find(listid) == animlist_map.end()) return ;
729         else 
730         {
731                 //transformation has animations
732                 const COLLADAFW::AnimationList *animlist = animlist_map[listid];
733                 const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings();
734                 //all the curves belonging to the current binding
735                 std::vector<FCurve*> animcurves;    
736                 for (unsigned int j = 0; j < bindings.getCount(); j++) {
737                          animcurves = curve_map[bindings[j].animation];
738                         //calculate rnapaths and array index of fcurves according to transformation and animation class
739                          BLI_strncpy(rna_path, anim_type , sizeof(rna_path));
740                          modify_fcurve(&animcurves, rna_path, 0 );
741                          std::vector<FCurve*>::iterator iter;
742                                 //Add the curves of the current animation to the object
743                                 for (iter = animcurves.begin(); iter != animcurves.end(); iter++) {
744                                         FCurve * fcu = *iter;
745                                         BLI_addtail(AnimCurves, fcu);   
746                                 }                               
747                 }
748         }
749         
750 }
751
752 void AnimationImporter::apply_matrix_curves_to_bone( Object * ob, std::vector<FCurve*>& animcurves, COLLADAFW::Node* root ,COLLADAFW::Node* node, 
753                                                                                                         COLLADAFW::Transformation * tm , char * joint_path, bool is_joint,const char * bone_name)
754 {
755         std::vector<float> frames;
756         find_frames(&frames, &animcurves);
757
758         float irest_dae[4][4];
759         float rest[4][4], irest[4][4];
760
761         if (is_joint) {
762                 get_joint_rest_mat(irest_dae, root, node);
763                 invert_m4(irest_dae);
764
765                 Bone *bone = get_named_bone((bArmature*)ob->data, bone_name);
766                 if (!bone) {
767                         fprintf(stderr, "cannot find bone \"%s\"\n", bone_name);
768                         return;
769                 }
770
771                 unit_m4(rest);
772                 copy_m4_m4(rest, bone->arm_mat);
773                 invert_m4_m4(irest, rest);
774         }
775     // new curves to assign matrix transform animation
776         FCurve *newcu[10]; // if tm_type is matrix, then create 10 curves: 4 rot, 3 loc, 3 scale
777         unsigned int totcu = 10 ;
778     const char *tm_str = NULL;
779         char rna_path[200];
780         for (int i = 0; i < totcu; i++) {
781
782                 int axis = i;
783
784                         if (i < 4) {
785                                 tm_str = "rotation_quaternion";
786                                 axis = i;
787                         }
788                         else if (i < 7) {
789                                 tm_str = "location";
790                                 axis = i - 4;
791                         }
792                         else {
793                                 tm_str = "scale";
794                                 axis = i - 7;
795                         }
796                 
797
798                 if (is_joint)
799                         BLI_snprintf(rna_path, sizeof(rna_path), "%s.%s", joint_path, tm_str);
800                 else
801                         strcpy(rna_path, tm_str);
802                 newcu[i] = create_fcurve(axis, rna_path);
803                 newcu[i]->totvert = frames.size();
804         }
805
806 //      Object *job = NULL;
807
808         if (frames.size() == 0)
809                 return;
810
811 std::sort(frames.begin(), frames.end());
812         //if (is_joint)
813         //      armature_importer->get_rna_path_for_joint(node, joint_path, sizeof(joint_path));
814
815         
816         std::vector<float>::iterator it;
817
818         // sample values at each frame
819         for (it = frames.begin(); it != frames.end(); it++) {
820                 float fra = *it;
821
822                 float mat[4][4];
823                 float matfra[4][4];
824
825                 unit_m4(matfra);
826         
827                 // calc object-space mat
828                 evaluate_transform_at_frame(matfra, node, fra);
829
830
831                 // for joints, we need a special matrix
832                 if (is_joint) {
833                         // special matrix: iR * M * iR_dae * R
834                         // where R, iR are bone rest and inverse rest mats in world space (Blender bones),
835                         // iR_dae is joint inverse rest matrix (DAE) and M is an evaluated joint world-space matrix (DAE)
836                         float temp[4][4], par[4][4];
837
838                         // calc M
839                         calc_joint_parent_mat_rest(par, NULL, root, node);
840                         mul_m4_m4m4(temp, matfra, par);
841
842                         // evaluate_joint_world_transform_at_frame(temp, NULL, , node, fra);
843
844                         // calc special matrix
845                         mul_serie_m4(mat, irest, temp, irest_dae, rest, NULL, NULL, NULL, NULL);
846                 }
847                 else {
848                         copy_m4_m4(mat, matfra);
849                 }
850
851                 float  rot[4], loc[3], scale[3];
852         
853                         mat4_to_quat(rot, mat);
854                         for ( int i = 0 ; i < 4  ;  i ++ )
855                         {
856                                 rot[i] = rot[i] * (180 / M_PI); 
857                         }
858                         copy_v3_v3(loc, mat[3]);
859                         mat4_to_size(scale, mat);
860                 
861                 // add keys
862                 for (int i = 0; i < totcu; i++) {
863                                 if (i < 4)
864                                         add_bezt(newcu[i], fra, rot[i]);
865                                 else if (i < 7)
866                                         add_bezt(newcu[i], fra, loc[i - 4]);
867                                 else
868                                         add_bezt(newcu[i], fra, scale[i - 7]);
869                 }
870         }
871         verify_adt_action((ID*)&ob->id, 1);
872
873         ListBase *curves = &ob->adt->action->curves;
874
875         // add curves
876         for (int i= 0; i < totcu; i++) {
877                 if (is_joint)
878                         add_bone_fcurve(ob, node, newcu[i]);
879                 else
880                         BLI_addtail(curves, newcu[i]);
881
882 #ifdef ARMATURE_TEST
883                 if (is_joint)
884                         BLI_addtail(&job->adt->action->curves, job_curves[i]);
885 #endif
886         }
887
888                 if (is_joint) {
889                         bPoseChannel *chan = get_pose_channel(ob->pose, bone_name);
890                         chan->rotmode = ROT_MODE_QUAT;
891                 }
892                 else {
893                         ob->rotmode = ROT_MODE_QUAT;
894                 }
895
896         return;
897
898 }
899
900 void AnimationImporter::translate_Animations_NEW ( COLLADAFW::Node * node , 
901                                                                                                    std::map<COLLADAFW::UniqueId, COLLADAFW::Node*>& root_map,
902                                                                                                    std::map<COLLADAFW::UniqueId, Object*>& object_map,
903                                                                                                    std::map<COLLADAFW::UniqueId, const COLLADAFW::Object*> FW_object_map)
904 {
905         AnimationImporter::AnimMix* animType = get_animation_type(node, FW_object_map );
906
907         bool is_joint = node->getType() == COLLADAFW::Node::JOINT;
908         COLLADAFW::Node *root = root_map.find(node->getUniqueId()) == root_map.end() ? node : root_map[node->getUniqueId()];
909         Object *ob = is_joint ? armature_importer->get_armature_for_joint(root) : object_map[node->getUniqueId()];
910         if (!ob)
911         {
912                 fprintf(stderr, "cannot find Object for Node with id=\"%s\"\n", node->getOriginalId().c_str());
913                 return;
914         }
915
916         bAction * act;
917         bActionGroup *grp = NULL;
918     
919         //if ( (animType & NODE_TRANSFORM) != 0 )
920         if ( (animType->transform) != 0 )
921         {
922         const char *bone_name = is_joint ? bc_get_joint_name(node) : NULL;
923         char joint_path[200];
924
925                 if ( is_joint ) 
926                 armature_importer->get_rna_path_for_joint(node, joint_path, sizeof(joint_path));
927                 
928         
929                 if (!ob->adt || !ob->adt->action) act = verify_adt_action((ID*)&ob->id, 1);
930                                         else act = ob->adt->action;
931                                         //Get the list of animation curves of the object
932             
933                 ListBase *AnimCurves = &(act->curves);
934
935                 const COLLADAFW::TransformationPointerArray& nodeTransforms = node->getTransformations();
936         
937                 //for each transformation in node 
938                 for (unsigned int i = 0; i < nodeTransforms.getCount(); i++) {
939                         COLLADAFW::Transformation *transform = nodeTransforms[i];
940                         COLLADAFW::Transformation::TransformationType tm_type = transform->getTransformationType();
941
942                         bool is_rotation = tm_type == COLLADAFW::Transformation::ROTATE;
943                         bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX;
944                                 
945                         const COLLADAFW::UniqueId& listid = transform->getAnimationList();
946                 
947                         //check if transformation has animations    
948                         if (animlist_map.find(listid) == animlist_map.end()) continue ; 
949                         else 
950                         {
951                                 //transformation has animations
952                                 const COLLADAFW::AnimationList *animlist = animlist_map[listid];
953                                 const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings();
954                                 //all the curves belonging to the current binding
955                                 std::vector<FCurve*> animcurves;    
956                                 for (unsigned int j = 0; j < bindings.getCount(); j++) {
957                                          animcurves = curve_map[bindings[j].animation];
958                                         //calculate rnapaths and array index of fcurves according to transformation and animation class
959                                          Assign_transform_animations(transform, &bindings[j], &animcurves, is_joint, joint_path ); 
960                                         
961                                          std::vector<FCurve*>::iterator iter;
962                                                 //Add the curves of the current animation to the object
963                                                 for (iter = animcurves.begin(); iter != animcurves.end(); iter++) {
964                                                         FCurve * fcu = *iter;
965                                                         if ((ob->type == OB_ARMATURE)){
966                                                                 if ( is_matrix){
967                                                                         float irest_dae[4][4];
968                                                                         get_joint_rest_mat(irest_dae, root, node);
969                                                                         apply_matrix_curves_to_bone(ob, animcurves, root , node,  transform ,joint_path , true , bone_name );
970                                                                         break;
971                                                                 } 
972                                                                 else
973                                                                         add_bone_fcurve( ob, node , fcu );
974                                                         } else 
975                                                          BLI_addtail(AnimCurves, fcu);  
976                                                 }                               
977                                 }
978                         }
979                         if (is_rotation) {
980                                 if (is_joint) 
981                                 {
982                                         bPoseChannel *chan = get_pose_channel(ob->pose, bone_name);
983                                         chan->rotmode = ROT_MODE_EUL;
984                                 }
985                                 else 
986                                 {
987                                         ob->rotmode = ROT_MODE_EUL;
988                                 }
989                         }
990                 }
991         }
992
993         if ((animType->light) != 0)
994         {
995                 Lamp * lamp  = (Lamp*) ob->data;
996
997                 if (!lamp->adt || !lamp->adt->action) act = verify_adt_action((ID*)&lamp->id, 1);
998                                         else act = lamp->adt->action;
999
1000                 ListBase *AnimCurves = &(act->curves);
1001                 const COLLADAFW::InstanceLightPointerArray& nodeLights = node->getInstanceLights();
1002
1003                 for (unsigned int i = 0; i < nodeLights.getCount(); i++) {
1004                         const COLLADAFW::Light *light = (COLLADAFW::Light *) FW_object_map[nodeLights[i]->getInstanciatedObjectId()];
1005
1006                         if ((animType->light & LIGHT_COLOR) != 0)
1007                         {
1008                                 const COLLADAFW::Color *col =  &(light->getColor());
1009                                 const COLLADAFW::UniqueId& listid = col->getAnimationList();
1010                                 
1011                                 Assign_color_animations(listid, AnimCurves, "color"); 
1012                         }
1013                         if ((animType->light & LIGHT_FOA) != 0 )
1014                         {
1015                                 const COLLADAFW::AnimatableFloat *foa =  &(light->getFallOffAngle());
1016                                 const COLLADAFW::UniqueId& listid = foa->getAnimationList();
1017                                 
1018                                 Assign_float_animations( listid ,AnimCurves, "spot_size"); 
1019                         }
1020                         if ( (animType->light & LIGHT_FOE) != 0 )
1021                         {
1022                                 const COLLADAFW::AnimatableFloat *foe =  &(light->getFallOffExponent());
1023                                 const COLLADAFW::UniqueId& listid = foe->getAnimationList();
1024                                 
1025                                 Assign_float_animations( listid ,AnimCurves, "spot_blend"); 
1026                         
1027                         }
1028                 }
1029         }
1030
1031         if ( (animType->camera) != 0) 
1032         {
1033                 Camera * camera  = (Camera*) ob->data;
1034
1035                 if (!camera->adt || !camera->adt->action) act = verify_adt_action((ID*)&camera->id, 1);
1036                                         else act = camera->adt->action;
1037
1038                 ListBase *AnimCurves = &(act->curves);
1039                 const COLLADAFW::InstanceCameraPointerArray& nodeCameras= node->getInstanceCameras();
1040
1041                 for (unsigned int i = 0; i < nodeCameras.getCount(); i++) {
1042                         const COLLADAFW::Camera *camera = (COLLADAFW::Camera *) FW_object_map[nodeCameras[i]->getInstanciatedObjectId()];
1043
1044                         if ((animType->camera & CAMERA_XFOV) != 0 )
1045                         {
1046                                 const COLLADAFW::AnimatableFloat *xfov =  &(camera->getXFov());
1047                                 const COLLADAFW::UniqueId& listid = xfov->getAnimationList();
1048                                 Assign_float_animations( listid ,AnimCurves, "lens"); 
1049                         }
1050
1051                         else if ((animType->camera & CAMERA_XMAG) != 0 )
1052                         {
1053                                 const COLLADAFW::AnimatableFloat *xmag =  &(camera->getXMag());
1054                                 const COLLADAFW::UniqueId& listid = xmag->getAnimationList();
1055                                 Assign_float_animations( listid ,AnimCurves, "ortho_scale"); 
1056                         }
1057
1058                         if ((animType->camera & CAMERA_ZFAR) != 0 )
1059                         {
1060                                 const COLLADAFW::AnimatableFloat *zfar =  &(camera->getFarClippingPlane());
1061                                 const COLLADAFW::UniqueId& listid = zfar->getAnimationList();
1062                                 Assign_float_animations( listid ,AnimCurves, "clip_end"); 
1063                         }
1064
1065                         if ((animType->camera & CAMERA_ZNEAR) != 0 )
1066                         {
1067                                 const COLLADAFW::AnimatableFloat *znear =  &(camera->getNearClippingPlane());
1068                                 const COLLADAFW::UniqueId& listid = znear->getAnimationList();
1069                                 Assign_float_animations( listid ,AnimCurves, "clip_start"); 
1070                         }
1071
1072                 }
1073         }
1074         if ( animType->material != 0){
1075                  Material *ma = give_current_material(ob, 1);
1076                  if (!ma->adt || !ma->adt->action) act = verify_adt_action((ID*)&ma->id, 1);
1077                                 else act = ma->adt->action;
1078
1079                 ListBase *AnimCurves = &(act->curves);
1080                 
1081                 const COLLADAFW::InstanceGeometryPointerArray& nodeGeoms = node->getInstanceGeometries();
1082                 for (unsigned int i = 0; i < nodeGeoms.getCount(); i++) {
1083                         const COLLADAFW::MaterialBindingArray& matBinds = nodeGeoms[i]->getMaterialBindings();
1084                         for (unsigned int j = 0; j < matBinds.getCount(); j++) {
1085                                 const COLLADAFW::UniqueId & matuid = matBinds[j].getReferencedMaterial();
1086                                 const COLLADAFW::Effect *ef = (COLLADAFW::Effect *) (FW_object_map[matuid]);
1087                                 const COLLADAFW::CommonEffectPointerArray& commonEffects  =  ef->getCommonEffects();
1088                                 COLLADAFW::EffectCommon *efc = commonEffects[0];
1089                                 if((animType->material & MATERIAL_SHININESS) != 0){
1090                                         const COLLADAFW::FloatOrParam *shin = &(efc->getShininess());
1091                                         const COLLADAFW::UniqueId& listid =  shin->getAnimationList();
1092                                         Assign_float_animations( listid, AnimCurves , "specular_hardness" );
1093                                 }
1094
1095                                 if((animType->material & MATERIAL_IOR) != 0){
1096                                         const COLLADAFW::FloatOrParam *ior = &(efc->getIndexOfRefraction());
1097                                         const COLLADAFW::UniqueId& listid =  ior->getAnimationList();
1098                                         Assign_float_animations( listid, AnimCurves , "raytrace_transparency.ior" );
1099                                 }
1100
1101                                 if((animType->material & MATERIAL_SPEC_COLOR) != 0){
1102                                         const COLLADAFW::ColorOrTexture *cot = &(efc->getSpecular());
1103                                         const COLLADAFW::UniqueId& listid =  cot->getColor().getAnimationList();
1104                                         Assign_color_animations( listid, AnimCurves , "specular_color" );
1105                                 }
1106                                 
1107                                 if((animType->material & MATERIAL_DIFF_COLOR) != 0){
1108                                         const COLLADAFW::ColorOrTexture *cot = &(efc->getDiffuse());
1109                                         const COLLADAFW::UniqueId& listid =  cot->getColor().getAnimationList();
1110                                         Assign_color_animations( listid, AnimCurves , "diffuse_color" );
1111                                 }
1112                         }
1113                 }       
1114         }
1115 }
1116
1117
1118 //Check if object is animated by checking if animlist_map holds the animlist_id of node transforms
1119 AnimationImporter::AnimMix* AnimationImporter::get_animation_type ( const COLLADAFW::Node * node , 
1120                                                                                         std::map<COLLADAFW::UniqueId, const COLLADAFW::Object*> FW_object_map) 
1121 {
1122         AnimMix *types = new AnimMix();
1123         
1124         const COLLADAFW::TransformationPointerArray& nodeTransforms = node->getTransformations();
1125         
1126         //for each transformation in node 
1127         for (unsigned int i = 0; i < nodeTransforms.getCount(); i++) {
1128                 COLLADAFW::Transformation *transform = nodeTransforms[i];
1129                 const COLLADAFW::UniqueId& listid = transform->getAnimationList();
1130                 
1131                 //check if transformation has animations    
1132                 if (animlist_map.find(listid) == animlist_map.end()) continue ;
1133                 else 
1134                 {
1135                         types->transform = types->transform|NODE_TRANSFORM;
1136                         break;
1137                 }
1138         }
1139         const COLLADAFW::InstanceLightPointerArray& nodeLights = node->getInstanceLights();
1140
1141         for (unsigned int i = 0; i < nodeLights.getCount(); i++) {
1142                 const COLLADAFW::Light *light = (COLLADAFW::Light *) FW_object_map[nodeLights[i]->getInstanciatedObjectId()];
1143                 types->light = setAnimType(&(light->getColor()),(types->light), LIGHT_COLOR);
1144                 types->light = setAnimType(&(light->getFallOffAngle()),(types->light), LIGHT_FOA);
1145                 types->light = setAnimType(&(light->getFallOffExponent()),(types->light), LIGHT_FOE);
1146                 
1147                 if ( types->light != 0) break;
1148                 
1149         }
1150
1151         const COLLADAFW::InstanceCameraPointerArray& nodeCameras = node->getInstanceCameras();
1152         for (unsigned int i = 0; i < nodeCameras.getCount(); i++) {
1153                 const COLLADAFW::Camera *camera = (COLLADAFW::Camera *) FW_object_map[nodeCameras[i]->getInstanciatedObjectId()];
1154
1155                 if ( camera->getCameraType() == COLLADAFW::Camera::PERSPECTIVE )
1156                 {
1157                     types->camera = setAnimType(&(camera->getXMag()),(types->camera), CAMERA_XFOV);
1158                 }
1159                 else 
1160                 {
1161                         types->camera = setAnimType(&(camera->getXMag()),(types->camera), CAMERA_XMAG);
1162                 }
1163                 types->camera = setAnimType(&(camera->getFarClippingPlane()),(types->camera), CAMERA_ZFAR);
1164                 types->camera = setAnimType(&(camera->getNearClippingPlane()),(types->camera), CAMERA_ZNEAR);
1165
1166                 if ( types->camera != 0) break;
1167
1168         }
1169
1170         const COLLADAFW::InstanceGeometryPointerArray& nodeGeoms = node->getInstanceGeometries();
1171         for (unsigned int i = 0; i < nodeGeoms.getCount(); i++) {
1172                 const COLLADAFW::MaterialBindingArray& matBinds = nodeGeoms[i]->getMaterialBindings();
1173                 for (unsigned int j = 0; j < matBinds.getCount(); j++) {
1174                         const COLLADAFW::UniqueId & matuid = matBinds[j].getReferencedMaterial();
1175                         const COLLADAFW::Effect *ef = (COLLADAFW::Effect *) (FW_object_map[matuid]);
1176                         const COLLADAFW::CommonEffectPointerArray& commonEffects  =  ef->getCommonEffects();
1177                         COLLADAFW::EffectCommon *efc = commonEffects[0];
1178                         types->material =  setAnimType(&(efc->getShininess()),(types->material), MATERIAL_SHININESS);
1179                         types->material =  setAnimType(&(efc->getSpecular().getColor()),(types->material), MATERIAL_SPEC_COLOR);
1180                         types->material =  setAnimType(&(efc->getDiffuse().getColor()),(types->material), MATERIAL_DIFF_COLOR);
1181                    // types->material =  setAnimType(&(efc->get()),(types->material), MATERIAL_TRANSPARENCY);
1182                         types->material =  setAnimType(&(efc->getIndexOfRefraction()),(types->material), MATERIAL_IOR);
1183                 }
1184         }
1185         return types;
1186 }
1187
1188 int AnimationImporter::setAnimType ( const COLLADAFW::Animatable * prop , int types, int addition)
1189 {
1190                 const COLLADAFW::UniqueId& listid =  prop->getAnimationList();
1191                 if (animlist_map.find(listid) != animlist_map.end()) 
1192                                 return types|addition;
1193                 else return types;
1194 }               
1195
1196 //XXX Is not used anymore.
1197 void AnimationImporter::find_frames_old(std::vector<float> * frames, COLLADAFW::Node * node , COLLADAFW::Transformation::TransformationType tm_type)
1198 {
1199         bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX;
1200         bool is_rotation = tm_type == COLLADAFW::Transformation::ROTATE;
1201         // for each <rotate>, <translate>, etc. there is a separate Transformation
1202         const COLLADAFW::TransformationPointerArray& nodeTransforms = node->getTransformations();
1203
1204         unsigned int i;
1205         // find frames at which to sample plus convert all rotation keys to radians
1206         for (i = 0; i < nodeTransforms.getCount(); i++) {
1207                 COLLADAFW::Transformation *transform = nodeTransforms[i];
1208                 COLLADAFW::Transformation::TransformationType nodeTmType = transform->getTransformationType();
1209
1210
1211                 if (nodeTmType == tm_type) {
1212                         //get animation bindings for the current transformation
1213                         const COLLADAFW::UniqueId& listid = transform->getAnimationList();
1214                         //if transform is animated its animlist must exist.
1215                         if (animlist_map.find(listid) != animlist_map.end()) {
1216                                 
1217                                 const COLLADAFW::AnimationList *animlist = animlist_map[listid];
1218                                 const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings();
1219                 
1220                                 if (bindings.getCount()) {
1221                                         //for each AnimationBinding get the fcurves which animate the transform
1222                                         for (unsigned int j = 0; j < bindings.getCount(); j++) {
1223                                                 std::vector<FCurve*>& curves = curve_map[bindings[j].animation];
1224                                                 bool xyz = ((nodeTmType == COLLADAFW::Transformation::TRANSLATE || nodeTmType == COLLADAFW::Transformation::SCALE) && bindings[j].animationClass == COLLADAFW::AnimationList::POSITION_XYZ);
1225
1226                                                 if ((!xyz && curves.size() == 1) || (xyz && curves.size() == 3) || is_matrix) {
1227                                                         std::vector<FCurve*>::iterator iter;
1228
1229                                                         for (iter = curves.begin(); iter != curves.end(); iter++) {
1230                                                                 FCurve *fcu = *iter;
1231                                 
1232                                                                 //if transform is rotation the fcurves values must be turned in to radian.
1233                                                                 if (is_rotation)
1234                                                                         fcurve_deg_to_rad(fcu);
1235
1236                                                                 for (unsigned int k = 0; k < fcu->totvert; k++) {
1237                                                                         //get frame value from bezTriple
1238                                                                         float fra = fcu->bezt[k].vec[1][0];
1239                                                                         //if frame already not added add frame to frames
1240                                                                         if (std::find(frames->begin(), frames->end(), fra) == frames->end())
1241                                                                                 frames->push_back(fra);
1242                                                                 }
1243                                                         }
1244                                                 }
1245                                                 else {
1246                                                         fprintf(stderr, "expected %d curves, got %d\n", xyz ? 3 : 1, (int)curves.size());
1247                                                 }
1248                                         }
1249                                 }
1250                         }
1251                 }
1252         }
1253 }
1254
1255
1256 // prerequisites:
1257 // animlist_map - map animlist id -> animlist
1258 // curve_map - map anim id -> curve(s)
1259 Object *AnimationImporter::translate_animation(COLLADAFW::Node *node,
1260                                                         std::map<COLLADAFW::UniqueId, Object*>& object_map,
1261                                                         std::map<COLLADAFW::UniqueId, COLLADAFW::Node*>& root_map,
1262                                                         COLLADAFW::Transformation::TransformationType tm_type,
1263                                                         Object *par_job)
1264 {
1265         
1266         bool is_rotation = tm_type == COLLADAFW::Transformation::ROTATE;
1267         bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX;
1268         bool is_joint = node->getType() == COLLADAFW::Node::JOINT;
1269         
1270         COLLADAFW::Node *root = root_map.find(node->getUniqueId()) == root_map.end() ? node : root_map[node->getUniqueId()];
1271         Object *ob = is_joint ? armature_importer->get_armature_for_joint(node) : object_map[node->getUniqueId()];
1272         const char *bone_name = is_joint ? bc_get_joint_name(node) : NULL;
1273         if (!ob) {
1274                 fprintf(stderr, "cannot find Object for Node with id=\"%s\"\n", node->getOriginalId().c_str());
1275                 return NULL;
1276         }
1277
1278         // frames at which to sample
1279         std::vector<float> frames;
1280         
1281         find_frames_old(&frames, node , tm_type);
1282         
1283         unsigned int i;
1284         
1285         float irest_dae[4][4];
1286         float rest[4][4], irest[4][4];
1287
1288         if (is_joint) {
1289                 get_joint_rest_mat(irest_dae, root, node);
1290                 invert_m4(irest_dae);
1291
1292                 Bone *bone = get_named_bone((bArmature*)ob->data, bone_name);
1293                 if (!bone) {
1294                         fprintf(stderr, "cannot find bone \"%s\"\n", bone_name);
1295                         return NULL;
1296                 }
1297
1298                 unit_m4(rest);
1299                 copy_m4_m4(rest, bone->arm_mat);
1300                 invert_m4_m4(irest, rest);
1301         }
1302
1303         Object *job = NULL;
1304
1305 #ifdef ARMATURE_TEST
1306         FCurve *job_curves[10];
1307         job = get_joint_object(root, node, par_job);
1308 #endif
1309
1310         if (frames.size() == 0)
1311                 return job;
1312
1313         std::sort(frames.begin(), frames.end());
1314
1315         const char *tm_str = NULL;
1316         switch (tm_type) {
1317         case COLLADAFW::Transformation::ROTATE:
1318                 tm_str = "rotation_quaternion";
1319                 break;
1320         case COLLADAFW::Transformation::SCALE:
1321                 tm_str = "scale";
1322                 break;
1323         case COLLADAFW::Transformation::TRANSLATE:
1324                 tm_str = "location";
1325                 break;
1326         case COLLADAFW::Transformation::MATRIX:
1327                 break;
1328         default:
1329                 return job;
1330         }
1331
1332         char rna_path[200];
1333         char joint_path[200];
1334
1335         if (is_joint)
1336                 armature_importer->get_rna_path_for_joint(node, joint_path, sizeof(joint_path));
1337
1338         // new curves
1339         FCurve *newcu[10]; // if tm_type is matrix, then create 10 curves: 4 rot, 3 loc, 3 scale
1340         unsigned int totcu = is_matrix ? 10 : (is_rotation ? 4 : 3);
1341
1342         for (i = 0; i < totcu; i++) {
1343
1344                 int axis = i;
1345
1346                 if (is_matrix) {
1347                         if (i < 4) {
1348                                 tm_str = "rotation_quaternion";
1349                                 axis = i;
1350                         }
1351                         else if (i < 7) {
1352                                 tm_str = "location";
1353                                 axis = i - 4;
1354                         }
1355                         else {
1356                                 tm_str = "scale";
1357                                 axis = i - 7;
1358                         }
1359                 }
1360
1361                 if (is_joint)
1362                         BLI_snprintf(rna_path, sizeof(rna_path), "%s.%s", joint_path, tm_str);
1363                 else
1364                         strcpy(rna_path, tm_str);
1365                 newcu[i] = create_fcurve(axis, rna_path);
1366
1367 #ifdef ARMATURE_TEST
1368                 if (is_joint)
1369                         job_curves[i] = create_fcurve(axis, tm_str);
1370 #endif
1371         }
1372
1373         std::vector<float>::iterator it;
1374
1375         // sample values at each frame
1376         for (it = frames.begin(); it != frames.end(); it++) {
1377                 float fra = *it;
1378
1379                 float mat[4][4];
1380                 float matfra[4][4];
1381
1382                 unit_m4(matfra);
1383
1384                 // calc object-space mat
1385                 evaluate_transform_at_frame(matfra, node, fra);
1386
1387                 // for joints, we need a special matrix
1388                 if (is_joint) {
1389                         // special matrix: iR * M * iR_dae * R
1390                         // where R, iR are bone rest and inverse rest mats in world space (Blender bones),
1391                         // iR_dae is joint inverse rest matrix (DAE) and M is an evaluated joint world-space matrix (DAE)
1392                         float temp[4][4], par[4][4];
1393
1394                         // calc M
1395                         calc_joint_parent_mat_rest(par, NULL, root, node);
1396                         mul_m4_m4m4(temp, matfra, par);
1397
1398                         // evaluate_joint_world_transform_at_frame(temp, NULL, , node, fra);
1399
1400                         // calc special matrix
1401                         mul_serie_m4(mat, irest, temp, irest_dae, rest, NULL, NULL, NULL, NULL);
1402                 }
1403                 else {
1404                         copy_m4_m4(mat, matfra);
1405                 }
1406
1407                 float val[4], rot[4], loc[3], scale[3];
1408
1409                 switch (tm_type) {
1410                 case COLLADAFW::Transformation::ROTATE:
1411                         mat4_to_quat(val, mat);
1412                         break;
1413                 case COLLADAFW::Transformation::SCALE:
1414                         mat4_to_size(val, mat);
1415                         break;
1416                 case COLLADAFW::Transformation::TRANSLATE:
1417                         copy_v3_v3(val, mat[3]);
1418                         break;
1419                 case COLLADAFW::Transformation::MATRIX:
1420                         mat4_to_quat(rot, mat);
1421                         copy_v3_v3(loc, mat[3]);
1422                         mat4_to_size(scale, mat);
1423                         break;
1424                 default:
1425                         break;
1426                 }
1427
1428                 // add keys
1429                 for (i = 0; i < totcu; i++) {
1430                         if (is_matrix) {
1431                                 if (i < 4)
1432                                         add_bezt(newcu[i], fra, rot[i]);
1433                                 else if (i < 7)
1434                                         add_bezt(newcu[i], fra, loc[i - 4]);
1435                                 else
1436                                         add_bezt(newcu[i], fra, scale[i - 7]);
1437                         }
1438                         else {
1439                                 add_bezt(newcu[i], fra, val[i]);
1440                         }
1441                 }
1442
1443 #ifdef ARMATURE_TEST
1444                 if (is_joint) {
1445                         switch (tm_type) {
1446                         case COLLADAFW::Transformation::ROTATE:
1447                                 mat4_to_quat(val, matfra);
1448                                 break;
1449                         case COLLADAFW::Transformation::SCALE:
1450                                 mat4_to_size(val, matfra);
1451                                 break;
1452                         case COLLADAFW::Transformation::TRANSLATE:
1453                                 copy_v3_v3(val, matfra[3]);
1454                                 break;
1455                         case MATRIX:
1456                                 mat4_to_quat(rot, matfra);
1457                                 copy_v3_v3(loc, matfra[3]);
1458                                 mat4_to_size(scale, matfra);
1459                                 break;
1460                         default:
1461                                 break;
1462                         }
1463
1464                         for (i = 0; i < totcu; i++) {
1465                                 if (is_matrix) {
1466                                         if (i < 4)
1467                                                 add_bezt(job_curves[i], fra, rot[i]);
1468                                         else if (i < 7)
1469                                                 add_bezt(job_curves[i], fra, loc[i - 4]);
1470                                         else
1471                                                 add_bezt(job_curves[i], fra, scale[i - 7]);
1472                                 }
1473                                 else {
1474                                         add_bezt(job_curves[i], fra, val[i]);
1475                                 }
1476                         }
1477                 }
1478 #endif
1479         }
1480
1481         verify_adt_action((ID*)&ob->id, 1);
1482
1483         ListBase *curves = &ob->adt->action->curves;
1484
1485         // add curves
1486         for (i = 0; i < totcu; i++) {
1487                 if (is_joint)
1488                         add_bone_fcurve(ob, node, newcu[i]);
1489                 else
1490                         BLI_addtail(curves, newcu[i]);
1491
1492 #ifdef ARMATURE_TEST
1493                 if (is_joint)
1494                         BLI_addtail(&job->adt->action->curves, job_curves[i]);
1495 #endif
1496         }
1497
1498         if (is_rotation || is_matrix) {
1499                 if (is_joint) {
1500                         bPoseChannel *chan = get_pose_channel(ob->pose, bone_name);
1501                         chan->rotmode = ROT_MODE_QUAT;
1502                 }
1503                 else {
1504                         ob->rotmode = ROT_MODE_QUAT;
1505                 }
1506         }
1507
1508         return job;
1509 }
1510
1511 // internal, better make it private
1512 // warning: evaluates only rotation
1513 // prerequisites: animlist_map, curve_map
1514 void AnimationImporter::evaluate_transform_at_frame(float mat[4][4], COLLADAFW::Node *node, float fra)
1515 {
1516         const COLLADAFW::TransformationPointerArray& tms = node->getTransformations();
1517
1518         unit_m4(mat);
1519
1520         for (unsigned int i = 0; i < tms.getCount(); i++) {
1521                 COLLADAFW::Transformation *tm = tms[i];
1522                 COLLADAFW::Transformation::TransformationType type = tm->getTransformationType();
1523                 float m[4][4];
1524
1525                 unit_m4(m);
1526                 if ( type != COLLADAFW::Transformation::MATRIX )
1527                         continue;
1528
1529                 std::string nodename = node->getName().size() ? node->getName() : node->getOriginalId();
1530                 if (!evaluate_animation(tm, m, fra, nodename.c_str())) {
1531                         /*switch (type) {
1532                         case COLLADAFW::Transformation::ROTATE:
1533                                 dae_rotate_to_mat4(tm, m);
1534                                 break;
1535                         case COLLADAFW::Transformation::TRANSLATE:
1536                                 dae_translate_to_mat4(tm, m);
1537                                 break;
1538                         case COLLADAFW::Transformation::SCALE:
1539                                 dae_scale_to_mat4(tm, m);
1540                                 break;
1541                         case COLLADAFW::Transformation::MATRIX:
1542                                 dae_matrix_to_mat4(tm, m);
1543                                 break;
1544                         default:
1545                                 fprintf(stderr, "unsupported transformation type %d\n", type);
1546                         }*/
1547                         dae_matrix_to_mat4(tm, m);
1548                         
1549                 }
1550
1551                 float temp[4][4];
1552                 copy_m4_m4(temp, mat);
1553
1554                 mul_m4_m4m4(mat, m, temp);
1555         }
1556 }
1557
1558 // return true to indicate that mat contains a sane value
1559 bool AnimationImporter::evaluate_animation(COLLADAFW::Transformation *tm, float mat[4][4], float fra, const char *node_id)
1560 {
1561         const COLLADAFW::UniqueId& listid = tm->getAnimationList();
1562         COLLADAFW::Transformation::TransformationType type = tm->getTransformationType();
1563
1564         if (type != COLLADAFW::Transformation::ROTATE &&
1565             type != COLLADAFW::Transformation::SCALE &&
1566             type != COLLADAFW::Transformation::TRANSLATE &&
1567             type != COLLADAFW::Transformation::MATRIX) {
1568                 fprintf(stderr, "animation of transformation %d is not supported yet\n", type);
1569                 return false;
1570         }
1571
1572         if (animlist_map.find(listid) == animlist_map.end())
1573                 return false;
1574
1575         const COLLADAFW::AnimationList *animlist = animlist_map[listid];
1576         const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings();
1577
1578         if (bindings.getCount()) {
1579                 float vec[3];
1580
1581                 bool is_scale = (type == COLLADAFW::Transformation::SCALE);
1582                 bool is_translate = (type == COLLADAFW::Transformation::TRANSLATE);
1583
1584                 if (is_scale)
1585                         dae_scale_to_v3(tm, vec);
1586                 else if (is_translate)
1587                         dae_translate_to_v3(tm, vec);
1588
1589                 for (unsigned int j = 0; j < bindings.getCount(); j++) {
1590                         const COLLADAFW::AnimationList::AnimationBinding& binding = bindings[j];
1591                         std::vector<FCurve*>& curves = curve_map[binding.animation];
1592                         COLLADAFW::AnimationList::AnimationClass animclass = binding.animationClass;
1593                         char path[100];
1594
1595                         switch (type) {
1596                         case COLLADAFW::Transformation::ROTATE:
1597                                 BLI_snprintf(path, sizeof(path), "%s.rotate (binding %u)", node_id, j);
1598                                 break;
1599                         case COLLADAFW::Transformation::SCALE:
1600                                 BLI_snprintf(path, sizeof(path), "%s.scale (binding %u)", node_id, j);
1601                                 break;
1602                         case COLLADAFW::Transformation::TRANSLATE:
1603                                 BLI_snprintf(path, sizeof(path), "%s.translate (binding %u)", node_id, j);
1604                                 break;
1605                         case COLLADAFW::Transformation::MATRIX:
1606                                 BLI_snprintf(path, sizeof(path), "%s.matrix (binding %u)", node_id, j);
1607                                 break;
1608                         default:
1609                                 break;
1610                         }
1611
1612                         if (animclass == COLLADAFW::AnimationList::UNKNOWN_CLASS) {
1613                                 fprintf(stderr, "%s: UNKNOWN animation class\n", path);
1614                                 //continue;
1615                         }
1616
1617                         if (type == COLLADAFW::Transformation::ROTATE) {
1618                                 if (curves.size() != 1) {
1619                                         fprintf(stderr, "expected 1 curve, got %d\n", (int)curves.size());
1620                                         return false;
1621                                 }
1622
1623                                 // TODO support other animclasses
1624                                 if (animclass != COLLADAFW::AnimationList::ANGLE) {
1625                                         fprintf(stderr, "%s: animation class %d is not supported yet\n", path, animclass);
1626                                         return false;
1627                                 }
1628
1629                                 COLLADABU::Math::Vector3& axis = ((COLLADAFW::Rotate*)tm)->getRotationAxis();
1630                                 float ax[3] = {axis[0], axis[1], axis[2]};
1631                                 float angle = evaluate_fcurve(curves[0], fra);
1632                                 axis_angle_to_mat4(mat, ax, angle);
1633
1634                                 return true;
1635                         }
1636                         else if (is_scale || is_translate) {
1637                                 bool is_xyz = animclass == COLLADAFW::AnimationList::POSITION_XYZ;
1638
1639                                 if ((!is_xyz && curves.size() != 1) || (is_xyz && curves.size() != 3)) {
1640                                         if (is_xyz)
1641                                                 fprintf(stderr, "%s: expected 3 curves, got %d\n", path, (int)curves.size());
1642                                         else
1643                                                 fprintf(stderr, "%s: expected 1 curve, got %d\n", path, (int)curves.size());
1644                                         return false;
1645                                 }
1646                                 
1647                                 switch (animclass) {
1648                                 case COLLADAFW::AnimationList::POSITION_X:
1649                                         vec[0] = evaluate_fcurve(curves[0], fra);
1650                                         break;
1651                                 case COLLADAFW::AnimationList::POSITION_Y:
1652                                         vec[1] = evaluate_fcurve(curves[0], fra);
1653                                         break;
1654                                 case COLLADAFW::AnimationList::POSITION_Z:
1655                                         vec[2] = evaluate_fcurve(curves[0], fra);
1656                                         break;
1657                                 case COLLADAFW::AnimationList::POSITION_XYZ:
1658                                         vec[0] = evaluate_fcurve(curves[0], fra);
1659                                         vec[1] = evaluate_fcurve(curves[1], fra);
1660                                         vec[2] = evaluate_fcurve(curves[2], fra);
1661                                         break;
1662                                 default:
1663                                         fprintf(stderr, "%s: animation class %d is not supported yet\n", path, animclass);
1664                                         break;
1665                                 }
1666                         }
1667                         else if (type == COLLADAFW::Transformation::MATRIX) {
1668                                 // for now, of matrix animation, support only the case when all values are packed into one animation
1669                                 if (curves.size() != 16) {
1670                                         fprintf(stderr, "%s: expected 16 curves, got %d\n", path, (int)curves.size());
1671                                         return false;
1672                                 }
1673
1674                                 COLLADABU::Math::Matrix4 matrix;
1675                                 int i = 0, j = 0;
1676
1677                                 for (std::vector<FCurve*>::iterator it = curves.begin(); it != curves.end(); it++) {
1678                                         matrix.setElement(i, j, evaluate_fcurve(*it, fra));
1679                                         j++;
1680                                         if (j == 4) {
1681                                                 i++;
1682                                                 j = 0;
1683                                         }
1684                                 }
1685
1686                                 COLLADAFW::Matrix tm(matrix);
1687                                 dae_matrix_to_mat4(&tm, mat);
1688
1689                                 return true;
1690                         }
1691                 }
1692
1693                 if (is_scale)
1694                         size_to_mat4(mat, vec);
1695                 else
1696                         copy_v3_v3(mat[3], vec);
1697
1698                 return is_scale || is_translate;
1699         }
1700
1701         return false;
1702 }
1703
1704 // gives a world-space mat of joint at rest position
1705 void AnimationImporter::get_joint_rest_mat(float mat[4][4], COLLADAFW::Node *root, COLLADAFW::Node *node)
1706 {
1707         // if bind mat is not available,
1708         // use "current" node transform, i.e. all those tms listed inside <node>
1709         if (!armature_importer->get_joint_bind_mat(mat, node)) {
1710                 float par[4][4], m[4][4];
1711
1712                 calc_joint_parent_mat_rest(par, NULL, root, node);
1713                 get_node_mat(m, node, NULL, NULL);
1714                 mul_m4_m4m4(mat, m, par);
1715         }
1716 }
1717
1718 // gives a world-space mat, end's mat not included
1719 bool AnimationImporter::calc_joint_parent_mat_rest(float mat[4][4], float par[4][4], COLLADAFW::Node *node, COLLADAFW::Node *end)
1720 {
1721         float m[4][4];
1722
1723         if (node == end) {
1724                 par ? copy_m4_m4(mat, par) : unit_m4(mat);
1725                 return true;
1726         }
1727
1728         // use bind matrix if available or calc "current" world mat
1729         if (!armature_importer->get_joint_bind_mat(m, node)) {
1730                 if (par) {
1731                         float temp[4][4];
1732                         get_node_mat(temp, node, NULL, NULL);
1733                         mul_m4_m4m4(m, temp, par);
1734                 }
1735                 else {
1736                         get_node_mat(m, node, NULL, NULL);
1737                 }
1738         }
1739
1740         COLLADAFW::NodePointerArray& children = node->getChildNodes();
1741         for (unsigned int i = 0; i < children.getCount(); i++) {
1742                 if (calc_joint_parent_mat_rest(mat, m, children[i], end))
1743                         return true;
1744         }
1745
1746         return false;
1747 }
1748
1749 #ifdef ARMATURE_TEST
1750 Object *AnimationImporter::get_joint_object(COLLADAFW::Node *root, COLLADAFW::Node *node, Object *par_job)
1751 {
1752         if (joint_objects.find(node->getUniqueId()) == joint_objects.end()) {
1753                 Object *job = add_object(scene, OB_EMPTY);
1754
1755                 rename_id((ID*)&job->id, (char*)get_joint_name(node));
1756
1757                 job->lay = object_in_scene(job, scene)->lay = 2;
1758
1759                 mul_v3_fl(job->size, 0.5f);
1760                 job->recalc |= OB_RECALC_OB;
1761
1762                 verify_adt_action((ID*)&job->id, 1);
1763
1764                 job->rotmode = ROT_MODE_QUAT;
1765
1766                 float mat[4][4];
1767                 get_joint_rest_mat(mat, root, node);
1768
1769                 if (par_job) {
1770                         float temp[4][4], ipar[4][4];
1771                         invert_m4_m4(ipar, par_job->obmat);
1772                         copy_m4_m4(temp, mat);
1773                         mul_m4_m4m4(mat, temp, ipar);
1774                 }
1775
1776                 TransformBase::decompose(mat, job->loc, NULL, job->quat, job->size);
1777
1778                 if (par_job) {
1779                         job->parent = par_job;
1780
1781                         par_job->recalc |= OB_RECALC_OB;
1782                         job->parsubstr[0] = 0;
1783                 }
1784
1785                 where_is_object(scene, job);
1786
1787                 // after parenting and layer change
1788                 DAG_scene_sort(CTX_data_main(C), scene);
1789
1790                 joint_objects[node->getUniqueId()] = job;
1791         }
1792
1793         return joint_objects[node->getUniqueId()];
1794 }
1795 #endif
1796
1797 #if 0
1798 // recursively evaluates joint tree until end is found, mat then is world-space matrix of end
1799 // mat must be identity on enter, node must be root
1800 bool AnimationImporter::evaluate_joint_world_transform_at_frame(float mat[4][4], float par[4][4], COLLADAFW::Node *node, COLLADAFW::Node *end, float fra)
1801 {
1802         float m[4][4];
1803         if (par) {
1804                 float temp[4][4];
1805                 evaluate_transform_at_frame(temp, node, node == end ? fra : 0.0f);
1806                 mul_m4_m4m4(m, temp, par);
1807         }
1808         else {
1809                 evaluate_transform_at_frame(m, node, node == end ? fra : 0.0f);
1810         }
1811
1812         if (node == end) {
1813                 copy_m4_m4(mat, m);
1814                 return true;
1815         }
1816         else {
1817                 COLLADAFW::NodePointerArray& children = node->getChildNodes();
1818                 for (int i = 0; i < children.getCount(); i++) {
1819                         if (evaluate_joint_world_transform_at_frame(mat, m, children[i], end, fra))
1820                                 return true;
1821                 }
1822         }
1823
1824         return false;
1825 }
1826 #endif
1827
1828 void AnimationImporter::add_bone_fcurve(Object *ob, COLLADAFW::Node *node, FCurve *fcu)
1829 {
1830         const char *bone_name = bc_get_joint_name(node);
1831         bAction *act = ob->adt->action;
1832                         
1833         /* try to find group */
1834         bActionGroup *grp = action_groups_find_named(act, bone_name);
1835
1836         /* no matching groups, so add one */
1837         if (grp == NULL) {
1838                 /* Add a new group, and make it active */
1839                 grp = (bActionGroup*)MEM_callocN(sizeof(bActionGroup), "bActionGroup");
1840                                         
1841                 grp->flag = AGRP_SELECTED;
1842                 BLI_strncpy(grp->name, bone_name, sizeof(grp->name));
1843                                         
1844                 BLI_addtail(&act->groups, grp);
1845                 BLI_uniquename(&act->groups, grp, "Group", '.', offsetof(bActionGroup, name), 64);
1846         }
1847                                 
1848         /* add F-Curve to group */
1849         action_groups_add_channel(act, grp, fcu);
1850 }
1851
1852 void AnimationImporter::add_bezt(FCurve *fcu, float fra, float value)
1853 {
1854         //float fps = (float)FPS;
1855         BezTriple bez;
1856         memset(&bez, 0, sizeof(BezTriple));
1857         bez.vec[1][0] = fra ;
1858         bez.vec[1][1] = value;
1859         bez.ipo = BEZT_IPO_LIN ;/* use default interpolation mode here... */
1860         bez.f1 = bez.f2 = bez.f3 = SELECT;
1861         bez.h1 = bez.h2 = HD_AUTO;
1862         insert_bezt_fcurve(fcu, &bez, 0);
1863         calchandles_fcurve(fcu);
1864 }