Skip to content

Instantly share code, notes, and snippets.

@Fweeb
Last active September 22, 2022 15:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fweeb/6806446285ccf44c006020e5bde1d013 to your computer and use it in GitHub Desktop.
Save Fweeb/6806446285ccf44c006020e5bde1d013 to your computer and use it in GitHub Desktop.
How I retrieve the render path from a .blend file

Getting the render path (or any variable) from a .blend file

Update: I posted a related question to the Blender StackExchange and got a very helpful response on how some of this information could be extracted from a .blend file without launching Blender. So while the technique I describe below works, it might be better to use the method described in the StackExchange answer.

When doing my work, sometimes I need to get information out of a .blend file. For example, I sometimes need to do batch image processing (renaming, file conversion, etc.) on my rendered output. There's not a nice way to just query the .blend file for that information. Sure, there's BAM and even the old blenderaid tools, but they're a bit heavy-handed if you don't have them installed. So here's the hacky solution I'm currently using.

The following line of bash assigns a RENDERPATH variable using a Frankencode mash of bash, blender, python, grep, and sed. I'm assuming that the .blend file path as an argument passed into a larger bash script (i.e. ./do_something_with_renderpath.sh myblend.blend):

RENDERPATH=`blender -b $1 --python-expr "import bpy; print('RENDERPATH='+bpy.context.scene.render.filepath)" | grep --color=never RENDERPATH | sed -r 's/RENDERPATH=\/\/(.+)/\.\/\1/'`

Breaking it down, it launches Blender in background mode to open the .blend file (blender -b $1) and passes in a little Python expression to print the renderpath to standard out (import bpy; print('RENDERPATH='+bpy.context.scene.render.filepath)).

However, when Blender launches, it dumps a lot of text to standard out (even in background mode)... stuff like add-ons failing to load, the sound device in use, or other fun things. That means I need to search through those lines of Blender output for the specific lines from my Python expression. That's why I pipe Blender's output to grep and look specifically for the word "RENDERPATH" (grep --color=never RENDERPATH). Blender's standard output doesn't have anything that looks like this, so it's easy for grep to find it.

Then, once I have that line of text from standard out, I need to format it to something that a bash script can actually use. That means, trimming off the "RENDERPATH=" I used for helping grep. I also need to convert Blender's notation for relative paths (//) to standard bash notation (./). That's why the output from grep is piped to sed (sed -r 's/RENDERPATH=\/\/(.+)/\.\/\1/'). Regular expressions are crazy voodoo magic, but that blob of globbity-gook says, "Look for text that starts with RENDERPATH=//, save that text in a group ((.+)), and then replace it with that same text (\1), but prefixed with ./."

Yeah. Convoluted, right?

But on the upside, after doing this in a bash script, you can then use that variable for all sorts of interesting things. For example, the following script gets the render path from Blender and then coverts all of the rendered PNG images from 8-bit RGB into 1-bit grayscale using FFMPEG (thanks to intercube on IRC for the FFMPEG hint, BTW):

#!/bin/bash

RENDERPATH=`blender -b $1 --python-expr "import bpy; print('RENDERPATH='+bpy.context.scene.render.filepath)" | grep --color=never RENDERPATH | sed -r 's/RENDERPATH=\/\/(.+)/\.\/\1/'

ffmpeg -f image2 -i $RENDERPATH%04d.png -pix_fmt monob -threads 0 $RENDERPATH%04d.png

Yeah, it works. Yeah, I feel kind of clever for figuring out a solution. But holy hell... I wish there were an easier way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment