PDA

View Full Version : python node try


aguirre
12-15-2010, 11:52 PM
hi!

im currently trying to learn some python api, and got stucked with this one.
it should eval the area of a given face.
inputs are: mesh, faceID
output: faceArea

i hope somebody could help me out
thanks


import sys
import maya.OpenMaya as om
import maya.OpenMayaMPx as omMPx

kPluginNodeTypeName = "spFaceAreaNode"
faceAreaNodeId = om.MTypeId(0x8701)

# Node definition
class faceAreaNode(omMPx.MPxNode):

# class variables
attInMesh = om.MObject()
attInFace = om.MObject()
attOutArea = om.MObject()

def __init__(self):

omMPx.MPxNode.__init__(self)

def compute(self, plug, dataBlock):

if (plug == faceAreaNode.attOutArea):
# read inputMesh
inMesh = dataBlock.inputValue(faceAreaNode.attInMesh).asMesh()
# read inputFaceIndex
faceIndex = dataBlock.inputValue(faceAreaNode.attInFace).asLong()

# create mesh iterator and set its index
dagPath = om.MDagPath()
polyIter = om.MItMeshPolygon(dagPath)
if inMesh != om.MObject.kNullObj:
polyIter.reset(inMesh)

defaultIndex = 0
polyIter.setIndex(faceIndex, defaultIndex)

# reveive information
util = om.MScriptUtil()
util.createFromFloat(0.0)
ptr = util.asFloatPtr()
polyIter.getArea(ptr, om.MSpace.kWorld)
area = om.MScriptUtil(ptr).asFloat()

outputHandle = dataBlock.outputValue(attOutArea)
outputHandle.setFloat(area)
dataBlock.setClean(plug)


# creator
def nodeCreator():
return omMPx.asMPxPtr(faceAreaNode())


# initializer
def nodeInitializer():
# input mesh
inMesh = om.MFnTypedAttribute();
faceAreaNode.attInMesh = inMesh.create("inMesh", "im", om.MFnData.kMesh)
inMesh.setStorable(True)
inMesh.setKeyable(False)
inMesh.setReadable(True)
inMesh.setWritable(True)
inMesh.setCached(False)
# intput faceIndex
inFace = om.MFnNumericAttribute();
faceAreaNode.attInFace = inFace.create("faceIndex", "f", om.MFnNumericData.kLong, 0)
inFace.setStorable(True)
inFace.setKeyable(True)
inFace.setReadable(True)
inFace.setWritable(True)
inFace.setMin(0)
# output faceArea
outAttr = om.MFnNumericAttribute();
faceAreaNode.attOutArea = outAttr.create("faceArea", "fa", om.MFnNumericData.kFloat, 0.0)
outAttr.setStorable(False)
outAttr.setKeyable(False)
outAttr.setReadable(True)
outAttr.setWritable(False)
# add attributes
faceAreaNode.addAttribute(faceAreaNode.attInMesh)
faceAreaNode.addAttribute(faceAreaNode.attInFace)
faceAreaNode.addAttribute(faceAreaNode.attOutArea)
# set dependencies
faceAreaNode.attributeAffects(faceAreaNode.attInMesh, faceAreaNode.attOutArea)
faceAreaNode.attributeAffects(faceAreaNode.attInFace, faceAreaNode.attOutArea)


# initialize the script plug-in
def initializePlugin(mobject):
mplugin = omMPx.MFnPlugin(mobject)
try:
mplugin.registerNode(kPluginNodeTypeName, faceAreaNodeId, nodeCreator, nodeInitializer)
except:
sys.stderr.write("Failed to register node: {0}".format(kPluginNodeTypeName))
raise


# uninitialize the script plug-in
def uninitializePlugin(mobject):
mplugin = omMPx.MFnPlugin(mobject)
try:
mplugin.deregisterNode(faceAreaNodeId)
except:
sys.stderr.write("Failed to register node: {0}".format(kPluginNodeTypeName))
raise

CGTalk Moderation
12-15-2010, 11:52 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.