How do I use Def Function strings in maya

早过忘川 提交于 2019-12-23 21:57:58

问题


I've been transposing from mel and was wondering if anyone could point me in the right direction with this. I'm not too sure how to run a Function with a specific arg in mind.

def testFUNCTION(field):
    if field == 'option1': print 'You have selected option 1'
    else: print 'You have selected option 2'

mc.window( w=150 )
mc.textFieldButtonGrp (l='option 1', bl='Set', ad3=2, bc=('testFUNCTION, option1'))
mc.textFieldButtonGrp (l='option 2', bl='Set', ad3=2, bc=('testFUNCTION, option2'))
mc.showWindow()

I keep getting:

line 1: name 'option1' is not defined

Any advise would be great! Thanks


回答1:


You are trying to create the callback from a string when you pass

mc.textFieldButtonGrp (l='option 1', bl='Set', ad3=2, bc=('testFUNCTION, option1')

When the string is evaluated by Maya, 'option1' is not quoted so Python thinks its a variable name.

In general you don't want to use the string format for callbacks for precisely this reason: there will be problems figuring out where variables are defined.

The usual workarounds are to use the functools module or a lambda to create callbacks which have all the information they need when they are created. For example:

def testFUNCTION(field):
    if field == 'option1': print 'You have selected option 1'
    else: print 'You have selected option 2'

window = mc.window( w=150 )
mc.columnLayout()
mc.textFieldButtonGrp (l='option 1', bl='Set', ad3=2, bc=(lambda : testFunction('option1'))
mc.textFieldButtonGrp (l='option 2', bl='Set', ad3=2, bc=(lambda : testFunction('option2'))
mc.showWindow(window)

There's a more detailed explanation of how to set up callbacks easily here.

PS: Note the addition of the columnLayout command. Without it your controls will lay out on top of eachother




回答2:


To pass arguments in maya UI with functools :

command = partial (defName, arg01, arg02) # any arguments you need

from functools import partial

def testFUNCTION(field, *args):
    if field == 'option1': print 'You have selected option 1'
    else: print 'You have selected option 2'

mc.window( w=150 )
mc.textFieldButtonGrp (l='option 1', bl='Set', ad3=2, bc=partial(testFUNCTION, 'option1'))
mc.textFieldButtonGrp (l='option 2', bl='Set', ad3=2, bc=partial(testFUNCTION, 'option2'))
mc.showWindow()



回答3:


At a glance, I believe your problem here is that your bc (buttonCommand) flag needs to be a function. For one liner functions you can use a lambda.

mc.textFieldButtonGrp (l='option 1', bl='Set', ad3=2, bc=lambda x='option1': testFUNCTION(x))

However, in general I would suggest re-evaluating all together and move away from maya cmds and use pymel.

Perhaps something like this would suite your needs.

results = pm.confirmDialog(title='Title', message='Here is an example', button=['option1', 'option2'])
testFUNCTION(result)

If you need a more robust GUI, consider experimenting with PySide.



来源:https://stackoverflow.com/questions/26021598/how-do-i-use-def-function-strings-in-maya

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!