svn merge ^/trunk/blender -r46100:46200
[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 = action_groups_find_named(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                 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 = get_pose_channel(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::find_frames( std::vector<float>* frames, std::vector<FCurve*>* curves)
442 {
443         std::vector<FCurve*>::iterator iter;
444         for (iter = curves->begin(); iter != curves->end(); iter++) {
445                 FCurve *fcu = *iter;
446
447                 for (unsigned int k = 0; k < fcu->totvert; k++) {
448                         //get frame value from bezTriple
449                         float fra = fcu->bezt[k].vec[1][0];
450                         //if frame already not added add frame to frames
451                         if (std::find(frames->begin(), frames->end(), fra) == frames->end())
452                                 frames->push_back(fra);
453
454                 }
455         }
456 }
457
458 //creates the rna_paths and array indices of fcurves from animations using transformation and bound animation class of each animation.
459 void AnimationImporter:: Assign_transform_animations(COLLADAFW::Transformation * transform,
460                                                                                                          const COLLADAFW::AnimationList::AnimationBinding * binding,
461                                                                                                          std::vector<FCurve*>* curves, bool is_joint, char * joint_path)
462 {
463         COLLADAFW::Transformation::TransformationType tm_type = transform->getTransformationType();
464         bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX;
465         bool is_rotation = tm_type  == COLLADAFW::Transformation::ROTATE;
466
467         //to check if the no of curves are valid
468         bool xyz = ((tm_type == COLLADAFW::Transformation::TRANSLATE ||tm_type  == COLLADAFW::Transformation::SCALE) && binding->animationClass == COLLADAFW::AnimationList::POSITION_XYZ);
469
470
471         if (!((!xyz && curves->size() == 1) || (xyz && curves->size() == 3) || is_matrix)) {
472                 fprintf(stderr, "expected %d curves, got %d\n", xyz ? 3 : 1, (int)curves->size());
473                 return;
474         }
475
476         char rna_path[100];
477
478         switch (tm_type) {
479                 case COLLADAFW::Transformation::TRANSLATE:
480                 case COLLADAFW::Transformation::SCALE:
481                         {
482                                 bool loc = tm_type == COLLADAFW::Transformation::TRANSLATE;
483                                 if (is_joint)
484                                         BLI_snprintf(rna_path, sizeof(rna_path), "%s.%s", joint_path, loc ? "location" : "scale");
485                                 else
486                                         BLI_strncpy(rna_path, loc ? "location" : "scale", sizeof(rna_path));
487
488                                 switch (binding->animationClass) {
489                 case COLLADAFW::AnimationList::POSITION_X:
490                         modify_fcurve(curves, rna_path, 0 );
491                         break;
492                 case COLLADAFW::AnimationList::POSITION_Y:
493                         modify_fcurve(curves, rna_path, 1 );
494                         break;
495                 case COLLADAFW::AnimationList::POSITION_Z:
496                         modify_fcurve(curves, rna_path, 2 );
497                         break;
498                 case COLLADAFW::AnimationList::POSITION_XYZ:
499                         modify_fcurve(curves, rna_path, -1 );
500                         break;
501                 default:
502                         fprintf(stderr, "AnimationClass %d is not supported for %s.\n",
503                                 binding->animationClass, loc ? "TRANSLATE" : "SCALE");
504                                 }
505                                 break;
506                         }
507
508
509                 case COLLADAFW::Transformation::ROTATE:
510                         {
511                                 if (is_joint)
512                                         BLI_snprintf(rna_path, sizeof(rna_path), "%s.rotation_euler", joint_path);
513                                 else
514                                         BLI_strncpy(rna_path, "rotation_euler", sizeof(rna_path));
515                                 std::vector<FCurve*>::iterator iter;
516                                 for (iter = curves->begin(); iter != curves->end(); iter++) {
517                                         FCurve* fcu = *iter;
518
519                                         //if transform is rotation the fcurves values must be turned in to radian.
520                                         if (is_rotation)
521                                                 fcurve_deg_to_rad(fcu);          
522                                 }                                       
523                                 COLLADAFW::Rotate* rot = (COLLADAFW::Rotate*)transform;
524                                 COLLADABU::Math::Vector3& axis = rot->getRotationAxis();
525
526                                 switch (binding->animationClass) {
527                 case COLLADAFW::AnimationList::ANGLE:
528                         if (COLLADABU::Math::Vector3::UNIT_X == axis) {
529                                 modify_fcurve(curves, rna_path, 0 );
530                         }
531                         else if (COLLADABU::Math::Vector3::UNIT_Y == axis) {
532                                 modify_fcurve(curves, rna_path, 1 );
533                         }
534                         else if (COLLADABU::Math::Vector3::UNIT_Z == axis) {
535                                 modify_fcurve(curves, rna_path, 2 );
536                         }
537                         break;
538                 case COLLADAFW::AnimationList::AXISANGLE:
539                         // TODO convert axis-angle to quat? or XYZ?
540                 default:
541                         fprintf(stderr, "AnimationClass %d is not supported for ROTATE transformation.\n",
542                                 binding->animationClass);
543                                 }
544                                 break;
545                         }
546
547                 case COLLADAFW::Transformation::MATRIX:
548                         /*{
549                         COLLADAFW::Matrix* mat = (COLLADAFW::Matrix*)transform;
550                         COLLADABU::Math::Matrix4 mat4 = mat->getMatrix();
551                         switch (binding->animationClass) {
552                         case COLLADAFW::AnimationList::TRANSFORM:
553
554                         }
555                         }*/
556                         break;
557                 case COLLADAFW::Transformation::SKEW:
558                 case COLLADAFW::Transformation::LOOKAT:
559                         fprintf(stderr, "Animation of SKEW and LOOKAT transformations is not supported yet.\n");
560                         break;
561         }
562
563 }
564
565 //creates the rna_paths and array indices of fcurves from animations using color and bound animation class of each animation.
566 void AnimationImporter:: Assign_color_animations(const COLLADAFW::UniqueId& listid, ListBase *AnimCurves, const char * anim_type)
567 {
568         char rna_path[100];
569         BLI_strncpy(rna_path, anim_type, sizeof(rna_path));
570
571         const COLLADAFW::AnimationList *animlist = animlist_map[listid];
572         const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings();
573         //all the curves belonging to the current binding
574         std::vector<FCurve*> animcurves;
575         for (unsigned int j = 0; j < bindings.getCount(); j++) {
576                 animcurves = curve_map[bindings[j].animation];
577
578                 switch (bindings[j].animationClass) {
579                 case COLLADAFW::AnimationList::COLOR_R:
580                         modify_fcurve(&animcurves, rna_path, 0 );
581                         break;
582                 case COLLADAFW::AnimationList::COLOR_G:
583                         modify_fcurve(&animcurves, rna_path, 1 );
584                         break;
585                 case COLLADAFW::AnimationList::COLOR_B:
586                         modify_fcurve(&animcurves, rna_path, 2 );
587                         break;
588                 case COLLADAFW::AnimationList::COLOR_RGB:
589                 case COLLADAFW::AnimationList::COLOR_RGBA: // to do-> set intensity
590                         modify_fcurve(&animcurves, rna_path, -1 );
591                         break;
592
593                 default:
594                         fprintf(stderr, "AnimationClass %d is not supported for %s.\n",
595                                 bindings[j].animationClass, "COLOR" );
596                 }
597
598                 std::vector<FCurve*>::iterator iter;
599                 //Add the curves of the current animation to the object
600                 for (iter = animcurves.begin(); iter != animcurves.end(); iter++) {
601                         FCurve * fcu = *iter;
602                         BLI_addtail(AnimCurves, fcu);   
603                 }
604         }
605
606
607 }
608
609 void AnimationImporter:: Assign_float_animations(const COLLADAFW::UniqueId& listid, ListBase *AnimCurves, const char * anim_type)
610 {
611         char rna_path[100];
612         if (animlist_map.find(listid) == animlist_map.end()) {
613                 return;
614         }
615         else {
616                 //anim_type has animations
617                 const COLLADAFW::AnimationList *animlist = animlist_map[listid];
618                 const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings();
619                 //all the curves belonging to the current binding
620                 std::vector<FCurve*> animcurves;
621                 for (unsigned int j = 0; j < bindings.getCount(); j++) {
622                         animcurves = curve_map[bindings[j].animation];
623
624                         BLI_strncpy(rna_path, anim_type, sizeof(rna_path));
625                         modify_fcurve(&animcurves, rna_path, 0 );
626                         std::vector<FCurve*>::iterator iter;
627                         //Add the curves of the current animation to the object
628                         for (iter = animcurves.begin(); iter != animcurves.end(); iter++) {
629                                 FCurve * fcu = *iter;
630                                 BLI_addtail(AnimCurves, fcu);
631                         }
632                 }
633         }
634         
635 }
636
637 void AnimationImporter::apply_matrix_curves( Object * ob, std::vector<FCurve*>& animcurves, COLLADAFW::Node* root, COLLADAFW::Node* node,
638                                                                                                         COLLADAFW::Transformation * tm )
639 {
640         bool is_joint = node->getType() == COLLADAFW::Node::JOINT;
641         const char *bone_name = is_joint ? bc_get_joint_name(node) : NULL;
642         char joint_path[200];
643         if ( is_joint ) 
644                 armature_importer->get_rna_path_for_joint(node, joint_path, sizeof(joint_path));
645
646         std::vector<float> frames;
647         find_frames(&frames, &animcurves);
648
649         float irest_dae[4][4];
650         float rest[4][4], irest[4][4];
651
652         if (is_joint) {
653                 get_joint_rest_mat(irest_dae, root, node);
654                 invert_m4(irest_dae);
655
656                 Bone *bone = get_named_bone((bArmature*)ob->data, bone_name);
657                 if (!bone) {
658                         fprintf(stderr, "cannot find bone \"%s\"\n", bone_name);
659                         return;
660                 }
661
662                 unit_m4(rest);
663                 copy_m4_m4(rest, bone->arm_mat);
664                 invert_m4_m4(irest, rest);
665         }
666         // new curves to assign matrix transform animation
667         FCurve *newcu[10]; // if tm_type is matrix, then create 10 curves: 4 rot, 3 loc, 3 scale
668         unsigned int totcu = 10;
669         const char *tm_str = NULL;
670         char rna_path[200];
671         for (int i = 0; i < totcu; i++) {
672
673                 int axis = i;
674
675                 if (i < 4) {
676                         tm_str = "rotation_quaternion";
677                         axis = i;
678                 }
679                 else if (i < 7) {
680                         tm_str = "location";
681                         axis = i - 4;
682                 }
683                 else {
684                         tm_str = "scale";
685                         axis = i - 7;
686                 }
687
688
689                 if (is_joint)
690                         BLI_snprintf(rna_path, sizeof(rna_path), "%s.%s", joint_path, tm_str);
691                 else
692                         BLI_strncpy(rna_path, tm_str, sizeof(rna_path));
693                 newcu[i] = create_fcurve(axis, rna_path);
694                 newcu[i]->totvert = frames.size();
695         }
696
697         if (frames.size() == 0)
698                 return;
699
700         std::sort(frames.begin(), frames.end());
701
702         std::vector<float>::iterator it;
703
704         // sample values at each frame
705         for (it = frames.begin(); it != frames.end(); it++) {
706                 float fra = *it;
707
708                 float mat[4][4];
709                 float matfra[4][4];
710
711                 unit_m4(matfra);
712
713                 // calc object-space mat
714                 evaluate_transform_at_frame(matfra, node, fra);
715
716
717                 // for joints, we need a special matrix
718                 if (is_joint) {
719                         // special matrix: iR * M * iR_dae * R
720                         // where R, iR are bone rest and inverse rest mats in world space (Blender bones),
721                         // iR_dae is joint inverse rest matrix (DAE) and M is an evaluated joint world-space matrix (DAE)
722                         float temp[4][4], par[4][4];
723
724                         // calc M
725                         calc_joint_parent_mat_rest(par, NULL, root, node);
726                         mult_m4_m4m4(temp, par, matfra);
727
728                         // evaluate_joint_world_transform_at_frame(temp, NULL, node, fra);
729
730                         // calc special matrix
731                         mul_serie_m4(mat, irest, temp, irest_dae, rest, NULL, NULL, NULL, NULL);
732                 }
733                 else {
734                         copy_m4_m4(mat, matfra);
735                 }
736
737                 float  rot[4], loc[3], scale[3];
738
739                 mat4_to_quat(rot, mat);
740                 /*for ( int i = 0 ; i < 4  ;  i ++ )
741                 {
742                 rot[i] = RAD2DEGF(rot[i]);
743                 }*/
744                 copy_v3_v3(loc, mat[3]);
745                 mat4_to_size(scale, mat);
746
747                 // add keys
748                 for (int i = 0; i < totcu; i++) {
749                         if (i < 4)
750                                 add_bezt(newcu[i], fra, rot[i]);
751                         else if (i < 7)
752                                 add_bezt(newcu[i], fra, loc[i - 4]);
753                         else
754                                 add_bezt(newcu[i], fra, scale[i - 7]);
755                 }
756         }
757         verify_adt_action((ID*)&ob->id, 1);
758
759         ListBase *curves = &ob->adt->action->curves;
760
761         // add curves
762         for (int i= 0; i < totcu; i++) {
763                 if (is_joint)
764                         add_bone_fcurve(ob, node, newcu[i]);
765                 else
766                         BLI_addtail(curves, newcu[i]);
767         }
768
769         if (is_joint) {
770                 bPoseChannel *chan = get_pose_channel(ob->pose, bone_name);
771                 chan->rotmode = ROT_MODE_QUAT;
772         }
773         else {
774                 ob->rotmode = ROT_MODE_QUAT;
775         }
776
777         return;
778
779 }
780
781 void AnimationImporter::translate_Animations ( COLLADAFW::Node * node,
782                                                                                                 std::map<COLLADAFW::UniqueId, COLLADAFW::Node*>& root_map,
783                                                                                                 std::multimap<COLLADAFW::UniqueId, Object*>& object_map,
784                                                                                                 std::map<COLLADAFW::UniqueId, const COLLADAFW::Object*> FW_object_map)
785 {
786         AnimationImporter::AnimMix* animType = get_animation_type(node, FW_object_map );
787
788         bool is_joint = node->getType() == COLLADAFW::Node::JOINT;
789         COLLADAFW::Node *root = root_map.find(node->getUniqueId()) == root_map.end() ? node : root_map[node->getUniqueId()];
790     Object *ob = is_joint ? armature_importer->get_armature_for_joint(root) : object_map.find(node->getUniqueId())->second;
791         if (!ob) {
792                 fprintf(stderr, "cannot find Object for Node with id=\"%s\"\n", node->getOriginalId().c_str());
793                 return;
794         }
795
796         bAction * act;
797
798         if ( (animType->transform) != 0 ) {
799                 /* const char *bone_name = is_joint ? bc_get_joint_name(node) : NULL; */ /* UNUSED */
800                 char joint_path[200];
801
802                 if ( is_joint ) 
803                         armature_importer->get_rna_path_for_joint(node, joint_path, sizeof(joint_path));
804
805
806                 if (!ob->adt || !ob->adt->action) act = verify_adt_action((ID*)&ob->id, 1);
807                 else act = ob->adt->action;
808
809                 //Get the list of animation curves of the object
810                 ListBase *AnimCurves = &(act->curves);
811
812                 const COLLADAFW::TransformationPointerArray& nodeTransforms = node->getTransformations();
813
814                 //for each transformation in node 
815                 for (unsigned int i = 0; i < nodeTransforms.getCount(); i++) {
816                         COLLADAFW::Transformation *transform = nodeTransforms[i];
817                         COLLADAFW::Transformation::TransformationType tm_type = transform->getTransformationType();
818
819                         bool is_rotation = tm_type == COLLADAFW::Transformation::ROTATE;
820                         bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX;
821
822                         const COLLADAFW::UniqueId& listid = transform->getAnimationList();
823
824                         //check if transformation has animations
825                         if (animlist_map.find(listid) == animlist_map.end()) {
826                                 continue;
827                         }
828                         else {
829                                 //transformation has animations
830                                 const COLLADAFW::AnimationList *animlist = animlist_map[listid];
831                                 const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings();
832                                 //all the curves belonging to the current binding
833                                 std::vector<FCurve*> animcurves;
834                                 for (unsigned int j = 0; j < bindings.getCount(); j++) {
835                                         animcurves = curve_map[bindings[j].animation];
836                                         if ( is_matrix ) {
837                                                 apply_matrix_curves(ob, animcurves, root, node,  transform  );
838                                         }
839                                         else {                          
840
841                                                 if (is_joint) {
842
843                                                         add_bone_animation_sampled(ob, animcurves, root, node, transform);
844                                                 }
845                                                 else {
846                                                         //calculate rnapaths and array index of fcurves according to transformation and animation class
847                                                         Assign_transform_animations(transform, &bindings[j], &animcurves, is_joint, joint_path ); 
848
849                                                         std::vector<FCurve*>::iterator iter;
850                                                         //Add the curves of the current animation to the object
851                                                         for (iter = animcurves.begin(); iter != animcurves.end(); iter++) {
852                                                                 FCurve * fcu = *iter;
853                                                         
854                                                                 BLI_addtail(AnimCurves, fcu);
855                                                         }
856                                                 }
857                                                 
858                                         }
859                                 }
860                         }
861                         if (is_rotation && !is_joint) {
862                                 ob->rotmode = ROT_MODE_EUL;
863                         }
864                 }
865         }
866
867         if ((animType->light) != 0) {
868                 Lamp * lamp  = (Lamp*) ob->data;
869
870                 if (!lamp->adt || !lamp->adt->action) act = verify_adt_action((ID*)&lamp->id, 1);
871                 else act = lamp->adt->action;
872
873                 ListBase *AnimCurves = &(act->curves);
874                 const COLLADAFW::InstanceLightPointerArray& nodeLights = node->getInstanceLights();
875
876                 for (unsigned int i = 0; i < nodeLights.getCount(); i++) {
877                         const COLLADAFW::Light *light = (COLLADAFW::Light *) FW_object_map[nodeLights[i]->getInstanciatedObjectId()];
878
879                         if ((animType->light & LIGHT_COLOR) != 0) {
880                                 const COLLADAFW::Color *col =  &(light->getColor());
881                                 const COLLADAFW::UniqueId& listid = col->getAnimationList();
882
883                                 Assign_color_animations(listid, AnimCurves, "color"); 
884                         }
885                         if ((animType->light & LIGHT_FOA) != 0 ) {
886                                 const COLLADAFW::AnimatableFloat *foa =  &(light->getFallOffAngle());
887                                 const COLLADAFW::UniqueId& listid = foa->getAnimationList();
888
889                                 Assign_float_animations( listid, AnimCurves, "spot_size");
890                         }
891                         if ( (animType->light & LIGHT_FOE) != 0 ) {
892                                 const COLLADAFW::AnimatableFloat *foe =  &(light->getFallOffExponent());
893                                 const COLLADAFW::UniqueId& listid = foe->getAnimationList();
894
895                                 Assign_float_animations( listid, AnimCurves, "spot_blend");
896
897                         }
898                 }
899         }
900
901         if ( (animType->camera) != 0)  {
902                 Camera * camera  = (Camera*) ob->data;
903
904                 if (!camera->adt || !camera->adt->action) act = verify_adt_action((ID*)&camera->id, 1);
905                 else act = camera->adt->action;
906
907                 ListBase *AnimCurves = &(act->curves);
908                 const COLLADAFW::InstanceCameraPointerArray& nodeCameras= node->getInstanceCameras();
909
910                 for (unsigned int i = 0; i < nodeCameras.getCount(); i++) {
911                         const COLLADAFW::Camera *camera = (COLLADAFW::Camera *) FW_object_map[nodeCameras[i]->getInstanciatedObjectId()];
912
913                         if ((animType->camera & CAMERA_XFOV) != 0 ) {
914                                 const COLLADAFW::AnimatableFloat *xfov =  &(camera->getXFov());
915                                 const COLLADAFW::UniqueId& listid = xfov->getAnimationList();
916                                 Assign_float_animations( listid, AnimCurves, "lens");
917                         }
918
919                         else if ((animType->camera & CAMERA_XMAG) != 0 ) {
920                                 const COLLADAFW::AnimatableFloat *xmag =  &(camera->getXMag());
921                                 const COLLADAFW::UniqueId& listid = xmag->getAnimationList();
922                                 Assign_float_animations( listid, AnimCurves, "ortho_scale");
923                         }
924
925                         if ((animType->camera & CAMERA_ZFAR) != 0 ) {
926                                 const COLLADAFW::AnimatableFloat *zfar =  &(camera->getFarClippingPlane());
927                                 const COLLADAFW::UniqueId& listid = zfar->getAnimationList();
928                                 Assign_float_animations( listid, AnimCurves, "clip_end");
929                         }
930
931                         if ((animType->camera & CAMERA_ZNEAR) != 0 ) {
932                                 const COLLADAFW::AnimatableFloat *znear =  &(camera->getNearClippingPlane());
933                                 const COLLADAFW::UniqueId& listid = znear->getAnimationList();
934                                 Assign_float_animations( listid, AnimCurves, "clip_start");
935                         }
936
937                 }
938         }
939         if ( animType->material != 0) {
940                 Material *ma = give_current_material(ob, 1);
941                 if (!ma->adt || !ma->adt->action) act = verify_adt_action((ID*)&ma->id, 1);
942                 else act = ma->adt->action;
943
944                 ListBase *AnimCurves = &(act->curves);
945
946                 const COLLADAFW::InstanceGeometryPointerArray& nodeGeoms = node->getInstanceGeometries();
947                 for (unsigned int i = 0; i < nodeGeoms.getCount(); i++) {
948                         const COLLADAFW::MaterialBindingArray& matBinds = nodeGeoms[i]->getMaterialBindings();
949                         for (unsigned int j = 0; j < matBinds.getCount(); j++) {
950                                 const COLLADAFW::UniqueId & matuid = matBinds[j].getReferencedMaterial();
951                                 const COLLADAFW::Effect *ef = (COLLADAFW::Effect *) (FW_object_map[matuid]);
952                                 if (ef != NULL) { /* can be NULL [#28909] */
953                                         const COLLADAFW::CommonEffectPointerArray& commonEffects  =  ef->getCommonEffects();
954                                         COLLADAFW::EffectCommon *efc = commonEffects[0];
955                                         if ((animType->material & MATERIAL_SHININESS) != 0) {
956                                                 const COLLADAFW::FloatOrParam *shin = &(efc->getShininess());
957                                                 const COLLADAFW::UniqueId& listid =  shin->getAnimationList();
958                                                 Assign_float_animations( listid, AnimCurves, "specular_hardness" );
959                                         }
960
961                                         if ((animType->material & MATERIAL_IOR) != 0) {
962                                                 const COLLADAFW::FloatOrParam *ior = &(efc->getIndexOfRefraction());
963                                                 const COLLADAFW::UniqueId& listid =  ior->getAnimationList();
964                                                 Assign_float_animations( listid, AnimCurves, "raytrace_transparency.ior" );
965                                         }
966
967                                         if ((animType->material & MATERIAL_SPEC_COLOR) != 0) {
968                                                 const COLLADAFW::ColorOrTexture *cot = &(efc->getSpecular());
969                                                 const COLLADAFW::UniqueId& listid =  cot->getColor().getAnimationList();
970                                                 Assign_color_animations( listid, AnimCurves, "specular_color" );
971                                         }
972
973                                         if ((animType->material & MATERIAL_DIFF_COLOR) != 0) {
974                                                 const COLLADAFW::ColorOrTexture *cot = &(efc->getDiffuse());
975                                                 const COLLADAFW::UniqueId& listid =  cot->getColor().getAnimationList();
976                                                 Assign_color_animations( listid, AnimCurves, "diffuse_color" );
977                                         }
978                                 }
979                         }
980                 }       
981         }
982 }
983
984 void AnimationImporter::add_bone_animation_sampled(Object * ob, std::vector<FCurve*>& animcurves, COLLADAFW::Node* root, COLLADAFW::Node* node, COLLADAFW::Transformation * tm)
985 {
986         const char *bone_name = bc_get_joint_name(node);
987         char joint_path[200];
988         armature_importer->get_rna_path_for_joint(node, joint_path, sizeof(joint_path));
989
990         std::vector<float> frames;
991         find_frames(&frames, &animcurves);
992
993         // convert degrees to radians
994         if (tm->getTransformationType() == COLLADAFW::Transformation::ROTATE) {
995
996                 std::vector<FCurve*>::iterator iter;
997                 for (iter = animcurves.begin(); iter != animcurves.end(); iter++) {
998                         FCurve* fcu = *iter;
999
1000                         fcurve_deg_to_rad(fcu);          
1001                 }                                       
1002         }
1003
1004
1005         float irest_dae[4][4];
1006         float rest[4][4], irest[4][4];
1007
1008         get_joint_rest_mat(irest_dae, root, node);
1009         invert_m4(irest_dae);
1010
1011         Bone *bone = get_named_bone((bArmature*)ob->data, bone_name);
1012         if (!bone) {
1013                 fprintf(stderr, "cannot find bone \"%s\"\n", bone_name);
1014                 return;
1015         }
1016
1017         unit_m4(rest);
1018         copy_m4_m4(rest, bone->arm_mat);
1019         invert_m4_m4(irest, rest);
1020
1021         // new curves to assign matrix transform animation
1022         FCurve *newcu[10]; // if tm_type is matrix, then create 10 curves: 4 rot, 3 loc, 3 scale
1023         unsigned int totcu = 10;
1024         const char *tm_str = NULL;
1025         char rna_path[200];
1026         for (int i = 0; i < totcu; i++) {
1027
1028                 int axis = i;
1029
1030                 if (i < 4) {
1031                         tm_str = "rotation_quaternion";
1032                         axis = i;
1033                 }
1034                 else if (i < 7) {
1035                         tm_str = "location";
1036                         axis = i - 4;
1037                 }
1038                 else {
1039                         tm_str = "scale";
1040                         axis = i - 7;
1041                 }
1042
1043
1044                 BLI_snprintf(rna_path, sizeof(rna_path), "%s.%s", joint_path, tm_str);
1045
1046                 newcu[i] = create_fcurve(axis, rna_path);
1047                 newcu[i]->totvert = frames.size();
1048         }
1049
1050         if (frames.size() == 0)
1051                 return;
1052
1053         std::sort(frames.begin(), frames.end());
1054
1055         std::vector<float>::iterator it;
1056
1057         // sample values at each frame
1058         for (it = frames.begin(); it != frames.end(); it++) {
1059                 float fra = *it;
1060
1061                 float mat[4][4];
1062                 float matfra[4][4];
1063
1064                 unit_m4(matfra);
1065
1066                 // calc object-space mat
1067                 evaluate_transform_at_frame(matfra, node, fra);
1068
1069
1070                 // for joints, we need a special matrix
1071                 // special matrix: iR * M * iR_dae * R
1072                 // where R, iR are bone rest and inverse rest mats in world space (Blender bones),
1073                 // iR_dae is joint inverse rest matrix (DAE) and M is an evaluated joint world-space matrix (DAE)
1074                 float temp[4][4], par[4][4];
1075
1076
1077                 // calc M
1078                 calc_joint_parent_mat_rest(par, NULL, root, node);
1079                 mult_m4_m4m4(temp, par, matfra);
1080
1081                 // evaluate_joint_world_transform_at_frame(temp, NULL,, node, fra);
1082
1083                 // calc special matrix
1084                 mul_serie_m4(mat, irest, temp, irest_dae, rest, NULL, NULL, NULL, NULL);
1085
1086                 float  rot[4], loc[3], scale[3];
1087
1088                 mat4_to_quat(rot, mat);
1089                 copy_v3_v3(loc, mat[3]);
1090                 mat4_to_size(scale, mat);
1091
1092                 // add keys
1093                 for (int i = 0; i < totcu; i++) {
1094                         if (i < 4)
1095                                 add_bezt(newcu[i], fra, rot[i]);
1096                         else if (i < 7)
1097                                 add_bezt(newcu[i], fra, loc[i - 4]);
1098                         else
1099                                 add_bezt(newcu[i], fra, scale[i - 7]);
1100                 }
1101         }
1102         verify_adt_action((ID*)&ob->id, 1);
1103
1104         // add curves
1105         for (int i= 0; i < totcu; i++) {
1106                 add_bone_fcurve(ob, node, newcu[i]);
1107         }
1108
1109         bPoseChannel *chan = get_pose_channel(ob->pose, bone_name);
1110         chan->rotmode = ROT_MODE_QUAT;
1111
1112 }
1113
1114
1115 //Check if object is animated by checking if animlist_map holds the animlist_id of node transforms
1116 AnimationImporter::AnimMix* AnimationImporter::get_animation_type ( const COLLADAFW::Node * node,
1117                                                                                         std::map<COLLADAFW::UniqueId, const COLLADAFW::Object*> FW_object_map) 
1118 {
1119         AnimMix *types = new AnimMix();
1120
1121         const COLLADAFW::TransformationPointerArray& nodeTransforms = node->getTransformations();
1122
1123         //for each transformation in node 
1124         for (unsigned int i = 0; i < nodeTransforms.getCount(); i++) {
1125                 COLLADAFW::Transformation *transform = nodeTransforms[i];
1126                 const COLLADAFW::UniqueId& listid = transform->getAnimationList();
1127
1128                 //check if transformation has animations
1129                 if (animlist_map.find(listid) == animlist_map.end()) {
1130                         continue;
1131                 }
1132                 else {
1133                         types->transform = types->transform|NODE_TRANSFORM;
1134                         break;
1135                 }
1136         }
1137         const COLLADAFW::InstanceLightPointerArray& nodeLights = node->getInstanceLights();
1138
1139         for (unsigned int i = 0; i < nodeLights.getCount(); i++) {
1140                 const COLLADAFW::Light *light = (COLLADAFW::Light *) FW_object_map[nodeLights[i]->getInstanciatedObjectId()];
1141                 types->light = setAnimType(&(light->getColor()), (types->light), LIGHT_COLOR);
1142                 types->light = setAnimType(&(light->getFallOffAngle()), (types->light), LIGHT_FOA);
1143                 types->light = setAnimType(&(light->getFallOffExponent()), (types->light), LIGHT_FOE);
1144
1145                 if ( types->light != 0) break;
1146
1147         }
1148
1149         const COLLADAFW::InstanceCameraPointerArray& nodeCameras = node->getInstanceCameras();
1150         for (unsigned int i = 0; i < nodeCameras.getCount(); i++) {
1151                 const COLLADAFW::Camera *camera = (COLLADAFW::Camera *) FW_object_map[nodeCameras[i]->getInstanciatedObjectId()];
1152
1153                 if ( camera->getCameraType() == COLLADAFW::Camera::PERSPECTIVE ) {
1154                         types->camera = setAnimType(&(camera->getXMag()), (types->camera), CAMERA_XFOV);
1155                 }
1156                 else {
1157                         types->camera = setAnimType(&(camera->getXMag()), (types->camera), CAMERA_XMAG);
1158                 }
1159                 types->camera = setAnimType(&(camera->getFarClippingPlane()), (types->camera), CAMERA_ZFAR);
1160                 types->camera = setAnimType(&(camera->getNearClippingPlane()), (types->camera), CAMERA_ZNEAR);
1161
1162                 if ( types->camera != 0) break;
1163
1164         }
1165
1166         const COLLADAFW::InstanceGeometryPointerArray& nodeGeoms = node->getInstanceGeometries();
1167         for (unsigned int i = 0; i < nodeGeoms.getCount(); i++) {
1168                 const COLLADAFW::MaterialBindingArray& matBinds = nodeGeoms[i]->getMaterialBindings();
1169                 for (unsigned int j = 0; j < matBinds.getCount(); j++) {
1170                         const COLLADAFW::UniqueId & matuid = matBinds[j].getReferencedMaterial();
1171                         const COLLADAFW::Effect *ef = (COLLADAFW::Effect *) (FW_object_map[matuid]);
1172                         if (ef != NULL) { /* can be NULL [#28909] */
1173                                 const COLLADAFW::CommonEffectPointerArray& commonEffects = ef->getCommonEffects();
1174                                 if (!commonEffects.empty()) {
1175                                         COLLADAFW::EffectCommon *efc = commonEffects[0];
1176                                         types->material =  setAnimType(&(efc->getShininess()), (types->material), MATERIAL_SHININESS);
1177                                         types->material =  setAnimType(&(efc->getSpecular().getColor()), (types->material), MATERIAL_SPEC_COLOR);
1178                                         types->material =  setAnimType(&(efc->getDiffuse().getColor()), (types->material), MATERIAL_DIFF_COLOR);
1179                                         // types->material =  setAnimType(&(efc->get()), (types->material), MATERIAL_TRANSPARENCY);
1180                                         types->material =  setAnimType(&(efc->getIndexOfRefraction()), (types->material), MATERIAL_IOR);
1181                                 }
1182                         }
1183                 }
1184         }
1185         return types;
1186 }
1187
1188 int AnimationImporter::setAnimType ( const COLLADAFW::Animatable * prop, int types, int addition)
1189 {
1190         const COLLADAFW::UniqueId& listid =  prop->getAnimationList();
1191         if (animlist_map.find(listid) != animlist_map.end())
1192                 return types|addition;
1193         else return types;
1194 }               
1195
1196 // Is not used anymore.
1197 void AnimationImporter::find_frames_old(std::vector<float> * frames, COLLADAFW::Node * node, COLLADAFW::Transformation::TransformationType tm_type)
1198 {
1199         bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX;
1200         bool is_rotation = tm_type == COLLADAFW::Transformation::ROTATE;
1201         // for each <rotate>, <translate>, etc. there is a separate Transformation
1202         const COLLADAFW::TransformationPointerArray& nodeTransforms = node->getTransformations();
1203
1204         unsigned int i;
1205         // find frames at which to sample plus convert all rotation keys to radians
1206         for (i = 0; i < nodeTransforms.getCount(); i++) {
1207                 COLLADAFW::Transformation *transform = nodeTransforms[i];
1208                 COLLADAFW::Transformation::TransformationType nodeTmType = transform->getTransformationType();
1209
1210
1211                 if (nodeTmType == tm_type) {
1212                         //get animation bindings for the current transformation
1213                         const COLLADAFW::UniqueId& listid = transform->getAnimationList();
1214                         //if transform is animated its animlist must exist.
1215                         if (animlist_map.find(listid) != animlist_map.end()) {
1216                                 
1217                                 const COLLADAFW::AnimationList *animlist = animlist_map[listid];
1218                                 const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings();
1219
1220                                 if (bindings.getCount()) {
1221                                         //for each AnimationBinding get the fcurves which animate the transform
1222                                         for (unsigned int j = 0; j < bindings.getCount(); j++) {
1223                                                 std::vector<FCurve*>& curves = curve_map[bindings[j].animation];
1224                                                 bool xyz = ((nodeTmType == COLLADAFW::Transformation::TRANSLATE || nodeTmType == COLLADAFW::Transformation::SCALE) && bindings[j].animationClass == COLLADAFW::AnimationList::POSITION_XYZ);
1225
1226                                                 if ((!xyz && curves.size() == 1) || (xyz && curves.size() == 3) || is_matrix) {
1227                                                         std::vector<FCurve*>::iterator iter;
1228
1229                                                         for (iter = curves.begin(); iter != curves.end(); iter++) {
1230                                                                 FCurve *fcu = *iter;
1231
1232                                                                 //if transform is rotation the fcurves values must be turned in to radian.
1233                                                                 if (is_rotation)
1234                                                                         fcurve_deg_to_rad(fcu);
1235
1236                                                                 for (unsigned int k = 0; k < fcu->totvert; k++) {
1237                                                                         //get frame value from bezTriple
1238                                                                         float fra = fcu->bezt[k].vec[1][0];
1239                                                                         //if frame already not added add frame to frames
1240                                                                         if (std::find(frames->begin(), frames->end(), fra) == frames->end())
1241                                                                                 frames->push_back(fra);
1242                                                                 }
1243                                                         }
1244                                                 }
1245                                                 else {
1246                                                         fprintf(stderr, "expected %d curves, got %d\n", xyz ? 3 : 1, (int)curves.size());
1247                                                 }
1248                                         }
1249                                 }
1250                         }
1251                 }
1252         }
1253 }
1254
1255
1256
1257 // prerequisites:
1258 // animlist_map - map animlist id -> animlist
1259 // curve_map - map anim id -> curve(s)
1260 Object *AnimationImporter::translate_animation_OLD(COLLADAFW::Node *node,
1261                                                         std::map<COLLADAFW::UniqueId, Object*>& object_map,
1262                                                         std::map<COLLADAFW::UniqueId, COLLADAFW::Node*>& root_map,
1263                                                         COLLADAFW::Transformation::TransformationType tm_type,
1264                                                         Object *par_job)
1265 {
1266         
1267         bool is_rotation = tm_type == COLLADAFW::Transformation::ROTATE;
1268         bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX;
1269         bool is_joint = node->getType() == COLLADAFW::Node::JOINT;
1270         
1271         COLLADAFW::Node *root = root_map.find(node->getUniqueId()) == root_map.end() ? node : root_map[node->getUniqueId()];
1272         Object *ob = is_joint ? armature_importer->get_armature_for_joint(node) : object_map[node->getUniqueId()];
1273         const char *bone_name = is_joint ? bc_get_joint_name(node) : NULL;
1274         if (!ob) {
1275                 fprintf(stderr, "cannot find Object for Node with id=\"%s\"\n", node->getOriginalId().c_str());
1276                 return NULL;
1277         }
1278
1279         // frames at which to sample
1280         std::vector<float> frames;
1281         
1282         find_frames_old(&frames, node, tm_type);
1283         
1284         unsigned int i;
1285         
1286         float irest_dae[4][4];
1287         float rest[4][4], irest[4][4];
1288
1289         if (is_joint) {
1290                 get_joint_rest_mat(irest_dae, root, node);
1291                 invert_m4(irest_dae);
1292
1293                 Bone *bone = get_named_bone((bArmature*)ob->data, bone_name);
1294                 if (!bone) {
1295                         fprintf(stderr, "cannot find bone \"%s\"\n", bone_name);
1296                         return NULL;
1297                 }
1298
1299                 unit_m4(rest);
1300                 copy_m4_m4(rest, bone->arm_mat);
1301                 invert_m4_m4(irest, rest);
1302         }
1303
1304         Object *job = NULL;
1305
1306 #ifdef ARMATURE_TEST
1307         FCurve *job_curves[10];
1308         job = get_joint_object(root, node, par_job);
1309 #endif
1310
1311         if (frames.size() == 0)
1312                 return job;
1313
1314         std::sort(frames.begin(), frames.end());
1315
1316         const char *tm_str = NULL;
1317         switch (tm_type) {
1318         case COLLADAFW::Transformation::ROTATE:
1319                 tm_str = "rotation_quaternion";
1320                 break;
1321         case COLLADAFW::Transformation::SCALE:
1322                 tm_str = "scale";
1323                 break;
1324         case COLLADAFW::Transformation::TRANSLATE:
1325                 tm_str = "location";
1326                 break;
1327         case COLLADAFW::Transformation::MATRIX:
1328                 break;
1329         default:
1330                 return job;
1331         }
1332
1333         char rna_path[200];
1334         char joint_path[200];
1335
1336         if (is_joint)
1337                 armature_importer->get_rna_path_for_joint(node, joint_path, sizeof(joint_path));
1338
1339         // new curves
1340         FCurve *newcu[10]; // if tm_type is matrix, then create 10 curves: 4 rot, 3 loc, 3 scale
1341         unsigned int totcu = is_matrix ? 10 : (is_rotation ? 4 : 3);
1342
1343         for (i = 0; i < totcu; i++) {
1344
1345                 int axis = i;
1346
1347                 if (is_matrix) {
1348                         if (i < 4) {
1349                                 tm_str = "rotation_quaternion";
1350                                 axis = i;
1351                         }
1352                         else if (i < 7) {
1353                                 tm_str = "location";
1354                                 axis = i - 4;
1355                         }
1356                         else {
1357                                 tm_str = "scale";
1358                                 axis = i - 7;
1359                         }
1360                 }
1361
1362                 if (is_joint)
1363                         BLI_snprintf(rna_path, sizeof(rna_path), "%s.%s", joint_path, tm_str);
1364                 else
1365                         BLI_strncpy(rna_path, tm_str, sizeof(rna_path));
1366                 newcu[i] = create_fcurve(axis, rna_path);
1367
1368 #ifdef ARMATURE_TEST
1369                 if (is_joint)
1370                         job_curves[i] = create_fcurve(axis, tm_str);
1371 #endif
1372         }
1373
1374         std::vector<float>::iterator it;
1375
1376         // sample values at each frame
1377         for (it = frames.begin(); it != frames.end(); it++) {
1378                 float fra = *it;
1379
1380                 float mat[4][4];
1381                 float matfra[4][4];
1382
1383                 unit_m4(matfra);
1384
1385                 // calc object-space mat
1386                 evaluate_transform_at_frame(matfra, node, fra);
1387
1388                 // for joints, we need a special matrix
1389                 if (is_joint) {
1390                         // special matrix: iR * M * iR_dae * R
1391                         // where R, iR are bone rest and inverse rest mats in world space (Blender bones),
1392                         // iR_dae is joint inverse rest matrix (DAE) and M is an evaluated joint world-space matrix (DAE)
1393                         float temp[4][4], par[4][4];
1394
1395                         // calc M
1396                         calc_joint_parent_mat_rest(par, NULL, root, node);
1397                         mult_m4_m4m4(temp, par, matfra);
1398
1399                         // evaluate_joint_world_transform_at_frame(temp, NULL,, node, fra);
1400
1401                         // calc special matrix
1402                         mul_serie_m4(mat, irest, temp, irest_dae, rest, NULL, NULL, NULL, NULL);
1403                 }
1404                 else {
1405                         copy_m4_m4(mat, matfra);
1406                 }
1407
1408                 float val[4], rot[4], loc[3], scale[3];
1409
1410                 switch (tm_type) {
1411                 case COLLADAFW::Transformation::ROTATE:
1412                         mat4_to_quat(val, mat);
1413                         break;
1414                 case COLLADAFW::Transformation::SCALE:
1415                         mat4_to_size(val, mat);
1416                         break;
1417                 case COLLADAFW::Transformation::TRANSLATE:
1418                         copy_v3_v3(val, mat[3]);
1419                         break;
1420                 case COLLADAFW::Transformation::MATRIX:
1421                         mat4_to_quat(rot, mat);
1422                         copy_v3_v3(loc, mat[3]);
1423                         mat4_to_size(scale, mat);
1424                         break;
1425                 default:
1426                         break;
1427                 }
1428
1429                 // add keys
1430                 for (i = 0; i < totcu; i++) {
1431                         if (is_matrix) {
1432                                 if (i < 4)
1433                                         add_bezt(newcu[i], fra, rot[i]);
1434                                 else if (i < 7)
1435                                         add_bezt(newcu[i], fra, loc[i - 4]);
1436                                 else
1437                                         add_bezt(newcu[i], fra, scale[i - 7]);
1438                         }
1439                         else {
1440                                 add_bezt(newcu[i], fra, val[i]);
1441                         }
1442                 }
1443
1444 #ifdef ARMATURE_TEST
1445                 if (is_joint) {
1446                         switch (tm_type) {
1447                         case COLLADAFW::Transformation::ROTATE:
1448                                 mat4_to_quat(val, matfra);
1449                                 break;
1450                         case COLLADAFW::Transformation::SCALE:
1451                                 mat4_to_size(val, matfra);
1452                                 break;
1453                         case COLLADAFW::Transformation::TRANSLATE:
1454                                 copy_v3_v3(val, matfra[3]);
1455                                 break;
1456                         case MATRIX:
1457                                 mat4_to_quat(rot, matfra);
1458                                 copy_v3_v3(loc, matfra[3]);
1459                                 mat4_to_size(scale, matfra);
1460                                 break;
1461                         default:
1462                                 break;
1463                         }
1464
1465                         for (i = 0; i < totcu; i++) {
1466                                 if (is_matrix) {
1467                                         if (i < 4)
1468                                                 add_bezt(job_curves[i], fra, rot[i]);
1469                                         else if (i < 7)
1470                                                 add_bezt(job_curves[i], fra, loc[i - 4]);
1471                                         else
1472                                                 add_bezt(job_curves[i], fra, scale[i - 7]);
1473                                 }
1474                                 else {
1475                                         add_bezt(job_curves[i], fra, val[i]);
1476                                 }
1477                         }
1478                 }
1479 #endif
1480         }
1481
1482         verify_adt_action((ID*)&ob->id, 1);
1483
1484         ListBase *curves = &ob->adt->action->curves;
1485
1486         // add curves
1487         for (i = 0; i < totcu; i++) {
1488                 if (is_joint)
1489                         add_bone_fcurve(ob, node, newcu[i]);
1490                 else
1491                         BLI_addtail(curves, newcu[i]);
1492
1493 #ifdef ARMATURE_TEST
1494                 if (is_joint)
1495                         BLI_addtail(&job->adt->action->curves, job_curves[i]);
1496 #endif
1497         }
1498
1499         if (is_rotation || is_matrix) {
1500                 if (is_joint) {
1501                         bPoseChannel *chan = get_pose_channel(ob->pose, bone_name);
1502                         chan->rotmode = ROT_MODE_QUAT;
1503                 }
1504                 else {
1505                         ob->rotmode = ROT_MODE_QUAT;
1506                 }
1507         }
1508
1509         return job;
1510 }
1511
1512 // internal, better make it private
1513 // warning: evaluates only rotation and only assigns matrix transforms now
1514 // prerequisites: animlist_map, curve_map
1515 void AnimationImporter::evaluate_transform_at_frame(float mat[4][4], COLLADAFW::Node *node, float fra)
1516 {
1517         const COLLADAFW::TransformationPointerArray& tms = node->getTransformations();
1518
1519         unit_m4(mat);
1520
1521         for (unsigned int i = 0; i < tms.getCount(); i++) {
1522                 COLLADAFW::Transformation *tm = tms[i];
1523                 COLLADAFW::Transformation::TransformationType type = tm->getTransformationType();
1524                 float m[4][4];
1525
1526                 unit_m4(m);
1527
1528                 std::string nodename = node->getName().size() ? node->getName() : node->getOriginalId();
1529                 if (!evaluate_animation(tm, m, fra, nodename.c_str())) {
1530                         switch (type) {
1531                         case COLLADAFW::Transformation::ROTATE:
1532                                 dae_rotate_to_mat4(tm, m);
1533                                 break;
1534                         case COLLADAFW::Transformation::TRANSLATE:
1535                                 dae_translate_to_mat4(tm, m);
1536                                 break;
1537                         case COLLADAFW::Transformation::SCALE:
1538                                 dae_scale_to_mat4(tm, m);
1539                                 break;
1540                         case COLLADAFW::Transformation::MATRIX:
1541                                 dae_matrix_to_mat4(tm, m);
1542                                 break;
1543                         default:
1544                                 fprintf(stderr, "unsupported transformation type %d\n", type);
1545                         }
1546                         // dae_matrix_to_mat4(tm, m);
1547                         
1548                 }
1549
1550                 float temp[4][4];
1551                 copy_m4_m4(temp, mat);
1552
1553                 mult_m4_m4m4(mat, temp, m);
1554         }
1555 }
1556
1557 // return true to indicate that mat contains a sane value
1558 bool AnimationImporter::evaluate_animation(COLLADAFW::Transformation *tm, float mat[4][4], float fra, const char *node_id)
1559 {
1560         const COLLADAFW::UniqueId& listid = tm->getAnimationList();
1561         COLLADAFW::Transformation::TransformationType type = tm->getTransformationType();
1562
1563         if (type != COLLADAFW::Transformation::ROTATE &&
1564                 type != COLLADAFW::Transformation::SCALE &&
1565                 type != COLLADAFW::Transformation::TRANSLATE &&
1566                 type != COLLADAFW::Transformation::MATRIX) {
1567                 fprintf(stderr, "animation of transformation %d is not supported yet\n", type);
1568                 return false;
1569         }
1570
1571         if (animlist_map.find(listid) == animlist_map.end())
1572                 return false;
1573
1574         const COLLADAFW::AnimationList *animlist = animlist_map[listid];
1575         const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings();
1576
1577         if (bindings.getCount()) {
1578                 float vec[3];
1579
1580                 bool is_scale = (type == COLLADAFW::Transformation::SCALE);
1581                 bool is_translate = (type == COLLADAFW::Transformation::TRANSLATE);
1582
1583                 if (is_scale)
1584                         dae_scale_to_v3(tm, vec);
1585                 else if (is_translate)
1586                         dae_translate_to_v3(tm, vec);
1587
1588                 for (unsigned int j = 0; j < bindings.getCount(); j++) {
1589                         const COLLADAFW::AnimationList::AnimationBinding& binding = bindings[j];
1590                         std::vector<FCurve*>& curves = curve_map[binding.animation];
1591                         COLLADAFW::AnimationList::AnimationClass animclass = binding.animationClass;
1592                         char path[100];
1593
1594                         switch (type) {
1595                         case COLLADAFW::Transformation::ROTATE:
1596                                 BLI_snprintf(path, sizeof(path), "%s.rotate (binding %u)", node_id, j);
1597                                 break;
1598                         case COLLADAFW::Transformation::SCALE:
1599                                 BLI_snprintf(path, sizeof(path), "%s.scale (binding %u)", node_id, j);
1600                                 break;
1601                         case COLLADAFW::Transformation::TRANSLATE:
1602                                 BLI_snprintf(path, sizeof(path), "%s.translate (binding %u)", node_id, j);
1603                                 break;
1604                         case COLLADAFW::Transformation::MATRIX:
1605                                 BLI_snprintf(path, sizeof(path), "%s.matrix (binding %u)", node_id, j);
1606                                 break;
1607                         default:
1608                                 break;
1609                         }
1610
1611                         if (animclass == COLLADAFW::AnimationList::UNKNOWN_CLASS) {
1612                                 fprintf(stderr, "%s: UNKNOWN animation class\n", path);
1613                                 //continue;
1614                         }
1615
1616                         if (type == COLLADAFW::Transformation::ROTATE) {
1617                                 if (curves.size() != 1) {
1618                                         fprintf(stderr, "expected 1 curve, got %d\n", (int)curves.size());
1619                                         return false;
1620                                 }
1621
1622                                 // TODO support other animclasses
1623                                 if (animclass != COLLADAFW::AnimationList::ANGLE) {
1624                                         fprintf(stderr, "%s: animation class %d is not supported yet\n", path, animclass);
1625                                         return false;
1626                                 }
1627
1628                                 COLLADABU::Math::Vector3& axis = ((COLLADAFW::Rotate*)tm)->getRotationAxis();
1629
1630                                 float ax[3] = {(float)axis[0], (float)axis[1], (float)axis[2]};
1631                                 float angle = evaluate_fcurve(curves[0], fra);
1632                                 axis_angle_to_mat4(mat, ax, angle);
1633
1634                                 return true;
1635                         }
1636                         else if (is_scale || is_translate) {
1637                                 bool is_xyz = animclass == COLLADAFW::AnimationList::POSITION_XYZ;
1638
1639                                 if ((!is_xyz && curves.size() != 1) || (is_xyz && curves.size() != 3)) {
1640                                         if (is_xyz)
1641                                                 fprintf(stderr, "%s: expected 3 curves, got %d\n", path, (int)curves.size());
1642                                         else
1643                                                 fprintf(stderr, "%s: expected 1 curve, got %d\n", path, (int)curves.size());
1644                                         return false;
1645                                 }
1646                                 
1647                                 switch (animclass) {
1648                                 case COLLADAFW::AnimationList::POSITION_X:
1649                                         vec[0] = evaluate_fcurve(curves[0], fra);
1650                                         break;
1651                                 case COLLADAFW::AnimationList::POSITION_Y:
1652                                         vec[1] = evaluate_fcurve(curves[0], fra);
1653                                         break;
1654                                 case COLLADAFW::AnimationList::POSITION_Z:
1655                                         vec[2] = evaluate_fcurve(curves[0], fra);
1656                                         break;
1657                                 case COLLADAFW::AnimationList::POSITION_XYZ:
1658                                         vec[0] = evaluate_fcurve(curves[0], fra);
1659                                         vec[1] = evaluate_fcurve(curves[1], fra);
1660                                         vec[2] = evaluate_fcurve(curves[2], fra);
1661                                         break;
1662                                 default:
1663                                         fprintf(stderr, "%s: animation class %d is not supported yet\n", path, animclass);
1664                                         break;
1665                                 }
1666                         }
1667                         else if (type == COLLADAFW::Transformation::MATRIX) {
1668                                 // for now, of matrix animation, support only the case when all values are packed into one animation
1669                                 if (curves.size() != 16) {
1670                                         fprintf(stderr, "%s: expected 16 curves, got %d\n", path, (int)curves.size());
1671                                         return false;
1672                                 }
1673
1674                                 COLLADABU::Math::Matrix4 matrix;
1675                                 int i = 0, j = 0;
1676
1677                                 for (std::vector<FCurve*>::iterator it = curves.begin(); it != curves.end(); it++) {
1678                                         matrix.setElement(i, j, evaluate_fcurve(*it, fra));
1679                                         j++;
1680                                         if (j == 4) {
1681                                                 i++;
1682                                                 j = 0;
1683                                         }
1684                                         unused_curves.erase(std::remove(unused_curves.begin(), unused_curves.end(), *it), unused_curves.end());
1685                                 }
1686
1687                                 COLLADAFW::Matrix tm(matrix);
1688                                 dae_matrix_to_mat4(&tm, mat);
1689
1690                                 std::vector<FCurve*>::iterator it;
1691
1692                                 return true;
1693                         }
1694                 }
1695
1696                 if (is_scale)
1697                         size_to_mat4(mat, vec);
1698                 else
1699                         copy_v3_v3(mat[3], vec);
1700
1701                 return is_scale || is_translate;
1702         }
1703
1704         return false;
1705 }
1706
1707 // gives a world-space mat of joint at rest position
1708 void AnimationImporter::get_joint_rest_mat(float mat[4][4], COLLADAFW::Node *root, COLLADAFW::Node *node)
1709 {
1710         // if bind mat is not available,
1711         // use "current" node transform, i.e. all those tms listed inside <node>
1712         if (!armature_importer->get_joint_bind_mat(mat, node)) {
1713                 float par[4][4], m[4][4];
1714
1715                 calc_joint_parent_mat_rest(par, NULL, root, node);
1716                 get_node_mat(m, node, NULL, NULL);
1717                 mult_m4_m4m4(mat, par, m);
1718         }
1719 }
1720
1721 // gives a world-space mat, end's mat not included
1722 bool AnimationImporter::calc_joint_parent_mat_rest(float mat[4][4], float par[4][4], COLLADAFW::Node *node, COLLADAFW::Node *end)
1723 {
1724         float m[4][4];
1725
1726         if (node == end) {
1727                 par ? copy_m4_m4(mat, par) : unit_m4(mat);
1728                 return true;
1729         }
1730
1731         // use bind matrix if available or calc "current" world mat
1732         if (!armature_importer->get_joint_bind_mat(m, node)) {
1733                 if (par) {
1734                         float temp[4][4];
1735                         get_node_mat(temp, node, NULL, NULL);
1736                         mult_m4_m4m4(m, par, temp);
1737                 }
1738                 else {
1739                         get_node_mat(m, node, NULL, NULL);
1740                 }
1741         }
1742
1743         COLLADAFW::NodePointerArray& children = node->getChildNodes();
1744         for (unsigned int i = 0; i < children.getCount(); i++) {
1745                 if (calc_joint_parent_mat_rest(mat, m, children[i], end))
1746                         return true;
1747         }
1748
1749         return false;
1750 }
1751
1752 #ifdef ARMATURE_TEST
1753 Object *AnimationImporter::get_joint_object(COLLADAFW::Node *root, COLLADAFW::Node *node, Object *par_job)
1754 {
1755         if (joint_objects.find(node->getUniqueId()) == joint_objects.end()) {
1756                 Object *job = add_object(scene, OB_EMPTY);
1757
1758                 rename_id((ID*)&job->id, (char*)get_joint_name(node));
1759
1760                 job->lay = object_in_scene(job, scene)->lay = 2;
1761
1762                 mul_v3_fl(job->size, 0.5f);
1763                 job->recalc |= OB_RECALC_OB;
1764
1765                 verify_adt_action((ID*)&job->id, 1);
1766
1767                 job->rotmode = ROT_MODE_QUAT;
1768
1769                 float mat[4][4];
1770                 get_joint_rest_mat(mat, root, node);
1771
1772                 if (par_job) {
1773                         float temp[4][4], ipar[4][4];
1774                         invert_m4_m4(ipar, par_job->obmat);
1775                         copy_m4_m4(temp, mat);
1776                         mult_m4_m4m4(mat, ipar, temp);
1777                 }
1778
1779                 TransformBase::decompose(mat, job->loc, NULL, job->quat, job->size);
1780
1781                 if (par_job) {
1782                         job->parent = par_job;
1783
1784                         par_job->recalc |= OB_RECALC_OB;
1785                         job->parsubstr[0] = 0;
1786                 }
1787
1788                 where_is_object(scene, job);
1789
1790                 // after parenting and layer change
1791                 DAG_scene_sort(CTX_data_main(C), scene);
1792
1793                 joint_objects[node->getUniqueId()] = job;
1794         }
1795
1796         return joint_objects[node->getUniqueId()];
1797 }
1798 #endif
1799
1800 #if 0
1801 // recursively evaluates joint tree until end is found, mat then is world-space matrix of end
1802 // mat must be identity on enter, node must be root
1803 bool AnimationImporter::evaluate_joint_world_transform_at_frame(float mat[4][4], float par[4][4], COLLADAFW::Node *node, COLLADAFW::Node *end, float fra)
1804 {
1805         float m[4][4];
1806         if (par) {
1807                 float temp[4][4];
1808                 evaluate_transform_at_frame(temp, node, node == end ? fra : 0.0f);
1809                 mult_m4_m4m4(m, par, temp);
1810         }
1811         else {
1812                 evaluate_transform_at_frame(m, node, node == end ? fra : 0.0f);
1813         }
1814
1815         if (node == end) {
1816                 copy_m4_m4(mat, m);
1817                 return true;
1818         }
1819         else {
1820                 COLLADAFW::NodePointerArray& children = node->getChildNodes();
1821                 for (int i = 0; i < children.getCount(); i++) {
1822                         if (evaluate_joint_world_transform_at_frame(mat, m, children[i], end, fra))
1823                                 return true;
1824                 }
1825         }
1826
1827         return false;
1828 }
1829 #endif
1830
1831 void AnimationImporter::add_bone_fcurve(Object *ob, COLLADAFW::Node *node, FCurve *fcu)
1832 {
1833         const char *bone_name = bc_get_joint_name(node);
1834         bAction *act = ob->adt->action;
1835                         
1836         /* try to find group */
1837         bActionGroup *grp = action_groups_find_named(act, bone_name);
1838
1839         /* no matching groups, so add one */
1840         if (grp == NULL) {
1841                 /* Add a new group, and make it active */
1842                 grp = (bActionGroup*)MEM_callocN(sizeof(bActionGroup), "bActionGroup");
1843                                         
1844                 grp->flag = AGRP_SELECTED;
1845                 BLI_strncpy(grp->name, bone_name, sizeof(grp->name));
1846                                         
1847                 BLI_addtail(&act->groups, grp);
1848                 BLI_uniquename(&act->groups, grp, "Group", '.', offsetof(bActionGroup, name), 64);
1849         }
1850                                 
1851         /* add F-Curve to group */
1852         action_groups_add_channel(act, grp, fcu);
1853 }
1854
1855 void AnimationImporter::add_bezt(FCurve *fcu, float fra, float value)
1856 {
1857         //float fps = (float)FPS;
1858         BezTriple bez;
1859         memset(&bez, 0, sizeof(BezTriple));
1860         bez.vec[1][0] = fra;
1861         bez.vec[1][1] = value;
1862         bez.ipo = BEZT_IPO_LIN ;/* use default interpolation mode here... */
1863         bez.f1 = bez.f2 = bez.f3 = SELECT;
1864         bez.h1 = bez.h2 = HD_AUTO;
1865         insert_bezt_fcurve(fcu, &bez, 0);
1866         calchandles_fcurve(fcu);
1867 }
1868