Maya 的 nameCommand 无法调用函数

Maya's nameCommand not able to call functions

我在使用热键从 Maya 中的 nameCommand 调用函数时遇到问题。我不知道这是 Maya 还是 Python 问题。

以下 MEL 按预期工作

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

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

但是,翻译成 Python,我得到一个错误

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".

是在Python中调用函数的方式不对,还是其他原因?我注意到括号被删除了,并且从脚本编辑器中使用 myPythonTest() 调用函数按预期工作。

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

应该可以

为了扩展之前的答案,我认为 在评估中是正确的 该 sourceType 似乎已损坏。

如果您希望能够将 python 代码传递给 nameCommand,您需要 首先创建一个 runTimeCommand

def testy():
    print('hello')

# There is no way to edit a runtime command so we need to check if it
# exists and then remove it if it does.
my_command_name = 'my_runtime_command'
if cmds.runTimeCommand(my_command_name, q=True, exists=True):
    cmds.runTimeCommand(my_command_name, e=True, delete=True)

cmds.runTimeCommand(
    my_command_name, 
    ann='My Command', 
    category='User', 
    command='testy()', 
    commandLanguage='python'
    )
cmds.nameCommand('my_name_command', ann='My Command', command=my_command_name)
cmds.hotkey(k='1', name='my_name_command')

如您所见,您不需要提供 sourceType,nameCommand 只是 runtimecommand 的字符串表示,将只执行 给出 runTimeCommand。所以真正指定执行语言的地方是在 runTimeCommand.

commandLanguage 标志中