Browse Source

Merge pull request #100 from Jason0214/collision_shape_animation

Fix compound rigid body, support physics animation
Lu Jiacheng 6 years ago
parent
commit
9a9c851567

+ 2 - 2
io_scene_godot/converters/animation/action.py

@@ -187,8 +187,8 @@ def export_transform_action(godot_node, animation_player, blender_object,
                 values_iter=frame_value_list,
             )
             track.set_parent_inverse(blender_object.matrix_parent_inverse)
-            if (godot_node.get_type()
-                    in ("SpotLight", "DirectionalLight", "Camera")):
+            if godot_node.get_type() in ("SpotLight", "DirectionalLight",
+                                         "Camera", "CollisionShape"):
                 track.is_directional = True
             animation_resource.add_track(track)
 

+ 5 - 1
io_scene_godot/converters/mesh.py

@@ -49,10 +49,14 @@ def export_mesh_node(escn_file, export_settings, node, parent_gd_node):
 
         mesh_node['mesh'] = "SubResource({})".format(mesh_id)
         mesh_node['visible'] = not node.hide
-        if not physics.has_physics(node) or not physics.is_physics_root(node):
+
+        # Transform of rigid mesh is moved up to its collision
+        # shapes.
+        if not physics.has_physics(node):
             mesh_node['transform'] = node.matrix_local
         else:
             mesh_node['transform'] = mathutils.Matrix.Identity(4)
+
         escn_file.add_node(mesh_node)
 
         export_object_link_material(

+ 5 - 10
io_scene_godot/converters/physics.py

@@ -4,15 +4,12 @@ object is exported. In blender, the object owns the physics. In Godot, the
 physics owns the object.
 """
 
-import math
 import logging
 import bpy
 import mathutils
 import bmesh
-from ..structures import NodeTemplate, InternalResource, Array
+from ..structures import NodeTemplate, InternalResource, Array, _AXIS_CORRECT
 
-
-AXIS_CORRECT = mathutils.Matrix.Rotation(math.radians(-90), 4, 'X')
 PHYSICS_TYPES = {'KinematicBody', 'RigidBody', 'StaticBody'}
 
 
@@ -65,11 +62,11 @@ def export_collision_shape(escn_file, export_settings, node, parent_gd_node,
     col_node = NodeTemplate(col_name, "CollisionShape", parent_gd_node)
 
     if parent_override is None:
-        col_node['transform'] = mathutils.Matrix.Identity(4) * AXIS_CORRECT
+        col_node['transform'] = mathutils.Matrix.Identity(4)
     else:
         parent_to_world = parent_override.matrix_world.inverted()
-        col_node['transform'] = (
-            parent_to_world * node.matrix_world * AXIS_CORRECT)
+        col_node['transform'] = parent_to_world * node.matrix_world
+    col_node['transform'] *= _AXIS_CORRECT
 
     rbd = node.rigid_body
 
@@ -239,9 +236,7 @@ def export_physics_properties(escn_file, export_settings, node,
         gd_node_ptr = gd_node_ptr.parent
     physics_gd_node = gd_node_ptr
 
-    export_collision_shape(
+    return export_collision_shape(
         escn_file, export_settings, node, physics_gd_node,
         parent_override=parent_rbd
     )
-
-    return parent_gd_node

+ 12 - 1
io_scene_godot/export_godot.py

@@ -28,7 +28,9 @@ import os
 import collections
 import functools
 import logging
+import math
 import bpy
+import mathutils
 
 from . import structures
 from . import converters
@@ -107,7 +109,9 @@ class GodotExporter:
                 parent_gd_node
             )
 
-        # Perform the export
+        # Perform the export, note that `exported_node.paren`t not
+        # always the same as `parent_gd_node`, as sometimes, one
+        # blender node exported as two parented node
         exported_node = exporter(self.escn_file, self.config, node,
                                  parent_gd_node)
 
@@ -117,6 +121,13 @@ class GodotExporter:
                     node, child['transform']
                 )
 
+        # CollisionShape node has different direction in blender
+        # and godot, so it has a -90 rotation around X axis,
+        # here rotate its children back
+        if exported_node.parent.get_type() == 'CollisionShape':
+            exported_node['transform'] *= (
+                mathutils.Matrix.Rotation(math.radians(90), 4, 'X'))
+
         # if the blender node is exported and it has animation data
         if exported_node != parent_gd_node:
             converters.ANIMATION_DATA_EXPORTER(

File diff suppressed because it is too large
+ 24 - 0
tests/reference_exports/action_animation/physics_animation.escn


+ 3 - 3
tests/reference_exports/armature/armature_with_physics.escn

@@ -43,9 +43,9 @@ sleeping = false
 transform = Transform(1.0, 0.0, 0.0, 0.0, -4.37114e-08, 1.0, 0.0, -1.0, -4.37114e-08, 0.0, 0.0, 0.0)
 shape = SubResource(1)
 
-[node name="Rig" type="Skeleton" parent="PhysicsPhysics"]
+[node name="Rig" type="Skeleton" parent="PhysicsPhysics/PhysicsCollision"]
 
-transform = Transform(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, -0.7, 0.0)
+transform = Transform(1.0, 0.0, 0.0, 0.0, 7.54979e-08, -1.0, 0.0, 1.0, 7.54979e-08, 0.0, -0.7, 0.0)
 bones/0/name = "Bone"
 bones/0/parent = -1
 bones/0/rest = Transform(1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0, 0.0, -0.1, 0.0)
@@ -83,7 +83,7 @@ bones/5/pose = Transform(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0,
 bones/5/enabled = true
 bones/5/bound_children = []
 
-[node name="Mesh" type="MeshInstance" parent="PhysicsPhysics/Rig"]
+[node name="Mesh" type="MeshInstance" parent="PhysicsPhysics/PhysicsCollision/Rig"]
 
 skeleton = NodePath("..:")
 mesh = SubResource(2)

+ 14 - 14
tests/reference_exports/mesh/physics.escn

@@ -171,11 +171,11 @@ collision_mask = 1
 transform = Transform(1.0, 0.0, 0.0, 0.0, -4.37114e-08, 1.0, 0.0, -1.0, -4.37114e-08, 0.0, 0.0, 0.0)
 shape = SubResource(1)
 
-[node name="Cube002" type="MeshInstance" parent="Cube002Physics"]
+[node name="Cube002" type="MeshInstance" parent="Cube002Physics/Cube002Collision"]
 
 mesh = SubResource(2)
 visible = true
-transform = Transform(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0)
+transform = Transform(1.0, 0.0, 0.0, 0.0, 7.54979e-08, -1.0, 0.0, 1.0, 7.54979e-08, 0.0, 0.0, 0.0)
 
 [node name="Cube001Physics" type="StaticBody" parent="."]
 
@@ -207,11 +207,11 @@ sleeping = false
 transform = Transform(1.0, 0.0, 0.0, 0.0, -4.37114e-08, 1.0, 0.0, -1.0, -4.37114e-08, 0.0, 0.0, 0.0)
 shape = SubResource(4)
 
-[node name="Sphere001" type="MeshInstance" parent="Sphere001Physics"]
+[node name="Sphere001" type="MeshInstance" parent="Sphere001Physics/Sphere001Collision"]
 
 mesh = SubResource(5)
 visible = true
-transform = Transform(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0)
+transform = Transform(1.0, 0.0, 0.0, 0.0, 7.54979e-08, -1.0, 0.0, 1.0, 7.54979e-08, 0.0, 0.0, 0.0)
 
 [node name="PlanePhysics" type="StaticBody" parent="."]
 
@@ -226,11 +226,11 @@ collision_mask = 1
 transform = Transform(1.0, 0.0, 0.0, 0.0, -4.37114e-08, 1.0, 0.0, -1.0, -4.37114e-08, 0.0, 0.0, 0.0)
 shape = SubResource(6)
 
-[node name="Plane" type="MeshInstance" parent="PlanePhysics"]
+[node name="Plane" type="MeshInstance" parent="PlanePhysics/PlaneCollision"]
 
 mesh = SubResource(7)
 visible = true
-transform = Transform(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0)
+transform = Transform(1.0, 0.0, 0.0, 0.0, 7.54979e-08, -1.0, 0.0, 1.0, 7.54979e-08, 0.0, 0.0, 0.0)
 
 [node name="CubePhysics" type="RigidBody" parent="."]
 
@@ -249,11 +249,11 @@ sleeping = false
 transform = Transform(1.0, 0.0, 0.0, 0.0, -4.37114e-08, 1.0, 0.0, -1.0, -4.37114e-08, 0.0, 0.0, 0.0)
 shape = SubResource(8)
 
-[node name="Cube" type="MeshInstance" parent="CubePhysics"]
+[node name="Cube" type="MeshInstance" parent="CubePhysics/CubeCollision"]
 
 mesh = SubResource(9)
 visible = true
-transform = Transform(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0)
+transform = Transform(1.0, 0.0, 0.0, 0.0, 7.54979e-08, -1.0, 0.0, 1.0, 7.54979e-08, 0.0, 0.0, 0.0)
 
 [node name="SpherePhysics" type="RigidBody" parent="."]
 
@@ -272,22 +272,22 @@ sleeping = false
 transform = Transform(1.0, 0.0, 0.0, 0.0, -4.37114e-08, 1.0, 0.0, -1.0, -4.37114e-08, 0.0, 0.0, 0.0)
 shape = SubResource(10)
 
-[node name="Sphere" type="MeshInstance" parent="SpherePhysics"]
+[node name="Sphere" type="MeshInstance" parent="SpherePhysics/SphereCollision"]
 
 mesh = SubResource(11)
 visible = true
-transform = Transform(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0)
+transform = Transform(1.0, 0.0, 0.0, 0.0, 7.54979e-08, -1.0, 0.0, 1.0, 7.54979e-08, 0.0, 0.0, 0.0)
 
 [node name="Sphere002Collision" type="CollisionShape" parent="SpherePhysics"]
 
 transform = Transform(1.0, 1.30385e-08, 2.98023e-08, 1.49012e-08, -2.97416e-08, 1.0, 2.42144e-08, -1.0, -5.1162e-08, 0.565972, 0.864336, 0.166554)
 shape = SubResource(12)
 
-[node name="Sphere002" type="MeshInstance" parent="SpherePhysics/Sphere"]
+[node name="Sphere002" type="MeshInstance" parent="SpherePhysics/Sphere002Collision"]
 
 mesh = SubResource(13)
 visible = true
-transform = Transform(1.0, -4.47035e-08, -5.96046e-08, -7.45058e-08, 1.0, 2.98023e-08, 5.96046e-08, 2.98023e-08, 1.0, 0.565971, 0.864337, 0.166554)
+transform = Transform(1.0, 0.0, 0.0, 0.0, 7.54979e-08, -1.0, 0.0, 1.0, 7.54979e-08, 0.0, 0.0, 0.0)
 
 [node name="Sphere003Physics" type="RigidBody" parent="."]
 
@@ -306,11 +306,11 @@ sleeping = false
 transform = Transform(1.0, 0.0, 0.0, 0.0, -4.37114e-08, 1.0, 0.0, -1.0, -4.37114e-08, 0.0, 0.0, 0.0)
 shape = SubResource(14)
 
-[node name="Sphere003" type="MeshInstance" parent="Sphere003Physics"]
+[node name="Sphere003" type="MeshInstance" parent="Sphere003Physics/Sphere003Collision"]
 
 mesh = SubResource(13)
 visible = true
-transform = Transform(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0)
+transform = Transform(1.0, 0.0, 0.0, 0.0, 7.54979e-08, -1.0, 0.0, 1.0, 7.54979e-08, 0.0, 0.0, 0.0)
 
 [node name="Lamp" type="OmniLight" parent="."]
 

BIN
tests/test_scenes/action_animation/physics_animation.blend


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