[英](Maya Python) How do I run radio option buttons through a GUI function?
通常,當我運行帶有單選按鈕選項的腳本時,您會選擇一個單選按鈕選項,然后使用該按鈕激活它。 我曾經把我的菜單 gui 放在一個函數之外:但是自從我學會了導入 Maya 腳本后,我開始將我的菜單界面包裝在一個 GUI 函數中,這意味着我的單選按鈕技術現在不再有效。 我也不知道如何讓它工作。 腳本本身很簡單:只需在導入腳本后選擇一個單選按鈕選項,然后使用按鈕創建一個形狀:至少它應該如何工作。 相反,我沒有得到任何錯誤,也沒有創建任何形狀,我不知道是什么壞了。
'''
import cubeTestTemp
reload (cubeTestTemp)
cubeTestTemp.gui()
'''
import maya.cmds as cmds
#Creates ui.
if cmds.window("cubeWin", exists =True):
cmds.deleteUI("cubeWin", window = True)
myWindow = cmds.window("cubeWin",t='DS shapeDemo V1',w=200, h=500, toolbox=True)
column = cmds.columnLayout(adj=True)
#Creates variable to indicate the check box status. 1=Checked 0=Not Checked.
cubeNum1 = 0
cubeNum2 = 0
#Creates Button funtion.
def defaultButtonPush(*args):
#The Print checks if button is pushed and what is the check box status.
print "Button is pushed."
print cubeNum1
#Check box argument finds the status of the variable and determines what to do.
#Eather create a cube or display a error dialog box.
if cubeNum1 == 1:
print "Cube Creation sucessful"
cmds.polyCube()
print "Button is pushed."
print cubeNum2
if cubeNum2 == 1:
print "Sphere Creation sucessful"
cmds.polySphere()
def gui(*args):
#Creates check box.
#In the onCommand Script (onc) and offCommand Script (ofc) flags all commands must be made in between double quotes.
cmds.radioCollection()
cmds.radioButton(label='Cube',align='left',onCommand="cubeNum1 = 1", offCommand="cubeNum1 = 0")
cmds.radioButton(label='Sphere',align='left',onCommand="cubeNum2 = 1", offCommand="cubeNum2 = 0")
#Creates Button.
cmds.button( label='Execute', command=defaultButtonPush ,align='left' )
cmds.showWindow()
這是修改后的腳本。 問題是腳本的范圍。 “onCommand”在其自己的范圍內執行,該范圍具有自己的“cubeNum”變量。
"""
import cubeTestTemp
reload (cubeTestTemp)
cubeTestTemp.gui()
"""
import maya.cmds as cmds
# Variable to indicate the radio button status. 1=Cube, 2=Shpere.
shape_selector = 0
def action_button(*args):
"""
Create an object based on the shape_selector status
"""
print "Button is pushed", repr(args)
if shape_selector == 1:
cmds.polyCube()
print "Cube created"
elif shape_selector == 2:
cmds.polySphere()
print "Sphere created"
else:
print "No shape selected"
def selection_changed(shape):
"""
Save the current shape selection
into global variable "shape_selector"
"""
global shape_selector
shape_selector = shape
print "Current selection:", shape_selector
def gui():
"""
Create the GUI
"""
if cmds.window("cubeWin", exists=True):
cmds.deleteUI("cubeWin", window=True)
myWindow = cmds.window("cubeWin", t='DS shapeDemo V1', w=200, h=500, toolbox=True)
column = cmds.columnLayout(adj=True)
# Create the radio buttons
cmds.radioCollection()
cmds.radioButton(label='Cube',align='left', select=True, onCommand=lambda x:selection_changed(1))
cmds.radioButton(label='Sphere',align='left', onCommand=lambda x:selection_changed(2))
# Create the push button
cmds.button(label='Create', command=action_button, align='left')
cmds.showWindow()
if __name__ == "__main__":
gui()
另一種方法是僅在需要時讀取單選按鈕狀態
"""
import cubeTestTemp
reload (cubeTestTemp)
cubeTestTemp.gui()
"""
import maya.cmds as cmds
def action_button(cube_button, sphere_button):
"""
Create an object based on the shape_selector status
"""
if cmds.radioButton(cube_button, query=True,select=True):
cmds.polyCube()
print "Cube created"
elif cmds.radioButton(sphere_button, query=True,select=True):
cmds.polySphere()
print "Sphere created"
else:
print "No shape selected"
def gui():
"""
Create the GUI
"""
if cmds.window("cubeWin", exists=True):
cmds.deleteUI("cubeWin", window=True)
myWindow = cmds.window("cubeWin", t='DS shapeDemo V1', w=200, h=500, toolbox=True)
column = cmds.columnLayout(adj=True)
# Create the radio buttons
cmds.radioCollection()
# Save the button ids
cube_button = cmds.radioButton(label='Cube',align='left', select=True)
sphere_button = cmds.radioButton(label='Sphere',align='left')
# Create the push button
cmds.button(label='Create', command=lambda x:action_button(cube_button, sphere_button), align='left')
cmds.showWindow()
if __name__ == "__main__":
gui()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.