1
$\begingroup$

I want to convert an euler or quaternion rotation from Blender to an euler or quaternion rotation on Unity.

The goal is not to export an object from Blender to Unity, but only to convert the rotation values needed to make a tool and I need to do this on the Blender side and not Unity.

Since Blender is right hand oriented and Unity is left hand oriented, I started by looking at how to convert from right hand to left hand. So I came across this article and transposed it into python in Blender (see below): https://butterflyofdream.wordpress.com/2016/07/05/converting-rotation-matrices-of-left-handed-coordinate-system/ The problem is that the rotation I get is not the right one. I don't know if it's because the Y and Z axes are reversed between Blender and Unity (I tried to reverse the values but the result is not good), I also saw that there was apparently a 90 degree offset on the X axis but I don't see if I should apply it before or after the right hand to left hand conversion.

I also tried the solution here but it doesn't work for all rotation source : Convert from Blender rotations to Right Handed Y-Up rotations (Maya, Houdini, ...)

Does anyone have a formula or algorithm for doing this?

Thank you!

import bpy
import bpy_extras
import math
import mathutils
from math import degrees, radians, cos, sin
    
# LEFT HANDED
def rotLX(a):
    mat = mathutils.Matrix()
    mat[0].xyz = 1,         0,          0
    mat[1].xyz = 0,         cos(a),     sin(a)
    mat[2].xyz = 0,         -sin(a),    cos(a)

    return mat

def rotLY(a):
    mat = mathutils.Matrix()
    mat[0].xyz = cos(a),    0,      -sin(a)
    mat[1].xyz = 0,         1,      0
    mat[2].xyz = sin(a),    0,      cos(a)

    return mat

def rotLZ(a):
    mat = mathutils.Matrix()
    mat[0].xyz = cos(a),    sin(a),     0
    mat[1].xyz = -sin(a),   cos(a),     0
    mat[2].xyz = 0,         0,          1

    return mat

# RIGHT HANDED
def rotRX(a):
    mat = mathutils.Matrix()
    mat[0].xyz = 1,         0,          0
    mat[1].xyz = 0,         cos(a),     -sin(a)
    mat[2].xyz = 0,         sin(a),     cos(a)

    return mat

def rotRY(a):
    mat = mathutils.Matrix()
    mat[0].xyz = cos(a),    0,          sin(a)
    mat[1].xyz = 0,         1,          0
    mat[2].xyz = -sin(a),   0,          cos(a)

    return mat

def rotRZ(a):
    mat = mathutils.Matrix()
    mat[0].xyz = cos(a),    -sin(a),    0
    mat[1].xyz = sin(a),    cos(a),     0
    mat[2].xyz = 0,         0,          1

    return mat

obj = bpy.data.objects['Cylinder']
euler_rotation = obj.rotation_euler

rRx = rotRX(euler_rotation.x)
rRy = rotRY(euler_rotation.y)
rRz = rotRZ(euler_rotation.z)

rR = rRz @ rRy @ rRx
resMatR = rR

euler_resR = resMatR.to_euler()
print("\nResult Right Hand : " + str(degrees(euler_resR.x)) + ", " + str(degrees(euler_resR.y)) + ", " + str(degrees(euler_resR.z)))

rLx = rotLX(euler_rotation.x)
rLy = rotLY(euler_rotation.y)
rLz = rotLZ(euler_rotation.z)

rL = rLz @ rLy @ rLx
resMatL = rL
euler_resL = resMatL.to_euler()
print("\nResult Left Hand : " + str(degrees(euler_resL.x)) + ", " + str(degrees(euler_resL.y)) + ", " + str(degrees(euler_resL.z)))
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.