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