my first command plugin....attempt ¬¬

81 views
Skip to first unread message

Rudi Hammad

unread,
Nov 3, 2015, 12:51:12 PM11/3/15
to Python Programming for Autodesk Maya
Hello again,
I hope it is okey to keep asking. So far every thing I posted was solved so thanks a lot for that
I hope you could help me out wth that too.
I bought Chayan Vinayak Dvd, and it excelent but there are some things that remain unclear. So I am trying to build this command plugin that just builds a joint when the argument jointRh is given
Also, the help flag doesn´t work so...
the command is cmds.myJointCommand(jRh=1)
it register and unregister okey, so can someone try it out?
Thanks



"""--------------------------------------------------------------------------------[REQUISITOS]--------------------------------------------------------------------------------"""

##[MODULOS,OBJETOS Y CLASS]##
# modulos
import maya.OpenMaya as OpenMaya
import maya.OpenMayaMPx as OpenMayaMPx
import sys
# objetos y classes
MPxCommand = OpenMayaMPx.MPxNode # class (que no objeto) de la que heredar

##[VARIABLES]##
kHelpShortFlg
= "-h"
kHelpLongFlg
= "-help"
helpMessage
= "este commando sirve para crear una joint mediante un command plugin"
kMyJointShortFlg
= "-jRh"
kMyJointtLongFlg
= "-jointRh"
#--[nombre del commando]--#
myCommandName
= "myJointCommand"

"""----------------------------------------------------------------------------------[CLASS]----------------------------------------------------------------------------------"""

##[CLASS]##
class MyJointPluginCommand(MPxCommand):
   
   
#--[static Var]--#
    myJointValue
= None
   
   
#--[methods]--#
   
# __init__
   
def __init__(self):
       
super(MyJointPluginCommand,self).__init__()
       
   
# argumentParser
   
def argumentParser(self,argList):
        syntax
= self.syntax()
       
OpenMaya.MArgDatabase(syntax, argList)     #MArgDatabase analiza(parse) command args, flags y flags args
        mArgDatabase
= OpenMaya.MArgDatabase(syntax, argList)
       
       
#--[flags]--]
       
# flag corta help corta/larga
       
if mArgDatabase.isFlagSet(kHelpShortFlg): # isFlagSet() viene heredado de la superclass de MArgDatabase que es MArgParse. Esto es para que queriee que flag le he dado
           
self.setResult(helpMessage) # el mesaje de ayuda como arg
           
return OpenMaya.MStatus.kSuccess
       
if mArgDatabase.isFlagSet(kHelpLongFlg):
           
self.setResult(helpMessage)
           
return OpenMaya.MStatus.kSuccess          
       
# flag corta joint corta/larga
       
if mArgDatabase.isFlagSet(kMyJointShortFlg):
           
self.myJointValue = mArgDatabase.flagArgumentBool(kMyJointShortFlg, 0) # aqui le digo que tipo de argumento espero recibir en la flag, y entr() los args son la flag, y el indice por defecto
           
return OpenMaya.MStatus.kSuccess
       
if mArgDatabase.isFlagSet(kMyJointtLongFlg):
           
self.myJointValue = mArgDatabase.flagArgumentBool(kMyJointtLongFlg, 0)
           
return OpenMaya.MStatus.kSuccess
   
   
       
def isUndoable(self):
           
return true  
   
       
def undoIt(self):
           
print "undo"
            mFnDagNode
= OpenMaya.MfnDagNode # para acceder al transform de lo que quiero borrar
            mDagModifier
= OpenMaya.MDagModifier(mFnDagNode.parent(0)) # es el stack de operacion
            mDagModifier
.deleteNode()
            mDagModifier
.doIt()
           
return OpenMaya.MStatus.kSuccess
       
       
def redoIt():
            mFnDagNode
= OpenMaya.MFnDagNode()
            objJoint
= mFnDagNode.create("joint", "myJoint")
           
return OpenMaya.MStatus.kSuccess    
           
       
def doIt(self,argList):
           
print "yeeeee"
           
self.argumentParser(argList) # como siempre, en OOP para usar un metodo dentro de otro (nesting), uso self para indicar que el metodopertenece a la class
           
if self.myJointValue != None:
               
self.redoIt() # aqui dice es donde va el script donde pilla vertices y pone cubos. redoIt porque se puede hacer varias veces. Y doIt porque solo hace argumentParser una vez
           
else:
               
print " nada que crear "
           
return OpenMaya.MStatus.kSuccess #es importante devolver el estado de lo has hecho
   
   


 
           
"""----------------------------------------------------------------------------------[FUNCIONES]----------------------------------------------------------------------------------"""

##[FUNCIONES]##

# def para atachar pointer
def myCommandCreator():
   
return OpenMayaMPx.asMPxPtr(MyJointPluginCommand())
   
# MSyntax object    
def mySyntaxCreator():
   
# creo objeto mSyntax
    mSyntax
= OpenMaya.MSyntax()
   
# addo flags
    mSyntax
.addFlag(kHelpShortFlg, kHelpLongFlg)
    mSyntax
.addFlag(kMyJointShortFlg, kMyJointShortFlg, OpenMaya.MSyntax.kDouble) #indico tipo de variable que debe recibir  
   
return mSyntax

       
"""------------------------------------------------------------------------------[REGISTRO Y DESREGISTRO]-----------------------------------------------------------------------------"""

#--[initialization command plugin]--#
def initializePlugin(myMObject):
    myPlugin
= OpenMayaMPx.MFnPlugin(myMObject)
   
try:
        myPlugin
.registerCommand( myCommandName, myCommandCreator, mySyntaxCreator ) #attacho pointer
        sys
.stderr.write("\n se ha inicializado correctamente " + myCommandName)
   
except:
        sys
.stderr.write("\n meeeccc!! , fallo de registro de " + myCommandName)
       
#--[uninitialization command plugin]--#
def uninitializePlugin(myMObject):
    myPlugin
= OpenMayaMPx.MFnPlugin(myMObject)
   
try:
        myPlugin
.deregisterCommand(myCommandName) #aqui no requiere attachar pointer, ya esta dentro del core
        sys
.stderr.write("\n se ha desinicializado correctamente " + myCommandName)
   
except:
        sys
.stderr.write("\n meeeccc!! , fallo de desregistro de " + myCommandName)


Marcus Ottosson

unread,
Nov 4, 2015, 3:42:11 AM11/4/15
to python_in...@googlegroups.com
Could you say again what the problem was, the help flag doesn't work? Would it be possible to post a minimal example of the problem?
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/9eef9d5d-099a-4db6-ab06-87eaf0150be7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rudi Hammad

unread,
Nov 4, 2015, 7:17:42 AM11/4/15
to Python Programming for Autodesk Maya
sure, so if I do cmds.myJointCommand(jRh=1) or cmds.myJointCommand(h=1)
I get > # Error: RuntimeError: file <string> line 2: You are not licensed to use the "myJointCommand" command. #
...

Anthony Tan

unread,
Nov 4, 2015, 7:36:09 AM11/4/15
to python_in...@googlegroups.com
Try make your kMyJointShortFlg shorter by one character?
 
Just from the addFlag documentation (and some googling for that error message, admittedly) on MSyntax you need < 4 characters.
 
 
On Wed, Nov 4, 2015, at 11:17 PM, Rudi Hammad wrote:
sure, so if I do cmds.myJointCommand(jRh=1) or cmds.myJointCommand(h=1)
I get > # Error: RuntimeError: file <string> line 2: You are not licensed to use the "myJointCommand" command. #
 
 
 
El miércoles, 4 de noviembre de 2015, 9:42:11 (UTC+1), Marcus Ottosson escribió:
Could you say again what the problem was, the help flag doesn't work? Would it be possible to post a minimal example of the problem?
 
On 3 Nov 2015, at 17:51, Rudi Hammad <rudih...@gmail.com> wrote:
Hello again,
I hope it is okey to keep asking. So far every thing I posted was solved so thanks a lot for that
I hope you could help me out wth that too.
I bought Chayan Vinayak Dvd, and it excelent but there are some things that remain unclear. So I am trying to build this command plugin that just builds a joint when the argument jointRh is given
Also, the help flag doesn´t work so...
the command is cmds.myJointCommand(jRh=1)
it register and unregister okey, so can someone try it out?
Thanks
 
 

"""--------------------------------------------------------------------------------[REQUISITOS]--------------------------------------------------------------------------------"""

##[MODULOS,OBJETOS Y CLASS]##
# modulos
import maya.OpenMayaasOpenMaya
import maya.OpenMayaMPxasOpenMayaMPx
import sys
# objetos y classes
MPxCommand=OpenMayaMPx.MPxNode# class (que no objeto) de la que heredar


##[VARIABLES]##
kHelpShortFlg
="-h"
kHelpLongFlg
="-help"
helpMessage
="este commando sirve para crear una joint mediante un command plugin"
kMyJointShortFlg
="-jRh"
kMyJointtLongFlg
="-jointRh"
#--[nombre del commando]--#
myCommandName
="myJointCommand"

"""----------------------------------------------------------------------------------[CLASS]----------------------------------------------------------------------------------"""

##[CLASS]##
classMyJointPluginCommand(MPxCommand):

#--[static Var]--#
    myJointValue
=None

#--[methods]--#
# __init__
def __init__(self):
super(MyJointPluginCommand,self).__init__()

# argumentParser
def argumentParser(self,argList):
        syntax
=self.syntax()
OpenMaya.MArgDatabase(syntax, argList)#MArgDatabase analiza(parse) command args, flags y flags args
        mArgDatabase
=OpenMaya.MArgDatabase(syntax, argList)


#--[flags]--]
# flag corta help corta/larga
if mArgDatabase.isFlagSet(kHelpShortFlg):# isFlagSet() viene heredado de la superclass de MArgDatabase que es MArgParse. Esto es para que queriee que flag le he dado
self.setResult(helpMessage)# el mesaje de ayuda como arg

returnOpenMaya.MStatus.kSuccess
if mArgDatabase.isFlagSet(kHelpLongFlg):
self.setResult(helpMessage)
returnOpenMaya.MStatus.kSuccess          
# flag corta joint corta/larga
if mArgDatabase.isFlagSet(kMyJointShortFlg):
self.myJointValue = mArgDatabase.flagArgumentBool(kMyJointShortFlg,0)# aqui le digo que tipo de argumento espero recibir en la flag, y entr() los args son la flag, y el indice por defecto

returnOpenMaya.MStatus.kSuccess
if mArgDatabase.isFlagSet(kMyJointtLongFlg):
self.myJointValue = mArgDatabase.flagArgumentBool(kMyJointtLongFlg,0)
returnOpenMaya.MStatus.kSuccess


def isUndoable(self):
returntrue

def undoIt(self):
print"undo"

            mFnDagNode
=OpenMaya.MfnDagNode# para acceder al transform de lo que quiero borrar
            mDagModifier
=OpenMaya.MDagModifier(mFnDagNode.parent(0))# es el stack de operacion

            mDagModifier
.deleteNode()
            mDagModifier
.doIt()
returnOpenMaya.MStatus.kSuccess

def redoIt():
            mFnDagNode
=OpenMaya.MFnDagNode()
            objJoint
= mFnDagNode.create("joint","myJoint")
returnOpenMaya.MStatus.kSuccess    

def doIt(self,argList):
print"yeeeee"
self.argumentParser(argList)# como siempre, en OOP para usar un metodo dentro de otro (nesting), uso self para indicar que el metodopertenece a la class
ifself.myJointValue !=None:
self.redoIt()# aqui dice es donde va el script donde pilla vertices y pone cubos. redoIt porque se puede hacer varias veces. Y doIt porque solo hace argumentParser una vez

else:
print" nada que crear "
returnOpenMaya.MStatus.kSuccess #es importante devolver el estado de lo has hecho







"""----------------------------------------------------------------------------------[FUNCIONES]----------------------------------------------------------------------------------"""

##[FUNCIONES]##

# def para atachar pointer
def myCommandCreator():
returnOpenMayaMPx.asMPxPtr(MyJointPluginCommand())

# MSyntax object    
def mySyntaxCreator():
# creo objeto mSyntax
    mSyntax
=OpenMaya.MSyntax()
# addo flags
    mSyntax
.addFlag(kHelpShortFlg, kHelpLongFlg)

    mSyntax
.addFlag(kMyJointShortFlg, kMyJointShortFlg,OpenMaya.MSyntax.kDouble)#indico tipo de variable que debe recibir  
return mSyntax


"""------------------------------------------------------------------------------[REGISTRO Y DESREGISTRO]-----------------------------------------------------------------------------"""

#--[initialization command plugin]--#
def initializePlugin(myMObject):
    myPlugin
=OpenMayaMPx.MFnPlugin(myMObject)
try:

        myPlugin
.registerCommand( myCommandName, myCommandCreator, mySyntaxCreator )#attacho pointer

        sys
.stderr.write("\n se ha inicializado correctamente "+ myCommandName)
except:
        sys
.stderr.write("\n meeeccc!! , fallo de registro de "+ myCommandName)

#--[uninitialization command plugin]--#
def uninitializePlugin

...


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Justin Israel

unread,
Nov 4, 2015, 12:13:15 PM11/4/15
to python_in...@googlegroups.com

Is there a reason you are aliasing MPxNode to MPxCommand? When you use MPxNode, you have to register an id with Maya. If you make your plugin actually subclass from the real MPxCommand, does it work?


Rudi Hammad

unread,
Nov 4, 2015, 2:00:35 PM11/4/15
to Python Programming for Autodesk Maya
upss...sorry, you are right, MPxNode was a mistake
so I read all your feedback and did that...but still not working. Man that´s frustrating. Same error


"""--------------------------------------------------------------------------------[REQUISITOS]--------------------------------------------------------------------------------"""

##[MODULOS,OBJETOS Y CLASS]##
# modulos
import maya.OpenMaya as OpenMaya
import maya.OpenMayaMPx as OpenMayaMPx
import sys
# objetos y classes
MPxCommand = OpenMayaMPx.MPxCommand # class (que no objeto) de la que heredar


##[VARIABLES]##
kHelpShortFlg
= "-h"

kHelpLongFlg
= "-hlp"

helpMessage
= "este commando sirve para crear una joint mediante un command plugin"
kMyJointShortFlg
= "-j"

kMyJointtLongFlg
= "-jnt"

#--[nombre del commando]--#
myCommandName
= "myJointCommand"

"""----------------------------------------------------------------------------------[CLASS]----------------------------------------------------------------------------------"""

##[CLASS]##
class MyJointPluginCommand(MPxCommand):
   
   
#--[static Var]--#
    myJointValue
= None
   
   
#--[methods]--#
   
# __init__
   
def __init__(self):
       
super(MyJointPluginCommand,self).__init__()
       
   
# argumentParser
   
def argumentParser(self,argList):

        syntax
= self.syntax()
       
OpenMaya.MArgDatabase(syntax, argList)     #MArgDatabase analiza(parse) command args, flags y flags args
        mArgDatabase
= OpenMaya.MArgDatabase(syntax, argList)

       
       
#--[flags]--]
       
# flag corta help corta/larga
       
if mArgDatabase.isFlagSet(kHelpShortFlg): # isFlagSet() viene heredado de la superclass de MArgDatabase que es MArgParse. Esto es para que queriee que flag le he dado

           
self.setResult(helpMessage) # el mesaje de ayuda como arg
           
return OpenMaya.MStatus.kSuccess
       
if mArgDatabase.isFlagSet(kHelpLongFlg):
           
self.setResult(helpMessage)
           
return OpenMaya.MStatus.
kSuccess          
       
# flag corta joint corta/larga
       
if mArgDatabase.isFlagSet(kMyJointShortFlg):
           
self.myJointValue = mArgDatabase.flagArgumentBool(kMyJointShortFlg, 0) # aqui le digo que tipo de argumento espero recibir en la flag, y entr() los args son la flag, y el indice por defecto

           
return OpenMaya.MStatus.kSuccess
       
if mArgDatabase.isFlagSet(kMyJointtLongFlg):
           
self.myJointValue = mArgDatabase.flagArgumentBool(kMyJointtLongFlg, 0)
           
return OpenMaya.MStatus.kSuccess
   
   
       
def isUndoable(self):
           
return true
 
   
       
def undoIt(self):
           
print "undo"
            mFnDagNode
= OpenMaya.MfnDagNode # para acceder al transform de lo que quiero borrar
            mDagModifier
= OpenMaya.MDagModifier(mFnDagNode.parent(0)) # es el stack de operacion
            mDagModifier
.deleteNode()
            mDagModifier
.doIt()

           
return OpenMaya.MStatus.kSuccess
       
       
def redoIt():
            mFnDagNode
= OpenMaya.MFnDagNode()

            objJoint
= mFnDagNode.create("joint", "myJoint")

           
return OpenMaya.MStatus.kSuccess    
           
       
def doIt(self,argList):
           
print "yeeeee"
           
self.argumentParser(argList) # como siempre, en OOP para usar un metodo dentro de otro (nesting), uso self para indicar que el metodopertenece a la class
           
if self.myJointValue != None:

               
self.redoIt() # aqui dice es donde va el script donde pilla vertices y pone cubos. redoIt porque se puede hacer varias veces. Y doIt porque solo hace argumentParser una vez

           
else:
               
print " nada que crear "

           
return OpenMaya.MStatus.kSuccess #es importante devolver el estado de lo has hecho

   
   


 
           
"""----------------------------------------------------------------------------------[FUNCIONES]----------------------------------------------------------------------------------"""

##[FUNCIONES]##

# def para atachar pointer
def myCommandCreator():
   
return OpenMayaMPx.asMPxPtr(MyJointPluginCommand())
   
# MSyntax object    
def mySyntaxCreator():
   
# creo objeto mSyntax
    mSyntax
= OpenMaya.MSyntax()
   
# addo flags
    mSyntax
.addFlag(kHelpShortFlg, kHelpLongFlg)

    mSyntax
.addFlag(kMyJointShortFlg, kMyJointShortFlg, OpenMaya.MSyntax.kDouble) #indico tipo de variable que debe recibir  
   
return mSyntax

       
"""------------------------------------------------------------------------------[REGISTRO Y DESREGISTRO]-----------------------------------------------------------------------------"""

#--[initialization command plugin]--#
def initializePlugin(myMObject):
    myPlugin
= OpenMayaMPx.MFnPlugin(myMObject)
   
try:

        myPlugin
.registerCommand( myCommandName, myCommandCreator, mySyntaxCreator ) #attacho pointer

        sys
.stderr.write("\n se ha inicializado correctamente " + myCommandName)
   
except:
        sys
.stderr.write("\n meeeccc!! , fallo de registro de " + myCommandName)
       
#--[uninitialization command plugin]--#
def uninitializePlugin(myMObject):
    myPlugin
= OpenMayaMPx.MFnPlugin(myMObject)
   
try:
        myPlugin
.deregisterCommand(myCommandName) #aqui no requiere attachar pointer, ya esta dentro del core

        sys
.stderr.write("\n se ha desinicializado correctamente " + myCommandName)
   
except:
        sys
.stderr.write("\n meeeccc!! , fallo de desregistro de " + myCommandName)
...

Rudi Hammad

unread,
Nov 5, 2015, 1:49:14 PM11/5/15
to Python Programming for Autodesk Maya
anyone? nop???
I am still stuck on this.
By the way, is it okey to use cmds inside a command plugin? in my mind, maya API and maya cmds are things that should not be mixed.
...

Justin Israel

unread,
Nov 5, 2015, 5:42:49 PM11/5/15
to Python Programming for Autodesk Maya
You had a bunch of problems in this code. I cleaned it all up, fixed the problems, and put it in a pastebin:

In the future, can you use some kind of code paste? Either pastebin, or gist, or something to make sure indentation is preserved?

Anyways, various things I fixed:
  • Bad indentation had a number of the plugin methods indented into the arg parsing method
  • Missing "self" in one of the methods
  • Returning true instead of True
  • Using MStatus instead of just returning, or raising exceptions
This works for me, when I run it with the help flag.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Rudi Hammad

unread,
Nov 5, 2015, 6:37:24 PM11/5/15
to Python Programming for Autodesk Maya
thank you very much. Of course I will paste it better next time.
My head was hurting. Thanks Justin
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Justin Israel

unread,
Nov 5, 2015, 8:30:58 PM11/5/15
to Python Programming for Autodesk Maya
Not a problem!

To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/c6454f77-974a-486c-8937-5c0a2e3e4309%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages