Script to save file

I would like to be able to save .blend file from the script or command line. I call .blend file with .py file which alters the scene and renders it. It should also save the new state as .blend.

I have seen question asked before but have found no answer


# save blend
bpy.ops.wm.save_mainfile()
# save as
bpy.ops.wm.save_as_mainfile(filepath=filepath)

1 Like

Works as a breeze. The “bpy.ops.wm.save_mainfile()” variation also requires “filepath=filepath” seemingly though. Else I get “untitled.blend” there in the directory.

Perhaps that’s when you haven’t already saved that file.

If bpy.data.filepath is set, ie it’s been saved once you get a filepath, otherwise you get ‘’ which will give the untitled.blend when saving, just like it does from the filebrowser.

if bpy.data.filepath == ‘’:
save as
else
save

1 Like