Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
470 views
in Technique[技术] by (71.8m points)

user interface - Maya Python - Using data from UI

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...