# Export transformation from Blender to OpenGL

I have custom exporter which exports data from blender to opengl app.

To convert meshes to opengl space I used:

global_matrix = axis_conversion(to_forward="-Z", to_up="Y").to_4x4()


Then when I export mesh I call mesh.transform(global_matrix) and vertex position is correct.

Each object I export as reference to mesh and transform matrix. To convert transform matrix I use:

matrix_world = global_matrix * obj.matrix_world * global_matrix


If I set axis_conversion forward to Z it works (but model is not facing camera), but -Z have wrong results.

Bellow is scene in blender, there are two triangle shaped objects and three cones.

This is wrong image, triangles are placed correctly but cones are upside down (used forward_axis -Z)

This image is shows objects exported with correct positions but wrong axis (used Z instead of -Z)

How can I fix this? Why are the results wrong with -Z?

• Shouldn't it be matrix_world = global_matrix.transposed() * obj.matrix_world * global_matrix? – maddin45 Mar 5 '15 at 17:35
• You are right! I changed it to matrix_world = global_matrix * obj.matrix_world * global_matrix.transposed() and it is working – Bobanini Mar 5 '15 at 18:37
• @maddin45 thanks! If you write suggestion as answer I will select it. – Bobanini Mar 5 '15 at 18:50

I made a mistake in my comment to your question, but here is the correct version:

Also, as you have found out yourself, it is the second global_matrix that needs to be transposed.

The correct formula is:

matrix_world = global_matrix * obj.matrix_world * global_matrix.transpose()


However this only works if global_matrix is just a rotation (which it is in your case). In general what you need to calculate is not the transpose of the matrix, but the inverse. In the case of rotation matrices the inverse is equal to the transpose.

So the general case looks like this:

matrix_world = global_matrix * obj.matrix_world * global_matrix.inverse()