Using external IDE PyCharm for writing Blender scripts

Blender has an internal text editor to write scripts on Python language. However, this editor is much inferior to specially designed for writing code IDEs. There is no satisfactory autocomplete, comfortable syntax lighting, possibility to organize projects in Blender internal text editor – all those things that determine the speed and ease of code writing. However, it is not difficult to connect and use an external IDE for writing Blender scripts.

External IDE for scripting in Blender
External IDE for scripting in Blender

At present, there are several IDEs (Integrated Development Environment) for Python language. Each of them has some advantages and disadvantages. Let’s connect Blender with an external IDE PyCharm by the JetBrains company. This is a quite powerful environment that has all basic functions which almost completely covers all the usual requirements for such programs. Also, it has free Community version.

  1. Download and install PyCharm on your computer
    1. Download the package from the official website: PyCharm Community Edition.
    2. Install PyCharm.
  2. PyCharm requires a separate Python interpreter. It must be downloaded and installed too.
    1. Download the package from the official Python website. The best is to download and install the same version of the interpreter that Blender use.
    2. To learn Blender Python interpreter version – In the main Blender menu choose:
      1. Help – Save System Info
      2. Specify place to save the file with current Blender system information.
      3. Open the saved file and in Python section find the version number.

        Learning Blender Python version number
        Learning Blender Python version number
      4. Blender 2.77 use Python version 3.5.1
      5. Install required version of Python interpreter.
  3. Now we can create Blender script file in PyCharm:
      1. Run PyCharm
      2. Create a new project
        1. Press Create New Project in start-up screen
      3. Specify the project location and choose the interpreter:
        1. In the “Location” field specify the project location. For example D:/Blender/motion_script
        2. In the “Interpreter” field specify the Python interpreter.
    Creating PyCharm project
    Creating PyCharm project
    1. After creating project right click the project name to open its menu. Select New – Python file to create a new script file. For example name it
Creating new script file
Creating new script file

The script file is ready.

  1. To get properly Blender API auto-complete in PyCharm we need to make API description files from Blender and include them in the created project.
    1. To make auto-complete files for Blender 2.77:
      1. To export Blender auto-complete description files we can use the utility pypredef_gen.
          1. We need only one file from it. This file is located in the python_api directory.
          2. Download this file and save it somewhere on your computer, for example in the root of drive D:/
          3. To execute it create a batch file ppg.cmd with the following content, replacing _BLENDER_PATH_ with the full path to Blender (eg it can be: % ProgramFiles%\blender_2.77, but be sure to write your own):

        1. Run ppg.cmd file.
        2. After ppg.cmd execution in the same place where the file is located (in our case in the root of drive D:/) will be created pypredef directory with files describing the Blender API structure. Save this directory in any convenient place.
      2. To make auto-complete files for Blender 2.79, 2.80 and later:
        1.  You can download already build auto-complete files from GitHub:
    2. Now it needs to link auto-complete files to our PyCharm project. Files can be included in all PyCharm projects (once, but this case they will be used, even in projects that are not related to Blender), or you can include them to each new separate project.
        1. For a one-time connection to all projects (current and future):
          1. In the PyCharm main menu select: File – Settings
          2. Project: The name of the current project – Project Interpreter
          3. Click the gear icon in the right upper corner of the window – More
          4. Click Show Paths For The Selected Interpreter
          5. Click the green positive sign icon and add the directory with auto-complete files to list.
      Adding Blender API autocomplit to all PyCharm projects
      Adding Blender API autocomplete to all PyCharm projects
      1. To connect files only to the current project:
        1. In the PyCharm main menu select: File – Settings
        2. Project: The name of the current project – Project Structure
        3. Click Add Content Root and add the directory with auto-complete files to list.
Adding Blender API autocomplit only to current PyCahrm project
Adding Blender API autocomplete only to current PyCahrm project

After performing these steps, Blender API autocomplete will work in the PyCharm project.

Blender API autocomplit
Blender API autocomplete
  1. Open Blender
    1. Open internal Blender text editor
    2. Press the “New” button to create a new file
    3. Write the following text, replacing _PATH_ with the PyCharm script path (st. 3.3.1.) (in our example D:/Blender/motion_script) and _FILE_NAME_ with the script file name (st. 3.4) (in our example motion_script).

Everything is ready to write scripts. You can switch to the PyCharm, write a necessary script in the created file, using all the advantages of this IDE. Then return to the Blender and just click the Run Script button to execute the prepared script.

Blender file with this text can be saved in a convenient place as a template, and you can use it to write different scripts in the future.

Bonus script example:

For example, type the following code to the script file created in PyCharm:

This script creates a cube, adds a facet to it, reproduce it to 20×20 cubes and randomly raises each of them by Z axis in the range from 0 to 1. The result is a stylish tech background with cubes. Just assign material, set lighting, and start rendering.

Script running result
Script running result

Author: Nikita

Blender add-ons developer and articles writer.