# Get edge lengths with python

How can I get the length of an edge with a script? I want to identify a face by its vertex group and get the lengths of its edges. I've gotten as far as identifying the vertex group so I can select it or get the indices - what next? Do I have to do some complicated math to get distances between points in 3d space or is there a direct way to get the edge length as it is displayed in the overlay? The angle would be great too, but I'm trying to take small bites.

• Commented Mar 4, 2020 at 15:46
• @batFINGER Oof. BMESH. Nothing confuses me like BMESH. It didn't work to select faces with vertex groups because it selected too many faces so I switched to selecting them with material slots. I figured that out, but now it sounds like I need to figure out how to do it with BMESH. I haven't wrapped my head around that process yet. Here goes... Commented Mar 10, 2020 at 1:34

That was easier than I expected!

import bpy
import bmesh
obj=bpy.data.objects["Cube"]
obj.select_set(True)
i = 0
for mat_slot in obj.material_slots:
#enter edit mode
bpy.ops.object.mode_set(mode="EDIT")
#deselect all
bpy.ops.mesh.select_all(action='DESELECT')
#choose the active slot based on the loop number
bpy.context.object.active_material_index = i
#select the faces of the active slot
bpy.ops.object.material_slot_select()
#make a bmesh from the mesh
me = bpy.context.object.data
bm = bmesh.from_edit_mesh(me)
#list to populate with lengths
edge_lengths = []
#if edge is selected, get its length
for e in bm.edges:
if e.select:
edge_lengths.append(e.calc_length())
print(edge_lengths)
i=i+1
bpy.ops.object.mode_set(mode="OBJECT")

• python comes with enumerate() Commented Mar 10, 2020 at 9:05