在Maya optionMenuGrp中创建函数

时间:2014-10-13 04:03:36

标签: python maya

是否可以将函数传递给Maya optionMenuGrp(combobox-like)?

我已经完成了一个简单的UI,我有这个组合框(maya cmds而不是PyQt),其中有2个选项。

我尝试使用以下代码,但我在menuItem语句中提示错误:

# Error: non-keyword arg after keyword arg
#   File "<maya console>", line 9
# SyntaxError: non-keyword arg after keyword arg #

然后我尝试使用test1_func()或test2_func的位置,它会在代码执行时打印出语句,但如果我尝试选择第二个菜单项则不再执行它...

我的代码:

def __init__(self, transform, startAnimation, endAnimation, cameraObj):

    self.fileExport = []
    self.initialWindow()

    mayaGlobal = OpenMaya.MGlobal()
    mayaGlobal.viewFrame(OpenMaya.MTime(1))

    for i in range(startAnimation, (endAnimation + 1)):

        focalLength = cameraObj.focalLength()

        vFilmApp = cameraObj.verticalFilmAperture()

        focalOut = 2* math.degrees(math.atan(vFilmApp * 25.4/ (2* focalLength)))

        myEuler = OpenMaya.MEulerRotation()
        spc = OpenMaya.MSpace.kWorld

        trans = transform.getTranslation(spc)

        rotation = transform.getRotation(myEuler)
        rotVector = OpenMaya.MVector(myEuler.asVector())

        self.fileExport.append((str(i) + '\t' + str(trans[0]) + "\t" + str(trans[1]) + "\t" + str(trans[2]) + "\t" + str(math.degrees(rotVector[0])) + "\t" + str(math.degrees(rotVector[1])) + "\t" + str(math.degrees(rotVector[2])) + "\t" + str(focalOut) + "\n"))

        mayaGlobal.viewFrame(OpenMaya.MTime(i+1))


def optionMenuCallback(*args):
    fn = cmds.menuItem (args[0], q=True, c=True)
    if fn:
        fn()

def menu1Callback():
    print 'menu 1 fired'
    startAnimation = cmds.playbackOptions(query=True, minTime=True)
    endAnimation = cmds.playbackOptions(query=True, maxTime=True)
    return startAnimation
    return endAnimation

def menu2Callback():
    print 'menu 2 fired'
    startAnimation = cmds.findKeyframe(which='first')
    endAnimation = cmds.findKeyframe(which='last')
    return startAnimation
    return endAnimation

def initialWindow(self, *args):
    w = cmds.window(w=150, h=100, title = "Export Selection" )
    cmds.columnLayout(adjustableColumn=True)
    form = cmds.formLayout(numberOfDivisions=100)
    exportSelection = cmds.optionMenuGrp(label='example', cc=optionMenuCallback)
    test1 = cmds.menuItem('item1', c = menu1Callback)
    test1 = cmds.menuItem('item2', c= menu2Callback)
    cmds.showWindow(w)


def __call__(self):
    return self.fileExport

1 个答案:

答案 0 :(得分:0)

你必须在optionMenu本身挂钩到changeCommand,而不是菜单项'命令。但是,由于optionMenu回调将传递所选项目,您可以使用它来从菜单项本身获取回调并手动触发:

def optionMenuCallback(*args):
    fn = cmds.menuItem (args[0], q=True, c=True)
    if fn:
        fn()

def menu1Callback():
    cmds.polySphere(n='created_by_item1')
    print 'menu 1 fired'

def menu2Callback():
    cmds.polyCube(n='created_by_item_2')
    print 'menu 2 fired'

w = cmds.window(w=150, h=100)
cmds.columnLayout(adjustableColumn=True)
form = cmds.formLayout(numberOfDivisions=100)
exportSelection = cmds.optionMenuGrp(label='example', cc=optionMenuCallback)
test1 = cmds.menuItem('item1', c = menu1Callback)
test1 = cmds.menuItem('item2', c= menu2Callback)
cmds.showWindow(w)