I have topographical data which is height on a square lat/lon grid and I've generated a mesh with the script shown below. My polygons are all quadrangles (close to, but not quite squares in this case) and some are potato-chip bent pretty badly. I think this causes problems with vertex or face normals during rendering.

Is dividing each rectangle to two triangles likely to address this problem? Is there a magic way to convert to triangles with a simple python method?

I could probably modify the script loop to generate pairs of triangles as well if necessary, just need to make sure to go around each triangle in the same direction.

But a quad can be cut into triangles two different ways, and there is probably a good way and a bad way, depending on the particular shape of the potato-chipped quad. So if I did it manually in my loop, I'd have to test each one first...

verts = list(zip(scale_x*X.flatten(), scale_y*Y.flatten(), scale_z*Z.flatten()))

faces = []
for i in range(ny-1):
    for j in range(nx-1):
        v1 = i*(nx) + j
        v2 = v1 + 1
        v3 = v2 + nx
        v4 = v3 - 1
        faces.append((v1, v2, v3, v4))

name = 'topo'
me = bpy.data.meshes.new(name)
ob = bpy.data.objects.new(name, me)

me.from_pydata(verts, [], faces)

bpy.data.objects[name].select = False
bpy.data.objects[name].select = True


enter image description here

Wireframe, Rendered, Texture


Is there a magic way to convert to triangles with a simple python method?

Yes, there is a method.

Based on @gandolf3's suggestion in chat I've tested bpy.ops.mesh.quads_convert_to_tris() and indeed it shows an improvement.

Subdivisioning the quad mesh first can be explored as well, either everywhere, or in some optimized way because the final topographic map is pretty big.

# from https://blender.stackexchange.com/a/35437/5334
ob.select = True
bpy.context.scene.objects.active = ob

bpy.ops.mesh.quads_convert_to_tris(quad_method='BEAUTY', ngon_method='BEAUTY')  # Convert to Triangles


enter image description here

Initial quads, final triangles


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.