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
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.
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()
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
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
}
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
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)
In the main function, all you have to do is call the register function.
skeleton_5.py
#Main function
if __name__ == "__main__":
register()
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
If the installation is completed successfully, you can check each screen as follows.
「User Preferences」-「Addons」
In another Qiita article, I introduced the key points when developing the Blender plugin. Please refer to it as well.
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
Recommended Posts