Maya Python - Using data from UI
Question: 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.


Answer: The easiest way to pass complex information around inside a GUI in Maya is to
wrap the whole UI in python class. The class can 'remember' all of your
fields, sliders, etc so that you can easily collect information from one or
more GUI items and act on them without too much extra work.

    
    
    class PistoUI(object):
    
        def __init__(self):
            self.window =  cmds.window("Pistol")
            cmds.columnLayout(adjustableColumn=True)
            self.bullet_count = cmds.intSliderGrp(label='Number of Shots', minValue=1, maxValue=9, value=4, field=True)
            self.distance = cmds.intSliderGrp(label='Distance to Target (metres)',  minValue=1, maxValue=50, value=25, field=True)
            cmds.button(label = "Fire", command = self.fire)
            cmds.button(label = "Cancel", command = self.cancel)
            cmds.showWindow(self.window)
    
        def fire(self, _ignore):
            bullets  =  cmds.intSliderGrp(self.bullet_count, q=True, v=True)
            distance = cmds.intSliderGrp(self.distance, q=True, v=True).
            goShoot(bullets, distance)
    
        def cancel(self, _ignore):
            cmds.deleteUI(self.window)
    

As you can see above, the `fire` function gets the correct fields from the
active window and collects their values to pass to the goShoot function
without any extra work in the layout step to pass the values directly to a
function. This is much simpler and more elegant than leaving all the pieces
lying around in the open. It's also more self-contained - you can create
multiple windows side-by-side in that scheme without worrying about creating
and deleting them by name.

Even better, the clases are really good for separating out differences in
logic from difference in data, so you can resuse the repetitive code with
ease:

    
    
    class WeaponUI(object):
        LABEL = 'weapon_name'  #default name
        SHOTS = (1, 9, 4)   # default shots
        RANGE = (1, 50, 25)  # default range
    
        def __init__(self):
            self.window =  cmds.window(title = self.LABEL)
            cmds.columnLayout(adjustableColumn=True)
            self.bullet_count = cmds.intSliderGrp(label='Number of Shots', minValue=self.SHOTS[0],
                                                        maxValue=self.SHOTS[1],
                                                        value=self.SHOTS[2], field=True)
            self.distance = cmds.intSliderGrp(label='Distance to Target (metres)',  
                                                        minValue=self.RANGE[0], 
                                                        maxValue=self.RANGE[1], 
                                                        value=self.RANGE[2], field=True)
            cmds.button(label = "Fire", command = self.fire)
            cmds.button(label = "Cancel", command = self.cancel)
            cmds.showWindow(self.window)
    
    
        def fire(self, _ignore):
            bullets  =  cmds.intSliderGrp(self.bullet_count, q=True, v=True)
            distance = cmds.intSliderGrp(self.distance, q=True, v=True).
            print "firing", self.LABEL
            goShoot(bullets, target)
    
        def cancel(self, _ignore):
            cmds.deleteUI(self.window)
    
    class PistolUI(WeaponUI):
        LABEL = 'pistol'  
        SHOTS = (1, 9, 4)   
        RANGE = (1, 50, 25) 
    
    class ShotgunUI(WeaponUI):
        LABEL = 'shotgun'  
        SHOTS = (1, 4, 2)  
        RANGE = (1, 50, 25) 
    
    class SniperUI(WeaponUI):
        LABEL = 'sniper'  
        SHOTS = (1, 4, 2)  
        RANGE = (1, 50, 25) 
    

... and so on

More about maya GUI connections
[here](http://techartsurvival.blogspot.com/2014/04/maya-callbacks-cheat-
sheet.html) and [here](http://techartsurvival.blogspot.com/2014/02/rescuing-
maya-gui-from-itself.html)

