# Should I convert my quad mesh to triangles? If so, can I do it in this script?

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)

bpy.context.scene.objects.link(ob)
me.from_pydata(verts, [], faces)

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

bpy.ops.object.shade_smooth()


Wireframe, Rendered, Texture

## 1 Answer

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
bpy.ops.object.select_all(action='DESELECT')
ob.select = True
bpy.context.scene.objects.active = ob

bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.quads_convert_to_tris(quad_method='BEAUTY', ngon_method='BEAUTY')  # Convert to Triangles
bpy.ops.object.mode_set(mode='OBJECT')

bpy.ops.object.shade_smooth()


Initial quads, final triangles