6
$\begingroup$

How can I save and close then reopen a blend file using a script, so that all data blocks are removed?

$\endgroup$
1
  • 1
    $\begingroup$ All caps is considered yelling... no need to be rude.... $\endgroup$
    – user1853
    Commented May 8, 2018 at 14:08

1 Answer 1

14
$\begingroup$

Just call save_as_mainfile() and open_mainfile() operators:

import bpy

bpy.ops.wm.save_as_mainfile(filepath=bpy.data.filepath)
bpy.ops.wm.open_mainfile(filepath=bpy.data.filepath)
$\endgroup$
2
  • $\begingroup$ Blender3.6.2: I get this error: Error: Cannot read file "/MyHome/BasePath/SomePath/MyFile.blend": Not a directory. And it ends up renaming SomePath to SomePath1 (the path where the file is), and it creates a small blend file at BasePath with the name "SomePath" (the blend file receives the name of the folder without the .blend extension, but that file has not the contents of the file I am trying to edit using the script, it has the default contents, the single cube, when we open blender without a file). $\endgroup$ Commented Aug 21, 2023 at 11:27
  • $\begingroup$ If the above happens, you need to place the file before the options like: blender MyFile.blend --background -P /MyHome/BasePath/AScript.py, now it worked. $\endgroup$ Commented Aug 21, 2023 at 11:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .