1
$\begingroup$

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?

$\endgroup$

1 Answer 1

2
$\begingroup$

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.

Test script

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.

def mid_edge(e):       
    return (e.verts[0].co + e.verts[1].co) / 2

elist = [(e, mw * mid_edge(e)) 
        for e in bm.edges if e.select]        
$\endgroup$
1
  • $\begingroup$ Wow! I didn't realize you could do all that directly in the list comprehension. This is great, thanks! $\endgroup$
    – andyV
    Commented Jul 11, 2018 at 6:32

You must log in to answer this question.

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