Maya scatter node

Just enjoying exploring the API…

I made a node that scatters a bunch of points and then a script that puts locators on those positions; not useful yet, but this attribute can be pushed to another node to say.. instance a mesh around or as source to spawn something from or whatever you like.

cmds.file(new=True, f=True)
cmds.unloadPlugin('MayaAPI.mll')

cmds.loadPlugin('MayaAPI.mll')
scatterNode = cmds.createNode('scatterPointsOnMesh')
inMesh = cmds.polyCylinder()[0]
cmds.connectAttr( '%s.outMesh'%inMesh, '%s.inMesh'%scatterNode )
cmds.dgeval( '%s.outPoints'%scatterNode )

for point in cmds.getAttr( '%s.outPoints'%scatterNode ):
    cmds.spaceLocator(p=point[0:3])

It left me with the knowledge that compiling for 32bit really won’t work if you only have 64bit Maya; I was using VS2008 which didn’t let me compile to 64bit profile so after installing VS2012 I had more success.

Additionally if you create numeric attributes, their data type in the compute method is going to be MFnData::kNumeric and not a more detailed MFnNumericType.

scatter

So what it basically does is use stdlib’s rand to get random UV coordinates (it returns an int so use (float)rand() / (float)RAND_MAX to get desirable results). Then it iterates over the mesh polygon Ids and uses MFnMesh.getPointAtUv to see whether the UV coord lies in that polygon and where the 3D point is.

I can see the flaw of non 0-1 space UVs or no UVs here, so make sure to use automatic mapping, auto layout and set the UV set before implementing this somewhere useful.

It’s something really basic, but brief nodes are the most reusable ones. Download solution here…

Leave a Reply

Your email address will not be published. Required fields are marked *