9
$\begingroup$

I have created a plane and have applied loop cut and slide twice horizontally and once vertically( using python script).

With this done, I intend to select the top two corner vertices using python script.

The image of the model can be viewed in the following link,

enter image description here

I browsed through net and found a code for selecting faces. So I figured, I would try the same with vertices. The code snippet is below,

bpy.ops.object.mode_set(mode = 'OBJECT')
obj = bpy.context.active_object
bpy.ops.object.mode_set(mode = 'EDIT') 
bpy.ops.mesh.select_mode(type="VERT")
bpy.ops.mesh.select_all(action = 'DESELECT')
obj.data.vertices[0].select = True # I TRIED THIS WITH ALL VALUES LIKE 0,1..

But, the result though no error did not give me the expected output.All Vertices remain unselected.

Please help me with solving this.

$\endgroup$
  • $\begingroup$ Even though I answered the immediate question, it might not be useful to you in the greater scheme of writing a python script. If you have more questions please edit your question or ask a new one separately. $\endgroup$ – zeffii Dec 16 '15 at 11:38
6
$\begingroup$

This is probably counter-intuitive but you should place the Object in OBJECT mode when doing selection using indices via Python. Then flip back to EDIT mode to see the result.

import bpy

bpy.ops.object.mode_set(mode = 'OBJECT')
obj = bpy.context.active_object
bpy.ops.object.mode_set(mode = 'EDIT') 
bpy.ops.mesh.select_mode(type="VERT")
bpy.ops.mesh.select_all(action = 'DESELECT')
bpy.ops.object.mode_set(mode = 'OBJECT')
obj.data.vertices[0].select = True
bpy.ops.object.mode_set(mode = 'EDIT') 

Another way to make selections is to use BMesh, you'll find templates for this in
TextEditor -> Templates -> Python -> Simple Bmesh (edit mode)

|improve this answer|||||
$\endgroup$
  • $\begingroup$ Thanks a lot. It works. I don't understand the logic though. $\endgroup$ – Gabriel Dec 16 '15 at 10:24
  • $\begingroup$ just remember that setting the selections using indices on data.vertices only works in OBJECT mode. or else you have to use a bmesh. $\endgroup$ – zeffii Dec 16 '15 at 11:31
  • $\begingroup$ I can't get this to work in 2.8. Does anyone know what are the relevant API changes and how to achieve this in 2.8? $\endgroup$ – z01ks Mar 16 '19 at 21:04

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.