How can I add a button in object and edit mode?

Hi! Sorry if this was already asked. I tried using the forum’s search engine but couldn’t find something related, so…

Currently I have this small script:

import bpy

class SnapperOBJ(bpy.types.Panel):
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOLS"
    bl_context = "objectmode"
    bl_label = "Snapper Panel"


    def draw(self, context):
        layout = self.layout
        col = layout.column(align=True)


        col.label(text="Snap:")
        col.operator("view3d.snap_selected_to_cursor", text="Selected to Cursor")
        col.operator("view3d.snap_cursor_to_selected", text="Cursor to Selected")
        
class SnapperEDIT(bpy.types.Panel):
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOLS"
    bl_context = "editmode"
    bl_label = "Snapper Panel"


    def draw(self, context):
        layout = self.layout
        col = layout.column(align=True)


        col.label(text="Snap:")
        col.operator("view3d.snap_selected_to_cursor", text="Selected to Cursor")
        col.operator("view3d.snap_cursor_to_selected", text="Cursor to Selected")


bpy.utils.register_class(SnapperOBJ)
bpy.utils.register_class(SnapperEDIT)

It does register the buttons the way I want in Object mode, which is great, but the menu doesn’t appear in Edit Mode, when I need this the most!

How can I make this work? Specifically, I need to select vertices/faces, and snap the cursor to those.

Also, FYI: I’m using Maya shortcuts, not Blender default.

The correct bl_context is mesh_edit:

bl_context = “mesh_edit”

But since you want the panel in object AND edit mode, get rid of it entirely and use a poll classmethod:

import bpy

class VIEW3D_PT_tools_snapper(bpy.types.Panel):
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOLS"
    #bl_context = "objectmode"
    bl_label = "Snapper Panel"

    @classmethod
    def poll(cls, context):
        return context.mode in {'OBJECT', 'EDIT_MESH'}

    def draw(self, context):
        layout = self.layout
        col = layout.column(align=True)
        col.label(text="Snap:")
        col.operator("view3d.snap_selected_to_cursor", text="Selected to Cursor")
        col.operator("view3d.snap_cursor_to_selected", text="Cursor to Selected")

def register():
    bpy.utils.register_module(__name__)


def unregister():
    bpy.utils.unregister_module(__name__)

if __name__ == "__main__":
    register()


Oh my! Thank you a lot CoDEmanX! :slight_smile: You surely helped me to save a lot of time!

Thank you!

Hi, What is the proper way to do this in Curve Edit Mode ?? thanks. Jozef

just found out

    
@classmethod    
        def poll(cls, context):
        return context.mode in {'OBJECT', 'EDIT_MESH', 'EDIT_CURVE'}