maya python api add plugin

Here we have a very simple plugin that adds two values together. I use it mainly as a plugin 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