Triangulating a mesh?


(Shagwana) #1

I am trying to write a export script for blitzbasic .b3d file format (www.blitzbasic.com) and there file format does not support quads.

Is there a way (or script) already to triangulate a given face?. Failing that, im after a inteligent way to convert a given quad face to two triangles.


if len(face.v)>3:
  #need to triangulate 
else:
  #its cool!

(just incase you was wondering what i was on about :wink:


(theeth) #2

Ctrl-T with the faces selected in edit mode.

Martin


(Shagwana) #3

Is there a way to do that from inside python?


(eeshlo) #4

As far as I know, work was started with the new python API (225) to triangulate any ngon mesh, but I don’t think it made it to the actual release version. In any case, triangulating a quad is very easy, you just have to make sure you connect vertices in the right order to preserve the face normal. Which for a blender quad with vertex indices (0,1,2,3) would be (0,1,2) for triangle 1 and (2,3,0) for triangle 2.
Just in case you need a code example:


import Blender

ob = Blender.Object.Get(meshname)
me = ob.data
nme = Blender.NMesh.GetRaw()

for v in me.verts:
	nv = Blender.NMesh.Vert(v.co[0], v.co[1], v.co[2])
	nme.verts.append(nv)

# creates new all-triangle mesh
# ignores single vertices and edges
for f in me.faces:

	# number of vertices used for this face
	numv = len(f.v)

	# if triangle, copy as is, else, if quad, create two triangles
	if numv==3:

		nf = Blender.NMesh.Face()
		nf.v.append(nme.verts[f.v[0].index])
		nf.v.append(nme.verts[f.v[1].index])
		nf.v.append(nme.verts[f.v[2].index])
		nme.faces.append(nf)

	elif numv==4:

		nf = Blender.NMesh.Face()
		nf.v.append(nme.verts[f.v[0].index])
		nf.v.append(nme.verts[f.v[1].index])
		nf.v.append(nme.verts[f.v[2].index])
		nme.faces.append(nf)

		nf = Blender.NMesh.Face()
		nf.v.append(nme.verts[f.v[2].index])
		nf.v.append(nme.verts[f.v[3].index])
		nf.v.append(nme.verts[f.v[0].index])
		nme.faces.append(nf)

Blender.NMesh.PutRaw(nme, "TRIMESH")
Blender.Window.RedrawAll()

Which creates a triangulated mesh from whatever mesh you gave as argument (ignores rotation,location & scaling)


(Shagwana) #5

Thanks a million :smiley: