Menu button values

My english isn’t that good but I will try to explain what I need:

I have 2 menus, I have created them in this way:


import Blender
from Blender import *

# Globals
glob_menu1 = Draw.Create(0)
glob_menu2 = Draw.Create(0)

mnu1Content = "Menu1%t|Option1|Option2"
mnu2Content = ""

menuDict = {"Option1":["Option1_1","Option1_2"],"Option2":["Option2_1","Option2_2","Option2_3"]}

def fillMenu2(val):
    
    global mnu1Content, mnu2Content, menuDict
    
    option = mnu1Content.split('|')[val]
    mnu2Content = "Menu2%t"
    for item in menuDict[option]:
        print item
        mnu2Content = mnu2Content+"|"+item
    Draw.Redraw()

def draw():
  
    global glob_menu1, glob_menu2, mnu1Content, mnu2Content
    
    BGL.glClearColor(0.600000,0.600000,0.600000,1)
    BGL.glClear(BGL.GL_COLOR_BUFFER_BIT)
 
    glob_menu1 = Draw.Menu(mnu1Content,1,10,10,100,20,glob_menu1.val,'Menu1')
    glob_menu2 = Draw.Menu(mnu2Content,2,120,10,100,20,glob_menu2.val,'Menu2')
    
    Draw.PushButton('Quit',3,230,10,50,20,'Exit script')

def event(evt,val): #ESC key press
    
    if evt == Draw.ESCKEY and not val:
        Draw.Exit()
    
def bevent(evt):    
    
    global glob_menu1
    
    if evt == 1:
        fillMenu2(glob_menu1.val)
    
    elif evt == 2:
        pass
    
    elif evt == 3:
        Draw.Exit()

Draw.Register(draw,event,bevent)

How can I control default menu selection? When script starts, both menus are blank (there’s no text in menu box) until I change to some option in Menu1. I could set Draw.Create(1) for both menus, but that will show Option1 only for Menu1, Menu2 will be still empty until I chage option for Menu1. And If I change Menu1 to Option2, then change Menu2 to Option2_3, and then again change Menu1 to Option1, Menu2 value will still be 3 and it will show nothing.
I don’t know a different way to create this so if anyone understands what I ask please help.
Thanks!

With the code you pasted, Menu2 will always start blank because the mnu2Content variable is not defined until you click on the first menu. At that point bevent will receive the appropriate event to run fillMenu2 and mnuContent will be defined. Menu2 will become blank in the case you mentioned because when you select Option1 from Menu1, Menu2 will be redefined to only have two entries, so having a value of 3 will cause it to display a blank. I hope that helps.

So what are you trying to do with this script?

Well, that’s exactly what I want to correct. If anyone knows better way for drawing those menus I would like to know :slight_smile:

That’s only a part of my script, the rest of my code isn’t ready for publish… jet :slight_smile:

You could do something like the following (changed lines in bold)


import Blender
from Blender import *

# Globals
<b>glob_menu1 = Draw.Create(1)</b>
<b>glob_menu2 = Draw.Create(1)</b>

mnu1Content = "Menu1%t|Option1|Option2"
mnu2Content = ""

menuDict = { "Option1":["Option1_1","Option1_2"], \
             "Option2":["Option2_1","Option2_2","Option2_3"] }

def fillMenu2(val):

  global mnu1Content, mnu2Content, menuDict

  option = mnu1Content.split('|')[val]
  mnu2Content = "Menu2%t"
  print option
  print menuDict[option]
  for item in menuDict[option]:
    print item
    mnu2Content = mnu2Content+"|"+item
    Draw.Redraw()

def draw():

  global glob_menu1, glob_menu2, mnu1Content, mnu2Content

  BGL.glClearColor(0.600000,0.600000,0.600000,1)
  BGL.glClear(BGL.GL_COLOR_BUFFER_BIT)

  glob_menu1 = Draw.Menu(mnu1Content,1,10,10,100,20,glob_menu1.val,'Menu1')
  glob_menu2 = Draw.Menu(mnu2Content,2,120,10,100,20,glob_menu2.val,'Menu2')

  Draw.PushButton('Quit',3,230,10,50,20,'Exit script')

def event(evt,val): #ESC key press

  if evt == Draw.ESCKEY and not val:
    Draw.Exit()

def bevent(evt):

  global glob_menu1, glob_menu2

  if evt == 1:
    fillMenu2(glob_menu1.val)
    <b>glob_menu2.val = 1</b>
  elif evt == 2:
    pass
  elif evt == 3:
    Draw.Exit()

<b># Initialize mnu2Content
fillMenu2( glob_menu1.val )</b>

Draw.Register(draw,event,bevent)

The first two changes will set the menus so they do not start off blank. The fillMenu2 line just above the Register command will initialize mnu2Content so the second menu is not empty. The glob_menu2.val = 1 line in the bevent function will reset the default value of the second menu whenever its contents are changed. Will that work for you?

That’s exactly what I need! :slight_smile: And I don’t get it, I have tried that “glob_menu2.val = 1” in bevent function but it didn’t worked for me… Well, I didn’t tried that in this code but in my script that I’m creating, but that should work there also, I will check… Thanks man…

Edit:
I have tried and I’ve realised why that “glob_menu2.val=1” didn’t worked for me :slight_smile: Stupid mistake, I have changed glob_menu2.val from a function but I’ve forget to include “global glob_menu2” in that func :slight_smile:

Excellent, I’m glad that worked out for you.