Просмотр исходного кода

support export parent_bone relation

Jason0214 7 лет назад
Родитель
Сommit
2e78a3eb67

+ 5 - 0
io_scene_godot/__init__.py

@@ -87,6 +87,11 @@ class ExportGodot(bpy.types.Operator, ExportHelper):
         description="Export only objects on the active layers.",
         default=True,
         )
+    export_parent_bone_constraint = BoolProperty(
+        name="Parent Bone",
+        description="Export parent relation to armature bones",
+        default=True,
+        )
     material_search_paths = EnumProperty(
         name="Material Search Paths",
         description="Search for existing godot materials with names that match"

+ 3 - 1
io_scene_godot/converters/__init__.py

@@ -18,7 +18,7 @@ are stored in individual files.
 from .simple_nodes import *  # pylint: disable=wildcard-import
 from .mesh import export_mesh_node
 from .physics import export_physics_properties
-from .armature import export_armature_node
+from .armature import export_armature_node, export_bone_attachment
 
 
 BLENDER_TYPE_TO_EXPORTER = {
@@ -28,3 +28,5 @@ BLENDER_TYPE_TO_EXPORTER = {
     "LAMP": export_lamp_node,
     "EMPTY": export_empty_node
 }
+
+BONE_ATTACHMENT_EXPORTER = export_bone_attachment

+ 24 - 2
io_scene_godot/converters/armature.py

@@ -1,6 +1,28 @@
 """Export a armature node"""
 import mathutils
-from ..structures import NodeTemplate
+from ..structures import NodeTemplate, NodePath, Array
+
+
+def export_bone_attachment(escn_file, node, parent_gd_node):
+    """Export a blender object with parent_bone to a BoneAttachment"""
+    bone_attachment = NodeTemplate('BoneAttachment',
+                                   'BoneAttachment', parent_gd_node)
+
+    # node.parent_bone is exactly the bone name
+    # in the parent armature node
+    bone_attachment['bone_name'] = "\"{}\"".format(node.parent_bone)
+
+    # regard to ```export_armature_node()```, the exported bone id
+    # is the index of the bone in node.parent.pose.bones list
+    bone_id = node.parent.pose.bones.find(node.parent_bone)
+
+    # append node to its parent bone's bound_children list
+    parent_gd_node["bones/{}/{}".format(bone_id, 'bound_children')].append(
+        NodePath(parent_gd_node.get_path(), bone_attachment.get_path())
+    )
+
+    escn_file.add_node(bone_attachment)
+    return bone_attachment
 
 
 def get_armature_data(node):
@@ -36,7 +58,7 @@ class Bone:
         self.rest = mathutils.Matrix()
         self.pose = mathutils.Matrix()
         self.enabled = True
-        self.bound_children = None
+        self.bound_children = Array(prefix='[', suffix=']')
 
     def attr_to_key(self, attr_name):
         """Add bone id to bone attribute"""

+ 8 - 0
io_scene_godot/export_godot.py

@@ -72,6 +72,14 @@ class GodotExporter:
             )
             exporter = converters.BLENDER_TYPE_TO_EXPORTER["EMPTY"]
 
+        if (self.config['export_parent_bone_constraint'] and
+                node.parent_bone != ''):
+            parent_gd_node = converters.BONE_ATTACHMENT_EXPORTER(
+                self.escn_file,
+                node,
+                parent_gd_node
+            )
+
         # Perform the export
         exported_node = exporter(self.escn_file, self.config, node,
                                  parent_gd_node)