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