I am working on a scripting program and struggling a bit with the UI. I've made a couple of UIs, all of which seem to work fine individually, but I don't know how to use the data inputted in a UI to another function. I'm trying to get the Gun Type (selected by the user in Bullet_Spray_Generator
) to then affect which of the UIs is then called. Each Gun Type requires a different set of values for the sliders, so I made a different UI for each. I think I need to pass the data selected from the BSG into an if
function in order to call the correct (second) UI, but running it just always jumps to the else
function and closes the window. Here's my code so far:
import maya.cmds as cmds
from functools import partial
if (cmds.window("Bullet_Spray_Generator", exists = True)):
cmds.deleteUI("Bullet_Spray_Generator")
if (cmds.window("BSG2", exists = True)):
cmds.deleteUI("BSG2")
cmds.select(all=True)
cmds.delete()
def goShoot(numOfShots, distToTarget, *pArgs):
print "Begin"
cmds.deleteUI("BSG2")
createWall()
def cancelShoot(*pArgs):
print "cancel"
cmds.deleteUI("Bullet_Spray_Generator")
def createWall():
cmds.select(all=True)
cmds.delete()
wall = cmds.polyCube(h=10, w=15, d=1, name='wall')
cmds.move(0,5,0, 'wall')
def createGunUI(gunType, *pArgs):
if (GunSelectCtrl == 'Pistol'):
createPistolUI()
elif (GunSelectCtrl == 'Shotgun'):
createShotgunUI()
elif (GunSelectCtrl == 'SMG'):
createSMGUI()
elif (GunSelectCtrl == 'Sniper Rifle'):
createSniperUI()
elif (GunSelectCtrl == 'RPG'):
createRPGUI()
else:
print "Something went wrong"
cancelShoot()
def createPistolUI():
cmds.window("Pistol")
cmds.columnLayout(adjustableColumn=True)
cmds.deleteUI("Bullet_Spray_Generator")
NumBulletsCtrl = cmds.intSliderGrp(label='Number of Shots',
minValue=1, maxValue=9, value=4, field=True)
DistCtrl = cmds.intSliderGrp(label='Distance to Target (metres)',
minValue=1, maxValue=50, value=25, field=True)
cmds.button(label = "Fire", command = lambda *args: goShoot(cmds.intSliderGrp(NumBulletsCtrl,
query=True, value=True), cmds.intSliderGrp(DistCtrl, query=True, value=True), ))
cmds.button(label = "Cancel", command = cancelShoot)
cmds.showWindow("Pistol")
def createShotgunUI():
cmds.window("Shotgun")
cmds.columnLayout(adjustableColumn=True)
cmds.deleteUI("Bullet_Spray_Generator")
NumBulletsCtrl = cmds.intSliderGrp(label='Number of Shots',
minValue=1, maxValue=4, value=2, field=True)
DistCtrl = cmds.intSliderGrp(label='Distance to Target (metres)',
minValue=1, maxValue=50, value=25, field=True)
cmds.button(label = "Fire", command = lambda *args: goShoot(cmds.intSliderGrp(NumBulletsCtrl,
query=True, value=True), cmds.intSliderGrp(DistCtrl, query=True, value=True), ))
cmds.button(label = "Cancel", command = cancelShoot)
cmds.showWindow("Shotgun")
def createSMGUI():
cmds.window("SMG")
cmds.columnLayout(adjustableColumn=True)
cmds.deleteUI("Bullet_Spray_Generator")
NumBulletsCtrl = cmds.intSliderGrp(label='Number of Shots',
minValue=1, maxValue=20, value=4, field=True)
DistCtrl = cmds.intSliderGrp(label='Distance to Target (metres)',
minValue=1, maxValue=50, value=25, field=True)
cmds.button(label = "Fire", command = lambda *args: goShoot(cmds.intSliderGrp(NumBulletsCtrl,
query=True, value=True), cmds.intSliderGrp(DistCtrl, query=True, value=True), ))
cmds.button(label = "Cancel", command = cancelShoot)
cmds.showWindow("SMG")
def createSniperUI():
cmds.window("Sniper")
cmds.columnLayout(adjustableColumn=True)
cmds.deleteUI("Bullet_Spray_Generator")
NumBulletsCtrl = cmds.intSliderGrp(label='Number of Shots',
minValue=1, maxValue=2, value=2, field=True)
DistCtrl = cmds.intSliderGrp(label='Distance to Target (metres)',
minValue=1, maxValue=50, value=25, field=True)
cmds.button(label = "Fire", command = lambda *args: goShoot(cmds.intSliderGrp(NumBulletsCtrl,
query=True, value=True), cmds.intSliderGrp(DistCtrl, query=True, value=True), ))
cmds.button(label = "Cancel", command = cancelShoot)
cmds.showWindow("Sniper")
def createRPGUI():
cmds.window("RPG")
cmds.columnLayout(adjustableColumn=True)
cmds.deleteUI("Bullet_Spray_Generator")
NumBulletsCtrl = cmds.intSliderGrp(label='Number of Shots',
minValue=1, maxValue=1, value=1, field=True)
DistCtrl = cmds.intSliderGrp(label='Distance to Target (metres)',
minValue=1, maxValue=50, value=25, field=True)
cmds.button(label = "Fire", command = lambda *args: goShoot(cmds.intSliderGrp(NumBulletsCtrl,
query=True, value=True), cmds.intSliderGrp(DistCtrl, query=True, value=True), ))
cmds.button(label = "Cancel", command = cancelShoot)
cmds.showWindow("RPG")
def printNewMenuItem(item):
print item
return item
def createUI():
cmds.window("Bullet_Spray_Generator")
cmds.columnLayout(adjustableColumn=True)
GunSelectCtrl = cmds.optionMenu(label='Gun', changeCommand=printNewMenuItem)
cmds.menuItem(label='Pistol')
cmds.menuItem(label='Shotgun')
cmds.menuItem(label='SMG')
cmds.menuItem(label='Sniper Rifle')
cmds.menuItem(label='RPG')
cmds.button(label = "Continue", command = partial(createGunUI, GunSelectCtrl))
cmds.button(label = "Cancel", command = cancelShoot)
cmds.showWindow("Bullet_Spray_Generator")
createUI()
Any help would be greatly appreciated, thanks very much.
See Question&Answers more detail:
os