Miniu
(Miniu)
March 13, 2016, 9:21am
1
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.
const
(const)
March 15, 2016, 1:45am
2
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")
Miniu
(Miniu)
March 15, 2016, 2:20am
3
EDIT: Didn’t read example correctly.
Thank you. Will try.
EDIT2: That’s even better then what i wanted really
Knew about invoke_popup but for some reason haven’t thought about passing self into it to use draw method, heh.
const
(const)
March 15, 2016, 2:35am
4
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.
Miniu
(Miniu)
March 15, 2016, 2:40am
5
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
const
(const)
March 17, 2016, 10:02am
6
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
Miniu
(Miniu)
March 17, 2016, 10:11am
7
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
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