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