style cleanup
[blender.git] / source / blender / collada / AnimationExporter.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed.
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 #include "GeometryExporter.h"
24 #include "AnimationExporter.h"
25 #include "MaterialExporter.h"
26
27 Global G;
28
29 template<class Functor>
30 void forEachObjectInExportSet(Scene *sce, Functor &f, LinkNode *export_set)
31 {
32         LinkNode *node;
33         for (node = export_set; node; node = node->next) {
34                 Object *ob = (Object *)node->link;
35                 f(ob);
36         }
37 }
38
39 bool AnimationExporter::exportAnimations(Scene *sce)
40 {
41         bool has_animations = hasAnimations(sce);
42         if (has_animations) {
43                 this->scene = sce;
44
45                 openLibrary();
46
47                 forEachObjectInExportSet(sce, *this, this->export_settings->export_set);
48
49                 closeLibrary();
50         }
51         return has_animations;
52 }
53
54 // called for each exported object
55 void AnimationExporter::operator()(Object *ob)
56 {
57         FCurve *fcu;
58         char *transformName;
59         /* bool isMatAnim = false; */ /* UNUSED */
60
61         //Export transform animations
62         if (ob->adt && ob->adt->action) {
63                 fcu = (FCurve *)ob->adt->action->curves.first;
64
65                 //transform matrix export for bones are temporarily disabled here.
66                 if (ob->type == OB_ARMATURE) {
67                         bArmature *arm = (bArmature *)ob->data;
68                         for (Bone *bone = (Bone *)arm->bonebase.first; bone; bone = bone->next)
69                                 write_bone_animation_matrix(ob, bone);
70                 }
71
72                 while (fcu) {
73                         //for armature animations as objects
74                         if (ob->type == OB_ARMATURE)
75                                 transformName =  fcu->rna_path;
76                         else 
77                                 transformName = extract_transform_name(fcu->rna_path);
78
79                         if ((!strcmp(transformName, "location") || !strcmp(transformName, "scale")) ||
80                             (!strcmp(transformName, "rotation_euler") && ob->rotmode == ROT_MODE_EUL) ||
81                             (!strcmp(transformName, "rotation_quaternion")))
82                         {
83                                 dae_animation(ob, fcu, transformName, false);
84                         }
85                         fcu = fcu->next;
86                 }
87
88         }
89
90         export_object_constraint_animation(ob);
91
92         //This needs to be handled by extra profiles, so postponed for now
93         //export_morph_animation(ob);
94                 
95         //Export Lamp parameter animations
96         if ( (ob->type == OB_LAMP) && ((Lamp *)ob->data)->adt && ((Lamp *)ob->data)->adt->action) {
97                 fcu = (FCurve *)(((Lamp *)ob->data)->adt->action->curves.first);
98                 while (fcu) {
99                         transformName = extract_transform_name(fcu->rna_path);
100
101                         if ((!strcmp(transformName, "color")) || (!strcmp(transformName, "spot_size")) ||
102                             (!strcmp(transformName, "spot_blend")) || (!strcmp(transformName, "distance")))
103                         {
104                                 dae_animation(ob, fcu, transformName, true);
105                         }
106                         fcu = fcu->next;
107                 }
108         }
109
110         //Export Camera parameter animations
111         if ( (ob->type == OB_CAMERA) && ((Camera *)ob->data)->adt && ((Camera *)ob->data)->adt->action) {
112                 fcu = (FCurve *)(((Camera *)ob->data)->adt->action->curves.first);
113                 while (fcu) {
114                         transformName = extract_transform_name(fcu->rna_path);
115
116                         if ((!strcmp(transformName, "lens")) ||
117                             (!strcmp(transformName, "ortho_scale")) ||
118                             (!strcmp(transformName, "clip_end")) || 
119                                 (!strcmp(transformName, "clip_start")))
120                         {
121                                 dae_animation(ob, fcu, transformName, true);
122                         }
123                         fcu = fcu->next;
124                 }
125         }
126
127         //Export Material parameter animations.
128         for (int a = 0; a < ob->totcol; a++) {
129                 Material *ma = give_current_material(ob, a + 1);
130                 if (!ma) continue;
131                 if (ma->adt && ma->adt->action) {
132                         /* isMatAnim = true; */
133                         fcu = (FCurve *)ma->adt->action->curves.first;
134                         while (fcu) {
135                                 transformName = extract_transform_name(fcu->rna_path);
136
137                                 if ((!strcmp(transformName, "specular_hardness")) || (!strcmp(transformName, "specular_color")) ||
138                                     (!strcmp(transformName, "diffuse_color")) || (!strcmp(transformName, "alpha")) ||
139                                     (!strcmp(transformName, "ior")))
140                                 {
141                                         dae_animation(ob, fcu, transformName, true, ma);
142                                 }
143                                 fcu = fcu->next;
144                         }
145                 }
146         }
147
148
149 }
150
151 void AnimationExporter::export_object_constraint_animation(Object *ob)
152 {
153         std::vector<float> fra;
154         //Takes frames of target animations
155         make_anim_frames_from_targets(ob, fra);
156
157         if (fra.size())
158                 dae_baked_object_animation(fra, ob);
159 }
160
161 void AnimationExporter::export_morph_animation(Object *ob)
162
163         FCurve *fcu;
164         char *transformName;
165         Key *key = BKE_key_from_object(ob);
166         if (!key) return;
167
168         if (key->adt && key->adt->action) {
169                 fcu = (FCurve *)key->adt->action->curves.first;
170                 
171                 while (fcu) {
172                         transformName = extract_transform_name(fcu->rna_path);
173
174                         dae_animation(ob, fcu, transformName, true);
175                         
176                         fcu = fcu->next;
177                 }
178         }
179
180 }
181
182 void AnimationExporter::make_anim_frames_from_targets(Object *ob, std::vector<float> &frames )
183 {
184         ListBase *conlist = get_active_constraints(ob);
185         if (conlist == NULL) return;
186         bConstraint *con;
187         for (con = (bConstraint *)conlist->first; con; con = con->next) {
188                 ListBase targets = {NULL, NULL};
189                 
190                 bConstraintTypeInfo *cti = BKE_constraint_get_typeinfo(con);
191                 
192                 if (!validateConstraints(con)) continue;
193
194                 if (cti && cti->get_constraint_targets) {
195                         bConstraintTarget *ct;
196                         Object *obtar;
197                         /* get targets 
198                          *  - constraints should use ct->matrix, not directly accessing values
199                          *      - ct->matrix members have not yet been calculated here! 
200                          */
201                         cti->get_constraint_targets(con, &targets);
202                         if (cti) {
203                                 for (ct = (bConstraintTarget *)targets.first; ct; ct = ct->next) {
204                                         obtar = ct->tar;
205                                         find_frames(obtar, frames);
206                                 }
207                         }
208                 }
209         }
210 }
211
212 //euler sources from quternion sources
213 float *AnimationExporter::get_eul_source_for_quat(Object *ob)
214 {
215         FCurve *fcu = (FCurve *)ob->adt->action->curves.first;
216         const int keys = fcu->totvert;  
217         float *quat = (float *)MEM_callocN(sizeof(float) * fcu->totvert * 4, "quat output source values");
218         float *eul = (float *)MEM_callocN(sizeof(float) * fcu->totvert * 3, "quat output source values");
219         float temp_quat[4];
220         float temp_eul[3];
221         while (fcu) {
222                 char *transformName = extract_transform_name(fcu->rna_path);
223
224                 if (!strcmp(transformName, "rotation_quaternion") ) {
225                         for (int i = 0; i < fcu->totvert; i++) {
226                                 *(quat + (i * 4) + fcu->array_index) = fcu->bezt[i].vec[1][1];
227                         }
228                 }
229                 fcu = fcu->next;
230         }
231
232         for (int i = 0; i < keys; i++) {
233                 for (int j = 0; j < 4; j++)
234                         temp_quat[j] = quat[(i * 4) + j];
235
236                 quat_to_eul(temp_eul, temp_quat);
237
238                 for (int k = 0; k < 3; k++)
239                         eul[i * 3 + k] = temp_eul[k];
240
241         }
242         MEM_freeN(quat);
243         return eul;
244
245 }
246
247 //Get proper name for bones
248 std::string AnimationExporter::getObjectBoneName(Object *ob, const FCurve *fcu)
249 {
250         //hard-way to derive the bone name from rna_path. Must find more compact method
251         std::string rna_path = std::string(fcu->rna_path);
252
253         char *boneName = strtok((char *)rna_path.c_str(), "\"");
254         boneName = strtok(NULL, "\"");
255
256         if (boneName != NULL)
257                 return /*id_name(ob) + "_" +*/ std::string(boneName);
258         else
259                 return id_name(ob);
260 }
261
262 std::string AnimationExporter::getAnimationPathId(const FCurve *fcu)
263 {
264         std::string rna_path = std::string(fcu->rna_path);
265         return translate_id(rna_path);
266 }
267
268 //convert f-curves to animation curves and write
269 void AnimationExporter::dae_animation(Object *ob, FCurve *fcu, char *transformName, bool is_param, Material *ma)
270 {
271         const char *axis_name = NULL;
272         char anim_id[200];
273
274         bool has_tangents = false;
275         bool quatRotation = false;
276
277         if (!strcmp(transformName, "rotation_quaternion") ) {
278                 fprintf(stderr, "quaternion rotation curves are not supported. rotation curve will not be exported\n");
279                 quatRotation = true;
280                 return;
281         }
282
283         //axis names for colors
284         else if (!strcmp(transformName, "color") ||
285                  !strcmp(transformName, "specular_color") ||
286                  !strcmp(transformName, "diffuse_color") ||
287                  !strcmp(transformName, "alpha"))
288         {
289                 const char *axis_names[] = {"R", "G", "B"};
290                 if (fcu->array_index < 3)
291                         axis_name = axis_names[fcu->array_index];
292         }
293
294         //axis names for transforms
295         else if (!strcmp(transformName, "location") ||
296                  !strcmp(transformName, "scale") ||
297                  !strcmp(transformName, "rotation_euler") ||
298                  !strcmp(transformName, "rotation_quaternion"))
299         {
300                 const char *axis_names[] = {"X", "Y", "Z"};
301                 if (fcu->array_index < 3)
302                         axis_name = axis_names[fcu->array_index];
303         }
304         else {
305                 /* no axis name. single parameter */
306                 axis_name = "";
307         }
308
309         std::string ob_name = std::string("null");
310
311         //Create anim Id
312         if (ob->type == OB_ARMATURE) {
313                 ob_name =  getObjectBoneName(ob, fcu);
314                 BLI_snprintf(
315                                 anim_id,
316                                 sizeof(anim_id),
317                                 "%s_%s.%s",
318                                 (char *)translate_id(ob_name).c_str(),
319                                 (char *)translate_id(transformName).c_str(),
320                                 axis_name);
321         }
322         else {
323                 if (ma)
324                         ob_name = id_name(ob) + "_material";
325                 else
326                         ob_name = id_name(ob);
327
328                 BLI_snprintf(
329                                 anim_id,
330                                 sizeof(anim_id),
331                                 "%s_%s_%s",
332                                 (char *)translate_id(ob_name).c_str(),
333                                 (char *)getAnimationPathId(fcu).c_str(),
334                                 axis_name);
335         }
336
337         openAnimation(anim_id, COLLADABU::Utils::EMPTY_STRING);
338
339         // create input source
340         std::string input_id = create_source_from_fcurve(COLLADASW::InputSemantic::INPUT, fcu, anim_id, axis_name);
341
342         // create output source
343         std::string output_id;
344
345         //quat rotations are skipped for now, because of complications with determining axis.
346         if (quatRotation) {
347                 float *eul  = get_eul_source_for_quat(ob);
348                 float *eul_axis = (float *)MEM_callocN(sizeof(float) * fcu->totvert, "quat output source values");
349                 for (int i = 0; i < fcu->totvert; i++) {
350                         eul_axis[i] = eul[i * 3 + fcu->array_index];
351                 }
352                 output_id = create_source_from_array(COLLADASW::InputSemantic::OUTPUT, eul_axis, fcu->totvert, quatRotation, anim_id, axis_name);
353                 MEM_freeN(eul);
354                 MEM_freeN(eul_axis);
355         }
356         else if (!strcmp(transformName, "lens") && (ob->type == OB_CAMERA)) {
357                 output_id = create_lens_source_from_fcurve((Camera *) ob->data, COLLADASW::InputSemantic::OUTPUT, fcu, anim_id);
358         }
359         else {
360                 output_id = create_source_from_fcurve(COLLADASW::InputSemantic::OUTPUT, fcu, anim_id, axis_name);
361         }
362
363         // create interpolations source
364         std::string interpolation_id = create_interpolation_source(fcu, anim_id, axis_name, &has_tangents);
365
366         // handle tangents (if required)
367         std::string intangent_id;
368         std::string outtangent_id;
369
370         if (has_tangents) {
371                 // create in_tangent source
372                 intangent_id = create_source_from_fcurve(COLLADASW::InputSemantic::IN_TANGENT, fcu, anim_id, axis_name);
373
374                 // create out_tangent source
375                 outtangent_id = create_source_from_fcurve(COLLADASW::InputSemantic::OUT_TANGENT, fcu, anim_id, axis_name);
376         }
377
378         std::string sampler_id = std::string(anim_id) + SAMPLER_ID_SUFFIX;
379         COLLADASW::LibraryAnimations::Sampler sampler(sw, sampler_id);
380         std::string empty;
381         sampler.addInput(COLLADASW::InputSemantic::INPUT, COLLADABU::URI(empty, input_id));
382         sampler.addInput(COLLADASW::InputSemantic::OUTPUT, COLLADABU::URI(empty, output_id));
383
384         // this input is required
385         sampler.addInput(COLLADASW::InputSemantic::INTERPOLATION, COLLADABU::URI(empty, interpolation_id));
386
387         if (has_tangents) {
388                 sampler.addInput(COLLADASW::InputSemantic::IN_TANGENT, COLLADABU::URI(empty, intangent_id));
389                 sampler.addInput(COLLADASW::InputSemantic::OUT_TANGENT, COLLADABU::URI(empty, outtangent_id));
390         }
391
392         addSampler(sampler);
393
394         std::string target;
395
396         if (!is_param)
397                 target = translate_id(ob_name) +
398                          "/" + get_transform_sid(fcu->rna_path, -1, axis_name, true);
399         else {
400                 if (ob->type == OB_LAMP)
401                         target = get_light_id(ob) +
402                                  "/" + get_light_param_sid(fcu->rna_path, -1, axis_name, true);
403
404                 if (ob->type == OB_CAMERA)
405                         target = get_camera_id(ob) +
406                                  "/" + get_camera_param_sid(fcu->rna_path, -1, axis_name, true);
407
408                 if (ma)
409                         target = translate_id(id_name(ma)) + "-effect" +
410                                  "/common/" /*profile common is only supported */ + get_transform_sid(fcu->rna_path, -1, axis_name, true);
411                 //if shape key animation, this is the main problem, how to define the channel targets.
412                 /*target = get_morph_id(ob) +
413                                  "/value" +*/ 
414         }
415         addChannel(COLLADABU::URI(empty, sampler_id), target);
416
417         closeAnimation();
418 }
419
420
421
422 //write bone animations in transform matrix sources
423 void AnimationExporter::write_bone_animation_matrix(Object *ob_arm, Bone *bone)
424 {
425         if (!ob_arm->adt)
426                 return;
427
428         //This will only export animations of bones in deform group.
429         /* if (!is_bone_deform_group(bone)) return; */
430
431         sample_and_write_bone_animation_matrix(ob_arm, bone);
432
433         for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next)
434                 write_bone_animation_matrix(ob_arm, child);
435 }
436
437 bool AnimationExporter::is_bone_deform_group(Bone *bone)
438 {   
439         bool is_def;
440         //Check if current bone is deform
441         if ((bone->flag & BONE_NO_DEFORM) == 0) return true;
442         //Check child bones
443         else {
444                 for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
445                         //loop through all the children until deform bone is found, and then return
446                         is_def = is_bone_deform_group(child);
447                         if (is_def) return true;
448                 }
449         }
450         //no deform bone found in children also
451         return false;
452 }
453
454 void AnimationExporter::sample_and_write_bone_animation_matrix(Object *ob_arm, Bone *bone)
455 {
456         bArmature *arm = (bArmature *)ob_arm->data;
457         int flag = arm->flag;
458         std::vector<float> fra;
459         //char prefix[256];
460
461         //Check if there is a fcurve in the armature for the bone in param
462         //when baking this check is not needed, solve every bone for every frame.
463         /*FCurve *fcu = (FCurve *)ob_arm->adt->action->curves.first;
464
465         while (fcu) {
466                 std::string bone_name = getObjectBoneName(ob_arm, fcu);
467                 int val = BLI_strcasecmp((char *)bone_name.c_str(), bone->name);
468                 if (val == 0) break;
469                 fcu = fcu->next;
470         }
471
472         if (!(fcu)) return;*/ 
473
474         bPoseChannel *pchan = BKE_pose_channel_find_name(ob_arm->pose, bone->name);
475         if (!pchan)
476                 return;
477
478         //every inserted keyframe of bones.     
479         find_frames(ob_arm, fra);
480
481         if (flag & ARM_RESTPOS) {
482                 arm->flag &= ~ARM_RESTPOS;
483                 BKE_pose_where_is(scene, ob_arm);
484         }
485
486         if (fra.size()) {
487                 dae_baked_animation(fra, ob_arm, bone);
488         }
489
490         if (flag & ARM_RESTPOS) 
491                 arm->flag = flag;
492         BKE_pose_where_is(scene, ob_arm);
493 }
494
495 void AnimationExporter::dae_baked_animation(std::vector<float> &fra, Object *ob_arm, Bone *bone)
496 {
497         std::string ob_name = id_name(ob_arm);
498         std::string bone_name = bone->name;
499         char anim_id[200];
500
501         if (!fra.size())
502                 return;
503
504         BLI_snprintf(anim_id, sizeof(anim_id), "%s_%s_%s", (char *)translate_id(ob_name).c_str(),
505                      (char *)translate_id(bone_name).c_str(), "pose_matrix");
506
507         openAnimation(anim_id, COLLADABU::Utils::EMPTY_STRING);
508
509         // create input source
510         std::string input_id = create_source_from_vector(COLLADASW::InputSemantic::INPUT, fra, false, anim_id, "");
511
512         // create output source
513         std::string output_id;
514
515         output_id = create_4x4_source(fra, ob_arm, bone, anim_id);
516
517         // create interpolations source
518         std::string interpolation_id = fake_interpolation_source(fra.size(), anim_id, "");
519
520         std::string sampler_id = std::string(anim_id) + SAMPLER_ID_SUFFIX;
521         COLLADASW::LibraryAnimations::Sampler sampler(sw, sampler_id);
522         std::string empty;
523         sampler.addInput(COLLADASW::InputSemantic::INPUT, COLLADABU::URI(empty, input_id));
524         sampler.addInput(COLLADASW::InputSemantic::OUTPUT, COLLADABU::URI(empty, output_id));
525
526         // TODO create in/out tangents source
527
528         // this input is required
529         sampler.addInput(COLLADASW::InputSemantic::INTERPOLATION, COLLADABU::URI(empty, interpolation_id));
530
531         addSampler(sampler);
532
533         std::string target = translate_id(bone_name) + "/transform";
534         addChannel(COLLADABU::URI(empty, sampler_id), target);
535
536         closeAnimation();
537 }
538
539 void AnimationExporter::dae_baked_object_animation(std::vector<float> &fra, Object *ob)
540 {
541         std::string ob_name = id_name(ob);
542         char anim_id[200];
543
544         if (!fra.size())
545                 return;
546
547         BLI_snprintf(anim_id, sizeof(anim_id), "%s_%s", (char *)translate_id(ob_name).c_str(),
548                      "object_matrix");
549
550         openAnimation(anim_id, COLLADABU::Utils::EMPTY_STRING);
551
552         // create input source
553         std::string input_id = create_source_from_vector(COLLADASW::InputSemantic::INPUT, fra, false, anim_id, "");
554
555         // create output source
556         std::string output_id;
557         output_id = create_4x4_source( fra, ob, NULL, anim_id);
558
559         // create interpolations source
560         std::string interpolation_id = fake_interpolation_source(fra.size(), anim_id, "");
561
562         std::string sampler_id = std::string(anim_id) + SAMPLER_ID_SUFFIX;
563         COLLADASW::LibraryAnimations::Sampler sampler(sw, sampler_id);
564         std::string empty;
565         sampler.addInput(COLLADASW::InputSemantic::INPUT, COLLADABU::URI(empty, input_id));
566         sampler.addInput(COLLADASW::InputSemantic::OUTPUT, COLLADABU::URI(empty, output_id));
567
568         // TODO create in/out tangents source
569
570         // this input is required
571         sampler.addInput(COLLADASW::InputSemantic::INTERPOLATION, COLLADABU::URI(empty, interpolation_id));
572
573         addSampler(sampler);
574
575         std::string target = translate_id(ob_name) + "/transform";
576         addChannel(COLLADABU::URI(empty, sampler_id), target);
577
578         closeAnimation();
579 }
580
581 // dae_bone_animation -> add_bone_animation
582 // (blend this into dae_bone_animation)
583 void AnimationExporter::dae_bone_animation(std::vector<float> &fra, float *values, int tm_type, int axis, std::string ob_name, std::string bone_name)
584 {
585         const char *axis_names[] = {"X", "Y", "Z"};
586         const char *axis_name = NULL;
587         char anim_id[200];
588         bool is_rot = tm_type == 0;
589
590         if (!fra.size())
591                 return;
592
593         char rna_path[200];
594         BLI_snprintf(rna_path, sizeof(rna_path), "pose.bones[\"%s\"].%s", bone_name.c_str(),
595                      tm_type == 0 ? "rotation_quaternion" : (tm_type == 1 ? "scale" : "location"));
596
597         if (axis > -1)
598                 axis_name = axis_names[axis];
599
600         std::string transform_sid = get_transform_sid(NULL, tm_type, axis_name, false);
601
602         BLI_snprintf(anim_id, sizeof(anim_id), "%s_%s_%s", (char *)translate_id(ob_name).c_str(),
603                      (char *)translate_id(bone_name).c_str(), (char *)transform_sid.c_str());
604
605         openAnimation(anim_id, COLLADABU::Utils::EMPTY_STRING);
606
607         // create input source
608         std::string input_id = create_source_from_vector(COLLADASW::InputSemantic::INPUT, fra, is_rot, anim_id, axis_name);
609
610         // create output source
611         std::string output_id;
612         if (axis == -1)
613                 output_id = create_xyz_source(values, fra.size(), anim_id);
614         else
615                 output_id = create_source_from_array(COLLADASW::InputSemantic::OUTPUT, values, fra.size(), is_rot, anim_id, axis_name);
616
617         // create interpolations source
618         std::string interpolation_id = fake_interpolation_source(fra.size(), anim_id, axis_name);
619
620         std::string sampler_id = std::string(anim_id) + SAMPLER_ID_SUFFIX;
621         COLLADASW::LibraryAnimations::Sampler sampler(sw, sampler_id);
622         std::string empty;
623         sampler.addInput(COLLADASW::InputSemantic::INPUT, COLLADABU::URI(empty, input_id));
624         sampler.addInput(COLLADASW::InputSemantic::OUTPUT, COLLADABU::URI(empty, output_id));
625
626         // TODO create in/out tangents source
627
628         // this input is required
629         sampler.addInput(COLLADASW::InputSemantic::INTERPOLATION, COLLADABU::URI(empty, interpolation_id));
630
631         addSampler(sampler);
632
633         std::string target = translate_id(ob_name + "_" + bone_name) + "/" + transform_sid;
634         addChannel(COLLADABU::URI(empty, sampler_id), target);
635
636         closeAnimation();
637 }
638
639 float AnimationExporter::convert_time(float frame)
640 {
641         return FRA2TIME(frame);
642 }
643
644 float AnimationExporter::convert_angle(float angle)
645 {
646         return COLLADABU::Math::Utils::radToDegF(angle);
647 }
648
649 std::string AnimationExporter::get_semantic_suffix(COLLADASW::InputSemantic::Semantics semantic)
650 {
651         switch (semantic) {
652                 case COLLADASW::InputSemantic::INPUT:
653                         return INPUT_SOURCE_ID_SUFFIX;
654                 case COLLADASW::InputSemantic::OUTPUT:
655                         return OUTPUT_SOURCE_ID_SUFFIX;
656                 case COLLADASW::InputSemantic::INTERPOLATION:
657                         return INTERPOLATION_SOURCE_ID_SUFFIX;
658                 case COLLADASW::InputSemantic::IN_TANGENT:
659                         return INTANGENT_SOURCE_ID_SUFFIX;
660                 case COLLADASW::InputSemantic::OUT_TANGENT:
661                         return OUTTANGENT_SOURCE_ID_SUFFIX;
662                 default:
663                         break;
664         }
665         return "";
666 }
667
668 void AnimationExporter::add_source_parameters(COLLADASW::SourceBase::ParameterNameList& param,
669                                               COLLADASW::InputSemantic::Semantics semantic, bool is_rot, const char *axis, bool transform)
670 {
671         switch (semantic) {
672                 case COLLADASW::InputSemantic::INPUT:
673                         param.push_back("TIME");
674                         break;
675                 case COLLADASW::InputSemantic::OUTPUT:
676                         if (is_rot) {
677                                 param.push_back("ANGLE");
678                         }
679                         else {
680                                 if (axis) {
681                                         param.push_back(axis);
682                                 }
683                                 else 
684                                 if (transform) {
685                                         param.push_back("TRANSFORM");
686                                 }
687                                 else {     //assumes if axis isn't specified all axises are added
688                                         param.push_back("X");
689                                         param.push_back("Y");
690                                         param.push_back("Z");
691                                 }
692                         }
693                         break;
694                 case COLLADASW::InputSemantic::IN_TANGENT:
695                 case COLLADASW::InputSemantic::OUT_TANGENT:
696                         param.push_back("X");
697                         param.push_back("Y");
698                         break;
699                 default:
700                         break;
701         }
702 }
703
704 void AnimationExporter::get_source_values(BezTriple *bezt, COLLADASW::InputSemantic::Semantics semantic, bool is_rotation, float *values, int *length)
705 {
706         switch (semantic) {
707                 case COLLADASW::InputSemantic::INPUT:
708                         *length = 1;
709                         values[0] = convert_time(bezt->vec[1][0]);
710                         break;
711                 case COLLADASW::InputSemantic::OUTPUT:
712                         *length = 1;
713                         if (is_rotation) {
714                                 values[0] = RAD2DEGF(bezt->vec[1][1]);
715                         }
716                         else {
717                                 values[0] = bezt->vec[1][1];
718                         }
719                         break;
720
721                 case COLLADASW::InputSemantic::IN_TANGENT:
722                         *length = 2;
723                         values[0] = convert_time(bezt->vec[0][0]);
724                         if (bezt->ipo != BEZT_IPO_BEZ) {
725                                 // We're in a mixed interpolation scenario, set zero as it's irrelevant but value might contain unused data
726                                 values[0] = 0;
727                                 values[1] = 0;
728                         }
729                         else if (is_rotation) {
730                                 values[1] = RAD2DEGF(bezt->vec[0][1]);
731                         }
732                         else {
733                                 values[1] = bezt->vec[0][1];
734                         }
735                         break;
736
737                 case COLLADASW::InputSemantic::OUT_TANGENT:
738                         *length = 2;
739                         values[0] = convert_time(bezt->vec[2][0]);
740                         if (bezt->ipo != BEZT_IPO_BEZ) {
741                                 // We're in a mixed interpolation scenario, set zero as it's irrelevant but value might contain unused data
742                                 values[0] = 0;
743                                 values[1] = 0;
744                         }
745                         else if (is_rotation) {
746                                 values[1] = RAD2DEGF(bezt->vec[2][1]);
747                         }
748                         else {
749                                 values[1] = bezt->vec[2][1];
750                         }
751                         break;
752                 default:
753                         *length = 0;
754                         break;
755         }
756 }
757
758 std::string AnimationExporter::create_source_from_fcurve(COLLADASW::InputSemantic::Semantics semantic, FCurve *fcu, const std::string& anim_id, const char *axis_name)
759 {
760         std::string source_id = anim_id + get_semantic_suffix(semantic);
761
762         //bool is_rotation = !strcmp(fcu->rna_path, "rotation");
763         bool is_angle = false;
764
765         if (strstr(fcu->rna_path, "rotation")) is_angle = true;
766
767         COLLADASW::FloatSourceF source(mSW);
768         source.setId(source_id);
769         source.setArrayId(source_id + ARRAY_ID_SUFFIX);
770         source.setAccessorCount(fcu->totvert);
771
772         switch (semantic) {
773                 case COLLADASW::InputSemantic::INPUT:
774                 case COLLADASW::InputSemantic::OUTPUT:
775                         source.setAccessorStride(1);
776                         break;
777                 case COLLADASW::InputSemantic::IN_TANGENT:
778                 case COLLADASW::InputSemantic::OUT_TANGENT:
779                         source.setAccessorStride(2);
780                         break;
781                 default:
782                         break;
783         }
784
785
786         COLLADASW::SourceBase::ParameterNameList &param = source.getParameterNameList();
787         add_source_parameters(param, semantic, is_angle, axis_name, false);
788
789         source.prepareToAppendValues();
790
791         for (unsigned int i = 0; i < fcu->totvert; i++) {
792                 float values[3]; // be careful!
793                 int length = 0;
794                 get_source_values(&fcu->bezt[i], semantic, is_angle, values, &length);
795                 for (int j = 0; j < length; j++)
796                         source.appendValues(values[j]);
797         }
798
799         source.finish();
800
801         return source_id;
802 }
803
804 /*
805  * Similar to create_source_from_fcurve, but adds conversion of lens
806  * animation data from focal length to FOV.
807  */
808 std::string AnimationExporter::create_lens_source_from_fcurve(Camera *cam, COLLADASW::InputSemantic::Semantics semantic, FCurve *fcu, const std::string& anim_id)
809 {
810         std::string source_id = anim_id + get_semantic_suffix(semantic);
811
812         COLLADASW::FloatSourceF source(mSW);
813         source.setId(source_id);
814         source.setArrayId(source_id + ARRAY_ID_SUFFIX);
815         source.setAccessorCount(fcu->totvert);
816
817         source.setAccessorStride(1);
818
819         COLLADASW::SourceBase::ParameterNameList &param = source.getParameterNameList();
820         add_source_parameters(param, semantic, false, "", false);
821
822         source.prepareToAppendValues();
823
824         for (unsigned int i = 0; i < fcu->totvert; i++) {
825                 float values[3]; // be careful!
826                 int length = 0;
827                 get_source_values(&fcu->bezt[i], semantic, false, values, &length);
828                 for (int j = 0; j < length; j++)
829                 {
830                         float val = RAD2DEGF(focallength_to_fov(values[j], cam->sensor_x));
831                         source.appendValues(val);
832                 }
833         }
834
835         source.finish();
836
837         return source_id;
838 }
839
840
841
842 //Currently called only to get OUTPUT source values ( if rotation and hence the axis is also specified )
843 std::string AnimationExporter::create_source_from_array(COLLADASW::InputSemantic::Semantics semantic, float *v, int tot, bool is_rot, const std::string& anim_id, const char *axis_name)
844 {
845         std::string source_id = anim_id + get_semantic_suffix(semantic);
846
847         COLLADASW::FloatSourceF source(mSW);
848         source.setId(source_id);
849         source.setArrayId(source_id + ARRAY_ID_SUFFIX);
850         source.setAccessorCount(tot);
851         source.setAccessorStride(1);
852
853         COLLADASW::SourceBase::ParameterNameList &param = source.getParameterNameList();
854         add_source_parameters(param, semantic, is_rot, axis_name,  false);
855
856         source.prepareToAppendValues();
857
858         for (int i = 0; i < tot; i++) {
859                 float val = v[i];
860                 ////if (semantic == COLLADASW::InputSemantic::INPUT)
861                 //      val = convert_time(val);
862                 //else
863                 if (is_rot)
864                         val = RAD2DEGF(val);
865                 source.appendValues(val);
866         }
867
868         source.finish();
869
870         return source_id;
871 }
872 // only used for sources with INPUT semantic
873 std::string AnimationExporter::create_source_from_vector(COLLADASW::InputSemantic::Semantics semantic, std::vector<float> &fra, bool is_rot, const std::string& anim_id, const char *axis_name)
874 {
875         std::string source_id = anim_id + get_semantic_suffix(semantic);
876
877         COLLADASW::FloatSourceF source(mSW);
878         source.setId(source_id);
879         source.setArrayId(source_id + ARRAY_ID_SUFFIX);
880         source.setAccessorCount(fra.size());
881         source.setAccessorStride(1);
882
883         COLLADASW::SourceBase::ParameterNameList &param = source.getParameterNameList();
884         add_source_parameters(param, semantic, is_rot, axis_name, false);
885
886         source.prepareToAppendValues();
887
888         std::vector<float>::iterator it;
889         for (it = fra.begin(); it != fra.end(); it++) {
890                 float val = *it;
891                 //if (semantic == COLLADASW::InputSemantic::INPUT)
892                 val = convert_time(val);
893                 /*else if (is_rot)
894                    val = convert_angle(val);*/
895                 source.appendValues(val);
896         }
897
898         source.finish();
899
900         return source_id;
901 }
902
903
904 std::string AnimationExporter::create_4x4_source(std::vector<float> &frames, Object *ob, Bone *bone, const std::string &anim_id)
905 {
906         COLLADASW::InputSemantic::Semantics semantic = COLLADASW::InputSemantic::OUTPUT;
907         std::string source_id = anim_id + get_semantic_suffix(semantic);
908
909         COLLADASW::Float4x4Source source(mSW);
910         source.setId(source_id);
911         source.setArrayId(source_id + ARRAY_ID_SUFFIX);
912         source.setAccessorCount(frames.size());
913         source.setAccessorStride(16);
914
915         COLLADASW::SourceBase::ParameterNameList &param = source.getParameterNameList();
916         add_source_parameters(param, semantic, false, NULL, true);
917
918         source.prepareToAppendValues();
919
920         bPoseChannel *parchan = NULL;
921         bPoseChannel *pchan = NULL;
922
923         if (ob->type == OB_ARMATURE ) {
924                 bPose *pose = ob->pose;
925                 pchan = BKE_pose_channel_find_name(pose, bone->name);
926                 if (!pchan)
927                         return "";
928
929                 parchan = pchan->parent;
930
931                 enable_fcurves(ob->adt->action, bone->name);
932         }
933         
934         std::vector<float>::iterator it;
935         int j = 0;
936         for (it = frames.begin(); it != frames.end(); it++) {
937                 float mat[4][4], ipar[4][4];
938
939                 float ctime = BKE_scene_frame_get_from_ctime(scene, *it);
940                 CFRA = BKE_scene_frame_get_from_ctime(scene, *it);
941                 //BKE_scene_update_for_newframe(G.main,scene,scene->lay);
942                 BKE_animsys_evaluate_animdata(scene, &ob->id, ob->adt, ctime, ADT_RECALC_ALL);
943                                 
944                 if (bone) {
945                         if (pchan->flag & POSE_CHAIN) {
946                                 enable_fcurves(ob->adt->action, NULL);
947                                 BKE_animsys_evaluate_animdata(scene, &ob->id, ob->adt, ctime, ADT_RECALC_ALL);
948                                 BKE_pose_where_is(scene, ob);
949                         }
950                         else {
951                                 BKE_pose_where_is_bone(scene, ob, pchan, ctime, 1);
952                         }
953                         
954                         // compute bone local mat
955                         if (bone->parent) {
956                                 invert_m4_m4(ipar, parchan->pose_mat);
957                                 mult_m4_m4m4(mat, ipar, pchan->pose_mat);
958                         }
959                         else
960                                 copy_m4_m4(mat, pchan->pose_mat);
961                         
962                 // SECOND_LIFE_COMPATIBILITY
963                 // AFAIK animation to second life is via BVH, but no
964                 // reason to not have the collada-animation be correct
965                         if (export_settings->second_life) {
966                                 float temp[4][4];
967                                 copy_m4_m4(temp, bone->arm_mat);
968                                 temp[3][0] = temp[3][1] = temp[3][2] = 0.0f;
969                                 invert_m4(temp);
970
971                                 mult_m4_m4m4(mat, mat, temp);
972
973                                 if (bone->parent) {
974                                         copy_m4_m4(temp, bone->parent->arm_mat);
975                                         temp[3][0] = temp[3][1] = temp[3][2] = 0.0f;
976
977                                         mult_m4_m4m4(mat, temp, mat);
978                                 }
979                         }
980
981                 }
982                 else {
983                         calc_ob_mat_at_time(ob, ctime, mat);
984                 }
985                 
986                 UnitConverter converter;
987
988                 double outmat[4][4];
989                 converter.mat4_to_dae_double(outmat, mat);
990
991                 source.appendValues(outmat);
992
993                 j++;
994
995                 BIK_release_tree(scene, ob, ctime);
996         }
997
998         enable_fcurves(ob->adt->action, NULL);
999
1000         source.finish();
1001
1002         return source_id;
1003 }
1004
1005
1006 // only used for sources with OUTPUT semantic ( locations and scale)
1007 std::string AnimationExporter::create_xyz_source(float *v, int tot, const std::string& anim_id)
1008 {
1009         COLLADASW::InputSemantic::Semantics semantic = COLLADASW::InputSemantic::OUTPUT;
1010         std::string source_id = anim_id + get_semantic_suffix(semantic);
1011
1012         COLLADASW::FloatSourceF source(mSW);
1013         source.setId(source_id);
1014         source.setArrayId(source_id + ARRAY_ID_SUFFIX);
1015         source.setAccessorCount(tot);
1016         source.setAccessorStride(3);
1017
1018         COLLADASW::SourceBase::ParameterNameList &param = source.getParameterNameList();
1019         add_source_parameters(param, semantic, false, NULL, false);
1020
1021         source.prepareToAppendValues();
1022
1023         for (int i = 0; i < tot; i++) {
1024                 source.appendValues(*v, *(v + 1), *(v + 2));
1025                 v += 3;
1026         }
1027
1028         source.finish();
1029
1030         return source_id;
1031 }
1032
1033 std::string AnimationExporter::create_interpolation_source(FCurve *fcu, const std::string& anim_id, const char *axis_name, bool *has_tangents)
1034 {
1035         std::string source_id = anim_id + get_semantic_suffix(COLLADASW::InputSemantic::INTERPOLATION);
1036
1037         COLLADASW::NameSource source(mSW);
1038         source.setId(source_id);
1039         source.setArrayId(source_id + ARRAY_ID_SUFFIX);
1040         source.setAccessorCount(fcu->totvert);
1041         source.setAccessorStride(1);
1042
1043         COLLADASW::SourceBase::ParameterNameList &param = source.getParameterNameList();
1044         param.push_back("INTERPOLATION");
1045
1046         source.prepareToAppendValues();
1047
1048         *has_tangents = false;
1049
1050         for (unsigned int i = 0; i < fcu->totvert; i++) {
1051                 if (fcu->bezt[i].ipo == BEZT_IPO_BEZ) {
1052                         source.appendValues(BEZIER_NAME);
1053                         *has_tangents = true;
1054                 }
1055                 else if (fcu->bezt[i].ipo == BEZT_IPO_CONST) {
1056                         source.appendValues(STEP_NAME);
1057                 }
1058                 else { // BEZT_IPO_LIN
1059                         source.appendValues(LINEAR_NAME);
1060                 }
1061         }
1062         // unsupported? -- HERMITE, CARDINAL, BSPLINE, NURBS
1063
1064         source.finish();
1065
1066         return source_id;
1067 }
1068
1069 std::string AnimationExporter::fake_interpolation_source(int tot, const std::string& anim_id, const char *axis_name)
1070 {
1071         std::string source_id = anim_id + get_semantic_suffix(COLLADASW::InputSemantic::INTERPOLATION);
1072
1073         COLLADASW::NameSource source(mSW);
1074         source.setId(source_id);
1075         source.setArrayId(source_id + ARRAY_ID_SUFFIX);
1076         source.setAccessorCount(tot);
1077         source.setAccessorStride(1);
1078
1079         COLLADASW::SourceBase::ParameterNameList &param = source.getParameterNameList();
1080         param.push_back("INTERPOLATION");
1081
1082         source.prepareToAppendValues();
1083
1084         for (int i = 0; i < tot; i++) {
1085                 source.appendValues(LINEAR_NAME);
1086         }
1087
1088         source.finish();
1089
1090         return source_id;
1091 }
1092
1093 std::string AnimationExporter::get_light_param_sid(char *rna_path, int tm_type, const char *axis_name, bool append_axis)
1094 {
1095         std::string tm_name;
1096         // when given rna_path, determine tm_type from it
1097         if (rna_path) {
1098                 char *name = extract_transform_name(rna_path);
1099
1100                 if (!strcmp(name, "color"))
1101                         tm_type = 1;
1102                 else if (!strcmp(name, "spot_size"))
1103                         tm_type = 2;
1104                 else if (!strcmp(name, "spot_blend"))
1105                         tm_type = 3;
1106                 else if (!strcmp(name, "distance"))
1107                         tm_type = 4;
1108                 else
1109                         tm_type = -1;
1110         }
1111
1112         switch (tm_type) {
1113                 case 1:
1114                         tm_name = "color";
1115                         break;
1116                 case 2:
1117                         tm_name = "fall_off_angle";
1118                         break;
1119                 case 3:
1120                         tm_name = "fall_off_exponent";
1121                         break;
1122                 case 4:
1123                         tm_name = "blender/blender_dist";
1124                         break;
1125
1126                 default:
1127                         tm_name = "";
1128                         break;
1129         }
1130
1131         if (tm_name.size()) {
1132                 if (axis_name[0])
1133                         return tm_name + "." + std::string(axis_name);
1134                 else 
1135                         return tm_name;
1136         }
1137
1138         return std::string("");
1139 }
1140
1141 std::string AnimationExporter::get_camera_param_sid(char *rna_path, int tm_type, const char *axis_name, bool append_axis)
1142 {
1143         std::string tm_name;
1144         // when given rna_path, determine tm_type from it
1145         if (rna_path) {
1146                 char *name = extract_transform_name(rna_path);
1147
1148                 if (!strcmp(name, "lens"))
1149                         tm_type = 0;
1150                 else if (!strcmp(name, "ortho_scale"))
1151                         tm_type = 1;
1152                 else if (!strcmp(name, "clip_end"))
1153                         tm_type = 2;
1154                 else if (!strcmp(name, "clip_start"))
1155                         tm_type = 3;
1156
1157                 else
1158                         tm_type = -1;
1159         }
1160
1161         switch (tm_type) {
1162                 case 0:
1163                         tm_name = "xfov";
1164                         break;
1165                 case 1:
1166                         tm_name = "xmag";
1167                         break;
1168                 case 2:
1169                         tm_name = "zfar";
1170                         break;
1171                 case 3:
1172                         tm_name = "znear";
1173                         break;
1174
1175                 default:
1176                         tm_name = "";
1177                         break;
1178         }
1179
1180         if (tm_name.size()) {
1181                 if (axis_name[0])
1182                         return tm_name + "." + std::string(axis_name);
1183                 else 
1184                         return tm_name;
1185         }
1186
1187         return std::string("");
1188 }
1189
1190 // Assign sid of the animated parameter or transform 
1191 // for rotation, axis name is always appended and the value of append_axis is ignored
1192 std::string AnimationExporter::get_transform_sid(char *rna_path, int tm_type, const char *axis_name, bool append_axis)
1193 {
1194         std::string tm_name;
1195         bool is_rotation = false;
1196         // when given rna_path, determine tm_type from it
1197         if (rna_path) {
1198                 char *name = extract_transform_name(rna_path);
1199
1200                 if (!strcmp(name, "rotation_euler"))
1201                         tm_type = 0;
1202                 else if (!strcmp(name, "rotation_quaternion"))
1203                         tm_type = 1;
1204                 else if (!strcmp(name, "scale"))
1205                         tm_type = 2;
1206                 else if (!strcmp(name, "location"))
1207                         tm_type = 3;
1208                 else if (!strcmp(name, "specular_hardness"))
1209                         tm_type = 4;
1210                 else if (!strcmp(name, "specular_color"))
1211                         tm_type = 5;
1212                 else if (!strcmp(name, "diffuse_color"))
1213                         tm_type = 6;
1214                 else if (!strcmp(name, "alpha"))
1215                         tm_type = 7;
1216                 else if (!strcmp(name, "ior"))
1217                         tm_type = 8;
1218
1219                 else
1220                         tm_type = -1;
1221         }
1222
1223         switch (tm_type) {
1224                 case 0:
1225                 case 1:
1226                         tm_name = "rotation";
1227                         is_rotation = true;
1228                         break;
1229                 case 2:
1230                         tm_name = "scale";
1231                         break;
1232                 case 3:
1233                         tm_name = "location";
1234                         break;
1235                 case 4:
1236                         tm_name = "shininess";
1237                         break;
1238                 case 5:
1239                         tm_name = "specular";
1240                         break;
1241                 case 6:
1242                         tm_name = "diffuse";
1243                         break;
1244                 case 7:
1245                         tm_name = "transparency";
1246                         break;
1247                 case 8:
1248                         tm_name = "index_of_refraction";
1249                         break;
1250
1251                 default:
1252                         tm_name = "";
1253                         break;
1254         }
1255
1256         if (tm_name.size()) {
1257                 if (is_rotation)
1258                         return tm_name + std::string(axis_name) + ".ANGLE";
1259                 else
1260                 if (axis_name[0])
1261                         return tm_name + "." + std::string(axis_name);
1262                 else
1263                         return tm_name;
1264         }
1265
1266         return std::string("");
1267 }
1268
1269 char *AnimationExporter::extract_transform_name(char *rna_path)
1270 {
1271         char *dot = strrchr(rna_path, '.');
1272         return dot ? (dot + 1) : rna_path;
1273 }
1274
1275 //find keyframes of all the objects animations
1276 void AnimationExporter::find_frames(Object *ob, std::vector<float> &fra)
1277 {
1278         FCurve *fcu = (FCurve *)ob->adt->action->curves.first;
1279
1280         for (; fcu; fcu = fcu->next) {
1281
1282                 for (unsigned int i = 0; i < fcu->totvert; i++) {
1283                         float f = fcu->bezt[i].vec[1][0];
1284                         if (std::find(fra.begin(), fra.end(), f) == fra.end())   
1285                                 fra.push_back(f);
1286                 }
1287         }
1288
1289         // keep the keys in ascending order
1290         std::sort(fra.begin(), fra.end());
1291 }
1292
1293
1294
1295 // enable fcurves driving a specific bone, disable all the rest
1296 // if bone_name = NULL enable all fcurves
1297 void AnimationExporter::enable_fcurves(bAction *act, char *bone_name)
1298 {
1299         FCurve *fcu;
1300         char prefix[200];
1301
1302         if (bone_name)
1303                 BLI_snprintf(prefix, sizeof(prefix), "pose.bones[\"%s\"]", bone_name);
1304
1305         for (fcu = (FCurve *)act->curves.first; fcu; fcu = fcu->next) {
1306                 if (bone_name) {
1307                         if (!strncmp(fcu->rna_path, prefix, strlen(prefix)))
1308                                 fcu->flag &= ~FCURVE_DISABLED;
1309                         else
1310                                 fcu->flag |= FCURVE_DISABLED;
1311                 }
1312                 else {
1313                         fcu->flag &= ~FCURVE_DISABLED;
1314                 }
1315         }
1316 }
1317
1318 bool AnimationExporter::hasAnimations(Scene *sce)
1319 {
1320         LinkNode *node;
1321
1322         for (node=this->export_settings->export_set; node; node=node->next) {
1323                 Object *ob = (Object *)node->link;
1324
1325                 FCurve *fcu = 0;
1326                 //Check for object transform animations
1327                 if (ob->adt && ob->adt->action)
1328                         fcu = (FCurve *)ob->adt->action->curves.first;
1329                 //Check for Lamp parameter animations
1330                 else if ( (ob->type == OB_LAMP) && ((Lamp *)ob->data)->adt && ((Lamp *)ob->data)->adt->action)
1331                         fcu = (FCurve *)(((Lamp *)ob->data)->adt->action->curves.first);
1332                 //Check for Camera parameter animations
1333                 else if ( (ob->type == OB_CAMERA) && ((Camera *)ob->data)->adt && ((Camera *)ob->data)->adt->action)
1334                         fcu = (FCurve *)(((Camera *)ob->data)->adt->action->curves.first);
1335
1336                 //Check Material Effect parameter animations.
1337                 for (int a = 0; a < ob->totcol; a++) {
1338                         Material *ma = give_current_material(ob, a + 1);
1339                         if (!ma) continue;
1340                         if (ma->adt && ma->adt->action) {
1341                                 fcu = (FCurve *)ma->adt->action->curves.first;
1342                         }
1343                 }
1344
1345                 //check shape key animation
1346                 if (!fcu) {
1347                         Key *key = BKE_key_from_object(ob);
1348                         if (key && key->adt && key->adt->action)
1349                                 fcu = (FCurve *)key->adt->action->curves.first;
1350                 }
1351                 if (fcu)
1352                         return true;
1353         }
1354         return false;
1355 }
1356
1357 //------------------------------- Not used in the new system.--------------------------------------------------------
1358 void AnimationExporter::find_rotation_frames(Object *ob, std::vector<float> &fra, const char *prefix, int rotmode)
1359 {
1360         if (rotmode > 0)
1361                 find_frames(ob, fra, prefix, "rotation_euler");
1362         else if (rotmode == ROT_MODE_QUAT)
1363                 find_frames(ob, fra, prefix, "rotation_quaternion");
1364         /*else if (rotmode == ROT_MODE_AXISANGLE)
1365            ;*/
1366 }
1367
1368 void AnimationExporter::find_frames(Object *ob, std::vector<float> &fra, const char *prefix, const char *tm_name)
1369 {
1370         FCurve *fcu = (FCurve *)ob->adt->action->curves.first;
1371
1372         for (; fcu; fcu = fcu->next) {
1373                 if (prefix && strncmp(prefix, fcu->rna_path, strlen(prefix)))
1374                         continue;
1375
1376                 char *name = extract_transform_name(fcu->rna_path);
1377                 if (!strcmp(name, tm_name)) {
1378                         for (unsigned int i = 0; i < fcu->totvert; i++) {
1379                                 float f = fcu->bezt[i].vec[1][0];
1380                                 if (std::find(fra.begin(), fra.end(), f) == fra.end())   
1381                                         fra.push_back(f);
1382                         }
1383                 }
1384         }
1385
1386         // keep the keys in ascending order
1387         std::sort(fra.begin(), fra.end());
1388 }
1389
1390 void AnimationExporter::write_bone_animation(Object *ob_arm, Bone *bone)
1391 {
1392         if (!ob_arm->adt)
1393                 return;
1394
1395         //write bone animations for 3 transform types
1396         //i=0 --> rotations
1397         //i=1 --> scale
1398         //i=2 --> location
1399         for (int i = 0; i < 3; i++)
1400                 sample_and_write_bone_animation(ob_arm, bone, i);
1401
1402         for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next)
1403                 write_bone_animation(ob_arm, child);
1404 }
1405
1406 void AnimationExporter::sample_and_write_bone_animation(Object *ob_arm, Bone *bone, int transform_type)
1407 {
1408         bArmature *arm = (bArmature *)ob_arm->data;
1409         int flag = arm->flag;
1410         std::vector<float> fra;
1411         char prefix[256];
1412
1413         BLI_snprintf(prefix, sizeof(prefix), "pose.bones[\"%s\"]", bone->name);
1414
1415         bPoseChannel *pchan = BKE_pose_channel_find_name(ob_arm->pose, bone->name);
1416         if (!pchan)
1417                 return;
1418         //Fill frame array with key frame values framed at \param:transform_type
1419         switch (transform_type) {
1420                 case 0:
1421                         find_rotation_frames(ob_arm, fra, prefix, pchan->rotmode);
1422                         break;
1423                 case 1:
1424                         find_frames(ob_arm, fra, prefix, "scale");
1425                         break;
1426                 case 2:
1427                         find_frames(ob_arm, fra, prefix, "location");
1428                         break;
1429                 default:
1430                         return;
1431         }
1432
1433         // exit rest position
1434         if (flag & ARM_RESTPOS) {
1435                 arm->flag &= ~ARM_RESTPOS;
1436                 BKE_pose_where_is(scene, ob_arm);
1437         }
1438         //v array will hold all values which will be exported. 
1439         if (fra.size()) {
1440                 float *values = (float *)MEM_callocN(sizeof(float) * 3 * fra.size(), "temp. anim frames");
1441                 sample_animation(values, fra, transform_type, bone, ob_arm, pchan);
1442
1443                 if (transform_type == 0) {
1444                         // write x, y, z curves separately if it is rotation
1445                         float *axisValues = (float *)MEM_callocN(sizeof(float) * fra.size(), "temp. anim frames");
1446
1447                         for (int i = 0; i < 3; i++) {
1448                                 for (unsigned int j = 0; j < fra.size(); j++)
1449                                         axisValues[j] = values[j * 3 + i];
1450
1451                                 dae_bone_animation(fra, axisValues, transform_type, i, id_name(ob_arm), bone->name);
1452                         }
1453                         MEM_freeN(axisValues);
1454                 }
1455                 else {
1456                         // write xyz at once if it is location or scale
1457                         dae_bone_animation(fra, values, transform_type, -1, id_name(ob_arm), bone->name);
1458                 }
1459
1460                 MEM_freeN(values);
1461         }
1462
1463         // restore restpos
1464         if (flag & ARM_RESTPOS) 
1465                 arm->flag = flag;
1466         BKE_pose_where_is(scene, ob_arm);
1467 }
1468
1469 void AnimationExporter::sample_animation(float *v, std::vector<float> &frames, int type, Bone *bone, Object *ob_arm, bPoseChannel *pchan)
1470 {
1471         bPoseChannel *parchan = NULL;
1472         bPose *pose = ob_arm->pose;
1473
1474         pchan = BKE_pose_channel_find_name(pose, bone->name);
1475
1476         if (!pchan)
1477                 return;
1478
1479         parchan = pchan->parent;
1480
1481         enable_fcurves(ob_arm->adt->action, bone->name);
1482
1483         std::vector<float>::iterator it;
1484         for (it = frames.begin(); it != frames.end(); it++) {
1485                 float mat[4][4], ipar[4][4];
1486
1487                 float ctime = BKE_scene_frame_get_from_ctime(scene, *it);
1488
1489
1490                 BKE_animsys_evaluate_animdata(scene, &ob_arm->id, ob_arm->adt, ctime, ADT_RECALC_ANIM);
1491                 BKE_pose_where_is_bone(scene, ob_arm, pchan, ctime, 1);
1492
1493                 // compute bone local mat
1494                 if (bone->parent) {
1495                         invert_m4_m4(ipar, parchan->pose_mat);
1496                         mult_m4_m4m4(mat, ipar, pchan->pose_mat);
1497                 }
1498                 else
1499                         copy_m4_m4(mat, pchan->pose_mat);
1500
1501                 switch (type) {
1502                         case 0:
1503                                 mat4_to_eul(v, mat);
1504                                 break;
1505                         case 1:
1506                                 mat4_to_size(v, mat);
1507                                 break;
1508                         case 2:
1509                                 copy_v3_v3(v, mat[3]);
1510                                 break;
1511                 }
1512
1513                 v += 3;
1514         }
1515
1516         enable_fcurves(ob_arm->adt->action, NULL);
1517 }
1518
1519 bool AnimationExporter::validateConstraints(bConstraint *con)
1520 {
1521         bool valid = true;
1522         bConstraintTypeInfo *cti = BKE_constraint_get_typeinfo(con);
1523         /* these we can skip completely (invalid constraints...) */
1524         if (cti == NULL) valid = false;
1525         if (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) valid = false;
1526         /* these constraints can't be evaluated anyway */
1527         if (cti->evaluate_constraint == NULL) valid = false;
1528         /* influence == 0 should be ignored */
1529         if (con->enforce == 0.0f) valid = false;
1530
1531         return valid;
1532 }
1533
1534 void AnimationExporter::calc_ob_mat_at_time(Object *ob, float ctime , float mat[][4])
1535 {
1536         ListBase *conlist = get_active_constraints(ob);
1537         bConstraint *con;
1538         for (con = (bConstraint *)conlist->first; con; con = con->next) {
1539                 ListBase targets = {NULL, NULL};
1540                 
1541                 bConstraintTypeInfo *cti = BKE_constraint_get_typeinfo(con);
1542                 
1543                 if (cti && cti->get_constraint_targets) {
1544                         bConstraintTarget *ct;
1545                         Object *obtar;
1546                         cti->get_constraint_targets(con, &targets);
1547                         for (ct = (bConstraintTarget *)targets.first; ct; ct = ct->next) {
1548                                 obtar = ct->tar;
1549                                 BKE_animsys_evaluate_animdata(scene, &obtar->id, obtar->adt, ctime, ADT_RECALC_ANIM);
1550                                 BKE_object_where_is_calc_time(scene, obtar, ctime);
1551                         }
1552                 }
1553         }
1554         BKE_object_where_is_calc_time(scene, ob, ctime);
1555         copy_m4_m4(mat, ob->obmat);
1556 }
1557