49 Implements the dependency graph node "spArrayAttrBlend".
51 This plug-in is an example of a user-defined dependency graph node.
52 It takes 2 array inputs and it computes an array output
53 where each output value is the average of the 2 corresponding inputs values.
54 A weight parameter controls how much of input A vs input B is taken into
55 account in the output computation
57 // Create a spArrayAttrBlend connected to a cube translation
58 createNode spArrayAttrBlend;
61 connectAttr spArrayAttrBlend1.output[0] pCube1.tx;
62 connectAttr spArrayAttrBlend1.output[1] pCube1.ty;
63 connectAttr spArrayAttrBlend1.output[2] pCube1.tz;
65 // Modify input parameters to affect the cube position
66 setAttr spArrayAttrBlend1.weight 1; // Output is driven 100% by inputB
67 setAttr spArrayAttrBlend1.inputB[1] 10; // will feed 10 to pCube1.ty
71 import maya.OpenMaya
as OpenMaya
72 import maya.OpenMayaMPx
as OpenMayaMPx
74 kPluginNodeTypeName =
"spArrayAttrBlend"
79 class spArrayAttrBlend(OpenMayaMPx.MPxNode):
89 OpenMayaMPx.MPxNode.__init__(self)
92 def getInputValue( data, attribute, logicalIndex ):
94 Helper method that extract an element value from the datablock for a specified
100 arrayDataHandle = data.inputArrayValue( attribute )
102 arrayDataHandle.jumpToElement( logicalIndex )
103 value = arrayDataHandle.inputValue().asFloat()
108 default_util = OpenMaya.MScriptUtil()
109 default_util.createFromDouble(0.0)
110 default_ptr = default_util.asFloatPtr()
112 value = default_util.getFloat( default_ptr )
117 def compute(self, plug, data):
119 This callback gets invoked when Maya needs to retrieve the value of
124 if (plug == spArrayAttrBlend.aOutput ):
129 return OpenMaya.kUnknownParameter
132 logicalIndex = plug.logicalIndex()
135 A = spArrayAttrBlend.getInputValue( data, spArrayAttrBlend.aInputA, logicalIndex )
136 B = spArrayAttrBlend.getInputValue( data, spArrayAttrBlend.aInputB, logicalIndex )
138 weight = data.inputValue( spArrayAttrBlend.aWeight ).asFloat()
140 blendedValue = A*(1.0-weight )+B*weight
142 data.outputValue(plug).setFloat( blendedValue )
143 data.setClean( plug )
146 return OpenMaya.kUnknownParameter
149 def setDependentsDirty( self, plug, plugArray ):
154 if (plug == spArrayAttrBlend.aInputA )
or (plug == spArrayAttrBlend.aInputB ):
156 logicalIndex = plug.logicalIndex()
157 thisNode = self.thisMObject()
159 outPlug.selectAncestorLogicalIndex( logicalIndex, spArrayAttrBlend.aOutput )
160 plugArray.append(outPlug)
164 def createNodeInstance():
165 """ Maya invokes this method everytime it needs to instanciate a spArrayAttrBlend node """
166 return OpenMayaMPx.asMPxPtr( spArrayAttrBlend() )
170 def initNodeTypeAttributes():
172 This method is invoked when spArrayAttrBlend node type gets registered
173 allowing the current plugin to register and setup attributes to the
174 new spArrayAttrBlend type
182 spArrayAttrBlend.aInputA = nAttr.create(
"inputA",
"ia", OpenMaya.MFnNumericData.kFloat, 0.2)
183 nAttr.setStorable(
True)
184 nAttr.setArray(
True )
186 spArrayAttrBlend.aInputB = nAttr.create(
"inputB",
"ib", OpenMaya.MFnNumericData.kFloat, 0.3)
187 nAttr.setStorable(
True)
188 nAttr.setArray(
True )
190 spArrayAttrBlend.aWeight = nAttr.create(
"weight",
"wt", OpenMaya.MFnNumericData.kFloat, 0.5)
191 nAttr.setStorable(
True)
192 nAttr.setArray(
False )
195 spArrayAttrBlend.aOutput = nAttr.create(
"output",
"out", OpenMaya.MFnNumericData.kFloat, 0.0)
196 nAttr.setStorable(
False)
197 nAttr.setWritable(
False)
198 nAttr.setArray(
True )
201 spArrayAttrBlend.addAttribute(spArrayAttrBlend.aInputA)
202 spArrayAttrBlend.addAttribute(spArrayAttrBlend.aInputB)
203 spArrayAttrBlend.addAttribute(spArrayAttrBlend.aWeight)
204 spArrayAttrBlend.addAttribute(spArrayAttrBlend.aOutput)
207 spArrayAttrBlend.attributeAffects(spArrayAttrBlend.aWeight, spArrayAttrBlend.aOutput)
215 def initializePlugin(mobject):
216 """ This method is automatically invoked when the plugin is loaded """
217 mplugin = OpenMayaMPx.MFnPlugin(mobject,
"Autodesk",
"1.0",
"Any")
219 mplugin.registerNode( kPluginNodeTypeName, kPluginNodeId, createNodeInstance, initNodeTypeAttributes )
221 sys.stderr.write(
"Failed to register node: %s" % kPluginNodeTypeName )
225 def uninitializePlugin(mobject):
226 """ This method is automatically invoked when the plugin is unloaded """
227 mplugin = OpenMayaMPx.MFnPlugin(mobject)
229 mplugin.deregisterNode( kPluginNodeId )
231 sys.stderr.write(
"Failed to deregister node: %s" % kPluginNodeTypeName )