Python api 2.0 - Cannot initialize plugin with MPxToolCommand

Python api 2.0 - Cannot initialize plugin with MPxToolCommand

tim.brooksUR6UJ
Explorer Explorer
1,972 Views
8 Replies
Message 1 of 9

Python api 2.0 - Cannot initialize plugin with MPxToolCommand

tim.brooksUR6UJ
Explorer
Explorer

Hi everyone. I was wondering if anyone else has managed to successfully initialize a custom context plugin that uses MPxToolCommand in python api 2? Maybe I'm doing something wrong.

 

In the registerContextCommand function, I've passed the arguments in this order:

context command name string

context command creator function

tool command name string

tool command creator function

tool command syntax function

 

If i remove the tool command arguments, it initializes. It also fails if I remove just the syntax function. If i change it over to API 1, it initializes.

 

Any thoughts? Thanks.

 

BTW, I've pasted the code here. It's just a test plugin with no functionality:

import maya.api.OpenMaya as om
import maya.api.OpenMayaUI as omui

import maya.cmds as cmds

def maya_useNewAPI():
    pass

class TestingContext(omui.MPxContext):

    TITLE = "TEST TOOL"

    def __init__(self):
        super(TestingContext, self).__init__()
    
    def helpStateHasChanged(self, event):
        self.setHelpString("Help test")

    def toolOnSetup(self, event):
        print("TOOL SET UP")

    def doPress(self, event, draw_manager, frame_context):
        print("PRESSED")

    def doDrag(self, event, draw_manager, frame_context):
        print("---")

    def doRelease(self, event, draw_manager, frame_context):
        print("RELEASED")

class TestingContextCmd(omui.MPxContextCommand):

    COMMAND_NAME = "testingCtx"

    def __init__(self):
        super(TestingContextCmd, self).__init__()
    
    def makeObj(self):
        return TestingContext()
    
    @classmethod
    def creator(cls):
        return TestingContextCmd()

class TestingContextToolCmd(omui.MPxToolCommand):

    COMMAND_NAME = "testingToolCmd"

    def __init__(self):
        super(TestingContextToolCmd, self).__init__()
        self.commandString = self.COMMAND_NAME

        self.undoable = False
    
    def doIt(self, arg_list):
        pass

    def undoIt(self):
        pass

    def redoIt(self):
        pass

    def isUndoable(self):
        return self.undoable
    
    def doFinalize(self):
        pass

    @classmethod
    def creator(cls):
        return TestingContextToolCmd()

    @classmethod
    def create_syntax(cls):
        syntax = om.MSyntax()
        
        return syntax


def initializePlugin(plugin):
    vendor = "me"
    version = "1.0"

    plugin_fn = om.MFnPlugin(plugin, vendor, version)
    try:
        plugin_fn.registerContextCommand(TestingContextCmd.COMMAND_NAME, TestingContextCmd.creator,
                                        TestingContextToolCmd.COMMAND_NAME, TestingContextToolCmd.creator,
                                        TestingContextToolCmd.create_syntax)
    except:
        om.MGlobal.displayError("Failed to register context command: {0}".format(TestingContextCmd.COMMAND_NAME))

def uninitializePlugin(plugin):
    plugin_fn = om.MFnPlugin(plugin)

    try:
        plugin_fn.deregisterContextCommand(TestingContextCmd.COMMAND_NAME, TestingContextToolCmd.COMMAND_NAME)
    except:
        om.MGlobal.displayError("Failed to deregister context command: {0}".format(TestingContextCmd.COMMAND_NAME))
0 Likes
1,973 Views
8 Replies
Replies (8)
Message 2 of 9

jmreinhart
Advisor
Advisor

 

def initializePlugin(plugin):
    vendor = "me"
    version = "1.0"

    plugin_fn = om.MFnPlugin(plugin, vendor, version)
    plugin_fn.registerContextCommand(TestingContextCmd.COMMAND_NAME, TestingContextCmd.creator)

 

because of C++ and Python differences, the API can't usually accept multiple sets of valid inputs for the same function.

So you should stick to registering the context command like this instead, and register the toolCommand and the context seperately.

0 Likes
Message 3 of 9

tim.brooksUR6UJ
Explorer
Explorer

Thanks for the response but I don't believe that's the solution. To be able to use a the newToolCommand function in MPxContext, it has to be registered with the context command. If you register the toolCommand separately it seems to be just a regular command. It also registers fine with the contextCommand in Python API 1

0 Likes
Message 4 of 9

jmreinhart
Advisor
Advisor

I looked into this a bit more and I think the errors we get when trying to use the two sets of arguments might help point towards the source of the problem.

 

Thanks for the response but I don't believe that's the solution. To be able to use a the newToolCommand function in MPxContext, it has to be registered with the context command. If you register the toolCommand separately it seems to be just a regular command. It also registers fine with the contextCommand in Python API 1

 

In API 1.0 you get this if you use invalid arguments:

 

# plugin_fn.registerContextCommand()
# TypeError: Wrong number or type of arguments for overloaded function 'MFnPlugin_registerContextCommand'.
# Possible C/C++ prototypes are:
# MFnPlugin::registerContextCommand(MString const &,MCreatorFunction)
# MFnPlugin::registerContextCommand(MString const &,MCreatorFunction,MString const &,MCreatorFunction,MCreateSyntaxFunction) //

 

 

In API 2.0 you get this:

# TestingContextToolCmd.COMMAND_NAME, TestingContextToolCmd.creator)
# TypeError: function takes exactly 2 arguments (5 given) //

# Traceback (most recent call last):

 

It looks like in API 1.0 the inputs for the functions are arg* kwargs* or some kind of wrapper set up that allows the function to take any set of inputs and then within the function call it checks the type and number to see if they are valid. That's what allows it to accept both sets of arguments. So the function is being called by python but fails internally (if you use the wrong arguments).

 

In API 2.0 you get the standard error that python throws when the arguments for a function call are invalid. So the function seems to be failing at a python level, and never even gets called. Since it seems to be a standard python function implementation then overloading (allowing multiple valid sets of arguments isn't possible). It's like trying to do this:

def testFunc(a):
    print '{}'.format(a)
    
def testFunc(a,b):
    print('{} and {}'.format(a,b))
    
testFunc('X')

 

 The call fails because only the second declaration of the function is valid, so you need to provide two arguments.

 

I think this is is likely just an issue with this function in particular, but I'm going to look at other Python API 2.0 commands that accept multiple sets of arguments to see.

 

 

 

 

 

0 Likes
Message 5 of 9

tim.brooksUR6UJ
Explorer
Explorer

Thanks for looking more into this. I wonder if this is an oversight on Autodesk's part for API 2, or if it was done for a reason? But I haven't been able to find anything on how to work around this, or even basically anything at all on MPxToolCommand with API 2.

0 Likes
Message 6 of 9

jmreinhart
Advisor
Advisor

Yeah, I think the most likely situation is that it was not fully ported over and that's why the documentation and functionality is quite spotty. I actually can't find it in any python API doc or any of the "What's new in 201X API pages" so my guess is that Autodesk don't consider it fully implemented enough to be used yet.

 

This bit below is more for people who may come across this post later:

When it comes to the Python API (for plugin stuff) I would recommend using the API 1.0 because it matches the features of the C++ API more closely than the API 2.0. If you're just writing a short individual function then API 2.0 is a good idea bc it's more python-like. But you don't want to find yourself in a situation where you've written lots of code for a plugin in 2.0 and then need a class/function that is only in 1.0 which forces you to switch everything over.

 

 

0 Likes
Message 7 of 9

tim.brooksUR6UJ
Explorer
Explorer
Yeah, I think I'll be rewriting my plugin to API 1. Oh well, it'd make porting it to C++ easier
0 Likes
Message 8 of 9

tim.brooksUR6UJ
Explorer
Explorer

Aaaaand I just remembered you can't create custom contexts with API 1 after the removal of the Legacy Viewport *faceplam*

0 Likes
Message 9 of 9

jmreinhart
Advisor
Advisor

C++ time! Woohoo!

0 Likes