4
$\begingroup$

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

enter image description here

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

2 Answers 2

4
$\begingroup$

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):
    for edge in active.link_edges:
        print(edge.other_vert(active))    
$\endgroup$
2
$\begingroup$

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)
$\endgroup$

You must log in to answer this question.

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