Dynamically Generating Menus

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
welford
Posts: 8
Joined: Tue Jan 27, 2009 2:10 pm
Location: Japan

Dynamically Generating Menus

Post by welford » Sun Sep 30, 2012 7:42 am

I have a custom menu defined similarly to this:

Code: Select all

class W3DMenu(bpy.types.Menu):
	bl_label = "W3D"
	bl_idname = "W3D_MT_menu"

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

		# file export
		layout.operator(".w3d_test", text="Test")
.w3d_test exports my models/anim. I want to be able to generate the menu dynamically and have a .w3d_test for the model and each animation contained within the file. Is is possible for me to pass a parameter to .w3d_test, i mean something like this:

Code: Select all

def draw(self, context):
	layout = self.layout
	layout.operator(".w3d_test",  parameter="model", text="Test")
	for action_key in bpy.data.actions:
		layout.operator(".w3d_test",  parameter=action_key, text="Test")
Thanks for any help,

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Post by CoDEmanX » Mon Oct 01, 2012 4:00 pm

yes, you can pass parameters. First, you need to add properties to the class:

Code: Select all

class W3DMenu(bpy.types.Menu): 
   bl_label = "W3D" 
   bl_idname = "W3D_MT_menu" 

   your_parameter = bpy.props.StringProperty()
   and_another = bpy.props.IntProperty()

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

      # file export 
      layout.operator(".w3d_test", text="Test") 
then pass the arguments like:

Code: Select all

def draw(self, context): 
   layout = self.layout 
   layout.operator(".w3d_test", text="Test").your_parameter = "model"

   for action_key in bpy.data.actions: 
      layout.operator(".w3d_test", text="Test").your_parameter = action_key
in case you wanna pass multiple arguments, do it like this:

Code: Select all

   for i, action_key in enumerate(bpy.data.actions): 
      props = layout.operator(".w3d_test", text="Test")
      props.your_parameter = action_key
      props.and_another = i
I'm sitting, waiting, wishing, building Blender in superstition...

welford
Posts: 8
Joined: Tue Jan 27, 2009 2:10 pm
Location: Japan

Post by welford » Sun Nov 18, 2012 7:00 am

Thank you very much :)

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest