[Blender] Complement Blender's Python API with a text editor

This is the article on the 23rd day of Blender Advent Calendar 2019.

When creating Blender scripts and add-ons, you will often edit the source code using a text editor. One of the merits of using a text editor is the efficiency of development by the code completion function, but since part of the Python API provided by Blender is provided as binary data instead of Python source code, it is provided as it is. Unable to complete code. To solve this problem, a pseudo module "fake-bpy-module" that describes only the interface part of the Python API provided by Blender. Developed.

By using "fake-bpy-module", you can complete code for Blender's Python API with basically any editor that has code completion function. In this article, I will show you how to use "fake-bpy-module" to complete code on Visual Studio Code and PyCharm.

Prerequisites

The version of Blender covered in this article is ** 2.80 **. Please refer to README on GitHub for the version supported by "fake-bpy-module". "Fake-bpy-module" works in any environment of Windows / Mac / Linux, but the completion function can be used only in Python ** Python 3.6 ** or higher with Type Hint function introduced. Please note.

In the future, we plan to provide modules for Blender 2.81 and later versions.

Provision form of "fake-bpy-module"

"Fake-bpy-module" is provided by the following three methods.

  1. PyPI package
  2. Modules available on GitHub
  3. Self-made module

In this article, I will explain the specific code completion procedure for each.

Method 1: PyPI package

1. Install the package using the pip command

By using the pip command, you can install the "fake-bpy-module" registered on PyPI. Execute the following command to install "fake-bpy-module".

 $ pip install fake-bpy-module-<version>

Where <version> is the Blender version. To install the "fake-bpy-module" for Blender 2.8, run the following command:

 $ pip install fake-bpy-module-2.80

If you can use pip, it is easy and reliable to install "fake-bpy-module" using pip, but if you cannot use pip, please try another method.

2. Check code completion in the editor

If you installed the package using the pip command, you don't need to set up each editor to complete the code. Here are some examples of code completion for Visual Studio Code and PyCharm respectively.

Visual Studio Code

vscode_completion.png

PyCharm

pycharm_completion.png

Method 2: Modules published on GitHub

1. Download the module from GitHub

"Fake-bpy-module" is published on GitHub. The file that summarizes the module set is published as fake_bpy_modules_ <Blender version>-<date when the module was created> .zip, so download the necessary modules according to the environment. Here, Module for Blender 2.80 created on October 8, 2019 is fake_bpy_modules_2.80-20191008. Download zip. After the download is complete, unzip the file.

2. Tell the editor the path of the module

Tell the editor the path of the module so you can complete the code. The method of telling the module path depends on the editor.

Visual Studio Code

To complete the code in Visual Studio Code, follow these steps:

  1. Download the Python Visual Studio Code Extention (https://marketplace.visualstudio.com/items?itemName=ms-python.python)
  2. Click * [File] *> * [Preferences] *> * [Settings] *
  3. When settings.json opens, set the module path to python.autoComplete.extraPaths
{
    "python.autoComplete.extraPaths": [
        "<path-to-generated-modules>"
    ]
}

For <path-to-generated-modules>, specify the absolute path of the module.

PyCharm

To complete your code with PyCharm, follow these steps:

  1. Click * [File] *> * [Settings] * for Windows * [Settings] * window, or * [Pycharm Menu] *> * [Preferences] * for macOS * Opens the Preferences * window
  2. Select * [Project:] *> * [Project Interpreter] *
  3. Click the gear icon to the right of * [Project Interpreter:] *
  4. Click * [Show All ...] * from the pop-up menu that appears.
  5. In the * [Project Interpreters] * window, click the * [Show paths for the selected Interpreter] * icon to display the * [Interpreter Paths] * window.
  6. Click the * [+] * icon to launch the file browser
  7. Specify the directory where the module is located and click * [OK] *
  8. Click * [OK] * multiple times until the * [Settings] * window on Windows or the * [Preferences] * window on macOS closes.

3. Check code completion in the editor

You will be able to complete the code in each editor as if you installed the module using Method 1.

Method 3: Make your own module

1. Download the Blender binary

Download the target Blender binary from the Official Blender Download Site. The Blender 2.80 binaries are available at https://download.blender.org/release/Blender2.80/.

2. Clone the Blender source code

Execute the following command to download the Blender source code.

$ git clone git://git.blender.org/blender.git

3. Clone the fake-bpy-module project on GitHub

Execute the following command to clone the fake-bpy-module project published on GitHub.

$ git clone https://github.com/nutti/fake-bpy-module.git

4. Generate "fake-bpy-module"

Execute the following command to generate "fake-bpy-module".

$ cd fake-bpy-module/src
$ sh gen_module.sh <source-dir> <blender-dir> <branch/tag/commit> <output-dir> <mod-version>

If you have executed the command continuously from 2, execute the command as follows.

$ cd fake-bpy-module/src
$ sh gen_module.sh ../../blender <The directory where the Blender binaries downloaded in 1 are located> v2.80 out 2.80

5. Tell the editor the path of the module

Tell the editor the path of the module according to Method 2.

6. Check code completion in the editor

As with method 1 and method 2, you will be able to complete the code in each editor.

Summary

I showed you how to use "fake-bpy-module" to complete Blender's Python API on Visual Studio Code and PyCharm. By using code completion, you can improve the efficiency of Blender script and add-on development, so please take advantage of it. Although not introduced in this article, even if you do not use the PyPI package, you can find a way to complete code in all editors, not just Visual Studio Code and PyCharm, on the GitHub project page Documents. //github.com/nutti/fake-bpy-module/blob/master/docs/setup_all_text_editor.md) is open to the public. Please refer to this as well.

In addition, since "fake-bpy-module" is released as OSS, Bug Report and [Pull Request](https: //) Contributions such as github.com/nutti/fake-bpy-module/pulls) are welcome! Let's have a fun Blender add-on development life!

Recommended Posts

[Blender] Complement Blender's Python API with a text editor
[Python] Make a game with Pyxel-Use an editor-
Text extraction with GCP Cloud Vision API (Python3.6)
Create a python3 build environment with Sublime Text3
Run Blender with python
Operate Blender with Python
Playing with a user-local artificial intelligence API in Python
Register a ticket with redmine API using python requests
Set up a Python development environment with Sublime Text 2
[Vagrant] Set up a simple API server with python
Text extraction (Read API) with Azure Computer Vision API (Python3.6)
Use Twitter API with Python
Web API with Python + Falcon
Make a fortune with Python
Play RocketChat with API / Python
I made a python text
Blender Python API in Houdini (Python 3)
Call the API with python3.
Use subsonic API with python3
Create a directory with python
A note about hitting the Facebook API with the Python SDK
Quickly take a query string with API Gateway-> Lambda (Python)
Complement python with emacs using company-jedi
[Python] What is a with statement?
Solve ABC163 A ~ C with Python
Operate a receipt printer with python
A python graphing manual with Matplotlib.
Create Awaitable with Python / C API
Rubyist tried to make a simple API with Python + bottle + MySQL
GOTO in Python with Sublime Text 3
Get reviews with python googlemap api
A story about adding a REST API to a daemon made with Python
Run Rotrics DexArm with python API
Text extraction with AWS Textract (Python3.6)
Solve ABC166 A ~ D with Python
Run mruby with Python or Blender
Quine Post with Qiita API (Python)
Create a virtual environment with Python!
I made a fortune with Python.
Text mining with Python ① Morphological analysis
Enable Python raw_input with Sublime Text 3
I want to do a full text search with elasticsearch + python
Building a virtual environment with Python 3
Hit the Etherpad-lite API with Python
Solve ABC168 A ~ C with Python
[python] Read information with Redmine API
Make a recommender system with python
Use blender as a python module
[LINE Messaging API] Create a BOT that connects with someone with Python
[Python] Generate a password with Slackbot
Solve ABC162 A ~ C with Python
Solve ABC167 A ~ C with Python
Speak Japanese text with OpenJTalk + python
Solve ABC158 A ~ C with Python
Let's make a graph with python! !!
[Python] Inherit a class with class variables
Easy modeling with Blender and Python
I made a daemon with Python
How to send a request to the DMM (FANZA) API with python
Get started with Python in Blender
Write a batch script with Python3.5 ~