How to get selected vertices/faces/edges?

Hi all,

This is under 2.58.1.

I’m having an inexplicably tough time figuring out in Python how to get the vertexes/edges/faces that are selected. If I go into Scripting view, add a cube, and select a single vertex in edit mode, then try to evaluate:

[ for vertex in bpy.context.selected_objects[0].data.vertices]

I always get this list of 8 True values, no matter how many vertices I select:

>>> [ for vertex in bpy.context.selected_objects[0].data.vertices]
[True, True, True, True, True, True, True, True]

So I must not be using the right code to find out what’s selected. Any ideas?

Thanks for any help!


Interactive Edit mode works on a temporary copy of the mesh data; changes made in Edit mode are not reflected in the actual object data accessible to Python until you go back from Edit to Object mode. This includes the selection status of individual vertices. So if you select one or more vertices of the cube, leave Edit mode and then evaluate your expression, you will get the result you expect.

Any serious mesh-manipulating Python script has to start by changing to Object mode, and finish by restoring whatever the initial mode was.

Best wishes,

Ah, I see, that makes sense. I tried it, and it worked.

Thanks very much!