问题描述
我正忙于在装配工具中创建一个函数,该函数允许对关节进行镜像并替换前缀.关节(行为和方向)的镜像都起作用,但是当我search and replace
两个文本字段中的内容时出现错误.场景中关节的前缀是R_
或L_
,我想用这个替换它们.
I'm busy creating a function in my rigging tool which allows mirroring of joints and replacing the prefix. The mirroring of joints (both behaviours and orientation) is working but I get an error when I search and replace
what is in my two text fields. The prefix of joints in the scene is either R_
or L_
and I would like to replace them with this.
错误如下:NameError:名称searchFor
未定义.奇怪的是,我实际上创建了一个名为searchFor
的变量和一个名为replaceWith
的变量.请参见下面的代码:
The error is as follows: NameError: name searchFor
is not defined. The odd part here is that I actually create a variable called searchFor
and one called replaceWith
. See the code below:
import maya.cmds as cmds
child2 = cmds.gridLayout( cw = self.size[ 0 ] / 2 - 10, nc = 2 )
cmds.text( l = ' Mirror Joints', al = 'left', font = "boldLabelFont" )
cmds.separator( style = 'none' )
searchFor = cmds.textFieldGrp( tx = 'Search for...' )
replaceWith = cmds.textFieldGrp( tx = 'Replace with...' )
cmds.button( label = 'Mirror Orientation',
command = "cmds.mirrorJoint( cmds.ls( sl = True ),
mirrorYZ = True,
mirrorBehavior = False,
searchReplace = cmds.textFieldGrp( searchFor, q = True, tx = True),
cmds.textFieldGrp( replaceWith, q = True, tx = True )" )
cmds.button( label = 'Mirror Behaviour',
command = "cmds.mirrorJoint( cmds.ls ( sl = True ),
mirrorYZ = True,
mirrorBehavior = True )" )
该代码是UI的一部分,就像在其他模块中调用其他功能一样.是否可能需要在UI模块中创建一个包含mirrorJoint
命令的函数?还是这是可行的方法?
The code is part of the UI as where the other functions are called forth from another module. Can it be that I need to create a function which includes the mirrorJoint
command in the UI module? Or is this a viable approach?
为更好地查看代码,请执行以下操作:https://dl.dropboxusercontent.com/u/545575/python .zip
For a better view of the code:https://dl.dropboxusercontent.com/u/545575/python.zip
推荐答案
您正在使用字符串来调用该函数,并且该字符串将在全局范围内进行解释-就像您在侦听器中键入了该字符串一样;在您的函数主体之外,'searchFor'不存在.
You're using a string to call the function, and that string gets interpreted in the global scope - as if you'd typed it in the listener; outside the body of your function, 'searchFor' does not exist.
以下是有关Maya如何看待回调命令的参考: http: //techartsurvival.blogspot.com/2014/04/maya-callbacks-cheat-sheet.html
Here's a reference on how maya sees callback commands: http://techartsurvival.blogspot.com/2014/04/maya-callbacks-cheat-sheet.html
MHLester的示例可以满足您的要求,您必须提防闭包. Lambda将从定义它的作用域中继承变量-但是在作用域关闭时而不是在定义时;这可能会引起一些意外.例如,如果您尝试这样做:
MHLester's example will do what you want, you will have to watch out for closures. The lambda will inherit variables from the scope where it is defined -- but at the time the scope closes, not at the time it was defined; this might cause some surprises. For example if you tried this:
def test():
w = cmds.window()
cmds.columnLayout()
example = 1
cmds.button("test", c= lambda *_: sys.stdout.write( "example: %i" % example ) )
example = 4
cmds.showWindow(w)
单击该按钮将显示"4",即使它看起来应该是"1".
clicking the button will print '4', even though it looks like it should be '1'.
这篇关于文本字段查询以替换前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!