A few days ago I wanted to make my custom Blender operator popup a little modal window and allow me to enter some values before running the script. I had given up on this because it seemed too hard. The preferred GUI workflow in Blender is to have operations run from the panels being triggered by callbacks from the poperties. For example, changing the Rotate property causes the rotation to be applied to the object and the object is redrawn. According to the API docs, there are no callbacks on custom properties so this approach was out of the question. Also, the act of inserting into a database is a once-off operation so is different to most Blender functions.
For once off operations, Blender uses a button in a panel which will kick off an operator. But what if I want to just be able to kit a key combo and make my operator run? I’m going to be running the operation a lot during a certain workflow and maybe I don’t want to be switching back to a panel all the time, changing the input params and clicking an operator.
Apparently other users have had this need too which is probably why the Blender developers recently added a invoke_props_dialog method. It allows you to popup a little dialog box to adjust some values which you can access from your executing script.
Here’s how it works:
First you need to setup the inputs to your operator that you want to appear on the popup.
class MyNewObjectOperator(bpy.types.Operator):
bl_idname = "my_new_object"
bl_label = "Create My Object"
my_new_prop = bpy.props.StringProperty(
name="My Prop Display Name",
description = "This description comes up in the tooltip",
default="blah")
Next you need to override the invoke method and call invoke_props_dialog. This method will create a modal dialog using the properties it finds in the operator passed via the ‘self’ param.
def invoke(self, context, event):
context.window_manager.invoke_props_dialog(self, width=500)
return {'RUNNING_MODAL'}
When the ‘Ok’ button is pressed, the ‘execute’ method will be called which you should also override. The property values entered in the modal dialog can be accessed via self.properties.my_new_prop
def execute(self, context):
my_new_prop_value = self.properties.my_new_prop
self.report( "INFO", "I got value: " + str(my_new_prop_value))
return {'FINISHED'}