4
$\begingroup$

I have some code which requires opening and closing blend files. The code has to save a temporary copy of the open file before opening other files and doing stuff to them. After the code is finished it reloads the temporary save of the original file.

The problem is, if the user goes to save their file (after the code has been carried out) it will save to the temporary location, of course they could change the save location manually but that would insert an unnecessary amount of entropy into this process.

I could also simply save the open file before the code was run, but I would like to avoid forcing that on the user. All that's holding me back is not being able to change the file's internal save location after all is finished.

If I could write to bpy.data.filepath (rather than only read it) problem would be solved. Anyone know of a way to set this value without actually saving the file with: bpy.ops.wm.save_as_mainfile() ?

As it stands I've developed a complicated method of working around this, but involves renaming and copying files. If I have to use it I will but hoping someone might know a slimmer solution here.

Also I'm aware there is a similar thread "Clear saved filepath" but I was hoping to explore this further. I just want to know is there something I'm missing with setting the blend file's save path.

EXPLANATION OF HOW IT'S DIFFERENT FROM "CLEAR SAVED FILEPATH": Let's see, that thread doesn't solve the problem. The end.

$\endgroup$
3
$\begingroup$

The bpy.data.filepath property reflects the current blendfile's location, which is why it is read only.

Not sure I understand your problem fully, but here's a suggestion: You can save the file implicitly (without asking the user to provide a save path), and decide yourself on an arbitrary temporary path for your save. By providing the bpy.ops.wm.save_as_mainfile() operator a filepath parameter, you can save directly to any path you desire.

bpy.ops.wm.save_as_mainfile( filepath = MYPATH )

You can later load that file implicitly again, with the opposite operator:

bpy.ops.wm.open_mainfile( filepath = MYPATH )

This way, you don't need to care what the value of bpy.data.filepath is.

$\endgroup$
  • $\begingroup$ Thanks for your response. If you save a file, then open it up, then delete the actual file while Blender is still running, then run print(bpy.data.filepath) in the console, it will return the path location from the file as though nothing happened. This means the value isn't being checked, but stored within the file $\endgroup$ – Sicut Unum Nov 30 '15 at 22:20
  • $\begingroup$ Sorry pressed enter to soon: Anyway.. This means the value is being stored within the file. The problem is I don't want the user's file to be force saved. So if I can save it as a temp file, run operations, then load that temp file and set its save location as the original file, then if after that point the user should choose to save, it will be as if nothing changed for the user. $\endgroup$ – Sicut Unum Nov 30 '15 at 22:30
  • $\begingroup$ So, to make sure I understand. The main issue you're worried about is what the default save path will be when the user opts to manually save the file? $\endgroup$ – TLousky Nov 30 '15 at 22:36
  • $\begingroup$ Correct, if they're working from an already saved file, they should be able to save without entering in a new location after every time the operator is run (or be forced to have their progress automatically saved when the operator is run). I've already figured out a bulky way of doing this but it would be so much cleaner if I could just set the internal save path. $\endgroup$ – Sicut Unum Nov 30 '15 at 22:40
  • $\begingroup$ Alright, so in that case I'd save another copy of the blendfile at the default path you want your user to save at using the save_as_mainfile operator, then delete that file (say using os.remove). Simple two step solution instead of copying, renaming and deleting. $\endgroup$ – TLousky Nov 30 '15 at 22:54
1
$\begingroup$

While I think that clear filepath is the same problem, another approach is to provide your own save operator as shown in Add Prefix to filename

To expand a bit more on creating your own save operator, you could also create a new save operator that hides the original by setting bl_idname = "wm.save_as_mainfile" for your operator. Your newer operator will hide the original save operator when you register it and the original will be available when your operator is unregistered. You will need to be sure to duplicate the properties and parameters of the original so that existing scripts don't break.

Disclaimer I don't think this is a good idea and expect it will be frowned on by many, also I haven't tested this past knowing that a duplicate bl_idname will cause the newly registered operator to be used before the old one.

$\endgroup$
  • $\begingroup$ Thanks for your response, I agree though I don't think this is a good solution but I like when people are willing to go as far as it takes to accomplish what they set out to do. $\endgroup$ – Sicut Unum Nov 30 '15 at 22:14

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.