Commit: 8d42c2f3ea4b37c6a538b2b01e4d00f98ca96100 Author: Gaia Clary Date: Sat Nov 29 13:24:26 2014 +0100 Branches: master https://developer.blender.org/rB8d42c2f3ea4b37c6a538b2b01e4d00f98ca96100
Improved Collada Armature Import Collada does not support Bone length. It has only Joints. This patch tries to improve the necessary "guesswork" for importing Rigs. Differential Revision: https://developer.blender.org/D920 =================================================================== M source/blender/collada/ArmatureImporter.cpp M source/blender/collada/ArmatureImporter.h M source/blender/collada/DocumentImporter.cpp M source/blender/collada/ImportSettings.h M source/blender/collada/collada.cpp M source/blender/collada/collada.h M source/blender/editors/io/io_collada.c =================================================================== diff --git a/source/blender/collada/ArmatureImporter.cpp b/source/blender/collada/ArmatureImporter.cpp index 033145b..db24bf6 100644 --- a/source/blender/collada/ArmatureImporter.cpp +++ b/source/blender/collada/ArmatureImporter.cpp @@ -37,6 +37,7 @@ #include "BKE_object.h" #include "BKE_armature.h" #include "BLI_string.h" +#include "BLI_listbase.h" #include "ED_armature.h" #include "ArmatureImporter.h" @@ -49,7 +50,23 @@ static const char *bc_get_joint_name(T *node) return id.size() ? id.c_str() : node->getOriginalId().c_str(); } -ArmatureImporter::ArmatureImporter(UnitConverter *conv, MeshImporterBase *mesh, Scene *sce) : +static EditBone *get_edit_bone(bArmature * armature, char *name) { + EditBone *eBone; + + eBone = (EditBone *)armature->edbo->first; + for (eBone; eBone; eBone = eBone->next) { + if (!strcmp(name, eBone->name)) + return eBone; + } + + return NULL; + +} + + + +ArmatureImporter::ArmatureImporter(UnitConverter *conv, MeshImporterBase *mesh, Scene *sce, const ImportSettings *import_settings) : + import_settings(import_settings), unit_converter(conv), TransformReader(conv), scene(sce), @@ -57,6 +74,15 @@ ArmatureImporter::ArmatureImporter(UnitConverter *conv, MeshImporterBase *mesh, mesh_importer(mesh) { } +void ArmatureImporter::clear_extended_boneset() +{ + for (std::map<std::string, BoneExtended *>::iterator it = extended_bones.begin(); it != extended_bones.end(); ++it) { + if (it->second != NULL) + delete it->second; + } + extended_bones.clear(); +} + ArmatureImporter::~ArmatureImporter() { // free skin controller data if we forget to do this earlier @@ -64,6 +90,7 @@ ArmatureImporter::~ArmatureImporter() for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) { it->second.free(); } + clear_extended_boneset(); } #if 0 @@ -83,16 +110,17 @@ JointData *ArmatureImporter::get_joint_data(COLLADAFW::Node *node); } #endif -void ArmatureImporter::create_bone(SkinInfo *skin, COLLADAFW::Node *node, EditBone *parent, int totchild, +int ArmatureImporter::create_bone(SkinInfo *skin, COLLADAFW::Node *node, EditBone *parent, int totchild, float parent_mat[4][4], bArmature *arm) { float mat[4][4]; float joint_inv_bind_mat[4][4]; + int chain_length = 0; //Checking if bone is already made. std::vector<COLLADAFW::Node *>::iterator it; it = std::find(finished_joints.begin(), finished_joints.end(), node); - if (it != finished_joints.end()) return; + if (it != finished_joints.end()) return chain_length; // JointData* jd = get_joint_data(node); @@ -143,96 +171,150 @@ void ArmatureImporter::create_bone(SkinInfo *skin, COLLADAFW::Node *node, EditBo // set tail, don't set it to head because 0-length bones are not allowed add_v3_v3v3(bone->tail, bone->head, vec); - // set parent tail + /* find smallest bone length in armature (used later for leaf bone length) */ if (parent) { - // XXX increase this to prevent "very" small bones? - const float epsilon = 0.000001f; - - // derive leaf bone length + /* guess reasonable leaf bone length */ float length = len_v3v3(parent->head, bone->head); - if ((length < leaf_bone_length || totbone == 0) && length > epsilon) { + if ((length < leaf_bone_length || totbone == 0) && length > MINIMUM_BONE_LENGTH) { leaf_bone_length = length; } - - if (totchild == 1) { - copy_v3_v3(parent->tail, bone->head); - - // not setting BONE_CONNECTED because this would lock child bone location with respect to parent - bone->flag |= BONE_CONNECTED; - - - // treat zero-sized bone like a leaf bone - if (length <= epsilon) { - add_leaf_bone(parent_mat, parent, node); - } - } - } COLLADAFW::NodePointerArray& children = node->getChildNodes(); + + BoneExtended &be = add_bone_extended(bone, node); + be.set_leaf_bone(true); + for (unsigned int i = 0; i < children.getCount(); i++) { - create_bone(skin, children[i], bone, children.getCount(), mat, arm); + int cl = create_bone(skin, children[i], bone, children.getCount(), mat, arm); + if (cl > chain_length) + chain_length = cl; } - // in second case it's not a leaf bone, but we handle it the same way - if (!children.getCount() || children.getCount() > 1) { - add_leaf_bone(mat, bone, node); - } bone->length = len_v3v3(bone->head, bone->tail); joint_by_uid[node->getUniqueId()] = node; finished_joints.push_back(node); + + be.set_chain_length(chain_length + 1); + + return chain_length + 1; } -void ArmatureImporter::add_leaf_bone(float mat[4][4], EditBone *bone, COLLADAFW::Node *node) +/** + * Collada only knows Joints, hence bones at the end of a bone chain + * don't have a defined length. This function guesses reasonable + * tail locations for the affected bones (nodes which don't have any connected child) + * Hint: The extended_bones set gets populated in ArmatureImporter::create_bone +**/ +void ArmatureImporter::fix_bone_orientation(bArmature *armature, Bone *bone) { - LeafBone leaf; + /* armature has no bones */ + if (bone == NULL) + return; - leaf.bone = bone; - copy_m4_m4(leaf.mat, mat); - BLI_strncpy(leaf.name, bone->name, sizeof(leaf.name)); - - TagsMap::iterator etit; - ExtraTags *et = 0; - etit = uid_tags_map.find(node->getUniqueId().toAscii()); - if (etit != uid_tags_map.end()) { - et = etit->second; - //else return; + BoneExtended *be = extended_bones[bone->name]; + if (be != NULL && be->is_leaf_bone() ) { + /* Collada only knows Joints, Here we guess a reasonable leaf bone length */ + float leaf_length = (leaf_bone_length == FLT_MAX) ? 1.0 : leaf_bone_length; - float x, y, z; - et->setData("tip_x", &x); - et->setData("tip_y", &y); - et->setData("tip_z", &z); - float vec[3] = {x, y, z}; - copy_v3_v3(leaf.bone->tail, leaf.bone->head); - add_v3_v3v3(leaf.bone->tail, leaf.bone->head, vec); - } - else { - leaf_bones.push_back(leaf); + EditBone *ebone = get_edit_bone(armature, bone->name); + + float vec[3]; + if (ebone->parent != NULL) { + EditBone *parent = ebone->parent; + sub_v3_v3v3(vec, ebone->head, parent->tail); + if (len_squared_v3(vec) < MINIMUM_BONE_LENGTH) + { + sub_v3_v3v3(vec, parent->tail, parent->head); + } + } + else { + vec[2] = 0.1f; + sub_v3_v3v3(vec, ebone->tail, ebone->head); + } + normalize_v3_v3(vec, vec); + mul_v3_fl(vec, leaf_length); + add_v3_v3v3(ebone->tail, ebone->head, vec); } -} -void ArmatureImporter::fix_leaf_bones( ) -{ - // Collada only knows Joints, Here we guess a reasonable - // leaf bone length - float leaf_length = (leaf_bone_length == FLT_MAX) ? 1.0:leaf_bone_length; + for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) { + fix_bone_orientation(armature, child); + } - // just setting tail for leaf bones here - std::vector<LeafBone>::iterator it; - for (it = leaf_bones.begin(); it != leaf_bones.end(); it++) { +} - LeafBone& leaf = *it; - // pointing up - float vec[3] = {0.0f, 0.0f, 0.1f}; - - sub_v3_v3v3(vec, leaf.bone->tail , leaf.bone->head); - mul_v3_fl(vec, leaf_length); - add_v3_v3v3(leaf.bone->tail, leaf.bone->head , vec); +void ArmatureImporter::connect_bone_chains(bArmature *armature, Bone *parentbone, int clip) +{ + BoneExtended *dominant_child = NULL; + int maxlen = 0; + Bone *child = (Bone *)parentbone->childbase.first; + if (child && (import_settings->find_chains || child->next==NULL) ) + { + for (child; child; child = child->next) { + BoneExtended *be = extended_bones[child->name]; + if (be != NULL) { + if (be->get_chain_length() <= clip) { + if (be->get_chain_length() > maxlen) { + dominant_child = be; + maxlen = be->get_chain_length(); + } + else if (be->get_chain_length() == maxlen) { + dominant_child = NULL; + } + } + } + } + } + BoneExtended *pbe = extended_bones[parentbone->name]; + if (dominant_child != NULL) { + /* Found a valid chain. Now connect current bone with that chain.*/ + EditBone *pebone = get_edit_bone(armature, parentbone->name); + EditBone *cebone = get_edit_bone(armature, dominant_child->get_name()); + if (pebone && !(cebone->flag & BONE_CONNECTED)) { + + float vec[3]; + sub_v3_v3v3(vec, cebone->head, pebone->head); + + /* + * It is possible that the child's head is located on the parents head. + * When this happens, then moving the parent's tail to the child's head + * would result in a zero sized bone and Blender would silently remove the bone. + * So we move the tail only when the resulting bone has a minimum length: + */ + + if (len_squared_v3(vec) > MINIMUM_BONE_LENGTH) + { + pebone->tail[0] = cebone->head[0]; + pebone->tail[1] = cebone->head[1]; + pebone->tail[2] = cebone->head[2]; + + if (pbe && pbe->get_chain_length() >= this->import_settings->min_chain_length) { + cebone->flag |= BONE_CONNECTED; + printf("Connecting chain: parent %s --> %s (child)\n", pebone->name, cebone->name); + pbe->set_leaf_bone(false); + } + } + } + for (Bone *child = (Bone *)parentbone->childbase.first; child; child = child->next) { + ArmatureImporter::connect_bone_chains(armature, child, UNLIMITED_CHAIN_MAX); + } + } + else if (maxlen>1 && maxlen > this->import_settings->min_chain_length) { + /* Try again with smaller chain length */ + ArmatureImporter::connect_bone_chains(armature, parentbone, maxlen - 1); + } + else { + /* can't connect this Bone. Proceed with children ... */ + if (pbe) pbe->set_leaf_bone(true); + for (Bone *child = (Bone *)parentbone->childbase.first; child; child = child->next) { + ArmatureImporter::connect_bone_chains(armature, child, UNLIMITED_CHAIN_MAX); + } } + } #if 0 @@ -351,21 +433,26 @@ void ArmatureImporter::create_armature_bones( ) continue; } + clear_extended_boneset(); + ED_armature_to_edit(armature); create_bone(NULL, *ri , NULL, (*ri)->getChildNodes().getCount(), NULL, armature); - //leaf bone tails are derived from the matrix, so no need of this. - fix_leaf_bones(); + /* exit armature edit mode to populate the Armature object */ + ED_armature_from_edit(armature); + ED_armature_edit_free(armature); + + /* and step back to edit mode to fix the leaf nodes */ + ED_armatur @@ Diff output truncated at 10240 characters. @@ _______________________________________________ Bf-blender-cvs mailing list [email protected] http://lists.blender.org/mailman/listinfo/bf-blender-cvs
