2
$\begingroup$
import bpy


def main(context):
    
    bpy.ops.object.vertex_group_add()
    bpy.ops.object.vertex_group_assign()
    bpy.ops.mesh.select_mirror(axis={'X'})
    bpy.ops.object.vertex_group_select()
    bpy.ops.object.vertex_group_remove(all=False, all_unlocked=False)

class SimpleOperator(bpy.types.Operator):
    
    bl_idname = "object.simple_operator"
    bl_lable = "Simple Object Operator"
    
       
    def execute(self, cocntext):
        main(context)
        return {'FINISHED'}    




class VIEW3D_MT_custom_select_similar(bpy.types.Menu):
    bl_label = "Mirror Select"
    bl_idname = "VIEW3D_MT_custom_select_similar"

    def draw(self, context):
        layout = self.layout
        
        scene = context.scene
       
        layout.label(text="X-Mirror")
        layout.operator("object.simple_operator")
        




def register():
    bpy.utils.register_class(VIEW3D_MT_custom_select_similar)
    bpy.utils.register_class(SimpleOperator)



def unregister():
    bpy.utils.unregister_class(VIEW3D_MT_custom_select_similar)

if __name__ == "__main__":
    register()
```
$\endgroup$
0

1 Answer 1

1
$\begingroup$

Here is your example, modified to add the entry to the menu:

import bpy

def main(context):
    bpy.ops.object.vertex_group_add()
    bpy.ops.object.vertex_group_assign()
    bpy.ops.mesh.select_mirror(axis={'X'})
    bpy.ops.object.vertex_group_select()
    bpy.ops.object.vertex_group_remove(all=False, all_unlocked=False)

class SimpleOperator(bpy.types.Operator):
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"
    def execute(self, cocntext):
        main(context)
        return {'FINISHED'}    

def select_similar_menu_draw(self, context):
    layout = self.layout
    
    scene = context.scene
   
    layout.label(text="X-Mirror")
    layout.operator("object.simple_operator")
    

def register():
    bpy.utils.register_class(SimpleOperator)
    bpy.types.VIEW3D_MT_select_edit_mesh.append(select_similar_menu_draw)


def unregister():
    bpy.types.VIEW3D_MT_select_edit_mesh.remove(select_similar_menu_draw)
    bpy.utils.unregister_class(SimpleOperator)

if __name__ == "__main__":
    register()

I fixed a typo in SimpleOperator and added register/unregister entries for it; but there are two main changes with respect to the menu entry:

  1. You only define a draw routine for a menu entry; you don't place it in a class. That meant changing the class to just a def and removing it from the register/unregister functions.

  2. You call append on the right type to add the entry to the menu and remove to remove it. I added that code to your register/unregister functions.

$\endgroup$

You must log in to answer this question.

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