How to make custom context in Maya API 2.0 receive flag/argument?

How to make custom context in Maya API 2.0 receive flag/argument?

atxadaaldo17022
Community Visitor Community Visitor
709 Views
0 Replies
Message 1 of 1

How to make custom context in Maya API 2.0 receive flag/argument?

atxadaaldo17022
Community Visitor
Community Visitor

Hello!,

 

Currently, I’m working on creating custom context using Maya API 2.0

 

I already searched from documentation and any tutorial I could find, but I don’t know how to get context command to receive flag/argument. The only closest reference I can get online is here helixTool.cpp. But it’s C++, and using Maya API 1.0 so I had a problem translating the code.

 

Below I already attach my progress so far, any help would be greatly appreciated!

Thanks,

-Aldo

# flag definition
kMeshFlag = "-m"
kMeshLongFlag = "-mesh"

# tell maya plugin produces and need to be passed, maya api 2.0
def maya_useNewAPI():
    pass

class CutomTool(omui.MPxToolCommand):
    mesh = None

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

    def syntaxCreator(self):
        syntax = om.MSyntax()

        # (short name, long name, argument data type)
        syntax.addFlag(kMeshFlag,kMeshLongFlag,om.MSyntax.kString)

        return syntax
    
    def argumentParser(self, argList):

        syntax = self.syntax()
        parsedArguments = om.MArgParser(syntax, argList)

        if parsedArguments.isFlagSet(kMeshFlag):
            self.mesh = parsedArguments.flagArgumentString(kMeshFlag, 0)
            return om.MStatus.kSuccess
        if parsedArguments.isFlagSet(kMeshLongFlag):
            self.mesh = parsedArguments.flagArgumentString(kMeshLongFlag, 0)
            return om.MStatus.kSuccess

    def doIt(self, argList):
        self.argumentParser(argList)

        if self.mesh != None:
            self.redoIt()
        return om.MStatus.kSuccess

    def isUndoable(self):
        return True

    # action to undo operation
    def undoIt(self):
        print ("undo")
        return om.MStatus.kSuccess

    def redoIt(self):
        print ("redo")
        return om.MStatus.kSuccess

    def finalize(self):
        command = om.MArgList()
        command.addArg(om.MString(kMeshFlag))
        command.addArg(self.mesh)
        return omui.MPxToolCommand.doFinalize(command)

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

class CustomContext(omui.MPxContext):
    TITLE = "Title"
    HELP_TEXT = "Help Text"
    
    def __init__(self):
        super(CustomContext, self).__init__()
        self.setTitleString(CustomContext.TITLE)  

    def helpStateHasChanged(self, event):
        self.setHelpString(CustomContext.HELP_TEXT)
        
    def toolOnSetup(self,event):
        om.MGlobal.selectCommand(om.MSelectionList())  # select nothing, to record undo
        self.reset_context()
        
    def toolOffCleanup(self):
        self.reset_context()

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

    def completeAction(self):
        print ("complete action")
        
    def deleteAction(self):
        print ("undo")
        
    def abortAction(self):
        print ("abort")

    def reset_context(self):
        print ("reset")
   
class CustomContextCmd(omui.MPxContextCommand):
    
    COMMAND_NAME = "customCommand"   # used as mel command to create context
    
    def __init__(self):
        super(CustomContextCmd, self).__init__()
        
    # required for maya to get instance of context
    def makeObj(self):
        return CustomContext()    # return ribbon spline ctx 
        
    @classmethod
    def creator(cls):
        return CustomContextCmd() # return ribbon spline ctx cmd   

def initializePlugin(plugin):
    
    plugin_fn = om.MFnPlugin(plugin)
    
    try:
        plugin_fn.registerContextCommand(CustomContextCmd.COMMAND_NAME,
                                         CustomContextCmd.creator)
    except:
        om.MGlobal.displayError("Failed to register context command: %s"
                                 %CustomContextCmd.COMMAND_NAME)

    
def uninitializePlugin(plugin):
    plugin_fn = om.MFnPlugin(plugin)
    
    try:
        plugin_fn.deregisterContextCommand(CustomContextCmd.COMMAND_NAME)

    except:
        om.MGlobal.displayError("Failed to deregister context command: %s"
                                 %CustomContextCmd.COMMAND_NAME)
Reply

 

0 Likes
710 Views
0 Replies
Replies (0)