Skip to content

Instantly share code, notes, and snippets.

@Rich2020
Forked from chrdiller/Blender as a Module.md
Created September 20, 2022 20:48
Show Gist options
  • Save Rich2020/c624f395fef774a0ee8ef9ba518ea9ff to your computer and use it in GitHub Desktop.
Save Rich2020/c624f395fef774a0ee8ef9ba518ea9ff 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

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