Saturday, October 25, 2014

Oddity of selecting vertices in Blender via python script

Okay so if you read into python scripting examples of moving vertices around in Blender, it seems whether using a bpy.context or bpy.data call to a given object and its corresponding vertices, you can resort to updating these without any problems.  On the other hand while you can see if for a given selected object, its given vertices are selected (in 'Edit' mode), it appears that changing a given selection status is another matter.  Technically you can change these but apparently relation between context and object are not given systemically in a way that translates to any related operator command.  Thus if you wanted to select particular vertices in adding these to a given vertex group via operator command, for instance, it appears that an operator based assignment process is a no go.

There is a work around that I've found at the moment, albeit slightly circuitous in updating a given set of vertices in so far as selection.

#example to select a vertex
bpy.ops.object.mode_set(mode = 'EDIT')
bpy.ops.mesh.select_all(action = 'DESELECT')  #deselecting vertices

#nextline ensures that we have an updated representation of selected vertices
bpy.context.object.update_from_editmode() 
me = bpy.context.object.data
bm = bmesh.new()   # create an empty BMesh
bm.from_mesh(me)   # fill it in from a Mesh

bm.verts[0].select_set(True)
bpy.ops.object.mode_set(mode = 'OBJECT')
bm.to_mesh(me)
bpy.ops.object.mode_set(mode = 'EDIT')

Presuming there is a vertex, for instance, in the zeroth container position to be selected for a given mesh object, this sets the vertex of the object to a selection true case.

No comments:

Post a Comment

Oblivion

 Between the fascination of an upcoming pandemic ridden college football season, Taylor Swift, and Kim Kardashian, wildfires, crazier weathe...