I thought I would post a more finished example based on what you guys
were talking about. I thought it was a good example and its sweet how
when you add to the input array it automatically adds to the output
array. I was going to post this on my blog with a cheers to you Jason
for bringing it up.
# Save this script off as a plugin and load it into maya's plugins
import sys
import maya.OpenMaya as om
import maya.OpenMayaMPx as omMPx
kPluginNodeTypeName = "pyNode"
pyNodeId = om.MTypeId(0x87000)
class pyNode( omMPx.MPxNode ):
inputs = om.MObject()
output = om.MObject()
def __init__( self ):
omMPx.MPxNode.__init__( self )
def compute( self, plug, dataBlock ):
if ( plug == pyNode.output ):
# Get the input handle
dataHandle = dataBlock.inputArrayValue( pyNode.inputs )
numElements = dataHandle.elementCount()
# Get output handle
outputHandle = dataBlock.outputArrayValue(
pyNode.output )
# Iterate through input array and set the output array
for i in range(numElements):
dataHandle.jumpToElement(i)
handle = dataHandle.inputValue()
result = handle.asFloat()
outputHandle.jumpToElement(i)
outdatahandle = outputHandle.outputValue()
outdatahandle.setFloat( result )
dataBlock.setClean( plug )
return om.kUnknownParameter
def nodeCreator():
return omMPx.asMPxPtr( pyNode() )
def nodeInitializer():
nAttr = om.MFnNumericAttribute()
pyNode.inputs = nAttr.create ( "inputs", "in",
om.MFnNumericData.kFloat, 0.0 )
nAttr.setArray(1)
nAttr.setStorable(1)
pyNode.addAttribute ( pyNode.inputs )
nAttr = om.MFnNumericAttribute()
pyNode.output = nAttr.create( "output", "out",
om.MFnNumericData.kFloat, 0.0 )
nAttr.setArray(1)
nAttr.setStorable(1)
nAttr.setWritable(1)
pyNode.addAttribute( pyNode.output )
pyNode.attributeAffects( pyNode.inputs, pyNode.output )
def initializePlugin(mobject):
mplugin = omMPx.MFnPlugin(mobject)
try:
mplugin.registerNode( kPluginNodeTypeName, pyNodeId,
nodeCreator,
nodeInitializer )
except:
sys.stderr.write( "Failed to register node: %s" %
kPluginNodeTypeName )
raise
def uninitializePlugin(mobject):
mplugin = omMPx.MFnPlugin(mobject)
try:
mplugin.deregisterNode( pyNodeId )
except:
sys.stderr.write( "Failed to deregister node: %s" %
PluginNodeTypeName )
raise
# Now run this in the script editor python tab
import maya.cmds as cmds
sphere = cmds.polySphere()
pNode = cmds.createNode( 'pyNode' )
cmds.move(1.2, 3.4, 4.5, sphere[0], os=True, wd=True )
cmds.connectAttr( (sphere[0] + '.translateX'), (pNode + '.inputs[0]'),
f=True )
cmds.connectAttr( (sphere[0] + '.translateY'), (pNode + '.inputs[1]'),
f=True )
cmds.connectAttr( (sphere[0] + '.translateZ'), (pNode + '.inputs[2]'),
f=True )
cmds.getAttr( (pNode + '.output[0]') )
cmds.getAttr( (pNode + '.output[1]') )
cmds.getAttr( (pNode + '.output[2]') )
On Feb 5, 3:20 pm, jasonosipa <[email protected]> wrote:
> EXCELLENT!
>
> This has made it easier to go ahead and learn more on my own instead
> of just immediately asking. I'm seriously thinking of cataloging all
> the gems on the path from knowing Mel to learning Python, and putting
> together some training on that specific path. I know I can't be the
> only one who would just flip for such a resource...
>
> Thank you! -J.
>
> On Feb 5, 3:06 pm, Dean Edmonds <[email protected]> wrote:
>
> > One thing which *is* described in the API docs, albeit in a rather
> > confusing fashion, is the difference between an array plug and a plug
> > array.
>
> > An array plug is a single plug which contains an array of values. You
> > set and retrieve all of the array values with a single API call.
>
> > A plug array is an array of many plugs, each containing its own value.
> > You set and retrieve the values of a plug array using separate API
> > calls for each element.
>
> > When you used 'setArray(1)' you were telling Maya that your plug was a
> > plug array, but when your compute() tried to retrieve the data using
> > 'asFloatArray()' it was trying to treat the plug as an array plug.
>
> > Just to further confuse things, it is possible to have an array of
> > plugs in which each plug contains an entire array of values. That
> > would, of course, be an 'array plug array' and, unless you had a
> > really good reason to do it, would likely make you unpopular with your
> > co-workers. :-)
>
> > --
> > -deane
--~--~---------~--~----~------------~-------~--~----~
Yours,
Maya-Python Club Team.
-~----------~----~----~----~------~----~------~--~---