2015-05-11 69 views
-1

我正在創建一個使用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) 

謝謝!

回答

0

我通過局部模塊更換您的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) 
+0

感謝您的幫助! – Arazmis

0

如果我理解正確,您必須查詢滑塊的值。鑑於從瑪雅人的幫助下面的例子: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) 
+0

歡呼聲中,我給它一個去:) – Arazmis