4
$\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$

1 Answer 1

6
$\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/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()

enter image description here

$\endgroup$
1
  • $\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
    Commented Jun 29, 2016 at 10:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .