I found this nice popup script by @CodeManX that works like a charm. I am trying to get an operator to only execute when the OK button from a popup is pressed. I have this simple setup, but I cannot figure out how to do this. I'm not sure if this is the most efficient or proper way. Basically I want to make it as simple as create_generic_popup(message, callback)
without needing to subclass the popup operator class.
import bpy
class OperatorThatNeedsConfirmation(bpy.types.Operator):
bl_idname = "wm.my_operator_that_needs_confirmation"
bl_label = "label"
def on_ok_execute_click(self, context):
print("OK was pressed to confirm operation so execute")
return {'FINISHED'}
def execute(self, context):
def on_ok_execute_click():
return self.execute_confirm(context)
create_generic_popup("Are you sure you want to execute?", callback=on_ok_execute_click)
return {'CANCELLED'}
bpy.utils.register_class(OperatorThatNeedsConfirmation)
class GenericPopupOperator(bpy.types.Operator):
bl_idname = "object.generic_popup_operator"
bl_label = "Message Popup"
message: bpy.props.StringProperty(name="Message")
callback: bpy.props.PointerProperty(type=bpy.types.Operator)
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
def draw(self, context):
layout = self.layout
col = layout.column()
col.label(text=self.message)
def execute(self, context):
self.report({'INFO'}, self.message)
if self.callback:
eval(self.callback)
return {'FINISHED'}
bpy.utils.register_class(GenericPopupOperator)
def create_generic_popup(message, callback=None):
bpy.ops.object.generic_popup_operator('INVOKE_DEFAULT', message=message, callback=callback)
I would also love though for the popup to be modal and not disappear when clicking away, but it seems not to be possible. Well at least the callback would be nice if I could get this working.
exec
it (really, really ugly don't do that) egcallback="print('my callback')"
and then in the operatorexec(self.callback)
or add it to a global property likeglobal callback; callback = lambda: bpy.ops.wm.my_operator_that_needs_confirmation()
and run that directly in the operator without passing it inbpy.ops
. Might be other ways too, I don't see much hope for non-hacky solutions. $\endgroup$