How to set attribute value in Maya Python API (Custom Node)


#1

I’m very new to Maya Python API, I’m trying to create custom Node in Maya Python API, which have two attributes one is Input1 second is Input2 and last attribute is Output. If I’ll enter Input1 and Input2 then output reflect in Output.

Example:
Input1 + Input2 = Output
2 + 2 = 4

My code

import sys
import maya.OpenMaya as OpenMaya
import maya.OpenMayaMPx as OpenMayaMPx

nodeName = ‘Sum’
nodeId = OpenMaya.MTypeId(0x123fff)

class Sum(OpenMayaMPx.MPxNode):
input1 = OpenMaya.MObject()
input2 = OpenMaya.MObject()
output = OpenMaya.MObject()

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

def compute(self):
    pass

def nodeCreator():
return OpenMayaMPx.asMPxPtr(Sum())

def nodeInitializer():
attr = OpenMaya.MFnNumericAttribute()

# ----------------------------
Sum.input1 = attr.create("input1", "in1", OpenMaya.MFnNumericData.kFloat, 0.0)
attr.setReadable(1)
attr.setWritable(1)
attr.setStorable(1)
attr.setKeyable(1)
# ----------------------------
Sum.input2 = attr.create("input2", "in2", OpenMaya.MFnNumericData.kFloat, 0.0)
attr.setReadable(1)
attr.setWritable(1)
attr.setStorable(1)
attr.setKeyable(1)
# ----------------------------
Sum.output = attr.create("output", "out", OpenMaya.MFnNumericData.kFloat, 0.0)
attr.setReadable(1)
attr.setWritable(1)
attr.setStorable(1)
attr.setKeyable(1)

# ----------------------------
Sum.addAttribute(Sum.input1)
Sum.addAttribute(Sum.input2)
Sum.addAttribute(Sum.output)

def initializePlugin(mobject):
mplugin = OpenMayaMPx.MFnPlugin(mobject, ‘Ajay Kumar Verma’)
try:
mplugin.registerNode(nodeName, nodeId, nodeCreator, nodeInitializer)
except:
sys.stderr.write(“Failed to register command: %s\n” % nodeName)

def uninitializePlugin(mobject):
mplugin = OpenMayaMPx.MFnPlugin(mobject)
try:
mplugin.deregisterNode(nodeId)
except:
sys.stderr.write(“Failed to unregister command: %s\n” % nodeName)

Please help me. Thank you.