0

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.

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)
        
  • 1
    Are you sure that there is a MStatus in the python api? As much as I know it was remove a long time ago. – haggi krey Aug 22 '22 at 08:47
  • Hey, that was my bad, I just found that I was reading the class for C++ but it was removed for python. Do you have an idea what should I return instead of "OpenMaya.MStatus.kSuccess/kUnknownParameter"? Thanks a lot – SaeedDomeer Aug 23 '22 at 02:34
  • Sorry, no idea what's the replacement for it. But since you do not usethe result of the argumentParser anyway there is not need to return anything and the example python commands do not return anything in the doIt, redoIt functions. – haggi krey Aug 23 '22 at 09:15

1 Answers1

0

Because MStatus.MStatusCode is an enum, you can return its integer value in Python. The only issue is that because in C++ this is an enum, there is no guarantee that the values won't change/shift between releases. This enum has remained consistent since 2019, so there isn't much risk of this happening for MStatus.MStatusCode.

https://help.autodesk.com/view/MAYAUL/2019/ENU/?guid=__cpp_ref_class_m_status_html https://help.autodesk.com/view/MAYAUL/2023/ENU/?guid=Maya_SDK_cpp_ref_class_m_status_html

Somewhere in the top of your file simply add the constants you intend to use:

MStatus_kSuccess = 0
MStatus_kFailure = 1
MStatus_kUnknownParameter = 5

Then return the constant instead in your functions:

if parserArguments.isFlagSet(kHelpFlag):
    self.setResult(helpMessage)
    return MStatus_kSuccess
Klaudikus
  • 382
  • 3
  • 12