92

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?

BenjaminGolder
  • 1,023
  • 1
  • 8
  • 6
  • 1
    also read through other questions tagged with command-line: http://blender.stackexchange.com/questions/tagged/command-line – zeffii Jun 25 '13 at 11:08
  • 2
    @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. – BenjaminGolder Jun 25 '13 at 11:10

4 Answers4

84

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

brockmann
  • 12,613
  • 4
  • 50
  • 93
Aldrik
  • 9,720
  • 22
  • 56
35

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
brockmann
  • 12,613
  • 4
  • 50
  • 93
Ruslan L.
  • 511
  • 5
  • 3
  • 2
    Links can go down. Please write some parts of the link. – Shady Puck Jul 15 '16 at 19:13
  • 1
    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. – Ruslan L. Jul 15 '16 at 20:37
8

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.
David
  • 49,291
  • 38
  • 159
  • 317
Ajay Rajawat
  • 91
  • 1
  • 1
5

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

cak3_lover
  • 477
  • 3
  • 11