moved is_leaf_bone() to collada utils for reuse in exporter and importer
authorGaia Clary <gaia.clary@machinimatrix.org>
Thu, 26 May 2016 16:22:36 +0000 (18:22 +0200)
committerGaia Clary <gaia.clary@machinimatrix.org>
Thu, 26 May 2016 16:22:36 +0000 (18:22 +0200)
source/blender/collada/ArmatureExporter.cpp
source/blender/collada/ArmatureImporter.cpp
source/blender/collada/ArmatureImporter.h
source/blender/collada/collada_utils.cpp
source/blender/collada/collada_utils.h

index cf02293886ca7eacec998063fe805580dbdc5f38..47a0ffda3c67e8e2ba56cfffa0a6c8dcc84e1dff 100644 (file)
@@ -150,16 +150,6 @@ std::string ArmatureExporter::get_joint_sid(Bone *bone, Object *ob_arm)
        return get_joint_id(bone, ob_arm);
 }
 
-static bool is_leaf_bone(Bone *bone)
-{
-       for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
-               if (child->flag & BONE_CONNECTED) {
-                       return false;
-               }
-       }
-       return true;
-}
-
 // parent_mat is armature-space
 void ArmatureExporter::add_bone_node(Bone *bone, Object *ob_arm, Scene *sce,
                                      SceneExporter *se,
@@ -185,7 +175,7 @@ void ArmatureExporter::add_bone_node(Bone *bone, Object *ob_arm, Scene *sce,
                                }
                        }
 
-                       if (is_leaf_bone(bone))
+                       if (bc_is_leaf_bone(bone))
                        {
                                node.addExtraTechniqueParameter("blender", "tip_x", bone->arm_tail[0] - bone->arm_head[0]);
                                node.addExtraTechniqueParameter("blender", "tip_y", bone->arm_tail[1] - bone->arm_head[1]);
index df60b213d161c0210ad94de12cd99d2c2d581ff9..4c318cd97cce731fd2a0314552b43732be0d140f 100644 (file)
@@ -210,18 +210,6 @@ int ArmatureImporter::create_bone(SkinInfo *skin, COLLADAFW::Node *node, EditBon
        return chain_length + 1;
 }
 
-/*
- * A bone is a leaf when it has no children or all children are not connected.
- */
-static bool is_leaf_bone(Bone *bone)
-{
-       for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
-               if (child->flag & BONE_CONNECTED)
-                       return false;
-       }
-       return true;
-}
-
 /**
   * Collada only knows Joints, hence bones at the end of a bone chain
   * don't have a defined length. This function guesses reasonable
@@ -233,35 +221,39 @@ void ArmatureImporter::fix_leaf_bones(bArmature *armature, Bone *bone)
        if (bone == NULL)
                return;
 
-       if (is_leaf_bone(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;
+       if (bc_is_leaf_bone(bone)) {
 
-               EditBone *ebone = get_edit_bone(armature, bone->name);
-               float vec[3];
+               BoneExtended *be = extended_bones[bone->name];
+               if (be == NULL || !be->has_custom_tail()) {
 
-               if (ebone->parent != NULL) {
-                       EditBone *parent = ebone->parent;
-                       sub_v3_v3v3(vec, ebone->head, parent->head);
-                       if (len_squared_v3(vec) < MINIMUM_BONE_LENGTH)
-                       {
-                               sub_v3_v3v3(vec, parent->tail, parent->head);
+                       /* 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;
+
+                       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->head);
+                               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);
                        }
-               }
-               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);
+                       normalize_v3_v3(vec, vec);
+                       mul_v3_fl(vec, leaf_length);
+                       add_v3_v3v3(ebone->tail, ebone->head, vec);
+               }
        }
 
        for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
                fix_leaf_bones(armature, child);
        }
-
 }
 
 void ArmatureImporter::fix_parent_connect(bArmature *armature, Bone *bone)
@@ -949,6 +941,7 @@ BoneExtended::BoneExtended(EditBone *aBone)
        this->tail[1]      = 0.5f;
        this->tail[2]      = 0.0f;
        this->use_connect  = -1;
+       this->has_tail     = false;
 }
 
 char *BoneExtended::get_name() 
@@ -986,6 +979,12 @@ void BoneExtended::set_tail(float vec[])
        this->tail[0] = vec[0];
        this->tail[1] = vec[1];
        this->tail[2] = vec[2];
+       this->has_tail = true;
+}
+
+bool BoneExtended::has_custom_tail()
+{
+       return this->has_tail;
 }
 
 float *BoneExtended::get_tail()
index 407fa83f84b3a057f9ca19c2040b8b1f0a566c0e..4cef3d4fb38f0e020c764a4536a67dabb9caeb9f 100644 (file)
@@ -67,6 +67,7 @@ private:
        bool  is_leaf;
        float tail[3];
        bool  use_connect;
+       bool  has_tail;
 
 public:
 
@@ -83,6 +84,7 @@ public:
 
        void set_tail(float *vec);
        float *get_tail();
+       bool has_custom_tail();
 
        void set_use_connect(int use_connect);
        int get_use_connect();
index f8feed8145cc1732829eb0ef0525fc39d5a097b8..30cf404cc3d93d99415cf33609e5f7fcf49d374c 100644 (file)
@@ -366,3 +366,15 @@ void bc_triangulate_mesh(Mesh *me)
        BM_mesh_bm_to_me(bm, me, &bm_to_me_params);
        BM_mesh_free(bm);
 }
+
+/*
+* A bone is a leaf when it has no children or all children are not connected.
+*/
+bool bc_is_leaf_bone(Bone *bone)
+{
+       for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
+               if (child->flag & BONE_CONNECTED)
+                       return false;
+       }
+       return true;
+}
index 4bc2f55cf333e27ea794be16f93adb3eaef5ed7b..7f8e93736b5f48fcdaa743abc413ed4e954f7fae 100644 (file)
@@ -87,7 +87,7 @@ extern void bc_match_scale(Object *ob, UnitConverter &bc_unit, bool scale_to_sce
 extern void bc_match_scale(std::vector<Object *> *objects_done, UnitConverter &unit_converter, bool scale_to_scene);
 
 extern void bc_triangulate_mesh(Mesh *me);
-
+extern bool bc_is_leaf_bone(Bone *bone);
 
 class BCPolygonNormalsIndices
 {