Compiling Blender as a Python Module for Windows 10 x64 using Visual Studio

If you want to do unit tests of Blender Python code, it might be to your benefit to not startup Blender every time you want to run them but to just import Blender as a Python module and run them from the command line. This is especially important/nice if you want to automate your tests. Note, before you jump in, if you just need mathutils you can get that separately here.

Luckily, Blender's build has a nifty feature by which you can compile it as a Python module and then import blender from Python...

import bpy #Starts up Blender as Python module
from mathutils import Vector #Import a Blender specific library

Unfortunately for me, all of the tutorials I found were not geared toward the target I was looking for: Blender 2.78, Windows 10 x64, Microsoft Visual Studio Express 2013+, and Python 3.5. After a couple days I was able to get it all building and working with this tool chain and it should work just as well for x86, a different Python version, or a different MSVS version.

The Build Process

  1. Install needed programs.

    • SVN: to check out the precompiled Windows dependencies
    • CMake: to make Blender
    • Microsoft Visual Studio >2013: For Blender compilation
    • Blender Source Code or Git: Blender source code as downloaded from the website or git to checkout the exact commit you want to build
    • Python installation of your desired version of the same bitness you want to build Blender in (Determining Python Bitness).
  2. Create a directory structure like the following

.
..
blender/                      #Blender source goes in here
build/                        #This is where you have CMake target
lib/win[dows,64]_vc[12,14]/   #Prebuilt binaries. The name matters! `windows` for 32 bit or `win64` for 64 bit, `vc12` for MSVS12 (2013) or `vc14` for MSVS14 (2015) and MSVS15 (2017)
  1. Checkout the precompiled Windows dependencies based on your bitness desired

  2. Download the source (or checkout the git repo) into the blender/ folder.

    • Blender's Git repos though you'll want to clone https://projects.blender.org/blender/blender.git specifically.
    • If you're using git make sure to checkout the specific commit you want and also git submodule update --init --recursive
  3. In build/, run cmake -DWITH_PLAYER=OFF -DWITH_PYTHON_INSTALL=OFF -DWITH_PYTHON_MODULE=ON -DPYTHON_VERSION=3.5 ../blender

    • For 64 bit, you might need to force CMake to use a specific generator. -G"Visual Studio 15 Win64" where 15 is your Visual Studio version
    • Replace the Python version number with your chosen Python version
    • There are other configurable options found in blender/CMakeLists.txt if you want to enable/disable other features (like the game engine)
  4. In the Visual Studio Developer Command Prompt, also in build/, run devenv Blender.sln /Build [TARGET] /Project INSTALL where [TARGET] is a Visual Studio release target. Most likely you'll want Release

    • You can see the other targets if you open the the .sln file.
  5. You now have the built files. Copy the files into the Python's global site-packages directory

copy bin\bpy.pyd C:\Python35\Lib\site-packages\
copy bin\*.dll C:\Python35\Lib\site-packages\
del C:\Python35\Lib\site-packages\python35.dll
xcopy /E bin\2.78 C:\Python35\2.78\

You can now open up python and test the module. Just open python on the command line and type import bpy and you now have access to Blender's Python modules. bpy.app will also give you useful information about the current build.

If you prefer to install it in a virtual environment, the commands above work just the same, though with the last one, make sure that 2.78 is copied into your virtual env Scripts folder and not the root

Troubleshooting:

  • Something with _Insert_n mentioning Eigen: Go into the mentioned .h (.hpp?) and change vector_base::_Insert_n to vector_base::insert in the corresponding .cpp file.
  • %1 is not a valid Win32 application: You have built the 32 bit Blender and tried running it from 64 bit Python. Rebuild with the correct bitness.
  • bpy: couldnt find 'scripts/modules', blender probably wont start.: You need to install the .pyd and related files in Python's site-packages, otherwise it cannot find the supporting files.

Thanks to all the other wonderful people who wrote tutorials that got me part of the way through this build!