Python exec bug?

I may have found a bug. Here’s some sample code:


#!BPY

from Blender import Draw

FOO=Draw.Create(.5)

def gui():
    global FOO
    s=get_text()
    exec s

def get_text():
    s="FOO=Draw.Number('Foo',1,10,10,150,25,FOO.val,.0,1.0,'funk')"
    return s

def event(evt,val):
    if not evt:return
    print FOO
    if evt==Draw.ESCKEY:
        Draw.Exit()

def bevent(evt):
    pass

Draw.Register(gui,event,bevent)

What happens when I run this is that the FOO variable cannot be changed. Well, it can be changed with the button, but then if I mouse around to another window, it returns to it’s default value. I was trying to write a function that creates a string to be passed back to the gui function to be run. It’s not working the way I thought it would.

It just occured to me that I may be able to change the values in the bevent function. Of course, that was never an issue without the exec.

Of course, that didn’t work. Any help?

I often use dict()'s instead of globals, this seems to work,


#!BPY

from Blender import Draw
FOO= {}
FOO['v1'] = Draw.Create(.5)



def gui():
    s=get_text()
    exec s

def get_text():
    s="FOO['v1']=Draw.Number('Foo',1,10,10,150,25,FOO['v1'].val,.0,1.0,'funk')"
    return s

def event(evt,val):
    if not evt: return
    if evt==Draw.ESCKEY:
        Draw.Exit()

def bevent(evt):
    print FOO['v1'].val
    pass

Draw.Register(gui,event,bevent)



That’s it. I ran the code and it works. Thank you.