By processing mesh geometry with a bmesh object, we can easily find common edges between two vertices, using the list of edges linked to current vertex.
Let’s create a “bmesh” object and transfer the geometry of the current active mesh to it.
1 2 3 4 5 6 |
import bmesh bm = bmesh.new() bm.from_mesh(bpy.context.object.data) bm.verts.ensure_lookup_table() bm.edges.ensure_lookup_table() |
For example we have two selected vertices, write them to the list:
1 2 3 |
vertices = [vertex for vertex in bm.verts if vertex.select] # [<BMVert(0x000002451BB9D008), index=17>, <BMVert(0x000002451BB9D158), index=23>] |
Each bmesh vertex has a pointer to the list of edges linked to it – link_edges.
To find an edge that lies between these two vertices and is common to them, we need to find a common element in two lists of edges for both vertices.
This can be done by turning each list into a set and performing an intersection operation.
1 |
set(vertices[0].link_edges) & set(vertices[1].link_edges) |
To get a pointer to the edge itself from the intersection, execute the next(iter) operation.
1 2 3 |
edge = next(iter(set(vertices[0].link_edges) & set(vertices[1].link_edges)), None) # <BMEdge(0x000002452970D600), index=35, verts=(0x000002451BBF6E88/17, 0x000002451BBF6FD8/23)> |
So, a common edge between two vertices can be found using just one line of code.