It is often necessary to select all the boundary edges of a mesh – edges that are on the outer perimeter of the mesh, or located around areas of the mesh that are not filled by polygons. This operation can be performed by the user from the 3D Viewport menu: Select – Select Loops – Select Boundary Loop.
When the user uses this menu item, the operator “region_to_loop()” is calling.
1 |
bpy.ops.mesh.region_to_loop() |
We can call this operator from the Python Console or programmatically in the code of our scripts or add-ons.
It should be noted that this operator only works on selection, so, to select all the boundaries of the mesh, all the mesh grid must be selected.
Also, we can select all the boundaries of the mesh without the help of this operator.
We can use the “is_boundary” property that all edges in bmesh have. This property indicates whether the edge is a boundary of the mesh (True) or not (False).
Let’s create a bmesh object, transfer the data from the currently active mesh to it and reindex the edges in the bmesh object. The original mesh in the scene must be in the “EDIT” mode.
1 2 3 |
import bmesh bm = bmesh.from_edit_mesh(bpy.context.object.data) bm.edges.ensure_lookup_table() |
Not to depend on the original selection, let’s reset the selection for all polygons.
1 2 |
for face in bm.faces: face.select_set(False) |
Now we can loop through all the edges, and if an edge is a boundary, select it.
1 2 3 |
for edge in bm.edges: if edge.is_boundary: edge.select_set(True) |
Next, all we have to do is return the data from bmesh back to the original mesh.
1 |
bmesh.update_edit_mesh(bpy.context.object.data) |
Now all the boundary edges on our mesh will be selected.
Mesh Updates: Before accessing properties like is_boundary, you may need to call
mesh.calc_edges () or
mesh.calc_loop_triangles() to
ensure the mesh data is up-to-date.
Thank you for the addition!