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