Python API 2.0. Command Plug-in .setResult() always returns a list to caller
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Windows 10, Maya 2019, Python
I'm writing a Python API 2.0 command plugin. When "returning" a single item via MPxCommand.setResult(), whether its a string, int, etc, it ends up being returned as a list to the caller. Strangely, this is not the case when using API 1 however. In troubleshooting this, I wrote the same plugin twice as simple as possible, one using API 1, and the other using API 2. Again, API 1 returns just a string as intended. API 2 returns a 1 item string list - not wanted.
API 1
import maya.OpenMayaMPx as OpenMayaMPx kPluginCmdName = "cmd_api1" class scriptedCommand(OpenMayaMPx.MPxCommand): def __init__(self): OpenMayaMPx.MPxCommand.__init__(self) def doIt(self, args): OpenMayaMPx.MPxCommand.setResult('Behold, a string from Maya Python API 1') def cmdCreator(): return OpenMayaMPx.asMPxPtr(scriptedCommand()) def initializePlugin(mobject): mplugin = OpenMayaMPx.MFnPlugin(mobject) mplugin.registerCommand(kPluginCmdName, cmdCreator) def uninitializePlugin(mobject): mplugin = OpenMayaMPx.MFnPlugin(mobject) mplugin.deregisterCommand(kPluginCmdName)
API 2 (Same as 1 as much as possible)
import maya.api.OpenMaya as OpenMaya from maya.api.OpenMaya import MPxCommand kPluginCmdName = "cmd_api2" def maya_useNewAPI(): pass class scriptedCommand(MPxCommand): def __init__(self): MPxCommand.__init__(self) def doIt(self, args): MPxCommand.setResult('Behold, a string from Maya Python API 2') def cmdCreator(): return scriptedCommand() def initializePlugin(mobject): mplugin = OpenMaya.MFnPlugin(mobject) mplugin.registerCommand(kPluginCmdName, cmdCreator) def uninitializePlugin(mobject): mplugin = OpenMaya.MFnPlugin(mobject) mplugin.deregisterCommand(kPluginCmdName)
After loading those 2 plug-ins, and running this from a Python tab in the script editor:
import maya.cmds as cm api_1_result = cm.cmd_api1() print('api_1_result: %s' % api_1_result) api_2_result = cm.cmd_api2() print('api_2_result: %s' % api_2_result)
I get:
api_1_result: Behold, a string from Maya Python API 1
api_2_result: [u'Behold, a string from Maya Python API 2.0']
And I get the same issue when making the same calls in MEL.
I've looked at the docs and scoured the web, but haven't gotten to the bottom of this yet. Anyone have any ideas?
-Paul