Maya 2020 Python 2.0 API - MPxContextCommand : Help with user defined parameters

Maya 2020 Python 2.0 API - MPxContextCommand : Help with user defined parameters

ejcarroll007
Explorer Explorer
979 Views
2 Replies
Message 1 of 3

Maya 2020 Python 2.0 API - MPxContextCommand : Help with user defined parameters

ejcarroll007
Explorer
Explorer

Good afternoon all,

 

I'm using the new python Context/ContextCommand API that were recently added and I'm not sure if I'm appending a new "-length" paramater/syntax to the MPxContextCommand properly.

 

This is a snippet of the MPxContextCommand...

 

class MoveBrushCommand(omuiapi2.MPxContextCommand):
    COMMAND_NAME = "MoveBrushCommand"
    LENGTH_FLAG = "-l"
    LENGTH_FLAG_LONG = "-length"
    
    MOVEBRUSHCONTEXT = None
    SYNTAX = omapi2.MSyntax()
    
    def __init__(self):
        omuiapi2.MPxContextCommand.__init__(self)
        
    @classmethod
    def creator(cls):
        return cls()
        
    def makeObj(self):
        MoveBrushCommand.MOVEBRUSHCONTEXT = MoveBrushContext()
        return MoveBrushCommand.MOVEBRUSHCONTEXT
        
    def doEditFlags(self):
        pass
        
    def doQueryFlags(self):
        argData = omapi2.MArgParser(MoveBrushCommand.SYNTAX, omapi2.MArgList())
        
        if (argData.isFlagSet(MoveBrushCommand.LENGTH_FLAG)):            
            length = argData.flagArgumentDouble(MoveBrushCommand.LENGTH_FLAG, 0)
            MoveBrushCommand.MOVEBRUSHCONTEXT.test_query_data()
        
    def appendSyntax(self):
        MoveBrushCommand.SYNTAX.addFlag(MoveBrushCommand.LENGTH_FLAG, MoveBrushCommand.LENGTH_FLAG_LONG, omapi2.MSyntax.kDouble)

 

 

If I try to queue my context and the instance with...

 

MoveBrushCommand -q -length MoveBrushCommand14857705;

 

 

I get this error...

 

// Error: line 1: Invalid flag '-length' //

 

 

 

I'm assuming the appendSyntax method is where you add user-defined arguments, is it not?

 

Can anyone help point me in the right direction? 😀

0 Likes
980 Views
2 Replies
Replies (2)
Message 2 of 3

RFlannery1
Collaborator
Collaborator

I don't have an answer; just some speculation that I hope helps steer you in the right direction.

 

I'm wondering about the line "SYNTAX = omapi2.MSyntax()". In C++, command flags aren't added to a default-constructed MSyntax object. Instead they are added to an object constructed using the "syntax()" function. This function is a member of the MPxContextCommand class. The docs say "USE _syntax() IN SCRIPT".

 

Here's where it gets a little tricky. In the Python API 2.0 docs, I don't see any mention of a function named "_syntax" in the MPxContextCommand class. In the best case scenario, the function is just missing from the documentation but it works just fine. In the worst case scenario, the function was not implemented so you will have to switch to Python API version 1 or C++.

 

I don't have Maya in front of me, so I can't test anything. But I would try running this code and seeing what happens:

    def appendSyntax(self):
        print(dir(self))
        syntaxObj = self._syntax()
        syntaxObj.addFlag(MoveBrushCommand.LENGTH_FLAG, MoveBrushCommand.LENGTH_FLAG_LONG, omapi2.MSyntax.kDouble)

Also, while troubleshooting this, I would recommend leaving the call to "self._syntax()" inside the "appendSyntax" function.  If you get it working, then you can try moving back into a class variable as "MoveBrushCommand._syntax()".  But I suspect it's not a static function, so that probably won't work.

0 Likes
Message 3 of 3

ejcarroll007
Explorer
Explorer

Hi rflannery,

 

As you pointed out with how the MSyntax works in C++, I also saw yesterday that the Python API didn't have an equivalent method unfortunately. Which I did attempt before to see it wasn't documented, but with no luck. (Seems the python documentation itself was copy'pasta haha.

appendSyntax() -> None

This method should be overridden to append syntax
to the context command.  The syntax object can be
obtained by calling the syntax method.
The following flags cannot be used as user-defined
flags as they are reserved for edit and query:
'-e', '-edit', '-q', '-query'.

 

 

But I tried again with the self.syntax() & your suggestion with self._syntax() and no dice.

 

 

 

The reason I made it a class variable was because I'm running into a similar issue with MArgParser. Where the C++ MPxContextCommand has a parser() method for returning it ready for you, but their is no equivalent Python method either.

 

Like you said, I should probably just switch to C++, but I really want to get the Python version working!!!! 🙂

 

The hunt continues...

 

Thanks,

 

-Eric

0 Likes