# 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 > 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):
"""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.types.VIEW3D_MT_mesh.append(toggle_automerge_button)

def unregister():
bpy.utils.unregister_class(ToggleAutomergeOperator)
#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)


Some general feedback:

• Don't use x is False, use x == False instead, or even better not x.
• In an operator, always use the passed-in context instead of bpy.context.
• Don't pass any parameters you're not using (in this case self).
• You can simplify some code.
def toggle_automerge(context):
ts = context.scene.tool_settings
ts.use_mesh_automerge = not ts.use_mesh_automerge


The class should be named MESH_OT_toggle_automerge. This is standard naming in Blender; the class name should follow the bl_idname.

bpy.types.VIEW3D_MT_mesh indeed doesn't exist in Blender 2.80 any more, but since you don't give any indication as to where you want the button to show, it's hard to give an alternative. Maybe bpy.types.VIEW3D_MT_view?

If you want to learn Python, check out Scripting for Artists and The Little Book of Python Anti-Patterns.

• Any ideas on how to add item to the quick favorites menu.? Apr 10 '19 at 3:06
• @batFINGER I looked at the Blender sources, and there doesn't seem to be any way to do that from Python. Managing the quick favourites is really up to the user. Apr 15 '19 at 11:51