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