I am creating a simple add-on in Blender. Initially I tested the code in Python console, then I wrapped it in an operator and added it to Object menu, it worked well:

class MyOperator(bpy.types.Operator):
bl_idname = 'object.MyOperator'
bl_label = 'MyOperator'
bl_options = {'REGISTER', 'UNDO'}

def execute(self, context):
# ...affairs...
return {'FINISHED'}

self.layout.operator(MyOperator.bl_idname)

def register():
bpy.utils.register_class(MyOperator)


But now I need to add several operators and a submenu. I tried this example, and but it is not working:

class MyMenu(bpy.types.Menu):

def draw(self, context):
layout = self.layout

# None of these is working :(

def register():
bpy.utils.register_class(MyOperator)


So, what's the problem about? How can I solve it?

There were a number of issues. From not registering menu class and having invalid bl_idname's (no capital letters)

Here is working code. In 2.8 it is a requirement that class names reflect the bl_idname.

import bpy

class OBJECT_OT_myoperator(bpy.types.Operator):
bl_idname = 'object.myoperator'
bl_label = 'MyOperator'
bl_options = {'REGISTER', 'UNDO'}

def execute(self, context):
# ...affairs...
return {'FINISHED'}

def draw(self, context):
layout = self.layout
layout.operator(OBJECT_OT_myoperator.bl_idname)

def register():
bpy.utils.register_class(OBJECT_OT_myoperator)