创建 Maya 用户界面

发布于 2024-09-14 04:23:29 字数 1972 浏览 7 评论 0原文

我正在创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

救赎№ 2024-09-21 04:23:29

您正在使用 cmd,我强烈推荐使用 pymel
导入 pymel.core 作为 pm
但这不是你的问题。

问题出在逻辑上。
您有名为 self.loadobject 的类成员变量 & self.basename。这 2 个代表 Maya UI 对象的名称。当您执行查询时,您将使用 Maya UI 对象的内容覆盖这些变量。

然后,当您再次尝试使用这两个对象时,Maya 会告诉您:

运行时错误:
“老兄,你在说谁!@#$?”

我向 Autodesk 的朋友们道歉,他们要么因为粗俗的语言而审查了第二行,要么只是省略了在 Maya 中包含有用的调试器。

import maya.cmds as cmds

#x=MyUI()
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.loadobjectTEXT = "Select Object and Load name"
        self.loadobjectFIELDNAME = cmds.textFieldButtonGrp(label="Load Object name",
                                                  buttonLabel="Load",
                                                  text=self.loadobjectTEXT,
                                                  buttonCommand=self.load)
        self.basenameTEXT = "Enter basename."                    
        self.basenameFIELDNAME = cmds.textFieldButtonGrp(label="Basename",
                                                buttonLabel="Rename",
                                                text=self.basenameTEXT,
                                                buttonCommand=self.replace_name)
        cmds.setParent(menu=True)

        cmds.showWindow(self.window)


    def load(self):
        select_objects = cmds.ls(selection=True)
        cmds.textFieldButtonGrp(self.loadobjectFIELDNAME, e=True, text=select_objects[0])


    def replace_name(self, *args):
        self.loadobjectTEXT = cmds.textFieldButtonGrp(self.loadobjectFIELDNAME,
                                               q=True, text=True)
        self.basenameTEXT = cmds.textFieldButtonGrp(self.basenameFIELDNAME,
                                             q=True, text=True)
        name = cmds.rename(self.loadobjectTEXT, self.basenameTEXT)
        ctlname = "%s_ctl" % name
        self.new_name = cmds.rename(name, ctlname)

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:

RuntimeError:
"Dude who the !@#$ are you talking about?"

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.

import maya.cmds as cmds

#x=MyUI()
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.loadobjectTEXT = "Select Object and Load name"
        self.loadobjectFIELDNAME = cmds.textFieldButtonGrp(label="Load Object name",
                                                  buttonLabel="Load",
                                                  text=self.loadobjectTEXT,
                                                  buttonCommand=self.load)
        self.basenameTEXT = "Enter basename."                    
        self.basenameFIELDNAME = cmds.textFieldButtonGrp(label="Basename",
                                                buttonLabel="Rename",
                                                text=self.basenameTEXT,
                                                buttonCommand=self.replace_name)
        cmds.setParent(menu=True)

        cmds.showWindow(self.window)


    def load(self):
        select_objects = cmds.ls(selection=True)
        cmds.textFieldButtonGrp(self.loadobjectFIELDNAME, e=True, text=select_objects[0])


    def replace_name(self, *args):
        self.loadobjectTEXT = cmds.textFieldButtonGrp(self.loadobjectFIELDNAME,
                                               q=True, text=True)
        self.basenameTEXT = cmds.textFieldButtonGrp(self.basenameFIELDNAME,
                                             q=True, text=True)
        name = cmds.rename(self.loadobjectTEXT, self.basenameTEXT)
        ctlname = "%s_ctl" % name
        self.new_name = cmds.rename(name, ctlname)
嗫嚅 2024-09-21 04:23:29

另外,我建议在这种情况下使用部分命令,

from functools import partial

[...]

cmds.textFieldButtonGrp(label="Basename",
                        buttonLabel="Rename",
                        text=self.basenameTEXT,
                        buttonCommand=partial(self.replace_name))
[...]
def load(self, *args):
[...]

这对您来说并没有太大改变。
但如果你想部分传递一些变量,让你的生活更轻松;)

例如:

[...]
cmds.textFieldButtonGrp(label="Basename",
                        buttonLabel="Rename",
                        text=self.basenameTEXT,
                        buttonCommand=partial(self.replace_name, obj))
[...]
def load(self, obj, *args):
[...]

plus, I sudgest to use partial for the command

from functools import partial

[...]

cmds.textFieldButtonGrp(label="Basename",
                        buttonLabel="Rename",
                        text=self.basenameTEXT,
                        buttonCommand=partial(self.replace_name))
[...]
def load(self, *args):
[...]

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 :

[...]
cmds.textFieldButtonGrp(label="Basename",
                        buttonLabel="Rename",
                        text=self.basenameTEXT,
                        buttonCommand=partial(self.replace_name, obj))
[...]
def load(self, obj, *args):
[...]
街角卖回忆 2024-09-21 04:23:29

如前所述,问题出在代码的第 33 行和第 35 行。您正在使用其内容的查询字符串值覆盖 textFieldButtonGrp 对象。因此,当您返回并第二次运行查询/编辑时,Maya 会抛出错误,因为这两个变量不再保存 textFieldButtonGrp 的实例,它们现在是一个简单的字符串。只需将这两行从: 更改

self.loadobject = ...
self.basename = ...

为本地化的:

loadobject = ...
basename = ...

或者,如果您需要在代码的其他部分将它们作为实例变量访问,则只需使用不同的变量名称,如 Mathieu 所解释的:

self.loadobjectText = ...
self.basenameText = ...

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:

self.loadobject = ...
self.basename = ...

to a localized:

loadobject = ...
basename = ...

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:

self.loadobjectText = ...
self.basenameText = ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文