B256: Get selected vertices

Let it given that i have a mesh-object, get its vertices, filtering for “v.select == true” i get a list of all selected vertices. But as im writing an mesh operator i noticed that the selected vertices don’t get updated. That means it runs correctly the first time. But if the user changes the selection in editmode the script still gets the old selection, since v.select did not change.

Is there the need to update something first?

My lines:


def getSelectedVertices(mesh):
    vertexList = []
    for vert in mesh.vertices:
        if vert.select:
            vertexList.append(vert)
    return vertexList

You need to flip in and out of edit / object / edit mode to get it to show the selected. http://blenderartists.org/forum/showthread.php?t=206336&p=1768759&viewfull=1#post1768759

Thank you. But now i have the next question. How can i let a script leaving the editmode and entering it again? Looked at the API but could not find it. Google is also no help, because of the same naming for user input. Any suggestions where this functionality is hiding inside bpy?

set mode to edit mode:


bpy.ops.object.mode_set('EDIT')

object mode:


bpy.ops.object.mode_set('OBJECT')

hope that helps

As i searched for a while, one minute before your post i found the solution. But big thanks anyways.

My solution is:


if bpy.context.object.mode == "EDIT":
    bpy.ops.object.editmode_toggle()
    bpy.ops.object.editmode_toggle()

you do know this gets you back to edit mode, instead of object mode where all modifications are done?

OK. Lets see. I have to switch to object-mode to update the selections. But i need to stay in objectmode for changes to be applied. After all is finished i update the Mesh with MeshData.update() (only needed if mesh structure is changed) and switch back to editmode again, before the script ends.

Is this correct?

I tried it and now i have another problem.


Traceback (most recent call last):
  File "/opt/blender256/2.56/scripts/addons/mesh_nsmooth.py", line 251, in execute
    normalSmoothMain(self, context)
  File "/opt/blender256/2.56/scripts/addons/mesh_nsmooth.py", line 214, in normalSmoothMain
    bpy.ops.object.mode_set('OBJECT')
  File "/opt/blender256/2.56/scripts/modules/bpy/ops.py", line 177, in __call__
    ret = op_call(self.idname_py(), C_dict, kw, C_exec)
TypeError: Calling operator "bpy.ops.object.mode_set" error, expected a string enum in ('INVOKE_DEFAULT', 'INVOKE_REGION_WIN', 'INVOKE_REGION_CHANNELS', 'INVOKE_REGION_PREVIEW', 'INVOKE_AREA', 'INVOKE_SCREEN', 'EXEC_DEFAULT', 'EXEC_REGION_WIN', 'EXEC_REGION_CHANNELS', 'EXEC_REGION_PREVIEW', 'EXEC_AREA', 'EXEC_SCREEN')

location:<unknown location>:-1

location:<unknown location>:-1

Looks odd and totally unrelated to me…

This looks as if you tried something like this:

bpy.ops.object.mode_set('<i>EDIT')</i>

Use something like this and it should work:

bpy.ops.object.mode_set(mode='EDIT')

Thank you. Could have guessed something like this. But im not much familiar with python right now. Next time i will know. :wink: