Maya Python UI-选中时显示对象类父类

Maya Python UI-选中时显示对象类父类,python,class,user-interface,parent,maya,Python,Class,User Interface,Parent,Maya,您好,我需要一些帮助我是高级python编码的新手,我一直在尝试解决这个问题,但我找不到答案。我试图找到一种方法,当有人在Maya中单击一个对象(例如基本球体)时,它会将对象类和父类打印到每个文本字段中;然后,如果用户选择其他对象(如多维数据集),它将显示该对象类&父类。我知道我需要一个函数,当一个对象被选中时,该函数将被激活,然后将值放入文本字段中,但我不知道该怎么做。如果有人有解决方案,我们将不胜感激:) 主要的绊脚石是您以一种非常不寻常的方式使用类设置。通常的模式是使用\uuuu init

您好,我需要一些帮助我是高级python编码的新手,我一直在尝试解决这个问题,但我找不到答案。我试图找到一种方法,当有人在Maya中单击一个对象(例如基本球体)时,它会将对象类和父类打印到每个文本字段中;然后,如果用户选择其他对象(如多维数据集),它将显示该对象类&父类。我知道我需要一个函数,当一个对象被选中时,该函数将被激活,然后将值放入文本字段中,但我不知道该怎么做。如果有人有解决方案,我们将不胜感激:)


主要的绊脚石是您以一种非常不寻常的方式使用类设置。通常的模式是使用
\uuuu init\uuuu
方法创建类的新副本(“实例”);在您的例子中,您是在定义类时执行代码,而不是在调用它时执行代码。通常的类shell如下所示:

class Something (object) # derive from object in python 2.7, ie, in maya
    def __init__(self):
       self.variable = 1 # set persistent variables for the object
       do_something()  # other setup runs now, when a new Something is created
    def method(self):
       print self.variable # you can use any variables defined in 'self' 
在maya中创建反应式UI的常用方法是使用。当某些事件在Maya内部发生时,这些将触发脚本回调。最简单的事件是
SelectionChanged
,当所选内容更改时触发该事件

您要做的另一件事是找出如何将其打包到类中。有时候,创建类对象,然后给它一个创建实际UI的
show()
layout()
方法是一个好主意,这在很大程度上取决于在显示窗口之前需要设置对象多少

在本例中,我将该行为放入
\uuuu init\uuu
中,以便在创建对象时创建UI。
\uuuu init\uuuu
还创建了scriptJob,并将其作为窗口的父对象(因此,当窗口执行此操作时,它将消失)

通过在scriptJob中使用不带参数的
self.update\u UI
,我们确保激发的函数知道它正在更新哪个对象。对textfields使用self变量可以让我们更新随窗口一起出现的UI,而不用担心记住其他范围中的名称


谢谢你的帮助,但现在我遇到了一个问题。我尝试将它与我的其他代码合并,没有得到任何错误MSG,但是窗口没有出现,为什么会出现这种情况?
class Something (object) # derive from object in python 2.7, ie, in maya
    def __init__(self):
       self.variable = 1 # set persistent variables for the object
       do_something()  # other setup runs now, when a new Something is created
    def method(self):
       print self.variable # you can use any variables defined in 'self' 
class drawUI(object):

    def __init__(self):

        if cmds.window("UI_MainWindow", exists = True):
            cmds.deleteUI("UI_MainWindow")
            #create actual window
        self.window = cmds.window("UI_MainWindow", title = "User Interface Test", w = 500, h = 700, mnb = False, mxb = False, sizeable = False)
        cmds.columnLayout("UI_MainLayout", adjustableColumn=True)
        cmds.text(label="Object's Class:")
        self.ObjClass = cmds.textField(text = cmds.objectType,editable = False)
        cmds.text(label="Object's Parent Class:")
        self.ObjParClass = cmds.textField(editable = False)
        cmds.showWindow(self.window)
        cmds.scriptJob (e = ("SelectionChanged", self.update_UI), p= self.window)

    def update_UI(self, *_, **__):
        sel = cmds.ls(selection=True) or []
        c = ["-none-", "-none-"]
        if sel:
            c = cmds.nodeType(sel[0], i = True)
        cmds.textField(self.ObjClass, e=True, text = c[-1])
        cmds.textField(self.ObjParClass, e=True, text = ", ".join(c[:-1]))

test = drawUI()