96
$\begingroup$

Can I make Blender run a Python script without opening a GUI? If not, can I incorporate Blender's Python API into my own Python script without running a GUI?

$\endgroup$
0

5 Answers 5

85
$\begingroup$

Command-line / subprocess

  • You can use subprocess to run blender (like any other application) from python.
  • Use the -b / --background switch to run blender in the backgroud (GUI-less).
  • Use the -P <filename> / --python <filename> switch to load desired python script.
    • Or use --python-console to run python from stdin.

Example: blender --background --python myscript.py

See: https://docs.blender.org/manual/en/latest/advanced/command_line/arguments.html

As module

This is an experimental feature and not enabled by default, but Blender can be compiled as a python module.

This allows 'bpy' to be imported from python or other applications/IDE's which embed python

$\endgroup$
0
38
$\begingroup$

All what Aldrik wrote, and more Blender Python API Tips and Tricks

From official Blender documentation:

For scripts that are not interactive it can end up being more efficient not to use Blenders interface at all and instead execute the script on the command line.

blender --background --python myscript.py

You might want to run this with a blend file so the script has some data to operate on.

blender myscene.blend --background --python myscript.py
$\endgroup$
0
8
$\begingroup$

For 2.79b

#blender --background --factory-startup --python $HOME/background_job.py -- \
#          --text="Hello World" \
#          --render="/tmp/hello" \
#          --save="/tmp/hello.blend"
#
# Notice:
# '--factory-startup' is used to avoid the user default settings from
#                     interfering with automated scene generation.
#
# '--' causes blender to ignore all following arguments so python can use them.
#
# See blender --help for details.
$\endgroup$
8
$\begingroup$

you can also directly run expressions without having to create a new .py file
like this:

blender "/path/to/file.blend" -b --python-expr "import bpy;print(bpy.data.filepath)"

In case anyone wants to run a script which involves indentations, for e.g. a loop:

blender "/path/to/file.blend" -b --python-expr $'import bpy\nfor obj in bpy.data.objects:\n    print(obj.name)'

You can also run one script from a blender file to change another blend file!

import bpy,subprocess

file_path="/path/to/another/file.blend"

code='''
import bpy
for obj in bpy.data.objects:
        print(obj.name)
'''

output = subprocess.check_output(f"blender '{file_path}' -b --python-expr '{code}'", shell=True)
print(output.decode('UTF-8'))

Heck, if you're crazy enough you can run a script from one blend file to run a script in another blend file all in a one liner from the command line

blender "/path/to/file.blend" -b --python-expr $'import bpy,subprocess\nprint("Objects in file ",bpy.data.filepath)\nfor obj in bpy.data.objects:\n        print(obj.name)\nfile_path="/path/to/another/file.blend"\ncode="""\nimport bpy\nprint("Objects in file ",bpy.data.filepath)\nfor obj in bpy.data.objects:\n        print(obj.name)\n"""\noutput = subprocess.check_output(f"blender \'{file_path}\' -b --python-expr \'{code}\'", shell=True)\nprint(output.decode(\'UTF-8\'))\n'

The possibilities are endless

$\endgroup$
0
3
$\begingroup$

Update from 2024:

You can now install bpy from pypi: https://pypi.org/project/bpy/.

Then, you can run a script in Python3.11 (other Python versions won't work!).
I like using blender in Jupyternotebooks e.g. with this scirpt:

import bpy
from IPython.display import display, Image

path = "test.png"
bpy.context.scene.render.resolution_x = 500
bpy.context.scene.render.resolution_y = 200
bpy.ops.render.render()
bpy.data.images["Render Result"].save_render(filepath=path)
display(Image(filename=path))

I've also made a small script collection here: https://github.com/kolibril13/ipyblender-experimental

enter image description here

$\endgroup$

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.