我需要Python编程类的帮助

2024-09-22 18:30:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个代码,我想直接从Maya脚本文件夹访问。。 当我试图从托盘访问“poly”或“srfc”时。上面写着“名字没有定义”。 我不熟悉python编程,但我想做一个这样的工具。请在这方面给予帮助。我甚至想在课堂上把这些都包起来。在这方面也请帮忙。。在

import maya.cmds as mc
def win():
    if mc.window("filters", exists = True):
        mc.deleteUI("filters")

    myWindow = mc.window("filters", t= "Tools", s = False, rtf= True, wh = (3, 500))
    mc.rowColumnLayout(nc=20)
    mc.button(l = "mesh", w= 30, h= 18, c = "poly()", bgc = (.3,.3,.3))
    mc.showWindow(myWindow)


def poly():
    currentPanel = mc.getPanel(wf = True)
    if mc.modelEditor(currentPanel, q=True, polymeshes = True) == 1:
        mc.modelEditor(currentPanel, e=True, polymeshes = False)
    elif mc.modelEditor(currentPanel, q=True, polymeshes = True) == 0:
        mc.modelEditor(currentPanel, e=True, polymeshes = True)

Tags: 代码脚本falsetrueifdefmcwindow
1条回答
网友
1楼 · 发布于 2024-09-22 18:30:57

您正在以字符串形式调用poly()函数。只有在poly与调用win()的代码在同一命名空间中定义时,这才有效。将这两个函数包装在一个类中可以让它们看到对方:

class Tool(object):

     def win(self):
         self.myWindow = mc.window("filters", t= "Tools", s = False, rtf= True, wh = (3, 500))
         mc.rowColumnLayout(nc=20)
         mc.button(l = "mesh", w= 30, h= 18, c = self.poly, bgc = (.3,.3,.3))
         mc.showWindow(self.myWindow)


     def poly(self, ignore):
         # ignore is needed because the button always passes an argument
         currentPanel = mc.getPanel(wf = True)
         if mc.modelEditor(currentPanel, q=True, polymeshes = True) == 1:
              mc.modelEditor(currentPanel, e=True, polymeshes = False)
         elif mc.modelEditor(currentPanel, q=True, polymeshes = True) == 0:
              mc.modelEditor(currentPanel, e=True, polymeshes = True)


t = Tool()
t.win()

您还应该直接引用函数对象poly,而不是字符串(注意这里缺少引号)

这是一个reference on the different ways to hook controls up cleanly

相关问题 更多 >