best way to return a failure condition from a python command?

best way to return a failure condition from a python command?

Anonymous
Not applicable
1,137 Views
1 Reply
Message 1 of 2

best way to return a failure condition from a python command?

Anonymous
Not applicable

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

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

0 Likes
Accepted solutions (1)
1,138 Views
1 Reply
Reply (1)
Message 2 of 2

cheng_xi_li
Autodesk Support
Autodesk Support
Accepted solution

Hi,

 

I checked the wrapper and I am afraid you could only throw an exception to return kFailure.

 

I talked with engineers and the remain in command buffer is a defect. I've logged an improvement and defect in our system on your behalf.

 

Yours,

Li

 

 

0 Likes