geomShader.py是使用API编写maya材质的简单的例子,是的API也可以用来写自定义材质。
为了方便我就直接复制帮助文档里的geomShader.py内容过来,加入适当的注释,要试用的话可以直接加载devkit\plug-ins\scripted中的geomShader.py,你会在Hypershade的utility栏下的color中找到"spGeomShader"
值得注意的是添加属性的方法,和注册插件的方法的变动
在线版本
[url]http://download.autodesk.com/us/maya/2011help/API/geom_shader_8py-example.html[/url]
为了方便我就直接复制帮助文档里的geomShader.py内容过来,加入适当的注释,要试用的话可以直接加载devkit\plug-ins\scripted中的geomShader.py,你会在Hypershade的utility栏下的color中找到"spGeomShader"
import sys
import maya.OpenMaya as OpenMaya
import maya.OpenMayaMPx as OpenMayaMPx
# 材质的名称
kPluginNodeName = "spGeomShader"
# 材质所在的位置
kPluginNodeClassify = "utility/color"
# 材质节点的类型ID
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:
# 给节点创建pointObj属性
geomShader.aPoint = nAttr.createPoint("pointObj", "p")
nAttr.setStorable(0)
nAttr.setHidden(1)
# 给节点创建scale属性
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)
# 给节点创建offset属性
geomShader.aOffset = nAttr.createPoint("offset", "o")
nAttr.setKeyable(1)
nAttr.setStorable(1)
nAttr.setReadable(1)
nAttr.setWritable(1)
# 给节点创建outColor属性
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
# initialize the script plug-in
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
# uninitialize the script plug-in
def uninitializePlugin(mobject):
mplugin = OpenMayaMPx.MFnPlugin(mobject)
try:
mplugin.deregisterNode( kPluginNodeId )
except:
sys.stderr.write( "Failed to deregister node: %s" % kPluginNodeName )
raise
值得注意的是添加属性的方法,和注册插件的方法的变动
在线版本
[url]http://download.autodesk.com/us/maya/2011help/API/geom_shader_8py-example.html[/url]