22 #include "COLLADABUPlatform.h"
26 #include "COLLADAFWUniqueId.h"
43 const std::string &
id =
node->getName();
44 return id.empty() ?
node->getOriginalId().c_str() :
id.c_str();
56 view_layer(view_layer),
58 import_settings(import_settings),
67 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it;
68 for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) {
74 JointData *ArmatureImporter::get_joint_data(COLLADAFW::Node *
node);
76 const COLLADAFW::UniqueId &joint_id =
node->getUniqueId();
78 if (joint_id_to_joint_index_map.find(joint_id) == joint_id_to_joint_index_map.end()) {
80 stderr,
"Cannot find a joint index by joint id for %s.\n",
node->getOriginalId().c_str());
84 int joint_index = joint_id_to_joint_index_map[joint_id];
86 return &joint_index_to_joint_info_map[joint_index];
90 int ArmatureImporter::create_bone(
SkinInfo *skin,
91 COLLADAFW::Node *
node,
94 float parent_mat[4][4],
96 std::vector<std::string> &layer_labels)
99 float joint_inv_bind_mat[4][4];
100 float joint_bind_mat[4][4];
101 int chain_length = 0;
104 std::vector<COLLADAFW::Node *>::iterator it;
105 it = std::find(finished_joints.begin(), finished_joints.end(),
node);
106 if (it != finished_joints.end()) {
117 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator skin_it;
118 bool bone_is_skinned =
false;
119 for (skin_it = skin_by_data_uid.begin(); skin_it != skin_by_data_uid.end(); skin_it++) {
135 bone_is_skinned =
true;
141 if (!bone_is_skinned) {
149 float loc[3],
size[3],
rot[3][3];
151 BoneExtended &be = add_bone_extended(bone,
node, totchild, layer_labels, extended_bones);
161 switch (use_connect) {
183 float rest_mat[4][4];
194 if (use_connect == 1) {
201 leaf_bone_length =
length;
205 COLLADAFW::NodePointerArray &children =
node->getChildNodes();
207 for (
unsigned int i = 0; i < children.getCount(); i++) {
208 int cl = create_bone(skin, children[i], bone, children.getCount(), mat, arm, layer_labels);
209 if (cl > chain_length) {
215 joint_by_uid[
node->getUniqueId()] =
node;
216 finished_joints.push_back(
node);
220 return chain_length + 1;
229 void ArmatureImporter::fix_leaf_bone_hierarchy(
bArmature *armature,
231 bool fix_orientation)
233 if (bone ==
nullptr) {
241 fix_leaf_bone(armature, ebone, be, fix_orientation);
245 fix_leaf_bone_hierarchy(armature, child, fix_orientation);
249 void ArmatureImporter::fix_leaf_bone(
bArmature *armature,
252 bool fix_orientation)
254 if (be ==
nullptr || !be->
has_tail()) {
257 float leaf_length = (leaf_bone_length == FLT_MAX) ? 1.0 : leaf_bone_length;
261 if (fix_orientation && ebone->
parent !=
nullptr) {
279 void ArmatureImporter::fix_parent_connect(
bArmature *armature,
Bone *bone)
282 if (bone ==
nullptr) {
291 fix_parent_connect(armature, child);
295 void ArmatureImporter::connect_bone_chains(
bArmature *armature,
297 int max_chain_length)
303 if (parentbone ==
nullptr) {
309 for (; child; child = child->
next) {
313 if (chain_len <= max_chain_length) {
314 if (chain_len > maxlen) {
318 else if (chain_len == maxlen) {
319 dominant_child =
nullptr;
327 if (dominant_child !=
nullptr) {
345 if (pbe && pbe->
get_chain_length() >= this->import_settings->min_chain_length) {
352 printf(
"Connect Bone chain: parent (%s --> %s) child)\n", pebone->
name, cebone->
name);
362 ArmatureImporter::connect_bone_chains(armature, parentbone, maxlen - 1);
376 void ArmatureImporter::set_leaf_bone_shapes(
Object *ob_arm)
380 std::vector<LeafBone>::iterator it;
381 for (it = leaf_bones.begin(); it != leaf_bones.end(); it++) {
382 LeafBone &leaf = *it;
386 pchan->
custom = get_empty_for_leaves();
389 fprintf(stderr,
"Cannot find a pose channel for leaf bone %s\n", leaf.name);
394 void ArmatureImporter::set_euler_rotmode()
398 std::map<COLLADAFW::UniqueId, COLLADAFW::Node *>::iterator it;
400 for (it = joint_by_uid.begin(); it != joint_by_uid.end(); it++) {
402 COLLADAFW::Node *joint = it->second;
404 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator sit;
406 for (sit = skin_by_data_uid.begin(); sit != skin_by_data_uid.end(); sit++) {
416 fprintf(stderr,
"Cannot find pose channel for %s.\n", get_joint_name(joint));
426 Object *ArmatureImporter::get_empty_for_leaves()
439 Object *ArmatureImporter::find_armature(COLLADAFW::Node *
node)
441 JointData *jd = get_joint_data(
node);
446 COLLADAFW::NodePointerArray &children =
node->getChildNodes();
447 for (
int i = 0; i < children.getCount(); i++) {
448 Object *ob_arm = find_armature(children[i]);
457 ArmatureJoints &ArmatureImporter::get_armature_joints(
Object *ob_arm)
460 std::vector<ArmatureJoints>::iterator it;
461 for (it = armature_joints.begin(); it != armature_joints.end(); it++) {
462 if ((*it).ob_arm == ob_arm) {
470 armature_joints.push_back(aj);
472 return armature_joints.back();
475 void ArmatureImporter::create_armature_bones(
Main *bmain, std::vector<Object *> &arm_objs)
477 std::vector<COLLADAFW::Node *>::iterator ri;
478 std::vector<std::string> layer_labels;
481 for (ri = root_joints.begin(); ri != root_joints.end(); ri++) {
482 COLLADAFW::Node *
node = *ri;
487 Object *ob_arm = joint_parent_map[
node->getUniqueId()];
501 "Reuse of child bone [%s] as root bone in same Armature is not supported.\n",
510 nullptr,
node,
nullptr,
node->getChildNodes().getCount(),
nullptr, armature, layer_labels);
520 fix_leaf_bone_hierarchy(
521 armature, (
Bone *)armature->
bonebase.
first, this->import_settings->fix_orientation);
522 unskinned_armature_map[
node->getUniqueId()] = ob_arm;
527 set_bone_transformation_type(
node, ob_arm);
529 int index = std::find(arm_objs.begin(), arm_objs.end(), ob_arm) - arm_objs.begin();
531 arm_objs.push_back(ob_arm);
584 std::vector<COLLADAFW::Node *> skin_root_joints;
585 std::vector<std::string> layer_labels;
587 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it;
588 for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) {
594 skin_root_joints.clear();
598 std::vector<COLLADAFW::Node *>::iterator ri;
599 for (ri = skin_root_joints.begin(); ri != skin_root_joints.end(); ri++) {
600 COLLADAFW::Node *
node = *ri;
601 if (
a->uses_joint_or_descendant(
node)) {
607 if (shared !=
nullptr) {
612 if (!shared && !this->joint_parent_map.empty()) {
620 shared = this->joint_parent_map.begin()->second;
642 std::vector<COLLADAFW::Node *>::iterator ri;
643 for (ri = root_joints.begin(); ri != root_joints.end(); ri++) {
644 COLLADAFW::Node *
node = *ri;
646 if (shared && std::find(skin_root_joints.begin(), skin_root_joints.end(),
node) !=
647 skin_root_joints.end()) {
655 &skin,
node,
nullptr,
node->getChildNodes().getCount(),
nullptr, armature, layer_labels);
657 if (joint_parent_map.find(
node->getUniqueId()) != joint_parent_map.end() &&
668 for (ri = root_joints.begin(); ri != root_joints.end(); ri++) {
669 COLLADAFW::Node *
node = *ri;
670 set_bone_transformation_type(
node, ob_arm);
677 fix_leaf_bone_hierarchy(
678 armature, (
Bone *)armature->
bonebase.
first, this->import_settings->fix_orientation);
687 void ArmatureImporter::set_bone_transformation_type(
const COLLADAFW::Node *
node,
Object *ob_arm)
694 COLLADAFW::NodePointerArray childnodes =
node->getChildNodes();
695 for (
int index = 0; index < childnodes.getCount(); index++) {
696 node = childnodes[index];
697 set_bone_transformation_type(
node, ob_arm);
701 void ArmatureImporter::set_pose(
Object *ob_arm,
702 COLLADAFW::Node *root_node,
703 const char *parentname,
704 float parent_mat[4][4])
712 bool is_decomposed = node_is_decomposed(root_node);
730 float invObmat[4][4];
741 COLLADAFW::NodePointerArray &children = root_node->getChildNodes();
742 for (
unsigned int i = 0; i < children.getCount(); i++) {
743 set_pose(ob_arm, children[i], bone_name, mat);
747 bool ArmatureImporter::node_is_decomposed(
const COLLADAFW::Node *
node)
749 const COLLADAFW::TransformationPointerArray &nodeTransforms =
node->getTransformations();
750 for (
unsigned int i = 0; i < nodeTransforms.getCount(); i++) {
751 COLLADAFW::Transformation *
transform = nodeTransforms[i];
752 COLLADAFW::Transformation::TransformationType tm_type =
transform->getTransformationType();
753 if (tm_type == COLLADAFW::Transformation::MATRIX) {
767 root_joints.push_back(
node);
769 joint_parent_map[
node->getUniqueId()] = parent;
779 get_armature_joints(ob_arm).root_joints.push_back(
node);
781 # ifdef COLLADA_DEBUG
783 fprintf(stderr,
"%s cannot be added to armature.\n", get_joint_name(
node));
793 std::vector<Object *> arm_objs;
794 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it;
797 leaf_bone_length = FLT_MAX;
799 for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) {
803 Object *ob_arm = create_armature_bones(bmain, skin);
808 if (guid !=
nullptr) {
813 std::vector<Object *>::iterator ob_it = std::find(
814 objects_to_scale.begin(), objects_to_scale.end(), ob);
816 if (ob_it != objects_to_scale.end()) {
817 int index = ob_it - objects_to_scale.begin();
818 objects_to_scale.erase(objects_to_scale.begin() + index);
821 if (std::find(objects_to_scale.begin(), objects_to_scale.end(), ob_arm) ==
822 objects_to_scale.end()) {
823 objects_to_scale.push_back(ob_arm);
826 if (std::find(arm_objs.begin(), arm_objs.end(), ob_arm) == arm_objs.end()) {
827 arm_objs.push_back(ob_arm);
831 fprintf(stderr,
"Cannot find object to link armature with.\n");
835 fprintf(stderr,
"Cannot find geometry to link armature with.\n");
849 create_armature_bones(bmain, arm_objs);
852 std::vector<Object *>::iterator ob_arm_it;
853 for (ob_arm_it = arm_objs.begin(); ob_arm_it != arm_objs.end(); ob_arm_it++) {
855 Object *ob_arm = *ob_arm_it;
870 void ArmatureImporter::link_armature(
Object *ob_arm,
871 const COLLADAFW::UniqueId &geom_id,
872 const COLLADAFW::UniqueId &controller_data_id)
877 fprintf(stderr,
"Cannot find object by geometry UID.\n");
881 if (skin_by_data_uid.find(controller_data_id) == skin_by_data_uid.end()) {
882 fprintf(stderr,
"Cannot find skin info by controller data UID.\n");
886 SkinInfo &skin = skin_by_data_uid[conroller_data_id];
907 const COLLADAFW::Matrix4Array &inv_bind_mats =
data->getInverseBindMatrices();
908 for (
unsigned int i = 0; i <
data->getJointsCount(); i++) {
912 skin_by_data_uid[
data->getUniqueId()] = skin;
920 const COLLADAFW::UniqueId &con_id =
controller->getUniqueId();
922 if (
controller->getControllerType() == COLLADAFW::Controller::CONTROLLER_TYPE_SKIN) {
923 COLLADAFW::SkinController *co = (COLLADAFW::SkinController *)
controller;
925 geom_uid_by_controller_uid[con_id] = co->getSource();
927 const COLLADAFW::UniqueId &data_uid = co->getSkinControllerData();
928 if (skin_by_data_uid.find(data_uid) == skin_by_data_uid.end()) {
929 fprintf(stderr,
"Cannot find skin by controller data UID.\n");
933 skin_by_data_uid[data_uid].set_controller(co);
936 else if (
controller->getControllerType() == COLLADAFW::Controller::CONTROLLER_TYPE_MORPH) {
937 COLLADAFW::MorphController *co = (COLLADAFW::MorphController *)
controller;
939 geom_uid_by_controller_uid[con_id] = co->getSource();
941 morph_controllers.push_back(co);
950 std::vector<COLLADAFW::MorphController *>::iterator mc;
953 for (mc = morph_controllers.begin(); mc != morph_controllers.end(); mc++) {
955 COLLADAFW::UniqueIdArray &morphTargetIds = (*mc)->getMorphTargets();
956 COLLADAFW::FloatOrDoubleArray &morphWeights = (*mc)->getMorphWeights();
974 for (
int i = 0; i < morphTargetIds.getCount(); i++) {
988 weight = morphWeights.getFloatValues()->getData()[i];
992 fprintf(stderr,
"Morph target geometry not found.\n");
997 fprintf(stderr,
"Morph target object not found.\n");
1004 if (geom_uid_by_controller_uid.find(controller_uid) == geom_uid_by_controller_uid.end()) {
1008 return &geom_uid_by_controller_uid[controller_uid];
1013 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it;
1014 for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) {
1022 std::map<COLLADAFW::UniqueId, Object *>::iterator arm;
1023 for (arm = unskinned_armature_map.begin(); arm != unskinned_armature_map.end(); arm++) {
1024 if (arm->first ==
node->getUniqueId()) {
1033 this->uid_tags_map = tags_map;
1040 char bone_name_esc[
sizeof(((
Bone *)
nullptr)->name) * 2];
1048 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it;
1050 for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) {
1062 COLLADAFW::Node *
node,
1064 std::vector<std::string> &layer_labels,
1068 extended_bones[bone->
name] = be;
1070 TagsMap::iterator etit;
1072 etit = uid_tags_map.find(
node->getUniqueId().toAscii());
1074 bool has_connect =
false;
1075 int connect_type = -1;
1077 if (etit != uid_tags_map.end()) {
1079 float tail[3] = {FLT_MAX, FLT_MAX, FLT_MAX};
1085 bool has_tail =
false;
1086 has_tail |= et->
setData(
"tip_x", &tail[0]);
1087 has_tail |= et->
setData(
"tip_y", &tail[1]);
1088 has_tail |= et->
setData(
"tip_z", &tail[2]);
1090 has_connect = et->
setData(
"connect", &connect_type);
1091 bool has_roll = et->
setData(
"roll", &roll);
1093 layers = et->
setData(
"layer", layers);
1095 if (has_tail && !has_connect) {
1110 if (!has_connect && this->import_settings->
auto_connect) {
1112 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(struct Mesh *me, struct Key *key, struct KeyBlock *kb)
struct KeyBlock * BKE_keyblock_add_ctime(struct Key *key, const char *name, const 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 size_t char size_t BLI_str_escape(char *__restrict dst, const char *__restrict src, const size_t dst_maxncpy) ATTR_NONNULL()
size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
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 length(const btQuaternion &q)
Return the length of a quaternion.
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_chain_length(const int aLength)
void set_bone_layers(std::string layers, std::vector< std::string > &layer_labels)
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
void find_root_joints(const std::vector< COLLADAFW::Node * > &root_joints, std::map< COLLADAFW::UniqueId, COLLADAFW::Node * > &joint_by_uid, std::vector< COLLADAFW::Node * > &result)
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