I was wondering if anyone could point me in the correct direction for setting up a node similar to the extrude function in that exists in maya.
Currently I’m using an MPxNode, which I set the output to a custom attribute on the selected object. This works correctly (the custom node shows up in the channel box input area of the object selected) however, if I run this on a shape object it does not show up in the channel box under inputs even though the connections are set up the same way in the hypergraph.
I may be going in a completely wrong direction for something similar to the built-in extrude- if so please let me know!
import maya.OpenMaya as OpenMaya
import maya.OpenMayaMPx as OpenMayaMPx
import maya.cmds as cmds
import math, sys, string, random
#TEMP use for debugging plugin in case of failure
def randomWord():
string.letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
wordLength = random.randint(4,8);
word = ''
for i in range(0,wordLength):
word+=random.choice(string.letters)
return str(word)
kNodeTypeName = randomWord()
kCmdName = "nodeTest"
kNodeId = OpenMaya.MTypeId((random.randint(224288,524288)))
#End TEMP
# Command to create node, attach to object selected
class BasicCommandTest(OpenMayaMPx.MPxCommand):
def __init__(self):
OpenMayaMPx.MPxCommand.__init__(self)
def doIt(self, args):
obj = getNode() #Selected
#Add the input attribute for the object you have selected
nAttr = OpenMaya.MFnNumericAttribute()
myattr = nAttr.create( "customAttr", "in", OpenMaya.MFnNumericData.kFloat,1.0);
nAttr.setWritable(True)
nAttr.setStorable(True)
nAttr.setReadable(True)
nAttr.setKeyable(True)
obj.addAttribute(myattr)
#MObject()
customAttrOutputAttr = obj.attribute(obj.attributeCount()-1) #Just added this, it is last
#MFnDependencyNode()
cmds.createNode(kNodeTypeName)#This is the new node that is created, which will do everything for me.
myNode = getNode();
cmds.connectAttr(str(myNode.name())+'.output',str(obj.name())+'.customAttr')
def getNode():
selList = OpenMaya.MSelectionList()
OpenMaya.MGlobal.getActiveSelectionList(selList)
parentObject = OpenMaya.MObject ()
if selList.length():
selList.getDependNode(0,parentObject)
node = OpenMaya.MFnDependencyNode(parentObject)
return (node)
##Custom Node
class rCustomUtilNode(OpenMayaMPx.MPxNode):
aOutput = OpenMaya.MObject()
acustomAttr = OpenMaya.MObject()
# class variables
def __init__(self):
OpenMayaMPx.MPxNode.__init__(self)
def compute(self, plug, dataBlock):
print "Computing"
def nodeInitializer():
nAttr = OpenMaya.MFnNumericAttribute()
cAttr = OpenMaya.MFnCompoundAttribute()
#Set up for compute
rCustomUtilNode.aOutput = nAttr.create('output', 'out', OpenMaya.MFnNumericData.kFloat)
rCustomUtilNode.addAttribute(rCustomUtilNode.aOutput)
#Create input for customAttr
rCustomUtilNode.acustomAttr = nAttr.create("customAttr", "in", OpenMaya.MFnNumericData.kFloat, 0.0)
nAttr.setWritable(True)
nAttr.setStorable(True)
nAttr.setReadable(True)
nAttr.setKeyable(True)
rCustomUtilNode.addAttribute( rCustomUtilNode.acustomAttr )
rCustomUtilNode.attributeAffects(rCustomUtilNode.acustomAttr, rCustomUtilNode.aOutput)
def cmdCreator():
return OpenMayaMPx.asMPxPtr(BasicCommandTest())
def cmdSyntaxCreator():
return OpenMaya.MSyntax()
def nodeCreator():
return OpenMayaMPx.asMPxPtr( rCustomUtilNode() )
# initialize the script plug-in
def initializePlugin(mobject):
mplugin = OpenMayaMPx.MFnPlugin(mobject, "Author", "1.0", "Any")
try:
mplugin.registerNode( kNodeTypeName, kNodeId, nodeCreator, nodeInitializer, OpenMayaMPx.MPxNode.kDependNode)
except:
sys.stderr.write( "Failed to register node: %s" % kNodeTypeName )
raise
try:
mplugin.registerCommand(kCmdName, cmdCreator, cmdSyntaxCreator)
except:
sys.stderr.write("Failed to register command: %s" % kCmdName)
raise
# uninitialize the script plug-in
def uninitializePlugin(mobject):
mplugin = OpenMayaMPx.MFnPlugin(mobject)
try:
mplugin.deregisterCommand(kCmdName)
except:
sys.stderr.write("Failed to deregister command: %s" % kCmdName)
raise
try:
mplugin.deregisterNode( kNodeId )
except:
sys.stderr.write( "Failed to deregister node: %s" % kNodeTypeName )
raise