如何在Maya中使用Def Function字符串 [英] How do I use Def Function strings in maya

查看:144
本文介绍了如何在Maya中使用Def Function字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在从梅勒转移,并想知道是否有人可以指出我在这个正确的方向。
我不太清楚如何在特定的arg下运行函数。

  def testFUNCTION(field ):
if field =='option1':print'您选择了选项1'
else:print'您已经选择了选项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()

我不断收到:

 第1行:name'option1'未定义

任何建议都会很棒!
谢谢

解决方案

当您传递 $ b $时,您试图从字符串创建回调b

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

当字符串由Maya求值时,'option1'不被引用,所以Python认为它是一个变量名。



一般而言,您不希望为回调使用字符串格式,原因正是这个原因:在定义变量的位置时会遇到问题。



通常的解决方法是使用 functools 模块或一个lambda创建回调函数,这些回调函数具有创建时所需的所有信息。 :

  def testFUNCTION(field):
如果field =='option1':print'您已选择选项1 '
else:print'您已经选择了选项2'

window = mc.window(w = 150)
mc.columnLayo ut()
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)
pre>

有关如何轻松设置回调的更详细说明这里

PS:请注意columnLayout命令的添加。如果没有它,你的控制将会放在彼此之上


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

解决方案

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

这篇关于如何在Maya中使用Def Function字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆