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