How to get vertex selection count ?

Hi all,

how do I get the number of vertices in selection ? I’m trying to draw a pie menu only if two or more vertices are selected (otherwise the merge operator returns an error because it doesn’t find “first” or “last” in the enum). So I did this :


class VIEW3D_PIE_merge_more(Menu):
    bl_label = "Merge"


    def draw(self, context):
        layout = self.layout


        pie = layout.menu_pie()
        
        if obj.data.total_vert_sel >= 2:
            pie.operator("mesh.merge", text="At first").type = 'FIRST'
            pie.operator("mesh.merge", text="At last").type = 'LAST'
            pie.operator("mesh.merge", text="At center").type = 'CENTER'
        pie.operator("mesh.remove_doubles", text="Remove doubles")

Apparently this total_vert_sel is at fault because the pie menu displays nothing.
What am I doing wrong ?

Hadrien

I didn’t know about that attributes… It does work, you only forgot to tell Blender what “obj” is:

import bpy
import bmesh
from bpy.types import Menu

# spawn an edit mode selection pie (run while object is in edit mode to get a valid output)


class VIEW3D_PIE_template(Menu):
    # label is displayed at the center of the pie menu.
    bl_label = "Select Mode"

    def draw(self, context):
        layout = self.layout
        ob = context.object

        pie = layout.menu_pie()
        
        if ob is not None and ob.type == 'MESH' and ob.data.total_vert_sel >= 2:
            pie.operator("mesh.merge", text="At first").type = 'FIRST'
            pie.operator("mesh.merge", text="At last").type = 'LAST'
            pie.operator("mesh.merge", text="At center").type = 'CENTER'
        pie.operator("mesh.remove_doubles", text="Remove doubles")


def register():
    bpy.utils.register_class(VIEW3D_PIE_template)


def unregister():
    bpy.utils.unregister_class(VIEW3D_PIE_template)


if __name__ == "__main__":
    register()

    bpy.ops.wm.call_menu_pie(name="VIEW3D_PIE_template")


Once again you save my soul CoDEmanX. Thanks a lot. I haven’t covered oop yet in my Python learning that’s why I am making these foolish mistakes.
Now I’d like to hardcode the position of a menu item in the pie : make it stay at top position even when it’s alone. So far it seems they get auto arranged in W, E, N, S, then NW, NE, SW, SE…
Any idea how to achieve that ?

There’s a hardcoded pattern:

https://developer.blender.org/diffusion/B/browse/master/source/blender/editors/interface/interface.c

Items should remain at their position as long as you add them to the interface in the same order.

Just noticed that actually ! Fill the pie with an operator’s enums and they stay in the same place even when only some of these enums are available. Great ! Thanks again.