geomShader.py
import sys
import maya.OpenMaya as OpenMaya
import maya.OpenMayaMPx as OpenMayaMPx
kPluginNodeName = "spGeomShader"
kPluginNodeClassify = "utility/color"
kPluginNodeId = OpenMaya.MTypeId(0x8700E)
class geomShader(OpenMayaMPx.MPxNode):
def __init__(self):
OpenMayaMPx.MPxNode.__init__(self)
aOutColor = OpenMaya.MObject()
aPoint = OpenMaya.MObject()
aScale = OpenMaya.MObject()
aOffset = OpenMaya.MObject()
def compute(self, plug, block):
if plug == geomShader.aOutColor or plug.parent() == geomShader.aOutColor:
resultColor = OpenMaya.MFloatVector(0.0,0.0,0.0)
point = block.inputValue( geomShader.aPoint ).asFloatVector()
scale = block.inputValue( geomShader.aScale ).asFloatVector()
offset = block.inputValue( geomShader.aOffset ).asFloatVector()
resultColor.x = point.x * scale.x + offset.x
resultColor.y = point.y * scale.y + offset.y
resultColor.x = point.z * scale.z + offset.z
outColorHandle = block.outputValue( geomShader.aOutColor )
outColorHandle.setMFloatVector(resultColor)
outColorHandle.setClean()
else:
return OpenMaya.kUnknownParameter
def nodeCreator():
return OpenMayaMPx.asMPxPtr( geomShader() )
def nodeInitializer():
nAttr = OpenMaya.MFnNumericAttribute()
try:
geomShader.aPoint = nAttr.createPoint("pointObj", "p")
nAttr.setStorable(0)
nAttr.setHidden(1)
geomShader.aScale = nAttr.createPoint("scale", "s")
nAttr.setKeyable(1)
nAttr.setStorable(1)
nAttr.setReadable(1)
nAttr.setWritable(1)
nAttr.setDefault(1.0, 1.0, 1.0)
geomShader.aOffset = nAttr.createPoint("offset", "o")
nAttr.setKeyable(1)
nAttr.setStorable(1)
nAttr.setReadable(1)
nAttr.setWritable(1)
geomShader.aOutColor = nAttr.createColor("outColor", "oc")
nAttr.setStorable(0)
nAttr.setHidden(0)
nAttr.setReadable(1)
nAttr.setWritable(0)
except:
sys.stderr.write("Failed to create attributes\n")
raise
try:
geomShader.addAttribute(geomShader.aPoint)
geomShader.addAttribute(geomShader.aScale)
geomShader.addAttribute(geomShader.aOffset)
geomShader.addAttribute(geomShader.aOutColor)
except:
sys.stderr.write("Failed to add attributes\n")
raise
try:
geomShader.attributeAffects (geomShader.aPoint, geomShader.aOutColor)
geomShader.attributeAffects (geomShader.aScale, geomShader.aOutColor)
geomShader.attributeAffects (geomShader.aOffset, geomShader.aOutColor)
except:
sys.stderr.write("Failed in setting attributeAffects\n")
raise
def initializePlugin(mobject):
mplugin = OpenMayaMPx.MFnPlugin(mobject)
try:
mplugin.registerNode( kPluginNodeName, kPluginNodeId, nodeCreator,
nodeInitializer, OpenMayaMPx.MPxNode.kDependNode, kPluginNodeClassify )
except:
sys.stderr.write( "Failed to register node: %s" % kPluginNodeName )
raise
def uninitializePlugin(mobject):
mplugin = OpenMayaMPx.MFnPlugin(mobject)
try:
mplugin.deregisterNode( kPluginNodeId )
except:
sys.stderr.write( "Failed to deregister node: %s" % kPluginNodeName )
raise