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.
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.
"Fake-bpy-module" is provided by the following three methods.
In this article, I will explain the specific code completion procedure for each.
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.
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
PyCharm
"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.
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:
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:
You will be able to complete the code in each editor as if you installed the module using Method 1.
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/.
Execute the following command to download the Blender source code.
$ git clone git://git.blender.org/blender.git
Execute the following command to clone the fake-bpy-module project published on GitHub.
$ git clone https://github.com/nutti/fake-bpy-module.git
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>
<source-dir>
: Root directory of Blender source code<blender-dir>
: Directory where Blender binaries are located<branch / tag / commit>
: Blender source code branch corresponding to the module to be generated<output-dir>
: Module generation directory<mod_version>
: For the specified version, modify the API with the patch placed in the mods
directory.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
Tell the editor the path of the module according to Method 2.
As with method 1 and method 2, you will be able to complete the code in each editor.
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