Selected vertices

Hi, all! Please look at the following script:


import bpy

count = 0

def evaluate(mesh):
____#mesh = bpy.types.Mesh
____#v = bpy.types.MeshVertex
____global count

     ____for v in mesh.vertices:
             ________if v.select:
                 ____________count += 1
        
     ____print(count)

evaluate(bpy.context.active_object.data)


As you can see, I want to get the count of a mesh’s selected vertices. When I run this script from the Blender default file, I always get the same answer : 8 ( which makes some kind of sense, as the only available mesh is a cube ).
The context (object or edit) doesn’t matter. The cube can be selected or not: it doesn’t matter. All vertices can be selected in edit mode, some of them or none of them: it doesn’t matter.

I can’t make any sense out of this.

Thanks in advance,

Thierry

P.S : I also can’t fix the indents in this post and that’s why I use underscores here. But please take my word: they are correct :o

The standard API does not access the live EditMesh, changes such as selection state need to be flushed manually (unless you use the bmesh module):

import bpy

ob = bpy.context.object
assert ob is not None
assert ob.type == 'MESH'

me = ob.data
if me.is_editmode:
    ob.update_from_editmode()
    
print("Selected vertices: %i" % len([v for v in me.vertices if v.select]))

Use code-tags to preserve indentation.

Thanks very much.