Incorrect normals in my exporter

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
Razor
Posts: 0
Joined: Mon Jun 05, 2006 7:46 am

Incorrect normals in my exporter

Post by Razor »

Hi,
I'm making a custom exporter, and I'm having a bit of trouble getting vertex normals to come out properly. It seems to depend on the rotation of the object. If I create a sphere in the top viewport and export, it works perfectly. But if I create it in the front viewport, the y and z axes seem to be negated. I am transforming the the mesh using:

Code: Select all

m.transform(ob.getMatrix(), 1)
which the docs say should recalculate the normals. Do I have to convert them to world space myself somehow?

Thanks for your time. Oh, and I don't know python, I've been picking it up as I go along. So that could be the problem :wink:.

EDIT: Never mind, I just threw in a calcNormals() call and it seems to work. Thanks anyway! I won't delete the post though, on the off chance that someone can shed some light on the subject.

macouno
Posts: 0
Joined: Tue Nov 18, 2003 3:37 am

Post by macouno »

Yes you have to get the normals and vert locations in worldspace yourself if you want that.


Here's two functions that allow you to convert local coords into world coords.

Code: Select all

# Apply a matrix to a vert and return a vector.
def apply_transform(verts, matrix):
	x, y, z = verts
	xloc, yloc, zloc = matrix[3][0], matrix[3][1], matrix[3][2]
	return Mathutils.Vector(
	x*matrix[0][0] + y*matrix[1][0] + z*matrix[2][0] + xloc,
	x*matrix[0][1] + y*matrix[1][1] + z*matrix[2][1] + yloc,
	x*matrix[0][2] + y*matrix[1][2] + z*matrix[2][2] + zloc)

# Apply a matrix to a normal and return a vector.
def normal_transform(normal, matrix):
	matrix = matrix.rotationPart()
	x, y, z = normal
	return Mathutils.Vector(
	x*matrix[0][0] + y*matrix[1][0] + z*matrix[2][0],
	x*matrix[0][1] + y*matrix[1][1] + z*matrix[2][1],
	x*matrix[0][2] + y*matrix[1][2] + z*matrix[2][2])
Before you do this you have to get the worldspace matrix for that object like:

Code: Select all

tarObj = Object.Get('objectname')
	matrix = Mathutils.Matrix(tarObj.getMatrix('worldspace'));
that should help you along.

ideasman
Posts: 0
Joined: Tue Feb 25, 2003 2:37 pm

Post by ideasman »

a cheating way willian and I added

me= ob.data
me.transform(ob.matrix)
# Now just use the verts and normals

Post Reply