Using external IDE PyCharm for writing Blender scripts

Blender has an interpnal 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. Lets connect Blender with an external IDE PyCharm by the JetBrains company. This is 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.
      1. The best is to download and install the same version of the interpreter that Blender use.
        1. To learn Blender Python interpreter version:
          1. 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 version number.
              1. Blender 2.77 use Python version 3.5.1

            Learning Blender Python version number
            Learning Blender Python version number
        2. 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 startup 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 motion_script.py.
Creating new script file
Creating new script file

The script file is ready.

  1. To get properly Blender API autocomplete in PyCharm we need to export API description files from Blender and include them to the created project.
    1. To export Blender autocomplete discription files we can use the utility pypredef_gen.
        1. We need only one file pypredef_gen.py 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 pypredef_gen.py 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.
      3. It needs to link created 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
              1. Project: The name of the current project – Project Interpreter
                1. Click the gear icon in the right upper corner of the window – More
                  1. Click Show Paths For The Selected Interpreter
                      1. Click the green positive sign icon and add the directory with pypredef files (st. 4.1.5) to list.
        Adding Blender API autocomplit to all PyCharm projects
        Adding Blender API autocomplit to all PyCharm projects
        1. To connect files only to the current project:
          1. In the PyCharm main menu select: File – Settings
            1. Project: The name of the current project – Project Structure
              1. Click Add Content Root and add the pypredef directory (st. 4.1.5) to list.
Adding Blender API autocomplit only to current PyCahrm project
Adding Blender API autocomplit only to current PyCahrm project

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

Blender API autocomplit
Blender API autocomplit
  1. Open Blender
    1. Open internal Blender text editor
      1. Press New button to create a new file
        1. 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 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 prepared script.

Blender file with this text can be saved in a convenient place as template, and you can use it to write different scripts in 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
  • Njoku OKECHUKWU VAL

    Please make sure that you equally update the directory or file path : d:/pypredef_gen.py to match your exact location of pypredef_gen.py as found in item 4.3. This gave me problems before I figured it out. However thanks so much.

    • Yes, you need the proper way to pypredef_gen.py in your cmd-file if it differs from example. Thank you for remark!