How do i calculate face normal to the correct direction

How do i calculate face normal to the correct direction

nir.poolinat
Participant Participant
5,389 Views
8 Replies
Message 1 of 9

How do i calculate face normal to the correct direction

nir.poolinat
Participant
Participant

if i get vector xyz form "FACE_NORMAL 0: 0.210450 -0.971706 -0.107230" 

 

How do i calculate this face normal to the correct direction or not 

 

0 Likes
5,390 Views
8 Replies
Replies (8)
Message 2 of 9

Anonymous
Not applicable

A triangle normal is generated using cross product between vectors. Lets say you have a triangle with vertices p1, p2, p3.

If you generate vectors v1 = p2 - p1, and v2 = p3 - p1, then you perform a cross product of this two vectors and normalize the resulting vector. 

 

https://www.khronos.org/opengl/wiki/Calculating_a_Surface_Normal

Message 3 of 9

nir.poolinat
Participant
Participant

frist, thank for  reply  i new coding here 

so the face normal  return from < polyInfo -fn  > can't be use right because it should  use 'u' and 'v'vector form vertex
or that face normal< polyInfo -fn  > are vertex position? 

0 Likes
Message 4 of 9

Anonymous
Not applicable

The normals that polyInfo -fn returns are the normals that your object has. They could be right or not. Firstly because user is able to invert the normals if he wants to. Thus, Maya will return the inverted normals.

What are you trying to do?

0 Likes
Message 5 of 9

nir.poolinat
Participant
Participant

i want to create check list that detect which face is not correct direction

so i want to try to use another version from i code because it too slow when it check every face on polygon

 

def checkFaceFlip(face_list):
	'''
	return : flipface name and mesh name
	'''
	for face in face_list:
		uvs = []
		vtxFaces = cmds.ls(cmds.polyListComponentConversion(face, toVertexFace=True), flatten=True)
		for vtxFace in vtxFaces:
			uv = cmds.polyListComponentConversion(vtxFace, fromVertexFace=True, toUV=True)
			uvs.append( uv[0] )

		#get edge vectors and cross them to get the uv face normal
		uvAPos = cmds.polyEditUV(uvs[0], q=True)
		uvBPos = cmds.polyEditUV(uvs[1], q=True)
		uvCPos = cmds.polyEditUV(uvs[2], q=True)
		uvAB = dt.Vector([uvBPos[0] - uvAPos[0], uvBPos[1] - uvAPos[1]])
		uvBC = dt.Vector([uvCPos[0] - uvBPos[0], uvCPos[1] - uvBPos[1]])

		# if face is wrong direction
		if not uvAB.cross(uvBC) * dt.Vector([0, 0, 1]) >= 0:
			print face # show first face which broken
			mesh = face.split('.')[0]
			return mesh
0 Likes
Message 6 of 9

Anonymous
Not applicable

Regarding the performance of the script, i guess i would try using Maya API instead of Python commands. That way, you could store VertexList, UVList and Normals in 3 lists and use them in your algorithm, instead of make so many calls per polygon.

https://download.autodesk.com/us/maya/2011help/API/class_m_fn_mesh.html

0 Likes
Message 7 of 9

nir.poolinat
Participant
Participant

thank for help i will try it

0 Likes
Message 8 of 9

nir.poolinat
Participant
Participant

still not understand  if i cross puduct will get the normal then what some thing  correct direction i check with?





0 Likes
Message 9 of 9

Anonymous
Not applicable

Well, first of all, i can't say that a normal that is pointing inward is bad. For instance, lets imagine that i want to have an sphere simulating the universe, with a simple night sky texture applied. Maybe i want the normals pointing inward. 

 

So i just can say that maybe you could check that all polygons are clockwise or anti-clockwise, because the ordering of the vertices is what determines the normal at the end

https://stackoverflow.com/questions/1165647/how-to-determine-if-a-list-of-polygon-points-are-in-cloc...

 

Maybe someone else is able to give us a better answer. I am curious now 🙂