Starting with bpy Blender Python – Part 1: Installation

posted in: Development, Tutorials | 0

Introduction

This is the first tutorial of the Blender Python development with the Blender Python module called ‘bpy’. Furthermore this tutorial is split into different parts. Part 1 will handle the installation of the ‘bpy’ module on a Windows 10 host system.

Unfortunately the ‘bpy’ module cannot be used outside of Blender. Meaning that the following instruction will most likely not work in the end. Nevertheless feel free to try it out. In case you have been successful feel free to inform me so that I can update this blog post.

Thank you in advance.

Installation

General instructions

Instead of following the next sections and figure out one error message after the other I recommend to pre-install the following tools and add their executables to the Windows PATH variable:

  • Python 3.x
  • Python module ‘future_fstrings’
  • Visual Studio Community 2019
  • Tortoise SVN including ‘command line client tools’
  • cmake
  • If necessary perform steps in ‘Error: No CMAKE_C_COMPILER could be found (2)’

The installation will take several minutes depending on the host system. Most noteworthy is that the installation will take a few GB of data on your system drive, so make sure you have free space available.

Install Python

Install one of the latest Python versions here. Python 3.8.1 seems to be sufficient as this version was the newest available at that date. To make things easier the Python binaries should be added to the PATH variable so that immediate command line execution (also from other tools) is possible.

Install the ‘bpy’ module

Install the ‘bpy’ module by using pip. When pip is not part of the environment variable PATH you find the pip executable in the Python install_dirscriptspip.exe

pip install bpy --no-cache-dir

Error: future_fstrings

Most likely you will get an error message. At least that is what happend to me. The error message will look similar to this command line excerpt:

    ERROR: Command errored out with exit status 1:
     command: 'd:installationspython381python.exe' -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'...Temppip-install-nmjr3o29bpysetup.py'"'"'; __file__='"'"'...Temppip-install-nmjr3o29bpysetup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base '...Temppip-install-nmjr3o29bpypip-egg-info'
         cwd: ...Temppip-install-nmjr3o29bpy
    Complete output (16 lines):
    Traceback (most recent call last):
      File "d:installationspython381libtokenize.py", line 342, in find_cookie
        codec = lookup(encoding)
    LookupError: unknown encoding: future_fstrings

    During handling of the above exception, another exception occurred:
    [...]
    SyntaxError: unknown encoding for '...pip-install-nmjr3o29bpysetup.py': future_fstrings
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

To emphasise is the line ‘LookupError: unknown encoding: future_fstrings’. This indicates that the module ‘future_fstrings’ is not working properly because it is not installed. Install the module with pip:

pip install future_fstrings --no-cache-dir

Error: Tortoise SVN ‘command line client tools’ not installed

If you have not installed Tortoise SVN yet you need to do this. The ‘bpy’ modul is located in a SVN lib which is fetched with the tool Tortoise SVN. Important is to install the ‘command line client tools’ as well, otherwise the installation will not work. You will get a similar error message like shown in the next section.

Error: ‘.blenderpylibwin64_vc14’ locked

In addition it was not sure how long the installation may take that’s why I aborted the first installation because I thought it crashed or freezed somehow. This is why the working space was locked. To unlock you must remove the folder mentioned in belows error message. For me it was ‘C:\users\user\.blenderpy’. Just remove the complete ‘.blenderpy’ folder. If you do not see the folder you need to unhide system files and folders in the windows folder options. HowToGeek explains that.

    ERROR: Command errored out with exit status 1:
     command: 'd:installationspython381python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'..LocalTemppip-install-zbeppzcrbpysetup.py'"'"'; __file__='"'"'..Temppip-install-zbeppzcrbpysetup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record '..LocalTemppip-record-_6dkr3k7install-record.txt' --single-version-externally-managed --compile
         cwd: ..LocalTemppip-install-zbeppzcrbpy
    Complete output (51 lines):
    running install
    running build
    running build_ext
    Preparing the build environment
    Checking out svn libs from https://svn.blender.org/svnroot/bf-blender/trunk/lib/win64_vc14
    warning: BuildCMakeExt: Windows users must have the svn executable available from the command line

    warning: BuildCMakeExt: Please install Tortoise SVN with "command line client tools" as described here

    warning: BuildCMakeExt: https://stackoverflow.com/questions/1625406/using-tortoisesvn-via-the-command-line
   [...]
        raise svn.exception.SvnException(
    svn.exception.SvnException: Command failed with (1): ['svn', '--non-interactive', 'checkout', 'https://svn.blender.org/svnroot/bf-blender/trunk/lib/win64_vc14', '...blenderpylibwin64_vc14']
    b"svn: E155004: Run 'svn cleanup' to remove locks (type 'svn help cleanup' for details)rnsvn: E155004: Working copy 'C:usersuser.blenderpylibwin64_vc14' locked.rnsvn: E155004: '...blenderpylibwin64_vc14' is already locked.rn"
    ----------------------------------------
ERROR: Command errored out with exit status 1: 'd:installationspython381python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'..Temppip-install-zbeppzcrbpysetup.py'"'"'; __file__='"'"'..Temppip-install-zbeppzcrbpysetup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record '..Temppip-record-_6dkr3k7install-record.txt' --single-version-externally-managed --compile Check the logs for full command output.

Error: command ‘cmake’ failed: No such file or directory

Further issue below can be solved very quick. Install cmake and run the installation again. Add cmake to the PATH variable and reboot your system after the installation.

    ERROR: Command errored out with exit status 1:
     command: 'd:installationspython381python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'..LocalTemppip-install-eznnpcg5bpysetup.py'"'"'; __file__='"'"'..LocalTemppip-install-eznnpcg5bpysetup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:UsersthomasAppDataLocalTemppip-record-bgm_imjcinstall-record.txt' --single-version-externally-managed --compile
         cwd: ..LocalTemppip-install-eznnpcg5bpy
    Complete output (10 lines):
    running install
    running build
    running build_ext
    Preparing the build environment
    Checking out svn libs from https://svn.blender.org/svnroot/bf-blender/trunk/lib/win64_vc14
    Cloning Blender source from git://git.blender.org/blender.git
    Updating Blender git submodules
    Configuring cmake project
    cmake -H...blenderpyblender -Bbuildtemp.win-amd64-3.8Release -DWITH_PLAYER=OFF -DWITH_PYTHON_INSTALL=OFF -DWITH_PYTHON_MODULE=ON -G "Visual Studio 14 2015 Win64"
    error: command 'cmake' failed: No such file or directory
    ----------------------------------------
ERROR: Command errored out with exit status 1: 'd:installationspython381python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'..Temppip-install-eznnpcg5bpysetup.py'"'"'; __file__='"'"'..Temppip-install-eznnpcg5bpysetup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record '..Temppip-record-bgm_imjcinstall-record.txt' --single-version-externally-managed --compile Check the logs for full command output.

Error: No CMAKE_C_COMPILER could be found (1)

This error means that you do not have installed Visual Studio or configured with cmake properly. To change this, I recommend to download Visual Studio 2019 Community, like it is explained in the Blender build information.

    ERROR: Command errored out with exit status 1:
     command: 'd:installationspython381python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'..Temppip-install-ix7xl1mhbpysetup.py'"'"'; __file__='"'"'..Temppip-install-ix7xl1mhbpysetup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:UsersthomasAppDataLocalTemppip-record-ht97md1kinstall-record.txt' --single-version-externally-managed --compile
         cwd: C:UsersthomasAppDataLocalTemppip-install-ix7xl1mhbpy
    Complete output (26 lines):
    running install
    running build
    running build_ext
    Preparing the build environment
    Checking out svn libs from https://svn.blender.org/svnroot/bf-blender/trunk/lib/win64_vc14
    Cloning Blender source from git://git.blender.org/blender.git
    Updating Blender git submodules
    Configuring cmake project
    D:installationscmakebincmake.exe -H...blenderpyblender -Bbuildtemp.win-amd64-3.8Release -DWITH_PLAYER=OFF -DWITH_PYTHON_INSTALL=OFF -DWITH_PYTHON_MODULE=ON -G "Visual Studio 14 2015 Win64"
    -- Selecting Windows SDK version  to target Windows 10.0.17763.
    -- The C compiler identification is unknown
    -- The CXX compiler identification is unknown
    CMake Error at CMakeLists.txt:110 (project):
      No CMAKE_C_COMPILER could be found.
    CMake Error at CMakeLists.txt:110 (project):
      No CMAKE_CXX_COMPILER could be found.
    -- Configuring incomplete, errors occurred!
    See also "../Temp/pip-install-ix7xl1mh/bpy/build/temp.win-amd64-3.8/Release/CMakeFiles/CMakeOutput.log".
    See also "../Temp/pip-install-ix7xl1mh/bpy/build/temp.win-amd64-3.8/Release/CMakeFiles/CMakeError.log".
    error: command 'D:installationscmakebincmake.exe' failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: 'd:installationspython381python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'..LocalTemppip-install-ix7xl1mhbpysetup.py'"'"'; __file__='"'"'..Temppip-install-ix7xl1mhbpysetup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:UsersthomasAppDataLocalTemppip-record-ht97md1kinstall-record.txt' --single-version-externally-managed --compile Check the logs for full command output.

Error: No CMAKE_C_COMPILER could be found (2)

Even if Visual Studio has been installed previously the issue could still occur. Unfortunately it is not always clear how to interpret such error messages. The major difficulty is the immediate clean up of the temporary log files. Meaning analyzing the files is impossible.

Nevertheless a solution may be to call the ‘make.bat’ file in the ‘C:\users\user\.blenderpy\blender’ folder. The script asks to install missing libraries which are important for the ‘bpy’ build and installation process.

Summary

In the end I only saw issues showing up. It took a lot of time to work on a possible solution. Unfortunately I was not successfully.