Render using Python

When I want to render a file in the background, I often use something like this:

/Applications/Blender.app/Contents/MacOS/blender --background /Users/paul/Desktop/myfile.blend --render-output //filename_ --render-frame 1

But, how can I do the same thing as a line or two of Python code in a file? I would then call that Python file with:

/Applications/Blender.app/Contents/MacOS/blender --background /Users/paul/Desktop/myfile.blend --python Documents/code/mypythonfile.py

The result is the same, but I can also have many other lines of code in the Python file.

bpy.context.scene.render.filepath = "/where/to/save/the/output.png"
bpy.ops.render.render(write_still=True)
2 Likes

Ah! That’s great. Thank you.

I changed it very slightly:

bpy.context.scene.render.filepath = "/where/to/save/the/output"
bpy.ops.render.render(write_still=True)

Then the file format is given by the blend file and the suffix is added to the file name. And, if one wanted to change the file format:
bpy.context.scene.render.image_settings.file_format = 'PNG'

What do you think?