12
$\begingroup$

What is the python way to select vertices of a mesh?

I've tried:

for vert in obj.data.vertices:  
    vert.select = True

I ran the above code in edit mode, but the vertices didn't turn yellow.

$\endgroup$
1

1 Answer 1

14
$\begingroup$
import bpy,bmesh

ob   = bpy.data.objects['Cube']
mesh = bmesh.from_edit_mesh(ob.data)
for v in mesh.verts:
    v.select = True

# trigger viewport update
bpy.context.scene.objects.active = bpy.context.scene.objects.active

Docs

$\endgroup$
5
  • 1
    $\begingroup$ What's the need for the ob variable? $\endgroup$
    – rien333
    Feb 2, 2018 at 10:30
  • 1
    $\begingroup$ I believe it's just so you don't have to keep typing bpy.data.objects['Cube'], for instance, where it's accessed again in bmesh.from_edit_mesh(ob.data) $\endgroup$
    – andyV
    Jul 21, 2019 at 5:44
  • 1
    $\begingroup$ thanks for this simple example. arent we suppose to free the bmesh after ? $\endgroup$
    – Fox
    Sep 24, 2019 at 21:14
  • $\begingroup$ When trying this in Blender 2.91 it complains that "The mesh must be in editmode".. $\endgroup$
    – user299831
    Feb 21, 2021 at 18:43
  • $\begingroup$ @user299831 bmesh.from_edit_mesh requires you being in edit mode for the object initially. $\endgroup$ Dec 31, 2021 at 20:06

You must log in to answer this question.

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