2
$\begingroup$

I am trying to duplicate the mesh selection mode buttons in a side panel. everything works except the button behavior is not the same. I would want shift-click to multi-select and click only allows one selection at a time. Is there a better approach? I tried this but couldn't make it work.

sample code below:

import bpy


class VIEW3D_PT_selection_tools(bpy.types.Panel):
    bl_label = "Selection Tools"
    bl_idname = "VIEW3D_PT_selection_tools"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "New Tab"
    bl_context = "mesh_edit"

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

        row = layout.row(align=True)
        row.label(text="Selection Mode:")
        # Show buttons with only icons and no space between them
        row.prop(context.tool_settings, "mesh_select_mode", index=0, text="", icon='VERTEXSEL', toggle=True)
        row.prop(context.tool_settings, "mesh_select_mode", index=1, text="", icon='EDGESEL', toggle=True)
        row.prop(context.tool_settings, "mesh_select_mode", index=2, text="", icon='FACESEL', toggle=True)


classes = (VIEW3D_PT_selection_tools,)

def register():
    for cls in classes:
        bpy.utils.register_class(cls)

def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)

if __name__ == "__main__":
    register()
```
$\endgroup$

1 Answer 1

1
$\begingroup$

The easiest way is probably to use the built-in operator bpy.ops.mesh.select_mode doc

and the boolean array tool_settings.mesh_select_mode doc

A separate operator is required in the layout to pass the selection type ('VERT','EDGE','FACE') with layout.operator(...).type=[TYPE]. For highlighting (depress) get members of boolean array for single and multi selection

import bpy

class VIEW3D_PT_selection_tools(bpy.types.Panel):
    bl_label = "Selection Tools"
    bl_idname = "VIEW3D_PT_selection_tools"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "New Tab"
    bl_context = "mesh_edit"
    
    def draw(self, context):
        layout = self.layout

        row = layout.row(align=True)
        row.label(text="Selection Mode:")        
        vert = row.operator('mesh.select_mode', text="", icon='VERTEXSEL',
                             depress=True if context.tool_settings.mesh_select_mode[0] else False,
                             ).type="VERT"
        edge = row.operator('mesh.select_mode', text="", icon='EDGESEL',
                             depress=True if context.tool_settings.mesh_select_mode[1] else False,       
                             ).type="EDGE"
        face = row.operator('mesh.select_mode', text="", icon='FACESEL',
                             depress=True if context.tool_settings.mesh_select_mode[2] else False,        
                             ).type="FACE"


classes = (VIEW3D_PT_selection_tools,)

def register():
    for cls in classes:
        bpy.utils.register_class(cls)

def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)

if __name__ == "__main__":
    register()
$\endgroup$
2
  • $\begingroup$ this works great, thanks $\endgroup$ Commented Jan 8 at 13:09
  • $\begingroup$ Weird that this is managed by an operator instead of a flag enum property ! $\endgroup$
    – Gorgious
    Commented Jan 8 at 15:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .