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