Dynamically Generating Menus

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

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

Dynamically Generating Menus

Postby 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

Postby 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

Postby welford » Sun Nov 18, 2012 7:00 am

Thank you very much :)


Return to “Python”

Who is online

Users browsing this forum: No registered users and 0 guests