# Python scripting: how to select next vertex in mesh

I have the following script (doesn't work) to try to select the next connected vertex (of the current object) in a mesh. Would appreciate some help. thanks

# seems like I need to toggle this to sync script to view. Please help
# if there is a better way!
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.mode_set(mode='EDIT')

count = len(bpy.context.scene.objects.active.data.vertices)
try:
active = [e.index for e in bpy.context.scene.objects.active.data.vertices if e.select][0]
except IndexError:
active = 0

active = (active+1) % count

for idx, e in enumerate(bpy.context.scene.objects.active.data.vertices):
if idx==active:
e.select = True
else:
e.select = False


• check out TextEditor -> Templates -> Python -> BMesh Simple EditMode. Oct 12 '15 at 16:29
• sweet - that's exactly the kind of help I needed. Posting the working script below.
– RoyM
Oct 12 '15 at 17:14
• modified text above to describe a bit better the intent
– RoyM
Oct 13 '15 at 13:59

You can get all adjacent vertices like this

import bpy
import bmesh

obj = bpy.context.active_object
mesh = obj.data

bm = bmesh.from_edit_mesh(mesh)
active = bm.select_history.active

if active and isinstance(active, bmesh.types.BMVert):
print(edge.other_vert(active))


Based on @zeffii's hint, here's a working script. It selects the first vertex if none selected, and cycles through the list again if needed.

import bpy
import bmesh

# Get the active mesh
obj = bpy.context.edit_object
me = obj.data
bm = bmesh.from_edit_mesh(me)

bm.faces.active = None

count = len(bm.verts)
try:
activeIndex = [e.index for e in bm.verts if e.select][0]
except IndexError:
activeIndex = 0
finally:
activeIndex = (activeIndex+1) % count

for idx,v in enumerate(bm.verts):
v.select = (idx==activeIndex)

# Show the updates in the viewport and recalculate n-gon tessellation.
bmesh.update_edit_mesh(me, True)