3
$\begingroup$

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.

$\endgroup$
2
  • 3
  • $\begingroup$ @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... $\endgroup$ Commented Mar 10, 2020 at 1:34

1 Answer 1

2
$\begingroup$

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")
$\endgroup$
1
  • 1
    $\begingroup$ python comes with enumerate() $\endgroup$
    – brockmann
    Commented Mar 10, 2020 at 9:05

You must log in to answer this question.

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