5
$\begingroup$

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?

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

1 Answer 1

7
$\begingroup$

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.

$\endgroup$
4
  • 4
    $\begingroup$ Can get Mesh edge with (v0.co - v1.co).length. $\endgroup$
    – scurest
    Nov 20, 2021 at 1:22
  • $\begingroup$ Yeah. I decided to write out the brute force version just to show how vertices and edges relate. $\endgroup$ Nov 20, 2021 at 1:25
  • 2
    $\begingroup$ 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 $\endgroup$
    – Gorgious
    Nov 20, 2021 at 11:04
  • $\begingroup$ Thanks a bunch! $\endgroup$
    – Cuda
    Nov 22, 2021 at 19:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .