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 motion_script.py.
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 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 the 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.
      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: https://github.com/Korchy/blender_autocomplete
    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
5 3 votes
Article Rating
Subscribe
Notify of
guest

24 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Jack
Jack
4 months ago

This is a great article. Thank you!

GitXuLdi
GitXuLdi
1 year ago

Hi there, thanks for the great tutorial! I followed it and it works fine when you want to Run a single script with its internal functions. However, I have an issue concerning my modules. I wrote a bunch of script in PyCharm and then a main app script that calls functions from my different modules. It seemingly works on PyCharm however in Blender, using the Run script I get an error telling me my module does not exist. I managed to create an Addon containing my script and successfully imported it into Blender but it still does not work.. Do you have any suggestion regarding this problem? Thank you !

schlussleben12
schlussleben12
3 years ago

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):

What’s the meaning of this part? What is the _BLENDER_PATH_ exactly? Where can I find the path anyway? Should I change blender’s path or just the .zip file’s path? I extracted my .zip file but have no ppg.cmd in there. It’s really lack of explanation.

Last edited 3 years ago by schlussleben12
SMS
SMS
3 years ago

Thanks for the blender_autocomplete repo. FYI, the given bonus script would not run correctly under Blender 2.93. The following script produces the intended result. The main change is the syntax of the modifier_apply call.

BlackStartx
3 years ago

Hi ^-^

For anyone interested I’ve released a plugin for PyCharm Professional/Community that helps the development of Blender Addons using the PyCharm IDE.

For anyone interested here is the GitHub Repository Link:
https://github.com/BlackStartx/PyCharm-Blender-Plugin

Hope it will help someone ^-^

P.S: If you are the author of this article and think this comment is inappropriate I will remove it instantly~

Filip
Filip
6 years ago

Hi, thanks for tutorial. Do you have newer API package (2.79)? I have problem to generated and PyCharm somehow ignoring od api.. :/
Thank you.

Nikita
6 years ago
Reply to  Filip
Robert Dowling
Robert Dowling
7 years ago

Hi, thank you for this!
I am having some issues when running the bat file. I’m getting and Attribute error:
AttributError: ‘NoneType’ object has no attribute ‘keymaps’…

This is with a clean install of Blender 2.79 and Python 3.5.3

Nikita
7 years ago
Reply to  Robert Dowling

Hello!
I think there ara some issues with pypredef generator and the newes Blender version of API.
I found this ready API headers in internet, try to use them: https://www.dropbox.com/s/en2nznvc71o6h5t/API.zip?dl=0

Robert Dowling
Robert Dowling
7 years ago
Reply to  Korchiy

That worked, thank you~!

Matt Down
Matt Down
7 years ago

Thanks for guide. Works great except for bmesh.ops and bmesh.types, any idea what the fix might be for this? Thanks in advance!

Nikita
7 years ago
Reply to  Matt Down

May be some issues in pypredef_gen? Its author updated it last time more than a year ago.
Try to use this bmesh.types https://www.dropbox.com/s/9o91lwzu4ckwawf/bmesh.types.py?dl=0
I do not have bmesh.ops too.

Matt Down
Matt Down
7 years ago
Reply to  Korchiy

Thanks for your quick response. I will take a look in pypredef_gen and see if I can get it working.

Nikita
7 years ago
Reply to  Matt Down

If you will be successful in solving this problem, please write here your solution.

Jérémy Legigan
Jérémy Legigan
7 years ago

Hello. I’ve got a problem with the bpy module. Pycharm recognizes all other modules except the bpy module. Have you got any idea where the problem can come from ?

Nikita
7 years ago

There can be many reasons. May be some error while pypredef exporting? Try this variant of bpy file https://www.dropbox.com/s/4x57a7b4ayfwqaq/bpy.py?dl=0

Colin Basnett
Colin Basnett
7 years ago

The formatting on this guide is really whack, makes it look a lot more complicated than it is. Perhaps consider removing all the nested lists.

Nikita
7 years ago
Reply to  Colin Basnett

Thank you for your comment! I corrected the most terrible lists, making them a little less indentation.

Njoku OKECHUKWU VAL
Njoku OKECHUKWU VAL
7 years ago

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.

Nikita
7 years ago

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