In the previous Post, I explained how to make a Blender plugin with a simple sample code. This time, I will explain how to create a sub (on the tree) menu. There may be some mistakes because there is little information to create a submenu and it is the result of trial and error.
From the previous Post, only the part that creates the menu is excerpted.
menu_1.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
#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)
Here is a simple sample code for creating a submenu. The structure of the displayed menu is "Hoge Menu"-"Hoge Sub Menu".
submenu_1.py
#Submenu
class SubMenu(bpy.types.Operator):
bl_idname = "uv.hoge_sub"
bl_label = "Sub Hoge Menu"
out_text = bpy.props.StringProperty()
def execute(self, context):
self.report({'INFO'}, self.out_text)
return {'FINISHED'}
#Main menu
class Menu(bpy.types.Menu):
bl_idname = "uv.hoge"
bl_label = "Hoge Menu"
bl_description = "Hoge Piyo"
def draw(self, context):
layout = self.layout
#Submenu registration + output character string registration
layout.operator(SubMenu.bl_idname).out_text = "Sub Hoge Menu"
#Register in the menu displayed when you press "U" in Edit mode (bpy).types.VIEW3D_MT_uv_map.append(menu_func)Called from)
def menu_func(self, context):
self.layout.separator() #Register separator in menu
self.layout.menu(Menu.bl_idname)
#Registration
def register():
bpy.utils.register_module(__name__)
bpy.types.VIEW3D_MT_uv_map.append(menu_func)
The submenu class is basically the same as the class used to create a normal menu, and inherits the bpy.types.Operator class. As a confirmation, let's add a variable called out_text to the submenu so that out_text will be output when the menu is executed.
submenu_2.py
#Submenu
class SubMenu(bpy.types.Operator):
bl_idname = "uv.hoge_sub"
bl_label = "Sub Hoge Menu"
out_text = bpy.props.StringProperty()
def execute(self, context):
self.report({'INFO'}, self.out_text)
return {'FINISHED'}
Since the main menu needs to create a menu on the tree, it inherits from the bpy.types.Menu class. Furthermore, the main menu does not need to think about what happens when it is selected, and the execute method is unnecessary. Instead, you need to add a draw method to display the menu. Inside the draw method, the submenu is registered by assigning the bl_idname of the subclass of bpy.types.Operator to the argument of layout.operator, and the character string is assigned to the member variable out_text of the registered submenu. I am.
submenu_3.py
#Main menu
class Menu(bpy.types.Menu):
bl_idname = "uv.hoge"
bl_label = "Hoge Menu"
bl_description = "Hoge Piyo"
def draw(self, context):
layout = self.layout
#Submenu registration + output character string registration
layout.operator(SubMenu.bl_idname).out_text = "Sub Hoge Menu"
The registration part is almost the same as the normal part, but the menu_func called from the function specified in the argument of bpy.types.VIEW3D_MT_uv_map.append has been modified. It has nothing to do with creating a submenu, but you can add a separate to the menu by executing the self.layout.separator function. This makes it clearly distinguishable from other menus and makes the menus added by installing the plugin easier to understand. You can register the menu on the tree by passing the bl_idname of the derived class of bpy.types.Menu as the argument of the self.layout.menu function.
submenu_4.py
#Register in the menu displayed when you press "U" in Edit mode (bpy).types.VIEW3D_MT_uv_map.append(menu_func)Called from)
def menu_func(self, context):
self.layout.separator() #Register separator in menu
self.layout.menu(Menu.bl_idname)
#Registration
def register():
bpy.utils.register_module(__name__)
bpy.types.VIEW3D_MT_uv_map.append(menu_func)
Here is the code of the sample plug-in that actually works, summarizing the above contents.
submenu.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
}
#Submenu
class SubMenu(bpy.types.Operator):
bl_idname = "uv.hoge_sub"
bl_label = "Sub Hoge Menu"
out_text = bpy.props.StringProperty()
def execute(self, context):
self.report({'INFO'}, self.out_text)
return {'FINISHED'}
#Main menu
class Menu(bpy.types.Menu):
bl_idname = "uv.hoge"
bl_label = "Hoge Menu"
bl_description = "Hoge Piyo"
def draw(self, context):
layout = self.layout
#Submenu registration + output character string registration
layout.operator(SubMenu.bl_idname).out_text = "Sub Hoge Menu"
#Register in the menu displayed when you press "U" in Edit mode (bpy).types.VIEW3D_MT_uv_map.append(menu_func)Called from)
def menu_func(self, context):
self.layout.separator() #Register separator in menu
self.layout.menu(Menu.bl_idname)
#Registration
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()
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
Recommended Posts