Maya Python:cmds.button(),UI传递变量并调用函数?

时间:2014-07-07 17:55:04

标签: python maya pymel

首先,这似乎是一个了解编程的好地方。 我编写了一个maya python脚本,其中两个函数都有效,但我无法通过UI按钮来调用superExtrude()函数。 第一个函数执行几何网格操作,第二个函数应该为用户输入生成UI:

import maya.cmds as cmds

def superExtrude(extrScale, extrDist):
    """Loops through a list of selected meshes and extrudes all of the mesh faces to produce a polygon frame, based on existing mesh tesselations"""
    myObjectLt = cmds.ls(selection=True)

    for i in range(len(myObjectLt)):
        numFaces = cmds.polyEvaluate(face=True)
        item = myObjectLt[i] + ".f[:]"
        cmds.select(clear=True)
        cmds.select(item, replace=True)

        #extrude by scale
        cmds.polyExtrudeFacet(constructionHistory=True, keepFacesTogether=False, localScaleX=extrScale, localScaleY=extrScale, localScaleZ=extrScale)
        selFaces = cmds.ls(selection=True)
        cmds.delete(selFaces)

        #extrude by height
        cmds.select(item, replace=True)
        cmds.polyExtrudeFacet(constructionHistory=True, keepFacesTogether=True, localTranslateZ=extrDist)

def extrWindow():
    """Creates the user interface UI for the user input of the extrusion scale and height"""
    windowID = "superExtrWindow"

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

    cmds.window(windowID, title="SuperExtrude", sizeable=False, resizeToFitChildren=True)
    cmds.rowColumnLayout(numberOfColumns=2, columnWidth=[(1,120),(2,120)], columnOffset=[1,"right",3])

    cmds.text(label="Extrusion Scale:")
    extrScaleVal = cmds.floatField(text=0.9)
    cmds.text(label="Extrusion Height:")
    extrDistVal = cmds.floatField(text=-0.3)
    cmds.separator(height=10, style="none")
    cmds.separator(height=10, style="none")
    cmds.separator(height=10, style="none")

    cmds.button(label="Apply", command=superExtrude(extrScaleVal, extrDistVal))
    cmds.showWindow()

extrWindow()

我对python和maya脚本很新,所以任何帮助都会非常感激。 :)

4 个答案:

答案 0 :(得分:6)

cmds.button(label="Apply", command=superExtrude(extrScaleVal, extrDistVal))

此行调用superExtrude并将其返回值分配给command。由于superExtrude没有返回任何内容,因此该按钮实际上具有None的通信。

也许你打算在单击按钮时调用superExtrude,在这种情况下你应该将它包装在lambda中以防止它被立即调用:

cmds.button(label="Apply", command=lambda *args: superExtrude(extrScaleVal, extrDistVal))

答案 1 :(得分:6)

我不确定这是否是你想要的答案,但你必须了解maya"命令"旗帜:

  • 如果你想把一个函数放在一个按钮调用中,你需要传入没有任何参数的函数名(例如:command = myFunction)(去除结束括号"()& #34;)

  • 在你的函数中,你需要添加一个" * args"因为maya按钮总是传递一个参数(我认为它" False")(例如:def myFunction(customArg1,customArg2,* args))

  • 如果要在按钮信号中传递参数,则需要使用functools模块中的partial函数(来自functools import partial)并使用它如下: cmds.button(command = partial(myFunction,arg1,arg2,kwarg1 = value1,kwarg2 = value2))

还有一件事,关于pymel和cmds ......它可能是一个永无止境的故事,但是pymel并不是万能的...当你必须处理很多信息时(比如获取一个顶点列表)网格),pymel可能比简单的maya命令慢40倍。 它有它的优点和缺点......如果你刚刚开始使用python,我现在不建议你进入pymel。 熟悉语法和命令,当你没问题时,切换到pymel(这在处理对象创建时非常有用)

希望这有帮助, 干杯

编辑:

根据您的第一篇文章,您需要更改代码才能使其正常工作:

import maya.cmds as cmds
from functools import partial

#You need to add the *args at the end of your function
def superExtrude(extrScaleField, extrDistField, *args):
    """Loops through a list of selected meshes and extrudes all of the mesh faces to produce a polygon frame, based on existing mesh tesselations"""
    myObjectLt = cmds.ls(selection=True)


    #In the function, we are passing the floatFields, not their values.
    #So if we want to query the value before running the script, we need to
    #use the floatField cmds with the "query" flag


    extrScale = cmds.floatField(extrScaleField, q=1, v=1)
    extrDist = cmds.floatField(extrDistField, q=1, v=1)

    for i in range(len(myObjectLt)):
        numFaces = cmds.polyEvaluate(face=True)
        item = myObjectLt[i] + ".f[:]"
        cmds.select(clear=True)
        cmds.select(item, replace=True)

        #extrude by scale
        cmds.polyExtrudeFacet(constructionHistory=True, keepFacesTogether=False, localScaleX=extrScale, localScaleY=extrScale, localScaleZ=extrScale)
        selFaces = cmds.ls(selection=True)
        cmds.delete(selFaces)

        #extrude by height
        cmds.select(item, replace=True)
        cmds.polyExtrudeFacet(constructionHistory=True, keepFacesTogether=True, localTranslateZ=extrDist)

def extrWindow():
    """Creates the user interface UI for the user input of the extrusion scale and height"""
    windowID = "superExtrWindow"

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

    cmds.window(windowID, title="SuperExtrude", sizeable=False, resizeToFitChildren=True)
    cmds.rowColumnLayout(numberOfColumns=2, columnWidth=[(1,120),(2,120)], columnOffset=[1,"right",3])

    cmds.text(label="Extrusion Scale:")

    # There were an error here, replace 'text' with 'value'
    # to give your floatField a default value on its creation

    extrScaleVal = cmds.floatField(value=0.9)
    cmds.text(label="Extrusion Height:")
    extrDistVal = cmds.floatField(value=-0.3)
    cmds.separator(height=10, style="none")
    cmds.separator(height=10, style="none")
    cmds.separator(height=10, style="none")

    # As said above, use the partial function to pass your arguments in the function
    # Here, the arguments are the floatFields names, so we can then query their value
    # everytime we will press the button.

    cmds.button(label="Apply", command=partial(superExtrude,extrScaleVal, extrDistVal))
    cmds.showWindow(windowID)

extrWindow()

答案 2 :(得分:1)

St4rb0y

首先,您的floatField调用(第33,35行)使用的是无效标记,' text'。您可能意味着使用' value',因此请更改这两行。

extrScaleVal = cmds.floatField(v=0.9)
extrDistVal = cmds.floatField(v=-0.3)

其次,在构建UI控件类型时,'命令' flag寻找一个字符串,所以你必须将命令和它的参数包装在引号中:

cmds.button(label="Apply", command='superExtrude(extrScaleVal, extrDistVal)')

更改这三行,它应该都可以正常工作。

提示:

要评论单行代码,请使用#而不是将整行包含在三个单引号中。使用三引号对于评论多行代码更为方便。

控制命令标志的另一个提示:您可以定义一个字符串变量来传递命令,并直接使用变量而不是字符串。在构建动态控件时,这个技巧会派上用场,即根据用户选择组装命令:

comStr = "superExtrude(extrScaleVal, extrDistVal)"
cmds.button(label="Apply", command=comStr)

答案 3 :(得分:-2)

所以我把所有东西都改成了pymel,这是你应该学习的东西.cmds是垃圾。 花点时间看看你和我之间的差异。像这样的脚本可以帮助您入门。如果需要进一步解释,请告诉我。

这样做有利于学习pymel

pymel online docs = http://download.autodesk.com/global/docs/maya2014/en_us/PyMel/

import pymel.core as pm
def superExtrude(*args):
    """Loops through a list of selected meshes and extrudes all of the mesh faces to produce a polygon frame, based on existing mesh tesselations"""
    #pymel uses python classes to make things easier
    #its ok to not understand what a class is but just think of it the same as if you were to add an attribute to a polycube. 
    #your code variable now has attributes 

    #so with that pymel ls returns a list of PyNodes that correspond to the objects
    #cmds ls returns a list of strings which is very unuseful
    #if you look at the help docs you can find most of whats available
    myObjectLt = pm.ls(selection=True)


    for i in myObjectLt:
        #instead of cycling through by a number were gonna cycle through the list itself
        #i is now the item in the list

        #its unnecessary to select the objects because we can specify it in the polyExtrude
        #cmds.select(item,  replace=True)

        #the extrude commands selects things but im not sure what your trying to achive here by seperating
        #the scale extrude and translate extrude
        pm.select(cl=True)



        #the way poly objects wrok is that you have a transform node and a shape node
        # if you graph it in the hypershade you'll see the two nodes
        #the faces are part of the shape node i like accessing things by this node but just know you can do it like this
        #i.f  <-- f is your attribute and i is the item
        #using i.getShape() returns the shape node

        # http://download.autodesk.com/global/docs/maya2014/en_us/PyMel/generated/classes/pymel.core.uitypes/pymel.core.uitypes.FloatField.html?highlight=floatfield#pymel.core.uitypes.FloatField
        #since were using pymel the extrScaleVal has function that lets you get the value
        thisScale = extrScaleVal.getValue()


        pm.polyExtrudeFacet(i.getShape().f, constructionHistory=True, keepFacesTogether=False, localScaleX=thisScale, localScaleY=thisScale, localScaleZ=thisScale)
        #selFaces = cmds.ls(selection=True)
        pm.delete()

        #same as before
        thisDist = extrDistVal.getValue()
        #extrude by height
        pm.polyExtrudeFacet(i.getShape().f, constructionHistory=True, keepFacesTogether=True, localTranslateZ=thisDist)

def extrWindow():
    #global is a way to transfer variables from function to function the way you had it
    # you would have had to query the value from your parameters in superExtrude
    #instead do this
    global extrScaleVal, extrDistVal
    #which will makes these parameters to the other function


    """Creates the user interface UI for the user input of the extrusion scale and height"""
    windowID = "superExtrWindow"

    #instead of having a query run just use try except
    #which will just go to except when the try fails
    try:
        pm.deleteUI(windowID)
    except:
        pass

    pm.window(windowID, title="SuperExtrude", sizeable=False, resizeToFitChildren=True)
    pm.rowColumnLayout(numberOfColumns=2, columnWidth=[(1,120),(2,120)], columnOffset=[1,"right",3])

    pm.text(label="Extrusion Scale:")
    extrScaleVal = pm.floatField(v=0.9)
    pm.text(label="Extrusion Height:")
    extrDistVal = pm.floatField(v=-0.3)
    pm.separator(height=10, style="none")
    pm.separator(height=10, style="none")
    pm.separator(height=10, style="none")

    pm.button(label="Apply", c=superExtrude)
    pm.showWindow()

extrWindow()