The way to call the function of the plural

28 views
Skip to first unread message

kurashima

unread,
Jul 1, 2015, 4:43:48 AM7/1/15
to python_in...@googlegroups.com
It's a question about pymel.
If a button is pressed, please tell me the way to call saves and load.
↓It was written like mel, but the following error goes out.
# Error: NameError: file <maya console> line 1: name 'saves' is not defined # 

def dual():
    def saves():
        print 'hello.'

    def load():
        print 'bye.'

    with pm.window( width=150 ):
        pm.columnLayout( adjustableColumn=True )
        pm.button(label='Button 1', command='saves();load()')
        
dual()

Justin Israel

unread,
Jul 1, 2015, 6:25:23 AM7/1/15
to python_in...@googlegroups.com
This is an issue concerning scope. If you set a command using a string, then it is going to expect these symbols to be available in the global scope. But you are declaring inner functions that disappear after the dual() scope ends. There is no closure happening here. 

My initial question is, do you need to use an inner function as your callback? Also, do you need to be using the string form of defining a command?

Ideally you would always pass a callable object as your command so that you don't have to deal with scope problems when the string is evaluated later on.

If you must use inner functions for your solution, then you should create a proper closure, and then pass a callable:
def dual():

    def saves():
        print "hello"

    def load():
        print "bye"

    def callback(*args):
        saves()
        load()

    with pm.window(width=150):
        pm.columnLayout(adj=True)
        pm.button(label="Button 1", command=callback)
Justin


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/80ef3762-0d7b-4852-9118-061eebadb2be%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

kurashima

unread,
Jul 1, 2015, 7:44:51 AM7/1/15
to python_in...@googlegroups.com
Mr.Justin Israel 

Thank you very much for your perfect answer!!


2015年7月1日水曜日 19時25分23秒 UTC+9 Justin Israel:
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages