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);
127
export_morph_controller(ob, key);
132
bool ArmatureExporter::already_written(Object *ob_arm)
134
return std::find(written_armatures.begin(), written_armatures.end(), ob_arm) != written_armatures.end();
137
void ArmatureExporter::wrote(Object *ob_arm)
139
written_armatures.push_back(ob_arm);
142
void ArmatureExporter::find_objects_using_armature(Object *ob_arm, std::vector<Object *>& objects, Scene *sce)
146
Base *base = (Base *) sce->base.first;
148
Object *ob = base->object;
150
if (ob->type == OB_MESH && get_assigned_armature(ob) == ob_arm) {
151
objects.push_back(ob);
159
std::string ControllerExporter::get_joint_sid(Bone *bone, Object *ob_arm)
161
return get_joint_id(bone, ob_arm);
164
std::string ControllerExporter::get_controller_id(Object *ob_arm, Object *ob)
166
return translate_id(id_name(ob_arm)) + "_" + translate_id(id_name(ob)) + SKIN_CONTROLLER_ID_SUFFIX;
169
std::string ControllerExporter::get_controller_id(Key *key, Object *ob)
171
return translate_id(id_name(ob)) + MORPH_CONTROLLER_ID_SUFFIX;
174
// ob should be of type OB_MESH
175
// both args are required
176
void ControllerExporter::export_skin_controller(Object *ob, Object *ob_arm)
179
// joint inverse bind matrices
183
// joint names: ob -> vertex group names
184
// vertex group weights: me->dvert -> groups -> index, weight
189
typedef struct MDeformVert {
190
struct MDeformWeight *dw;
192
int flag; // flag only in use for weightpaint now
195
typedef struct MDeformWeight {
201
bool use_instantiation = this->export_settings->use_object_instantiation;
204
if (this->export_settings->apply_modifiers)
205
me = bc_to_mesh_apply_modifiers(scene, ob, this->export_settings->export_mesh_type);
207
me = (Mesh *)ob->data;
209
BKE_mesh_tessface_ensure(me);
211
if (!me->dvert) return;
213
std::string controller_name = id_name(ob_arm);
214
std::string controller_id = get_controller_id(ob_arm, ob);
216
openSkin(controller_id, controller_name,
217
COLLADABU::URI(COLLADABU::Utils::EMPTY_STRING, get_geometry_id(ob, use_instantiation)));
219
add_bind_shape_mat(ob);
221
std::string joints_source_id = add_joints_source(ob_arm, &ob->defbase, controller_id);
222
std::string inv_bind_mat_source_id = add_inv_bind_mats_source(ob_arm, &ob->defbase, controller_id);
224
std::list<int> vcounts;
225
std::list<int> joints;
226
std::list<float> weights;
231
// def group index -> joint index
232
std::vector<int> joint_index_by_def_index;
235
for (def = (bDeformGroup *)ob->defbase.first, i = 0, j = 0; def; def = def->next, i++) {
236
if (is_bone_defgroup(ob_arm, def))
237
joint_index_by_def_index.push_back(j++);
239
joint_index_by_def_index.push_back(-1);
243
for (i = 0; i < me->totvert; i++) {
244
MDeformVert *vert = &me->dvert[i];
245
std::map<int, float> jw;
247
// We're normalizing the weights later
250
for (j = 0; j < vert->totweight; j++) {
251
int idx = vert->dw[j].def_nr;
252
if (idx >= joint_index_by_def_index.size()) {
253
// XXX: Maybe better find out where and
254
// why the Out Of Bound indexes get created ?
259
int joint_index = joint_index_by_def_index[idx];
260
if (joint_index != -1 && vert->dw[j].weight > 0.0f) {
261
jw[joint_index] += vert->dw[j].weight;
262
sumw += vert->dw[j].weight;
269
float invsumw = 1.0f / sumw;
270
vcounts.push_back(jw.size());
271
for (std::map<int, float>::iterator m = jw.begin(); m != jw.end(); ++m) {
272
joints.push_back((*m).first);
273
weights.push_back(invsumw * (*m).second);
277
vcounts.push_back(0);
279
vcounts.push_back(1);
280
joints.push_back(-1);
281
weights.push_back(1.0f);
286
if (oob_counter > 0) {
287
fprintf(stderr, "Ignored %d Vertex weigths which use index to non existing VGroup.\n", oob_counter, joint_index_by_def_index.size());
291
std::string weights_source_id = add_weights_source(me, controller_id, weights);
292
add_joints_element(&ob->defbase, joints_source_id, inv_bind_mat_source_id);
293
add_vertex_weights_element(weights_source_id, joints_source_id, vcounts, joints);
295
if (this->export_settings->apply_modifiers)
297
BKE_libblock_free_us(&(G.main->mesh), me);
303
void ControllerExporter::export_morph_controller(Object *ob, Key *key)
305
bool use_instantiation = this->export_settings->use_object_instantiation;
308
if (this->export_settings->apply_modifiers) {
309
me = bc_to_mesh_apply_modifiers(scene, ob, this->export_settings->export_mesh_type);
312
me = (Mesh *)ob->data;
314
BKE_mesh_tessface_ensure(me);
316
std::string controller_name = id_name(ob) + "-morph";
317
std::string controller_id = get_controller_id(key, ob);
319
openMorph(controller_id, controller_name,
320
COLLADABU::URI(COLLADABU::Utils::EMPTY_STRING, get_geometry_id(ob, use_instantiation)));
322
std::string targets_id = add_morph_targets(key, ob);
323
std::string morph_weights_id = add_morph_weights(key, ob);
325
COLLADASW::TargetsElement targets(mSW);
327
COLLADASW::InputList &input = targets.getInputList();
329
input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::MORPH_TARGET, // constant declared in COLLADASWInputList.h
330
COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, targets_id)));
331
input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::MORPH_WEIGHT,
332
COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, morph_weights_id)));
335
if (this->export_settings->apply_modifiers)
337
BKE_libblock_free_us(&(G.main->mesh), me);
340
//support for animations
341
//can also try the base element and param alternative
342
add_weight_extras(key);
347
std::string ControllerExporter::add_morph_targets(Key *key, Object *ob)
349
std::string source_id = translate_id(id_name(ob)) + TARGETS_SOURCE_ID_SUFFIX;
351
COLLADASW::IdRefSource source(mSW);
352
source.setId(source_id);
353
source.setArrayId(source_id + ARRAY_ID_SUFFIX);
354
source.setAccessorCount(key->totkey - 1);
355
source.setAccessorStride(1);
357
COLLADASW::SourceBase::ParameterNameList ¶m = source.getParameterNameList();
358
param.push_back("IDREF");
360
source.prepareToAppendValues();
362
KeyBlock * kb = (KeyBlock*)key->block.first;
365
for (; kb; kb = kb->next) {
366
std::string geom_id = get_geometry_id(ob, false) + "_morph_" + translate_id(kb->name);
367
source.appendValues(geom_id);
376
std::string ControllerExporter::add_morph_weights(Key *key, Object *ob)
378
std::string source_id = translate_id(id_name(ob)) + WEIGHTS_SOURCE_ID_SUFFIX;
380
COLLADASW::FloatSourceF source(mSW);
381
source.setId(source_id);
382
source.setArrayId(source_id + ARRAY_ID_SUFFIX);
383
source.setAccessorCount(key->totkey - 1);
384
source.setAccessorStride(1);
386
COLLADASW::SourceBase::ParameterNameList ¶m = source.getParameterNameList();
387
param.push_back("MORPH_WEIGHT");
389
source.prepareToAppendValues();
391
KeyBlock * kb = (KeyBlock*)key->block.first;
394
for (; kb; kb = kb->next) {
395
float weight = kb->curval;
396
source.appendValues(weight);
403
//Added to implemente support for animations.
404
void ControllerExporter::add_weight_extras(Key *key)
406
// can also try the base element and param alternative
407
COLLADASW::BaseExtraTechnique extra;
409
KeyBlock * kb = (KeyBlock*)key->block.first;
412
for (; kb; kb = kb->next) {
413
// XXX why is the weight not used here and set to 0.0?
414
float weight = kb->curval;
415
extra.addExtraTechniqueParameter ("KHR", "morph_weights" , 0.000, "MORPH_WEIGHT_TO_TARGET");
421
void ControllerExporter::add_joints_element(ListBase *defbase,
422
const std::string& joints_source_id, const std::string& inv_bind_mat_source_id)
424
COLLADASW::JointsElement joints(mSW);
425
COLLADASW::InputList &input = joints.getInputList();
427
input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::JOINT, // constant declared in COLLADASWInputList.h
428
COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, joints_source_id)));
429
input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::BINDMATRIX,
430
COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, inv_bind_mat_source_id)));
434
void ControllerExporter::add_bind_shape_mat(Object *ob)
436
double bind_mat[4][4];
438
converter.mat4_to_dae_double(bind_mat, ob->obmat);
440
addBindShapeTransform(bind_mat);
443
std::string ControllerExporter::add_joints_source(Object *ob_arm, ListBase *defbase, const std::string& controller_id)
445
std::string source_id = controller_id + JOINTS_SOURCE_ID_SUFFIX;
449
for (def = (bDeformGroup *)defbase->first; def; def = def->next) {
450
if (is_bone_defgroup(ob_arm, def))
454
COLLADASW::NameSource source(mSW);
455
source.setId(source_id);
456
source.setArrayId(source_id + ARRAY_ID_SUFFIX);
457
source.setAccessorCount(totjoint);
458
source.setAccessorStride(1);
460
COLLADASW::SourceBase::ParameterNameList ¶m = source.getParameterNameList();
461
param.push_back("JOINT");
463
source.prepareToAppendValues();
465
for (def = (bDeformGroup *)defbase->first; def; def = def->next) {
466
Bone *bone = get_bone_from_defgroup(ob_arm, def);
468
source.appendValues(get_joint_sid(bone, ob_arm));
476
std::string ControllerExporter::add_inv_bind_mats_source(Object *ob_arm, ListBase *defbase, const std::string& controller_id)
478
std::string source_id = controller_id + BIND_POSES_SOURCE_ID_SUFFIX;
481
for (bDeformGroup *def = (bDeformGroup *)defbase->first; def; def = def->next) {
482
if (is_bone_defgroup(ob_arm, def))
486
COLLADASW::FloatSourceF source(mSW);
487
source.setId(source_id);
488
source.setArrayId(source_id + ARRAY_ID_SUFFIX);
489
source.setAccessorCount(totjoint); //BLI_countlist(defbase));
490
source.setAccessorStride(16);
492
source.setParameterTypeName(&COLLADASW::CSWC::CSW_VALUE_TYPE_FLOAT4x4);
493
COLLADASW::SourceBase::ParameterNameList ¶m = source.getParameterNameList();
494
param.push_back("TRANSFORM");
496
source.prepareToAppendValues();
498
bPose *pose = ob_arm->pose;
499
bArmature *arm = (bArmature *)ob_arm->data;
501
int flag = arm->flag;
503
// put armature in rest position
504
if (!(arm->flag & ARM_RESTPOS)) {
505
arm->flag |= ARM_RESTPOS;
506
BKE_pose_where_is(scene, ob_arm);
509
for (bDeformGroup *def = (bDeformGroup *)defbase->first; def; def = def->next) {
510
if (is_bone_defgroup(ob_arm, def)) {
511
bPoseChannel *pchan = BKE_pose_channel_find_name(pose, def->name);
515
float inv_bind_mat[4][4];
517
// SECOND_LIFE_COMPATIBILITY
518
if (export_settings->second_life) {
519
// Only translations, no rotation vs armature
522
copy_v3_v3(temp[3], pchan->bone->arm_mat[3]);
523
mult_m4_m4m4(world, ob_arm->obmat, temp);
526
// make world-space matrix, arm_mat is armature-space
527
mult_m4_m4m4(world, ob_arm->obmat, pchan->bone->arm_mat);
530
invert_m4_m4(mat, world);
531
converter.mat4_to_dae(inv_bind_mat, mat);
533
source.appendValues(inv_bind_mat);
537
// back from rest positon
538
if (!(flag & ARM_RESTPOS)) {
540
BKE_pose_where_is(scene, ob_arm);
548
Bone *ControllerExporter::get_bone_from_defgroup(Object *ob_arm, bDeformGroup *def)
550
bPoseChannel *pchan = BKE_pose_channel_find_name(ob_arm->pose, def->name);
551
return pchan ? pchan->bone : NULL;
554
bool ControllerExporter::is_bone_defgroup(Object *ob_arm, bDeformGroup *def)
556
return get_bone_from_defgroup(ob_arm, def) != NULL;
559
std::string ControllerExporter::add_weights_source(Mesh *me, const std::string& controller_id, const std::list<float>& weights)
561
std::string source_id = controller_id + WEIGHTS_SOURCE_ID_SUFFIX;
563
COLLADASW::FloatSourceF source(mSW);
564
source.setId(source_id);
565
source.setArrayId(source_id + ARRAY_ID_SUFFIX);
566
source.setAccessorCount(weights.size());
567
source.setAccessorStride(1);
569
COLLADASW::SourceBase::ParameterNameList ¶m = source.getParameterNameList();
570
param.push_back("WEIGHT");
572
source.prepareToAppendValues();
574
for (std::list<float>::const_iterator i = weights.begin(); i != weights.end(); ++i) {
575
source.appendValues(*i);
583
void ControllerExporter::add_vertex_weights_element(const std::string& weights_source_id, const std::string& joints_source_id,
584
const std::list<int>& vcounts,
585
const std::list<int>& joints)
587
COLLADASW::VertexWeightsElement weightselem(mSW);
588
COLLADASW::InputList &input = weightselem.getInputList();
591
input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::JOINT, // constant declared in COLLADASWInputList.h
592
COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, joints_source_id), offset++));
593
input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::WEIGHT,
594
COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, weights_source_id), offset++));
596
weightselem.setCount(vcounts.size());
598
// write number of deformers per vertex
599
COLLADASW::PrimitivesBase::VCountList vcountlist;
601
vcountlist.resize(vcounts.size());
602
std::copy(vcounts.begin(), vcounts.end(), vcountlist.begin());
604
weightselem.prepareToAppendVCountValues();
605
weightselem.appendVertexCount(vcountlist);
607
weightselem.CloseVCountAndOpenVElement();
609
// write deformer index - weight index pairs
610
int weight_index = 0;
611
for (std::list<int>::const_iterator i = joints.begin(); i != joints.end(); ++i) {
612
weightselem.appendValues(*i, weight_index++);
615
weightselem.finish();