Close
blender

Building Blender as Python Module on Windows 10

My Configuration:

  • Windows 10 x64
  • Blender version 2.92
  • Visual Studio 2019
  • Python version 3.7.9
  • Virtual Environment

Recently, I was struggling on my way to build Blender as a Python module when I started trying my hands on Blender addon development. Here, I am listing the steps which finally worked for me after banging my head for over a weekend.

Requirements – Install Development Tools

Follow this wiki to setup build environment. For ease, I am mentioning them here again.

Subversion, Git, CMake and Visual Studio must all be installed.

Download Source Code and Libraries

I am assuming (but not necessary), you are working on the D: drive of your system as I was, since I think it’s better to keep stuff separate from bootable C: drive.

Create a folder blender-dev and change directory to it. We will do everything inside this directory.

mkdir blender-dev
cd blender-dev

Clone the Blender source code by running below command. It will download around 400 MB of data. Time depends on your Internet speed.

git clone https://github.com/blender/blender.git

Go inside the newly created directory with blender source code and run below command which will download all the necessary libraries to build blender and update the source code if needed.

cd blender
make update

Please note that this will download more than 7 GB of files depending on your OS version and architecture. For me, this was 7.5 GB on Visual Studio 2019 and Windows 10 x64.

After the download finishes, you will have a new folder lib inside blender-dev folder so that this folder looks like below

D:\blender-dev
|------blender
|------lib

Building Blender as Python Module – bpy

Now to build Blender as Python Module, I tried to follow steps mentioned on wiki page. Make sure you are inside source code blender folder, and run

make bpy

This took around 15-20 minutes on my i7 system. Once this is done successfully, you will have another folder created inside blender-dev that looks something like build_windows_Bpy_x64_vc16_Release depending on you build setup. Now, your folder structure should look like –

D:\blender-dev
          |------blender
          |------lib
          |------build_windows_Bpy_x64_vc16_Release

Now, let’s create a Python virtual environment. This version should match Python used by Blender release. In my case, I had Python 3.7.9 installed on my system and Blender currently ships Python 3.7.7 but things went fine so I guess it should be okay even if your patch (last) number doesn’t match.

virtualenv -p python py37

This will create a folder named py37 and install a virtual environment inside that. This is the environment we can use for our projects / addon development. Your folder structure would look like this-

D:\blender-dev
          |------blender
          |------lib
          |------build_windows_Bpy_x64_vc16_Release
          |------py37
             

To activate this virtual environment, run

.\py37\Scripts\activate.bat

Most Important Steps:

  • Now, copy the file bpy.pyd and all *.dll files from blender-dev\build_windows_Bpy_x64_vc16_Release\bin\Release\ to py37\Lib\site-packages
  • Inside blender-dev\build_windows_Bpy_x64_vc16_Release\bin\Release\ folder, you will also see a 2.xx folder depending on what version of Blender you built. In my case, it was 2.92.

Now, the wiki says we need to copy this folder inside Python install directory but this thing didn’t work in my case with virtual environment. At least it was not clear to me from wiki. I kept getting error mentioned at the end of this post.

Finally, I figured out that even if we are using virtual environment, this folder will go to our system Python directory which is in my case C:\Users\sur365\AppData\Local\Programs\Python\Python37

So, just copy folder blender-dev\build_windows_Bpy_x64_vc16_Release\bin\Release\2.92 and paste it inside C:\Users\sur365\AppData\Local\Programs\Python\Python37 so that this folder contains a 2.92 folder like in the screenshot

Congratulations!!! Now, you should be good to go. Run below command to make sure that everything is working fine.

python -c "import bpy; bpy.ops.render.render(write_still=True)"

You should be able to see output something like below:

Error you get if your Blender 2.xx folder is not in the correct place

(p37) D:\workspace\blender-dev>python -c "import bpy"
Color management: using fallback mode for management
Color management: Error could not find role data role.
Color management: scene view "Filmic" not found, setting default "Standard".
blf_load_font_default: 'fonts' data path not found for 'droidsans.ttf', will not be able to display text
blf_load_font_default: 'fonts' data path not found for 'bmonofont-i18n.ttf', will not be able to display text
blf_load_font_default: 'fonts' data path not found for 'bmonofont-i18n.ttf', will not be able to display text
bpy: couldn't find 'scripts/modules', blender probably wont start.
Freestyle: couldn't find 'scripts/freestyle/modules', Freestyle won't work properly.
ModuleNotFoundError: No module named 'bpy_types'
ModuleNotFoundError: No module named 'bpy_types'
ERROR (bpy.rna): D:\workspace\blender-dev\blender\source\blender\python\intern\bpy_rna.c:7283 pyrna_srna_ExternalType: failed to find 'bpy_types' module
ModuleNotFoundError: No module named 'bpy_types'
ModuleNotFoundError: No module named 'bpy_types'
ERROR (bpy.rna): D:\workspace\blender-dev\blender\source\blender\python\intern\bpy_rna.c:7283 pyrna_srna_ExternalType: failed to find 'bpy_types' module
ModuleNotFoundError: No module named 'bpy_types'
ERROR (bpy.rna): D:\workspace\blender-dev\blender\source\blender\python\intern\bpy_rna.c:7283 pyrna_srna_ExternalType: failed to find 'bpy_types' module
ModuleNotFoundError: No module named 'bpy_types'
ERROR (bpy.rna): D:\workspace\blender-dev\blender\source\blender\python\intern\bpy_rna.c:7283 pyrna_srna_ExternalType: failed to find 'bpy_types' module
ModuleNotFoundError: No module named 'bpy_types'
ERROR (bpy.rna): D:\workspace\blender-dev\blender\source\blender\python\intern\bpy_rna.c:7283 pyrna_srna_ExternalType: failed to find 'bpy_types' module
ModuleNotFoundError: No module named 'bpy_types'
ERROR (bpy.rna): D:\workspace\blender-dev\blender\source\blender\python\intern\bpy_rna.c:7283 pyrna_srna_ExternalType: failed to find 'bpy_types' module
ModuleNotFoundError: No module named 'bpy_types'
ERROR (bpy.rna): D:\workspace\blender-dev\blender\source\blender\python\intern\bpy_rna.c:7283 pyrna_srna_ExternalType: failed to find 'bpy_types' module
F0110 21:40:41.153805 33188 utilities.cc:346] Check failed: !IsGoogleLoggingInitialized() You called InitGoogleLogging() twice!
*** Check failure stack trace: ***
    @   00007FFF03C87DEB  google::LogMessage::Flush
    @   00007FFF03C87439  google::LogMessageFatal::~LogMessageFatal
    @   00007FFF03C8BD93  google::glog_internal_namespace_::InitGoogleLoggingUtilities
    @   00007FFF0394124B  libmv_initLogging
    @   00007FFF02F4F78C  main_python_enter
    @   00007FFF0344B0A4  dealloc_obj_dealloc
    @   00007FFF3072404B  _PyObject_GenericSetAttrWithDict
    @   00007FFF3074C483  PyModule_AddObject
    @   00007FFF3074E0E0  _PyNamespace_New
    @   00007FFF3074E23F  _PyNamespace_New
    @   00007FFF3074E1A5  _PyNamespace_New
    @   00007FFF307154E0  _PyMethodDef_RawFastCallDict
    @   00007FFF3072BF5F  _PyEval_EvalFrameDefault
    @   00007FFF3071C0FC  _PyEval_EvalCodeWithName
    @   00007FFF3071B95C  _PyArg_UnpackStack
    @   00007FFF307292C8  _PyEval_EvalFrameDefault
    @   00007FFF3071B841  _PyArg_UnpackStack
    @   00007FFF30728484  _PyEval_EvalFrameDefault
    @   00007FFF3071B841  _PyArg_UnpackStack
    @   00007FFF30728A6E  _PyEval_EvalFrameDefault
    @   00007FFF3071B841  _PyArg_UnpackStack
    @   00007FFF30728A6E  _PyEval_EvalFrameDefault
    @   00007FFF3071B841  _PyArg_UnpackStack
    @   00007FFF30728A6E  _PyEval_EvalFrameDefault
    @   00007FFF3071B294  _PyFunction_FastCallDict
    @   00007FFF30714587  _PyObject_CallMethodIdObjArgs
    @   00007FFF3071444D  _PyObject_CallMethodIdObjArgs
    @   00007FFF30755036  PyBytes_Fini
    @   00007FFF30713FE0  PyImport_ImportModuleLevelObject
    @   00007FFF30749CA7  PyImport_ImportModuleLevel
    @   00007FFF0344D6B2  BPy_init_modules
    @   00007FFF0344AC18  BPY_python_start

Leave a Reply

Your email address will not be published. Required fields are marked *