Search code examples
pythonmaya

Maya's nameCommand not able to call functions


I'm having an issue with calling functions from nameCommand's in Maya used with hotkeys. I can't tell if it's a Maya or Python issue.

The following MEL works as expected

proc myTest() {
    print("test");
}

nameCommand -ann "test" -command "myTest()" testCommand;
hotkey -k "l" -name "testCommand";

However, translated to Python, I get an error

import maya.cmds as cmds

def myPythonTest():
    print("myPythonTest")

cmds.nameCommand("pythonTestCommand", ann="pythonTest", command="myPythonTest()", sourceType="python")
cmds.hotkey(k="l", name="pythonTestCommand")

// Error: line 1: Cannot find procedure "myPythonTest".

Is it the wrong way to call functions in Python, or is something else going on? I've noticed that the parentheses are being stripped, and calling the function with myPythonTest() from the script editor works as expected.


Solution

  • cmds.nameCommand("pythonTestCommand", ann="pythonTest", command='python("myPythonTest()")', sourceType="python")
    cmds.hotkey(k="l", name="pythonTestCommand")
    

    Should work