Notifications
Clear all

[Closed] Creating mesh faces

Hi All,
This is a new area for me, so I’m not sure about one or two things.

     I'm looping through all the faces in a poly object, and creating new mesh objects per face. The trouble is, some of the faces are flipped ... and I'm not sure what the pattern is to define that or not.

 arrMesh = #()
  arrFaces = #()
  createMesh = true
   
   for f = 1 to polyOp.getNumFaces $ do
        	(
        	-- verts	
        		baVerts		= polyOp.getVertsUsingFace $ #{f}
        		arrFaceVerts	= for v in baVerts collect polyOp.getVert $ v
        		append arrFaces arrFaceVerts
        		
        	-- create mesh
        		if createMesh do
        			(
        			if arrFaceVerts.count == 3 then
        				m = mesh vertices:arrFaceVerts faces:#([1,2,3])
        			else
        				m = mesh vertices:arrFaceVerts faces:#([1,2,3],[4,3,2])
        			append arrMesh m
        			)
        	)
     A script and screen shot are attached if anyone can point me in the right direction. I suspect that it has to do with the order of vertex creation (clockwise or not) but the results don't (I think) support my hypothesis.
     
     Thanks,
     Dave
5 Replies

Hi Dave,

I don’t have much time to look through your script but I can confirm that it is the orde of vertices that decides the normal direction of a face.

don’t know how useful that is to you!

J.

Hey Josh (and any others reading!),
Thanks for the reply. Take a quick squiz at the thumbnail… you can see that MOST of the faces get correctly created. I would ASSUME that there was some kind of order on original creation of teh torus, so I can’t work out why only some of the faces turn out right.

A teapot was even more screwy!

Does anyone have any algorithms to order the verts in relation to the face normal?

Much obliged!
Dave

Hey Dave,

After reading through your script, I noticed that you use polyOp.getVertsUsingFace. This doesn’t (always) give the correct order of vertices, whereas polyOp.getFaceVerts does. So I replaced that line with that.

After doing that, you have to modify the order you use to build the faces, which sould be #([1,2,3], [3,4,1]).

Farewell,
Light

1 Reply
(@halfvector)
Joined: 11 months ago

Posts: 0

Yes, that’s right. I’ve modified the script so you can create meshes for an arbitrary number of face triangles (for example it will triangulate a polygon conformed by six vertices).

fn createObjPerFace obj = (

	local meshes = #()

	-- Number of faces of the original object
	local numFaces = polyOp.getNumFaces obj

	-- Loop through all faces
	for f = 1 to numFaces do (

		-- Indices to the vertices for the current face
		indices = polyOp.getFaceVerts obj f

		-- Face vertices
		verts = for idx in indices collect (polyOp.getVert obj idx)
		
		-- Temporary indices
		local v1, v2, v3		
		-- Array of indices that conform the new face (mesh)
		local meshFaces = #()
		-- Number of faces (triangles) to create
		local numMeshFaces = verts.count - 2

		-- Loop through the faces of the new mesh and create the indices
		for nFace = 1 to numMeshFaces do (

			if nFace == 1 then (
				-- If this is the first face, we know the indices
				v1 = 1; v2 = 2; v3 = 3
			)
			else (
				-- Else, we have to increment them based upon previous indices
				v2 += 1; v3 += 1
			)

			-- Append the current face to the array of faces
			append meshFaces [v1,v2,v3]
		)
		
		-- Create and append the new mesh to our list of meshes
		append meshes (mesh vertices:verts faces:meshFaces)
	)

	-- Return the mesh list
	return meshes
)

I think it works.

Originally Posted by Light
After reading through your script, I noticed that you use polyOp.getVertsUsingFace. This doesn’t (always) give the correct order of vertices, whereas polyOp.getFaceVerts does. So I replaced that line with that.

[i]

Thanks Guys,
That was exactly the sort of thing I was hoping to find out on the board.
Cheers!
Dave
[/i]