53
$\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
52
$\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
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
2
$\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$

protected by David Nov 18 '18 at 15:14

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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