Вот модифицированный скрипт. Проблема заключалась в объеме сценариев. «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()