i am working on a tool and need to get selection islands in the mesh. I want to keep only the biggest connected vertex selection in the mesh.
I wrote a function that recursively searches a bmesh for connected and selected vertices:
bm = bmesh.new()
bm.from_mesh(obj.data)
_verts = [v for v in bm.verts if v.select]
_edges = [e for e in bm.edges if e.select]
islands = [] # all selection islands
#iterate all selected verts in mesh
for v in _verts.copy():
#check if that vert is already registered
if not any([v in i for i in islands]):
#new selection island
_island = []
def get_selected_connected_verts(v):
'''
recursive search vertices for connected and selected vertices
@v : bmesh vertex
'''
#iterate all edges linked to the vertex
for e in v.link_edges:
#is edge selected
if e in _edges:
#get adjacent vert
v_other = e.other_vert(v)
#register vert to island
if v_other not in _island:
_island.append(v_other)
#search again
get_selected_connected_verts(v_other)
get_selected_connted_verts(v)
#register island
islands.append(_island)
This works great for most meshes. Sadly with higher vertex counts python returns the following error:
RecursionError: maximum recursion depth exceeded while calling a Python object
When i try to increase the recursion limit, the following error appears:
Error : EXCEPTION_STACK_OVERFLOW
The terminal process terminated with exit code: 3221225725
Theoretically i know that i should be able to make the recursive function iterative... I am just not able to..
Also, if there is a totally different way to do this, please tell me!
I kindly ask for support of more talented programmers.
Thanks a lot!