Edit mode bmesh
& numpy
Thought I'd throw this one in the ring as an example to speed up getting selection purely from an edit mode bmesh
.
For example sake, will take a test run via entering code in python console. The default cube "Cube" is active object.
Load in an edit mode bmesh from the mesh of the current edit object.
>>> import bmesh
>>> bm = bmesh.from_edit_mesh(C.edit_object.data)
Create a numpy verts array
>>> import numpy as np
>>> verts = np.array(bm.verts)
>>> verts
array([<BMVert(0x7f8ac738e090), index=0>,
<BMVert(0x7f8ac738e0c8), index=1>,
<BMVert(0x7f8ac738e100), index=2>,
<BMVert(0x7f8ac738e138), index=3>,
<BMVert(0x7f8ac738e170), index=4>,
<BMVert(0x7f8ac738e1a8), index=5>,
<BMVert(0x7f8ac738e1e0), index=6>,
<BMVert(0x7f8ac738e218), index=7>], dtype=object)
Define a "foreach get" method
Example of np.frompyfunc
as suggested in this stackoverflow answer
frompyfunc
iterates as the [a for a in A.flat]
does, but is somewhat
faster, and throws all the power of numpy
broadcasting at the task.
>>> selected = np.frompyfunc(lambda a: a.select, 1, 1)
With all verts selected
>>> selected(verts)
array([True, True, True, True, True, True, True, True], dtype=object)
Change selection in UI to single vert, run again
>>> selected(verts)
array([False, False, False, False, False, True, False, False],
dtype=object)
Cannot index with the object dtype
>>> sel = selected(verts).astype(bool)
>>> sel
array([False, False, False, False, False, True, False, False])
>>> verts[sel]
array([<BMVert(0x7f8ac738e1a8), index=5>], dtype=object)
Above could be used to create a function, "get_where" that takes the sequence, the property name and value to check, and the property to return
selected_verts = get_where(bm.verts, "select", True, "index")
returning an array of indices of selected vertices.