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