Adding new meshes

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
MangoFusion
Posts: 0
Joined: Wed Nov 19, 2003 4:10 pm

Adding new meshes

Post by MangoFusion »

Hi there,

I have written an importer script for a particular model format.
However, when i take a look at the meshes i import with the script in blender, there are large numbers of faces that are pointing the wrong way!

The model format uses triangle strips, so i had to convert them into triangles, but that shouldn't be a problem, since the format also stores vertex normals for every vertex, and therefor i would assume that they would have had a noticable affect on the meshes (aka they wouldn't be covered with faces facing the wrong way).

Here is my code :

Code: Select all

		bmesh = NMesh.New()
		bmesh.setMode("NoVNormalsFlip","Optimal")
		bmesh.hasFaceUV(1)
		bmesh.hasVertexUV(1)
		bmesh.update(False)

		mesh.convertToTris() # Make sure we ONLY have triangles

		# Vertextes
		for vert in mesh.verts:
			bmesh.verts.append(NMesh.Vert(vert[0], vert[1], vert[2]))

		# Get sticky texture coords
		for count in range(0, len(mesh.tverts)):
			bmesh.verts[count].uvco[0] = mesh.tverts[count][0]
			bmesh.verts[count].uvco[1] = mesh.tverts[count][1]

		addMats = []
		for fac in mesh.primitives:
			verts = []

			# Import the face verts
			for vert in range(fac.firstElement, fac.firstElement+3):
				verts.append(bmesh.verts[mesh.indices[vert]])
			bmesh.faces.append(NMesh.Face(verts))
			del verts # cleanup
			
		# Normals
		for count in range(0, len(mesh.normals)):
			bmesh.verts[count].no[0] = mesh.normals[count][0]
			bmesh.verts[count].no[1] = mesh.normals[count][1]
			bmesh.verts[count].no[2] = mesh.normals[count][2]

		# Finally plonk it into blender
		object = NMesh.PutRaw(bmesh, name, False)
		print "Put Mesh %s" % (name)
		meshCount += 1
		return object


To convert the triangle strip into triangles, i simply keep going through its list of indices, incrementing the position by one and adding the triangle from that position.
e.g. 1234 -> 123 234

Any ideas?

der_ton
Posts: 0
Joined: Mon Oct 20, 2003 1:02 am

Post by der_ton »

Try this instead:
1234 -> 123 243

Facingness of tris is controlled by ordering of the vertices.
It's just a quick idea, maybe it'll do the trick. Won't be a big deal to change your script to test it... :)

MangoFusion
Posts: 0
Joined: Wed Nov 19, 2003 4:10 pm

Post by MangoFusion »

I tried that, and it seems to be working now.
Specifically, i added a flag that gets flipped every time a face is added (except for the first 2 faces), which controls which order i insert the indices.

However in some cases i still get the odd surface pointing the wrong way. But not as bad as before ;)

Post Reply