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