openMaya api nodes

anyone got examples of working with nodes using the API from python, just simple stuff like creating a polyExtrudeEdge node and hooking it up to a mesh.

This autodesk example gives you the basics.

Unless you’re doing this in a plugin where you need to use API code – or doing something that’s super perf critical – it’s rarely worth the hassle of doing this in the API when the cmds/pymel route is viable. API code is a lot more work for the same results most of the time.

it’s needed from the API, since it would be executed from a custom context i am making, on it’s doRelease method.

Though im still trying to weigh the benefits of using the node or just modifying the mesh via MfnMesh since i will be using this in a retopo type tool.

Thanks for the link.

little off topic, but i need to figure this out for the same tool, what is the best method for getting vertex ids, from a dag path and component mobject returned from a selection list? Sofar all i can think of is using a MItMeshVert and it’s index() method, though that dosnt seem very efficient, i need this so i can work with the data in a MfnMesh function set.

edit:
MFnSingleIndexedComponent seems to be the best way i found sofar

Can’t you use maya.cmds in a custom context? Why do you need to connect an extrude using the API and not by calling polyExtrude?

Can’t really help with what you need, but here is some api code for creating a node.

Here is a simple math node I created a while back. It’s handy just to use as a template.



import sys
import maya.OpenMaya as OpenMaya
import maya.OpenMayaMPx as OpenMayaMPx
##set vars
kAddUtilNodeTypeName = "Add"
kAddUtilNodeClassify = "utility/general"
kAddUtilNodeId = OpenMaya.MTypeId(0x87327)

# define a new matrixUtilNode class derived from the MPxNode class
class rtAddUtilNode(OpenMayaMPx.MPxNode):

	# class variables
	input = OpenMaya.MObject()
	input1 = OpenMaya.MObject()
	input2 = OpenMaya.MObject()
	output = OpenMaya.MObject()

	def __init__(self):
		OpenMayaMPx.MPxNode.__init__(self)

	# arguments ( self, MPlug, MDataBlock)
	def compute(self, plug, dataBlock):

		# if these attributes are requested, recompute their values
		if  plug == rtAddUtilNode.output:

			# get MDataHandle's to attributes
			#
			try:
                            input_dataHandle1 = dataBlock.inputValue( rtAddUtilNode.input1 )
			except:
			    sys.stderr.write( "Failed to get MDataHandle inputValue input1" )
			    raise
                        try:
                            input_dataHandle2 = dataBlock.inputValue( rtAddUtilNode.input2 )
			except:
			    sys.stderr.write( "Failed to get MDataHandle inputValue input2" )
			    raise
			
			try:
			    output_dataHandle = dataBlock.outputValue( rtAddUtilNode.output )
			except:
			    sys.stderr.write( "Failed to get MDataHandle outputValue output" )
			    raise

			# get values from dataHandle
			#
			input_value1 = input_dataHandle1.asFloat()
                        input_value2 = input_dataHandle2.asFloat()
                        result = input_value1 + input_value2
			# set the output
			output_dataHandle.setFloat(result)

			# set the plug clean so maya knows it can update
			dataBlock.setClean(plug)

		else:
			return OpenMaya.kUnknownParameter

		return OpenMaya.MStatus.kSuccess

def nodeCreator():

	return OpenMayaMPx.asMPxPtr( rtAddUtilNode() )

# create and initialize the attributes to the node
def nodeInitializer():

	nAttr = OpenMaya.MFnNumericAttribute()

	# create input attributes
	#
	rtAddUtilNode.input1 = nAttr.create("input1", "i1", OpenMaya.MFnNumericData.kFloat, 0.0)
	nAttr.setWritable(True)
	nAttr.setStorable(True)
	nAttr.setReadable(True)
	nAttr.setKeyable(True)
	
	rtAddUtilNode.input2 = nAttr.create("input2", "i2", OpenMaya.MFnNumericData.kFloat, 0.0)
	nAttr.setWritable(True)
	nAttr.setStorable(True)
	nAttr.setReadable(True)
	nAttr.setKeyable(True)


	rtAddUtilNode.output = nAttr.create("output", "o", OpenMaya.MFnNumericData.kFloat, 0.0)
	nAttr.setWritable(True)
	nAttr.setStorable(True)
	nAttr.setReadable(True)
	
	# add attribues
	#
	rtAddUtilNode.addAttribute( rtAddUtilNode.input1)
	rtAddUtilNode.addAttribute( rtAddUtilNode.input2)
	rtAddUtilNode.addAttribute( rtAddUtilNode.output)

	# Setup which attributes affect each other
	rtAddUtilNode.attributeAffects ( rtAddUtilNode.input1, rtAddUtilNode.output )
        rtAddUtilNode.attributeAffects ( rtAddUtilNode.input2, rtAddUtilNode.output )
# initialize the script plug-in
def initializePlugin(mobject):
	mplugin = OpenMayaMPx.MFnPlugin(mobject, "Tim Callaway", "1.0", "Any")
	try:
		mplugin.registerNode( kAddUtilNodeTypeName, kAddUtilNodeId, nodeCreator, nodeInitializer, OpenMayaMPx.MPxNode.kDependNode, kAddUtilNodeClassify)
	except:
		sys.stderr.write( "Failed to register node: %s" % kAddUtilNodeTypeName )
		raise

# uninitialize the script plug-in
def uninitializePlugin(mobject):
	mplugin = OpenMayaMPx.MFnPlugin(mobject)
	try:
		mplugin.deregisterNode( kAddUtilNodeId )
	except:
		sys.stderr.write( "Failed to deregister node: %s" % kAddUtilNodeTypeName )
		raise