The blender layout code can be viewed by right clicking on the select menu and selecting view source. This will open space_view3d.py
in the text editor. The select similar menu can be found around line 707
class VIEW3D_MT_edit_mesh_select_similar(Menu):
bl_label = "Select Similar"
def draw(self, context):
layout = self.layout
layout.operator_enum("mesh.select_similar", "type")
You can append or prepend to a menu via (see https://www.blender.org/api/current/bpy.types.Menu.html#extending-menus)
import bpy
def add_menu_item(self, context):
self.layout.label("Added this line")
self.layout.operator("mesh.select_similar_kcl_flags")
bpy.types.VIEW3D_MT_edit_mesh_select_similar.append(add_menu_item)
For a custom submenu, create a menu class and call layout.menu
in your draw function (see Templates > Python > UI Menu template for reference):
import bpy
bl_info = {
"name": "My Add-on",
"description": "",
"category": "3D View"
}
class VIEW3D_MT_custom_select_similar(bpy.types.Menu):
bl_label = "Custom Menu"
bl_idname = "VIEW3D_MT_custom_select_similar"
def draw(self, context):
layout = self.layout
layout.operator("wm.open_mainfile")
layout.operator("wm.save_as_mainfile").copy = True
layout.operator("object.shade_smooth")
layout.label(text="Hello world!", icon='WORLD_DATA')
# use an operator enum property to populate a sub-menu
layout.operator_menu_enum("object.select_by_type",
property="type",
text="Select All by Type...",
)
# call another menu
layout.operator("wm.call_menu", text="Unwrap").name = "VIEW3D_MT_uv_map"
def add_menu_item(self, context):
layout = self.layout
layout.separator()
layout.menu(VIEW3D_MT_custom_select_similar.bl_idname)
def register():
bpy.utils.register_class(VIEW3D_MT_custom_select_similar)
# add the menu
bpy.types.VIEW3D_MT_edit_mesh_select_similar.append(add_menu_item)
def unregister():
bpy.utils.unregister_class(VIEW3D_MT_custom_select_similar)
# remove menu
bpy.types.VIEW3D_MT_edit_mesh_select_similar.remove(add_menu_item)
if __name__ == "__main__":
register()