如何在Maya中使用Def Function字符串

编程入门 行业动态 更新时间:2024-10-24 18:28:27
本文介绍了如何在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字符串

本文发布于:2023-10-27 02:56:10,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   如何在   Def   Maya   Function

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!