2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed,
19 * Nathan Letwory, Sukhitha Jayathilake
21 * ***** END GPL LICENSE BLOCK *****
24 /** \file blender/collada/ControllerExporter.cpp
28 #include "COLLADASWBaseInputElement.h"
29 #include "COLLADASWInstanceController.h"
30 #include "COLLADASWPrimitves.h"
31 #include "COLLADASWSource.h"
33 #include "DNA_action_types.h"
34 #include "DNA_meshdata_types.h"
35 #include "DNA_modifier_types.h"
37 #include "BKE_action.h"
38 #include "BKE_armature.h"
43 #include "BKE_global.h"
44 #include "BKE_library.h"
47 #include "ED_armature.h"
49 #include "BLI_listbase.h"
51 #include "GeometryExporter.h"
52 #include "ArmatureExporter.h"
53 #include "ControllerExporter.h"
54 #include "SceneExporter.h"
56 #include "collada_utils.h"
58 // XXX exporter writes wrong data for shared armatures. A separate
59 // controller should be written for each armature-mesh binding how do
60 // we make controller ids then?
61 ControllerExporter::ControllerExporter(COLLADASW::StreamWriter *sw, const ExportSettings *export_settings) : COLLADASW::LibraryControllers(sw), export_settings(export_settings) {
64 bool ControllerExporter::is_skinned_mesh(Object *ob)
66 return bc_get_assigned_armature(ob) != NULL;
70 void ControllerExporter::write_bone_URLs(COLLADASW::InstanceController &ins, Object *ob_arm, Bone *bone)
72 if (bc_is_root_bone(bone, this->export_settings->deform_bones_only))
73 ins.addSkeleton(COLLADABU::URI(COLLADABU::Utils::EMPTY_STRING, get_joint_id(bone, ob_arm)));
75 for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
76 write_bone_URLs(ins, ob_arm, child);
81 bool ControllerExporter::add_instance_controller(Object *ob)
83 Object *ob_arm = bc_get_assigned_armature(ob);
84 bArmature *arm = (bArmature *)ob_arm->data;
86 const std::string& controller_id = get_controller_id(ob_arm, ob);
88 COLLADASW::InstanceController ins(mSW);
89 ins.setUrl(COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, controller_id));
91 Mesh *me = (Mesh *)ob->data;
92 if (!me->dvert) return false;
94 // write root bone URLs
96 for (bone = (Bone *)arm->bonebase.first; bone; bone = bone->next) {
97 write_bone_URLs(ins, ob_arm, bone);
100 InstanceWriter::add_material_bindings(ins.getBindMaterial(), ob, this->export_settings->active_uv_only);
106 void ControllerExporter::export_controllers(Scene *sce)
113 gf.forEachMeshObjectInExportSet<ControllerExporter>(sce, *this, this->export_settings->export_set);
118 void ControllerExporter::operator()(Object *ob)
120 Object *ob_arm = bc_get_assigned_armature(ob);
121 Key *key = BKE_key_from_object(ob);
124 export_skin_controller(ob, ob_arm);
126 export_morph_controller(ob, key);
131 bool ArmatureExporter::already_written(Object *ob_arm)
133 return std::find(written_armatures.begin(), written_armatures.end(), ob_arm) != written_armatures.end();
136 void ArmatureExporter::wrote(Object *ob_arm)
138 written_armatures.push_back(ob_arm);
141 void ArmatureExporter::find_objects_using_armature(Object *ob_arm, std::vector<Object *>& objects, Scene *sce)
145 Base *base = (Base *) sce->base.first;
147 Object *ob = base->object;
149 if (ob->type == OB_MESH && get_assigned_armature(ob) == ob_arm) {
150 objects.push_back(ob);
158 std::string ControllerExporter::get_joint_sid(Bone *bone, Object *ob_arm)
160 return get_joint_id(bone, ob_arm);
163 std::string ControllerExporter::get_controller_id(Object *ob_arm, Object *ob)
165 return translate_id(id_name(ob_arm)) + "_" + translate_id(id_name(ob)) + SKIN_CONTROLLER_ID_SUFFIX;
168 std::string ControllerExporter::get_controller_id(Key *key, Object *ob)
170 return translate_id(id_name(ob)) + MORPH_CONTROLLER_ID_SUFFIX;
173 // ob should be of type OB_MESH
174 // both args are required
175 void ControllerExporter::export_skin_controller(Object *ob, Object *ob_arm)
178 // joint inverse bind matrices
182 // joint names: ob -> vertex group names
183 // vertex group weights: me->dvert -> groups -> index, weight
188 typedef struct MDeformVert {
189 struct MDeformWeight *dw;
191 int flag; // flag only in use for weightpaint now
194 typedef struct MDeformWeight {
200 bool use_instantiation = this->export_settings->use_object_instantiation;
203 if (this->export_settings->apply_modifiers)
204 me = bc_to_mesh_apply_modifiers(scene, ob, this->export_settings->export_mesh_type);
206 me = (Mesh *)ob->data;
208 BKE_mesh_tessface_ensure(me);
210 if (!me->dvert) return;
212 std::string controller_name = id_name(ob_arm);
213 std::string controller_id = get_controller_id(ob_arm, ob);
215 openSkin(controller_id, controller_name,
216 COLLADABU::URI(COLLADABU::Utils::EMPTY_STRING, get_geometry_id(ob, use_instantiation)));
218 add_bind_shape_mat(ob);
220 std::string joints_source_id = add_joints_source(ob_arm, &ob->defbase, controller_id);
221 std::string inv_bind_mat_source_id = add_inv_bind_mats_source(ob_arm, &ob->defbase, controller_id);
223 std::list<int> vcounts;
224 std::list<int> joints;
225 std::list<float> weights;
230 // def group index -> joint index
231 std::vector<int> joint_index_by_def_index;
234 for (def = (bDeformGroup *)ob->defbase.first, i = 0, j = 0; def; def = def->next, i++) {
235 if (is_bone_defgroup(ob_arm, def))
236 joint_index_by_def_index.push_back(j++);
238 joint_index_by_def_index.push_back(-1);
241 for (i = 0; i < me->totvert; i++) {
242 MDeformVert *vert = &me->dvert[i];
243 std::map<int, float> jw;
245 // We're normalizing the weights later
248 for (j = 0; j < vert->totweight; j++) {
249 int idx = vert->dw[j].def_nr;
251 int joint_index = joint_index_by_def_index[idx];
252 if (joint_index != -1 && vert->dw[j].weight > 0.0f) {
253 jw[joint_index] += vert->dw[j].weight;
254 sumw += vert->dw[j].weight;
260 float invsumw = 1.0f / sumw;
261 vcounts.push_back(jw.size());
262 for (std::map<int, float>::iterator m = jw.begin(); m != jw.end(); ++m) {
263 joints.push_back((*m).first);
264 weights.push_back(invsumw * (*m).second);
268 vcounts.push_back(0);
270 vcounts.push_back(1);
271 joints.push_back(-1);
272 weights.push_back(1.0f);
278 std::string weights_source_id = add_weights_source(me, controller_id, weights);
279 add_joints_element(&ob->defbase, joints_source_id, inv_bind_mat_source_id);
280 add_vertex_weights_element(weights_source_id, joints_source_id, vcounts, joints);
282 if (this->export_settings->apply_modifiers)
284 BKE_libblock_free_us(&(G.main->mesh), me);
290 void ControllerExporter::export_morph_controller(Object *ob, Key *key)
292 bool use_instantiation = this->export_settings->use_object_instantiation;
295 if (this->export_settings->apply_modifiers) {
296 me = bc_to_mesh_apply_modifiers(scene, ob, this->export_settings->export_mesh_type);
299 me = (Mesh *)ob->data;
301 BKE_mesh_tessface_ensure(me);
303 std::string controller_name = id_name(ob) + "-morph";
304 std::string controller_id = get_controller_id(key, ob);
306 openMorph(controller_id, controller_name,
307 COLLADABU::URI(COLLADABU::Utils::EMPTY_STRING, get_geometry_id(ob, use_instantiation)));
309 std::string targets_id = add_morph_targets(key, ob);
310 std::string morph_weights_id = add_morph_weights(key, ob);
312 COLLADASW::TargetsElement targets(mSW);
314 COLLADASW::InputList &input = targets.getInputList();
316 input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::MORPH_TARGET, // constant declared in COLLADASWInputList.h
317 COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, targets_id)));
318 input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::MORPH_WEIGHT,
319 COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, morph_weights_id)));
322 if (this->export_settings->apply_modifiers)
324 BKE_libblock_free_us(&(G.main->mesh), me);
327 //support for animations
328 //can also try the base element and param alternative
329 add_weight_extras(key);
334 std::string ControllerExporter::add_morph_targets(Key *key, Object *ob)
336 std::string source_id = translate_id(id_name(ob)) + TARGETS_SOURCE_ID_SUFFIX;
338 COLLADASW::IdRefSource source(mSW);
339 source.setId(source_id);
340 source.setArrayId(source_id + ARRAY_ID_SUFFIX);
341 source.setAccessorCount(key->totkey - 1);
342 source.setAccessorStride(1);
344 COLLADASW::SourceBase::ParameterNameList ¶m = source.getParameterNameList();
345 param.push_back("IDREF");
347 source.prepareToAppendValues();
349 KeyBlock * kb = (KeyBlock*)key->block.first;
352 for (; kb; kb = kb->next) {
353 std::string geom_id = get_geometry_id(ob, false) + "_morph_" + translate_id(kb->name);
354 source.appendValues(geom_id);
363 std::string ControllerExporter::add_morph_weights(Key *key, Object *ob)
365 std::string source_id = translate_id(id_name(ob)) + WEIGHTS_SOURCE_ID_SUFFIX;
367 COLLADASW::FloatSourceF source(mSW);
368 source.setId(source_id);
369 source.setArrayId(source_id + ARRAY_ID_SUFFIX);
370 source.setAccessorCount(key->totkey - 1);
371 source.setAccessorStride(1);
373 COLLADASW::SourceBase::ParameterNameList ¶m = source.getParameterNameList();
374 param.push_back("MORPH_WEIGHT");
376 source.prepareToAppendValues();
378 KeyBlock * kb = (KeyBlock*)key->block.first;
381 for (; kb; kb = kb->next) {
382 float weight = kb->curval;
383 source.appendValues(weight);
390 //Added to implemente support for animations.
391 void ControllerExporter::add_weight_extras(Key *key){
392 // can also try the base element and param alternative
393 COLLADASW::BaseExtraTechnique extra;
395 KeyBlock * kb = (KeyBlock*)key->block.first;
398 for (; kb; kb = kb->next) {
399 // XXX why is the weight not used here and set to 0.0?
400 float weight = kb->curval;
401 extra.addExtraTechniqueParameter ("KHR", "morph_weights" , 0.000, "MORPH_WEIGHT_TO_TARGET");
407 void ControllerExporter::add_joints_element(ListBase *defbase,
408 const std::string& joints_source_id, const std::string& inv_bind_mat_source_id)
410 COLLADASW::JointsElement joints(mSW);
411 COLLADASW::InputList &input = joints.getInputList();
413 input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::JOINT, // constant declared in COLLADASWInputList.h
414 COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, joints_source_id)));
415 input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::BINDMATRIX,
416 COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, inv_bind_mat_source_id)));
420 void ControllerExporter::add_bind_shape_mat(Object *ob)
422 double bind_mat[4][4];
424 converter.mat4_to_dae_double(bind_mat, ob->obmat);
426 addBindShapeTransform(bind_mat);
429 std::string ControllerExporter::add_joints_source(Object *ob_arm, ListBase *defbase, const std::string& controller_id)
431 std::string source_id = controller_id + JOINTS_SOURCE_ID_SUFFIX;
435 for (def = (bDeformGroup *)defbase->first; def; def = def->next) {
436 if (is_bone_defgroup(ob_arm, def))
440 COLLADASW::NameSource source(mSW);
441 source.setId(source_id);
442 source.setArrayId(source_id + ARRAY_ID_SUFFIX);
443 source.setAccessorCount(totjoint);
444 source.setAccessorStride(1);
446 COLLADASW::SourceBase::ParameterNameList ¶m = source.getParameterNameList();
447 param.push_back("JOINT");
449 source.prepareToAppendValues();
451 for (def = (bDeformGroup *)defbase->first; def; def = def->next) {
452 Bone *bone = get_bone_from_defgroup(ob_arm, def);
454 source.appendValues(get_joint_sid(bone, ob_arm));
462 std::string ControllerExporter::add_inv_bind_mats_source(Object *ob_arm, ListBase *defbase, const std::string& controller_id)
464 std::string source_id = controller_id + BIND_POSES_SOURCE_ID_SUFFIX;
467 for (bDeformGroup *def = (bDeformGroup *)defbase->first; def; def = def->next) {
468 if (is_bone_defgroup(ob_arm, def))
472 COLLADASW::FloatSourceF source(mSW);
473 source.setId(source_id);
474 source.setArrayId(source_id + ARRAY_ID_SUFFIX);
475 source.setAccessorCount(totjoint); //BLI_countlist(defbase));
476 source.setAccessorStride(16);
478 source.setParameterTypeName(&COLLADASW::CSWC::CSW_VALUE_TYPE_FLOAT4x4);
479 COLLADASW::SourceBase::ParameterNameList ¶m = source.getParameterNameList();
480 param.push_back("TRANSFORM");
482 source.prepareToAppendValues();
484 bPose *pose = ob_arm->pose;
485 bArmature *arm = (bArmature *)ob_arm->data;
487 int flag = arm->flag;
489 // put armature in rest position
490 if (!(arm->flag & ARM_RESTPOS)) {
491 arm->flag |= ARM_RESTPOS;
492 BKE_pose_where_is(scene, ob_arm);
495 for (bDeformGroup *def = (bDeformGroup *)defbase->first; def; def = def->next) {
496 if (is_bone_defgroup(ob_arm, def)) {
497 bPoseChannel *pchan = BKE_pose_channel_find_name(pose, def->name);
501 float inv_bind_mat[4][4];
503 // SECOND_LIFE_COMPATIBILITY
504 if (export_settings->second_life) {
505 // Only translations, no rotation vs armature
508 copy_v3_v3(temp[3], pchan->bone->arm_mat[3]);
509 mult_m4_m4m4(world, ob_arm->obmat, temp);
512 // make world-space matrix, arm_mat is armature-space
513 mult_m4_m4m4(world, ob_arm->obmat, pchan->bone->arm_mat);
516 invert_m4_m4(mat, world);
517 converter.mat4_to_dae(inv_bind_mat, mat);
519 source.appendValues(inv_bind_mat);
523 // back from rest positon
524 if (!(flag & ARM_RESTPOS)) {
526 BKE_pose_where_is(scene, ob_arm);
534 Bone *ControllerExporter::get_bone_from_defgroup(Object *ob_arm, bDeformGroup *def)
536 bPoseChannel *pchan = BKE_pose_channel_find_name(ob_arm->pose, def->name);
537 return pchan ? pchan->bone : NULL;
540 bool ControllerExporter::is_bone_defgroup(Object *ob_arm, bDeformGroup *def)
542 return get_bone_from_defgroup(ob_arm, def) != NULL;
545 std::string ControllerExporter::add_weights_source(Mesh *me, const std::string& controller_id, const std::list<float>& weights)
547 std::string source_id = controller_id + WEIGHTS_SOURCE_ID_SUFFIX;
549 COLLADASW::FloatSourceF source(mSW);
550 source.setId(source_id);
551 source.setArrayId(source_id + ARRAY_ID_SUFFIX);
552 source.setAccessorCount(weights.size());
553 source.setAccessorStride(1);
555 COLLADASW::SourceBase::ParameterNameList ¶m = source.getParameterNameList();
556 param.push_back("WEIGHT");
558 source.prepareToAppendValues();
560 for (std::list<float>::const_iterator i = weights.begin(); i != weights.end(); ++i) {
561 source.appendValues(*i);
569 void ControllerExporter::add_vertex_weights_element(const std::string& weights_source_id, const std::string& joints_source_id,
570 const std::list<int>& vcounts,
571 const std::list<int>& joints)
573 COLLADASW::VertexWeightsElement weightselem(mSW);
574 COLLADASW::InputList &input = weightselem.getInputList();
577 input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::JOINT, // constant declared in COLLADASWInputList.h
578 COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, joints_source_id), offset++));
579 input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::WEIGHT,
580 COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, weights_source_id), offset++));
582 weightselem.setCount(vcounts.size());
584 // write number of deformers per vertex
585 COLLADASW::PrimitivesBase::VCountList vcountlist;
587 vcountlist.resize(vcounts.size());
588 std::copy(vcounts.begin(), vcounts.end(), vcountlist.begin());
590 weightselem.prepareToAppendVCountValues();
591 weightselem.appendVertexCount(vcountlist);
593 weightselem.CloseVCountAndOpenVElement();
595 // write deformer index - weight index pairs
596 int weight_index = 0;
597 for (std::list<int>::const_iterator i = joints.begin(); i != joints.end(); ++i) {
598 weightselem.appendValues(*i, weight_index++);
601 weightselem.finish();