How can I get number of linked vertices whithout selection?

I know you can get the number of selected vertices, but how about linked vertices? I have a couple mesh lines and need to get the count for each one.

verts_sel = len([v for v in obj.data.vertices if v.select == True])
               

You don’t even need to loop to get the selected geometry count. They are cached in:

obj.data.total_vert_sel
obj.data.total_face_sel
obj.data.total_edge_sel

Linked edges/verts can be found with something like this. You’ll need to have a starting vert (or edge).

import bpy, bmesh

me = bpy.context.object.data
bm = bmesh.from_edit_mesh(me)

def any_vert(bm):
    for v in bm.verts:
        if v.select:
            return v

vert_start = any_vert(bm)

if vert_start is not None:
    verts = {vert_start}  # All verts connected to vert_start.
    edges = set()

    check_edges = {*vert_start.link_edges}
    while check_edges:
        e = check_edges.pop()
        if e in edges:
            continue
        edges.add(e)
        for v in e.verts:
            if v in verts:
                continue
            verts.add(v)
            for e_ in v.link_edges:
                if e_ in edges:
                    continue
                check_edges.add(e_)

    for v in verts:
        v.select = True
    bmesh.update_edit_mesh(me)
2 Likes