创建 Maya 用户界面
我正在创建一个 Maya UI,但是当我在 Maya 中运行一次“replace_name”按钮后尝试运行“loadobject”按钮命令两次时,出现此错误:
cmds.textFieldButtonGrp(self.loadobject, e=True, text=select_objects[0])
RuntimeError:
希望有人可以帮助我解决此问题。 谢谢。
import maya.cmds as cmds
class MyUI:
def __init__(self):
self.title = "Test UI"
self.window = cmds.window(self.title, widthHeight=(1000, 600),
resizeToFitChildren=1)
cmds.rowLayout("button1, button2, button3", numberOfColumns=5)
cmds.columnLayout(adjustableColumn=True, columnAlign="center",
rowSpacing=10)
self.loadobject = cmds.textFieldButtonGrp(label="Load Object name",
buttonLabel="Load",
text="Select Object %s."
% "and Load name",
buttonCommand=self.load)
self.basename = cmds.textFieldButtonGrp(label="Basename",
buttonLabel="Rename",
text="Enter basename.",
buttonCommand=self.replace_name)
cmds.setParent(menu=True)
cmds.showWindow(self.window)
def load(self, *args):
select_objects = cmds.ls(selection=True)
cmds.textFieldButtonGrp(self.loadobject, e=True, text=select_objects[0])
def replace_name(self, *args):
self.loadobject = cmds.textFieldButtonGrp(self.loadobject,
q=True, text=True)
self.basename = cmds.textFieldButtonGrp(self.basename,
q=True, text=True)
name = cmds.rename(self.loadobject, self.basename)
ctlname = "%s_ctl" % name
self.new_name = cmds.rename(name, ctlname)
I am creating a maya UI but when I try to run my loadobject button command twice after I run my replace_name button once in maya I get this error:
cmds.textFieldButtonGrp(self.loadobject, e=True, text=select_objects[0])
RuntimeError:
Hope someone could help me fix this.
Thanks.
import maya.cmds as cmds
class MyUI:
def __init__(self):
self.title = "Test UI"
self.window = cmds.window(self.title, widthHeight=(1000, 600),
resizeToFitChildren=1)
cmds.rowLayout("button1, button2, button3", numberOfColumns=5)
cmds.columnLayout(adjustableColumn=True, columnAlign="center",
rowSpacing=10)
self.loadobject = cmds.textFieldButtonGrp(label="Load Object name",
buttonLabel="Load",
text="Select Object %s."
% "and Load name",
buttonCommand=self.load)
self.basename = cmds.textFieldButtonGrp(label="Basename",
buttonLabel="Rename",
text="Enter basename.",
buttonCommand=self.replace_name)
cmds.setParent(menu=True)
cmds.showWindow(self.window)
def load(self, *args):
select_objects = cmds.ls(selection=True)
cmds.textFieldButtonGrp(self.loadobject, e=True, text=select_objects[0])
def replace_name(self, *args):
self.loadobject = cmds.textFieldButtonGrp(self.loadobject,
q=True, text=True)
self.basename = cmds.textFieldButtonGrp(self.basename,
q=True, text=True)
name = cmds.rename(self.loadobject, self.basename)
ctlname = "%s_ctl" % name
self.new_name = cmds.rename(name, ctlname)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在使用 cmd,我强烈推荐使用 pymel
导入 pymel.core 作为 pm
但这不是你的问题。
问题出在逻辑上。
您有名为
self.loadobject
的类成员变量 &self.basename
。这 2 个代表 Maya UI 对象的名称。当您执行查询时,您将使用 Maya UI 对象的内容覆盖这些变量。然后,当您再次尝试使用这两个对象时,Maya 会告诉您:
我向 Autodesk 的朋友们道歉,他们要么因为粗俗的语言而审查了第二行,要么只是省略了在 Maya 中包含有用的调试器。
You are using cmds I highly recommend pymel instead
import pymel.core as pm
But that is not your issue.
The issue is in the logic.
You have class member vars called
self.loadobject
&self.basename
. Those 2 represent the name of the Maya UI objects. When you do a query, you overwrite those vars with the content of the Maya UI objects.Then when you try to use those 2 objects again Maya is telling you:
I apologize for my friends at Autodesk, they either censored the second line due to the coarse language or just omitted to include a useful debugger in Maya.
另外,我建议在这种情况下使用部分命令,
这对您来说并没有太大改变。
但如果你想部分传递一些变量,让你的生活更轻松;)
例如:
plus, I sudgest to use partial for the command
in this case doesn't really change too mutch to you.
but if you want to pass some varibles partial make your life easer ;)
eg :
如前所述,问题出在代码的第 33 行和第 35 行。您正在使用其内容的查询字符串值覆盖 textFieldButtonGrp 对象。因此,当您返回并第二次运行查询/编辑时,Maya 会抛出错误,因为这两个变量不再保存 textFieldButtonGrp 的实例,它们现在是一个简单的字符串。只需将这两行从: 更改
为本地化的:
或者,如果您需要在代码的其他部分将它们作为实例变量访问,则只需使用不同的变量名称,如 Mathieu 所解释的:
As previously stated, the issue is with lines 33 and 35 of your code. You are overwriting the textFieldButtonGrp objects with a queried string value of their content. So when you come back and run the query/edit for the second time is when Maya is throwing the error because these two variables no longer hold an instance of a textFieldButtonGrp, they are now a simple string. Just change these two lines from:
to a localized:
Or, if you need access to them as an instanced variable in other parts of your code, you simply need to use a different variable name as Mathieu explained: