Vishus Blog

Blender Python Setup on Linux

Updated

Using Blender with Python introduces some very cool possibilities; unfortunately I found it very hard to get started. This is more of a reminder to self, but hopefully it's useful to others as well.

Table of Contents

🔗Installation script

Since manually downloading and extracting Blender doesn't install the blender.desktop file you won't have a shortcut for it in your menu. Nor will pip be setup for use with the python version Blender ships (it is independent of the Python installed on your system). Also it would be nice to store the location of Blender's custom Python executable in a variable, to make interacting with it easier.

Since this needs to be done every time you want to upgrade to a new version of Blender, I made a script.

🔗What it does

  • downloads and extracts Blender to a specified location
  • finds the path for Blender Python (no more digging around for it)
  • sets up and installs the blender.desktop file (so you have a nice little shortcut for it)
  • ensures pip is installed
  • appends BPY, BLENDER_PATH, and BLENDER_VERSION to your .bashrc file so you can easily find Blender

🔗Installing

At least on Linux it's not too hard to automate this setup.
  1. Download install_blender.sh

  2. Open a terminal to the directory where you downloaded the install_blender.sh script

  3. Make the script executable

    chmod +x install_blender.sh
    
  4. Specify the version to install and where to install it

    Find download link

    To find the url for the blender .tar.xz archive:

    1. Go to Blender.org > About > Website
    2. Choose a mirror from the section titled External Mirrors
    3. Choose the release folder
    4. Choose the folder with the latest version
    5. Copy the link for the file ending with -linux-x64.tar.xz
    export BLENDER_URL=https://mirror.clarkson.edu/blender/release/Blender3.3/blender-3.3.1-linux-x64.tar.xz
    export BLENDER_INSTALL=$HOME/bin/blender/
    
  5. Run the install_blender.sh script

    ./install_blender.sh
    

    I got this error, but it still works:

    Error encountered: pip's dependency resolver does not currently take into account all the packages that are installed.  This behavior is the source of the following dependency conflicts.
    Everything still works - Successfully installed pip

🔗Testing it out

Let's make sure it worked
  1. After adding the lines to your .bashrc file that were suggested by the install script, open a new terminal

  2. You can install any module but as an example I will use Pillow:

    "$BPY" -m pip install --upgrade pillow
    

    Note: got another error here but it still works

    Error encountered: 'pip's dependency resolver does not currently take into account all the packages that are installed.  This behavior is the source of the following dependency conflicts.'
    Ignore this message
  3. Open blender and click the Scripting workspace

    Blender's scripting layout is located at the top of the screen towards the center

  4. In Blender's Python console type:

    import PIL
    

    It should just import the module without any errors.

    Blender python console showing `import PIL` with no errors

🔗Importing local Python files

Frustratingly, Blender's python will not automatically resolve imports for local files. To get around this I append the .blend file's path to sys.path.

To automatically add the current file's path
  1. Download import_cur_path.py
  2. Copy it to your scripts/startup folder

Or run this to download the file directly to your scripts/startup/ folder:

curl -o "$BLENDER_PATH/$BLENDER_VERSION/scripts/startup/import_cur_path.py" "https://vishusandy.github.io/assets/blender_python/import_cur_path.py"
Or manually

Paste the following into your python console:

import sys
import os
import bpy
dir = os.path.dirname(bpy.data.filepath)
if not dir in sys.path:
    sys.path.append(dir)

Now you can import python scripts from the folder where your .blend file is saved.

For example, if you had a python file called zebra.py, in the same directory as your .blend file, you would import it like:

import zebra