[SOLVED] in Python in Maya, partial forces the first argrument to be 'False' instead of its default value

Issue

In Autodesk Maya I am using the functools partial function for when my UI needs to trigger functions. But when the function has a default value for its first argument, partial always sends a False (or 0) value, overriding the default. No arguments besides the first are affected.

Why does this occur, and how do I get around it? Here’s some example code to clarify it better:

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

The result I get back is:

The first number should be 5, and it is False The second number should
be 6 and it is 6

Solution

The problem is that the callback of the mc.button() function always receives some args from the button command and the first argument is replaced. You can solve it by using

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

as function call. This catches all normal arguments and does not touch the keyword agruments.

Answered By – haggi krey

Answer Checked By – David Marino (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.