12#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 chain_length = std::max(cl, chain_length);
206 joint_by_uid[node->getUniqueId()] = node;
207 finished_joints.push_back(node);
211 return chain_length + 1;
214void ArmatureImporter::fix_leaf_bone_hierarchy(
bArmature *armature,
216 bool fix_orientation)
218 if (bone ==
nullptr) {
223 BoneExtensionMap &extended_bones = bone_extension_manager.getExtensionMap(armature);
224 BoneExtended *be = extended_bones[bone->
name];
226 fix_leaf_bone(armature, ebone, be, fix_orientation);
230 fix_leaf_bone_hierarchy(armature, child, fix_orientation);
234void ArmatureImporter::fix_leaf_bone(
bArmature *armature,
237 bool fix_orientation)
239 if (be ==
nullptr || !be->
has_tail()) {
242 float leaf_length = (leaf_bone_length ==
FLT_MAX) ? 1.0 : leaf_bone_length;
246 if (fix_orientation && ebone->
parent !=
nullptr) {
247 EditBone *parent = ebone->
parent;
264void ArmatureImporter::fix_parent_connect(
bArmature *armature,
Bone *bone)
267 if (bone ==
nullptr) {
276 fix_parent_connect(armature, child);
280void ArmatureImporter::connect_bone_chains(
bArmature *armature,
282 int max_chain_length)
284 BoneExtensionMap &extended_bones = bone_extension_manager.getExtensionMap(armature);
285 BoneExtended *dominant_child =
nullptr;
288 if (parentbone ==
nullptr) {
293 if (child && (import_settings->find_chains || child->next ==
nullptr)) {
294 for (; child; child = child->next) {
295 BoneExtended *be = extended_bones[child->name];
298 if (chain_len <= max_chain_length) {
299 if (chain_len > maxlen) {
303 else if (chain_len == maxlen) {
304 dominant_child =
nullptr;
311 BoneExtended *pbe = extended_bones[parentbone->
name];
312 if (dominant_child !=
nullptr) {
332 BoneExtended *cbe = extended_bones[cebone->
name];
337 printf(
"Connect Bone chain: parent (%s --> %s) child)\n", pebone->
name, cebone->
name);
345 else if (maxlen > 1 && maxlen > this->import_settings->min_chain_length) {
347 ArmatureImporter::connect_bone_chains(armature, parentbone, maxlen - 1);
361void ArmatureImporter::set_leaf_bone_shapes(
Object *ob_arm)
365 std::vector<LeafBone>::iterator it;
366 for (it = leaf_bones.begin(); it != leaf_bones.end(); it++) {
367 LeafBone &leaf = *it;
371 pchan->
custom = get_empty_for_leaves();
374 fprintf(stderr,
"Cannot find a pose channel for leaf bone %s\n", leaf.name);
379void ArmatureImporter::set_euler_rotmode()
383 std::map<COLLADAFW::UniqueId, COLLADAFW::Node *>::iterator it;
385 for (it = joint_by_uid.begin(); it != joint_by_uid.end(); it++) {
387 COLLADAFW::Node *joint = it->second;
389 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator sit;
391 for (sit = skin_by_data_uid.begin(); sit != skin_by_data_uid.end(); sit++) {
392 SkinInfo &skin = sit->second;
401 fprintf(stderr,
"Cannot find pose channel for %s.\n", get_joint_name(joint));
411Object *ArmatureImporter::get_empty_for_leaves()
424Object *ArmatureImporter::find_armature(COLLADAFW::Node *node)
426 JointData *jd = get_joint_data(node);
431 COLLADAFW::NodePointerArray &children = node->getChildNodes();
432 for (
int i = 0;
i < children.getCount();
i++) {
433 Object *ob_arm = find_armature(children[
i]);
442ArmatureJoints &ArmatureImporter::get_armature_joints(
Object *ob_arm)
445 std::vector<ArmatureJoints>::iterator it;
446 for (it = armature_joints.begin(); it != armature_joints.end(); it++) {
447 if ((*it).ob_arm == ob_arm) {
455 armature_joints.push_back(aj);
457 return armature_joints.back();
460void ArmatureImporter::create_armature_bones(
Main *bmain, std::vector<Object *> &arm_objs)
462 std::vector<COLLADAFW::Node *>::iterator ri;
463 std::vector<std::string> layer_labels;
466 for (ri = root_joints.begin(); ri != root_joints.end(); ri++) {
467 COLLADAFW::Node *node = *ri;
472 Object *ob_arm = joint_parent_map[node->getUniqueId()];
492 "Reuse of child bone [%s] as root bone in same Armature is not supported.\n",
500 nullptr, node,
nullptr, node->getChildNodes().getCount(),
nullptr, armature, layer_labels);
501 if (this->import_settings->find_chains) {
510 fix_leaf_bone_hierarchy(
512 unskinned_armature_map[node->getUniqueId()] = ob_arm;
517 set_bone_transformation_type(node, ob_arm);
519 int index = std::find(arm_objs.begin(), arm_objs.end(), ob_arm) - arm_objs.begin();
521 arm_objs.push_back(ob_arm);
573 std::vector<COLLADAFW::Node *> skin_root_joints;
574 std::vector<std::string> layer_labels;
576 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it;
577 for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) {
578 SkinInfo *
b = &it->second;
579 if (
b == a ||
b->BKE_armature_from_object() ==
nullptr) {
583 skin_root_joints.clear();
585 b->find_root_joints(root_joints, joint_by_uid, skin_root_joints);
587 std::vector<COLLADAFW::Node *>::iterator ri;
588 for (ri = skin_root_joints.begin(); ri != skin_root_joints.end(); ri++) {
589 COLLADAFW::Node *node = *ri;
591 shared =
b->BKE_armature_from_object();
601 if (!
shared && !this->joint_parent_map.empty()) {
609 shared = this->joint_parent_map.begin()->second;
631 std::vector<COLLADAFW::Node *>::iterator ri;
632 for (ri = root_joints.begin(); ri != root_joints.end(); ri++) {
633 COLLADAFW::Node *node = *ri;
635 if (
shared && std::find(skin_root_joints.begin(), skin_root_joints.end(), node) !=
636 skin_root_joints.end())
645 &skin, node,
nullptr, node->getChildNodes().getCount(),
nullptr, armature, layer_labels);
647 if (joint_parent_map.find(node->getUniqueId()) != joint_parent_map.end() &&
650 skin.
set_parent(joint_parent_map[node->getUniqueId()]);
659 for (ri = root_joints.begin(); ri != root_joints.end(); ri++) {
660 COLLADAFW::Node *node = *ri;
661 set_bone_transformation_type(node, ob_arm);
665 if (this->import_settings->find_chains) {
668 fix_leaf_bone_hierarchy(
678void ArmatureImporter::set_bone_transformation_type(
const COLLADAFW::Node *node,
Object *ob_arm)
685 COLLADAFW::NodePointerArray childnodes = node->getChildNodes();
686 for (
int index = 0; index < childnodes.getCount(); index++) {
687 node = childnodes[index];
688 set_bone_transformation_type(node, ob_arm);
692void ArmatureImporter::set_pose(
Object *ob_arm,
693 COLLADAFW::Node *root_node,
694 const char *parentname,
695 float parent_mat[4][4])
703 bool is_decomposed = node_is_decomposed(root_node);
721 float invObmat[4][4];
722 invert_m4_m4(invObmat, ob_arm->object_to_world().ptr());
732 COLLADAFW::NodePointerArray &children = root_node->getChildNodes();
733 for (
uint i = 0;
i < children.getCount();
i++) {
734 set_pose(ob_arm, children[
i], bone_name, mat);
738bool ArmatureImporter::node_is_decomposed(
const COLLADAFW::Node *node)
740 const COLLADAFW::TransformationPointerArray &nodeTransforms = node->getTransformations();
741 for (
uint i = 0;
i < nodeTransforms.getCount();
i++) {
742 COLLADAFW::Transformation *
transform = nodeTransforms[
i];
743 COLLADAFW::Transformation::TransformationType tm_type =
transform->getTransformationType();
744 if (tm_type == COLLADAFW::Transformation::MATRIX) {
753 root_joints.push_back(node);
755 joint_parent_map[node->getUniqueId()] = parent;
763 Object *ob_arm = find_armature(node);
765 get_armature_joints(ob_arm).root_joints.push_back(node);
769 fprintf(stderr,
"%s cannot be added to armature.\n", get_joint_name(node));
778 std::vector<Object *> arm_objs;
779 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it;
784 for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) {
788 Object *ob_arm = create_armature_bones(bmain, skin);
793 if (guid !=
nullptr) {
794 Object *ob = mesh_importer->get_object_by_geom_uid(*guid);
798 std::vector<Object *>::iterator ob_it = std::find(
799 objects_to_scale.begin(), objects_to_scale.end(), ob);
801 if (ob_it != objects_to_scale.end()) {
802 int index = ob_it - objects_to_scale.begin();
803 objects_to_scale.erase(objects_to_scale.begin() + index);
806 if (std::find(objects_to_scale.begin(), objects_to_scale.end(), ob_arm) ==
807 objects_to_scale.end())
809 objects_to_scale.push_back(ob_arm);
812 if (std::find(arm_objs.begin(), arm_objs.end(), ob_arm) == arm_objs.end()) {
813 arm_objs.push_back(ob_arm);
817 fprintf(stderr,
"Cannot find object to link armature with.\n");
821 fprintf(stderr,
"Cannot find geometry to link armature with.\n");
835 create_armature_bones(bmain, arm_objs);
838 std::vector<Object *>::iterator ob_arm_it;
839 for (ob_arm_it = arm_objs.begin(); ob_arm_it != arm_objs.end(); ob_arm_it++) {
841 Object *ob_arm = *ob_arm_it;
856void ArmatureImporter::link_armature(
Object *ob_arm,
857 const COLLADAFW::UniqueId &geom_id,
858 const COLLADAFW::UniqueId &controller_data_id)
863 fprintf(stderr,
"Cannot find object by geometry UID.\n");
867 if (skin_by_data_uid.find(controller_data_id) == skin_by_data_uid.end()) {
868 fprintf(stderr,
"Cannot find skin info by controller data UID.\n");
872 SkinInfo &skin = skin_by_data_uid[conroller_data_id];
893 const COLLADAFW::Matrix4Array &inv_bind_mats =
data->getInverseBindMatrices();
894 for (
uint i = 0;
i <
data->getJointsCount();
i++) {
898 skin_by_data_uid[
data->getUniqueId()] = skin;
906 const COLLADAFW::UniqueId &con_id =
controller->getUniqueId();
908 if (
controller->getControllerType() == COLLADAFW::Controller::CONTROLLER_TYPE_SKIN) {
909 COLLADAFW::SkinController *co = (COLLADAFW::SkinController *)
controller;
911 geom_uid_by_controller_uid[con_id] = co->getSource();
913 const COLLADAFW::UniqueId &data_uid = co->getSkinControllerData();
914 if (skin_by_data_uid.find(data_uid) == skin_by_data_uid.end()) {
915 fprintf(stderr,
"Cannot find skin by controller data UID.\n");
919 skin_by_data_uid[data_uid].set_controller(co);
922 else if (
controller->getControllerType() == COLLADAFW::Controller::CONTROLLER_TYPE_MORPH) {
923 COLLADAFW::MorphController *co = (COLLADAFW::MorphController *)
controller;
925 geom_uid_by_controller_uid[con_id] = co->getSource();
927 morph_controllers.push_back(co);
936 std::vector<COLLADAFW::MorphController *>::iterator mc;
939 for (mc = morph_controllers.begin(); mc != morph_controllers.end(); mc++) {
941 COLLADAFW::UniqueIdArray &morphTargetIds = (*mc)->getMorphTargets();
942 COLLADAFW::FloatOrDoubleArray &morphWeights = (*mc)->getMorphWeights();
945 Object *source_ob = this->mesh_importer->get_object_by_geom_uid((*mc)->getSource());
960 for (
int i = 0;
i < morphTargetIds.getCount();
i++) {
964 Mesh *mesh = this->mesh_importer->get_mesh_by_geom_uid(morphTargetIds[
i]);
968 std::string morph_name = *this->mesh_importer->get_geometry_name(mesh->
id.
name);
974 weight = morphWeights.getFloatValues()->getData()[
i];
978 fprintf(stderr,
"Morph target geometry not found.\n");
983 fprintf(stderr,
"Morph target object not found.\n");
990 if (geom_uid_by_controller_uid.find(controller_uid) == geom_uid_by_controller_uid.end()) {
994 return &geom_uid_by_controller_uid[controller_uid];
999 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it;
1000 for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) {
1008 std::map<COLLADAFW::UniqueId, Object *>::iterator arm;
1009 for (arm = unskinned_armature_map.begin(); arm != unskinned_armature_map.end(); arm++) {
1010 if (arm->first == node->getUniqueId()) {
1019 this->uid_tags_map = tags_map;
1024 size_t joint_path_maxncpy)
1028 BLI_snprintf(joint_path, joint_path_maxncpy,
"pose.bones[\"%s\"]", bone_name_esc);
1033 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it;
1035 for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) {
1048 COLLADAFW::Node *node,
1050 std::vector<std::string> &layer_labels,
1054 extended_bones[bone->
name] = be;
1056 TagsMap::iterator etit;
1058 etit = uid_tags_map.find(node->getUniqueId().toAscii());
1060 bool has_connect =
false;
1061 int connect_type = -1;
1063 if (etit != uid_tags_map.end()) {
1070 bool has_tail =
false;
1071 has_tail |= et->
setData(
"tip_x", &tail[0]);
1072 has_tail |= et->
setData(
"tip_y", &tail[1]);
1073 has_tail |= et->
setData(
"tip_z", &tail[2]);
1075 has_connect = et->
setData(
"connect", &connect_type);
1076 bool has_roll = et->
setData(
"roll", &roll);
1080 if (has_tail && !has_connect) {
1094 if (!has_connect && this->import_settings->auto_connect) {
1096 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)
BMesh const char void * data
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])
EditBone * bc_get_edit_bone(bArmature *armature, const char *name)
bool bc_is_leaf_bone(Bone *bone)
std::map< std::string, BoneExtended * > BoneExtensionMap