瀏覽代碼

fix the issue mesh bone weights may exported incorrectly

Jason0214 7 年之前
父節點
當前提交
bcb2bda7b3

+ 23 - 4
io_scene_godot/converters/mesh.py

@@ -91,6 +91,19 @@ def export_mesh(escn_file, export_settings, node, armature_data):
     return mesh_id
 
 
+def find_bone_vertex_groups(vertex_groups, armature_data):
+    """Find the id of vertex groups connected to bone weights,
+    return a dict() mapping from vertex_group id to bone id"""
+    ret = dict()
+    if armature_data is not None:
+        # the bone's index in the bones list is exported as the id
+        for bone_id, bone in enumerate(armature_data.bones):
+            group = vertex_groups.get(bone.name)
+            if group is not None:
+                ret[group.index] = bone_id
+    return ret
+
+
 def make_arrays(escn_file, export_settings, node, armature_data):
     """Generates arrays of positions, normals etc"""
     if armature_data is not None:
@@ -126,12 +139,16 @@ def make_arrays(escn_file, export_settings, node, armature_data):
         mesh.calc_normals_split()
         has_tangents = False
 
+    # find the vertex group id of bone weights
+    gid_to_bid_map = find_bone_vertex_groups(node.vertex_groups, armature_data)
+
     # Separate by materials into single-material surfaces
     surfaces = generate_surfaces(
         escn_file,
         export_settings,
         mesh,
-        has_tangents
+        has_tangents,
+        gid_to_bid_map
     )
 
     has_bone = True if armature_data is not None else False
@@ -145,7 +162,8 @@ def make_arrays(escn_file, export_settings, node, armature_data):
     return surfaces
 
 
-def generate_surfaces(escn_file, export_settings, mesh, has_tangents):
+def generate_surfaces(escn_file, export_settings, mesh, has_tangents,
+                      gid_to_bid_map):
     """Splits up the mesh into surfaces with a single material each.
     Within this, it creates the Vertex structure to contain all data about
     a single vertex
@@ -197,8 +215,9 @@ def generate_surfaces(escn_file, export_settings, mesh, has_tangents):
                 new_vert.bitangent = fix_vertex(loop.bitangent)
 
             for vertex_group in mesh.vertices[loop.vertex_index].groups:
-                new_vert.bones.append(vertex_group.group)
-                new_vert.weights.append(vertex_group.weight)
+                if vertex_group.group in gid_to_bid_map:
+                    new_vert.bones.append(gid_to_bid_map[vertex_group.group])
+                    new_vert.weights.append(vertex_group.weight)
 
             # Merge similar vertices
             tup = new_vert.get_tup()

File diff suppressed because it is too large
+ 7 - 0
tests/reference-exports/armature_with_other_vertex_groups.escn


二進制
tests/scenes/armature_with_other_vertex_groups.blend


Some files were not shown because too many files changed in this diff