Skip to content

Instantly share code, notes, and snippets.

@Ending2015a
Created June 5, 2024 11:55
Show Gist options
  • Save Ending2015a/8b20b6a4a1fd11cce0f89ecde1ca5265 to your computer and use it in GitHub Desktop.
Save Ending2015a/8b20b6a4a1fd11cce0f89ecde1ca5265 to your computer and use it in GitHub Desktop.
Convert rigid transformations (TRS) from coordinate system A to coordinate system B
# this program demonstrate how to cnoevrt TRS from
# blender's coordinate to Unity's coordinate.
import numpy as np
from scipy.spatial.transform import Rotation
blender_west = [1, 0, 0]
blender_up = [0, 0, 1]
blender_north= [0, -1, 0]
blender_coord = np.array([
blender_west, blender_up, blender_north
], dtype=np.float64).T
unity_west = [1, 0, 0]
unity_up = [0, 1, 0]
unity_north = [0, 0, -1]
unity_coord = np.array([
unity_west, unity_up, unity_north
], dtype=np.float64).T
b2u = unity_coord @ np.linalg.inv(blender_coord)
trans = np.array([-8.1831, 5.3352, 2.2994])
rot = np.array([14.1, -36.6, 47.3])
scale = np.array([3.0, 4.0, 2.0])
print("Translation (Blender):", trans)
print("Rotation (Blender):", rot)
print("Scale (Blender):", scale)
Rb = Rotation.from_euler('xyz', rot, degrees=True).as_matrix()
# change of basis
Ru = b2u @ Rb @ np.linalg.inv(b2u)
z, x, y = Rotation.from_matrix(Ru).as_euler('zxy', degrees=True) # unity use zxy-order
unity_rot = np.array([x, y, z])
print("Translation (Unity):", trans @ b2u.T)
print("Rotation (Unity):", unity_rot)
print("Scale (Unity):", scale @ b2u.T)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment