How can I decompose a matrix for use in DirectX? I'm writing a custom exporter from Blender that will work with DirectX. I am using as a guideline the DirectX exporter found in Blender 2.76. The Blender export script for DirectX uses the following conversion matrix:

code:

# SystemMatrix converts from right-handed, z-up to the target coordinate system
self.SystemMatrix = Matrix()

if self.Config.CoordinateSystem == 'LEFT_HANDED':
    self.SystemMatrix *= Matrix.Scale(-1, 4, Vector((0, 0, 1)))

if self.Config.UpAxis == 'Y':
    self.SystemMatrix *= Matrix.Rotation(radians(-90), 4, 'X')


matrix:

1, 0, 0, 0
0, 0, 1, 0
0, 1, 0, 0
0, 0, 0, 1


I want to write the components of this matrix so I decompose the matrix like so:

loc, rot, sca = self.SystemMatrix.decompose()


now I convert the rotation from quaternion to euler like so:

eul = rot.to_euler('XYZ')


write components
...

The problem is that when I try to recompose the matrix in my DirectX graphics application I get the wrong matrix...

code to recompose matrix:

Matrix m = Matrix.Scaling(sca.x, sca.y, sca.z) * Matrix.RotationYawPitchRoll(eul.x, eul.y, eul.z)
* Matrix.Translation(loc.x, loc.y, loc.z)


result:

0, 0, 1, 0
0, 1, 0, 0
1, 0, 0, 0
0, 0, 0, 1
In your engine you need to compose the matrix m like this:

Matrix m = Matrix.Translation(loc.x, loc.y, loc.z) * Matrix.RotationYawPitchRoll(eul.x, eul.y, eul.z) * Matrix.Scaling(sca.x, sca.y, sca.z)

# matrix_world = matLoc * matRot * matScale