How can I save and close then reopen a blend file using a script, so that all data blocks are removed?
1 Answer
$\begingroup$
$\endgroup$
2
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)
-
$\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