Blender 2.8 Python Addon: How to register a Button? I want to achieve a simple button (including the possibility to map a shortcut to it) to toggle the automerge option on and off.
Obviously this is wrong:

bpy.types.VIEW3D_MT_mesh.append(toggle_automerge_button)


But, how to find the right register_class?

I am sure one could define a shortcut in the preferences, but for several reasons I'm interested in this add-on solution:


I'm trying to learn Python.
This is supposed to work as an example to include other things later.
an extra button provides the possibiltity to put it in the Q-menu


bl_info = {
    "name": "Toggle Automerge",
    "blender": (2, 80, 0),
    "location": "View3D &gt; Mesh",
    "description": "Toggles Automerge on and off",
    "category": "Mesh",
}

import bpy

def toggle_automerge(self, context):

    if bpy.context.scene.tool_settings.use_mesh_automerge is False:
        bpy.context.scene.tool_settings.use_mesh_automerge = True
    else:
        bpy.context.scene.tool_settings.use_mesh_automerge = False

class ToggleAutomergeOperator(bpy.types.Operator):
#class ToggleAutomergeOperator(bpy.types.ToolSettings):
#class bpy.types.ToolSettings(bpy_struct):
#class HelloWorldOperator(bpy.types.Operator):
#class OBJECT_OT_add_object(Operator, AddObjectHelper):
    """Toggle Automerge"""
    bl_idname = "mesh.toggle_automerge"
    bl_label = "Toggle Automerge"
    bl_options = {'REGISTER'}

    def execute(self, context):

        toggle_automerge(self, context)

        return {'FINISHED'}

# Registration

def toggle_automerge_button(self, context):
    self.layout.operator(
        ToggleAutomergeOperator.bl_idname,
        text="Toggle Automerge",
        icon='PLUGIN')

def register():
    bpy.utils.register_class(ToggleAutomergeOperator)
    #bpy.utils.register_manual_map(add_object_manual_map)
    #bpy.types.VIEW3D_MT_mesh.append(toggle_automerge_button)

def unregister():
    bpy.utils.unregister_class(ToggleAutomergeOperator)
    #bpy.utils.unregister_manual_map(add_object_manual_map)
    #bpy.types.VIEW3D_MT_mesh.remove(toggle_automerge_button)

if __name__ == "__main__":
    register()
wm.context_toggle

For a simple toggle of a boolean property with a context path can use the bpy.ops.wm.context_toggle(data_path="") operator.  For the example in question

data_path = "scene.tool_settings.use_mesh_automerge"


To add as a layout element

op = layout.operator("wm.context_toggle")
op.data_path = "scene.tool_settings.use_mesh_automerge"


Removes the need to write an operator simply to toggle a property.

import bpy

def toggle_automerge_button(self, context):
    op = self.layout.operator(
        "wm.context_toggle",
        text="Toggle Automerge",
        icon='PLUGIN')
    op.data_path = "scene.tool_settings.use_mesh_automerge"

def register():
    bpy.types.VIEW3D_MT_edit_mesh.append(toggle_automerge_button)

def unregister():
    bpy.types.VIEW3D_MT_edit_mesh.remove(toggle_automerge_button)

if __name__ == "__main__":
    register()


Note could also simply set the property

def toggle_automerge_button(self, context):
    ts = context.scene.tool_settings
    layout = self.layout
    layout.prop(ts, "use_mesh_automerge", toggle=True)