What's the fastest way to calculate the midpoints of a sequence of edges?

by andyV   Last Updated July 11, 2018 06:15 AM

I'm trying to create a list that calculates the midpoints of a sequence of edges.

Right now I'm doing it like this:

eList = []
for e in [e for e in bm.edges if e.select]:
    pt_1, pt_2 = (ob.matrix_world * e.verts[0].co), (ob.matrix_world * e.verts[1].co)
    addPt = pt_1 + pt_2
    co = mathutils.Vector((addPt[0]/2,addPt[1]/2,addPt[2]/2 ))
    eList.append((e, co))

I multiply each vert in the edge by the object's world matrix, then add the verts together. I divide each axis in the vert by 2 and covert them to a vector and then put the edge and the vector in a tuple and append that to a list.

It's probably not a very efficient process but I'm still pretty new to python and can't think of a faster way.

Is this something that could be done faster in numpy?

What would be the fastest way to perform this function over a large sequence of edges?



Answers 1


Some improvements.

Use list comprehension Only need to multiply the local average edge coord by matrix world once. Multiply / divide a vector by a scalar, rather than on a per component basis.

import bpy
import bmesh

ob = bpy.context.edit_object
me = ob.data
bm = bmesh.from_edit_mesh(me)

mw = ob.matrix_world
elist = [(e, mw * (e.verts[0].co + e.verts[1].co) / 2) 
        for e in bm.edges if e.select]

could make a little helper routine. Will have a default of local avge if not passed a matrix.

def mid_edge(e, matrix = mathutils.Matrix()):       
    return matrix * (e.verts[0].co + e.verts[1].co) / 2
batFINGER
batFINGER
July 11, 2018 06:14 AM

Related Questions


Using Blender for plotting graphs?

Updated February 23, 2016 01:06 AM


Make Ball rolling down function animation

Updated April 26, 2018 22:15 PM

Find lower left vertex of a quad with python

Updated March 28, 2017 22:15 PM

Procrustes in Blender

Updated July 20, 2017 13:15 PM