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.

1 Like
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?

I use a separate project that’s just a video sequencer with a custom panel that searches a directory for .blend projects, creates a list, and gives options to render in background, view rendered sequence, or migrate rendered images of each of the projects. I can share if you are interested. Right now it’s pretty specific to my workflow, but not hard to tweak

Yes please!

@MadeHuman Here you go!

RenderController_demo.zip (750.9 KB)

Although I saw from your first post you are on Mac? So this might not work, I use on Windows, and the command line ops in python haven’t been set to be cross-platform. Basically you just fill out the directory of your .blend files, then your directory of your rendered sequences. Then click “refresh accessible files” - you’ll see a list of all your blend projects and a blender icon next to them (open the project), and a render icon next to that (render/rerender in background). After rendering with this button you will see additional options, view in image sequencer, copy rendered images to new directory, (and I linked this to use TexturePacker in the background as well, outside of blender).

The renders always take the name of the .blend file they are rendered from, and use render settings from the file they point to.

So basically this works as if you are rendering in the background from a console, but with a nifty little UI and a way to view the rendered sequences easily. The zip here contains a folder of 2 blend files with simple animations of a cube if you want to quickly test if it is working.

Not the cleanest code or anything, this was just for personal use, so let me know if you hit any snags and I can try my best to answer.

Thank you!
Where in the blend do I find RenderControl.py
Could you please also send the Python as a separate file??

How do you “run” the UI???

If you just want to render the file you currently have open in Blender, you can also use the old standard:

import bpy

# Render frames 1 to 90
for step in range(1, 91):

    bpy.context.scene.frame_set(step)
    bpy.data.scene.render.filepath = 'PATH_NAME/frame_%d.png' % step
    bpy.ops.render.render(write_still=True)

Paste it into the Text Editor, set the path and the frame range, and click the Run Script button.

Yes! Thank you. This works perfectly, but I don’t seem to be able to use the Print function:

print(“done”)

It’s not important, but I am curious.

From the other answers, this is probably overkill for what you want to do, but might be insightful to read through the script. The panel should be visible in the properties UI of the video sequencer “render source control“ window. And is already registered, so if it didn’t run when opening the file might be a issue of your operating system not being windows. If you open a text editor window in the project, you’ll see the RenderControl.py (along with some others I might have had registered by default that don’t matter for this.) Run manually by selecting text>Run or the play button at the top of the text editor window

Tried the script (in Blender 3.4) to see if print() was an issue, and found a BIG whoops!

bpy.data.scene.render.filepath doesn’t exist. The correct version is this:

bpy.context.scene.render.filepath = 'PATH_NAME/frame_%d.png' % step

The mistake came from a sloppy copy paste edit of this line:

bpy.data.scenes["Scene"].render.filepath = 'PATH_NAME/frame_%d.png' % step

which lets you choose which scene to render.

Sorry for posting garbage code. :sob:

Anyway,

Here’s the updated & corrected version:

import bpy

# Render frames 1 to 90
for step in range(1, 91):

    bpy.context.scene.frame_set(step)

    # Render the current open scene
    bpy.context.scene.render.filepath = 'PATH_NAME/frame_%d.png' % step

    # Choose which scene to render
    # bpy.data.scenes["Scene"].render.filepath = 'PATH_NAME/frame_%d.png' % step

    bpy.ops.render.render(write_still=True)

print("Done")

I ran it and it rendered the frame(s) and printed “Done” in the console, so all should be good. :v:

Thanks! Ignoring the rendering for the moment, are you pasting your code into Scripting > Text Editor or into the Python Console?
print(“done”) does not work for me in the Scripting > Text Editor.

Ah, right. It prints “Done” in the System Console (Top Menu > Windows > Toggle System Console), not the Python Console or the Info window.

image

At least I assume this is still available on Windows. I’m on a Mac, so I use the Terminal app to launch Blender and read output.

1 Like