Previous Thread  Next Thread

chat icon Dynamically Generating Menus

welford

Posted: Sun Sep 30, 2012 6:42 am
Joined: 27 Jan 2009
Posts: 8
I have a custom menu defined similarly to this:

Code:
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:
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,
Reply with quote


CoDEmanX

Posted: Mon Oct 01, 2012 3:00 pm
Joined: 05 Apr 2009
Posts: 695
yes, you can pass parameters. First, you need to add properties to the class:

Code:
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:
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:
   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...
Reply with quote


welford

Posted: Sun Nov 18, 2012 7:00 am
Joined: 27 Jan 2009
Posts: 8
Thank you very much Smile
Reply with quote


 
Jump to:  
Powered by phpBB © 2001, 2005 phpBB Group