PDA

View Full Version : averaging face normals to find vert normals?


3rd Dimentia
03-22-2008, 02:27 AM
Is this the way that others would go about finding vertex normals or is there a more elegant way?



clearlistener()

vertNum = 53
normalTotal = [0,0,0]

try(delete $cone01)catch()
try(delete $torus01)catch()

tor = torus radius1:50 name:"torus01"
convertTo tor PolyMeshObject

polyArray = (polyop.getFacesUsingVert tor vertNum)as array

for i = 1 to polyArray.count do
(
normalTotal += (polyop.getfacenormal tor polyarray[i])
)

tempMatrix = matrixFromNormal (normalTotal/polyArray.count)

co = cone height:50 radius1:5 name:"cone01"
co.rotation = inverse tempMatrix.Rotation
co.position = (polyop.getvert tor vertNum)

RobGalanakis
03-22-2008, 06:18 AM
Sort of absurd that EPoly doesn't have some of the basic things EMesh has, like getNormal. But as long as you are doing "convertTo tor PolyMeshObject", why not just convert it to a mesh instead, and then do "getNormal <mesh> <vert_index_integer>" ?

Edit: or apply an edit mesh modifier instead of converting to a mesh

3rd Dimentia
03-22-2008, 08:09 AM
I was also surprised that there wasn't a polyop.getVertNormal. This was just an exercise for me. I'm trying to familiarise myself with vectors and matrices and how to do stuff to/with them. So converting to mesh would sort of defeat the original purpose for this exercise. The torus that I created and converted in that script was just so I had something to work with. Plus I always like working with polys rather than meshes.

Thanks for your suggestion though.

I just wrote a different version of that script exercise that took the area of the surrounding polys into account and weighted the normals based on poly area before finding the average.

Feedback or comments? All welcome.


clearlistener()


-- if it doesn't exist, create plane to work with
if $plane01 == undefined then
(
newPoly = editable_mesh name:"Plane01"
convertTo newPoly PolyMeshObject
vertArray = #([-100,-100,0],[0,-100,0],[0,-100,-10],[-100,0,0],[0,0,0],[0,4.37114e-007,-10],[-100,100,0],[0,100,0],[0,100,-10])
(
for i = 1 to 9 do
(
polyop.createVert newPoly vertArray[i]
)
)
polyArray = #(#(1, 2, 5, 4),#(2, 3, 6, 5),#(4, 5, 8, 7),#(5, 6, 9, 8))
(
for i = 1 to 4 do
(
polyop.createPolygon newPoly polyArray[i]
)
)
update newPoly
)


vertNum = 5 --going to use vert 5 of the poly to add the cone to
normalTotal = [0,0,0] -- initialise normalTotal
polyArray = (polyop.getFacesUsingVert $Plane01 vertNum)as array -- create an array of all the faces that use the vert
normalArray = #() -- initialise the array that will store the face normals
faceAreaArray =#() -- initialise the array that will store the face areas
faceAreaTotal = 0 -- initialise the variable that will hold the face area total




for i = 1 to polyArray.count do
(
normalArray[i] = (polyop.getfacenormal $Plane01 polyarray[i]) -- collect normalArray values
faceAreaArray[i] = (polyop.getFaceArea $Plane01 polyarray[i]) -- collect faceArea values
faceAreaTotal += faceAreaArray[i] -- find the sum of all the face's area to be used to normalise the area values
)

for i = 1 to polyArray.count do
(
normalTotal += (normalArray[i]*(faceAreaArray[i]/faceAreaTotal))
)

tempMatrix = matrixFromNormal (normalTotal/polyArray.count)
co = cone height:50 radius1:5 name:"cone01"
co.rotation = inverse tempMatrix.Rotation
co.position = (polyop.getvert $plane01 5)

CGTalk Moderation
03-22-2008, 08:09 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.