style cleanup
[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_XMAG) != 0) {
938                                 const COLLADAFW::AnimatableFloat *xmag =  &(camera->getXMag());
939                                 const COLLADAFW::UniqueId& listid = xmag->getAnimationList();
940                                 Assign_float_animations(listid, AnimCurves, "ortho_scale");
941                         }
942
943                         if ((animType->camera & CAMERA_ZFAR) != 0) {
944                                 const COLLADAFW::AnimatableFloat *zfar =  &(camera->getFarClippingPlane());
945                                 const COLLADAFW::UniqueId& listid = zfar->getAnimationList();
946                                 Assign_float_animations(listid, AnimCurves, "clip_end");
947                         }
948
949                         if ((animType->camera & CAMERA_ZNEAR) != 0) {
950                                 const COLLADAFW::AnimatableFloat *znear =  &(camera->getNearClippingPlane());
951                                 const COLLADAFW::UniqueId& listid = znear->getAnimationList();
952                                 Assign_float_animations(listid, AnimCurves, "clip_start");
953                         }
954
955                 }
956         }
957         if (animType->material != 0) {
958                 Material *ma = give_current_material(ob, 1);
959                 if (!ma->adt || !ma->adt->action) act = verify_adt_action((ID *)&ma->id, 1);
960                 else act = ma->adt->action;
961
962                 ListBase *AnimCurves = &(act->curves);
963
964                 const COLLADAFW::InstanceGeometryPointerArray& nodeGeoms = node->getInstanceGeometries();
965                 for (unsigned int i = 0; i < nodeGeoms.getCount(); i++) {
966                         const COLLADAFW::MaterialBindingArray& matBinds = nodeGeoms[i]->getMaterialBindings();
967                         for (unsigned int j = 0; j < matBinds.getCount(); j++) {
968                                 const COLLADAFW::UniqueId & matuid = matBinds[j].getReferencedMaterial();
969                                 const COLLADAFW::Effect *ef = (COLLADAFW::Effect *) (FW_object_map[matuid]);
970                                 if (ef != NULL) { /* can be NULL [#28909] */
971                                         const COLLADAFW::CommonEffectPointerArray& commonEffects  =  ef->getCommonEffects();
972                                         COLLADAFW::EffectCommon *efc = commonEffects[0];
973                                         if ((animType->material & MATERIAL_SHININESS) != 0) {
974                                                 const COLLADAFW::FloatOrParam *shin = &(efc->getShininess());
975                                                 const COLLADAFW::UniqueId& listid =  shin->getAnimationList();
976                                                 Assign_float_animations(listid, AnimCurves, "specular_hardness");
977                                         }
978
979                                         if ((animType->material & MATERIAL_IOR) != 0) {
980                                                 const COLLADAFW::FloatOrParam *ior = &(efc->getIndexOfRefraction());
981                                                 const COLLADAFW::UniqueId& listid =  ior->getAnimationList();
982                                                 Assign_float_animations(listid, AnimCurves, "raytrace_transparency.ior");
983                                         }
984
985                                         if ((animType->material & MATERIAL_SPEC_COLOR) != 0) {
986                                                 const COLLADAFW::ColorOrTexture *cot = &(efc->getSpecular());
987                                                 const COLLADAFW::UniqueId& listid =  cot->getColor().getAnimationList();
988                                                 Assign_color_animations(listid, AnimCurves, "specular_color");
989                                         }
990
991                                         if ((animType->material & MATERIAL_DIFF_COLOR) != 0) {
992                                                 const COLLADAFW::ColorOrTexture *cot = &(efc->getDiffuse());
993                                                 const COLLADAFW::UniqueId& listid =  cot->getColor().getAnimationList();
994                                                 Assign_color_animations(listid, AnimCurves, "diffuse_color");
995                                         }
996                                 }
997                         }
998                 }       
999         }
1000 }
1001
1002 void AnimationImporter::add_bone_animation_sampled(Object *ob, std::vector<FCurve *>& animcurves, COLLADAFW::Node *root, COLLADAFW::Node *node, COLLADAFW::Transformation *tm)
1003 {
1004         const char *bone_name = bc_get_joint_name(node);
1005         char joint_path[200];
1006         armature_importer->get_rna_path_for_joint(node, joint_path, sizeof(joint_path));
1007
1008         std::vector<float> frames;
1009         find_frames(&frames, &animcurves);
1010
1011         // convert degrees to radians
1012         if (tm->getTransformationType() == COLLADAFW::Transformation::ROTATE) {
1013
1014                 std::vector<FCurve *>::iterator iter;
1015                 for (iter = animcurves.begin(); iter != animcurves.end(); iter++) {
1016                         FCurve *fcu = *iter;
1017
1018                         fcurve_deg_to_rad(fcu);          
1019                 }                                       
1020         }
1021
1022
1023         float irest_dae[4][4];
1024         float rest[4][4], irest[4][4];
1025
1026         get_joint_rest_mat(irest_dae, root, node);
1027         invert_m4(irest_dae);
1028
1029         Bone *bone = BKE_armature_find_bone_name((bArmature *)ob->data, bone_name);
1030         if (!bone) {
1031                 fprintf(stderr, "cannot find bone \"%s\"\n", bone_name);
1032                 return;
1033         }
1034
1035         unit_m4(rest);
1036         copy_m4_m4(rest, bone->arm_mat);
1037         invert_m4_m4(irest, rest);
1038
1039         // new curves to assign matrix transform animation
1040         FCurve *newcu[10]; // if tm_type is matrix, then create 10 curves: 4 rot, 3 loc, 3 scale
1041         unsigned int totcu = 10;
1042         const char *tm_str = NULL;
1043         char rna_path[200];
1044         for (int i = 0; i < totcu; i++) {
1045
1046                 int axis = i;
1047
1048                 if (i < 4) {
1049                         tm_str = "rotation_quaternion";
1050                         axis = i;
1051                 }
1052                 else if (i < 7) {
1053                         tm_str = "location";
1054                         axis = i - 4;
1055                 }
1056                 else {
1057                         tm_str = "scale";
1058                         axis = i - 7;
1059                 }
1060
1061
1062                 BLI_snprintf(rna_path, sizeof(rna_path), "%s.%s", joint_path, tm_str);
1063
1064                 newcu[i] = create_fcurve(axis, rna_path);
1065                 newcu[i]->totvert = frames.size();
1066         }
1067
1068         if (frames.size() == 0)
1069                 return;
1070
1071         std::sort(frames.begin(), frames.end());
1072
1073         std::vector<float>::iterator it;
1074
1075         // sample values at each frame
1076         for (it = frames.begin(); it != frames.end(); it++) {
1077                 float fra = *it;
1078
1079                 float mat[4][4];
1080                 float matfra[4][4];
1081
1082                 unit_m4(matfra);
1083
1084                 // calc object-space mat
1085                 evaluate_transform_at_frame(matfra, node, fra);
1086
1087
1088                 // for joints, we need a special matrix
1089                 // special matrix: iR * M * iR_dae * R
1090                 // where R, iR are bone rest and inverse rest mats in world space (Blender bones),
1091                 // iR_dae is joint inverse rest matrix (DAE) and M is an evaluated joint world-space matrix (DAE)
1092                 float temp[4][4], par[4][4];
1093
1094
1095                 // calc M
1096                 calc_joint_parent_mat_rest(par, NULL, root, node);
1097                 mult_m4_m4m4(temp, par, matfra);
1098
1099                 // evaluate_joint_world_transform_at_frame(temp, NULL,, node, fra);
1100
1101                 // calc special matrix
1102                 mul_serie_m4(mat, irest, temp, irest_dae, rest, NULL, NULL, NULL, NULL);
1103
1104                 float rot[4], loc[3], scale[3];
1105
1106                 mat4_to_quat(rot, mat);
1107                 copy_v3_v3(loc, mat[3]);
1108                 mat4_to_size(scale, mat);
1109
1110                 // add keys
1111                 for (int i = 0; i < totcu; i++) {
1112                         if (i < 4)
1113                                 add_bezt(newcu[i], fra, rot[i]);
1114                         else if (i < 7)
1115                                 add_bezt(newcu[i], fra, loc[i - 4]);
1116                         else
1117                                 add_bezt(newcu[i], fra, scale[i - 7]);
1118                 }
1119         }
1120         verify_adt_action((ID *)&ob->id, 1);
1121
1122         // add curves
1123         for (int i = 0; i < totcu; i++) {
1124                 add_bone_fcurve(ob, node, newcu[i]);
1125         }
1126
1127         bPoseChannel *chan = BKE_pose_channel_find_name(ob->pose, bone_name);
1128         chan->rotmode = ROT_MODE_QUAT;
1129
1130 }
1131
1132
1133 //Check if object is animated by checking if animlist_map holds the animlist_id of node transforms
1134 AnimationImporter::AnimMix *AnimationImporter::get_animation_type(const COLLADAFW::Node *node,
1135                                                                   std::map<COLLADAFW::UniqueId, const COLLADAFW::Object *> FW_object_map)
1136 {
1137         AnimMix *types = new AnimMix();
1138
1139         const COLLADAFW::TransformationPointerArray& nodeTransforms = node->getTransformations();
1140
1141         //for each transformation in node 
1142         for (unsigned int i = 0; i < nodeTransforms.getCount(); i++) {
1143                 COLLADAFW::Transformation *transform = nodeTransforms[i];
1144                 const COLLADAFW::UniqueId& listid = transform->getAnimationList();
1145
1146                 //check if transformation has animations
1147                 if (animlist_map.find(listid) == animlist_map.end()) {
1148                         continue;
1149                 }
1150                 else {
1151                         types->transform = types->transform | NODE_TRANSFORM;
1152                         break;
1153                 }
1154         }
1155         const COLLADAFW::InstanceLightPointerArray& nodeLights = node->getInstanceLights();
1156
1157         for (unsigned int i = 0; i < nodeLights.getCount(); i++) {
1158                 const COLLADAFW::Light *light = (COLLADAFW::Light *) FW_object_map[nodeLights[i]->getInstanciatedObjectId()];
1159                 types->light = setAnimType(&(light->getColor()), (types->light), LIGHT_COLOR);
1160                 types->light = setAnimType(&(light->getFallOffAngle()), (types->light), LIGHT_FOA);
1161                 types->light = setAnimType(&(light->getFallOffExponent()), (types->light), LIGHT_FOE);
1162
1163                 if (types->light != 0) break;
1164
1165         }
1166
1167         const COLLADAFW::InstanceCameraPointerArray& nodeCameras = node->getInstanceCameras();
1168         for (unsigned int i = 0; i < nodeCameras.getCount(); i++) {
1169                 const COLLADAFW::Camera *camera = (COLLADAFW::Camera *) FW_object_map[nodeCameras[i]->getInstanciatedObjectId()];
1170
1171                 if (camera->getCameraType() == COLLADAFW::Camera::PERSPECTIVE) {
1172                         types->camera = setAnimType(&(camera->getXMag()), (types->camera), CAMERA_XFOV);
1173                 }
1174                 else {
1175                         types->camera = setAnimType(&(camera->getXMag()), (types->camera), CAMERA_XMAG);
1176                 }
1177                 types->camera = setAnimType(&(camera->getFarClippingPlane()), (types->camera), CAMERA_ZFAR);
1178                 types->camera = setAnimType(&(camera->getNearClippingPlane()), (types->camera), CAMERA_ZNEAR);
1179
1180                 if (types->camera != 0) break;
1181
1182         }
1183
1184         const COLLADAFW::InstanceGeometryPointerArray& nodeGeoms = node->getInstanceGeometries();
1185         for (unsigned int i = 0; i < nodeGeoms.getCount(); i++) {
1186                 const COLLADAFW::MaterialBindingArray& matBinds = nodeGeoms[i]->getMaterialBindings();
1187                 for (unsigned int j = 0; j < matBinds.getCount(); j++) {
1188                         const COLLADAFW::UniqueId & matuid = matBinds[j].getReferencedMaterial();
1189                         const COLLADAFW::Effect *ef = (COLLADAFW::Effect *) (FW_object_map[matuid]);
1190                         if (ef != NULL) { /* can be NULL [#28909] */
1191                                 const COLLADAFW::CommonEffectPointerArray& commonEffects = ef->getCommonEffects();
1192                                 if (!commonEffects.empty()) {
1193                                         COLLADAFW::EffectCommon *efc = commonEffects[0];
1194                                         types->material =  setAnimType(&(efc->getShininess()), (types->material), MATERIAL_SHININESS);
1195                                         types->material =  setAnimType(&(efc->getSpecular().getColor()), (types->material), MATERIAL_SPEC_COLOR);
1196                                         types->material =  setAnimType(&(efc->getDiffuse().getColor()), (types->material), MATERIAL_DIFF_COLOR);
1197                                         // types->material =  setAnimType(&(efc->get()), (types->material), MATERIAL_TRANSPARENCY);
1198                                         types->material =  setAnimType(&(efc->getIndexOfRefraction()), (types->material), MATERIAL_IOR);
1199                                 }
1200                         }
1201                 }
1202         }
1203         return types;
1204 }
1205
1206 int AnimationImporter::setAnimType(const COLLADAFW::Animatable *prop, int types, int addition)
1207 {
1208         const COLLADAFW::UniqueId& listid =  prop->getAnimationList();
1209         if (animlist_map.find(listid) != animlist_map.end())
1210                 return types | addition;
1211         else return types;
1212 }               
1213
1214 // Is not used anymore.
1215 void AnimationImporter::find_frames_old(std::vector<float> *frames, COLLADAFW::Node *node, COLLADAFW::Transformation::TransformationType tm_type)
1216 {
1217         bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX;
1218         bool is_rotation = tm_type == COLLADAFW::Transformation::ROTATE;
1219         // for each <rotate>, <translate>, etc. there is a separate Transformation
1220         const COLLADAFW::TransformationPointerArray& nodeTransforms = node->getTransformations();
1221
1222         unsigned int i;
1223         // find frames at which to sample plus convert all rotation keys to radians
1224         for (i = 0; i < nodeTransforms.getCount(); i++) {
1225                 COLLADAFW::Transformation *transform = nodeTransforms[i];
1226                 COLLADAFW::Transformation::TransformationType nodeTmType = transform->getTransformationType();
1227
1228
1229                 if (nodeTmType == tm_type) {
1230                         //get animation bindings for the current transformation
1231                         const COLLADAFW::UniqueId& listid = transform->getAnimationList();
1232                         //if transform is animated its animlist must exist.
1233                         if (animlist_map.find(listid) != animlist_map.end()) {
1234                                 
1235                                 const COLLADAFW::AnimationList *animlist = animlist_map[listid];
1236                                 const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings();
1237
1238                                 if (bindings.getCount()) {
1239                                         //for each AnimationBinding get the fcurves which animate the transform
1240                                         for (unsigned int j = 0; j < bindings.getCount(); j++) {
1241                                                 std::vector<FCurve *>& curves = curve_map[bindings[j].animation];
1242                                                 bool xyz = ((nodeTmType == COLLADAFW::Transformation::TRANSLATE || nodeTmType == COLLADAFW::Transformation::SCALE) && bindings[j].animationClass == COLLADAFW::AnimationList::POSITION_XYZ);
1243
1244                                                 if ((!xyz && curves.size() == 1) || (xyz && curves.size() == 3) || is_matrix) {
1245                                                         std::vector<FCurve *>::iterator iter;
1246
1247                                                         for (iter = curves.begin(); iter != curves.end(); iter++) {
1248                                                                 FCurve *fcu = *iter;
1249
1250                                                                 //if transform is rotation the fcurves values must be turned in to radian.
1251                                                                 if (is_rotation)
1252                                                                         fcurve_deg_to_rad(fcu);
1253
1254                                                                 for (unsigned int k = 0; k < fcu->totvert; k++) {
1255                                                                         //get frame value from bezTriple
1256                                                                         float fra = fcu->bezt[k].vec[1][0];
1257                                                                         //if frame already not added add frame to frames
1258                                                                         if (std::find(frames->begin(), frames->end(), fra) == frames->end())
1259                                                                                 frames->push_back(fra);
1260                                                                 }
1261                                                         }
1262                                                 }
1263                                                 else {
1264                                                         fprintf(stderr, "expected %d curves, got %d\n", xyz ? 3 : 1, (int)curves.size());
1265                                                 }
1266                                         }
1267                                 }
1268                         }
1269                 }
1270         }
1271 }
1272
1273
1274
1275 // prerequisites:
1276 // animlist_map - map animlist id -> animlist
1277 // curve_map - map anim id -> curve(s)
1278 Object *AnimationImporter::translate_animation_OLD(COLLADAFW::Node *node,
1279                                                    std::map<COLLADAFW::UniqueId, Object *>& object_map,
1280                                                    std::map<COLLADAFW::UniqueId, COLLADAFW::Node *>& root_map,
1281                                                    COLLADAFW::Transformation::TransformationType tm_type,
1282                                                    Object *par_job)
1283 {
1284         
1285         bool is_rotation = tm_type == COLLADAFW::Transformation::ROTATE;
1286         bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX;
1287         bool is_joint = node->getType() == COLLADAFW::Node::JOINT;
1288         
1289         COLLADAFW::Node *root = root_map.find(node->getUniqueId()) == root_map.end() ? node : root_map[node->getUniqueId()];
1290         Object *ob = is_joint ? armature_importer->get_armature_for_joint(node) : object_map[node->getUniqueId()];
1291         const char *bone_name = is_joint ? bc_get_joint_name(node) : NULL;
1292         if (!ob) {
1293                 fprintf(stderr, "cannot find Object for Node with id=\"%s\"\n", node->getOriginalId().c_str());
1294                 return NULL;
1295         }
1296
1297         // frames at which to sample
1298         std::vector<float> frames;
1299         
1300         find_frames_old(&frames, node, tm_type);
1301         
1302         unsigned int i;
1303         
1304         float irest_dae[4][4];
1305         float rest[4][4], irest[4][4];
1306
1307         if (is_joint) {
1308                 get_joint_rest_mat(irest_dae, root, node);
1309                 invert_m4(irest_dae);
1310
1311                 Bone *bone = BKE_armature_find_bone_name((bArmature *)ob->data, bone_name);
1312                 if (!bone) {
1313                         fprintf(stderr, "cannot find bone \"%s\"\n", bone_name);
1314                         return NULL;
1315                 }
1316
1317                 unit_m4(rest);
1318                 copy_m4_m4(rest, bone->arm_mat);
1319                 invert_m4_m4(irest, rest);
1320         }
1321
1322         Object *job = NULL;
1323
1324 #ifdef ARMATURE_TEST
1325         FCurve *job_curves[10];
1326         job = get_joint_object(root, node, par_job);
1327 #endif
1328
1329         if (frames.size() == 0)
1330                 return job;
1331
1332         std::sort(frames.begin(), frames.end());
1333
1334         const char *tm_str = NULL;
1335         switch (tm_type) {
1336                 case COLLADAFW::Transformation::ROTATE:
1337                         tm_str = "rotation_quaternion";
1338                         break;
1339                 case COLLADAFW::Transformation::SCALE:
1340                         tm_str = "scale";
1341                         break;
1342                 case COLLADAFW::Transformation::TRANSLATE:
1343                         tm_str = "location";
1344                         break;
1345                 case COLLADAFW::Transformation::MATRIX:
1346                         break;
1347                 default:
1348                         return job;
1349         }
1350
1351         char rna_path[200];
1352         char joint_path[200];
1353
1354         if (is_joint)
1355                 armature_importer->get_rna_path_for_joint(node, joint_path, sizeof(joint_path));
1356
1357         // new curves
1358         FCurve *newcu[10]; // if tm_type is matrix, then create 10 curves: 4 rot, 3 loc, 3 scale
1359         unsigned int totcu = is_matrix ? 10 : (is_rotation ? 4 : 3);
1360
1361         for (i = 0; i < totcu; i++) {
1362
1363                 int axis = i;
1364
1365                 if (is_matrix) {
1366                         if (i < 4) {
1367                                 tm_str = "rotation_quaternion";
1368                                 axis = i;
1369                         }
1370                         else if (i < 7) {
1371                                 tm_str = "location";
1372                                 axis = i - 4;
1373                         }
1374                         else {
1375                                 tm_str = "scale";
1376                                 axis = i - 7;
1377                         }
1378                 }
1379
1380                 if (is_joint)
1381                         BLI_snprintf(rna_path, sizeof(rna_path), "%s.%s", joint_path, tm_str);
1382                 else
1383                         BLI_strncpy(rna_path, tm_str, sizeof(rna_path));
1384                 newcu[i] = create_fcurve(axis, rna_path);
1385
1386 #ifdef ARMATURE_TEST
1387                 if (is_joint)
1388                         job_curves[i] = create_fcurve(axis, tm_str);
1389 #endif
1390         }
1391
1392         std::vector<float>::iterator it;
1393
1394         // sample values at each frame
1395         for (it = frames.begin(); it != frames.end(); it++) {
1396                 float fra = *it;
1397
1398                 float mat[4][4];
1399                 float matfra[4][4];
1400
1401                 unit_m4(matfra);
1402
1403                 // calc object-space mat
1404                 evaluate_transform_at_frame(matfra, node, fra);
1405
1406                 // for joints, we need a special matrix
1407                 if (is_joint) {
1408                         // special matrix: iR * M * iR_dae * R
1409                         // where R, iR are bone rest and inverse rest mats in world space (Blender bones),
1410                         // iR_dae is joint inverse rest matrix (DAE) and M is an evaluated joint world-space matrix (DAE)
1411                         float temp[4][4], par[4][4];
1412
1413                         // calc M
1414                         calc_joint_parent_mat_rest(par, NULL, root, node);
1415                         mult_m4_m4m4(temp, par, matfra);
1416
1417                         // evaluate_joint_world_transform_at_frame(temp, NULL,, node, fra);
1418
1419                         // calc special matrix
1420                         mul_serie_m4(mat, irest, temp, irest_dae, rest, NULL, NULL, NULL, NULL);
1421                 }
1422                 else {
1423                         copy_m4_m4(mat, matfra);
1424                 }
1425
1426                 float val[4], rot[4], loc[3], scale[3];
1427
1428                 switch (tm_type) {
1429                         case COLLADAFW::Transformation::ROTATE:
1430                                 mat4_to_quat(val, mat);
1431                                 break;
1432                         case COLLADAFW::Transformation::SCALE:
1433                                 mat4_to_size(val, mat);
1434                                 break;
1435                         case COLLADAFW::Transformation::TRANSLATE:
1436                                 copy_v3_v3(val, mat[3]);
1437                                 break;
1438                         case COLLADAFW::Transformation::MATRIX:
1439                                 mat4_to_quat(rot, mat);
1440                                 copy_v3_v3(loc, mat[3]);
1441                                 mat4_to_size(scale, mat);
1442                                 break;
1443                         default:
1444                                 break;
1445                 }
1446
1447                 // add keys
1448                 for (i = 0; i < totcu; i++) {
1449                         if (is_matrix) {
1450                                 if (i < 4)
1451                                         add_bezt(newcu[i], fra, rot[i]);
1452                                 else if (i < 7)
1453                                         add_bezt(newcu[i], fra, loc[i - 4]);
1454                                 else
1455                                         add_bezt(newcu[i], fra, scale[i - 7]);
1456                         }
1457                         else {
1458                                 add_bezt(newcu[i], fra, val[i]);
1459                         }
1460                 }
1461
1462 #ifdef ARMATURE_TEST
1463                 if (is_joint) {
1464                         switch (tm_type) {
1465                                 case COLLADAFW::Transformation::ROTATE:
1466                                         mat4_to_quat(val, matfra);
1467                                         break;
1468                                 case COLLADAFW::Transformation::SCALE:
1469                                         mat4_to_size(val, matfra);
1470                                         break;
1471                                 case COLLADAFW::Transformation::TRANSLATE:
1472                                         copy_v3_v3(val, matfra[3]);
1473                                         break;
1474                                 case MATRIX:
1475                                         mat4_to_quat(rot, matfra);
1476                                         copy_v3_v3(loc, matfra[3]);
1477                                         mat4_to_size(scale, matfra);
1478                                         break;
1479                                 default:
1480                                         break;
1481                         }
1482
1483                         for (i = 0; i < totcu; i++) {
1484                                 if (is_matrix) {
1485                                         if (i < 4)
1486                                                 add_bezt(job_curves[i], fra, rot[i]);
1487                                         else if (i < 7)
1488                                                 add_bezt(job_curves[i], fra, loc[i - 4]);
1489                                         else
1490                                                 add_bezt(job_curves[i], fra, scale[i - 7]);
1491                                 }
1492                                 else {
1493                                         add_bezt(job_curves[i], fra, val[i]);
1494                                 }
1495                         }
1496                 }
1497 #endif
1498         }
1499
1500         verify_adt_action((ID *)&ob->id, 1);
1501
1502         ListBase *curves = &ob->adt->action->curves;
1503
1504         // add curves
1505         for (i = 0; i < totcu; i++) {
1506                 if (is_joint)
1507                         add_bone_fcurve(ob, node, newcu[i]);
1508                 else
1509                         BLI_addtail(curves, newcu[i]);
1510
1511 #ifdef ARMATURE_TEST
1512                 if (is_joint)
1513                         BLI_addtail(&job->adt->action->curves, job_curves[i]);
1514 #endif
1515         }
1516
1517         if (is_rotation || is_matrix) {
1518                 if (is_joint) {
1519                         bPoseChannel *chan = BKE_pose_channel_find_name(ob->pose, bone_name);
1520                         chan->rotmode = ROT_MODE_QUAT;
1521                 }
1522                 else {
1523                         ob->rotmode = ROT_MODE_QUAT;
1524                 }
1525         }
1526
1527         return job;
1528 }
1529
1530 // internal, better make it private
1531 // warning: evaluates only rotation and only assigns matrix transforms now
1532 // prerequisites: animlist_map, curve_map
1533 void AnimationImporter::evaluate_transform_at_frame(float mat[4][4], COLLADAFW::Node *node, float fra)
1534 {
1535         const COLLADAFW::TransformationPointerArray& tms = node->getTransformations();
1536
1537         unit_m4(mat);
1538
1539         for (unsigned int i = 0; i < tms.getCount(); i++) {
1540                 COLLADAFW::Transformation *tm = tms[i];
1541                 COLLADAFW::Transformation::TransformationType type = tm->getTransformationType();
1542                 float m[4][4];
1543
1544                 unit_m4(m);
1545
1546                 std::string nodename = node->getName().size() ? node->getName() : node->getOriginalId();
1547                 if (!evaluate_animation(tm, m, fra, nodename.c_str())) {
1548                         switch (type) {
1549                                 case COLLADAFW::Transformation::ROTATE:
1550                                         dae_rotate_to_mat4(tm, m);
1551                                         break;
1552                                 case COLLADAFW::Transformation::TRANSLATE:
1553                                         dae_translate_to_mat4(tm, m);
1554                                         break;
1555                                 case COLLADAFW::Transformation::SCALE:
1556                                         dae_scale_to_mat4(tm, m);
1557                                         break;
1558                                 case COLLADAFW::Transformation::MATRIX:
1559                                         dae_matrix_to_mat4(tm, m);
1560                                         break;
1561                                 default:
1562                                         fprintf(stderr, "unsupported transformation type %d\n", type);
1563                         }
1564                         // dae_matrix_to_mat4(tm, m);
1565                         
1566                 }
1567
1568                 float temp[4][4];
1569                 copy_m4_m4(temp, mat);
1570
1571                 mult_m4_m4m4(mat, temp, m);
1572         }
1573 }
1574
1575 // return true to indicate that mat contains a sane value
1576 bool AnimationImporter::evaluate_animation(COLLADAFW::Transformation *tm, float mat[4][4], float fra, const char *node_id)
1577 {
1578         const COLLADAFW::UniqueId& listid = tm->getAnimationList();
1579         COLLADAFW::Transformation::TransformationType type = tm->getTransformationType();
1580
1581         if (type != COLLADAFW::Transformation::ROTATE &&
1582             type != COLLADAFW::Transformation::SCALE &&
1583             type != COLLADAFW::Transformation::TRANSLATE &&
1584             type != COLLADAFW::Transformation::MATRIX) {
1585                 fprintf(stderr, "animation of transformation %d is not supported yet\n", type);
1586                 return false;
1587         }
1588
1589         if (animlist_map.find(listid) == animlist_map.end())
1590                 return false;
1591
1592         const COLLADAFW::AnimationList *animlist = animlist_map[listid];
1593         const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings();
1594
1595         if (bindings.getCount()) {
1596                 float vec[3];
1597
1598                 bool is_scale = (type == COLLADAFW::Transformation::SCALE);
1599                 bool is_translate = (type == COLLADAFW::Transformation::TRANSLATE);
1600
1601                 if (is_scale)
1602                         dae_scale_to_v3(tm, vec);
1603                 else if (is_translate)
1604                         dae_translate_to_v3(tm, vec);
1605
1606                 for (unsigned int j = 0; j < bindings.getCount(); j++) {
1607                         const COLLADAFW::AnimationList::AnimationBinding& binding = bindings[j];
1608                         std::vector<FCurve *>& curves = curve_map[binding.animation];
1609                         COLLADAFW::AnimationList::AnimationClass animclass = binding.animationClass;
1610                         char path[100];
1611
1612                         switch (type) {
1613                                 case COLLADAFW::Transformation::ROTATE:
1614                                         BLI_snprintf(path, sizeof(path), "%s.rotate (binding %u)", node_id, j);
1615                                         break;
1616                                 case COLLADAFW::Transformation::SCALE:
1617                                         BLI_snprintf(path, sizeof(path), "%s.scale (binding %u)", node_id, j);
1618                                         break;
1619                                 case COLLADAFW::Transformation::TRANSLATE:
1620                                         BLI_snprintf(path, sizeof(path), "%s.translate (binding %u)", node_id, j);
1621                                         break;
1622                                 case COLLADAFW::Transformation::MATRIX:
1623                                         BLI_snprintf(path, sizeof(path), "%s.matrix (binding %u)", node_id, j);
1624                                         break;
1625                                 default:
1626                                         break;
1627                         }
1628
1629                         if (animclass == COLLADAFW::AnimationList::UNKNOWN_CLASS) {
1630                                 fprintf(stderr, "%s: UNKNOWN animation class\n", path);
1631                                 //continue;
1632                         }
1633
1634                         if (type == COLLADAFW::Transformation::ROTATE) {
1635                                 if (curves.size() != 1) {
1636                                         fprintf(stderr, "expected 1 curve, got %d\n", (int)curves.size());
1637                                         return false;
1638                                 }
1639
1640                                 // TODO support other animclasses
1641                                 if (animclass != COLLADAFW::AnimationList::ANGLE) {
1642                                         fprintf(stderr, "%s: animation class %d is not supported yet\n", path, animclass);
1643                                         return false;
1644                                 }
1645
1646                                 COLLADABU::Math::Vector3& axis = ((COLLADAFW::Rotate *)tm)->getRotationAxis();
1647
1648                                 float ax[3] = {(float)axis[0], (float)axis[1], (float)axis[2]};
1649                                 float angle = evaluate_fcurve(curves[0], fra);
1650                                 axis_angle_to_mat4(mat, ax, angle);
1651
1652                                 return true;
1653                         }
1654                         else if (is_scale || is_translate) {
1655                                 bool is_xyz = animclass == COLLADAFW::AnimationList::POSITION_XYZ;
1656
1657                                 if ((!is_xyz && curves.size() != 1) || (is_xyz && curves.size() != 3)) {
1658                                         if (is_xyz)
1659                                                 fprintf(stderr, "%s: expected 3 curves, got %d\n", path, (int)curves.size());
1660                                         else
1661                                                 fprintf(stderr, "%s: expected 1 curve, got %d\n", path, (int)curves.size());
1662                                         return false;
1663                                 }
1664                                 
1665                                 switch (animclass) {
1666                                         case COLLADAFW::AnimationList::POSITION_X:
1667                                                 vec[0] = evaluate_fcurve(curves[0], fra);
1668                                                 break;
1669                                         case COLLADAFW::AnimationList::POSITION_Y:
1670                                                 vec[1] = evaluate_fcurve(curves[0], fra);
1671                                                 break;
1672                                         case COLLADAFW::AnimationList::POSITION_Z:
1673                                                 vec[2] = evaluate_fcurve(curves[0], fra);
1674                                                 break;
1675                                         case COLLADAFW::AnimationList::POSITION_XYZ:
1676                                                 vec[0] = evaluate_fcurve(curves[0], fra);
1677                                                 vec[1] = evaluate_fcurve(curves[1], fra);
1678                                                 vec[2] = evaluate_fcurve(curves[2], fra);
1679                                                 break;
1680                                         default:
1681                                                 fprintf(stderr, "%s: animation class %d is not supported yet\n", path, animclass);
1682                                                 break;
1683                                 }
1684                         }
1685                         else if (type == COLLADAFW::Transformation::MATRIX) {
1686                                 // for now, of matrix animation, support only the case when all values are packed into one animation
1687                                 if (curves.size() != 16) {
1688                                         fprintf(stderr, "%s: expected 16 curves, got %d\n", path, (int)curves.size());
1689                                         return false;
1690                                 }
1691
1692                                 COLLADABU::Math::Matrix4 matrix;
1693                                 int i = 0, j = 0;
1694
1695                                 for (std::vector<FCurve *>::iterator it = curves.begin(); it != curves.end(); it++) {
1696                                         matrix.setElement(i, j, evaluate_fcurve(*it, fra));
1697                                         j++;
1698                                         if (j == 4) {
1699                                                 i++;
1700                                                 j = 0;
1701                                         }
1702                                         unused_curves.erase(std::remove(unused_curves.begin(), unused_curves.end(), *it), unused_curves.end());
1703                                 }
1704
1705                                 COLLADAFW::Matrix tm(matrix);
1706                                 dae_matrix_to_mat4(&tm, mat);
1707
1708                                 std::vector<FCurve *>::iterator it;
1709
1710                                 return true;
1711                         }
1712                 }
1713
1714                 if (is_scale)
1715                         size_to_mat4(mat, vec);
1716                 else
1717                         copy_v3_v3(mat[3], vec);
1718
1719                 return is_scale || is_translate;
1720         }
1721
1722         return false;
1723 }
1724
1725 // gives a world-space mat of joint at rest position
1726 void AnimationImporter::get_joint_rest_mat(float mat[4][4], COLLADAFW::Node *root, COLLADAFW::Node *node)
1727 {
1728         // if bind mat is not available,
1729         // use "current" node transform, i.e. all those tms listed inside <node>
1730         if (!armature_importer->get_joint_bind_mat(mat, node)) {
1731                 float par[4][4], m[4][4];
1732
1733                 calc_joint_parent_mat_rest(par, NULL, root, node);
1734                 get_node_mat(m, node, NULL, NULL);
1735                 mult_m4_m4m4(mat, par, m);
1736         }
1737 }
1738
1739 // gives a world-space mat, end's mat not included
1740 bool AnimationImporter::calc_joint_parent_mat_rest(float mat[4][4], float par[4][4], COLLADAFW::Node *node, COLLADAFW::Node *end)
1741 {
1742         float m[4][4];
1743
1744         if (node == end) {
1745                 par ? copy_m4_m4(mat, par) : unit_m4(mat);
1746                 return true;
1747         }
1748
1749         // use bind matrix if available or calc "current" world mat
1750         if (!armature_importer->get_joint_bind_mat(m, node)) {
1751                 if (par) {
1752                         float temp[4][4];
1753                         get_node_mat(temp, node, NULL, NULL);
1754                         mult_m4_m4m4(m, par, temp);
1755                 }
1756                 else {
1757                         get_node_mat(m, node, NULL, NULL);
1758                 }
1759         }
1760
1761         COLLADAFW::NodePointerArray& children = node->getChildNodes();
1762         for (unsigned int i = 0; i < children.getCount(); i++) {
1763                 if (calc_joint_parent_mat_rest(mat, m, children[i], end))
1764                         return true;
1765         }
1766
1767         return false;
1768 }
1769
1770 #ifdef ARMATURE_TEST
1771 Object *AnimationImporter::get_joint_object(COLLADAFW::Node *root, COLLADAFW::Node *node, Object *par_job)
1772 {
1773         if (joint_objects.find(node->getUniqueId()) == joint_objects.end()) {
1774                 Object *job = bc_add_object(scene, OB_EMPTY, (char *)get_joint_name(node));
1775
1776                 job->lay = BKE_scene_base_find(scene, job)->lay = 2;
1777
1778                 mul_v3_fl(job->size, 0.5f);
1779                 job->recalc |= OB_RECALC_OB;
1780
1781                 verify_adt_action((ID *)&job->id, 1);
1782
1783                 job->rotmode = ROT_MODE_QUAT;
1784
1785                 float mat[4][4];
1786                 get_joint_rest_mat(mat, root, node);
1787
1788                 if (par_job) {
1789                         float temp[4][4], ipar[4][4];
1790                         invert_m4_m4(ipar, par_job->obmat);
1791                         copy_m4_m4(temp, mat);
1792                         mult_m4_m4m4(mat, ipar, temp);
1793                 }
1794
1795                 TransformBase::decompose(mat, job->loc, NULL, job->quat, job->size);
1796
1797                 if (par_job) {
1798                         job->parent = par_job;
1799
1800                         par_job->recalc |= OB_RECALC_OB;
1801                         job->parsubstr[0] = 0;
1802                 }
1803
1804                 BKE_object_where_is_calc(scene, job);
1805
1806                 // after parenting and layer change
1807                 DAG_scene_sort(CTX_data_main(C), scene);
1808
1809                 joint_objects[node->getUniqueId()] = job;
1810         }
1811
1812         return joint_objects[node->getUniqueId()];
1813 }
1814 #endif
1815
1816 #if 0
1817 // recursively evaluates joint tree until end is found, mat then is world-space matrix of end
1818 // mat must be identity on enter, node must be root
1819 bool AnimationImporter::evaluate_joint_world_transform_at_frame(float mat[4][4], float par[4][4], COLLADAFW::Node *node, COLLADAFW::Node *end, float fra)
1820 {
1821         float m[4][4];
1822         if (par) {
1823                 float temp[4][4];
1824                 evaluate_transform_at_frame(temp, node, node == end ? fra : 0.0f);
1825                 mult_m4_m4m4(m, par, temp);
1826         }
1827         else {
1828                 evaluate_transform_at_frame(m, node, node == end ? fra : 0.0f);
1829         }
1830
1831         if (node == end) {
1832                 copy_m4_m4(mat, m);
1833                 return true;
1834         }
1835         else {
1836                 COLLADAFW::NodePointerArray& children = node->getChildNodes();
1837                 for (int i = 0; i < children.getCount(); i++) {
1838                         if (evaluate_joint_world_transform_at_frame(mat, m, children[i], end, fra))
1839                                 return true;
1840                 }
1841         }
1842
1843         return false;
1844 }
1845 #endif
1846
1847 void AnimationImporter::add_bone_fcurve(Object *ob, COLLADAFW::Node *node, FCurve *fcu)
1848 {
1849         const char *bone_name = bc_get_joint_name(node);
1850         bAction *act = ob->adt->action;
1851                         
1852         /* try to find group */
1853         bActionGroup *grp = BKE_action_group_find_name(act, bone_name);
1854
1855         /* no matching groups, so add one */
1856         if (grp == NULL) {
1857                 /* Add a new group, and make it active */
1858                 grp = (bActionGroup *)MEM_callocN(sizeof(bActionGroup), "bActionGroup");
1859                                         
1860                 grp->flag = AGRP_SELECTED;
1861                 BLI_strncpy(grp->name, bone_name, sizeof(grp->name));
1862                                         
1863                 BLI_addtail(&act->groups, grp);
1864                 BLI_uniquename(&act->groups, grp, "Group", '.', offsetof(bActionGroup, name), 64);
1865         }
1866                                 
1867         /* add F-Curve to group */
1868         action_groups_add_channel(act, grp, fcu);
1869 }
1870
1871 void AnimationImporter::add_bezt(FCurve *fcu, float fra, float value)
1872 {
1873         //float fps = (float)FPS;
1874         BezTriple bez;
1875         memset(&bez, 0, sizeof(BezTriple));
1876         bez.vec[1][0] = fra;
1877         bez.vec[1][1] = value;
1878         bez.ipo = BEZT_IPO_LIN; /* use default interpolation mode here... */
1879         bez.f1 = bez.f2 = bez.f3 = SELECT;
1880         bez.h1 = bez.h2 = HD_AUTO;
1881         insert_bezt_fcurve(fcu, &bez, 0);
1882         calchandles_fcurve(fcu);
1883 }
1884