2
$\begingroup$

I have an operator that needs the file to be saved before it can continue (it uploads the file but that's not relevant). Rather than just give an error message that the user needs to save the file first I would like to invoke the default save operator (with the confirmation dialog).

E.g.

import bpy
class TestOperator(bpy.types.Operator):
    bl_idname = "custom.test_operator"
    def execute(self, context):
        do_stuff_with_file()

    def invoke(self, context, event):
        if bpy.data.is_dirty:
            return bpy.ops.wm.save_mainfile('INVOKE_DEFAULT')
        return self.execute(context)

But in the example above execute() will never get called if the file is unsaved. Is there a way invoke save_mainfile with the default confirm popup and have execute() run after it's finished?

I could do something like this:

import bpy
class TestOperator(bpy.types.Operator):
    bl_idname = "custom.test_operator"
    bl_label = "Save File?"
    def execute(self, context):
        if bpy.data.is_dirty:
            bpy.ops.wm.save_mainfile()

        do_stuff_with_file()

    def invoke(self, context, event):
        if bpy.data.is_dirty:
            return context.window_manager.invoke_confirm(self, event)
        return self.execute(context)

But that doesn't use the same confirm as the save operator, I'd like it to show the path it'd overwrite and use the file browser if you're not editing a file.

$\endgroup$
  • $\begingroup$ Yes, that‘s possible: blender.stackexchange.com/a/73548 $\endgroup$ – p2or Nov 20 '17 at 23:56
  • $\begingroup$ @p2or I already have an example that uses invoke_confirm to create a dialog box asking the user if they want to save. What I'm looking for is a way to call execute() on the main operator after execute() has been called on bpy.ops.wm.save_mainfile (which is called as soon as the user confirms to save). $\endgroup$ – Isaac Nov 21 '17 at 1:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.