The convex_hull() function allows us to create a convex hull for a mesh – a shape that completely encloses the mesh at its extreme points. With its help, we can quickly build simplified models from complex meshes.
To use the convex_hull() function, we must initialize the bmesh object structure first.
Let’s create a bmesh and load the geometry of the currently selected mesh into it.
1 2 3 4 |
import bmesh bm = bmesh.new() bm.from_mesh(bpy.context.object.data) |
Now we can call the convex_hull() function to build a convex hull around the geometry in the bmesh object.
1 |
hull = bmesh.ops.convex_hull(bm, input=bm.verts) |
Two parameters are passed to the function:
- pointer to the bmesh geometry
- set of points around which the convex hull will be built
In our case, we pass in the second parameter all the points of the bmesh.
The function returns a dictionary with four keys:
- geom – list of vertices from which the convex hull is built
- geom_interior – list of all geometry vertices
- geom_unused – list of unused vertices, if such vertices exists
- geom_holes – list of vertices that form holes in the geometry, if such vertices exists
We can use this information, for example, to select vertices that form only the constructed shell itself.
We need to deselect all points at starting point:
1 2 3 |
[v.select_set(False) for v in bm.verts] [e.select_set(False) for e in bm.edges] [f.select_set(False) for f in bm.faces] |
And after calling convex_hull(), select points from the list by “geom” key:
1 2 |
for v in hull['geom']: v.select = True |
After performing the necessary actions, transfer the processed geometry from bmesh back to the original mesh, and clean and delete the bmesh itself, which we no longer need.
1 2 3 |
bm.to_mesh(bpy.context.object.data) bm.clear() bm.free() |
Full code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import bmesh bm = bmesh.new() bm.from_mesh(bpy.context.object.data) [v.select_set(False) for v in bm.verts] [e.select_set(False) for e in bm.edges] [f.select_set(False) for f in bm.faces] hull = bmesh.ops.convex_hull(bm, input=bm.verts) print([(k, len(hull[k])) for k in hull.keys()]) for v in hull['geom']: v.select = True bm.to_mesh(bpy.context.object.data) bm.clear() bm.free() |