As already said by pink vertex and Jerryno, a poll()
classmethod should be added to the operator you add to the custom menu. The disabling (graying out) will be managed by Blender automatically:
import bpy
class SimpleOperator(bpy.types.Operator):
bl_label = "Simple Operator"
bl_idname = "wm.simple_operator"
@classmethod
def poll(cls, context):
return len(context.selected_objects) % 2 == 0
def execute(self, context):
self.report({'INFO'}, "Simple Operator executed.")
return {'FINISHED'}
class SimpleOperator2(bpy.types.Operator):
bl_label = "Simple Operator 2"
bl_idname = "wm.simple_operator2"
@classmethod
def poll(cls, context):
return (context.object is not None and
context.object.type == 'CAMERA')
def execute(self, context):
self.report({'INFO'}, "Simple Operator 2 executed.")
return {'FINISHED'}
class SimpleCustomMenu(bpy.types.Menu):
bl_label = "Simple Custom Menu"
bl_idname = "OBJECT_MT_simple_custom_menu"
def draw(self, context):
layout = self.layout
layout.label("Enabled, if even number of objects selected", icon='INFO')
layout.operator("wm.simple_operator")
layout.separator()
layout.label("Enabled, if active object is a camera", icon='INFO')
layout.operator("wm.simple_operator2")
def register():
bpy.utils.register_module(__name__)
def unregister():
bpy.utils.unregister_module(__name__)
if __name__ == "__main__":
register()
# The menu can also be called from scripts
bpy.ops.wm.call_menu(name=SimpleCustomMenu.bl_idname)