我正在創建一個使用Python的Maya中運行的Bullet Spray Generator,我一直在堅持如何在用戶界面中輸入用戶輸入的值,並在函數中使用它們。 bulletSpread
函數需要取值爲createGunUI
,特別是DistCtrl
滑塊,並將其乘以GunDictionary
中的值以獲得擴展。簡而言之,我只需要知道如何從UI中的滑塊獲取輸入值並在函數中使用它們。Maya Python - 將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)
謝謝!
感謝您的幫助! – Arazmis