- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
So, let's say I have a maya python command like so:
import maya.cmds as commands
import maya.api.OpenMaya as open_maya
class ExampleCmd(open_maya.MPxCommand):
PLUGIN_COMMAND_NAME = 'examplecmd'
def __init__(self):
open_maya.MPxCommand.__init__(self)
# noinspection PyMethodMayBeStatic
def doIt(self, args):
# the command has failed and I'd like to let the caller know the command
# has failed.
pass
@staticmethod
def cmd_creator():
return ExampleCmd()
def maya_useNewAPI():
"""
Function necessary to identify we're using the API 2.0 interfaces (in Maya)
"""
pass
def initializePlugin(maya_object):
"""
Initialize the plugin + register the appropriate commands
"""
maya_plugin = open_maya.MFnPlugin(maya_object)
try:
maya_plugin.registerCommand(ExampleCmd.PLUGIN_COMMAND_NAME,
ExampleCmd.cmd_creator)
except RuntimeError as error:
print('Failed to register command: ' +
ExampleCmd.PLUGIN_COMMAND_NAME + '\n' +
error.message + '\n')
raise
def uninitializePlugin(maya_object):
"""
And to unload + de-register the plugin/commands
"""
maya_plugin = open_maya.MFnPlugin(maya_object)
try:
maya_plugin.deregisterCommand(ExampleCmd.PLUGIN_COMMAND_NAME)
except RuntimeError as error:
print('Failed to de-register command: ' +
ExampleCmd.PLUGIN_COMMAND_NAME + '\n' +
error.message)
raiseWhat is the _correct_ way to do the equivalent (in the C++ API) of returning a MStatus::kFailure code?
I ask as I'm creating a overall command hierarchy that I'll invoke elsewhere (in C#) and I'd like for the caller (again, in C#) to know that the command failed.
Just raising a RuntimeException feels wrong, as it looks like that's more for "you've messed up the command, here's another chance for you to get the arguments right" ...
For example, if I modify the above code to raise an exception:
def doIt(self, args):
# the command has failed and I'd like to let the caller know the command
# has failed.
raise RuntimeError('You screwed up the command')This results in the attached screenshot. The command sitting in the active command 'buffer' doesn't feel right.
If that's the expected way to do things, that's cool (not it's not) and I'll come up with another way to indicate if a command has succeeded or failed.
Solved! Go to Solution.