1bcf51f9d1f50bb2da6ddb0f6fb04ea92dc75605
[blender.git] / source / blender / collada / AnimationImporter.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Nathan Letwory, Sukhitha Jayathilake.
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/collada/AnimationImporter.cpp
24  *  \ingroup collada
25  */
26
27 #include <stddef.h>
28
29 /* COLLADABU_ASSERT, may be able to remove later */
30 #include "COLLADABUPlatform.h"
31
32 #include "DNA_armature_types.h"
33
34 #include "ED_keyframing.h"
35
36 #include "BLI_listbase.h"
37 #include "BLI_math.h"
38 #include "BLI_path_util.h"
39 #include "BLI_string.h"
40
41 #include "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 }
632
633 void AnimationImporter:: Assign_float_animations(const COLLADAFW::UniqueId& listid, ListBase *AnimCurves, const char *anim_type)
634 {
635         char rna_path[100];
636         if (animlist_map.find(listid) == animlist_map.end()) {
637                 return;
638         }
639         else {
640                 //anim_type has animations
641                 const COLLADAFW::AnimationList *animlist = animlist_map[listid];
642                 const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings();
643                 //all the curves belonging to the current binding
644                 std::vector<FCurve *> animcurves;
645                 for (unsigned int j = 0; j < bindings.getCount(); j++) {
646                         animcurves = curve_map[bindings[j].animation];
647
648                         BLI_strncpy(rna_path, anim_type, sizeof(rna_path));
649                         modify_fcurve(&animcurves, rna_path, 0);
650                         std::vector<FCurve *>::iterator iter;
651                         //Add the curves of the current animation to the object
652                         for (iter = animcurves.begin(); iter != animcurves.end(); iter++) {
653                                 FCurve *fcu = *iter;
654                                 /* All anim_types whose values are to be converted from Degree to Radians can be ORed here */
655                                 if (STREQ("spot_size", anim_type)) {
656                                         /* NOTE: Do NOT convert if imported file was made by blender <= 2.69.10
657                                          * Reason: old blender versions stored spot_size in radians (was a bug)
658                                          */
659                                         if (this->import_from_version == "" || BLI_natstrcmp(this->import_from_version.c_str(), "2.69.10") != -1) {
660                                                 fcurve_deg_to_rad(fcu);
661                                         }
662                                 }
663                                 /** XXX What About animtype "rotation" ? */
664
665                                 BLI_addtail(AnimCurves, fcu);
666                                 fcurve_is_used(fcu);
667                         }
668                 }
669         }
670         
671 }
672
673 /*
674  * Lens animations must be stored in COLLADA by using FOV,
675  * while blender internally uses focal length.
676  * The imported animation curves must be converted appropriately.
677  */
678 void AnimationImporter::Assign_lens_animations(const COLLADAFW::UniqueId& listid, ListBase *AnimCurves, const double aspect, Camera *cam, const char *anim_type, int fov_type)
679 {
680         char rna_path[100];
681         if (animlist_map.find(listid) == animlist_map.end()) {
682                 return;
683         }
684         else {
685                 //anim_type has animations
686                 const COLLADAFW::AnimationList *animlist = animlist_map[listid];
687                 const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings();
688                 //all the curves belonging to the current binding
689                 std::vector<FCurve *> animcurves;
690                 for (unsigned int j = 0; j < bindings.getCount(); j++) {
691                         animcurves = curve_map[bindings[j].animation];
692
693                         BLI_strncpy(rna_path, anim_type, sizeof(rna_path));
694
695                         modify_fcurve(&animcurves, rna_path, 0);
696                         std::vector<FCurve *>::iterator iter;
697                         //Add the curves of the current animation to the object
698                         for (iter = animcurves.begin(); iter != animcurves.end(); iter++) {
699                                 FCurve *fcu = *iter;
700                                 
701                                 for (unsigned int i = 0; i < fcu->totvert; i++) {
702
703                                         double input_fov = fcu->bezt[i].vec[1][1];
704
705                                         // NOTE: Needs more testing (As we curretnly have no official test data for this)
706                                         double xfov = (fov_type == CAMERA_YFOV) ? (2.0f * atanf(aspect * tanf(DEG2RADF(input_fov) * 0.5f))) : DEG2RADF(input_fov);
707
708                                         fcu->bezt[i].vec[1][1] = fov_to_focallength(xfov, cam->sensor_x);
709                                 }
710
711                                 BLI_addtail(AnimCurves, fcu);
712                                 fcurve_is_used(fcu);
713                         }
714                 }
715         }
716 }
717
718 void AnimationImporter::apply_matrix_curves(Object *ob, std::vector<FCurve *>& animcurves, COLLADAFW::Node *root, COLLADAFW::Node *node,
719                                             COLLADAFW::Transformation *tm)
720 {
721         bool is_joint = node->getType() == COLLADAFW::Node::JOINT;
722         const char *bone_name = is_joint ? bc_get_joint_name(node) : NULL;
723         char joint_path[200];
724         if (is_joint)
725                 armature_importer->get_rna_path_for_joint(node, joint_path, sizeof(joint_path));
726
727         std::vector<float> frames;
728         find_frames(&frames, &animcurves);
729
730         float irest_dae[4][4];
731         float rest[4][4], irest[4][4];
732
733         if (is_joint) {
734                 get_joint_rest_mat(irest_dae, root, node);
735                 invert_m4(irest_dae);
736
737                 Bone *bone = BKE_armature_find_bone_name((bArmature *)ob->data, bone_name);
738                 if (!bone) {
739                         fprintf(stderr, "cannot find bone \"%s\"\n", bone_name);
740                         return;
741                 }
742
743                 unit_m4(rest);
744                 copy_m4_m4(rest, bone->arm_mat);
745                 invert_m4_m4(irest, rest);
746         }
747         // new curves to assign matrix transform animation
748         FCurve *newcu[10]; // if tm_type is matrix, then create 10 curves: 4 rot, 3 loc, 3 scale
749         unsigned int totcu = 10;
750         const char *tm_str = NULL;
751         char rna_path[200];
752         for (int i = 0; i < totcu; i++) {
753
754                 int axis = i;
755
756                 if (i < 4) {
757                         tm_str = "rotation_quaternion";
758                         axis = i;
759                 }
760                 else if (i < 7) {
761                         tm_str = "location";
762                         axis = i - 4;
763                 }
764                 else {
765                         tm_str = "scale";
766                         axis = i - 7;
767                 }
768
769
770                 if (is_joint)
771                         BLI_snprintf(rna_path, sizeof(rna_path), "%s.%s", joint_path, tm_str);
772                 else
773                         BLI_strncpy(rna_path, tm_str, sizeof(rna_path));
774                 newcu[i] = create_fcurve(axis, rna_path);
775                 newcu[i]->totvert = frames.size();
776         }
777
778         if (frames.size() == 0)
779                 return;
780
781         std::sort(frames.begin(), frames.end());
782
783         std::vector<float>::iterator it;
784
785         // sample values at each frame
786         for (it = frames.begin(); it != frames.end(); it++) {
787                 float fra = *it;
788
789                 float mat[4][4];
790                 float matfra[4][4];
791
792                 unit_m4(matfra);
793
794                 // calc object-space mat
795                 evaluate_transform_at_frame(matfra, node, fra);
796
797
798                 // for joints, we need a special matrix
799                 if (is_joint) {
800                         // special matrix: iR * M * iR_dae * R
801                         // where R, iR are bone rest and inverse rest mats in world space (Blender bones),
802                         // iR_dae is joint inverse rest matrix (DAE) and M is an evaluated joint world-space matrix (DAE)
803                         float temp[4][4], par[4][4];
804
805                         // calc M
806                         calc_joint_parent_mat_rest(par, NULL, root, node);
807                         mul_m4_m4m4(temp, par, matfra);
808
809                         // evaluate_joint_world_transform_at_frame(temp, NULL, node, fra);
810
811                         // calc special matrix
812                         mul_m4_series(mat, irest, temp, irest_dae, rest);
813                 }
814                 else {
815                         copy_m4_m4(mat, matfra);
816                 }
817
818                 float rot[4], loc[3], scale[3];
819
820                 mat4_to_quat(rot, mat);
821                 /*for ( int i = 0 ; i < 4  ;  i ++ )
822                    {
823                    rot[i] = RAD2DEGF(rot[i]);
824                    }*/
825                 copy_v3_v3(loc, mat[3]);
826                 mat4_to_size(scale, mat);
827
828                 // add keys
829                 for (int i = 0; i < totcu; i++) {
830                         if (i < 4)
831                                 add_bezt(newcu[i], fra, rot[i]);
832                         else if (i < 7)
833                                 add_bezt(newcu[i], fra, loc[i - 4]);
834                         else
835                                 add_bezt(newcu[i], fra, scale[i - 7]);
836                 }
837         }
838         verify_adt_action((ID *)&ob->id, 1);
839
840         ListBase *curves = &ob->adt->action->curves;
841
842         // add curves
843         for (int i = 0; i < totcu; i++) {
844                 if (is_joint)
845                         add_bone_fcurve(ob, node, newcu[i]);
846                 else
847                         BLI_addtail(curves, newcu[i]);
848                 // fcurve_is_used(newcu[i]);  // never added to unused
849         }
850
851
852         if (is_joint) {
853                 bPoseChannel *chan = BKE_pose_channel_find_name(ob->pose, bone_name);
854                 chan->rotmode = ROT_MODE_QUAT;
855         }
856         else {
857                 ob->rotmode = ROT_MODE_QUAT;
858         }
859
860         return;
861
862 }
863
864 /*
865  * This function returns the aspet ration from the Collada camera.
866  *
867  * Note:COLLADA allows to specify either XFov, or YFov alone. 
868  * In that case the aspect ratio can be determined from
869  * the viewport aspect ratio (which is 1:1 ?)
870  * XXX: check this: its probably wrong!
871  * If both values are specified, then the aspect ration is simply xfov/yfov
872  * and if aspect ratio is efined, then .. well then its that one.
873  */
874 static const double get_aspect_ratio(const COLLADAFW::Camera *camera)
875 {
876         double aspect =  camera->getAspectRatio().getValue();
877
878         if (aspect == 0) {
879                 const double yfov   =  camera->getYFov().getValue();
880
881                 if (yfov == 0) {
882                         aspect = 1; // assume yfov and xfov are equal
883                 }
884                 else {
885                         const double xfov   =  camera->getXFov().getValue();
886                         if (xfov==0)
887                                 aspect = 1;
888                         else
889                                 aspect = xfov / yfov;
890                 }
891         }
892         return aspect;
893 }
894
895
896 void AnimationImporter::translate_Animations(COLLADAFW::Node *node,
897                                              std::map<COLLADAFW::UniqueId, COLLADAFW::Node *>& root_map,
898                                              std::multimap<COLLADAFW::UniqueId, Object *>& object_map,
899                                              std::map<COLLADAFW::UniqueId, const COLLADAFW::Object *> FW_object_map)
900 {
901         bool is_joint = node->getType() == COLLADAFW::Node::JOINT;
902         COLLADAFW::UniqueId uid = node->getUniqueId();
903         COLLADAFW::Node *root = root_map.find(uid) == root_map.end() ? node : root_map[uid];
904
905         Object *ob;
906         if (is_joint)
907                 ob = armature_importer->get_armature_for_joint(root);
908         else
909                 ob = object_map.find(uid) == object_map.end() ? NULL : object_map.find(uid)->second;
910
911         if (!ob) {
912                 fprintf(stderr, "cannot find Object for Node with id=\"%s\"\n", node->getOriginalId().c_str());
913                 return;
914         }
915
916
917         AnimationImporter::AnimMix *animType = get_animation_type(node, FW_object_map);
918         bAction *act;
919
920         if ( (animType->transform) != 0) {
921                 /* const char *bone_name = is_joint ? bc_get_joint_name(node) : NULL; */ /* UNUSED */
922                 char joint_path[200];
923
924                 if (is_joint)
925                         armature_importer->get_rna_path_for_joint(node, joint_path, sizeof(joint_path));
926
927
928                 if (!ob->adt || !ob->adt->action) act = verify_adt_action((ID *)&ob->id, 1);
929                 else act = ob->adt->action;
930
931                 //Get the list of animation curves of the object
932                 ListBase *AnimCurves = &(act->curves);
933
934                 const COLLADAFW::TransformationPointerArray& nodeTransforms = node->getTransformations();
935
936                 //for each transformation in node 
937                 for (unsigned int i = 0; i < nodeTransforms.getCount(); i++) {
938                         COLLADAFW::Transformation *transform = nodeTransforms[i];
939                         COLLADAFW::Transformation::TransformationType tm_type = transform->getTransformationType();
940
941                         bool is_rotation = tm_type == COLLADAFW::Transformation::ROTATE;
942                         bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX;
943
944                         const COLLADAFW::UniqueId& listid = transform->getAnimationList();
945
946                         //check if transformation has animations
947                         if (animlist_map.find(listid) == animlist_map.end()) {
948                                 continue;
949                         }
950                         else {
951                                 //transformation has animations
952                                 const COLLADAFW::AnimationList *animlist = animlist_map[listid];
953                                 const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings();
954                                 //all the curves belonging to the current binding
955                                 std::vector<FCurve *> animcurves;
956                                 for (unsigned int j = 0; j < bindings.getCount(); j++) {
957                                         animcurves = curve_map[bindings[j].animation];
958                                         if (is_matrix) {
959                                                 apply_matrix_curves(ob, animcurves, root, node,  transform);
960                                         }
961                                         else {                          
962
963                                                 if (is_joint) {
964                                                         add_bone_animation_sampled(ob, animcurves, root, node, transform);
965                                                 }
966                                                 else {
967                                                         //calculate rnapaths and array index of fcurves according to transformation and animation class
968                                                         Assign_transform_animations(transform, &bindings[j], &animcurves, is_joint, joint_path);
969
970                                                         std::vector<FCurve *>::iterator iter;
971                                                         //Add the curves of the current animation to the object
972                                                         for (iter = animcurves.begin(); iter != animcurves.end(); iter++) {
973                                                                 FCurve *fcu = *iter;
974                                                         
975                                                                 BLI_addtail(AnimCurves, fcu);
976                                                                 fcurve_is_used(fcu);
977                                                         }
978                                                 }
979                                                 
980                                         }
981                                 }
982                         }
983                         if (is_rotation && !is_joint) {
984                                 ob->rotmode = ROT_MODE_EUL;
985                         }
986                 }
987         }
988
989         if ((animType->light) != 0) {
990                 Lamp *lamp  = (Lamp *) ob->data;
991
992                 if (!lamp->adt || !lamp->adt->action) act = verify_adt_action((ID *)&lamp->id, 1);
993                 else act = lamp->adt->action;
994
995                 ListBase *AnimCurves = &(act->curves);
996                 const COLLADAFW::InstanceLightPointerArray& nodeLights = node->getInstanceLights();
997
998                 for (unsigned int i = 0; i < nodeLights.getCount(); i++) {
999                         const COLLADAFW::Light *light = (COLLADAFW::Light *) FW_object_map[nodeLights[i]->getInstanciatedObjectId()];
1000
1001                         if ((animType->light & LIGHT_COLOR) != 0) {
1002                                 const COLLADAFW::Color *col =  &(light->getColor());
1003                                 const COLLADAFW::UniqueId& listid = col->getAnimationList();
1004
1005                                 Assign_color_animations(listid, AnimCurves, "color"); 
1006                         }
1007                         if ((animType->light & LIGHT_FOA) != 0) {
1008                                 const COLLADAFW::AnimatableFloat *foa =  &(light->getFallOffAngle());
1009                                 const COLLADAFW::UniqueId& listid = foa->getAnimationList();
1010
1011                                 Assign_float_animations(listid, AnimCurves, "spot_size");
1012                         }
1013                         if ( (animType->light & LIGHT_FOE) != 0) {
1014                                 const COLLADAFW::AnimatableFloat *foe =  &(light->getFallOffExponent());
1015                                 const COLLADAFW::UniqueId& listid = foe->getAnimationList();
1016
1017                                 Assign_float_animations(listid, AnimCurves, "spot_blend");
1018
1019                         }
1020                 }
1021         }
1022
1023         if (animType->camera != 0) {
1024                 Camera *cam  = (Camera *) ob->data;
1025                 if (!cam->adt || !cam->adt->action)
1026                         act = verify_adt_action((ID *)&cam->id, 1);
1027                 else
1028                         act = cam->adt->action;
1029
1030                 ListBase *AnimCurves = &(act->curves);
1031                 const COLLADAFW::InstanceCameraPointerArray& nodeCameras = node->getInstanceCameras();
1032
1033                 for (unsigned int i = 0; i < nodeCameras.getCount(); i++) {
1034                         const COLLADAFW::Camera *camera = (COLLADAFW::Camera *) FW_object_map[nodeCameras[i]->getInstanciatedObjectId()];
1035
1036                         if ((animType->camera & CAMERA_XFOV) != 0) {
1037                                 const COLLADAFW::AnimatableFloat *xfov =  &(camera->getXFov());
1038                                 const COLLADAFW::UniqueId& listid = xfov->getAnimationList();
1039                                 double aspect = get_aspect_ratio(camera); 
1040                                 Assign_lens_animations(listid, AnimCurves, aspect, cam, "lens", CAMERA_XFOV);
1041                         }
1042
1043                         else if ((animType->camera & CAMERA_YFOV) != 0) {
1044                                 const COLLADAFW::AnimatableFloat *yfov =  &(camera->getYFov());
1045                                 const COLLADAFW::UniqueId& listid = yfov->getAnimationList();
1046                                 double aspect = get_aspect_ratio(camera); 
1047                                 Assign_lens_animations(listid, AnimCurves, aspect, cam, "lens", CAMERA_YFOV);
1048                         }
1049
1050                         else if ((animType->camera & CAMERA_XMAG) != 0) {
1051                                 const COLLADAFW::AnimatableFloat *xmag =  &(camera->getXMag());
1052                                 const COLLADAFW::UniqueId& listid = xmag->getAnimationList();
1053                                 Assign_float_animations(listid, AnimCurves, "ortho_scale");
1054                         }
1055
1056                         else if ((animType->camera & CAMERA_YMAG) != 0) {
1057                                 const COLLADAFW::AnimatableFloat *ymag =  &(camera->getYMag());
1058                                 const COLLADAFW::UniqueId& listid = ymag->getAnimationList();
1059                                 Assign_float_animations(listid, AnimCurves, "ortho_scale");
1060                         }
1061
1062                         if ((animType->camera & CAMERA_ZFAR) != 0) {
1063                                 const COLLADAFW::AnimatableFloat *zfar =  &(camera->getFarClippingPlane());
1064                                 const COLLADAFW::UniqueId& listid = zfar->getAnimationList();
1065                                 Assign_float_animations(listid, AnimCurves, "clip_end");
1066                         }
1067
1068                         if ((animType->camera & CAMERA_ZNEAR) != 0) {
1069                                 const COLLADAFW::AnimatableFloat *znear =  &(camera->getNearClippingPlane());
1070                                 const COLLADAFW::UniqueId& listid = znear->getAnimationList();
1071                                 Assign_float_animations(listid, AnimCurves, "clip_start");
1072                         }
1073
1074                 }
1075         }
1076         if (animType->material != 0) {
1077                 Material *ma = give_current_material(ob, 1);
1078                 if (!ma->adt || !ma->adt->action) act = verify_adt_action((ID *)&ma->id, 1);
1079                 else act = ma->adt->action;
1080
1081                 ListBase *AnimCurves = &(act->curves);
1082
1083                 const COLLADAFW::InstanceGeometryPointerArray& nodeGeoms = node->getInstanceGeometries();
1084                 for (unsigned int i = 0; i < nodeGeoms.getCount(); i++) {
1085                         const COLLADAFW::MaterialBindingArray& matBinds = nodeGeoms[i]->getMaterialBindings();
1086                         for (unsigned int j = 0; j < matBinds.getCount(); j++) {
1087                                 const COLLADAFW::UniqueId & matuid = matBinds[j].getReferencedMaterial();
1088                                 const COLLADAFW::Effect *ef = (COLLADAFW::Effect *) (FW_object_map[matuid]);
1089                                 if (ef != NULL) { /* can be NULL [#28909] */
1090                                         const COLLADAFW::CommonEffectPointerArray& commonEffects  =  ef->getCommonEffects();
1091                                         COLLADAFW::EffectCommon *efc = commonEffects[0];
1092                                         if ((animType->material & MATERIAL_SHININESS) != 0) {
1093                                                 const COLLADAFW::FloatOrParam *shin = &(efc->getShininess());
1094                                                 const COLLADAFW::UniqueId& listid =  shin->getAnimationList();
1095                                                 Assign_float_animations(listid, AnimCurves, "specular_hardness");
1096                                         }
1097
1098                                         if ((animType->material & MATERIAL_IOR) != 0) {
1099                                                 const COLLADAFW::FloatOrParam *ior = &(efc->getIndexOfRefraction());
1100                                                 const COLLADAFW::UniqueId& listid =  ior->getAnimationList();
1101                                                 Assign_float_animations(listid, AnimCurves, "raytrace_transparency.ior");
1102                                         }
1103
1104                                         if ((animType->material & MATERIAL_SPEC_COLOR) != 0) {
1105                                                 const COLLADAFW::ColorOrTexture *cot = &(efc->getSpecular());
1106                                                 const COLLADAFW::UniqueId& listid =  cot->getColor().getAnimationList();
1107                                                 Assign_color_animations(listid, AnimCurves, "specular_color");
1108                                         }
1109
1110                                         if ((animType->material & MATERIAL_DIFF_COLOR) != 0) {
1111                                                 const COLLADAFW::ColorOrTexture *cot = &(efc->getDiffuse());
1112                                                 const COLLADAFW::UniqueId& listid =  cot->getColor().getAnimationList();
1113                                                 Assign_color_animations(listid, AnimCurves, "diffuse_color");
1114                                         }
1115                                 }
1116                         }
1117                 }
1118         }
1119
1120         delete animType;
1121 }
1122
1123 void AnimationImporter::add_bone_animation_sampled(Object *ob, std::vector<FCurve *>& animcurves, COLLADAFW::Node *root, COLLADAFW::Node *node, COLLADAFW::Transformation *tm)
1124 {
1125         const char *bone_name = bc_get_joint_name(node);
1126         char joint_path[200];
1127         armature_importer->get_rna_path_for_joint(node, joint_path, sizeof(joint_path));
1128
1129         std::vector<float> frames;
1130         find_frames(&frames, &animcurves);
1131
1132         // convert degrees to radians
1133         if (tm->getTransformationType() == COLLADAFW::Transformation::ROTATE) {
1134
1135                 std::vector<FCurve *>::iterator iter;
1136                 for (iter = animcurves.begin(); iter != animcurves.end(); iter++) {
1137                         FCurve *fcu = *iter;
1138
1139                         fcurve_deg_to_rad(fcu);
1140                 }
1141         }
1142
1143
1144         float irest_dae[4][4];
1145         float rest[4][4], irest[4][4];
1146
1147         get_joint_rest_mat(irest_dae, root, node);
1148         invert_m4(irest_dae);
1149
1150         Bone *bone = BKE_armature_find_bone_name((bArmature *)ob->data, bone_name);
1151         if (!bone) {
1152                 fprintf(stderr, "cannot find bone \"%s\"\n", bone_name);
1153                 return;
1154         }
1155
1156         unit_m4(rest);
1157         copy_m4_m4(rest, bone->arm_mat);
1158         invert_m4_m4(irest, rest);
1159
1160         // new curves to assign matrix transform animation
1161         FCurve *newcu[10]; // if tm_type is matrix, then create 10 curves: 4 rot, 3 loc, 3 scale
1162         unsigned int totcu = 10;
1163         const char *tm_str = NULL;
1164         char rna_path[200];
1165         for (int i = 0; i < totcu; i++) {
1166
1167                 int axis = i;
1168
1169                 if (i < 4) {
1170                         tm_str = "rotation_quaternion";
1171                         axis = i;
1172                 }
1173                 else if (i < 7) {
1174                         tm_str = "location";
1175                         axis = i - 4;
1176                 }
1177                 else {
1178                         tm_str = "scale";
1179                         axis = i - 7;
1180                 }
1181
1182
1183                 BLI_snprintf(rna_path, sizeof(rna_path), "%s.%s", joint_path, tm_str);
1184
1185                 newcu[i] = create_fcurve(axis, rna_path);
1186                 newcu[i]->totvert = frames.size();
1187         }
1188
1189         if (frames.size() == 0)
1190                 return;
1191
1192         std::sort(frames.begin(), frames.end());
1193
1194         std::vector<float>::iterator it;
1195
1196         // sample values at each frame
1197         for (it = frames.begin(); it != frames.end(); it++) {
1198                 float fra = *it;
1199
1200                 float mat[4][4];
1201                 float matfra[4][4];
1202
1203                 unit_m4(matfra);
1204
1205                 // calc object-space mat
1206                 evaluate_transform_at_frame(matfra, node, fra);
1207
1208
1209                 // for joints, we need a special matrix
1210                 // special matrix: iR * M * iR_dae * R
1211                 // where R, iR are bone rest and inverse rest mats in world space (Blender bones),
1212                 // iR_dae is joint inverse rest matrix (DAE) and M is an evaluated joint world-space matrix (DAE)
1213                 float temp[4][4], par[4][4];
1214
1215
1216                 // calc M
1217                 calc_joint_parent_mat_rest(par, NULL, root, node);
1218                 mul_m4_m4m4(temp, par, matfra);
1219
1220                 // evaluate_joint_world_transform_at_frame(temp, NULL, node, fra);
1221
1222                 // calc special matrix
1223                 mul_m4_series(mat, irest, temp, irest_dae, rest);
1224
1225                 float rot[4], loc[3], scale[3];
1226
1227                 mat4_to_quat(rot, mat);
1228                 copy_v3_v3(loc, mat[3]);
1229                 mat4_to_size(scale, mat);
1230
1231                 // add keys
1232                 for (int i = 0; i < totcu; i++) {
1233                         if (i < 4)
1234                                 add_bezt(newcu[i], fra, rot[i]);
1235                         else if (i < 7)
1236                                 add_bezt(newcu[i], fra, loc[i - 4]);
1237                         else
1238                                 add_bezt(newcu[i], fra, scale[i - 7]);
1239                 }
1240         }
1241         verify_adt_action((ID *)&ob->id, 1);
1242
1243         // add curves
1244         for (int i = 0; i < totcu; i++) {
1245                 add_bone_fcurve(ob, node, newcu[i]);
1246                 // fcurve_is_used(newcu[i]);  // never added to unused
1247         }
1248
1249         bPoseChannel *chan = BKE_pose_channel_find_name(ob->pose, bone_name);
1250         chan->rotmode = ROT_MODE_QUAT;
1251
1252 }
1253
1254
1255 //Check if object is animated by checking if animlist_map holds the animlist_id of node transforms
1256 AnimationImporter::AnimMix *AnimationImporter::get_animation_type(const COLLADAFW::Node *node,
1257                                                                   std::map<COLLADAFW::UniqueId, const COLLADAFW::Object *> FW_object_map)
1258 {
1259         AnimMix *types = new AnimMix();
1260
1261         const COLLADAFW::TransformationPointerArray& nodeTransforms = node->getTransformations();
1262
1263         //for each transformation in node 
1264         for (unsigned int i = 0; i < nodeTransforms.getCount(); i++) {
1265                 COLLADAFW::Transformation *transform = nodeTransforms[i];
1266                 const COLLADAFW::UniqueId& listid = transform->getAnimationList();
1267
1268                 //check if transformation has animations
1269                 if (animlist_map.find(listid) == animlist_map.end()) {
1270                         continue;
1271                 }
1272                 else {
1273                         types->transform = types->transform | NODE_TRANSFORM;
1274                         break;
1275                 }
1276         }
1277         const COLLADAFW::InstanceLightPointerArray& nodeLights = node->getInstanceLights();
1278
1279         for (unsigned int i = 0; i < nodeLights.getCount(); i++) {
1280                 const COLLADAFW::Light *light = (COLLADAFW::Light *) FW_object_map[nodeLights[i]->getInstanciatedObjectId()];
1281                 types->light = setAnimType(&(light->getColor()), (types->light), LIGHT_COLOR);
1282                 types->light = setAnimType(&(light->getFallOffAngle()), (types->light), LIGHT_FOA);
1283                 types->light = setAnimType(&(light->getFallOffExponent()), (types->light), LIGHT_FOE);
1284
1285                 if (types->light != 0) break;
1286
1287         }
1288
1289         const COLLADAFW::InstanceCameraPointerArray& nodeCameras = node->getInstanceCameras();
1290         for (unsigned int i = 0; i < nodeCameras.getCount(); i++) {
1291                 const COLLADAFW::Camera *camera  = (COLLADAFW::Camera *) FW_object_map[nodeCameras[i]->getInstanciatedObjectId()];
1292                 if ( camera == NULL ) {
1293                         // Can happen if the node refers to an unknown camera.
1294                         continue;
1295                 }
1296
1297                 const bool is_perspective_type   = camera->getCameraType() == COLLADAFW::Camera::PERSPECTIVE;
1298
1299                 int addition;
1300                 const COLLADAFW::Animatable *mag;
1301                 const COLLADAFW::UniqueId listid = camera->getYMag().getAnimationList();
1302                 if (animlist_map.find(listid) != animlist_map.end()) {
1303                         mag = &(camera->getYMag());
1304                         addition = (is_perspective_type) ? CAMERA_YFOV: CAMERA_YMAG;
1305                 }
1306                 else {
1307                         mag = &(camera->getXMag());
1308                         addition = (is_perspective_type) ? CAMERA_XFOV: CAMERA_XMAG;
1309                 }
1310                 types->camera = setAnimType(mag, (types->camera), addition);
1311
1312                 types->camera = setAnimType(&(camera->getFarClippingPlane()), (types->camera), CAMERA_ZFAR);
1313                 types->camera = setAnimType(&(camera->getNearClippingPlane()), (types->camera), CAMERA_ZNEAR);
1314
1315                 if (types->camera != 0) break;
1316
1317         }
1318
1319         const COLLADAFW::InstanceGeometryPointerArray& nodeGeoms = node->getInstanceGeometries();
1320         for (unsigned int i = 0; i < nodeGeoms.getCount(); i++) {
1321                 const COLLADAFW::MaterialBindingArray& matBinds = nodeGeoms[i]->getMaterialBindings();
1322                 for (unsigned int j = 0; j < matBinds.getCount(); j++) {
1323                         const COLLADAFW::UniqueId & matuid = matBinds[j].getReferencedMaterial();
1324                         const COLLADAFW::Effect *ef = (COLLADAFW::Effect *) (FW_object_map[matuid]);
1325                         if (ef != NULL) { /* can be NULL [#28909] */
1326                                 const COLLADAFW::CommonEffectPointerArray& commonEffects = ef->getCommonEffects();
1327                                 if (!commonEffects.empty()) {
1328                                         COLLADAFW::EffectCommon *efc = commonEffects[0];
1329                                         types->material =  setAnimType(&(efc->getShininess()), (types->material), MATERIAL_SHININESS);
1330                                         types->material =  setAnimType(&(efc->getSpecular().getColor()), (types->material), MATERIAL_SPEC_COLOR);
1331                                         types->material =  setAnimType(&(efc->getDiffuse().getColor()), (types->material), MATERIAL_DIFF_COLOR);
1332                                         // types->material =  setAnimType(&(efc->get()), (types->material), MATERIAL_TRANSPARENCY);
1333                                         types->material =  setAnimType(&(efc->getIndexOfRefraction()), (types->material), MATERIAL_IOR);
1334                                 }
1335                         }
1336                 }
1337         }
1338         return types;
1339 }
1340
1341 int AnimationImporter::setAnimType(const COLLADAFW::Animatable *prop, int types, int addition)
1342 {
1343         int anim_type;
1344         const COLLADAFW::UniqueId& listid       = prop->getAnimationList();
1345         if (animlist_map.find(listid) != animlist_map.end())
1346                 anim_type =  types | addition;
1347         else
1348                 anim_type = types;
1349
1350         return anim_type;
1351 }               
1352
1353 // Is not used anymore.
1354 void AnimationImporter::find_frames_old(std::vector<float> *frames, COLLADAFW::Node *node, COLLADAFW::Transformation::TransformationType tm_type)
1355 {
1356         bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX;
1357         bool is_rotation = tm_type == COLLADAFW::Transformation::ROTATE;
1358         // for each <rotate>, <translate>, etc. there is a separate Transformation
1359         const COLLADAFW::TransformationPointerArray& nodeTransforms = node->getTransformations();
1360
1361         unsigned int i;
1362         // find frames at which to sample plus convert all rotation keys to radians
1363         for (i = 0; i < nodeTransforms.getCount(); i++) {
1364                 COLLADAFW::Transformation *transform = nodeTransforms[i];
1365                 COLLADAFW::Transformation::TransformationType nodeTmType = transform->getTransformationType();
1366
1367
1368                 if (nodeTmType == tm_type) {
1369                         //get animation bindings for the current transformation
1370                         const COLLADAFW::UniqueId& listid = transform->getAnimationList();
1371                         //if transform is animated its animlist must exist.
1372                         if (animlist_map.find(listid) != animlist_map.end()) {
1373                                 
1374                                 const COLLADAFW::AnimationList *animlist = animlist_map[listid];
1375                                 const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings();
1376
1377                                 if (bindings.getCount()) {
1378                                         //for each AnimationBinding get the fcurves which animate the transform
1379                                         for (unsigned int j = 0; j < bindings.getCount(); j++) {
1380                                                 std::vector<FCurve *>& curves = curve_map[bindings[j].animation];
1381                                                 bool xyz = ((nodeTmType == COLLADAFW::Transformation::TRANSLATE || nodeTmType == COLLADAFW::Transformation::SCALE) && bindings[j].animationClass == COLLADAFW::AnimationList::POSITION_XYZ);
1382
1383                                                 if ((!xyz && curves.size() == 1) || (xyz && curves.size() == 3) || is_matrix) {
1384                                                         std::vector<FCurve *>::iterator iter;
1385
1386                                                         for (iter = curves.begin(); iter != curves.end(); iter++) {
1387                                                                 FCurve *fcu = *iter;
1388
1389                                                                 //if transform is rotation the fcurves values must be turned in to radian.
1390                                                                 if (is_rotation)
1391                                                                         fcurve_deg_to_rad(fcu);
1392
1393                                                                 for (unsigned int k = 0; k < fcu->totvert; k++) {
1394                                                                         //get frame value from bezTriple
1395                                                                         float fra = fcu->bezt[k].vec[1][0];
1396                                                                         //if frame already not added add frame to frames
1397                                                                         if (std::find(frames->begin(), frames->end(), fra) == frames->end())
1398                                                                                 frames->push_back(fra);
1399                                                                 }
1400                                                         }
1401                                                 }
1402                                                 else {
1403                                                         fprintf(stderr, "expected %d curves, got %d\n", xyz ? 3 : 1, (int)curves.size());
1404                                                 }
1405                                         }
1406                                 }
1407                         }
1408                 }
1409         }
1410 }
1411
1412
1413
1414 // prerequisites:
1415 // animlist_map - map animlist id -> animlist
1416 // curve_map - map anim id -> curve(s)
1417 Object *AnimationImporter::translate_animation_OLD(COLLADAFW::Node *node,
1418                                                    std::map<COLLADAFW::UniqueId, Object *>& object_map,
1419                                                    std::map<COLLADAFW::UniqueId, COLLADAFW::Node *>& root_map,
1420                                                    COLLADAFW::Transformation::TransformationType tm_type,
1421                                                    Object *par_job)
1422 {
1423         
1424         bool is_rotation = tm_type == COLLADAFW::Transformation::ROTATE;
1425         bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX;
1426         bool is_joint = node->getType() == COLLADAFW::Node::JOINT;
1427         
1428         COLLADAFW::Node *root = root_map.find(node->getUniqueId()) == root_map.end() ? node : root_map[node->getUniqueId()];
1429         Object *ob = is_joint ? armature_importer->get_armature_for_joint(node) : object_map[node->getUniqueId()];
1430         const char *bone_name = is_joint ? bc_get_joint_name(node) : NULL;
1431         if (!ob) {
1432                 fprintf(stderr, "cannot find Object for Node with id=\"%s\"\n", node->getOriginalId().c_str());
1433                 return NULL;
1434         }
1435
1436         // frames at which to sample
1437         std::vector<float> frames;
1438         
1439         find_frames_old(&frames, node, tm_type);
1440         
1441         unsigned int i;
1442         
1443         float irest_dae[4][4];
1444         float rest[4][4], irest[4][4];
1445
1446         if (is_joint) {
1447                 get_joint_rest_mat(irest_dae, root, node);
1448                 invert_m4(irest_dae);
1449
1450                 Bone *bone = BKE_armature_find_bone_name((bArmature *)ob->data, bone_name);
1451                 if (!bone) {
1452                         fprintf(stderr, "cannot find bone \"%s\"\n", bone_name);
1453                         return NULL;
1454                 }
1455
1456                 unit_m4(rest);
1457                 copy_m4_m4(rest, bone->arm_mat);
1458                 invert_m4_m4(irest, rest);
1459         }
1460
1461         Object *job = NULL;
1462
1463 #ifdef ARMATURE_TEST
1464         FCurve *job_curves[10];
1465         job = get_joint_object(root, node, par_job);
1466 #endif
1467
1468         if (frames.size() == 0)
1469                 return job;
1470
1471         std::sort(frames.begin(), frames.end());
1472
1473         const char *tm_str = NULL;
1474         switch (tm_type) {
1475                 case COLLADAFW::Transformation::ROTATE:
1476                         tm_str = "rotation_quaternion";
1477                         break;
1478                 case COLLADAFW::Transformation::SCALE:
1479                         tm_str = "scale";
1480                         break;
1481                 case COLLADAFW::Transformation::TRANSLATE:
1482                         tm_str = "location";
1483                         break;
1484                 case COLLADAFW::Transformation::MATRIX:
1485                         break;
1486                 default:
1487                         return job;
1488         }
1489
1490         char rna_path[200];
1491         char joint_path[200];
1492
1493         if (is_joint)
1494                 armature_importer->get_rna_path_for_joint(node, joint_path, sizeof(joint_path));
1495
1496         // new curves
1497         FCurve *newcu[10]; // if tm_type is matrix, then create 10 curves: 4 rot, 3 loc, 3 scale
1498         unsigned int totcu = is_matrix ? 10 : (is_rotation ? 4 : 3);
1499
1500         for (i = 0; i < totcu; i++) {
1501
1502                 int axis = i;
1503
1504                 if (is_matrix) {
1505                         if (i < 4) {
1506                                 tm_str = "rotation_quaternion";
1507                                 axis = i;
1508                         }
1509                         else if (i < 7) {
1510                                 tm_str = "location";
1511                                 axis = i - 4;
1512                         }
1513                         else {
1514                                 tm_str = "scale";
1515                                 axis = i - 7;
1516                         }
1517                 }
1518
1519                 if (is_joint)
1520                         BLI_snprintf(rna_path, sizeof(rna_path), "%s.%s", joint_path, tm_str);
1521                 else
1522                         BLI_strncpy(rna_path, tm_str, sizeof(rna_path));
1523                 newcu[i] = create_fcurve(axis, rna_path);
1524
1525 #ifdef ARMATURE_TEST
1526                 if (is_joint)
1527                         job_curves[i] = create_fcurve(axis, tm_str);
1528 #endif
1529         }
1530
1531         std::vector<float>::iterator it;
1532
1533         // sample values at each frame
1534         for (it = frames.begin(); it != frames.end(); it++) {
1535                 float fra = *it;
1536
1537                 float mat[4][4];
1538                 float matfra[4][4];
1539
1540                 unit_m4(matfra);
1541
1542                 // calc object-space mat
1543                 evaluate_transform_at_frame(matfra, node, fra);
1544
1545                 // for joints, we need a special matrix
1546                 if (is_joint) {
1547                         // special matrix: iR * M * iR_dae * R
1548                         // where R, iR are bone rest and inverse rest mats in world space (Blender bones),
1549                         // iR_dae is joint inverse rest matrix (DAE) and M is an evaluated joint world-space matrix (DAE)
1550                         float temp[4][4], par[4][4];
1551
1552                         // calc M
1553                         calc_joint_parent_mat_rest(par, NULL, root, node);
1554                         mul_m4_m4m4(temp, par, matfra);
1555
1556                         // evaluate_joint_world_transform_at_frame(temp, NULL, node, fra);
1557
1558                         // calc special matrix
1559                         mul_m4_series(mat, irest, temp, irest_dae, rest);
1560                 }
1561                 else {
1562                         copy_m4_m4(mat, matfra);
1563                 }
1564
1565                 float val[4], rot[4], loc[3], scale[3];
1566
1567                 switch (tm_type) {
1568                         case COLLADAFW::Transformation::ROTATE:
1569                                 mat4_to_quat(val, mat);
1570                                 break;
1571                         case COLLADAFW::Transformation::SCALE:
1572                                 mat4_to_size(val, mat);
1573                                 break;
1574                         case COLLADAFW::Transformation::TRANSLATE:
1575                                 copy_v3_v3(val, mat[3]);
1576                                 break;
1577                         case COLLADAFW::Transformation::MATRIX:
1578                                 mat4_to_quat(rot, mat);
1579                                 copy_v3_v3(loc, mat[3]);
1580                                 mat4_to_size(scale, mat);
1581                                 break;
1582                         default:
1583                                 break;
1584                 }
1585
1586                 // add keys
1587                 for (i = 0; i < totcu; i++) {
1588                         if (is_matrix) {
1589                                 if (i < 4)
1590                                         add_bezt(newcu[i], fra, rot[i]);
1591                                 else if (i < 7)
1592                                         add_bezt(newcu[i], fra, loc[i - 4]);
1593                                 else
1594                                         add_bezt(newcu[i], fra, scale[i - 7]);
1595                         }
1596                         else {
1597                                 add_bezt(newcu[i], fra, val[i]);
1598                         }
1599                 }
1600
1601 #ifdef ARMATURE_TEST
1602                 if (is_joint) {
1603                         switch (tm_type) {
1604                                 case COLLADAFW::Transformation::ROTATE:
1605                                         mat4_to_quat(val, matfra);
1606                                         break;
1607                                 case COLLADAFW::Transformation::SCALE:
1608                                         mat4_to_size(val, matfra);
1609                                         break;
1610                                 case COLLADAFW::Transformation::TRANSLATE:
1611                                         copy_v3_v3(val, matfra[3]);
1612                                         break;
1613                                 case MATRIX:
1614                                         mat4_to_quat(rot, matfra);
1615                                         copy_v3_v3(loc, matfra[3]);
1616                                         mat4_to_size(scale, matfra);
1617                                         break;
1618                                 default:
1619                                         break;
1620                         }
1621
1622                         for (i = 0; i < totcu; i++) {
1623                                 if (is_matrix) {
1624                                         if (i < 4)
1625                                                 add_bezt(job_curves[i], fra, rot[i]);
1626                                         else if (i < 7)
1627                                                 add_bezt(job_curves[i], fra, loc[i - 4]);
1628                                         else
1629                                                 add_bezt(job_curves[i], fra, scale[i - 7]);
1630                                 }
1631                                 else {
1632                                         add_bezt(job_curves[i], fra, val[i]);
1633                                 }
1634                         }
1635                 }
1636 #endif
1637         }
1638
1639         verify_adt_action((ID *)&ob->id, 1);
1640
1641         ListBase *curves = &ob->adt->action->curves;
1642
1643         // add curves
1644         for (i = 0; i < totcu; i++) {
1645                 if (is_joint)
1646                         add_bone_fcurve(ob, node, newcu[i]);
1647                 else
1648                         BLI_addtail(curves, newcu[i]);
1649
1650 #ifdef ARMATURE_TEST
1651                 if (is_joint)
1652                         BLI_addtail(&job->adt->action->curves, job_curves[i]);
1653 #endif
1654         }
1655
1656         if (is_rotation || is_matrix) {
1657                 if (is_joint) {
1658                         bPoseChannel *chan = BKE_pose_channel_find_name(ob->pose, bone_name);
1659                         chan->rotmode = ROT_MODE_QUAT;
1660                 }
1661                 else {
1662                         ob->rotmode = ROT_MODE_QUAT;
1663                 }
1664         }
1665
1666         return job;
1667 }
1668
1669 // internal, better make it private
1670 // warning: evaluates only rotation and only assigns matrix transforms now
1671 // prerequisites: animlist_map, curve_map
1672 void AnimationImporter::evaluate_transform_at_frame(float mat[4][4], COLLADAFW::Node *node, float fra)
1673 {
1674         const COLLADAFW::TransformationPointerArray& tms = node->getTransformations();
1675
1676         unit_m4(mat);
1677
1678         for (unsigned int i = 0; i < tms.getCount(); i++) {
1679                 COLLADAFW::Transformation *tm = tms[i];
1680                 COLLADAFW::Transformation::TransformationType type = tm->getTransformationType();
1681                 float m[4][4];
1682
1683                 unit_m4(m);
1684
1685                 std::string nodename = node->getName().size() ? node->getName() : node->getOriginalId();
1686                 if (!evaluate_animation(tm, m, fra, nodename.c_str())) {
1687                         switch (type) {
1688                                 case COLLADAFW::Transformation::ROTATE:
1689                                         dae_rotate_to_mat4(tm, m);
1690                                         break;
1691                                 case COLLADAFW::Transformation::TRANSLATE:
1692                                         dae_translate_to_mat4(tm, m);
1693                                         break;
1694                                 case COLLADAFW::Transformation::SCALE:
1695                                         dae_scale_to_mat4(tm, m);
1696                                         break;
1697                                 case COLLADAFW::Transformation::MATRIX:
1698                                         dae_matrix_to_mat4(tm, m);
1699                                         break;
1700                                 default:
1701                                         fprintf(stderr, "unsupported transformation type %d\n", type);
1702                         }
1703                 }
1704
1705                 float temp[4][4];
1706                 copy_m4_m4(temp, mat);
1707
1708                 mul_m4_m4m4(mat, temp, m);
1709         }
1710 }
1711
1712 static void report_class_type_unsupported(const char *path, 
1713                                          const COLLADAFW::AnimationList::AnimationClass animclass,
1714                                          const COLLADAFW::Transformation::TransformationType type) 
1715 {
1716         if (animclass == COLLADAFW::AnimationList::UNKNOWN_CLASS) {
1717                 fprintf(stderr, "%s: UNKNOWN animation class\n", path);
1718         }
1719         else {
1720                 fprintf(stderr, "%s: animation class %d is not supported yet for transformation type %d\n", path, animclass, type);
1721         }
1722 }
1723
1724 // return true to indicate that mat contains a sane value
1725 bool AnimationImporter::evaluate_animation(COLLADAFW::Transformation *tm, float mat[4][4], float fra, const char *node_id)
1726 {
1727         const COLLADAFW::UniqueId& listid = tm->getAnimationList();
1728         COLLADAFW::Transformation::TransformationType type = tm->getTransformationType();
1729
1730         if (type != COLLADAFW::Transformation::ROTATE &&
1731             type != COLLADAFW::Transformation::SCALE &&
1732             type != COLLADAFW::Transformation::TRANSLATE &&
1733             type != COLLADAFW::Transformation::MATRIX) {
1734                 fprintf(stderr, "animation of transformation %d is not supported yet\n", type);
1735                 return false;
1736         }
1737
1738         if (animlist_map.find(listid) == animlist_map.end())
1739                 return false;
1740
1741         const COLLADAFW::AnimationList *animlist = animlist_map[listid];
1742         const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings();
1743
1744         if (bindings.getCount()) {
1745                 float vec[3];
1746
1747                 bool is_scale = (type == COLLADAFW::Transformation::SCALE);
1748                 bool is_translate = (type == COLLADAFW::Transformation::TRANSLATE);
1749
1750                 if (is_scale)
1751                         dae_scale_to_v3(tm, vec);
1752                 else if (is_translate)
1753                         dae_translate_to_v3(tm, vec);
1754
1755                 for (unsigned int j = 0; j < bindings.getCount(); j++) {
1756                         const COLLADAFW::AnimationList::AnimationBinding& binding = bindings[j];
1757                         std::vector<FCurve *>& curves = curve_map[binding.animation];
1758                         COLLADAFW::AnimationList::AnimationClass animclass = binding.animationClass;
1759                         char path[100];
1760
1761                         switch (type) {
1762                                 case COLLADAFW::Transformation::ROTATE:
1763                                         BLI_snprintf(path, sizeof(path), "%s.rotate (binding %u)", node_id, j);
1764                                         break;
1765                                 case COLLADAFW::Transformation::SCALE:
1766                                         BLI_snprintf(path, sizeof(path), "%s.scale (binding %u)", node_id, j);
1767                                         break;
1768                                 case COLLADAFW::Transformation::TRANSLATE:
1769                                         BLI_snprintf(path, sizeof(path), "%s.translate (binding %u)", node_id, j);
1770                                         break;
1771                                 case COLLADAFW::Transformation::MATRIX:
1772                                         BLI_snprintf(path, sizeof(path), "%s.matrix (binding %u)", node_id, j);
1773                                         break;
1774                                 default:
1775                                         break;
1776                         }
1777
1778                         if (type == COLLADAFW::Transformation::ROTATE) {
1779                                 if (curves.size() != 1) {
1780                                         fprintf(stderr, "expected 1 curve, got %d\n", (int)curves.size());
1781                                         return false;
1782                                 }
1783
1784                                 // TODO support other animclasses
1785                                 if (animclass != COLLADAFW::AnimationList::ANGLE) {
1786                                         report_class_type_unsupported(path, animclass, type);
1787                                         return false;
1788                                 }
1789
1790                                 COLLADABU::Math::Vector3& axis = ((COLLADAFW::Rotate *)tm)->getRotationAxis();
1791
1792                                 float ax[3] = {(float)axis[0], (float)axis[1], (float)axis[2]};
1793                                 float angle = evaluate_fcurve(curves[0], fra);
1794                                 axis_angle_to_mat4(mat, ax, angle);
1795
1796                                 return true;
1797                         }
1798                         else if (is_scale || is_translate) {
1799                                 bool is_xyz = animclass == COLLADAFW::AnimationList::POSITION_XYZ;
1800
1801                                 if ((!is_xyz && curves.size() != 1) || (is_xyz && curves.size() != 3)) {
1802                                         if (is_xyz)
1803                                                 fprintf(stderr, "%s: expected 3 curves, got %d\n", path, (int)curves.size());
1804                                         else
1805                                                 fprintf(stderr, "%s: expected 1 curve, got %d\n", path, (int)curves.size());
1806                                         return false;
1807                                 }
1808                                 
1809                                 switch (animclass) {
1810                                         case COLLADAFW::AnimationList::POSITION_X:
1811                                                 vec[0] = evaluate_fcurve(curves[0], fra);
1812                                                 break;
1813                                         case COLLADAFW::AnimationList::POSITION_Y:
1814                                                 vec[1] = evaluate_fcurve(curves[0], fra);
1815                                                 break;
1816                                         case COLLADAFW::AnimationList::POSITION_Z:
1817                                                 vec[2] = evaluate_fcurve(curves[0], fra);
1818                                                 break;
1819                                         case COLLADAFW::AnimationList::POSITION_XYZ:
1820                                                 vec[0] = evaluate_fcurve(curves[0], fra);
1821                                                 vec[1] = evaluate_fcurve(curves[1], fra);
1822                                                 vec[2] = evaluate_fcurve(curves[2], fra);
1823                                                 break;
1824                                         default:
1825                                                 report_class_type_unsupported(path, animclass, type);
1826                                                 break;
1827                                 }
1828                         }
1829                         else if (type == COLLADAFW::Transformation::MATRIX) {
1830                                 // for now, of matrix animation, support only the case when all values are packed into one animation
1831                                 if (curves.size() != 16) {
1832                                         fprintf(stderr, "%s: expected 16 curves, got %d\n", path, (int)curves.size());
1833                                         return false;
1834                                 }
1835
1836                                 COLLADABU::Math::Matrix4 matrix;
1837                                 int i = 0, j = 0;
1838
1839                                 for (std::vector<FCurve *>::iterator it = curves.begin(); it != curves.end(); it++) {
1840                                         matrix.setElement(i, j, evaluate_fcurve(*it, fra));
1841                                         j++;
1842                                         if (j == 4) {
1843                                                 i++;
1844                                                 j = 0;
1845                                         }
1846                                         fcurve_is_used(*it);
1847                                 }
1848
1849                                 COLLADAFW::Matrix tm(matrix);
1850                                 dae_matrix_to_mat4(&tm, mat);
1851
1852                                 std::vector<FCurve *>::iterator it;
1853
1854                                 return true;
1855                         }
1856                 }
1857
1858                 if (is_scale)
1859                         size_to_mat4(mat, vec);
1860                 else
1861                         copy_v3_v3(mat[3], vec);
1862
1863                 return is_scale || is_translate;
1864         }
1865
1866         return false;
1867 }
1868
1869 // gives a world-space mat of joint at rest position
1870 void AnimationImporter::get_joint_rest_mat(float mat[4][4], COLLADAFW::Node *root, COLLADAFW::Node *node)
1871 {
1872         // if bind mat is not available,
1873         // use "current" node transform, i.e. all those tms listed inside <node>
1874         if (!armature_importer->get_joint_bind_mat(mat, node)) {
1875                 float par[4][4], m[4][4];
1876
1877                 calc_joint_parent_mat_rest(par, NULL, root, node);
1878                 get_node_mat(m, node, NULL, NULL);
1879                 mul_m4_m4m4(mat, par, m);
1880         }
1881 }
1882
1883 // gives a world-space mat, end's mat not included
1884 bool AnimationImporter::calc_joint_parent_mat_rest(float mat[4][4], float par[4][4], COLLADAFW::Node *node, COLLADAFW::Node *end)
1885 {
1886         float m[4][4];
1887
1888         if (node == end) {
1889                 par ? copy_m4_m4(mat, par) : unit_m4(mat);
1890                 return true;
1891         }
1892
1893         // use bind matrix if available or calc "current" world mat
1894         if (!armature_importer->get_joint_bind_mat(m, node)) {
1895                 if (par) {
1896                         float temp[4][4];
1897                         get_node_mat(temp, node, NULL, NULL);
1898                         mul_m4_m4m4(m, par, temp);
1899                 }
1900                 else {
1901                         get_node_mat(m, node, NULL, NULL);
1902                 }
1903         }
1904
1905         COLLADAFW::NodePointerArray& children = node->getChildNodes();
1906         for (unsigned int i = 0; i < children.getCount(); i++) {
1907                 if (calc_joint_parent_mat_rest(mat, m, children[i], end))
1908                         return true;
1909         }
1910
1911         return false;
1912 }
1913
1914 #ifdef ARMATURE_TEST
1915 Object *AnimationImporter::get_joint_object(COLLADAFW::Node *root, COLLADAFW::Node *node, Object *par_job)
1916 {
1917         if (joint_objects.find(node->getUniqueId()) == joint_objects.end()) {
1918                 Object *job = bc_add_object(scene, OB_EMPTY, (char *)get_joint_name(node));
1919
1920                 job->lay = BKE_scene_base_find(scene, job)->lay = 2;
1921
1922                 mul_v3_fl(job->size, 0.5f);
1923                 DAG_id_tag_update(&job->id, OB_RECALC_OB);
1924
1925                 verify_adt_action((ID *)&job->id, 1);
1926
1927                 job->rotmode = ROT_MODE_QUAT;
1928
1929                 float mat[4][4];
1930                 get_joint_rest_mat(mat, root, node);
1931
1932                 if (par_job) {
1933                         float temp[4][4], ipar[4][4];
1934                         invert_m4_m4(ipar, par_job->obmat);
1935                         copy_m4_m4(temp, mat);
1936                         mul_m4_m4m4(mat, ipar, temp);
1937                 }
1938
1939                 TransformBase::decompose(mat, job->loc, NULL, job->quat, job->size);
1940
1941                 if (par_job) {
1942                         job->parent = par_job;
1943
1944                         DAG_id_tag_update(&par_job->id, OB_RECALC_OB);
1945                         job->parsubstr[0] = 0;
1946                 }
1947
1948                 BKE_object_where_is_calc(scene, job);
1949
1950                 // after parenting and layer change
1951                 DAG_relations_tag_update(CTX_data_main(C));
1952
1953                 joint_objects[node->getUniqueId()] = job;
1954         }
1955
1956         return joint_objects[node->getUniqueId()];
1957 }
1958 #endif
1959
1960 #if 0
1961 // recursively evaluates joint tree until end is found, mat then is world-space matrix of end
1962 // mat must be identity on enter, node must be root
1963 bool AnimationImporter::evaluate_joint_world_transform_at_frame(float mat[4][4], float par[4][4], COLLADAFW::Node *node, COLLADAFW::Node *end, float fra)
1964 {
1965         float m[4][4];
1966         if (par) {
1967                 float temp[4][4];
1968                 evaluate_transform_at_frame(temp, node, node == end ? fra : 0.0f);
1969                 mul_m4_m4m4(m, par, temp);
1970         }
1971         else {
1972                 evaluate_transform_at_frame(m, node, node == end ? fra : 0.0f);
1973         }
1974
1975         if (node == end) {
1976                 copy_m4_m4(mat, m);
1977                 return true;
1978         }
1979         else {
1980                 COLLADAFW::NodePointerArray& children = node->getChildNodes();
1981                 for (int i = 0; i < children.getCount(); i++) {
1982                         if (evaluate_joint_world_transform_at_frame(mat, m, children[i], end, fra))
1983                                 return true;
1984                 }
1985         }
1986
1987         return false;
1988 }
1989 #endif
1990
1991 void AnimationImporter::add_bone_fcurve(Object *ob, COLLADAFW::Node *node, FCurve *fcu)
1992 {
1993         const char *bone_name = bc_get_joint_name(node);
1994         bAction *act = ob->adt->action;
1995                         
1996         /* try to find group */
1997         bActionGroup *grp = BKE_action_group_find_name(act, bone_name);
1998
1999         /* no matching groups, so add one */
2000         if (grp == NULL) {
2001                 /* Add a new group, and make it active */
2002                 grp = (bActionGroup *)MEM_callocN(sizeof(bActionGroup), "bActionGroup");
2003
2004                 grp->flag = AGRP_SELECTED;
2005                 BLI_strncpy(grp->name, bone_name, sizeof(grp->name));
2006
2007                 BLI_addtail(&act->groups, grp);
2008                 BLI_uniquename(&act->groups, grp, CTX_DATA_(BLT_I18NCONTEXT_ID_ACTION, "Group"), '.',
2009                                offsetof(bActionGroup, name), 64);
2010         }
2011
2012         /* add F-Curve to group */
2013         action_groups_add_channel(act, grp, fcu);
2014 }
2015
2016 void AnimationImporter::add_bezt(FCurve *fcu, float fra, float value)
2017 {
2018         //float fps = (float)FPS;
2019         BezTriple bez;
2020         memset(&bez, 0, sizeof(BezTriple));
2021         bez.vec[1][0] = fra;
2022         bez.vec[1][1] = value;
2023         bez.ipo = BEZT_IPO_LIN; /* use default interpolation mode here... */
2024         bez.f1 = bez.f2 = bez.f3 = SELECT;
2025         bez.h1 = bez.h2 = HD_AUTO;
2026         insert_bezt_fcurve(fcu, &bez, 0);
2027         calchandles_fcurve(fcu);
2028 }
2029
2030 void AnimationImporter::set_import_from_version(std::string import_from_version)
2031 {
2032         this->import_from_version = import_from_version;
2033 }