From 7690c2a38db02317d7ad289a16b8c55b5ec0ed3a Mon Sep 17 00:00:00 2001
From: Campbell Barton <ideasman42@gmail.com>
Date: Mon, 25 Jul 2011 04:02:19 +0000
Subject: [PATCH] fix for direct-x exporting vertex groups which had indicies
 out of the objects defgroup range

Tested using the file 'FBX_Export_Glitched.blend' from report: [#27964]
---
 io_export_directx_x.py | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/io_export_directx_x.py b/io_export_directx_x.py
index eaa4cbe2a..056111b26 100644
--- a/io_export_directx_x.py
+++ b/io_export_directx_x.py
@@ -568,10 +568,14 @@ def WriteMeshSkinWeights(Config, Object, Mesh):
         UsedBones = set()
         #Maps bones to a list of vertices they affect
         VertexGroups = {}
-        
+        ObjectVertexGroups = {i: Group.name for (i, Group) in enumerate(Object.vertex_groups)}
         for Vertex in Mesh.vertices:
             #BoneInfluences contains the bones of the armature that affect the current vertex
-            BoneInfluences = [PoseBones[Object.vertex_groups[Group.group].name] for Group in Vertex.groups if Object.vertex_groups[Group.group].name in PoseBones]
+            BoneInfluences = [PoseBone for Group in Vertex.groups
+                              for PoseBone in (PoseBones.get(ObjectVertexGroups.get(Group.group, "")), )
+                              if PoseBone is not None
+                              ]
+
             if len(BoneInfluences) > MaxInfluences:
                 MaxInfluences = len(BoneInfluences)
             for Bone in BoneInfluences:
@@ -611,10 +615,12 @@ def WriteMeshSkinWeights(Config, Object, Mesh):
                     if Vertex in VertexIndexes:
                         Config.File.write("{}{}".format("  " * Config.Whitespace, Index))
 
-                        GroupIndexes = {Object.vertex_groups[Group.group].name: Index for Index, Group in enumerate(Mesh.vertices[Vertex].groups) if Object.vertex_groups[Group.group].name in PoseBones}
+                        GroupIndexes = {ObjectVertexGroups.get(Group.group): Index
+                                        for Index, Group in enumerate(Mesh.vertices[Vertex].groups)
+                                        if ObjectVertexGroups.get(Group.group, "") in PoseBones}
 
                         WeightTotal = 0.0
-                        for Weight in [Group.weight for Group in Mesh.vertices[Vertex].groups if Object.vertex_groups[Group.group].name in PoseBones]:
+                        for Weight in (Group.weight for Group in Mesh.vertices[Vertex].groups if ObjectVertexGroups.get(Group.group, "") in PoseBones):
                             WeightTotal += Weight
 
                         if WeightTotal:
-- 
GitLab