在Maya的Python中,部分强制'False'第一个参数而不是其默认值



在Autodesk Maya中,当UI需要触发函数时,我使用functools分部函数。但是,当函数的第一个参数有默认值时,partial总是发送一个False(或0(值,覆盖默认值。除了第一个论点外,没有任何论点受到影响。

为什么会发生这种情况,我该如何应对?这里有一些示例代码可以更好地说明它:

import maya.cmds as mc
from functools import partial
def MyFunction(X=5,Y=6):
print("The first number should be 5, and it is "+str(X) +"nThe second number should be 6 and it is "+str(Y))

def PartialProblemUI():
##remove the window if it exists
if mc.window("PartialProblem_Window", ex=True): mc.deleteUI("PartialProblem_Window")
##start of the window UI
mc.window("PartialProblem_Window", title="Partial Problem",w=120,h=65, tlb=True,s=False)   
form=mc.formLayout()   
c=mc.button("UI_Btn_PartialProblem",l="Partial Problem",c=partial(MyFunction),w=150,h=40)
mc.formLayout(form,e=True,attachForm=[(c,"left",21),((c,"top",5))])

##show the window
mc.showWindow("PartialProblem_Window")    

####force the window to be a certain size
mc.window("PartialProblem_Window", e=True ,w=200,h=115)     
PartialProblemUI()

我得到的结果是:

第一个数字应该是5,它是False第二个数字应该是6,它是6

问题是mc.button()函数的回调总是从按钮命令接收一些参数,并且第一个参数被替换。你可以使用来解决它

def MyFunction(*args, X=5, Y=6):

作为函数调用。这会捕获所有正常的参数,并且不会触及关键字agruments。

相关内容

最新更新