Maya Python - 将 UI 连接到函数

Maya Python - Connecting UI to functions

我正在使用 Python 在 Maya 中创建一个小的子弹喷射发生器 运行,我一直在研究如何获取用户在 [=24] 中输入的值=] 并在函数中使用它们。 bulletSpread 函数需要从 createGunUI 中获取值,特别是 DistCtrl 滑块并将其乘以 GunDictionary 中的值以获得点差。简而言之,我只需要知道如何从 UI 中的滑块获取输入值并在函数中使用它们。

我在下面包含了我的代码,非常感谢任何帮助!

import maya.cmds as cmds
import random
import math

    #Dictionary containing weapon names and presets - presets contain values for shot sliders, distance sliders and multipliers for spread calculation
GunDictionary = {}
GunDictionary["weapon"] = ["Pistol", "Shotgun", "SMG", "Sniper", "RPG"]
GunDictionary["weaponSelected"] = GunDictionary["weapon"][0]
GunDictionary["Pistol_preset"] = [(1,18,9), (10,50,25), (0.1)]
GunDictionary["Shotgun_preset"] = [(1,4,2), (10,50,25), (0.3)]
GunDictionary["SMG_preset"] = [(5,30,15), (10,50,25), (0.2)]
GunDictionary["Sniper_preset"] = [(1,3,2), (10,50,25), (0.05)]
GunDictionary["RPG_preset"] = [(1,2,1), (10,50,25), (0)]

    #Initial cleanup of UIs
if (cmds.window("Gun_Select", exists = True)): 
    cmds.deleteUI("Gun_Select")

    #Initial cleanup of scene
cmds.select(all=True)
cmds.delete()


    #Fire button condition - creates wall
def goShoot(gunSelected, numOfShots, *pArgs): 
    print "Begin"
    weaponName = GunDictionary["weaponSelected"]
    cmds.deleteUI(weaponName)
    createWall()
    bulletSpread(GunDictionary["weaponSelected"])


    #Cancel UI2 - deletes UI2
def cancelShoot(*pArgs):
    print "cancel"
    weaponName = GunDictionary["weaponSelected"]
    cmds.deleteUI(weaponName)

    #Cancel UI1 - deletes UI1
def cancelSelect(*pArgs):
    print "cancel"
    cmds.deleteUI("Gun_Select")


    #Function to create wall
def createWall():
    wall = cmds.polyCube(h=10, w=15, d=1, name='wall')
    cmds.move(0,5,0, 'wall')



newRange = 0;
    #Function to generate bullet spread
def bulletSpread(gunSelected, distance):
    weaponName = GunDictionary["weaponSelected"]
    multiplier = GunDictionary[weaponName + "_preset"][2]
    distance = ???
    newRange = distance * multiplier
    print newRange



    #Function to create drop-down menu in createSelectUI
def printNewMenuItem(item):
    print item
    GunDictionary["weaponSelected"] = item
    return GunDictionary["weaponSelected"]



    #Function to take weapon selected in UI1 and call UI2, also calls function to calculate bullet spread
def ui_refreshSelWeapon(fun, *args):
    createGunUI(GunDictionary["weaponSelected"])



    #UI allowing user to pick from a selection of Guns in a drop-down menu
def createSelectUI(): 
    cmds.window("Gun_Select")
    cmds.columnLayout(adjustableColumn=True)

    GunSelectCtrl = cmds.optionMenu(label='Gun', changeCommand=printNewMenuItem)
    for i in GunDictionary["weapon"]:
        cmds.menuItem(label=i)

    cmds.button(label = "Continue", command = ui_refreshSelWeapon)

    cmds.button(label = "Cancel", command = cancelSelect)

    cmds.showWindow("Gun_Select")

createSelectUI()



weapon_uiDic = {}

    #Called after gun select, allows user to choose the number of shots and distance from wall
def createGunUI(gunSelected):

    weaponName = GunDictionary["weaponSelected"]

    if cmds.window(weaponName, exists=True):
       cmds.deleteUI(weaponName)

    cmds.window(weaponName)
    cmds.columnLayout(adjustableColumn=True)

    cmds.deleteUI("Gun_Select")

    numBullets = GunDictionary[weaponName + "_preset"][0]
    distToTarget = GunDictionary[weaponName + "_preset"][1]

    weapon_uiDic["NumBulletsCtrl"] = cmds.intSliderGrp(label='Number of Shots',
                                                       minValue=numBullets[0], maxValue=numBullets[1], value=numBullets[2], field=True)

    weapon_uiDic["DistCtrl"] = cmds.intSliderGrp(label='Distance to Target (metres)', 
                                                 minValue=distToTarget[0], maxValue=distToTarget[1], value=distToTarget[2], field=True)

    weapon_uiDic["fireButton"] = cmds.button(label = "Fire", 
                                            command = lambda *args: goShoot(cmds.intSliderGrp(weapon_uiDic["NumBulletsCtrl"], 
                                            query=True, value=True), cmds.intSliderGrp(weapon_uiDic["DistCtrl"], query=True, value=True)))

    cmds.button(label = "Cancel", command = cancelShoot)

    cmds.showWindow(weaponName)

谢谢!!

如果我没理解错的话,你要查询滑块的值。给定 Maya 帮助中的以下示例:http://download.autodesk.com/us/maya/2009help/CommandsPython/intSliderGrp.html

window = cmds.window( title='intSliderGrp Example' )
cmds.columnLayout()
cmds.intSliderGrp( field=True, label='Group 1' )
my_value = cmds.intSliderGrp( field=True, label='Group 2',     minValue=-10, maxValue=10, fieldMinValue=-100, fieldMaxValue=100, value=0 )
cmds.showWindow( window )

# Query the value
cmds.intSliderGrp(my_value, query=True, value=True)

我用部分模块替换了你的 lambda 函数。它还用于通过 maya UI.

传递一些参数

我不明白你为什么不通过不同的功能跟踪你的距离数据。 如果您不了解它们的流程,您只需阅读您的代码并在每一步打印值以查看它的去向。 为什么在 "goShoot" 中,如果您使用 GunDictionary["weaponSelected"] 而不是 Gunselected,则定义要输入的变量 gunSelected。 在您的 "bulletSpread" 中,您指定了三个参数并在 "goShoot" 下仅输入一个。 如果您的函数没有正确链接,它就无法工作。

您可以在下面找到我的代码版本:

import maya.cmds as cmds
import random
import math
from functools import partial

    #Dictionary containing weapon names and presets - presets contain values for shot sliders, distance sliders and multipliers for spread calculation
GunDictionary = {}
GunDictionary["weapon"] = ["Pistol", "Shotgun", "SMG", "Sniper", "RPG"]
GunDictionary["weaponSelected"] = GunDictionary["weapon"][0]
GunDictionary["Pistol_preset"] = [(1,18,9), (10,50,25), (0.1)]
GunDictionary["Shotgun_preset"] = [(1,4,2), (10,50,25), (0.3)]
GunDictionary["SMG_preset"] = [(5,30,15), (10,50,25), (0.2)]
GunDictionary["Sniper_preset"] = [(1,3,2), (10,50,25), (0.05)]
GunDictionary["RPG_preset"] = [(1,2,1), (10,50,25), (0)]

    #Initial cleanup of UIs
if (cmds.window("Gun_Select", exists = True)): 
    cmds.deleteUI("Gun_Select")

    #Initial cleanup of scene
cmds.select(all=True)
cmds.delete()


    #Fire button condition - creates wall
def goShoot(gunSelected, numOfShots, distance, *pArgs): 
    print "Begin"
    weaponName = gunSelected
    cmds.deleteUI(weaponName)
    createWall()
    bulletSpread(GunDictionary["weaponSelected"], distance)


#Cancel UI2 - deletes UI2
def cancelShoot(*pArgs):
    print "cancel"
    weaponName = GunDictionary["weaponSelected"]
    cmds.deleteUI(weaponName)

#Cancel UI1 - deletes UI1
def cancelSelect(*pArgs):
    print "cancel"
    cmds.deleteUI("Gun_Select")


#Function to create wall
def createWall():
    wall = cmds.polyCube(h=10, w=15, d=1, name='wall')
    cmds.move(0,5,0, 'wall')



#Function to generate bullet spread
def bulletSpread(gunSelected, distance):
    weaponName = GunDictionary["weaponSelected"]
    multiplier = GunDictionary[weaponName + "_preset"][2]
    distance = distance
    newRange = distance * multiplier
    print newRange



    #Function to create drop-down menu in createSelectUI
def printNewMenuItem(item):
    print item
    GunDictionary["weaponSelected"] = item
    return GunDictionary["weaponSelected"]



    #Function to take weapon selected in UI1 and call UI2, also calls function to calculate bullet spread
def ui_refreshSelWeapon(fun, *args):
    createGunUI(GunDictionary["weaponSelected"])



    #UI allowing user to pick from a selection of Guns in a drop-down menu
def createSelectUI(): 
    cmds.window("Gun_Select")
    cmds.columnLayout(adjustableColumn=True)

    GunSelectCtrl = cmds.optionMenu(label='Gun', changeCommand=printNewMenuItem)
    for i in GunDictionary["weapon"]:
        cmds.menuItem(label=i)

    cmds.button(label = "Continue", command = ui_refreshSelWeapon)

    cmds.button(label = "Cancel", command = cancelSelect)

    cmds.showWindow("Gun_Select")

createSelectUI()

def refreshValues(*args):
    function = args[0]
    weapon = args[1]
    get01 = cmds.intSliderGrp(args[2],query=True, value=True)
    get02 = cmds.intSliderGrp(args[3],query=True, value=True)
    print get01, get02
    function(weapon, get01, get02)

weapon_uiDic = {}

#Called after gun select, allows user to choose the number of shots and distance from wall
def createGunUI(gunSelected):

    weaponName = GunDictionary["weaponSelected"]

    if cmds.window(weaponName, exists=True):
       cmds.deleteUI(weaponName)

    cmds.window(weaponName)
    cmds.columnLayout(adjustableColumn=True)

    cmds.deleteUI("Gun_Select")

    numBullets = GunDictionary[weaponName + "_preset"][0]
    distToTarget = GunDictionary[weaponName + "_preset"][1]

    weapon_uiDic["NumBulletsCtrl"] = cmds.intSliderGrp(label='Number of Shots',
                                                       minValue=numBullets[0], maxValue=numBullets[1], value=numBullets[2], field=True)

    weapon_uiDic["DistCtrl"] = cmds.intSliderGrp(label='Distance to Target (metres)', 
                                                 minValue=distToTarget[0], maxValue=distToTarget[1], value=distToTarget[2], field=True)

    weapon_uiDic["fireButton"] = cmds.button(label = "Fire",  command = '')
    cmds.button(weapon_uiDic["fireButton"], e=1,                                      
                                            c=partial(refreshValues,
                                                      goShoot,
                                                      GunDictionary["weaponSelected"],
                                                      weapon_uiDic["NumBulletsCtrl"],
                                                      weapon_uiDic["DistCtrl"]))

    cmds.button(label = "Cancel", command = cancelShoot)

    cmds.showWindow(weaponName)