10#include "COLLADABUPlatform.h"
14#include "COLLADAFWUniqueId.h"
34 const std::string &
id = node->getName();
35 return id.empty() ? node->getOriginalId().c_str() :
id.c_str();
47 view_layer(view_layer),
49 import_settings(import_settings),
58 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it;
59 for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) {
65JointData *ArmatureImporter::get_joint_data(COLLADAFW::Node *node);
67 const COLLADAFW::UniqueId &joint_id = node->getUniqueId();
69 if (joint_id_to_joint_index_map.find(joint_id) == joint_id_to_joint_index_map.end()) {
71 stderr,
"Cannot find a joint index by joint id for %s.\n", node->getOriginalId().c_str());
75 int joint_index = joint_id_to_joint_index_map[joint_id];
77 return &joint_index_to_joint_info_map[joint_index];
81int ArmatureImporter::create_bone(
SkinInfo *skin,
82 COLLADAFW::Node *node,
85 float parent_mat[4][4],
87 std::vector<std::string> &layer_labels)
90 float joint_inv_bind_mat[4][4];
91 float joint_bind_mat[4][4];
95 std::vector<COLLADAFW::Node *>::iterator it;
96 it = std::find(finished_joints.begin(), finished_joints.end(), node);
97 if (it != finished_joints.end()) {
108 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator skin_it;
109 bool bone_is_skinned =
false;
110 for (skin_it = skin_by_data_uid.begin(); skin_it != skin_by_data_uid.end(); skin_it++) {
112 SkinInfo *
b = &skin_it->second;
113 if (
b->get_joint_inv_bind_matrix(joint_inv_bind_mat, node)) {
126 bone_is_skinned =
true;
132 if (!bone_is_skinned) {
140 float loc[3],
size[3],
rot[3][3];
141 BoneExtensionMap &extended_bones = bone_extension_manager.getExtensionMap(arm);
142 BoneExtended &be = add_bone_extended(bone, node, totchild, layer_labels, extended_bones);
154 switch (use_connect) {
160 bone->
flag &= ~BONE_CONNECTED;
175 if (bone_is_skinned && this->import_settings->keep_bind_info) {
176 float rest_mat[4][4];
177 get_node_mat(rest_mat, node,
nullptr,
nullptr,
nullptr);
187 if (use_connect == 1) {
194 leaf_bone_length =
length;
198 COLLADAFW::NodePointerArray &children = node->getChildNodes();
200 for (
uint i = 0; i < children.getCount(); i++) {
201 int cl = create_bone(skin, children[i], bone, children.getCount(), mat, arm, layer_labels);
202 if (cl > chain_length) {
208 joint_by_uid[node->getUniqueId()] = node;
209 finished_joints.push_back(node);
213 return chain_length + 1;
216void ArmatureImporter::fix_leaf_bone_hierarchy(
bArmature *armature,
218 bool fix_orientation)
220 if (bone ==
nullptr) {
225 BoneExtensionMap &extended_bones = bone_extension_manager.getExtensionMap(armature);
226 BoneExtended *be = extended_bones[bone->
name];
228 fix_leaf_bone(armature, ebone, be, fix_orientation);
232 fix_leaf_bone_hierarchy(armature, child, fix_orientation);
236void ArmatureImporter::fix_leaf_bone(
bArmature *armature,
239 bool fix_orientation)
241 if (be ==
nullptr || !be->
has_tail()) {
244 float leaf_length = (leaf_bone_length ==
FLT_MAX) ? 1.0 : leaf_bone_length;
248 if (fix_orientation && ebone->
parent !=
nullptr) {
249 EditBone *parent = ebone->
parent;
266void ArmatureImporter::fix_parent_connect(
bArmature *armature,
Bone *bone)
269 if (bone ==
nullptr) {
278 fix_parent_connect(armature, child);
282void ArmatureImporter::connect_bone_chains(
bArmature *armature,
284 int max_chain_length)
286 BoneExtensionMap &extended_bones = bone_extension_manager.getExtensionMap(armature);
287 BoneExtended *dominant_child =
nullptr;
290 if (parentbone ==
nullptr) {
295 if (child && (import_settings->find_chains || child->next ==
nullptr)) {
296 for (; child; child = child->next) {
297 BoneExtended *be = extended_bones[child->name];
300 if (chain_len <= max_chain_length) {
301 if (chain_len > maxlen) {
305 else if (chain_len == maxlen) {
306 dominant_child =
nullptr;
313 BoneExtended *pbe = extended_bones[parentbone->
name];
314 if (dominant_child !=
nullptr) {
332 if (pbe && pbe->
get_chain_length() >= this->import_settings->min_chain_length) {
334 BoneExtended *cbe = extended_bones[cebone->
name];
339 printf(
"Connect Bone chain: parent (%s --> %s) child)\n", pebone->
name, cebone->
name);
347 else if (maxlen > 1 && maxlen > this->import_settings->min_chain_length) {
349 ArmatureImporter::connect_bone_chains(armature, parentbone, maxlen - 1);
363void ArmatureImporter::set_leaf_bone_shapes(
Object *ob_arm)
367 std::vector<LeafBone>::iterator it;
368 for (it = leaf_bones.begin(); it != leaf_bones.end(); it++) {
369 LeafBone &leaf = *it;
373 pchan->
custom = get_empty_for_leaves();
376 fprintf(stderr,
"Cannot find a pose channel for leaf bone %s\n", leaf.name);
381void ArmatureImporter::set_euler_rotmode()
385 std::map<COLLADAFW::UniqueId, COLLADAFW::Node *>::iterator it;
387 for (it = joint_by_uid.begin(); it != joint_by_uid.end(); it++) {
389 COLLADAFW::Node *joint = it->second;
391 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator sit;
393 for (sit = skin_by_data_uid.begin(); sit != skin_by_data_uid.end(); sit++) {
394 SkinInfo &skin = sit->second;
403 fprintf(stderr,
"Cannot find pose channel for %s.\n", get_joint_name(joint));
413Object *ArmatureImporter::get_empty_for_leaves()
426Object *ArmatureImporter::find_armature(COLLADAFW::Node *node)
428 JointData *jd = get_joint_data(node);
433 COLLADAFW::NodePointerArray &children = node->getChildNodes();
434 for (
int i = 0; i < children.getCount(); i++) {
435 Object *ob_arm = find_armature(children[i]);
444ArmatureJoints &ArmatureImporter::get_armature_joints(
Object *ob_arm)
447 std::vector<ArmatureJoints>::iterator it;
448 for (it = armature_joints.begin(); it != armature_joints.end(); it++) {
449 if ((*it).ob_arm == ob_arm) {
457 armature_joints.push_back(aj);
459 return armature_joints.back();
462void ArmatureImporter::create_armature_bones(
Main *bmain, std::vector<Object *> &arm_objs)
464 std::vector<COLLADAFW::Node *>::iterator ri;
465 std::vector<std::string> layer_labels;
468 for (ri = root_joints.begin(); ri != root_joints.end(); ri++) {
469 COLLADAFW::Node *node = *ri;
474 Object *ob_arm = joint_parent_map[node->getUniqueId()];
494 "Reuse of child bone [%s] as root bone in same Armature is not supported.\n",
502 nullptr, node,
nullptr, node->getChildNodes().getCount(),
nullptr, armature, layer_labels);
503 if (this->import_settings->find_chains) {
512 fix_leaf_bone_hierarchy(
513 armature, (
Bone *)armature->
bonebase.
first, this->import_settings->fix_orientation);
514 unskinned_armature_map[node->getUniqueId()] = ob_arm;
519 set_bone_transformation_type(node, ob_arm);
521 int index = std::find(arm_objs.begin(), arm_objs.end(), ob_arm) - arm_objs.begin();
523 arm_objs.push_back(ob_arm);
575 std::vector<COLLADAFW::Node *> skin_root_joints;
576 std::vector<std::string> layer_labels;
578 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it;
579 for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) {
580 SkinInfo *
b = &it->second;
581 if (
b == a ||
b->BKE_armature_from_object() ==
nullptr) {
585 skin_root_joints.clear();
587 b->find_root_joints(root_joints, joint_by_uid, skin_root_joints);
589 std::vector<COLLADAFW::Node *>::iterator ri;
590 for (ri = skin_root_joints.begin(); ri != skin_root_joints.end(); ri++) {
591 COLLADAFW::Node *node = *ri;
593 shared =
b->BKE_armature_from_object();
598 if (shared !=
nullptr) {
603 if (!shared && !this->joint_parent_map.empty()) {
611 shared = this->joint_parent_map.begin()->second;
633 std::vector<COLLADAFW::Node *>::iterator ri;
634 for (ri = root_joints.begin(); ri != root_joints.end(); ri++) {
635 COLLADAFW::Node *node = *ri;
637 if (shared && std::find(skin_root_joints.begin(), skin_root_joints.end(), node) !=
638 skin_root_joints.end())
647 &skin, node,
nullptr, node->getChildNodes().getCount(),
nullptr, armature, layer_labels);
649 if (joint_parent_map.find(node->getUniqueId()) != joint_parent_map.end() &&
652 skin.
set_parent(joint_parent_map[node->getUniqueId()]);
661 for (ri = root_joints.begin(); ri != root_joints.end(); ri++) {
662 COLLADAFW::Node *node = *ri;
663 set_bone_transformation_type(node, ob_arm);
667 if (this->import_settings->find_chains) {
670 fix_leaf_bone_hierarchy(
671 armature, (
Bone *)armature->
bonebase.
first, this->import_settings->fix_orientation);
680void ArmatureImporter::set_bone_transformation_type(
const COLLADAFW::Node *node,
Object *ob_arm)
687 COLLADAFW::NodePointerArray childnodes = node->getChildNodes();
688 for (
int index = 0;
index < childnodes.getCount();
index++) {
689 node = childnodes[
index];
690 set_bone_transformation_type(node, ob_arm);
694void ArmatureImporter::set_pose(
Object *ob_arm,
695 COLLADAFW::Node *root_node,
696 const char *parentname,
697 float parent_mat[4][4])
705 bool is_decomposed = node_is_decomposed(root_node);
723 float invObmat[4][4];
724 invert_m4_m4(invObmat, ob_arm->object_to_world().ptr());
734 COLLADAFW::NodePointerArray &children = root_node->getChildNodes();
735 for (
uint i = 0; i < children.getCount(); i++) {
736 set_pose(ob_arm, children[i], bone_name, mat);
740bool ArmatureImporter::node_is_decomposed(
const COLLADAFW::Node *node)
742 const COLLADAFW::TransformationPointerArray &nodeTransforms = node->getTransformations();
743 for (
uint i = 0; i < nodeTransforms.getCount(); i++) {
744 COLLADAFW::Transformation *
transform = nodeTransforms[i];
745 COLLADAFW::Transformation::TransformationType tm_type =
transform->getTransformationType();
746 if (tm_type == COLLADAFW::Transformation::MATRIX) {
755 root_joints.push_back(node);
757 joint_parent_map[node->getUniqueId()] = parent;
765 Object *ob_arm = find_armature(node);
767 get_armature_joints(ob_arm).root_joints.push_back(node);
771 fprintf(stderr,
"%s cannot be added to armature.\n", get_joint_name(node));
780 std::vector<Object *> arm_objs;
781 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it;
786 for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) {
790 Object *ob_arm = create_armature_bones(bmain, skin);
795 if (guid !=
nullptr) {
796 Object *ob = mesh_importer->get_object_by_geom_uid(*guid);
800 std::vector<Object *>::iterator ob_it = std::find(
801 objects_to_scale.begin(), objects_to_scale.end(), ob);
803 if (ob_it != objects_to_scale.end()) {
804 int index = ob_it - objects_to_scale.begin();
805 objects_to_scale.erase(objects_to_scale.begin() + index);
808 if (std::find(objects_to_scale.begin(), objects_to_scale.end(), ob_arm) ==
809 objects_to_scale.end())
811 objects_to_scale.push_back(ob_arm);
814 if (std::find(arm_objs.begin(), arm_objs.end(), ob_arm) == arm_objs.end()) {
815 arm_objs.push_back(ob_arm);
819 fprintf(stderr,
"Cannot find object to link armature with.\n");
823 fprintf(stderr,
"Cannot find geometry to link armature with.\n");
837 create_armature_bones(bmain, arm_objs);
840 std::vector<Object *>::iterator ob_arm_it;
841 for (ob_arm_it = arm_objs.begin(); ob_arm_it != arm_objs.end(); ob_arm_it++) {
843 Object *ob_arm = *ob_arm_it;
858void ArmatureImporter::link_armature(
Object *ob_arm,
859 const COLLADAFW::UniqueId &geom_id,
860 const COLLADAFW::UniqueId &controller_data_id)
865 fprintf(stderr,
"Cannot find object by geometry UID.\n");
869 if (skin_by_data_uid.find(controller_data_id) == skin_by_data_uid.end()) {
870 fprintf(stderr,
"Cannot find skin info by controller data UID.\n");
874 SkinInfo &skin = skin_by_data_uid[conroller_data_id];
895 const COLLADAFW::Matrix4Array &inv_bind_mats =
data->getInverseBindMatrices();
896 for (
uint i = 0; i <
data->getJointsCount(); i++) {
900 skin_by_data_uid[
data->getUniqueId()] = skin;
908 const COLLADAFW::UniqueId &con_id =
controller->getUniqueId();
910 if (
controller->getControllerType() == COLLADAFW::Controller::CONTROLLER_TYPE_SKIN) {
911 COLLADAFW::SkinController *co = (COLLADAFW::SkinController *)
controller;
913 geom_uid_by_controller_uid[con_id] = co->getSource();
915 const COLLADAFW::UniqueId &data_uid = co->getSkinControllerData();
916 if (skin_by_data_uid.find(data_uid) == skin_by_data_uid.end()) {
917 fprintf(stderr,
"Cannot find skin by controller data UID.\n");
921 skin_by_data_uid[data_uid].set_controller(co);
924 else if (
controller->getControllerType() == COLLADAFW::Controller::CONTROLLER_TYPE_MORPH) {
925 COLLADAFW::MorphController *co = (COLLADAFW::MorphController *)
controller;
927 geom_uid_by_controller_uid[con_id] = co->getSource();
929 morph_controllers.push_back(co);
938 std::vector<COLLADAFW::MorphController *>::iterator mc;
941 for (mc = morph_controllers.begin(); mc != morph_controllers.end(); mc++) {
943 COLLADAFW::UniqueIdArray &morphTargetIds = (*mc)->getMorphTargets();
944 COLLADAFW::FloatOrDoubleArray &morphWeights = (*mc)->getMorphWeights();
947 Object *source_ob = this->mesh_importer->get_object_by_geom_uid((*mc)->getSource());
962 for (
int i = 0; i < morphTargetIds.getCount(); i++) {
966 Mesh *mesh = this->mesh_importer->get_mesh_by_geom_uid(morphTargetIds[i]);
970 std::string morph_name = *this->mesh_importer->get_geometry_name(mesh->
id.
name);
976 weight = morphWeights.getFloatValues()->getData()[i];
980 fprintf(stderr,
"Morph target geometry not found.\n");
985 fprintf(stderr,
"Morph target object not found.\n");
992 if (geom_uid_by_controller_uid.find(controller_uid) == geom_uid_by_controller_uid.end()) {
996 return &geom_uid_by_controller_uid[controller_uid];
1001 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it;
1002 for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) {
1010 std::map<COLLADAFW::UniqueId, Object *>::iterator arm;
1011 for (arm = unskinned_armature_map.begin(); arm != unskinned_armature_map.end(); arm++) {
1012 if (arm->first == node->getUniqueId()) {
1021 this->uid_tags_map = tags_map;
1026 size_t joint_path_maxncpy)
1030 BLI_snprintf(joint_path, joint_path_maxncpy,
"pose.bones[\"%s\"]", bone_name_esc);
1035 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it;
1037 for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) {
1050 COLLADAFW::Node *node,
1052 std::vector<std::string> &layer_labels,
1056 extended_bones[bone->
name] = be;
1058 TagsMap::iterator etit;
1060 etit = uid_tags_map.find(node->getUniqueId().toAscii());
1062 bool has_connect =
false;
1063 int connect_type = -1;
1065 if (etit != uid_tags_map.end()) {
1072 bool has_tail =
false;
1073 has_tail |= et->
setData(
"tip_x", &tail[0]);
1074 has_tail |= et->
setData(
"tip_y", &tail[1]);
1075 has_tail |= et->
setData(
"tip_z", &tail[2]);
1077 has_connect = et->
setData(
"connect", &connect_type);
1078 bool has_roll = et->
setData(
"roll", &roll);
1082 if (has_tail && !has_connect) {
1096 if (!has_connect && this->import_settings->auto_connect) {
1098 connect_type = sibcount == 1;
C++ functions to deal with Armature collections (i.e. the successor of bone layers).
BoneCollection * ANIM_armature_bonecoll_get_by_name(bArmature *armature, const char *name) ATTR_WARN_UNUSED_RESULT
bool ANIM_armature_bonecoll_assign_editbone(BoneCollection *bcoll, EditBone *ebone)
static const char * bc_get_joint_name(T *node)
static const char * bc_get_joint_name(T *node)
#define UNLIMITED_CHAIN_MAX
#define MINIMUM_BONE_LENGTH
Blender kernel action and pose functionality.
bPoseChannel * BKE_pose_channel_find_name(const bPose *pose, const char *name)
void mat3_to_vec_roll(const float mat[3][3], float r_vec[3], float *r_roll)
Bone * BKE_armature_find_bone_name(bArmature *arm, const char *name)
Main * CTX_data_main(const bContext *C)
Key * BKE_key_add(Main *bmain, ID *id)
void BKE_keyblock_convert_from_mesh(const Mesh *mesh, const Key *key, KeyBlock *kb)
KeyBlock * BKE_keyblock_add_ctime(Key *key, const char *name, bool do_force)
General operations, lookup, etc. for blender objects.
#define LISTBASE_FOREACH(type, var, list)
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
void mat4_to_loc_rot_size(float loc[3], float rot[3][3], float size[3], const float wmat[4][4])
void copy_m4_m4(float m1[4][4], const float m2[4][4])
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
bool invert_m4(float mat[4][4])
void mat4_to_axis_angle(float axis[3], float *angle, const float mat[4][4])
MINLINE float len_squared_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE float normalize_v3_v3(float r[3], const float a[3])
size_t BLI_snprintf(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
size_t BLI_str_escape(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
void DEG_id_tag_update(ID *id, unsigned int flags)
struct bPoseChannel bPoseChannel
struct BoneCollection BoneCollection
struct bArmature bArmature
static Controller * controller
static double angle(const Eigen::Vector3d &v1, const Eigen::Vector3d &v2)
EditBone * ED_armature_ebone_add(bArmature *arm, const char *name)
void ED_armature_edit_free(bArmature *arm)
void ED_armature_from_edit(Main *bmain, bArmature *arm)
void ED_armature_to_edit(bArmature *arm)
SIMD_FORCE_INLINE btVector3 transform(const btVector3 &point) const
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
SIMD_FORCE_INLINE btScalar length() const
Return the length of the vector.
void make_shape_keys(bContext *C)
void make_armatures(bContext *C, std::vector< Object * > &objects_to_scale)
void get_rna_path_for_joint(COLLADAFW::Node *node, char *joint_path, size_t joint_path_maxncpy)
bool write_skin_controller_data(const COLLADAFW::SkinControllerData *data)
bool write_controller(const COLLADAFW::Controller *controller)
void set_tags_map(TagsMap &tags_map)
ArmatureImporter(UnitConverter *conv, MeshImporterBase *mesh, Main *bmain, Scene *sce, ViewLayer *view_layer, const ImportSettings *import_settings)
Object * get_armature_for_joint(COLLADAFW::Node *node)
void add_root_joint(COLLADAFW::Node *node, Object *parent)
bool get_joint_bind_mat(float m[4][4], COLLADAFW::Node *joint)
COLLADAFW::UniqueId * get_geometry_uid(const COLLADAFW::UniqueId &controller_uid)
void set_use_connect(int use_connect)
void set_roll(float roll)
const std::vector< std::string > & get_bone_collections()
void set_bone_collections(std::vector< std::string > bone_collections)
void set_tail(const float vec[])
void set_leaf_bone(bool state)
void set_chain_length(int aLength)
virtual Object * get_object_by_geom_uid(const COLLADAFW::UniqueId &geom_uid)=0
const COLLADAFW::UniqueId & get_controller_uid()
void link_armature(bContext *C, Object *ob, std::map< COLLADAFW::UniqueId, COLLADAFW::Node * > &joint_by_uid, TransformReader *tm)
Object * set_armature(Object *ob_arm)
void borrow_skin_controller_data(const COLLADAFW::SkinControllerData *skin)
bool uses_joint_or_descendant(COLLADAFW::Node *node)
Object * BKE_armature_from_object()
bool get_joint_inv_bind_matrix(float inv_bind_mat[4][4], COLLADAFW::Node *node)
void add_joint(const COLLADABU::Math::Matrix4 &matrix)
void set_parent(Object *_parent)
bPoseChannel * get_pose_channel_from_node(COLLADAFW::Node *node)
Object * create_armature(Main *bmain, Scene *scene, ViewLayer *view_layer)
Object * bc_add_object(Main *bmain, Scene *scene, ViewLayer *view_layer, int type, const char *name)
bool bc_set_parent(Object *ob, Object *par, bContext *C, bool is_parent_space)
void bc_set_IDPropertyMatrix(EditBone *ebone, const char *key, float mat[4][4])
bool bc_is_leaf_bone(Bone *bone)
EditBone * bc_get_edit_bone(bArmature *armature, char *name)
std::map< std::string, BoneExtended * > BoneExtensionMap
local_group_size(16, 16) .push_constant(Type b