Launch blender from python and access API?

Hi, the last few months I’ve been using blender quite a bit for scientific visualizations and I’ve ended up implementing a python library for basic plotting to make the process easier: GitHub - Linusnie/blender-plots: Python library for plotting in blender with a matplotlib-like interface. Since the script editor in the GUI is quite limited I’ve been using blender notebook so that I can run analysis and plots from the same python environment in an interactive way

However one issue with this approach is that I’ll typically have a different python dependencies for each project I’m working on. So I end up having to install my (possibly conflicting) dependencies to the python interpreter in the blender install folder every time I switch projects, which gets messy quickly

Ideally I’d like to be able to do one of the following:

  1. tell blender what python interpreter to use with when I start it. This way I would just need to ensure bpy is installed in every new environment I create
  2. be able to launch blender from inside any python process and access the API. Something like e.g. import blender; bpy ="/path/to/executable") would launch a non-blocking blender GUI and then allow you to use the python API via bpy

Any thoughts? Is there a way to accomplish either of those options?

1 Like

Maybe you can run blender --python-use-system-env, with the PYTHONUSERBASE environment variable pointing to different user site package directories for each project.

There’s also PYTHONPATH, but completely replacing the Blender bundled site packages is likely to break something.

after trying some different options I’ve found creating a separate blender ipython kernel for each project works pretty well. For each new python environment:

  1. Activate the environment with source path/to/new_venv/bin/activate
  2. run python -m pip install blender_notebook
  3. run blender_notebook install --blender-exec=... --kernel-name=blender_new_venv

This way the site-packages paths from new_venv will automatically be added to sys.path at kernel startup (see here).

One caveat is that any library already installed in the blender python interpreter (e.g. numpy) will override whatever the corresponding version in new_venv is. This could possibly be reversed by changing the line linked earlier to sys.path.insert(1, RUNTIME_CONFIG['python_path']) which would give the venv packages higher priority (though risks breaking the API)

Thank you for your tips keep share with us.

1 Like

as of blender 3.4 it seems like it’s possible to pip install bpy to any python environment: Reference/Release Notes/3.4/Python API - Blender Developer Wiki. So it’s at least possible to run in headless mode, if anyone knows a way to connect/launch the GUI after import bpy I’d love to know!

I wouldn’t say any, support is for a narrow range only, py 3.10 and only on platforms/architectures we ship

if anyone knows a way to connect/launch the GUI after import bpy I’d love to know!

Not included in the bpy build afaik.

do you think a feature like import bpy; bpy.run_gui() could be feasible? Assuming all requirements regarding python version etc. are satisfied.

I guess the main difference would be that blender would need to somehow attach to a running python interpreter rather than launching it itself