Maya Python – Using data from UI

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 and here

Leave a Comment