1
$\begingroup$

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.

$\endgroup$
2
  • 2
    $\begingroup$ You can't add pointer properties to operator properties. You'll have to either fill in your callback as a string and exec it (really, really ugly don't do that) eg callback="print('my callback')" and then in the operator exec(self.callback) or add it to a global property like global callback; callback = lambda: bpy.ops.wm.my_operator_that_needs_confirmation() and run that directly in the operator without passing it in bpy.ops. Might be other ways too, I don't see much hope for non-hacky solutions. $\endgroup$
    – Gorgious
    Commented Feb 7 at 15:07
  • 1
    $\begingroup$ Hi @Gorgious thank you for your response! Guess we'll do it the hacky way then hehe. Can you add it as answer? $\endgroup$
    – Harry McKenzie
    Commented Feb 7 at 16:50

0

You must log in to answer this question.

Browse other questions tagged .