How can I create a button that calls "bpy.ops.wm.revert_mainfile()" when pressed?
Asked
Active
Viewed 59 times
-1
-
1Where the button should live? Please add a bit more context. Related or dupe: How to create a custom UI? – brockmann Jun 28 '21 at 06:59
1 Answers
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