Environment Variables As Arguments For Blender Python Scripts

Passing Environment Variables to Blender from the Command Line

Environment variables are dynamic, named values that can affect the way running processes will behave on a computer. They are part of the environment in which a process runs. When launching Blender from the command line, environment variables can be passed as arguments to configure Blender before startup.

The key reasons for passing environment variables to Blender from the command line are:

  • To customize Blender’s functionality for a specific task or workflow
  • To parameterize Blender Python scripts by externalizing configuration options
  • To access reusable values like file paths and credentials across multiple scripts
  • To integrate Blender with other applications using shared environment variables

By learning how to effectively pass environment variables from the terminal or command prompt, Blender artists and developers can optimize and automate their Blender-based tools and pipelines.

What are Environment Variables and Why Pass Them to Blender?

Environment variables are named values accessible by all running processes on an operating system. They are useful for customizing software tools and automating workflows by externalizing configuration details from code or scripts into the environment.

Some common examples include:

  • $PATH – directories to search for executable programs
  • $HOME – user’s home directory path
  • $TMPDIR – path for temporary files
  • $DBUS_SESSION_BUS_ADDRESS – integration with desktop services

When launching Blender, custom environment variables can be passed in to parametrize Blender Python scripts. This allows artists and developers to:

  • Reuse scripts across projects using different external data
  • Access remote resources like datasets and credentials
  • Share common settings across a pipeline of connected Blender & Python processes
  • Automate complex multi-step workflows from a single terminal command

Overall, exporting configuration through environment variables makes Blender pipelines more customizable, automated, and scalable.

Accessing Environment Variables in Blender Python Scripts

Inside Blender, the values of environment variables passed through the command line are accessible to Python scripts through the os module:


import os

production_api_key = os.getenv('PROD_API_KEY') 
scene_objects = get_remote_data(api_key=production_api_key)

The key function is os.getenv(), which retrieves the value of a supplied environment variable name. This allows Blender Python logic to adapt based on external configuration parameters.

Here are some examples of useful tasks:

  • Get file paths to load project assets from
  • Access online databases, APIs, credentials
  • Inherit settings from production pipeline tools
  • Parametrize loads, physics, materials based on scene

The values passed to getenv as environment variables can be read seamlessly as strings, ints, floats etc. within Python scripts.

Setting and Getting Environment Variables in Python

To set environment variables from Python that child processes can inherit, the standard os module also provides:

  
import os
os.environ['CUSTOM_SETTING'] = 'My Value' 
child_process = Popen(cmd, env=os.environ.copy())

This exports a new pair into the environment variables dictionary that can be accessed by child processes.

To manage environment variables purely within Python, dotenv provides more functionality like loading from .env files and type safety:


from dotenv import load_dotenv, set_key, get_key

load_dotenv() # Parse .env file

set_key('PORT', 5000) # Sets PORT=5000
print(get_key('PORT')) # Prints 5000 

These tools give full control over environment variables within Python for configuring Blender and other processes.

Real-World Examples

Setting the Blender Install Path

For pipelines running automated Blender jobs, dynamically finding the Blender executable can be useful. An environment variable can store this:

  
import os
import subprocess

blender_path = os.getenv('BLENDER_PATH', '/opt/blender/blender') 

subprocess.call([blender_path, '--python', 'my_script.py'])

Then launch processes using:


BLENDER_PATH=/myapp/blender ./start_pipeline.sh 

This way a single script can adapt to different departmental pipelines by inheriting the $BLENDER_PATH variable.

Passing Animation Parameters

For a reusable animation rendering script, input parameters can be passed through environment variables:


import os
import bpy

# Get animation parameters
start_frame = int(os.getenv('START_FRAME', 1))
end_frame = int(os.getenv('END_FRAME', 250)) 
output_path = os.getenv('OUTPUT_PATH', '/renders')

# Render animation frames
bpy.context.scene.frame_start = start_frame
bpy.context.scene.frame_end = end_frame 

bpy.context.scene.render.filepath = output_path 
bpy.ops.render.render(animation=True) 

Then change parameters per render by setting variables:


START_FRAME=100 END_FRAME=200 OUTPUT_PATH=/new/renders blender -b animation.blend -P render.py

This helps automate batch processing animations with different configurations.

Controlling Scene Properties

Replicating scene settings across projects is also possible with environment variables:


import os
import bpy

engine = os.getenv('RENDER_ENGINE', 'CYCLES')
samples = int(os.getenv('SAMPLES', 100))

bpy.context.scene.render.engine = engine
bpy.context.scene.cycles.samples = samples  

Launch with variable overrides:

  
RENDER_ENGINE=EEVEE SAMPLES=30 blender -P render_base_setup.py

This enables reusable logic while projects customize settings through the OS environment.

Common Pitfalls and Troubleshooting

When passing environment variables into Blender, some common issues include:

  • Spelling variable names incorrectly – double check capitalization
  • Blender’s OS environment differs from terminal – verify inheritance
  • Editing unrelated config files instead of command arguments
  • Security with credentials – use chroot / containers / secrets management

Some tips for troubleshooting:

  • Print os.environ to inspect full environment
  • Test setting variables directly in Python terminal
  • Launch Blender directly from script to isolate issues
  • Enable debug logs for environment handling in related Python modules

Overall, visually confirm variables pass correctly at each step and minimize unrelated configurations that could interfere.

Additional Resources

For more on using environment variables with Blender and Python, check out these additional references:

With some practice, environment variables become a powerful tool for building configurable, reusable, and automated Blender pipelines. Mastering this technique unlocks simpler Python logic and more dynamic workflows.

Leave a Reply

Your email address will not be published. Required fields are marked *