Sub topic 14
Auditing a Mesh with a Short Python Script
Assume you know basic Blender navigation, can switch to Edit Mode, and can run a script from Blender’s Scripting workspace. This chapter audits the base mesh used by a Subdivision Surface modifier. It does not replace inspecting the shaded result in the viewport.
What the audit measures
A mesh contains vertices, edges, and faces. The script counts these face types:
- A triangle has three vertices.
- A quad has four vertices.
- An n-gon has more than four vertices.
It also counts poles by vertex valence. Valence is the number of edges connected to a vertex. A valence-3 vertex is often called an E-pole. A vertex with valence 5 or higher is an N-pole. Regular interior vertices usually have valence 4.
These counts help locate topology that may create pinching or make edge flow difficult to control. The counts alone do not prove that a mesh is good or bad. A pole can be harmless on a flat area and troublesome near a curved feature.
Run the audit in Blender
- Select the mesh object you want to inspect.
- Switch to Edit Mode.
- Open the Scripting workspace.
- Create a new text block and paste the script below.
- Click Run Script.
- Read the result in Blender’s system console or the scripting output area.
import bpy
import bmesh
obj = bpy.context.edit_object
if obj is None or obj.type != 'MESH':
raise RuntimeError("Select a mesh object and switch to Edit Mode.")
bm = bmesh.from_edit_mesh(obj.data)
triangles = sum(1 for face in bm.faces if len(face.verts) == 3)
quads = sum(1 for face in bm.faces if len(face.verts) == 4)
ngons = sum(1 for face in bm.faces if len(face.verts) > 4)
e_poles = sum(1 for vertex in bm.verts if len(vertex.link_edges) == 3)
n_poles = sum(1 for vertex in bm.verts if len(vertex.link_edges) >= 5)
print(f"Object: {obj.name}")
print(f"Vertices: {len(bm.verts)}")
print(f"Edges: {len(bm.edges)}")
print(f"Faces: {len(bm.faces)}")
print(f"Triangles: {triangles}")
print(f"Quads: {quads}")
print(f"N-gons: {ngons}")
print(f"E-poles, valence 3: {e_poles}")
print(f"N-poles, valence 5 or more: {n_poles}")The script uses bpy, Blender’s Python module, to access the active object. It uses bmesh, a mesh-editing API designed for Edit Mode data. The link_edges collection gives the edges connected to each vertex.
Interpret the results
A subdivision-friendly model often contains mostly quads because quad loops are easier to continue around a form. Triangles are not automatically errors. They can work well in flat or low-curvature areas. N-gons deserve closer inspection because subdivision can spread their influence in less predictable ways.
Use the pole counts as a map for further inspection:
- Check N-poles near eyes, mouths, corners, or other high-curvature areas.
- Check whether edge loops stop or change direction around a pole.
- Move harmless poles toward flat regions when they create visible artifacts.
- Treat valence-3 boundary vertices carefully. They may belong to an open border rather than an intentional E-pole.
The script reads the control mesh, not the dense surface generated by the modifier. It also does not detect bad normals, stretched faces, overlapping vertices, or poor edge spacing. After running the audit, enable the Subdivision Surface modifier and inspect the model with smooth shading. Select suspicious faces and use Edit Mode overlays to connect the numerical result to the actual topology.
Exercise
A mesh has many quads but one N-pole directly beside a sharply curved eyelid. What should you inspect first?
Show answer
Inspect the subdivided shading and the edge flow around the eyelid. If the pole causes pinching, reroute the loops or move the pole to a flatter area.