Maya Python:cmds.button(),UI传递变量并调用函数? [英] Maya Python: cmds.button( ) with UI passing variables and calling a function?

查看:187
本文介绍了Maya Python:cmds.button(),UI传递变量并调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,这似乎是学习更多编程知识的好地方. 我已经编写了一个Maya python脚本,两个函数都可以在其中运行,但是我无法让UI按钮调用superExtrude()函数. 第一个功能执行几何网格物体操作,第二个功能应为用户输入生成用户界面:

First of all, this seems to be a great place to learn more about programming. I've written a maya python script, where both functions work, I'm having trouble getting the UI button to call the superExtrude() function, though. The first function does the geometric mesh manipulations and the second one should produce the UI for the user inputs:

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脚本,我还是一个新手,因此,我们将不胜感激. :)

I'm pretty new to python and maya scripting, so any help would be greatly appreciated. :)

推荐答案

我不确定这是否是您想要的答案,但您不知道有关Maya命令"标志的知识:

Im not sure if this is the answer you want but what you have to know about maya "commands" flags :

  • 如果要在按钮调用中放置函数,则需要传入不带任何参数的函数名(例如:command = myFunction)(去掉结尾的括号()")

  • If you want to put a function in a button call, you need to pass in the function name without any arguments (eg : command = myFunction) (get rid of the ending brackets "()" )

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

in your function, you need to add a "*args" because the maya button always passes an argument (I think it's "False") (eg : def myFunction(customArg1, customArg2, *args) )

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

If you want to pass arguments in the button signal, you need to use the partial function from the functools module (from functools import partial) and use it like this: cmds.button( command = partial(myFunction, arg1, arg2, kwarg1=value1, kwarg2=value2) )

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

One more thing, about pymel and cmds... it's probably a never ending story but pymel is not almighty... When you have to deal with a lot of informations (like getting a vertices list on a mesh), pymel can be something like 40x slower than a simple maya commands. It has its pros and its cons... If you've just started with python, I wouldn't recommend to get into pymel right now. Get familiar with the syntax and the commands, and when you're ok, switch to pymel (which is very useful when you are dealing with objects creation)

希望这对您有所帮助, 干杯

Hope this helped, Cheers

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

Based on your first post, what you need to change in your code to make it work is :

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()

这篇关于Maya Python:cmds.button(),UI传递变量并调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆