2
$\begingroup$

Is it possible to add a new option to the "Select Similar" (Shift+G in Edit Mode) menu of Blender?

I have a custom face data layer in my meshes to which I'd like to select all the faces with the same value in it. Out of the blue, not knowing how to approach this, I tried registering the following operator, but it did not appear:

class KclSelectSimilar(bpy.types.Operator):
    bl_idname = "mesh.select_similar_kcl_flags"
    bl_label = "Collision Flags"

    def execute(self, context):
        print("Hello World")
        return {'FINISHED'}

I thought giving it the same prefix mesh.select_similar_ like the other "Select Similar" operators would be enough.

$\endgroup$
3
$\begingroup$

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/blender_python_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 CustomMenu(bpy.types.Menu):
    bl_label = "Custom Menu"
    bl_idname = "OBJECT_MT_custom_menu"

    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.menu(CustomMenu.bl_idname)

def register():
    bpy.utils.register_module(__name__)

    # lets add the menu
    bpy.types.VIEW3D_MT_edit_mesh_select_similar.append(add_menu_item)

def unregister():
    bpy.utils.unregister_module(__name__)

    # remove menu
    bpy.types.VIEW3D_MT_edit_mesh_select_similar.remove(add_menu_item)

if __name__ == "__main__":
    register()

enter image description here

$\endgroup$
  • $\begingroup$ This clears a lot of things up for me! I always wondered how people found out the bpy.types enum value to add to menues. $\endgroup$ – Ray Koopa Jun 29 '16 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.