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