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

@shikolad
Copy link

Thank you a lot, this helped me to finally make my script work!)

@italosalgado14
Copy link

What is -DPYTHON_ROOT_DIR? I try to use this in a virtualenv D:

@chrdiller
Copy link
Author

What is -DPYTHON_ROOT_DIR? I try to use this in a virtualenv D:

This should point to where your python is installed, for me it was a global pyenv installation but it should also work if you point it to your virtualenv

@samuel-pate
Copy link

I tried this installation but there's a problem :

-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- WITH_DRACO requires WITH_PYTHON_INSTALL to be ON, disabling WITH_DRACO for now
-- Performing Test SUPPORT_SSE_BUILD
-- Performing Test SUPPORT_SSE_BUILD - Success
-- SSE Support: detected.
-- Performing Test SUPPORT_SSE2_BUILD
-- Performing Test SUPPORT_SSE2_BUILD - Success
-- SSE2 Support: detected.
-- Found Git: /usr/bin/git (found version "2.25.1") 
-- Found JPEG: /usr/lib/x86_64-linux-gnu/libjpeg.so (found version "80") 
-- Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found version "1.2.11") 
-- Found PNG: /usr/lib/x86_64-linux-gnu/libpng.so (found version "1.6.37") 
-- Found Freetype: /usr/lib/x86_64-linux-gnu/libfreetype.so (found version "2.10.1") 
-- Found PythonLibsUnix: /usr/local/lib/libpython3.7m.a  
-- Could NOT find OpenEXR (missing: _openexr_LIBRARIES OPENEXR_INCLUDE_DIR) 
-- Could NOT find OpenJPEG (missing: OPENJPEG_LIBRARY OPENJPEG_INCLUDE_DIR) 
-- Could NOT find TIFF (missing: TIFF_LIBRARY TIFF_INCLUDE_DIR) 
-- Could NOT find OpenAL (missing: OPENAL_LIBRARY OPENAL_INCLUDE_DIR) 
-- Could NOT find SDL2 (missing: SDL2_LIBRARY SDL2_INCLUDE_DIR) 
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Could NOT find SDL (missing: SDL_LIBRARY SDL_INCLUDE_DIR) 
-- Could NOT find Jack (missing: JACK_LIBRARY JACK_INCLUDE_DIR) 
-- Could NOT find SndFile (missing: LIBSNDFILE_LIBRARY LIBSNDFILE_INCLUDE_DIR) 
-- Could NOT find Fftw3 (missing: FFTW3_LIBRARY FFTW3_INCLUDE_DIR) 
-- Could NOT find OpenCOLLADA (missing: _opencollada_LIBRARIES _opencollada_INCLUDES) 
-- Could NOT find Spacenav (missing: SPACENAV_LIBRARY SPACENAV_INCLUDE_DIR) 
-- Could NOT find OSL (missing: _osl_LIBRARIES OSL_INCLUDE_DIR OSL_COMPILER) 
-- OSL not found, disabling it from Cycles
-- Could NOT find OPENVDB (missing: OPENVDB_LIBRARY OPENVDB_INCLUDE_DIR) 
-- Could NOT find BLOSC (missing: BLOSC_LIBRARY BLOSC_INCLUDE_DIR) 
-- OpenVDB not found, disabling it
-- Could NOT find ALEMBIC (missing: ALEMBIC_LIBRARY ALEMBIC_INCLUDE_DIR) 
-- Could NOT find Boost (missing: Boost_INCLUDE_DIR filesystem regex thread date_time locale system) (Required is at least version "1.48")
-- Could NOT find Boost (missing: Boost_INCLUDE_DIR filesystem regex thread date_time locale system) (Required is at least version "1.48")
-- Could NOT find OpenImageIO (missing: OPENIMAGEIO_LIBRARY OPENIMAGEIO_INCLUDE_DIR) 
-- OpenImageIO not found, disabling WITH_CYCLES
-- Could NOT find OpenColorIO (missing: _opencolorio_LIBRARIES OPENCOLORIO_INCLUDE_DIR) 
-- OpenColorIO not found
-- Could NOT find OPENIMAGEDENOISE (missing: OPENIMAGEDENOISE_LIBRARY OPENIMAGEDENOISE_INCLUDE_DIR) 
-- OpenImageDenoise not found
-- Found LLVM: /usr/lib/llvm-10/lib/libLLVM-10.0.0.so  
-- Could NOT find OpenSubdiv (missing: _opensubdiv_LIBRARIES OPENSUBDIV_INCLUDE_DIR) 
-- OpenSubdiv not found
-- Found TBB: /usr/lib/x86_64-linux-gnu/libtbb.so  
CMake Error at CMakeLists.txt:815 (message):
  WITH_MOD_OCEANSIM requires WITH_FFTW3 to be ON


-- Configuring incomplete, errors occurred!

Any idea to fix it ?

@FiveFootSeventeen
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.

@TigerYuHuzi
Copy link

WITH_MOD_OCEANSIM requires WITH_FFTW3 to be ON

Same issues

@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