Skip to content

Instantly share code, notes, and snippets.

@chrdiller
Last active May 20, 2024 12:10
Show Gist options
  • Save chrdiller/ae72a70ce7414ec9d35609536113345c to your computer and use it in GitHub Desktop.
Save chrdiller/ae72a70ce7414ec9d35609536113345c to your computer and use it in GitHub Desktop.
Installing Blender Python Module on Ubuntu 20.04

Summary: How to install Blender as a python module into your venv:

Prerequisites

  • Install current python3.7 version with pyenv (Ubuntu 20.04 ships with 3.8)
    • Install with fPIC: env PYTHON_CFLAGS=-fPIC pyenv install 3.7.8
  • Install numpy globally for that version with pip

Installation

$ git clone https://git.blender.org/blender.git; cd blender
$ git checkout v2.82a
$ git submodule update --init --recursive
$ mkdir lib; cd lib; svn checkout https://svn.blender.org/svnroot/bf-blender/tags/blender-2.82-release/lib/linux_centos7_x86_64; cd ..
$ mkdir build; cd build
$ cmake .. -DWITH_PYTHON_INSTALL=OFF -DWITH_PYTHON_MODULE=ON -DWITH_INSTALL_PORTABLE=ON -DWITH_CYCLES_EMBREE=OFF -DWITH_MEM_JEMALLOC=OFF -DPYTHON_ROOT_DIR=/path/to/.pyenv/versions/3.7.8/ -DCMAKE_INSTALL_PREFIX=/path/to/venv/lib/python3.7/site-packages/
$ make -j8
$ make install

Test with python -c "import bpy; bpy.ops.wm.save_as_mainfile(filepath='my.blend')"

Remaining issues

  • ImportError: /usr/lib/x86_64-linux-gnu/libjemalloc.so.2: cannot allocate memory in static TLS block
    • Temporary fix: env LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2 python -c 'import bpy'

Resources

Blender Building Guide

Blender As a Python Module

@BatmanofZuhandArrgh
Copy link

This response is probably too late for your problem but for those who come upon this later this may help. There is a dependency installation script in the blender repo ./build_files/build_environment/install_deps.sh if you run that it may fix some errors associated with missing dependencies.

I did this and it worked. Also you have to install numpy to that python version's site-packages, not the virtualenv's site packages

@BatmanofZuhandArrgh
Copy link

Thank you guys!

@danmaier2010
Copy link

danmaier2010 commented Nov 11, 2022

Install Blender as a python module into Blender python environment in a clean Ubuntu 20.04 :

sudo apt update -y
sudo apt install -y build-essential git subversion cmake libx11-dev libxxf86vm-dev libxcursor-dev libxi-dev libxrandr-dev libxinerama-dev libegl-dev
sudo apt install -y libwayland-dev wayland-protocols libxkbcommon-dev libdbus-1-dev linux-libc-dev

mkdir ~/blender-git
cd ~/blender-git
git clone https://git.blender.org/blender.git

mkdir ~/blender-git/lib
cd ~/blender-git/lib
svn checkout https://svn.blender.org/svnroot/bf-blender/trunk/lib/linux_centos7_x86_64

cd ~/blender-git/blender
git submodule update --init --recursive

cd ~/blender-git/blender
sudo make bpy

copy bpy build with add-ons into blender internal python dist

sudo cp -a /~/blender-git/build_linux_bpy/bin/bpy /~/blender-git/lib/linux_centos7_x86_64/python/lib/python3.10/site-packages/

run from that distribution

~/blender-git/lib/linux_centos7_x86_64/python/bin/python3.10 -c "import bpy"

extend from here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment