Maya (2023) crashes when tuple supplied as flag to command in python api 2

Hi,

I found this documentation for supplying a tuple as a flag to a custom command in api 1 and I’m tried convert it to api 2 but it crashes when I call the command. Here is my code

import sys
import maya.api.OpenMaya as om


def maya_useNewAPI():
    """
    The presence of this function tells Maya that the plugin produces, and
    expects to be passed, objects created using the Maya Python API 2.0.
    """
    pass


kShortFlagName = '-tf'
kLongFlagName = '-myTupleFlag'


class MyCommandWithFlagClass(om.MPxCommand):
    kPluginCmdName = 'myCommandWithFlag'

    def __init__(self):
        super(MyCommandWithFlagClass, self).__init__()

    def doIt(self, args):
        self.parseArguments(args)

    def parseArguments(self, args):
        argData = om.MArgParser(self.syntax(), args)

        if argData.isFlagSet(kShortFlagName):
            flagParam0 = argData.flagArgumentInt(kShortFlagName, 0)
            flagParam1 = argData.flagArgumentInt(kShortFlagName, 1)
            flagParam2 = argData.flagArgumentInt(kShortFlagName, 2)

            print(kLongFlagName + '[0]: ' + str(flagParam0))
            print(kLongFlagName + '[1]: ' + str(flagParam1))
            print(kLongFlagName + '[2]: ' + str(flagParam2))

    @staticmethod
    def cmdCreator():
        return MyCommandWithFlagClass()

    @staticmethod
    def syntaxCreator():
        syntax = om.MSyntax()

        syntax.addFlag(kShortFlagName, kLongFlagName, om.MSyntax.kDouble, om.MSyntax.kDouble, om.MSyntax.kDouble)

        return syntax


# Initialize the plug-in
def initializePlugin(plugin):
    pluginFn = om.MFnPlugin(plugin)
    try:
        pluginFn.registerCommand(
            MyCommandWithFlagClass.kPluginCmdName, MyCommandWithFlagClass.cmdCreator, MyCommandWithFlagClass.syntaxCreator
        )
    except:
        sys.stderr.write(
            "Failed to register command: %s\n" % MyCommandWithFlagClass.kPluginCmdName
        )


# Uninitialize the plug-in
def uninitializePlugin(plugin):
    pluginFn = om.MFnPlugin(plugin)
    try:
        pluginFn.deregisterCommand(MyCommandWithFlagClass.kPluginCmdName)
    except:
        sys.stderr.write(
            "Failed to unregister command: %s\n" % MyCommandWithFlagClass.kPluginCmdName
        )

I took line 46 (and the rest of the contents of the syntaxCreator method) directly from the documentation so I’m not sure why it’s crashing. Any help would be greatly appreciated!

Thanks,
-Harry

Try the latest documentation for Maya Python API 2:
Command plug-ins: Flags and arguments.

Thanks but that doesn’t cover how to pass a tupple as an arg or flag like the api 1 documentation that I linked does.

Thanks,
-Harry

1 Like

I don’t have a chance to test this in Maya right now. I may be wrong, but perhaps you should pass the tuple as… a tuple:

syntax.addFlag(kShortFlagName, kLongFlagName, (om.MSyntax.kDouble, om.MSyntax.kDouble, om.MSyntax.kDouble))

upd:
Yes, that is right:

help "myCommandWithFlag";
// Result: 
// 
// Synopsis: myCommandWithFlag [flags]
// Flags:
//   -tf -myTupleFlag  Float Float Float
// 
// 
// Command Type: Command

myCommandWithFlag -myTupleFlag 12.0 0.1 3.2;
-myTupleFlag[0]: 12
-myTupleFlag[1]: 0
-myTupleFlag[2]: 3

myCommandWithFlag -tf 5.0 3 .1;
-myTupleFlag[0]: 5
-myTupleFlag[1]: 3
-myTupleFlag[2]: 0

Sorry for the late reply, I’m in the process of moving!

Thanks for the solution, it’s obvious now but I didn’t expect the definition to change between api 1 and 2.

Thanks again,
-Harry