gpt4 book ai didi

python - 玛雅Python : OptionMenu Selection With Button

转载 作者:行者123 更新时间:2023-12-01 04:48:52 25 4
gpt4 key购买 nike

我是 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 功能/重置按钮似乎不起作用,因此如果有答案,请告诉我。

最佳答案

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 = "多边形选择:")然后可以使用以下代码在代码中的任何位置访问此 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

希望这对您有帮助。

关于python - 玛雅Python : OptionMenu Selection With Button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28833940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com