From ff5c86fcc343ef8bfa208d4594712ce53fac6b9e Mon Sep 17 00:00:00 2001
From: Campbell Barton <ideasman42@gmail.com>
Date: Tue, 13 Mar 2012 16:44:59 +0000
Subject: [PATCH] option to export only deforming bones (defaults to True for
 Unity3D)

---
 io_scene_fbx/__init__.py   |  5 +++++
 io_scene_fbx/export_fbx.py | 21 +++++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/io_scene_fbx/__init__.py b/io_scene_fbx/__init__.py
index f2ee6cbbc..f3862f7cd 100644
--- a/io_scene_fbx/__init__.py
+++ b/io_scene_fbx/__init__.py
@@ -132,6 +132,11 @@ class ExportFBX(bpy.types.Operator, ExportHelper):
                          "pipeline errors with XNA"),
             default=False,
             )
+    use_armature_deform_only = BoolProperty(
+            name="Only Deform Bones",
+            description="Only write deforming bones",
+            default=False,
+            )
     use_anim = BoolProperty(
             name="Include Animation",
             description="Export keyframe animation",
diff --git a/io_scene_fbx/export_fbx.py b/io_scene_fbx/export_fbx.py
index 33d120ae6..c71ba3cdf 100644
--- a/io_scene_fbx/export_fbx.py
+++ b/io_scene_fbx/export_fbx.py
@@ -216,6 +216,7 @@ def save_single(operator, scene, filepath="",
         object_types={'EMPTY', 'CAMERA', 'LAMP', 'ARMATURE', 'MESH'},
         use_mesh_modifiers=True,
         mesh_smooth_type='FACE',
+        use_armature_deform_only=False,
         use_anim=True,
         use_anim_optimize=True,
         anim_optimize_precision=6,
@@ -2098,11 +2099,30 @@ def save_single(operator, scene, filepath="",
         # fbxName, blenderObject, my_bones, blenderActions
         #ob_arms[i] = fbxArmObName, ob, arm_my_bones, (ob.action, [])
 
+        if use_armature_deform_only:
+            # tag non deforming bones that have no deforming children
+            deform_map = dict.fromkeys(my_arm.blenData.bones, False)
+            for bone in my_arm.blenData.bones:
+                if bone.use_deform:
+                    deform_map[bone] = True
+                    # tag all parents, even ones that are not deform since their child _is_
+                    for parent in bone.parent_recursive:
+                        deform_map[parent] = True
+
         for bone in my_arm.blenData.bones:
+
+            if use_armature_deform_only:
+                # if this bone doesnt deform, and none of its children deform, skip it!
+                if not deform_map[bone]:
+                    continue
+
             my_bone = my_bone_class(bone, my_arm)
             my_arm.fbxBones.append(my_bone)
             ob_bones.append(my_bone)
 
+        if use_armature_deform_only:
+            del deform_map
+
     # add the meshes to the bones and replace the meshes armature with own armature class
     #for obname, ob, mtx, me, mats, arm, armname in ob_meshes:
     for my_mesh in ob_meshes:
@@ -2932,6 +2952,7 @@ def defaults_unity3d():
                 use_selection=False,
                 object_types={'ARMATURE', 'EMPTY', 'MESH'},
                 use_mesh_modifiers=True,
+                use_armature_deform_only=True,
                 use_anim=True,
                 use_anim_optimize=False,
                 use_anim_action_all=True,
-- 
GitLab