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