Set Up Blender Addon Development Environment in Windows 10

Preface

Skill Requirements:

  • Basic proficiency with computers

Tutorial was made with:

  • Blender 2.93
  • Visual Studio Code 1.57.1

Difficulty: Beginner

Introduction

First things first, we need to answer an important question that we should be asking ourselves every time we set out to do something. The question is — why? So, in the case of this tutorial — why would we bother wasting time and bogging down our computer by downloading, installing, and setting up a bunch of external software when Blender has a perfectly functional built-in text editor?

Blender’s built-in text editor

The short answer is — because that’s how the pros do it! And the long answer is — because that’s how the pros do it! (for the following reasons):

  • Blender’s text editor is almost as basic as text editors come with limited functionality which makes using it painful for anything but quick testing and creating simple one-file scripts.
  • External code editors and IDEs provide quality-of-life features that make development quicker, easier, and much less of a pain. Some of the more important features include: easy file management, syntax highlighting, autocomplete, reference look-up, navigation to definition, advanced search and replace functionality, debugging, version control system integration, and more. All of these features are essential for efficient workflow and we get them by using external code editors or IDEs.
What is an IDE?

An integrated development environment (IDE) is a software application that has many tools which help software engineers develop software. Generally, an IDE is packed full of features such as code editor, debugging, build automation, file management, refactoring tools, syntax highlighting, advanced code search and replace functionality, autocomplete, and other.

Downloading Required Software

To set-up Blender addon development environment, we’re going to need 3 pieces of software:

  • Visual Studio Code — one of the most popular code editors on the market which also happens to be free and open-source.
  • Blender — the star that this tutorial is built for.
  • Python — Python programming language interpreter with built-in Python packages.
Why not use PyCharm? It has better refactoring tools and is an overall better IDE than a text editor.

PyCharm has some convenient refactoring tools that as of the day of writing this tutorial, Visual Studio Code lacks. The main ones I miss a lot in Visual Studio Code are moving a function to another module or class and having all references update in all files, or updating import statements when moving Python modules or classes.

However, Visual Studio Code has a huge advantage over PyCharm when it comes to Blender addon development — a quick and effortless way to update script changes in the addon that is loaded in Blender. This is achieved via the Visual Studio Code addon Blender Development by Jacques Lucke. It even has an option to sync changes to Blender on Visual Studio Code file save which makes iterating code changes a breeze.

From my limited investigation that I did into replicating the same functionality from Visual Studio Code in PyCharm, I didn’t manage to find a quick and easy way to do that.

Visual Studio Code

Go to https://code.visualstudio.com to download Visual Studio Code.

Install it using the default settings.

Blender

Go to https://www.blender.org/download/ to download the latest Blender version. Install it using the default settings. For this tutorial, I’m using Blender 2.93, but it should work with any Blender version from 2.80 onwards.

Python

Blender comes with Python bundled-in, so technically, we installed Python when we installed Blender. However, that’s only half true because as far as Windows is concerned, Python doesn’t exist on our system, as it doesn’t know where to look for it. To tell Windows that we have Python installed, we need to add Python executable location to Windows PATH environment variable. When installing Python via its installer, PATH variable gets edited automatically.

What is PATH environment variable?

Environment variables are globally accessible dynamic values for running programs to use.

PATH is one of those environment variables. Its purpose is to define the directory where executable programs are installed so that other programs that need to access those executables, know where to find them.

Other examples of environment variables include:
TEMP: defines the location where programs can store their temporary files
windir: defines the directory where Windows is installed

You can edit, remove, or add new variables by pressing WIN , searching for Edit the system environment variables, clicking it, and clicking Environment Variables in the opened window.

Blender’s Python vs System Python

Before we proceed, let’s pause for a minute. As already mentioned earlier, there are two ways we can go from here. We can either:

  1. Use Python interpreter embedded in Blender, or
  2. Download a separate Python interpreter and install it globally.

Everything is simple with the first option — when selecting an interpreter in Visual Studio Code, we navigate to Blender’s Python bin folder, select python.exe and we’re good to go.

If we go with the second option, there are some caveats regarding Python version differences between Blender’s Python and global Python. Let me explain.

If you go to https://www.python.org/downloads/, you’ll see that by clicking the main download button you get a certain version of Python. At the moment of writing this tutorial, it is Python 3.9.5. Latest versions of Blender don’t always have the latest version of Python bundled-in. For example, Blender 2.93 ships with Python 3.9.2. Thus, for it to work you would want to download Python 3.9.2 from the website and install that instead of the latest one. To make Blender use the globally installed Python instead of the bundled one, we would need to delete the bundled Python first because Blender will, by default, always favor its own Python over the global one. It can be found at C:\Program Files\Blender Foundation\Blender 2.93\2.93\python. If we don’t delete it, Blender will simply use the bundled Python.

Why should the globally installed Python’s version match Blender’s Python version?

If you don’t delete Python bundled with Blender and the versions don’t match — it’s no big deal. You could install the latest Python version and develop for Blender without any problems if you didn’t use any of the Python features from the newer version. If you do use some of those features, Blender will throw errors and your script will not work. This is because your code editor or IDE would use the system Python and Blender — its own.

If you do delete the bundled Python and the versions don’t match, Blender will simply not run. It needs the same Python version it was compiled with. It means that the system Python version must match exactly the one that came bundled with Blender. Both major and minor versions, along with the processor architecture (32-bit or 64-bit) must match. For example, if the bundled Python version is 3.9.2 64-bit, you must download and install Python 3.9.2 64-bit version from https://www.python.org/downloads/ for Blender to work.

To keep it simple, and because I use Python for Blender development only and don’t need many versions of it on my system, I don’t need it installed globally. Thus, I’m going to pick option one for the rest of this tutorial.

Setting Up Visual Studio Code Extensions

We need to install two extensions:

  1. Open Visual Studio Code
  2. Go to View > Extensions
  3. In the search bar, enter python
  4. Select Python extension created by Microsoft and click Install
  5. Wait for the installation to finish
  6. In the search bar, enter blender
  7. Select Blender Development extension created by Jacques Lucke and click Install
  8. Wait for the installation to finish
Installing Python extension
Installing Blender Development extension

We’re done installing extensions. Close the Extension tab by clicking X and restart Visual Studio Code.

Installing Autocomplete

Before going further, let’s test our set up we have so far. In Visual Studio Code:

  1. Go to File > Open Folder.
  2. Choose a location where you would like your project folder to reside and create a new folder there.
  3. Name it Test, select it, and press Select Folder.
  4. Expand the Test section on the left side of the screen and click the New File icon. Name it test.py.
Create new file

Visual Studio Code may show you a message saying that Python is not installed and prompt you to download it. As we are going to be using Python that comes with with Blender for this tutorial, ignore it for now. We will deal with it at the end of the tutorial.

Python not installed prompt

For Visual Studio Code to recognize Python files, we need to show it where Python executable is located. To do that:

  1. Press F1 and type python interpreter.
  2. Select the Python: Select Interpreter option, then Enter Interpreter Path…, and finally Find….
  3. Go to C:\Program Files\Blender Foundation\Blender 2.93\2.93\python\bin and select python.exe.

Python interpreter should now load and you should see the loaded Python interpreter version in the bottom left corner of the screen :

It still says Select Python Interpreter in the corner.

Go to C:\Program Files\Blender Foundation\Blender 2.93\2.93\python\bin in your file browser and try to launch python.exe to make sure that it launches without any errors. There may be an error shown that VCRuntime140.dll is missing. In this case, you will need to download and install the Microsoft Visual C++ 2015 Redistributable Update 3.

If another DLL file is missing, you can find what package you need to install by searching the DLL on the web.

Python is now locked and loaded. Let’s try to execute a line of Python code to check a couple of things. In the opened test.py file, type print:

Python autocomplete

We can see a popup appear — that’s autocomplete in action. Now, finish typing the command

print("Hello World!")Code language: Python (python)

and hit the green triangle in the top right corner of the screen:

Clicking the green triangle executed our code and we can see the output in the terminal window

Observe the command execute in the Terminal. Everything works as it should. There is one more thing left to verify. On the next line, type

import bpyCode language: Python (python)

This command imports Blender’s main python library into the test module (test.py file). Every script that interfaces with Blender by using the bpy library will need to have this line. In the next line below, type

bpy.Code language: Python (python)

A popup should appear in the same way it did earlier, but it doesn’t:

Blender autocomplete not working

In order for it to appear, we need to install Blender Python API library. We do this by installing fake-bpy-module using Python’s PIP package manager that comes installed with Blender’s Python. Type the following command in the open Visual Studio Code terminal window:

& "C:\Program Files\Blender Foundation\Blender 2.93\2.93\python\bin\python.exe" --% -m pip install fake-bpy-module-2.92 --user

in the terminal and press Enter (substitute the 2.92 version number in the command with your Blender’s version. But first, check fake-bpy-module page if this version is available, or else you’ll get an error and the module will not install). Wait a minute for it to download and finish installing. Once it says in the terminal that the module was successfully installed, restart Visual Studio Code.

There isn’t a fake-bpy-module version for my Blender version. What should I do?

Sometimes, the author of fake-bpy-module won’t have the time to update it the moment a new version of Blender releases. Therefore, you may not always find the latest package there. In this case, you could install the last version available and it should still work fine. Blender’s Python API doesn’t change much lately between versions, so it should do the job until the author updates the module.

Alternatively, you could generate Blender as a module and install it yourself so yo don’t have to wait for an update.

Now, try typing bpy. again and give it a moment to load. You should see something like this:

Blender fake-bpy-module in-action

Blender autocomplete is working.

Setting Up Linting

You may have noticed a prompt in the bottom right corner of the screen complaining that Linter pylint is not installed.

A linter is a program that checks your code for syntax and style errors and highlights them. They are useful because not only do they save time by marking errors so you can spot them easier, they also hint at potentially bad coding practices that you should take a moment to address.

Replace the

bpy.Code language: Python (python)

code with

prit("Bye World!")Code language: JavaScript (javascript)
No linter active

Notice how we have misspelled the print() command and Visual Studio Code does not indicate this as an error. Let’s install a linter and see what changes.

There are several different linters available. We will install pylint for now. If you don’t see the “Linter is not installed” prompt, press F1, type linter, and select Python: Select Linter > pylint. Press Install. Wait until it finishes installing, save the file by pressing Ctrl + S. You should see a wiggly line under the erroneous command and if you hover your mouse cursor over it, a popup will appear with hints to what may be wrong.

Pylint linter active

By default, linting happens when you save the file. It doesn’t run live as you type due to performance reasons. If you wish to have live-linting, there is a workaround for this. We will have to enable autosave by going to File > Auto Save. With this option enabled, Visual Studio Code will automatically save changes made to a file with a delay of 1 second. Every time a save is made — linter runs.

There are several other linters available. The main difference between them is how strict they are in detecting “malpractices.” You can install them and switch between them the same way we installed pylint. Feel free to experiment and Google around about their differences if you please as it’s mostly personal preference which one you choose to use.

Updating Scripts in Blender

If you have Visual Studio Code running, close it and open it with Administrator rights. Administrator rights will be necessary for Visual Studio to install Python Debugging libraries. Press RMB on Visual Studio Code icon and press Run as administrator:

Run As Administrator command in Windows 10
Run as Administrator

Once you have opened Visual Studio Code with administrator rights, we’ll build a simple Blender addon for demonstration purposes. First, rename the test.py file to __init__.py (note: there are two underscores on each side of the word “init”) and then replace all the content with the following:

import bpy

bl_info = {
 "name": "Test Addon",
 "description": "Addon for testing",
 "author": "Tester",
 "blender": (2, 80, 0),
 "version": (1, 0, 0),
 "category": "Test",
 "location": "View3D > UI > Unity Batch FBX Export",
}
    
class TESTADDON_PT_TestPanel(bpy.types.Panel):
    bl_idname = "TESTADDON_PT_TestPanel"
    bl_label = "Test Addon"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "Test Addon"
    bl_context = "objectmode"    
    
    def draw(self, context):

        layout = self.layout

        row = layout.row()                
        row.label(text="How cool is this!")
               

def register():
    bpy.utils.register_class(TESTADDON_PT_TestPanel)


def unregister():
    bpy.utils.unregister_class(TESTADDON_PT_TestPanel)
    
if __name__ == "__main__":
    register()
Code language: Python (python)

This code will make a panel appear in the properties panel in Blender. To run this code, press F1, type blender start, choose Blender: Start > Choose a new Blender executable, go to C:\Program Files\Blender Foundation\Blender 2.93 and select blender.exe. Give Blender some time to boot as this is the time where Python debugging libraries are being downloaded from the internet and installed with administrator rights (this is needed only once). After some time, Blender will boot. If you press N in 3D Viewport, a properties panel will pop up and among the tabs there we can see our Test Addon and Dev tab:

Dev tab is an indication that Blender is connected to Visual Studio Code and all the changes are ready to be synced across. Test Addon is our addon. The last part remaining is to make some changes and witness them appear in Blender.

Open Visual Studio Code and after the 26th line add the following:

row = layout.row()
row.label(text="Mighty cool!")
Code language: Python (python)

Save the files, press F1, type reload addons in the search bar, select Blender: Reload Addons. Switch to Blender and you will see Test Addon updated with the new text.

Every time you make a change in Visual Studio Code, all you need to do to update the addon in Blender is to press F1 > Blender: Reload Addons.

Updating Scripts in Blender with CTRL + S

Visual Studio Code Blender Development addon has a neat feature to execute Blender: Reload Addons command when you save a file by hitting CTRL + S. To enable it, go to File > Preferences > Settings, type blender reload in the search bar and tick the Reload On Save option.

Dealing with the “Python is not installed” popup

Because we haven’t installed Python globally, every time you launch Visual Studio Code, a message will pop up in the lower right corner of the screen telling you that “Python is not installed.” If the message is too annoying for you, there are two ways to get rid of the message:

  • Add the Blender Python’s directory to the Visual Studio Code settings.
  • Add Blender Python’s directory to Windows PATH variable.

Adding Python to Visual Studio Code Settings

  1. Open Visual Studio Code settings and enter python path in the search bar
  2. Scroll down a bit until you see the Python Path setting
  3. In the Python Path setting field, enter the path to the Blender Python’s bin directory which is: C:\Program Files\Blender Foundation\Blender 2.93\2.93\python\bin
Python Path field in Visual Studio Code preferences

Adding Python to Windows PATH Variable

  1. Press WIN
  2. Search for Edit the system environment variables and click it
  3. Click Environment Variables in the opened window.

In the System variables section:

  1. Select Path system variable and click Edit
  2. Click New
  3. Enter C:\Program Files\Blender Foundation\Blender 2.93\2.93\python\bin
  4. Replace the Blender version number with the version of the Blender you’re using
  5. Click OK to close the opened windows

Now, every time you open Visual Studio code, it will know where to find Python and the warning message will not be shown.

Conclusion

We have everything set up to start working on our addon! Let me know what addon you wish to create by filling out the form bellow.

Did you like it? Please consider the following to help me make more great content for you:

Was this helpful? Have a question?

Please leave me feedback by filling out the form bellow. It means a lot to me: