Maya Python:带按钮的OptionMenu选择

时间:2015-03-03 14:11:09

标签: python user-interface button maya optionmenu

我是Maya中的python新手,我正在尝试构建一个可以生成形状并对其进行转换的UI。我认为问题在于ObjectCreation函数,但我不确定。到目前为止,这就是我所拥有的:

import maya.cmds as cmds

#check to see if window exists
if cmds.window("UserInterface", exists = True):
    cmds.deleteUI("UserInterface")
#create actual window
UIwindow = cmds.window("UserInterface", title = "User Interface Test", w = 500, h = 700, mnb = False, mxb = False, sizeable = False)
mainLayout = cmds.columnLayout(w = 300, h =500)

def SceneClear(*args):
    cmds.delete(all=True, c=True) #Deletes all objects in scene
cmds.button(label = "Reset", w = 300, command=SceneClear)

polygonSelectMenu = cmds.optionMenu(w = 250, label = "Polygon Selection:")
cmds.menuItem(label = " ")
cmds.menuItem(label = "Sphere")
cmds.menuItem(label = "Cube")
cmds.menuItem(label = "Cylinder")
cmds.menuItem(label = "Cone")

def ObjectCreation(*args):
    if polygonSelectMenu.index == 2: #tried referring to index
        ma.polySphere(name = "Sphere")
    elif polygonSelectMenu == "Cube":
        ma.polyCube(name = "Cube")
    elif polygonSelectMenu == "Cylinder":
        ma.polyCylinder(name = "Cylinder")
    elif polygonSelectMenu == "Cone":
        ma.polyCone(name = "Cone")
cmds.button(label = "Create", w = 200, command=ObjectCreation)

def DeleteButton(*args):
    cmds.delete()
cmds.button(label = "Delete", w = 200, command=DeleteButton)#Deletes selected object   

cmds.showWindow(UIwindow) #shows window

我所追求的是让用户从选项菜单中选择一个选项,然后按下创建按钮以生成该形状。我试图通过名称和索引来引用它,但我不知道我错过了什么。就像我说我是python的新手,所以当我尝试寻找答案时,我找不到任何东西,当我找到类似的东西时,我无法理解它。加上由于某些原因,SceneClear功能/重置按钮似乎不起作用,所以如果有答案,请告诉我。

2 个答案:

答案 0 :(得分:2)

polygonSelectMenu包含optionMenu UI元素的路径。就我而言,它是:UserInterface|columnLayout7|optionMenu4。 这只是一个字符串,而不是对UI元素的引用。

要访问它的当前值,您必须使用:

currentValue = cmds.optionMenu(polygonSelectMenu, query=True, value=True)

所有optionMenu的标记都列在here (Maya 2014 commands doc)中,可查询的标记旁边有一个绿色的Q.


因此,这是您的ObjectCreation(*args)功能:

def ObjectCreation(*args):
    currentValue = cmds.optionMenu(polygonSelectMenu, query=True, value=True)
    if currentValue == "Sphere": #tried referring to index
        cmds.polySphere(name = "Sphere")
    elif currentValue == "Cube":
        cmds.polyCube(name = "Cube")
    elif currentValue == "Cylinder":
        cmds.polyCylinder(name = "Cylinder")
    elif currentValue == "Cone":
        cmds.polyCone(name = "Cone")

题外话:

避免在代码行之间声明函数(在您的情况下,UI创建代码),尝试将UI创建代码放在函数中,并在脚本的末尾调用此函数。

它是可读的,因为您现在只有很少的UI元素。但是一旦你开始拥有20个或更多按钮/标签/输入,它就会很快变得一团糟。

此外,我更喜欢为UI元素提供一个对象名称,就像使用窗口("UserInterface")一样。 举个具体的例子: cmds.optionMenu("UI_polygonOptionMenu", w = 250, label = "Polygon Selection:") 然后可以使用以下命令在代码中的任何位置访问此optionMenu: cmds.optionMenu("UI_polygonOptionMenu", query=True, value=True)

如果您需要,这是完整修改过的脚本:

import maya.cmds as cmds

def drawUI(): #Function that will draw the entire window
    #check to see if window exists
    if cmds.window("UI_MainWindow", exists = True):
        cmds.deleteUI("UI_MainWindow")
    #create actual window
    cmds.window("UI_MainWindow", title = "User Interface Test", w = 500, h = 700, mnb = False, mxb = False, sizeable = False)
    cmds.columnLayout("UI_MainLayout", w = 300, h =500)

    cmds.button("UI_ResetButton", label = "Reset", w = 300, command=SceneClear)

    cmds.optionMenu("UI_PolygonOptionMenu", w = 250, label = "Polygon Selection:")
    cmds.menuItem(label = " ")
    cmds.menuItem(label = "Sphere")
    cmds.menuItem(label = "Cube")
    cmds.menuItem(label = "Cylinder")
    cmds.menuItem(label = "Cone")

    cmds.button("UI_CreateButton", label = "Create", w = 200, command=ObjectCreation)
    cmds.button("UI_DeleteButton", label = "Delete", w = 200, command=DeleteButton)#Deletes selected object   

    cmds.showWindow("UI_MainWindow") #shows window

def SceneClear(*args):
    cmds.delete(all=True, c=True) #Deletes all objects in scene

def ObjectCreation(*args):
    currentValue = cmds.optionMenu("UI_PolygonOptionMenu", query=True, value=True)
    if currentValue == "Sphere": 
        cmds.polySphere(name = "Sphere")
    elif currentValue == "Cube":
        cmds.polyCube(name = "Cube")
    elif currentValue == "Cylinder":
        cmds.polyCylinder(name = "Cylinder")
    elif currentValue == "Cone":
        cmds.polyCone(name = "Cone")

def DeleteButton(*args):
    cmds.delete()

drawUI() #Calling drawUI now at the end of the script

希望这会对你有所帮助。

答案 1 :(得分:0)

如上所述,maya.cmds非常类似于mel,你必须使用命令cmds.optionMenu()和polygonSelectMenu作为第一个arg。

或者,如果您使用pymel,则可以使用点运算符访问polygonSelectMenu的类attrs:

import pymel.core as pm

if pm.window("UserInterface", exists = True):
    pm.deleteUI("UserInterface")

UIwindow = pm.window("UserInterface", title = "User Interface Test", w = 500, h = 700, mnb = False, mxb = False, sizeable = False)
mainLayout = pm.columnLayout(w = 300, h =500)
polygonSelectMenu = pm.optionMenu(w = 250, label = "Polygon Selection:")
pm.menuItem(label = " ")
pm.menuItem(label = "Sphere")
pm.menuItem(label = "Cube")
pm.menuItem(label = "Cylinder")
pm.menuItem(label = "Cone")
pm.button(label = "Create", w = 200, command=ObjectCreation)
UIwindow.show()


def ObjectCreation(*args):
    print polygonSelectMenu.getValue()

你也可以用一个drawUI方法把程序变成一个类,这样可以很容易地把你在ObjectCreation创建的所有项目存储在类attr中,这样你就可以删除它们使用你的重置按钮(因为我注意到你有cmds.delete(all=True)我认为在maya中不再支持),或者将UI元素存储在self.ui_element中。这样他们以后可以作为变量被引用而没有可能的冲突,因为打开多个窗口都有像“UI_CreateButton”或“okButton”等按钮......

import maya.cmds as cmds

class UI_Test_thingy():
    windowName = 'UserInterface'
    version = 'v1.1.1'
    debug = True
    createdThingys = []

    def __init__(self):
        self.drawUI()


    def drawUI(self):
        if UI_Test_thingy.debug: print 'DEBUG - drawUI called'
        #check to see if window exists
        try:
            cmds.deleteUI(UI_Test_thingy.windowName)

        except:
            pass

        #create actual window
        UIwindow = cmds.window(UI_Test_thingy.windowName, title = "User Interface Test {}".format(UI_Test_thingy.version), w = 500, h = 700, mnb = False, mxb = False, sizeable = False)
        mainLayout = cmds.columnLayout(w = 300, h =500)
        cmds.button(label = "Reset", w = 300, command=self.SceneClear)

        self.polygonSelectMenu = cmds.optionMenu(w = 250, label = "Polygon Selection:")
        cmds.menuItem(label = " ")
        cmds.menuItem(label = "Sphere")
        cmds.menuItem(label = "Cube")
        cmds.menuItem(label = "Cylinder")
        cmds.menuItem(label = "Cone")

        cmds.button(label = "Create", w = 200, command=self.ObjectCreation)

        cmds.button(label = "Delete", w = 200, command=self.DeleteButton)#Deletes selected object   

        cmds.showWindow(UIwindow) #shows window


    def DeleteButton(self, *args):
        if UI_Test_thingy.debug: print 'DEBUG - DeleteButton called: args: {}'.format(args)
        cmds.delete()


    def SceneClear(self, *args):
        if UI_Test_thingy.debug: print 'DEBUG - SceneClear called: args: {}'.format(args)
        thingsToDel = UI_Test_thingy.createdThingys[:] # copy the list of things created by this script
        UI_Test_thingy.createdThingys = [] # reset the list before deleteing the items
        print 'deleteing: {}'.format(thingsToDel)
        if thingsToDel:
            cmds.delete( thingsToDel ) #Deletes all objects created by this script

    def ObjectCreation(self, *args):
        if UI_Test_thingy.debug: print 'DEBUG - ObjectCreation called: args: {}'.format(args)
        menuVal = cmds.optionMenu(self.polygonSelectMenu, q=True, value=True)
        if menuVal == "Sphere": 
            UI_Test_thingy.createdThingys += cmds.polySphere(name = "Sphere") # store the results to the class attr createdThingys
        elif menuVal == "Cube":
            UI_Test_thingy.createdThingys += cmds.polyCube(name = "Cube") # store the results to the class attr createdThingys
        elif menuVal == "Cylinder":
            UI_Test_thingy.createdThingys += cmds.polyCylinder(name = "Cylinder") # store the results to the class attr createdThingys
        elif menuVal == "Cone":
            UI_Test_thingy.createdThingys += cmds.polyCone(name = "Cone") # store the results to the class attr createdThingys


if __name__ == '__main__':
    ui = UI_Test_thingy()