6
$\begingroup$

I want to get the last selected vertex. I tried with bm.select_history.active but this don't work.

$\endgroup$
1
  • 1
    $\begingroup$ That should work, can you show a small example script that fails? $\endgroup$
    – ideasman42
    Commented May 8, 2015 at 1:55

2 Answers 2

8
$\begingroup$

You can walk the select history in reverse and find the first BMVert:

import bpy
import bmesh

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

for elem in reversed(bm.select_history):
    if isinstance(elem, bmesh.types.BMVert):
        print("Active vertex:", elem)
        break

Note that select_history does not support selection operators like box and lasso select. They don't make a geometry element active, nor do they add to the history at all.

$\endgroup$
1
  • 1
    $\begingroup$ Note that if this is to match the active vertex from Blender's perspective (all tools and draw code) - This should break after the first element. $\endgroup$
    – ideasman42
    Commented Oct 19, 2015 at 8:11
8
$\begingroup$

This function returns the active vertex,

Note that if the last selected element isn't a vertex, Blender considers there to be no active vertex.

import bpy
import bmesh


def bmesh_vert_active(bm):
    if bm.select_history:
        elem = bm.select_history[-1]
        if isinstance(elem, bmesh.types.BMVert):
            return elem
    return None


ob = bpy.context.object
me = ob.data

bm = bmesh.from_edit_mesh(me)

print(bmesh_vert_active(bm))
$\endgroup$

You must log in to answer this question.

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