源码

Maya Python – 使用来自UI的数据


我正在编写一个脚本程序,并在UI上苦苦挣扎.我已经制作了几个UI,所有这些UI似乎都可以单独使用,但我不知道如何将UI中输入的数据用于其他功能.我正在尝试获取Gun Type(由用户在Bullet_Spray_Generator中选择)然后影响随后调用哪些UI.每种枪型都需要一组不同的滑块值,因此我为每个枪型设置了不同的UI.我想我需要将从BSG中选择的数据传递给if函数以调用正确的(第二个)UI,但运行它只是总是跳转到else函数并关闭窗口.到目前为止,这是我的代码:

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()

非常感谢任何帮助,非常感谢.

(0)

本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/maya-python-shiyonglaiziuideshuju.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:9月 25, 2019 at 07:14 下午

热评文章