1

所以我正在尝试制作一个 GUI,允许用户将任何指定的 polyShapes(用复选框选择)构建到他们需要的任何属性(在滑块中设置)。此外,当每个复选框被选中时,它会取消选择其他框并将用户不需要的某些属性变灰。

我正在努力使用定义每个复选框被选中时会发生什么的功能。选择复选框后,按下属性和创建按钮,该过程正常工作(我希望结果),但我会收到以下错误,除了最后一个复选框外,所有其他复选框:

RuntimeError: Object 'window1|formLayout46|checkBox3' not found. #

我被告知这是一个错误,因为函数运行在旧/以前版本的窗口中,并且将滑块等作为参数传递给函数将修复错误。但是,这并不能解决问题,当然我不能在创建复选框之前将它们作为参数传递,如下面的代码所示。它也没有解释为什么最后一个复选框(checkBoxCylinder)工作得很好——我假设这工作得很好,因为在它之前一切都已经完成了。

任何关于此事的建议/解释都将不胜感激,因为我现在觉得我在绕圈子。为下面的长代码道歉,我不能使用摘录,因为它们都链接在一起!!

import pymel.core as pm

class MakeLightDome(object):

def __init__(self):
    sel = []

def checkSelection(self):
    sel = pm.ls(selection = True)
    length = len(sel)

#Check Selection    
    if length == 0:
        print "Please create a PolySphere."

        def show():
            global win
            try:
                win.delete()
            except:
                pass
            win = pm.window(title = "Dome Generator")

            mainLayout = pm.verticalLayout()
            mainLayout.setHeight(400)


            def buttonPressed(*args):
                #Create Sphere
                if checkBoxSphere.getValue():
                    radiusSet = rSlider.getValue()
                    xSubDivs = xSubDSlider.getValue()
                    ySubDivs = ySubDSlider.getValue()
                    pm.polySphere(r = radiusSet, sx = xSubDivs, sy = ySubDivs)
                #Move on to create lights        
                    win.delete()

                if checkBoxCube.getValue():
                #CreateCube
                    xDime = xSlider.getValue()
                    yDime = ySlider.getValue()
                    zDime = zSlider.getValue()
                    xSubDivs = xSubDSlider.getValue()
                    ySubDivs = ySubDSlider.getValue()
                    zSubDivs = zSubDSlider.getValue()
                    pm.polyCube(w = xDime, h = yDime, d = zDime, sx = xSubDivs, sy = ySubDivs, sz = zSubDivs)
                #Move on to create lights        
                    win.delete()

                if checkBoxCone.getValue():
                #Create Cone
                    yDime = ySlider.getValue()
                    radiusSet = rSlider.getValue()
                    xSubDivs = xSubDSlider.getValue()
                    ySubDivs = ySubDSlider.getValue()
                    zSubDivs = zSubDSlider.getValue()
                    pm.polyCone(h = yDime, r = radiusSet, sx = xSubDivs, sy = ySubDivs, sz = zSubDivs)
                #Move on to create lights        

                    win.delete()

                if checkBoxCylinder.getValue():
                #Create Cylinder 
                    yDime = ySlider.getValue()
                    radiusSet = rSlider.getValue()
                    xSubDivs = xSubDSlider.getValue()
                    ySubDivs = ySubDSlider.getValue()
                    zSubDivs = zSubDSlider.getValue()
                    pm.polyCylinder(h = yDime, r = radiusSet, sx = xSubDivs, sy = ySubDivs, sz = zSubDivs)
                #Move on to create lights        
                    win.delete()

            def sphereBoxChecked(*args):
                xSlider = args[0]
                ySlider = args[1]
                zSlider = args[2]
                rSlider = args[3]
                xSubDSlider = args[4]
                ySubDSlider = args[5]
                zSubDSlider = args[6]
                checkBoxCube = args[7]
                checkBoxCone = args[8]
                checkBoxCylinder = args[9]
            #Checkbox off
                checkBoxCube.setValue(False)
                checkBoxCone.setValue(False)
                checkBoxCylinder.setValue(False)
             #Slider enable
                xSlider.setValue(en = False)
                ySlider.setValue(en = False)
                zSlider.setValue(en = False)
                rSlider.setValue(5, en = True)
                xSubDSlider.setValue(10, en = True)
                ySubDSlider.setValue(10, en = True)
                zSubDSlider.setValue(en = False)


            def cubeBoxChecked(*args):
                xSlider = args[0]
                ySlider = args[1]
                zSlider = args[2]
                rSlider = args[3]
                xSubDSlider = args[4]
                ySubDSlider = args[5]
                zSubDSlider = args[6]
                checkBoxSphere = args[7]
                checkBoxCone = args[8]
                checkBoxCylinder = args[9]
            #Checkbox off
                checkBoxSphere.setValue(False)
                checkBoxCone.setValue(False)
                checkBoxCylinder.setValue(False)
            #Slider enable
                xSlider.setValue(10, en = True)
                ySlider.setValue(10, en = True)
                zSlider.setValue(10, en = True)
                rSlider.setValue(en = False)
                xSubDSlider.setValue(5, en = True)
                ySubDSlider.setValue(5, en = True)
                zSubDSlider.setValue(5, en = True)

            def coneBoxChecked(*args):
                xSlider = args[0]
                ySlider = args[1]
                zSlider = args[2]
                rSlider = args[3]
                xSubDSlider = args[4]
                ySubDSlider = args[5]
                zSubDSlider = args[6]
                checkBoxSphere = args[7]
                checkBoxCube = args[8]
                checkBoxCylinder = args[9]
            #Checkbox off
                checkBoxSphere.setValue(False)
                checkBoxCube.setValue(False)
                checkBoxCylinder.setValue(False)
            #Slider enable
                xSlider.setValue(en = False)
                ySlider.setValue(10, en = True)
                zSlider.setValue(en = False)
                rSlider.setValue(5, en = True)
                xSubDSlider.setValue(15, en = True)
                ySubDSlider.setValue(10, en = True)
                zSubDSlider.setValue(5, en = True)

            def cylinderBoxChecked(*args):
            #Checkbox off
                checkBoxSphere.setValue(False)
                checkBoxCube.setValue(False)
                checkBoxCone.setValue(False)
            #Slider enable
                xSlider.setValue(en = False)
                ySlider.setValue(15, en = True)
                zSlider.setValue(en = False)
                rSlider.setValue(5, en = True)
                xSubDSlider.setValue(15, en = True)
                ySubDSlider.setValue(5, en = True)
                zSubDSlider.setValue(5, en = True)

        #Slider settings
            #Dimensions
            xSlider = pm.floatSliderGrp(label = "x Dimension", field = True, parent = mainLayout, en = False)
            xSlider.setValue(10)
            ySlider = pm.floatSliderGrp(label = "y Dimension", field = True, parent = mainLayout, en = False)
            ySlider.setValue(10)
            zSlider = pm.floatSliderGrp(label = "z Dimension", field = True, parent = mainLayout, en = False)
            zSlider.setValue(10)
            rSlider = pm.floatSliderGrp(label = "Radius", field = True, parent = mainLayout, en = False)
            rSlider.setValue(10)
            #SubDivisions
            xSubDSlider = pm.intSliderGrp(label = "x SubDivs",field = True, parent = mainLayout, en = False)
            xSubDSlider.setValue(10)
            ySubDSlider = pm.intSliderGrp(label = "y SubDivs", field = True, parent = mainLayout, en = False)
            ySubDSlider.setValue(10)
            zSubDSlider = pm.intSliderGrp(label = "z SubDivs", field = True, parent = mainLayout, en = False)
            zSubDSlider.setValue(10)

        #Check Box Settings
            checkBoxSphere = pm.checkBox(label = "Sphere", value = False, parent = mainLayout, onc = pm.Callback(sphereBoxChecked, xSlider, ySlider, zSlider, rSlider, xSubDSlider, ySubDSlider, zSubDSlider, checkBoxCube, checkBoxCone, checkBoxCylinder))
            checkBoxCube = pm.checkBox(label = "Cube", value = False, parent = mainLayout, onc = pm.Callback(cubeBoxChecked, xSlider, ySlider, zSlider, rSlider, xSubDSlider, ySubDSlider, zSubDSlider, checkBoxSphere, checkBoxCone, checkBoxCylinder))
            checkBoxCone = pm.checkBox(label = "Cone", value = False, parent = mainLayout, onc = pm.Callback(coneBoxChecked, xSlider, ySlider, zSlider, rSlider, xSubDSlider, ySubDSlider, zSubDSlider, checkBoxSphere, checkBoxCube, checkBoxCylinder))
            checkBoxCylinder = pm.checkBox(label = "Cylinder", value = False, parent = mainLayout, onc = pm.Callback(cylinderBoxChecked))


            btn = pm.button(label = "Create", parent = mainLayout, command = pm.Callback(buttonPressed))
            mainLayout.redistribute()

            win.show()
        show()
dome = MakeLightDome()
dome.checkSelection()
4

1 回答 1

1

您可能不想为此使用全局,这会使您的状态管理复杂化。执行此操作的通常“正确”方法(故意使用引号)是将 GUI 包装在一个类中并将所有状态存储在该类中,因此很容易知道所有这些都将相同的状态数据连接到相同的小部件。除了使代码更整洁之外,它还可以打开同一个对话框的多个副本(例如,如果您需要那种东西,每个对象一个)。

这是一个非常简单的例子。由于所有小部件(至少是您关心的所有小部件)都包含在对话框的“自我”中,因此在响应按钮单击时获取所需信息很简单

import pymel.core as pm

class SliderDialog(object):

     def __init__(self):
        self.SomeSpecialData = "Hello world"
        self.Window = pm.window(width = 300)
        with pm.columnLayout():
           self.XSlider = pm.floatSliderGrp(label  = 'x', field = True)
           self.YSlider = pm.floatSliderGrp(label  = 'y', field = True)
           self.ZSlider = pm.floatSliderGrp(label  = 'z', field = True)
           with pm.rowLayout(nc= 2):
               self.Cancel = pm.button("cancel", width = 150, command = self.cancel_button)
               self.Go = pm.button("go", width = 150, c=self.go_button)
     def show(self):
         self.Window.show()

     def go_button(self, *_):
         # your actual work goes here
         print "Special Data = ", self.SomeSpecialData 
         x, y, z = self.XSlider.getValue(), self.YSlider.getValue(),  self.ZSlider.getValue()          

         print "do something with XYZ (%i, %i, %i)" % (x, y, z)

     def cancel_button(self, *_):
         print "cancelling"
         pm.deleteUI(self.Window)

SliderDialog().show()

这是有关将 python 函数挂钩到 Maya GUI 回调的详细参考。

于 2014-03-12T20:09:20.740 回答