Browse Source

fix transform animation of spotlight, directional light and camera

Jason0214 7 years ago
parent
commit
c69a69bea1

+ 19 - 10
io_scene_godot/converters/animation.py

@@ -5,7 +5,7 @@ import copy
 import bpy
 import bpy
 import mathutils
 import mathutils
 from . import armature
 from . import armature
-from ..structures import (NodeTemplate, NodePath,
+from ..structures import (NodeTemplate, NodePath, fix_directional_transform,
                           InternalResource, Array, fix_matrix)
                           InternalResource, Array, fix_matrix)
 
 
 LINEAR_INTERPOLATION = 1
 LINEAR_INTERPOLATION = 1
@@ -231,6 +231,12 @@ def export_transform_action(godot_node, animation_player,
 
 
                 default_frame = None
                 default_frame = None
 
 
+                # the fcurve location is matrix_basis.to_translation()
+                default_frame = TransformFrame(
+                    blender_object.matrix_basis,
+                    blender_object.rotation_mode
+                )
+
                 if object_path.startswith('pose'):
                 if object_path.startswith('pose'):
                     bone_name = blender_path_to_bone_name(object_path)
                     bone_name = blender_path_to_bone_name(object_path)
 
 
@@ -246,12 +252,6 @@ def export_transform_action(godot_node, animation_player,
                         pose_bone.matrix_basis,
                         pose_bone.matrix_basis,
                         pose_bone.rotation_mode
                         pose_bone.rotation_mode
                     )
                     )
-                else:
-                    # the fcurve location is matrix_basis.to_translation()
-                    default_frame = TransformFrame(
-                        blender_object.matrix_basis,
-                        blender_object.rotation_mode
-                    )
 
 
                 transform_frames_map[object_path] = [
                 transform_frames_map[object_path] = [
                     copy.deepcopy(default_frame)
                     copy.deepcopy(default_frame)
@@ -273,9 +273,18 @@ def export_transform_action(godot_node, animation_player,
             # object_path equals '' represents node itself
             # object_path equals '' represents node itself
 
 
             # convert matrix_basis to matrix_local(parent space transform)
             # convert matrix_basis to matrix_local(parent space transform)
-            normalized_frame_list = [
-                blender_object.matrix_parent_inverse *
-                x.to_matrix() for x in frame_list]
+            if (godot_node.get_type()
+                    in ("SpotLight", "DirectionalLight", "Camera")):
+                normalized_frame_list = [
+                    fix_directional_transform(
+                        blender_object.matrix_parent_inverse * x.to_matrix()
+                    ) for x in frame_list
+                ]
+            else:
+                normalized_frame_list = [
+                    blender_object.matrix_parent_inverse *
+                    x.to_matrix() for x in frame_list
+                ]
 
 
             track_path = NodePath(
             track_path = NodePath(
                 animation_player.parent.get_path(),
                 animation_player.parent.get_path(),

+ 3 - 7
io_scene_godot/converters/simple_nodes.py

@@ -6,11 +6,7 @@ Anything more complex should go in it's own file
 import math
 import math
 import logging
 import logging
 import mathutils
 import mathutils
-from ..structures import NodeTemplate
-
-# Used to correct spotlights and cameras, which in blender are Z-forwards and
-# in Godot are Y-forwards
-AXIS_CORRECT = mathutils.Matrix.Rotation(math.radians(-90), 4, 'X')
+from ..structures import NodeTemplate, fix_directional_transform
 
 
 
 
 def export_empty_node(escn_file, export_settings, node, parent_gd_node):
 def export_empty_node(escn_file, export_settings, node, parent_gd_node):
@@ -43,7 +39,7 @@ def export_camera_node(escn_file, export_settings, node, parent_gd_node):
         cam_node['projection'] = 1
         cam_node['projection'] = 1
         cam_node['size'] = camera.ortho_scale
         cam_node['size'] = camera.ortho_scale
 
 
-    cam_node['transform'] = node.matrix_local * AXIS_CORRECT
+    cam_node['transform'] = fix_directional_transform(node.matrix_local)
     escn_file.add_node(cam_node)
     escn_file.add_node(cam_node)
 
 
     return cam_node
     return cam_node
@@ -93,7 +89,7 @@ def export_lamp_node(escn_file, export_settings, node, parent_gd_node):
     if light_node is not None:
     if light_node is not None:
         # Properties common to all lights
         # Properties common to all lights
         light_node['light_color'] = mathutils.Color(light.color)
         light_node['light_color'] = mathutils.Color(light.color)
-        light_node['transform'] = node.matrix_local * AXIS_CORRECT
+        light_node['transform'] = fix_directional_transform(node.matrix_local)
         light_node['light_negative'] = light.use_negative
         light_node['light_negative'] = light.use_negative
         light_node['light_specular'] = 1.0 if light.use_specular else 0.0
         light_node['light_specular'] = 1.0 if light.use_specular else 0.0
         light_node['light_energy'] = light.energy
         light_node['light_energy'] = light.energy

+ 10 - 0
io_scene_godot/structures.py

@@ -3,6 +3,7 @@
 This file contains classes to help dealing with the actual writing to the file
 This file contains classes to help dealing with the actual writing to the file
 """
 """
 import os
 import os
+import math
 import collections
 import collections
 import mathutils
 import mathutils
 
 
@@ -326,6 +327,15 @@ def fix_matrix(mtx):
     return trans
     return trans
 
 
 
 
+_AXIS_CORRECT = mathutils.Matrix.Rotation(math.radians(-90), 4, 'X')
+
+
+def fix_directional_transform(mtx):
+    """Used to correct spotlights and cameras, which in blender are
+    Z-forwards and in Godot are Y-forwards"""
+    return mtx * _AXIS_CORRECT
+
+
 # ------------------ Implicit Conversions of Blender Types --------------------
 # ------------------ Implicit Conversions of Blender Types --------------------
 def mat4_to_string(mtx):
 def mat4_to_string(mtx):
     """Converts a matrix to a "Transform" string that can be parsed by Godot"""
     """Converts a matrix to a "Transform" string that can be parsed by Godot"""

File diff suppressed because it is too large
+ 9 - 0
tests/reference_exports/animation_spot_light_transform.escn


BIN
tests/test_scenes/animation_spot_light_transform.blend


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