57
$\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$
  • 1
    $\begingroup$ also read through other questions tagged with command-line: blender.stackexchange.com/questions/tagged/command-line $\endgroup$ – zeffii Jun 25 '13 at 11:08
  • 1
    $\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$ – BenjaminGolder Jun 25 '13 at 11:10
53
$\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

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$
  • $\begingroup$ The "compiled as a python module" link is broken $\endgroup$ – frank Mar 26 at 15:57
  • $\begingroup$ I think these are the updated instructions for Blender 2.8. $\endgroup$ – bluenote10 Oct 7 at 7:03
24
$\begingroup$

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

Updated

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
    $\begingroup$ Links can go down. Please write some parts of the link. $\endgroup$ – Shady Puck Jul 15 '16 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 '16 at 20:37
4
$\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$

Not the answer you're looking for? Browse other questions tagged or ask your own question.