0
$\begingroup$

I have been experimenting with the background rendering of multiple files in Blender using the command line (Windows) and I have two questions:

  1. Is there a way to tell the command line to render ALL the Blender files in a given directory? For instance, I have a folder /AbstractRenders/ with five .blends named AbstractRender01.blend, AbstractRender02.blend, etc. Can I give Blender the instruction to render the first frame of each file without explicitly stating an absolute path and -f 1 for each of them?

  2. If so, can I somehow automate the naming of the output image? Right now, I have the same Image output set for the compositor. It then adds the default #### (0001Image) for each render therefore overwriting the files sequentially. The result being I only get the last render output in line. The obvious solution is to setup each file in advance with a different name so that it doesn't overwrite them, which I did, but I was wondering whether there is an instruction that could, for instance, tell Blender to take the .blend file's name and use it for the image output name? Or any other way to ensure they don't overwrite without having to state image output name explicitly again.

$\endgroup$

1 Answer 1

0
$\begingroup$

In your directory, create a render_batch.bat, and open it with your favorite text editor (preferably one with batch syntax highlighting like sublime text, but the default notepad works too).

In there, put the following code:

@echo off
setlocal enabledelayedexpansion

:: Path to Blender executable
set "BLENDER_EXEC=blender"

:: Path to work directory
set "WORK_DIR=%~dp0"

:: Path to output directory for rendered images
set "OUTPUT_DIR=%~dp0RenderedImages"

:: Ensure the output directory exists
if not exist "%OUTPUT_DIR%" mkdir "%OUTPUT_DIR%"

:: Iterate through .blend files and render the first frame
for %%F in ("%WORK_DIR%\*.blend") do (
    set "FILENAME=%%~nF"
    %BLENDER_EXEC% -b "%%F" -o "%OUTPUT_DIR%\!FILENAME!_####" -f 1
)

echo Rendering complete.
pause

What you need to adapt:

  • Set the path to your blender.exe executable.
    • Leaving blender alone works only if you added blender's directory to your PATH environment variables, which you probably should see how to do if you often use Blender in command line.
    • If you don't know where the Blender executable could be, see "System Directories" in Blender’s Directory Layout - Blender 4.3 Manual, , but omit the version number folder. It's probably something like %ProgramFiles%\Blender Foundation\Blender\blender.exe
  • Set your work directory (the /AbstractRenders/ you mentionned). Use %~dp0 for path_to_bat_file/, which is what I left by default, and which will assume that your blend files are in the same folder as the bat file.
  • Set your output directory, where to save files. Using %~dp0RenderedImages will save the files to path_to_bat_file/RenderedImages/. You can leave it to %~dp0 if you prefer to keep the image files in the same folder as the bat file.

What it will do:

  • For each blend file in WORK_DIR:
    • Get the blend file name
    • Render the first frame
    • Save render to OUTPUT_DIR/blendFileName_####
  • pause and wait for a keypress to close, so that you can inspect the console output if any issue happened.
$\endgroup$
2
  • $\begingroup$ This is great, thank you! I noticed we are starting Blender anew with every file. It is not at all a big deal but if there was a way to only start Blender once? Something like ...\blender -b FILE1 -f 1 FILE2 -f1 FILE3 -f 1 where 'FILE' stands for the absolute path to my .blends, as I had been using previously, but with cmdline optimization? This only opens Blender once and renders all of the files. $\endgroup$ Commented Feb 16 at 4:07
  • $\begingroup$ AFAIK Blender CLI doesn't allow it. You could achieve that result by writing a python script that does the file opening and rendering, and use blender -b -P myscript.py to run Blender CLI with the script. But to be honest, this just complexify things IMHO. Opening blender in cli doesn't cost much compared to the time it takes to open a file, it's like 2 seconds tops on my system and it's with lots of addons that aren't necessary for rendering, and less than 400ms with ` --factory-startup`. $\endgroup$ Commented Feb 16 at 17:14

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.