Exporting Normals and Cooridinates

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
DanteDragon
Posts: 0
Joined: Wed Jan 09, 2013 2:04 pm
Location: United States

Exporting Normals and Cooridinates

Post by DanteDragon » Wed Jan 09, 2013 2:13 pm

I thought I'd be nice and post my findings. I'm not sure if this is a bug in Blender API or if it is intentional.

For a few days, I was going nuts because my model exports were not getting proper lighting in my game engine. It turned out it was because the normals were not exporting the correct cooridinates. After some trial and error I found this to work. Note: I'm using OpenGL and Right-Handed Cooridinates (Y is Up, -Z is forward).

vertex.normal[0] is the X Cooridinate
vertex.normal[1] is the Negative Z Cooridinate
vertex.normal[2] is the Y Cooridinate

I suspect this has something to do with the fact that Z is up in the blender environment. Though exporting with 'Apply Modifiers', which I took from the OBJ export script, the modifiers do not seem to apply to Normal Cooridinates.

CoDEmanX
Posts: 0
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Post by CoDEmanX » Wed Jan 09, 2013 7:11 pm

some exporters provide an axis option for coordinate system conversion.

But you could also do Mesh.transform(correction_matrix) before export, it will be destructive though.

The rough way would be to do something like this:

Code: Select all

me = bpy.context.object.data
mat=Matrix(((1,0,0,0),(0,0,-1,0),(0,1,0,0),(0,0,0,1)))
me.transform(mat)
me.update
I'm sitting, waiting, wishing, building Blender in superstition...

DanteDragon
Posts: 0
Joined: Wed Jan 09, 2013 2:04 pm
Location: United States

Post by DanteDragon » Thu Jan 10, 2013 1:00 am

Thanks for the reply. I forgot to mention that I also have used the code for setting coordinate system on export from the OBJ exporter script that is packaged with Blender.

So, all the Vertex Position Coordinates (vertex.co) are correct, but the Normal Coordinates are not updated. So exporting vertex.co and vertex.normal in this situation are 2 different coordinate systems.

CoDEmanX
Posts: 0
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Post by CoDEmanX » Thu Jan 10, 2013 1:41 am

that sounds like a serious bug, you should submit a bug report!
I'm sitting, waiting, wishing, building Blender in superstition...

DanteDragon
Posts: 0
Joined: Wed Jan 09, 2013 2:04 pm
Location: United States

Post by DanteDragon » Thu Jan 10, 2013 1:51 am

Good news, and hopefully this will help any others that had this difficulty. Hopefully I'm not the only one :oops:

This is from the OBJ exporter, to get the coordinate system the user desires on export:

Code: Select all

global_matrix = (global_matrix *     
    axis_conversion(to_forward=self.axis_forward,
    to_up=self.axis_up,
    ).to_4x4())
Skipping a little, 'ob' is an object from 'context.scene.objects', or 'context.selected_objects':

Code: Select all

curMesh = ob.to_mesh(context.scene, self.EXPORT_APPLY_MODIFIERS, 'PREVIEW')


And where it applies the transform:

Code: Select all

curMesh.transform(global_matrix * ob.matrix_world)
The step I was missing is that you have to call a function to specifically updates the Vertex Normals. That's why my position cooridinates were fine, but the normals were not:

Code: Select all

curMesh.calc_normals()
Now each vector/coordinate in curMesh.vertex.co[:] and curMesh.vertex.normal[:] are in the same coordinate system.

Post Reply