# How do I measure the length of a selected edge with Python?

Which operation would I use to acquire the length of the edge I have selected so that I could store that in a variable for later use?

• It's the distance between its two verts. If you're using bmesh, there's a calc_length member. Nov 20, 2021 at 0:49

In the simplest form, if you don't want to use bmesh, or any vector operations you can use

import bpy

object = bpy.context.active_object
mesh = object.data
for edge in mesh.edges:
if edge.select:
v0 = mesh.vertices[edge.vertices[0]]
v1 = mesh.vertices[edge.vertices[1]]
x2 = (v0.co[0] - v1.co[0]) ** 2
y2 = (v0.co[1] - v1.co[1]) ** 2
z2 = (v0.co[2] - v1.co[2]) ** 2
length = (x2+y2+z2)**.5
print(length)


replacing the print statement with whatever sort of storage you want.

This assumes that you have an active object and it is a mesh object. It will print the length of every edge that is selected.

See the manual entry for Mesh for details, but simplified, this searches all of the edges in the mesh and for each selected edge find the distance between its two vertices. That distance, of course is the length.

Note: as pointed out in the comment you can simplify the length calculation. I wrote it out in full just to show the relationship between edges, vertices and coordinates. The simplified version of the for loop is

for edge in mesh.edges:
if edge.select:
v0 = mesh.vertices[edge.vertices[0]]
v1 = mesh.vertices[edge.vertices[1]]
print(f"length of edge {edge.index} = {(v0.co-v1.co).length}")


No transforms are applied. As mentioned in another comment, to apply transforms to get real world coordinates, replace the print statement with something like

co0_t = object.matrix_world @ v0.co
co1_t = object.matrix_world @ v1.co
print(f"length of edge {edge.index} = {(co0_t-co1_tt).length}")


where @ is the Python matrix multiply operator

Using bmesh is a bit simpler:

import bpy
import bmesh

object = bpy.context.active_object
if object.mode == 'OBJECT':
bm = bmesh.new()
bm.from_mesh(object.data)
else:
bm = bmesh.from_edit_mesh(object.data)

bm.edges.ensure_lookup_table()

for edge in bm.edges:
if edge.select:
print(edge.calc_length())


The same caveats about there being an active object that is a mesh holds.

• Can get Mesh edge with (v0.co - v1.co).length. Nov 20, 2021 at 1:22
• Yeah. I decided to write out the brute force version just to show how vertices and edges relate. Nov 20, 2021 at 1:25
• You might want to use obj.matrix_world @ v.co to get real world coordinates if your object has unapplied scale or is parented to an object with unapplied scale Nov 20, 2021 at 11:04
• Thanks a bunch!
– Cuda
Nov 22, 2021 at 19:29