exporting normals

(iamamoocow) #1

Hello all, though I suspect this will just be answered by eeshlo fellow… (good work btw)

I’m trying to export the normals from faces. (Is that at all related to uv coords? what are they btw?)

I’m bringing them into Java3D, and I need to create an XYZ vector for the face normals.

thanks in advance


(Jamesk) #2

Check the VirtuaLight exporter by Jano. There’s a function in it called calcNormal performing the stuff you need. It goes something like this:

def calcNormal(face, verts, n):
	# If it's a quad, calculate normal from diagonal instead
	# Assume verts are sorted in circular fasion (i.e. next to each other)
	m = (3 * n) | 1    # when n=0 (tri), m=1; when n=1 (quad), m=3
	vectorU = [verts[face[0]][0] - verts[face[2]][0],
	           verts[face[0]][1] - verts[face[2]][1],
	           verts[face[0]][2] - verts[face[2]][2]]
	vectorV = [verts[face[n]][0] - verts[face[m]][0],
	           verts[face[n]][1] - verts[face[m]][1],
	           verts[face[n]][2] - verts[face[m]][2]]
	normal = [(vectorU[1] * vectorV[2]) - (vectorU[2] * vectorV[1]),
	          (vectorU[2] * vectorV[0]) - (vectorU[0] * vectorV[2]),
	          (vectorU[0] * vectorV[1]) - (vectorU[1] * vectorV[0])]
	nLength = sqrt((normal[0] ** 2) + (normal[1] ** 2) + (normal[2] ** 2))
	# divide by zero check
	if not nLength: return "stray"
	else: return (normal[0]/nLength, normal[1]/nLength, normal[2]/nLength)

(eeshlo) #3

I’m quite certain I’m not the only one who knows the answer to some questions, I just seem to be the one to answer more often than others maybe.
Anyway, a normal is the direction a face is pointing, iow. if you were to put your back against the face, the normal would be the direction when looking straight ahead. They are very important for all kinds of things like lighting for instance.
The code Jamesk gave you does work, however, the normals for triangles seem to be inverted, this doesn’t matter to VirtuaLight, but for your application it might be very important, so take that into account.

(Jamesk) #4

Good point well made.