9
$\begingroup$

I have seen this question now many times and every question here and on other forum points to build blender as python module experimental tutorial by ideaman42. Now sadly a lot of answers are invalid right now because this central link is down. I couldn't find anything by Googling so if anybody has a fork of the tutorial or a second source that would be great? The link can't be gone for too long, I saw an answer with it 4 months ago.

The only other thing I have found is this tutorial for windows, but it also references the now missing tutorial. Any ideas how to find it?

$\endgroup$
  • $\begingroup$ Are looking for all of blender as a python module or just "fake version" to be used for coding in an IDE? $\endgroup$ – Aoradon Aug 29 '18 at 20:49
  • $\begingroup$ I just want to insert cubes a camera and perform a visibility test. I was thinking of maybe ray casting not sure yet. But yeah not the full blender $\endgroup$ – Hakaishin Aug 29 '18 at 21:01
  • $\begingroup$ The old wiki pages are still available with a slightly altered URL in blenders archived wiki. It really isn't that different to a normal blender build, mostly you need to enable the WITH_PYTHON_MODULE option. $\endgroup$ – sambler Aug 30 '18 at 5:44
  • $\begingroup$ I struggled with following the processes on the (archived) blender wiki, but managed to muddle through eventually with the help of this and a few other StackExchange questions. I've written up the full process I used on GitHub here $\endgroup$ – B.Roullier Feb 21 at 11:30
9
$\begingroup$

Here it is pulled from the archive.is (all Ideasman42 cited):

Overview


The official blender.org embeds a Python interpreter (CPython 3.x). This makes sense from the perspective of a user, who's primary needs are to have an application which can be extended with scripts.

However from the perspective of a Python developer, it can be useful to being Blender into your existing scripts and access its feature set.

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

Rationale


This is a build option to be able to import blender into python and access its modules

Possible uses include:

  • 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.

This is mainly limited to features which can be usable in background mode, so you cant for instance do OpenGL preview renders.

Prerequisites


OSX

get Python3.6-Framework from Python.org and install it.

Building


Assuming you have a CMake out-of-source build setup, see: http://wiki.blender.org/index.php/Dev:Doc/Building_Blender/Linux/Ubuntu/CMake

Change these CMake options from the defaults:

WITH_PYTHON_INSTALL=OFF
WITH_PLAYER=OFF
WITH_PYTHON_MODULE=ON

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

Installation


Linux

  • System Wide Install

    You may want install the module to the systems Python path, eg:

    /usr/lib/python3.6/site-packages
    

    For a system wide installation:

    WITH_INSTALL_PORTABLE=OFF
    

    note, PYTHON_SITE_PACKAGES will be used as the target path, but this is auto detected, nevertheless, you may want to modify.

    Once these optiosn are set, run:

    make install
    
  • Local Install

    Alternately you might want to use your user Python path (see https://www.python.org/dev/peps/pep-0370/)

    $HOME/.local/lib/python3.6/site-packages
    

    For a local install use the following options:

    WITH_INSTALL_PORTABLE=ON
    CMAKE_INSTALL_PREFIX=$HOME/.local/lib/python3.6/site-packages
    

    Once these optiosn are set, run:

    make install
    

Windows

copy bin\bpy.pyd C:\Python36\Lib\site-packages\
copy bin\*.dll C:\Python36\Lib\site-packages\
del C:\Python36\Lib\site-packages\python36.dll
xcopy /E bin\2.79 C:\Python36\

OSX

After compiling and "make install", copy needed files to your python framework

cp ./bin/bpy.so 
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/
cp -R ./bin/2.79 
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/

Note. Unlike on *nix C:\Python36\2.79 is not in the site packages, this is because of a difference in how Windows finds the scripts path and should eventually be fixed.

Testing


This allows 'bpy' to be imported from python or other applications/IDE's which embed python, eg:

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

This runs in background mode and has similar restrictions to running a script:

blender --background --python test.py

Troubleshooting


The Python version requirements are the same with building a regular blender binary (if Blender us using Python3.6 then there is NO WAY to use another version - 2.7/3.2/3.6 will all fail).

On Windows, you probably won't want to use a debug build, since this requires a debug python installation (python36_d.dll rather then python36.dll), so while it can be made to work, its more trouble.

$\endgroup$
  • $\begingroup$ Does making such an install interfere if I would already have installed blender normally over apt? My idea is to install blender then test something and if it works to automate it, but I'm not sure if an already existing blender installation over ubuntus package manager would interfere with building it as a python module $\endgroup$ – Hakaishin Aug 31 '18 at 11:45
  • 1
    $\begingroup$ @Hakaishin That shouldn't be a problem, you can install the builded module locally, there shouldn't be any interference $\endgroup$ – Jaroslav Jerryno Novotny Aug 31 '18 at 14:17
  • $\begingroup$ Do you know maybe if another version of python can be used like 3.6 or does it have to be 3.4? I vaguely remember reading somewhere that it will only work with 3.4 but I'm not sure $\endgroup$ – Hakaishin Sep 3 '18 at 8:07
  • $\begingroup$ @Hakaishin It should be the same version as is bundled with Blender, so for 2.79b python 3.5.3. You get the version like this $\endgroup$ – Jaroslav Jerryno Novotny Sep 3 '18 at 8:59
  • 1
    $\begingroup$ @Hakaishin Yes, this you can. You can compile Blender with python 3.6, pass -DPYTHON_VERSION=3.6 when configuring CMake. $\endgroup$ – Jaroslav Jerryno Novotny Sep 3 '18 at 10:40
3
$\begingroup$

I struggeld probably the same way as you did but i managed to actually build it as a Python module in the End.

I'm on a Ubuntu 18.04 LTS System so the following report on how i achieved it might depend on the system.

Now to the process of the solution.

Starting point were the following three sources as follows :

  1. Your Question here

  2. An Archived Blender Wiki entry

  3. The offical "Build from source" Wiki article

How did i achieve the build ?

I started by following the description provided of the 2. Source; which wasn't really precise in how to handle the source and so on and did not work for me in the end.

After a few failures of this, i went on by just following the 3. Source.

So i did what was stated there :

mkdir ~/blender-git
cd ~/blender-git
git clone https://git.blender.org/blender.git
cd blender
git submodule update --init --recursive
git submodule foreach git checkout master
git submodule foreach git pull --rebase origin master

Afterwards :

cd ~/blender-git
./blender/build_files/build_environment/install_deps.sh

Now I had every bit of blender code and all dependencies installed. Since i did not want blender compiled with UI (as normal programm) i was curious what (described in the next step, step 3, in Source 3.) make in ~/blender-git/blender offered. So i proceeded with simply calling make help which gave the following (truncated for brevity) output :

Convenience targets provided for building blender, (multiple at once can be used)
* debug     - build a debug binary
* full      - enable all supported dependencies & options
* lite      - disable non essential features for a smaller binary and faster build
* headless  - build without an interface (renderfarm or server automation)
* cycles    - build Cycles standalone only, without Blender
* bpy       - build as a python module which can be loaded from python directly
* deps      - build library dependencies (intended only for platform maintainers)

* config    - run cmake configuration tool to set build options

Note, passing the argument 'BUILD_DIR=path' when calling make will override the
default build dir.
Note, passing the argument 'BUILD_CMAKE_ARGS=args' lets you add cmake arguments.

"Oh Hey, look what we got!" There it was, a make bpy which promised to solve all my problems.

So i executed it and it compiled. But I ended up with the Python module in a, at least for my desires, wrong directory : /usr/local/PYTHON_SITE_PACKAGES-NOTFOUND. I wondered how this happend.

I soon figured out that I had to reconfigure cmake in the new created ~/blender-git/linux_build_bpy via ccmake linux_build_bpy while in ~/blender-git. There i needed to enable the Advanced Settings via pressing t and then editing the Key PYTHON_SITE_PACKAGES from PYTHON_SITE_PACKAGES-NOTFOUND to my desired Path /usr/lib/python3.6/site-packages.

After changing this i had to rebuild the project, which didn't actually rebuild since the changed setting didn't affect the main build process.

Rebuild in ~/blender-git/blender via make bpy.

In the End I also deleted the stuff in /usr/local/PYTHON_SITE_PACKAGES-NOTFOUND. (Ofc i could've just moved/copied it...)

That's how it worked form me. I hope this is helpful in any kind for anyone in the future.

NOTES:

  1. make bpy might need superuser-rights so do a sudo make bpy and don't worry about a complete rebuild (it's allready done just linking/copying in the end needs those rights).

  2. The configuration after the first build might not be needed or can be done before hand, but i didn't figure out how to do so.

  3. If followed through these steps, and the Python module (strangely a directory called 2.80 or simmilar number) exists in the desired path but yet you are not able to import the module bpy in any of your scripts, make sure that your desired path is included in your PYTHONPATH Env-variable.

$\endgroup$
1
$\begingroup$

If all you want is a "fake" version of blender's libraries so you can code bpy in an IDE with autocomplete, I'm currently using THIS open source library. It takes a running version of blender and strips out all of the data types and available functions.

Once downloaded it can be run with the following bash script with the paths changed to what they are on your local machine.

"PATH_TO_BLENDER\Blender.exe" -b -P "PATH_TO_PYTHONFILE\pypredef_gen.py"

The generated python files can be brought into your IDE and whereas they provide no functionality (The final script needs to be run in blender) you can code within your IDE of choice with autocomplete.

$\endgroup$
  • $\begingroup$ Good answer, but this is not my usecase I need to run the code outside of blender $\endgroup$ – Hakaishin Aug 29 '18 at 21:16
  • $\begingroup$ Ah sorry. I miss-understood from the comments earlier. $\endgroup$ – Aoradon Aug 29 '18 at 21:19
  • $\begingroup$ No problem, maybe somebody else wants to do exactly this $\endgroup$ – Hakaishin Aug 29 '18 at 21:20
1
$\begingroup$

If you have anaconda, it is quite simple to build a blender-python package, which will allow you to import bpy in a Python script. The following (slightly modified) instructions were obtained from (https://launchpad.net/blender-conda).

If you want to build from source, execute the following commands:

git clone https://git.launchpad.net/blender-conda
cd blender-conda
conda-build .
conda install python-blender --use-local

If you want to download prebuilt packages, use

conda install -c kitsune.one python-blender

Note that I have no relation to kitsune.one and cannot verify the integrity of the prebuilt packages.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.