7 #include "COLLADASWBaseInputElement.h"
8 #include "COLLADASWInstanceController.h"
9 #include "COLLADASWPrimitves.h"
10 #include "COLLADASWSource.h"
40 void ControllerExporter::write_bone_URLs(COLLADASW::InstanceController &ins,
44 if (
bc_is_root_bone(bone, this->export_settings.get_deform_bones_only())) {
50 write_bone_URLs(ins, ob_arm, child);
60 const std::string &controller_id = get_controller_id(ob_arm, ob);
62 COLLADASW::InstanceController ins(mSW);
73 write_bone_URLs(ins, ob_arm, bone);
77 ins.getBindMaterial(), ob, this->export_settings.get_active_uv_only());
85 Scene *sce = blender_context.get_scene();
90 sce, *
this, this->export_settings.get_export_set());
101 export_skin_controller(ob, ob_arm);
103 if (key && this->export_settings.get_include_shapekeys()) {
104 export_morph_controller(ob, key);
109 bool ArmatureExporter::already_written(
Object *ob_arm)
111 return std::find(written_armatures.begin(), written_armatures.end(), ob_arm) !=
112 written_armatures.end();
115 void ArmatureExporter::wrote(
Object *ob_arm)
117 written_armatures.push_back(ob_arm);
120 void ArmatureExporter::find_objects_using_armature(
Object *ob_arm,
121 std::vector<Object *> &objects,
126 Base *base = (
Base *)sce->base.first;
130 if (ob->
type ==
OB_MESH && get_assigned_armature(ob) == ob_arm) {
131 objects.push_back(ob);
139 std::string ControllerExporter::get_controller_id(
Object *ob_arm,
Object *ob)
142 SKIN_CONTROLLER_ID_SUFFIX;
145 std::string ControllerExporter::get_controller_id(
Key *key,
Object *ob)
150 void ControllerExporter::export_skin_controller(
Object *ob,
Object *ob_arm)
160 bool use_instantiation = this->export_settings.get_use_object_instantiation();
163 if (((
Mesh *)ob->
data)->dvert ==
nullptr) {
169 this->export_settings.get_export_mesh_type(),
170 this->export_settings.get_apply_modifiers(),
171 this->export_settings.get_triangulate());
173 std::string controller_name =
id_name(ob_arm);
174 std::string controller_id = get_controller_id(ob_arm, ob);
176 openSkin(controller_id,
180 add_bind_shape_mat(ob);
183 std::string joints_source_id = add_joints_source(ob_arm, defbase, controller_id);
184 std::string inv_bind_mat_source_id = add_inv_bind_mats_source(ob_arm, defbase, controller_id);
186 std::list<int> vcounts;
187 std::list<int> joints;
188 std::list<float> weights;
194 std::vector<int> joint_index_by_def_index;
198 if (is_bone_defgroup(ob_arm, def)) {
199 joint_index_by_def_index.push_back(j++);
202 joint_index_by_def_index.push_back(-1);
207 for (i = 0; i < me->
totvert; i++) {
209 std::map<int, float> jw;
216 if (idx >= joint_index_by_def_index.size()) {
222 int joint_index = joint_index_by_def_index[idx];
223 if (joint_index != -1 && vert->
dw[j].
weight > 0.0f) {
224 jw[joint_index] += vert->
dw[j].
weight;
231 float invsumw = 1.0f / sumw;
232 vcounts.push_back(jw.size());
233 for (
auto &index_and_weight : jw) {
234 joints.push_back(index_and_weight.first);
235 weights.push_back(invsumw * index_and_weight.second);
239 vcounts.push_back(0);
241 vcounts.push_back(1);
242 joints.push_back(-1);
243 weights.push_back(1.0f);
248 if (oob_counter > 0) {
250 "Ignored %d Vertex weights which use index to non existing VGroup %zu.\n",
252 joint_index_by_def_index.size());
256 std::string weights_source_id = add_weights_source(me, controller_id, weights);
257 add_joints_element(defbase, joints_source_id, inv_bind_mat_source_id);
258 add_vertex_weights_element(weights_source_id, joints_source_id, vcounts, joints);
266 void ControllerExporter::export_morph_controller(
Object *ob,
Key *key)
268 bool use_instantiation = this->export_settings.get_use_object_instantiation();
273 this->export_settings.get_export_mesh_type(),
274 this->export_settings.get_apply_modifiers(),
275 this->export_settings.get_triangulate());
277 std::string controller_name =
id_name(ob) +
"-morph";
278 std::string controller_id = get_controller_id(key, ob);
285 std::string targets_id = add_morph_targets(key, ob);
286 std::string morph_weights_id = add_morph_weights(key, ob);
288 COLLADASW::TargetsElement targets(mSW);
290 COLLADASW::InputList &
input = targets.getInputList();
292 input.push_back(COLLADASW::Input(
293 COLLADASW::InputSemantic::MORPH_TARGET,
296 COLLADASW::Input(COLLADASW::InputSemantic::MORPH_WEIGHT,
304 add_weight_extras(key);
309 std::string ControllerExporter::add_morph_targets(
Key *key,
Object *ob)
313 COLLADASW::IdRefSource source(mSW);
314 source.setId(source_id);
315 source.setArrayId(source_id + ARRAY_ID_SUFFIX);
316 source.setAccessorCount(key->
totkey - 1);
317 source.setAccessorStride(1);
319 COLLADASW::SourceBase::ParameterNameList ¶m = source.getParameterNameList();
320 param.push_back(
"IDREF");
322 source.prepareToAppendValues();
327 for (; kb; kb = kb->
next) {
329 source.appendValues(geom_id);
337 std::string ControllerExporter::add_morph_weights(
Key *key,
Object *ob)
341 COLLADASW::FloatSourceF source(mSW);
342 source.setId(source_id);
343 source.setArrayId(source_id + ARRAY_ID_SUFFIX);
344 source.setAccessorCount(key->
totkey - 1);
345 source.setAccessorStride(1);
347 COLLADASW::SourceBase::ParameterNameList ¶m = source.getParameterNameList();
348 param.push_back(
"MORPH_WEIGHT");
350 source.prepareToAppendValues();
355 for (; kb; kb = kb->
next) {
356 float weight = kb->
curval;
357 source.appendValues(weight);
364 void ControllerExporter::add_weight_extras(
Key *key)
367 COLLADASW::BaseExtraTechnique extra;
372 for (; kb; kb = kb->
next) {
375 extra.addExtraTechniqueParameter(
"KHR",
"morph_weights", 0.000,
"MORPH_WEIGHT_TO_TARGET");
379 void ControllerExporter::add_joints_element(
const ListBase *defbase,
380 const std::string &joints_source_id,
381 const std::string &inv_bind_mat_source_id)
383 COLLADASW::JointsElement joints(mSW);
384 COLLADASW::InputList &
input = joints.getInputList();
386 input.push_back(COLLADASW::Input(
387 COLLADASW::InputSemantic::JOINT,
390 COLLADASW::Input(COLLADASW::InputSemantic::BINDMATRIX,
395 void ControllerExporter::add_bind_shape_mat(
Object *ob)
397 double bind_mat[4][4];
401 if (export_settings.get_apply_global_orientation()) {
410 if (this->export_settings.get_limit_precision()) {
414 addBindShapeTransform(bind_mat);
417 std::string ControllerExporter::add_joints_source(
Object *ob_arm,
419 const std::string &controller_id)
421 std::string source_id = controller_id + JOINTS_SOURCE_ID_SUFFIX;
426 if (is_bone_defgroup(ob_arm, def)) {
431 COLLADASW::NameSource source(mSW);
432 source.setId(source_id);
433 source.setArrayId(source_id + ARRAY_ID_SUFFIX);
434 source.setAccessorCount(totjoint);
435 source.setAccessorStride(1);
437 COLLADASW::SourceBase::ParameterNameList ¶m = source.getParameterNameList();
438 param.push_back(
"JOINT");
440 source.prepareToAppendValues();
443 Bone *bone = get_bone_from_defgroup(ob_arm, def);
454 std::string ControllerExporter::add_inv_bind_mats_source(
Object *ob_arm,
456 const std::string &controller_id)
458 std::string source_id = controller_id + BIND_POSES_SOURCE_ID_SUFFIX;
462 if (is_bone_defgroup(ob_arm, def)) {
467 COLLADASW::FloatSourceF source(mSW);
468 source.setId(source_id);
469 source.setArrayId(source_id + ARRAY_ID_SUFFIX);
470 source.setAccessorCount(totjoint);
471 source.setAccessorStride(16);
473 source.setParameterTypeName(&COLLADASW::CSWC::CSW_VALUE_TYPE_FLOAT4x4);
474 COLLADASW::SourceBase::ParameterNameList ¶m = source.getParameterNameList();
475 param.push_back(
"TRANSFORM");
477 source.prepareToAppendValues();
482 int flag = arm->
flag;
494 if (is_bone_defgroup(ob_arm, def)) {
499 float inv_bind_mat[4][4];
501 float bind_mat[4][4];
510 this->export_settings, pchan->
bone, bind_mat, pchan->
bone->
arm_mat,
true);
513 if (export_settings.get_open_sim()) {
515 float rot[3] = {0, 0, 0};
528 if (export_settings.get_apply_global_orientation()) {
535 if (this->export_settings.get_limit_precision()) {
538 source.appendValues(inv_bind_mat);
558 return pchan ? pchan->
bone :
nullptr;
563 return get_bone_from_defgroup(ob_arm, def) !=
nullptr;
566 std::string ControllerExporter::add_weights_source(
Mesh *me,
567 const std::string &controller_id,
568 const std::list<float> &weights)
570 std::string source_id = controller_id + WEIGHTS_SOURCE_ID_SUFFIX;
572 COLLADASW::FloatSourceF source(mSW);
573 source.setId(source_id);
574 source.setArrayId(source_id + ARRAY_ID_SUFFIX);
575 source.setAccessorCount(weights.size());
576 source.setAccessorStride(1);
578 COLLADASW::SourceBase::ParameterNameList ¶m = source.getParameterNameList();
579 param.push_back(
"WEIGHT");
581 source.prepareToAppendValues();
583 for (
float weight : weights) {
584 source.appendValues(weight);
592 void ControllerExporter::add_vertex_weights_element(
const std::string &weights_source_id,
593 const std::string &joints_source_id,
594 const std::list<int> &vcounts,
595 const std::list<int> &joints)
597 COLLADASW::VertexWeightsElement weightselem(mSW);
598 COLLADASW::InputList &
input = weightselem.getInputList();
601 input.push_back(COLLADASW::Input(
602 COLLADASW::InputSemantic::JOINT,
606 COLLADASW::Input(COLLADASW::InputSemantic::WEIGHT,
610 weightselem.setCount(vcounts.size());
613 COLLADASW::PrimitivesBase::VCountList vcountlist;
615 vcountlist.resize(vcounts.size());
616 std::copy(vcounts.begin(), vcounts.end(), vcountlist.begin());
618 weightselem.prepareToAppendVCountValues();
619 weightselem.appendVertexCount(vcountlist);
621 weightselem.CloseVCountAndOpenVElement();
624 int weight_index = 0;
625 for (
int joint_index : joints) {
626 weightselem.appendValues(joint_index, weight_index++);
629 weightselem.finish();
Blender kernel action and pose functionality.
struct bPoseChannel * BKE_pose_channel_find_name(const struct bPose *pose, const char *name)
void BKE_pose_where_is(struct Depsgraph *depsgraph, struct Scene *scene, struct Object *ob)
struct Key * BKE_key_from_object(struct Object *ob)
void BKE_id_free(struct Main *bmain, void *idv)
void BKE_object_matrix_local_get(struct Object *ob, float r_mat[4][4])
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
bool invert_m4_m4(float R[4][4], const float A[4][4])
void loc_eulO_size_to_mat4(float R[4][4], const float loc[3], const float eul[3], const float size[3], short order)
struct Depsgraph Depsgraph
static void sanitize(Matrix &matrix, int precision)
bool add_instance_controller(Object *ob)
void operator()(Object *ob)
bool is_skinned_mesh(Object *ob)
void export_controllers()
void add_material_bindings(COLLADASW::BindMaterial &bind_material, Object *ob, bool active_uv_only)
static void mat4_to_dae(float out[4][4], float in[4][4])
static void mat4_to_dae_double(double out[4][4], float in[4][4])
std::string get_joint_sid(Bone *bone)
std::string translate_id(const char *idString)
std::string get_geometry_id(Object *ob)
std::string id_name(void *id)
bool bc_is_root_bone(Bone *aBone, bool deform_bones_only)
Mesh * bc_get_mesh_copy(BlenderContext &blender_context, Object *ob, BC_export_mesh_type export_mesh_type, bool apply_modifiers, bool triangulate)
void bc_add_global_transform(Matrix &to_mat, const Matrix &from_mat, const BCMatrix &global_transform, const bool invert)
void bc_apply_global_transform(Matrix &to_mat, const BCMatrix &global_transform, const bool invert)
void bc_create_restpose_mat(BCExportSettings &export_settings, Bone *bone, float to_mat[4][4], float from_mat[4][4], bool use_local_space)
bool bc_get_property_matrix(Bone *bone, std::string key, float mat[4][4])
Object * bc_get_assigned_armature(Object *ob)
void bc_decompose(float mat[4][4], float *loc, float eul[3], float quat[4], float *size)
constexpr int LIMITTED_PRECISION
const Depsgraph * depsgraph
ccl_gpu_kernel_postfix ccl_global float int int int int float bool int offset
ccl_global KernelShaderEvalInput * input
static void copy(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node)
void forEachMeshObjectInExportSet(Scene *sce, Functor &f, LinkNode *export_set)
struct MDeformVert * dvert