‘module’ object has no attribute ‘MStatus' in Maya python Api 2020

‘module’ object has no attribute ‘MStatus' in Maya python Api 2020

saeeddameeres
Contributor Contributor
2,003 Views
1 Reply
Message 1 of 2

‘module’ object has no attribute ‘MStatus' in Maya python Api 2020

saeeddameeres
Contributor
Contributor

Hi!

I am trying to build a command in python for Maya following a course on youtube and it's showing this error "# Error: RuntimeError: file line 2: AttributeError: file C:/Users/saeed/OneDrive/Documents/maya/2020/Plugins/vertexParticle.py line 23: 'module' object has no attribute 'MStatus' # "

I checked Maya API documents and we should have "MStatus" class but I have no idea why it's not accepted, I tried to use "MStatusCode" and it's showing same error.

 

that's my script

 

from maya import OpenMaya
from maya import OpenMayaMPx
from maya import OpenMayaFX
import sys

commandName = "vertexParticle"
kHelpFlag = "-h"
kHelpLongFlag = "-help"
kSparseFlag = "-s"
kSparseLongFlag = "-sparse"
helpMessage = "This command will attach a particle for each vertex"

class pluginCommand(OpenMayaMPx.MPxCommand):
    sparse = None
    def __init__(self):
        OpenMayaMPx.MPxCommand.__init__(self)
        
    def argumentParser(self, argList):
        syntax = self.syntax()
        parserArguments = OpenMaya.MArgDatabase(syntax, argList)
        if parserArguments.isFlagSet(kSparseFlag):
            self.sparse = parserArguments.flagArgumentDouble(kSparseFlag, 0)
            return OpenMaya.MStatus.kSuccess
            
        if parserArguments.isFlagSet(kSparseLongFlag):
            self.sparse = parserArguments.flagArgumentDouble(kSparseLongFlag, 0)
            return OpenMaya.MStatus.kSuccess
            
        if parserArguments.isFlagSet(kHelpFlag):
            self.setResult(helpMessage)
            return OpenMaya.MStatus.kSuccess
            
        if parserArguments.isFlagSet(kHelpLongFlag):
            self.setResult(helpMessage)
            return OpenMaya.MStatus.kSuccess
            
    def isUndoable(self):
        return True
        
    def undoIt(self):
        print "undo"
        mFnDagNode = OpenMaya.MFnDagNode(self.mObjParticle)
        mDagMod = OpenMaya.MDagModifier()
        mDagMod.deleteNode(mFnDagNode.parent(0))
        mDagMod.doIt()
        return OpenMaya.MStatus.kSuccess
            
    def redoIt(self):
        mSel = OpenMaya.MSelectionList()
        mDagPath = OpenMaya.MDagPath()
        mFnMesh = OpenMaya.MFnMesh()
        OpenMaya.MGlobal.getActiveSelectionList(mSel)
        if mSel.length() >= 1:
            try:
                mSel.getDagPath(0, mDagPath)
                mFnMesh.setObject(mDagPath) 
                
            except:
                print "please select a poly mesh"
                return OpenMaya.MStatus.kUnknownParameter
                
        else:
            print "please select a poly mesh"
            return OpenMaya.MStatus.kUnknownParameter
            
        mPointArray = OpenMaya.MPointArray()
        mFnMesh.getPoints(mPointArray, OpenMaya.MSpace.kWorld)
        
        mFnParticle = OpenMayaFX.MFnParticleSystem()
        self.mObjParticle = mFnParticle.create()
        
        mFnParticle = OpenMayaFX.MFnParticleSystem(self.mObjParticle)
        
        counter == 0
        for i in xrange(mPointArray.length()):
            if i%self.sparse == 0:
                mFnParticle.emit(mPointArray[i])
                counter += 1
        print "total points :" + str(counter)
        mFnParticle.saveInitialState()
        return OpenMaya.MStatus.kSuccess
        
    def doIt(self, argList):
        self.argumentParser(argList)
        if self.sparse != None:
            self.redoIt()
        return OpenMaya.MStatus.kSuccess
        
def cmdCreator():
    return OpenMayaMPx.asMPxPtr(pluginCommand())
    
def syntaxCreator():
    syntax = OpenMaya.MSyntax()
    
    syntax.addFlag(kHelpFlag, kHelpLongFlag)
    syntax.addFlag(kSparseFlag, kSparseLongFlag, OpenMaya.MSyntax.kDouble)
    
    return syntax
        
    
def initializePlugin(mObject):
    mPlugin = OpenMayaMPx.MFnPlugin(mObject)
    try:
        mPlugin.registerCommand(commandName, cmdCreator, syntaxCreator)
    except:
        sys.stderr.write("Failed to register" + commandName)
        
def uninitializePlugin(mObject):
    mPlugin = OpenMayaMPx.MFnPlugin(pluginCommand())
    try:
        mPlugin.deregisterCommand(commandName)
    except:
        sys.stderr.write("Failed to deregister" + commandName)

 

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

jmreinhart
Advisor
Advisor
Accepted solution

If you go to this page

https://help.autodesk.com/view/MAYAUL/2022/ENU/?guid=Maya_SDK_Maya_Python_API_Maya_Python_API_1_0_Us...

and go to the section labelled 

"Exceptions versus MStatus"

You will find specific info on this issue, but the important bit is:

 

Users writing plug-ins in Python should raise exceptions rather than return MStatus values, unless they want their compute() method to indicate that it is not going to handle a plug. In this case, it should return maya.OpenMaya.kUnknownParameter.

0 Likes