-1

How can I create a button that calls "bpy.ops.wm.revert_mainfile()" when pressed?

maroxe
  • 147
  • 5

1 Answers1

2

Just pass the identifier of the operator to UILayout.operator(operator_identifier). Example based on the Panel Simple template that comes with Blender (adds a panel to the Object Properties):

import bpy

class HelloWorldPanel(bpy.types.Panel): """Creates a Panel in the Object properties window""" bl_label = "Hello World Panel" bl_idname = "OBJECT_PT_hello" bl_space_type = 'PROPERTIES' bl_region_type = 'WINDOW' bl_context = "object"

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

    row = layout.row()            
    row.operator("wm.revert_mainfile")


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

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

if name == "main": register()

Further reading: How to create a custom UI?

brockmann
  • 12,613
  • 4
  • 50
  • 93