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$
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$
  • 1
    $\begingroup$ What's the need for the ob variable? $\endgroup$ – rien333 Feb 2 '18 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 at 5:44
  • $\begingroup$ thanks for this simple example. arent we suppose to free the bmesh after ? $\endgroup$ – DB3D Sep 24 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.