8 #include "COLLADABUPlatform.h"
12 #include "COLLADAFWUniqueId.h"
29 const std::string &
id =
node->getName();
30 return id.empty() ?
node->getOriginalId().c_str() :
id.c_str();
42 view_layer(view_layer),
44 import_settings(import_settings),
53 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it;
54 for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) {
60 JointData *ArmatureImporter::get_joint_data(COLLADAFW::Node *
node);
62 const COLLADAFW::UniqueId &joint_id =
node->getUniqueId();
64 if (joint_id_to_joint_index_map.find(joint_id) == joint_id_to_joint_index_map.end()) {
66 stderr,
"Cannot find a joint index by joint id for %s.\n",
node->getOriginalId().c_str());
70 int joint_index = joint_id_to_joint_index_map[joint_id];
72 return &joint_index_to_joint_info_map[joint_index];
76 int ArmatureImporter::create_bone(
SkinInfo *skin,
77 COLLADAFW::Node *
node,
80 float parent_mat[4][4],
82 std::vector<std::string> &layer_labels)
85 float joint_inv_bind_mat[4][4];
86 float joint_bind_mat[4][4];
90 std::vector<COLLADAFW::Node *>::iterator it;
91 it = std::find(finished_joints.begin(), finished_joints.end(),
node);
92 if (it != finished_joints.end()) {
103 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator skin_it;
104 bool bone_is_skinned =
false;
105 for (skin_it = skin_by_data_uid.begin(); skin_it != skin_by_data_uid.end(); skin_it++) {
108 if (
b->get_joint_inv_bind_matrix(joint_inv_bind_mat,
node)) {
121 bone_is_skinned =
true;
127 if (!bone_is_skinned) {
135 float loc[3],
size[3],
rot[3][3];
137 BoneExtended &be = add_bone_extended(bone,
node, totchild, layer_labels, extended_bones);
147 switch (use_connect) {
169 float rest_mat[4][4];
180 if (use_connect == 1) {
187 leaf_bone_length =
length;
191 COLLADAFW::NodePointerArray &children =
node->getChildNodes();
193 for (
unsigned int i = 0; i < children.getCount(); i++) {
194 int cl = create_bone(skin, children[i], bone, children.getCount(), mat, arm, layer_labels);
195 if (cl > chain_length) {
201 joint_by_uid[
node->getUniqueId()] =
node;
202 finished_joints.push_back(
node);
206 return chain_length + 1;
209 void ArmatureImporter::fix_leaf_bone_hierarchy(
bArmature *armature,
211 bool fix_orientation)
213 if (bone ==
nullptr) {
221 fix_leaf_bone(armature, ebone, be, fix_orientation);
225 fix_leaf_bone_hierarchy(armature, child, fix_orientation);
229 void ArmatureImporter::fix_leaf_bone(
bArmature *armature,
232 bool fix_orientation)
234 if (be ==
nullptr || !be->
has_tail()) {
237 float leaf_length = (leaf_bone_length == FLT_MAX) ? 1.0 : leaf_bone_length;
241 if (fix_orientation && ebone->
parent !=
nullptr) {
259 void ArmatureImporter::fix_parent_connect(
bArmature *armature,
Bone *bone)
262 if (bone ==
nullptr) {
271 fix_parent_connect(armature, child);
275 void ArmatureImporter::connect_bone_chains(
bArmature *armature,
277 int max_chain_length)
283 if (parentbone ==
nullptr) {
289 for (; child; child = child->
next) {
293 if (chain_len <= max_chain_length) {
294 if (chain_len > maxlen) {
298 else if (chain_len == maxlen) {
299 dominant_child =
nullptr;
307 if (dominant_child !=
nullptr) {
325 if (pbe && pbe->
get_chain_length() >= this->import_settings->min_chain_length) {
332 printf(
"Connect Bone chain: parent (%s --> %s) child)\n", pebone->
name, cebone->
name);
342 ArmatureImporter::connect_bone_chains(armature, parentbone, maxlen - 1);
356 void ArmatureImporter::set_leaf_bone_shapes(
Object *ob_arm)
360 std::vector<LeafBone>::iterator it;
361 for (it = leaf_bones.begin(); it != leaf_bones.end(); it++) {
362 LeafBone &leaf = *it;
366 pchan->
custom = get_empty_for_leaves();
369 fprintf(stderr,
"Cannot find a pose channel for leaf bone %s\n", leaf.name);
374 void ArmatureImporter::set_euler_rotmode()
378 std::map<COLLADAFW::UniqueId, COLLADAFW::Node *>::iterator it;
380 for (it = joint_by_uid.begin(); it != joint_by_uid.end(); it++) {
382 COLLADAFW::Node *joint = it->second;
384 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator sit;
386 for (sit = skin_by_data_uid.begin(); sit != skin_by_data_uid.end(); sit++) {
396 fprintf(stderr,
"Cannot find pose channel for %s.\n", get_joint_name(joint));
406 Object *ArmatureImporter::get_empty_for_leaves()
419 Object *ArmatureImporter::find_armature(COLLADAFW::Node *
node)
421 JointData *jd = get_joint_data(
node);
426 COLLADAFW::NodePointerArray &children =
node->getChildNodes();
427 for (
int i = 0; i < children.getCount(); i++) {
428 Object *ob_arm = find_armature(children[i]);
437 ArmatureJoints &ArmatureImporter::get_armature_joints(
Object *ob_arm)
440 std::vector<ArmatureJoints>::iterator it;
441 for (it = armature_joints.begin(); it != armature_joints.end(); it++) {
442 if ((*it).ob_arm == ob_arm) {
450 armature_joints.push_back(aj);
452 return armature_joints.back();
455 void ArmatureImporter::create_armature_bones(
Main *bmain, std::vector<Object *> &arm_objs)
457 std::vector<COLLADAFW::Node *>::iterator ri;
458 std::vector<std::string> layer_labels;
461 for (ri = root_joints.begin(); ri != root_joints.end(); ri++) {
462 COLLADAFW::Node *
node = *ri;
467 Object *ob_arm = joint_parent_map[
node->getUniqueId()];
481 "Reuse of child bone [%s] as root bone in same Armature is not supported.\n",
490 nullptr,
node,
nullptr,
node->getChildNodes().getCount(),
nullptr, armature, layer_labels);
500 fix_leaf_bone_hierarchy(
501 armature, (
Bone *)armature->
bonebase.
first, this->import_settings->fix_orientation);
502 unskinned_armature_map[
node->getUniqueId()] = ob_arm;
507 set_bone_transformation_type(
node, ob_arm);
509 int index = std::find(arm_objs.begin(), arm_objs.end(), ob_arm) - arm_objs.begin();
511 arm_objs.push_back(ob_arm);
564 std::vector<COLLADAFW::Node *> skin_root_joints;
565 std::vector<std::string> layer_labels;
567 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it;
568 for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) {
570 if (
b ==
a ||
b->BKE_armature_from_object() ==
nullptr) {
574 skin_root_joints.clear();
576 b->find_root_joints(root_joints, joint_by_uid, skin_root_joints);
578 std::vector<COLLADAFW::Node *>::iterator ri;
579 for (ri = skin_root_joints.begin(); ri != skin_root_joints.end(); ri++) {
580 COLLADAFW::Node *
node = *ri;
581 if (
a->uses_joint_or_descendant(
node)) {
582 shared =
b->BKE_armature_from_object();
587 if (shared !=
nullptr) {
592 if (!shared && !this->joint_parent_map.empty()) {
600 shared = this->joint_parent_map.begin()->second;
622 std::vector<COLLADAFW::Node *>::iterator ri;
623 for (ri = root_joints.begin(); ri != root_joints.end(); ri++) {
624 COLLADAFW::Node *
node = *ri;
626 if (shared && std::find(skin_root_joints.begin(), skin_root_joints.end(),
node) !=
627 skin_root_joints.end()) {
635 &skin,
node,
nullptr,
node->getChildNodes().getCount(),
nullptr, armature, layer_labels);
637 if (joint_parent_map.find(
node->getUniqueId()) != joint_parent_map.end() &&
648 for (ri = root_joints.begin(); ri != root_joints.end(); ri++) {
649 COLLADAFW::Node *
node = *ri;
650 set_bone_transformation_type(
node, ob_arm);
657 fix_leaf_bone_hierarchy(
658 armature, (
Bone *)armature->
bonebase.
first, this->import_settings->fix_orientation);
667 void ArmatureImporter::set_bone_transformation_type(
const COLLADAFW::Node *
node,
Object *ob_arm)
674 COLLADAFW::NodePointerArray childnodes =
node->getChildNodes();
675 for (
int index = 0; index < childnodes.getCount(); index++) {
676 node = childnodes[index];
677 set_bone_transformation_type(
node, ob_arm);
681 void ArmatureImporter::set_pose(
Object *ob_arm,
682 COLLADAFW::Node *root_node,
683 const char *parentname,
684 float parent_mat[4][4])
692 bool is_decomposed = node_is_decomposed(root_node);
710 float invObmat[4][4];
721 COLLADAFW::NodePointerArray &children = root_node->getChildNodes();
722 for (
unsigned int i = 0; i < children.getCount(); i++) {
723 set_pose(ob_arm, children[i], bone_name, mat);
727 bool ArmatureImporter::node_is_decomposed(
const COLLADAFW::Node *
node)
729 const COLLADAFW::TransformationPointerArray &nodeTransforms =
node->getTransformations();
730 for (
unsigned int i = 0; i < nodeTransforms.getCount(); i++) {
731 COLLADAFW::Transformation *
transform = nodeTransforms[i];
732 COLLADAFW::Transformation::TransformationType tm_type =
transform->getTransformationType();
733 if (tm_type == COLLADAFW::Transformation::MATRIX) {
742 root_joints.push_back(
node);
744 joint_parent_map[
node->getUniqueId()] = parent;
754 get_armature_joints(ob_arm).root_joints.push_back(
node);
756 # ifdef COLLADA_DEBUG
758 fprintf(stderr,
"%s cannot be added to armature.\n", get_joint_name(
node));
767 std::vector<Object *> arm_objs;
768 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it;
771 leaf_bone_length = FLT_MAX;
773 for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) {
777 Object *ob_arm = create_armature_bones(bmain, skin);
782 if (guid !=
nullptr) {
787 std::vector<Object *>::iterator ob_it = std::find(
788 objects_to_scale.begin(), objects_to_scale.end(), ob);
790 if (ob_it != objects_to_scale.end()) {
791 int index = ob_it - objects_to_scale.begin();
792 objects_to_scale.erase(objects_to_scale.begin() + index);
795 if (std::find(objects_to_scale.begin(), objects_to_scale.end(), ob_arm) ==
796 objects_to_scale.end()) {
797 objects_to_scale.push_back(ob_arm);
800 if (std::find(arm_objs.begin(), arm_objs.end(), ob_arm) == arm_objs.end()) {
801 arm_objs.push_back(ob_arm);
805 fprintf(stderr,
"Cannot find object to link armature with.\n");
809 fprintf(stderr,
"Cannot find geometry to link armature with.\n");
823 create_armature_bones(bmain, arm_objs);
826 std::vector<Object *>::iterator ob_arm_it;
827 for (ob_arm_it = arm_objs.begin(); ob_arm_it != arm_objs.end(); ob_arm_it++) {
829 Object *ob_arm = *ob_arm_it;
844 void ArmatureImporter::link_armature(
Object *ob_arm,
845 const COLLADAFW::UniqueId &geom_id,
846 const COLLADAFW::UniqueId &controller_data_id)
851 fprintf(stderr,
"Cannot find object by geometry UID.\n");
855 if (skin_by_data_uid.find(controller_data_id) == skin_by_data_uid.end()) {
856 fprintf(stderr,
"Cannot find skin info by controller data UID.\n");
860 SkinInfo &skin = skin_by_data_uid[conroller_data_id];
881 const COLLADAFW::Matrix4Array &inv_bind_mats =
data->getInverseBindMatrices();
882 for (
unsigned int i = 0; i <
data->getJointsCount(); i++) {
886 skin_by_data_uid[
data->getUniqueId()] = skin;
894 const COLLADAFW::UniqueId &con_id =
controller->getUniqueId();
896 if (
controller->getControllerType() == COLLADAFW::Controller::CONTROLLER_TYPE_SKIN) {
897 COLLADAFW::SkinController *co = (COLLADAFW::SkinController *)
controller;
899 geom_uid_by_controller_uid[con_id] = co->getSource();
901 const COLLADAFW::UniqueId &data_uid = co->getSkinControllerData();
902 if (skin_by_data_uid.find(data_uid) == skin_by_data_uid.end()) {
903 fprintf(stderr,
"Cannot find skin by controller data UID.\n");
907 skin_by_data_uid[data_uid].set_controller(co);
910 else if (
controller->getControllerType() == COLLADAFW::Controller::CONTROLLER_TYPE_MORPH) {
911 COLLADAFW::MorphController *co = (COLLADAFW::MorphController *)
controller;
913 geom_uid_by_controller_uid[con_id] = co->getSource();
915 morph_controllers.push_back(co);
924 std::vector<COLLADAFW::MorphController *>::iterator mc;
927 for (mc = morph_controllers.begin(); mc != morph_controllers.end(); mc++) {
929 COLLADAFW::UniqueIdArray &morphTargetIds = (*mc)->getMorphTargets();
930 COLLADAFW::FloatOrDoubleArray &morphWeights = (*mc)->getMorphWeights();
948 for (
int i = 0; i < morphTargetIds.getCount(); i++) {
962 weight = morphWeights.getFloatValues()->getData()[i];
966 fprintf(stderr,
"Morph target geometry not found.\n");
971 fprintf(stderr,
"Morph target object not found.\n");
978 if (geom_uid_by_controller_uid.find(controller_uid) == geom_uid_by_controller_uid.end()) {
982 return &geom_uid_by_controller_uid[controller_uid];
987 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it;
988 for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) {
996 std::map<COLLADAFW::UniqueId, Object *>::iterator arm;
997 for (arm = unskinned_armature_map.begin(); arm != unskinned_armature_map.end(); arm++) {
998 if (arm->first ==
node->getUniqueId()) {
1007 this->uid_tags_map = tags_map;
1014 char bone_name_esc[
sizeof(((
Bone *)
nullptr)->name) * 2];
1021 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it;
1023 for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) {
1035 COLLADAFW::Node *
node,
1037 std::vector<std::string> &layer_labels,
1041 extended_bones[bone->
name] = be;
1043 TagsMap::iterator etit;
1045 etit = uid_tags_map.find(
node->getUniqueId().toAscii());
1047 bool has_connect =
false;
1048 int connect_type = -1;
1050 if (etit != uid_tags_map.end()) {
1052 float tail[3] = {FLT_MAX, FLT_MAX, FLT_MAX};
1058 bool has_tail =
false;
1059 has_tail |= et->
setData(
"tip_x", &tail[0]);
1060 has_tail |= et->
setData(
"tip_y", &tail[1]);
1061 has_tail |= et->
setData(
"tip_z", &tail[2]);
1063 has_connect = et->
setData(
"connect", &connect_type);
1064 bool has_roll = et->
setData(
"roll", &roll);
1066 layers = et->
setData(
"layer", layers);
1068 if (has_tail && !has_connect) {
1083 if (!has_connect && this->import_settings->
auto_connect) {
1085 connect_type = sibcount == 1;
static const char * bc_get_joint_name(T *node)
#define UNLIMITED_CHAIN_MAX
#define MINIMUM_BONE_LENGTH
Blender kernel action and pose functionality.
struct bPoseChannel * BKE_pose_channel_find_name(const struct bPose *pose, const char *name)
struct Bone * BKE_armature_find_bone_name(struct bArmature *arm, const char *name)
void mat3_to_vec_roll(const float mat[3][3], float r_vec[3], float *r_roll)
struct Main * CTX_data_main(const bContext *C)
void BKE_keyblock_convert_from_mesh(const struct Mesh *me, const struct Key *key, struct KeyBlock *kb)
struct KeyBlock * BKE_keyblock_add_ctime(struct Key *key, const char *name, bool do_force)
struct Key * BKE_key_add(struct Main *bmain, struct ID *id)
General operations, lookup, etc. for blender objects.
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
bool invert_m4(float R[4][4])
bool invert_m4_m4(float R[4][4], const float A[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])
void mat4_to_axis_angle(float axis[3], float *angle, const float M[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 maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
size_t size_t char size_t BLI_str_escape(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL()
void DEG_id_tag_update(struct ID *id, int flag)
static Controller * controller
EditBone * ED_armature_ebone_add(bArmature *arm, const char *name)
void ED_armature_edit_free(struct 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 angle(const btVector3 &v) const
Return the angle between this and another vector.
void make_shape_keys(bContext *C)
void get_rna_path_for_joint(COLLADAFW::Node *node, char *joint_path, size_t count)
void make_armatures(bContext *C, std::vector< Object * > &objects_to_scale)
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)
void set_tail(const float vec[])
void set_leaf_bone(bool state)
void set_bone_layers(std::string layers, std::vector< std::string > &layer_labels)
void set_chain_length(int aLength)
BoneExtensionMap & getExtensionMap(bArmature *armature)
virtual Object * get_object_by_geom_uid(const COLLADAFW::UniqueId &geom_uid)=0
virtual std::string * get_geometry_name(const std::string &mesh_name)=0
virtual Mesh * get_mesh_by_geom_uid(const COLLADAFW::UniqueId &mesh_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)
bool bc_set_parent(Object *ob, Object *par, bContext *C, bool is_parent_space)
EditBone * bc_get_edit_bone(bArmature *armature, char *name)
void bc_set_IDPropertyMatrix(EditBone *ebone, const char *key, float mat[4][4])
Object * bc_add_object(Main *bmain, Scene *scene, ViewLayer *view_layer, int type, const char *name)
bool bc_is_leaf_bone(Bone *bone)
std::map< std::string, BoneExtended * > BoneExtensionMap
T length(const vec_base< T, Size > &a)
static const pxr::TfToken b("b", pxr::TfToken::Immortal)