[Blender] How to make a Blender plugin

What is a Blender plugin?

Blender exposes various APIs to extend its functionality. By using the API, you can get the data that Blender uses internally, such as "currently selected face" and "position of object vertices". There are various information that can be obtained from the API, and the public API can be confirmed from the following, for example. http://www.blender.org/documentation/blender_python_api_2_67_release/contents.html

What you need to do to create a plugin

The API is Python-based and not intended for users, so you need to know the following: At first glance, the threshold seems to be quite high, but in reality, if you have made Blender and made something, I think that it will be manageable. On the contrary, it may be easy for those who are accustomed to 3D related programming.

Sample plugin

A simple sample plugin for Blender.

skeleton.py


#Required to access data structures inside Blender
import bpy

#Information about plugins
bl_info = {
    "name" : "Hoge Plugin",             #Plugin name
    "author" : "Piyo",                  #author
    "version" : (0,1),                  #Plugin version
    "blender" : (2, 6, 5),              #The version of Blender where the plugin works
    "location" : "UV Mapping > Hoge",   #Positioning of plugins inside Blender
    "description" : "Hoge Fuga Piyo",   #Plugin description
    "warning" : "",
    "wiki_url" : "",                    #URL of the wiki page where the plugin description is located
    "tracker_url" : "",                 #Blender Developer Org thread URL
    "category" : "UV"                   #Plugin category name
}

#menu
class CHoge(bpy.types.Operator):
    
    bl_idname = "uv.hoge"               #ID name
    bl_label = "Hoge Menu"              #The character string displayed in the menu
    bl_description = "Hoge Piyo"        #Description displayed in the menu
    bl_options = {'REGISTER', 'UNDO'}

    #The process that the plug-in actually performs the process
    def execute(self, context):
        return {'FINISHED'}             #Returns FINISHED on success

#Function to register a menu
def menu_func(self, context):
    self.layout.operator("uv.hoge")     #"Bl" of the class you want to register_Specify "idname"

#What happens when you install the plugin
def register():
    bpy.utils.register_module(__name__)
    bpy.types.VIEW3D_MT_uv_map.append(menu_func)

#What happens when you uninstall the plugin
def unregister():
    bpy.utils.unregister_module(__name__)
    bpy.types.VIEW3D_MT_uv_map.remove(menu_func)

#Main function
if __name__ == "__main__":
    register()

Explanation of sample plug-in

bpy module import

In order to access Blender's internal data, you need to import a module called bpy.

skeleton_1.py


#Required to access data structures inside Blender
import bpy

Information about plugins (bl_info)

Describes information about the plug-in. Since the comments in the source code are explained as they are, nothing in particular is explained here. If you want to publish your plugin to the blender wiki, it's a good idea to include the URL of the wiki page or the URL of the Blender Deverloper Org. However, if you use it individually, there is no problem if you pay attention only to the location and category. For reference, the Wiki page of the plugin I created and the URL of the Blender Developer Org are shown below. [Wiki] http://wiki.blender.org/index.php/Extensions:2.6/Py/Scripts/UV/Copy_Paste_UVs [Blender Developer Org] https://developer.blender.org/T38460

skeleton_2.py


#Information about plugins
bl_info = {
    "name" : "Hoge Plugin",             #Plugin name
    "author" : "Piyo",                  #author
    "version" : (0,1),                  #Plugin version
    "blender" : (2, 6, 5),              #The version of Blender where the plugin works
    "location" : "UV Mapping > Hoge",   #Positioning of plugins inside Blender
    "description" : "Hoge Fuga Piyo",   #Plugin description
    "warning" : "",
    "wiki_url" : "",                    #URL of the wiki page where the plugin description is located
    "tracker_url" : "",                 #Blender Developer Org thread URL
    "category" : "UV"                   #Plugin category name
}

Class for menu

Create a single menu item in a class that actually describes the processing of the plugin. Must inherit from the bpy.types.Operator class. It is necessary to describe the process to be executed in the execute method and return "FINISHED" as the return value if it succeeds. Another thing that can be specified as a return value is "CANCEL LED" that terminates the operation when an error occurs. See below for details. http://www.blender.org/documentation/blender_python_api_2_67_1/bpy.types.Operator.html

skeleton_3.py


#menu
class CHoge(bpy.types.Operator):
    
    bl_idname = "uv.hoge"               #ID name
    bl_label = "Hoge Menu"              #The character string displayed in the menu
    bl_description = "Hoge Piyo"        #Description displayed in the menu
    bl_options = {'REGISTER', 'UNDO'}

    #The process that the plug-in actually performs the process
    def execute(self, context):
        return {'FINISHED'}             #Returns FINISHED on success

Processing when installing / uninstalling a plug-in

Describe the process executed when the plug-in is installed in the "register" function, and the process executed when the plug-in is uninstalled in the "unregister" function. Register the plugin with bpy.utils.register_module and bpy.utils.unregister_module. bpy.types.VIEW3D_MT_uv_map.append and bpy.types.VIEW3D_MT_uv_map.remove add / remove items to the "UV Map" menu. Create a function (menu_func here) that describes the process of passing the "bl_idname" defined in the class you want to register to the argument of self.layout.operator, and pass it to the argument of bpy.types.VIEW3D_MT_uv_map.append. The item specified by bl_label is added to the "UV Map" menu. If you pass it to the argument of bpy.types.VIEW3D_MT_uv_map.remove, the added item will be deleted.

skeleton_4.py


#Function to register a menu
def menu_func(self, context):
    self.layout.operator("uv.hoge")     #"Bl" of the class you want to register_Specify "idname"

#What happens when you install the plugin
def register():
    bpy.utils.register_module(__name__)
    bpy.types.VIEW3D_MT_uv_map.append(menu_func)

#What happens when you uninstall the plugin
def unregister():
    bpy.utils.unregister_module(__name__)
    bpy.types.VIEW3D_MT_uv_map.remove(menu_func)

main function

In the main function, all you have to do is call the register function.

skeleton_5.py


#Main function
if __name__ == "__main__":
    register()

Run sample plugin

Let's actually operate the above sample plugin. The procedure for installing the plug-in is as follows. The installation procedure is also described on the Blender Wiki page. http://wiki.blender.org/index.php/Doc:JA/2.6/Manual/Extensions/Python/Add-Ons

  1. Select File-User Preferences
  2. Select the Addons tab
  3. Press the "Install from File ..." button
  4. Select the plugin you want to install
  5. Select the check box next to the installed plugin
  6. Press "U" in "Edit Mode"
  7. Confirm that "Hoge Menu" is displayed.
  8. Click "Hoge Menu" to execute the process (in this case, nothing is done because nothing is done)

Execution result

If the installation is completed successfully, you can check each screen as follows.

「User Preferences」-「Addons」 20140905224305.jpg

"U" menu of "Edit Mode"

20140905224318.jpg

Qiita article on Blender plugin development

In another Qiita article, I introduced the key points when developing the Blender plugin. Please refer to it as well.

Article for those who want to know more about Blender API

Articles for those who want to publish Blender plugins

Articles for those who want to improve the development environment of Blender plugins

Blender plugin development tutorial

We are creating a tutorial for beginners for those who want to develop Blender plugins. If you are interested, please refer to this as well.

https://www.gitbook.com/book/nutti/introduction-to-add-on-development-in-blender/details

References

Recommended Posts

[Blender] How to make a Blender plugin
How to make a Japanese-English translation
How to make a slack bot
How to make a crawler --Advanced
How to make a recursive function
How to make a deadman's switch
[Blender] How to make Blender scripts multilingual
How to make a crawler --Basic
[Python] How to make a class iterable
How to make a Backtrader custom indicator
How to make a Pelican site map
Spigot (Paper) Introduction to how to make a plug-in for 2020 # 01 (Environment construction)
How to make a dialogue system dedicated to beginners
How to make Spigot plugin (for Java beginners)
How to make a dictionary with a hierarchical structure.
[Blender x Python] How to make an animation
How to make Substance Painter Python plugin (Introduction)
[Blender x Python] How to make vertex animation
How to study Bukkit Plugin
How to hack a terminal
How to make a shooting game with toio (Part 1)
Basics of PyTorch (2) -How to make a neural network-
How to make a Cisco Webex Teams BOT with Flask
How to write a Python class
[Python] How to make a list of character strings character by character
How to put a symbolic link
How to make a multiplayer online action game on Slack
How to make a hacking lab-Kali Linux (2020.1) VirtualBox 64-bit Part 2-
How to create a Conda package
How to create a virtual bridge
How to make a hacking lab-Kali Linux (2020.1) VirtualBox 64-bit edition-
How to make a Python package (written for an intern)
How to make a simple Flappy Bird game with pygame
How to create a Dockerfile (basic)
How to delete a Docker container
Make a curtain generator in Blender
How to create a config file
How to make a string into an array or an array into a string in Python
How to make a surveillance camera (Security Camera) with Opencv and Python
[C language] How to create, avoid, and make a zombie process
How to make a .dylib library from a .a library on OSX (El Capitan)
Slack --APIGateway --Lambda (Python) --How to make a RedShift interactive app
How to make a unit test Part.1 Design pattern for introduction
[Python] How to make a matrix of repeating patterns (repmat / tile)
How to make Word Cloud characters monochromatic
[Blender] How to set shape_key with script
How to make Selenium as light as possible
How to create a clone from Github
How to split and save a DataFrame
How to build a sphinx translation environment
How to create a git clone folder
Qiita (1) How to write a code name
How to add a package with PyCharm
How to draw a graph using Matplotlib
[Python] How to convert a 2D list to a 1D list
Try to make a kernel of Jupyter
[Python] How to invert a character string
[Ubuntu] How to execute a shell script
How to get a stacktrace in python
How to make multi-boot USB (Windows 10 compatible)
How to create a repository from media