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