How to pass parameters to Menu

Let’s say i have a menu like:

class SomeMenu(bpy.types.Menu):
    bl_idname = 'some.menu'
    
    some_list = []


    def draw(self, context):
        for item in some_list:
            self.layout.label(item)

And i’m calling it from some other operator:

bpy.ops.wm.call_menu(name=SomeMenu.bl_idname)

How can I pass list to SomeMenu.some_list from that operator?

Right now i’m just using global variable but there has to be a better way.


import bpy


###########################################################


message = ""


class MessageWithGlobal(bpy.types.Menu):
    bl_idname = "message.global"
    bl_label = ""


    def draw(self, context):
        global message
        self.layout.label(message)


bpy.utils.register_class(MessageWithGlobal)


message = "Draw Message With Global"
bpy.ops.wm.call_menu(name=MessageWithGlobal.bl_idname)


###########################################################


class MessageWithOperator(bpy.types.Operator):
    bl_idname = "message.operator"
    bl_label = ""
    message = bpy.props.StringProperty()
    
    def invoke(self, context, event):
        return context.window_manager.invoke_popup(self)
    
    def execute(self, context):
        return {'FINISHED'}
    
    def draw(self, context):
        self.layout.label(self.message)


bpy.utils.register_class(MessageWithOperator)


bpy.ops.message.operator('INVOKE_DEFAULT', message = "Message With Operator")

EDIT: Didn’t read example correctly.

Thank you. Will try.

EDIT2: That’s even better then what i wanted really :slight_smile:
Knew about invoke_popup but for some reason haven’t thought about passing self into it to use draw method, heh.

The second option has a few problems though.

You can’t use it while you are in the invoke/execute method of another operator. However you can use it from any panel and the console.

Will keep that in mind, but in a way i’m using it right now it works better. I really just needed a popup in one of my operators not a separate menu :slight_smile:

Something new, I found this snippet somewhere on Github. To my large surprise this function is undocumented in the API and no one mentioned it so far.


def draw(self, context):
    self.layout.label("Hello World")


context.window_manager.popup_menu(draw, title="Greeting", icon='INFO')

For GoogleBot: bpy draw menu without class

That’s the only way I knew how to call a popup before, I haven’t thought of the class way that you told me about :slight_smile:
And it’s documented in here https://www.blender.org/api/blender_python_api_2_77_0/bpy.types.WindowManager.html?highlight=popup_menu#bpy.types.WindowManager.popup_menu