92
$\begingroup$

I'm interested in using Blender for producing images, 3d files, or calculating geometry on a server.

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$
2
  • 1
    $\begingroup$ also read through other questions tagged with command-line: blender.stackexchange.com/questions/tagged/command-line $\endgroup$
    – zeffii
    Jun 25, 2013 at 11:08
  • 2
    $\begingroup$ @zeffii thanks. I was aware that Blender could render from command line, but was unsure of broader usage. At this point all the command line questions deal with rendering. $\endgroup$ Jun 25, 2013 at 11:10

4 Answers 4

84
$\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$
2
  • $\begingroup$ The "compiled as a python module" link is broken $\endgroup$ Mar 26, 2019 at 15:57
  • $\begingroup$ I think these are the updated instructions for Blender 2.8. $\endgroup$
    – bluenote10
    Oct 7, 2019 at 7:03
35
$\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$
2
  • 2
    $\begingroup$ Links can go down. Please write some parts of the link. $\endgroup$
    – Shady Puck
    Jul 15, 2016 at 19:13
  • 1
    $\begingroup$ This is the link to the official Blender API documentation, even if the link will be broken, then anyone can find documentation. Anyway, I've updated my answer. $\endgroup$
    – Ruslan L.
    Jul 15, 2016 at 20:37
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$
5
$\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$
2
  • $\begingroup$ is it possible to run loops with python expressions? $\endgroup$ Nov 10, 2022 at 15:33
  • $\begingroup$ @JuanManuelLynch yes it is! I've updated the answer $\endgroup$
    – cak3_lover
    Nov 10, 2022 at 20:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .