Build Blender as Python Module
Published: 2022-08-26 09:47:00

Blender is an open source 3d standalone application and it has a feature called scripting. Scripting is a tool that uses the python API to run and control blender. For example, you can create a cube using this script, can apply a texture for that and We can render output using this script as well. This is very useful for the creators to build complex modeling, automate a few steps in their pipeline, and more.

There is another way of using this script, It is “Blender as a Python Module” in external python programs. You may be able to import the blender module as “import bpy” and do the blender-related script in another application. It eliminates the difficulty of running the blender python script from an external file. And we don’t need to run the blender as a software instance in your pc.

The option to build Blender as a Python module is not officially supported, in the sense that Blender.org isn't distributing it along with regular releases. Currently, it's a build option you can enable, for your own use.

This is a build option to be able to import blender into python and access its modules
Here we have some of the possible uses of this module
rendering animations.

- image processing using Blender's compositor.
- video editing (using Blender's sequencer).
- importers, exporters (convert 3D file formats).
- development, accessing bpy from Python IDE's and debugging tools for example.
- automation.

Let’s dive into the initial setup

## Building Blender on Windows

Below instructions guide us to building Blender for Windows, with Microsoft Visual Studio.

## Install Development Tools

We need to install Subversion, Git, CMake and Visual Studio for windows.

- Install [Visual Studio 2019 or 2022 Community Edition](https://visualstudio.microsoft.com/) (free, be sure to install the 'Desktop Development with C++' workload)
- Install [Subversion for Windows](http://www.sliksvn.com/en/download) (SlikSVN)
- Install [Git for Windows](https://gitforwindows.org/)
   In the installer, choose to add Git to your PATH to ensure “make update” can correctly function.
- Install [CMake](https://cmake.org/)
   In the installer set the system path option to Add CMake to the system PATH for all users.

## Open the command prompt
Then open the command prompt window by hitting the Windows key+R, and then typing cmd.exe

> It is important that you use cmd.exe and not powershell or any other shell, the instructions below may not work otherwise.

## Download Sources and Libraries
Create a folder to store your copy of the Blender source code. This guide will assume your chosen folder is C:\blender-git.
Download the Blender source code:

```
cd C:\blender-git
git clone git://git.blender.org/blender.git
```

Download the Blender libraries:

```
cd C:\blender-git\blender
make update
```
make will automatically detect the libraries you need and offer to download them for you. Do note that this set of libraries is several gigabytes large and may take a while to download.
_Need to install Subversion for Windows before run this code. _


## Building
Easy (automated) way: see building blender docs for instructions, but run
`make bpy`

Everything should build as normal except in the cmake directory you will have ./bin/bpy.so (./bin/bpy.pyd on Windows) instead of ./bin/blender

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 –

```
C:\blender-git
          |------blender
          |------lib
          |------build_windows_Bpy_x64_vc16_Release

```

## Install 
Let’s add the python module to your development environment. This python version should match the python used Blender release. For example, I had Python 3.10 on my pc and I got the latest blender version that was built using Python 3.10. If this doesn't match you may face some compilation error. 

1. Create a virtual environment 

`virtualenv -p python python310`
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

```
C:\blender-dev
          |------blender
          |------lib
          |------build_windows_Bpy_x64_vc16_Release
          |------python310
```

2. Activate the virtual environment
`.\python310\Scripts\activate.bat`

3. Add the following files to the correct location

- copy the file **_bpy.pyd_** and _**all *.dll**_ files from _**blender-dev\build_windows_Bpy_x64_vc16_Release\bin\Release\**_ to **_python310\Lib\site-packages_**

- Inside _**blender-dev\build_windows_Bpy_x64_vc16_Release\bin\Release\**_ folder, you will also see a 3.xx folder depending on what version of Blender you built. In my case, it was 3.14.
Copy that folder and paste it into the system python directory.
For example: _**C:\Users\Jana\AppData\Local\Programs\Python\Python310**_

## Testing
Congratulations!!! Now, you should be good to go. 
Open your Command prompt and Run below command to make sure that everything is working fine.

```
python -c "import bpy; bpy.ops.render.render(write_still=True)"
```
It will render an image with the cube, you can find the output in C:/tmp/.png