2
$\begingroup$

I am writing an addon that uses linked libraries. In order to refresh the linked node groups you need to close and reopen blender. I am using some code from the Amaranth addon that I modified. Normally Amaranth prompts the user to save if they haven't yet. I would rather the the file be auto saved in a temp file, closed, then reopened. I have all of this functionality done.

My problem lies with the fact that now when the user goes to save it will save the temp file. I want to know if there is a way to clear the location it will save in?

I found bpy.data.filepath but it is read-only. I need a way to clear this so when a user presses CTRL + S it doesn't save in a weird location.

My operator is here:

class NGLRefresh(Operator):
    bl_label = "Refresh"
    bl_idname = "ngl.refresh"
    bl_description = "Relinks the nodes from the library file (saves and reloads)"

    temp_path = bpy.utils.script_path_user() + os.sep + "NGL_TEMP_FILE.blend"

    # A huge thanks to Pablo Vazquez
    # for the code from Amaranth (https://github.com/venomgfx/amaranth)

    def save_reload(self, context, path):
        if not path:
            bpy.ops.wm.save_as_mainfile(filepath=self.temp_path)
            bpy.ops.wm.open_mainfile(filepath=self.temp_path)
            return
        bpy.ops.wm.save_mainfile()
        self.report({"INFO"}, "Saved & Reloaded")
        bpy.ops.wm.open_mainfile("EXEC_DEFAULT", filepath=path)

    def execute(self, context):
        path = bpy.data.filepath
        self.save_reload(context, path)

        # Remove temp file if necessary
        if os.path.exists(self.temp_path):
            os.remove(self.temp_path)

        return {"FINISHED"}
$\endgroup$
2
$\begingroup$

You need to save the original path so you can use it later. This can be done by adding a StringProperty to store it.

bpy.types.Scene.orig_path = bpy.props.StringProperty()

As you can't alter the filepath, you could try setting up an app.handler.save_pre to check this property before saving a blend file, if it is there then use save_as_mainfile() to save it using the old path name. The gotcha here is that you could be interfering with the user choosing 'save as'.

The drawback to this approach is the user will see the tmp path in between your operator and choosing to save, so you may want to stick with having to save before your operator is run.

I think a better approach would be to create an addon preference that allows the user to enable saving the blend file before your operator runs, then if enabled you can simply save the blendfile and reload without any issues. If the user has not enabled it then prompt them to save and reload.

$\endgroup$
  • $\begingroup$ The problem with the first method you suggested is that the automatic saving runs when the file hasn't been saved at all. I will probably just prompt the user to save. $\endgroup$ – Nathan Craddock Nov 14 '15 at 15:35

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.