Using MArrayDataBuilder - python 2.0 API

Using MArrayDataBuilder - python 2.0 API

froszu
Enthusiast Enthusiast
694 Views
0 Replies
Message 1 of 1

Using MArrayDataBuilder - python 2.0 API

froszu
Enthusiast
Enthusiast

I need a simple DG node that uses connected mesh and copies vertex coordinates to output MPoint array.

I am having trouble with populating this array with MArrayDataBuilder. It ether always stays zero-length or grows on every compute().

This surely must be simple to do, but I'm stuck.

 

from __future__ import print_function


def maya_useNewAPI():
	pass

import maya.api.OpenMaya as OpenMaya
import maya.api.OpenMayaAnim as OpenMayaAnim

kPluginNodeTypeName = "mfVertexCacheChanExtractor"
chanExtractorId = OpenMaya.MTypeId(0x8703)


class chanExtractor(OpenMaya.MPxNode):
	inMesh = OpenMaya.MObject() # mesh type
	worldSpace = OpenMaya.MObject() # bool
	outPoints = OpenMaya.MObject() # sparse point array

   	def __init__(self):
		OpenMaya.MPxNode.__init__(self)

	def compute(self, plug, dataBlock):
		if ( plug == self.outPoints ):
			mesh = dataBlock.inputValue(self.inMesh).asMesh()
			inputMeshFn = OpenMaya.MFnMesh(mesh)

			space = OpenMaya.MSpace.kObject
			if(dataBlock.inputValue(self.worldSpace).asBool()):
				space = OpenMaya.MSpace.kWorld

			inPoints = inputMeshFn.getPoints( space )
			outPointsH = dataBlock.outputArrayValue(self.outPoints)

			# out builder...
			outPointsB = outPointsH.builder()

			# resize to match inPoints size
			if len(outPointsB) < len(inPoints):
				add = len(inPoints) - len(outPointsB)
				outPointsB.growArray( add )
				outPointsH.set(outPointsB)
			# it seems the array is still 0 at this point ??

			for i, in_pt in enumerate(inPoints):
				outPointsH.jumpToLogicalElement(i) # this fails, since the array is empty
				outPointsH.outputValue().set3Double(in_pt.x, in_pt.y, in_pt.z)

			###
			###
			dataBlock.setClean(plug)
		else:
			return None


def nodeCreator():
	return chanExtractor()


def nodeInitializer():
	nAttr = OpenMaya.MFnNumericAttribute()

	typedAttr = OpenMaya.MFnTypedAttribute()
	chanExtractor.inMesh = typedAttr.create("inMesh", "inMesh",OpenMaya.MFnData.kMesh )

	chanExtractor.worldSpace = nAttr.create( "worldSpace", "worldSpace", OpenMaya.MFnNumericData.kBoolean, 0)

	# output
	chanExtractor.outPoints = nAttr.createPoint( "outPoints", "outPoints", )
	nAttr.array = True
	nAttr.usesArrayDataBuilder = True

	chanExtractor.addAttribute( chanExtractor.inMesh )
	chanExtractor.addAttribute( chanExtractor.worldSpace )
	chanExtractor.addAttribute( chanExtractor.outPoints )

	chanExtractor.attributeAffects( chanExtractor.inMesh, chanExtractor.outPoints )
	chanExtractor.attributeAffects( chanExtractor.worldSpace, chanExtractor.outPoints )



# initialize the script plug-in
def initializePlugin(mobject):
	mplugin = OpenMaya.MFnPlugin(mobject)
	mplugin.registerNode( kPluginNodeTypeName, chanExtractorId, nodeCreator, nodeInitializer, OpenMaya.MPxNode.kDependNode )

# uninitialize the script plug-in
def uninitializePlugin(mobject):
	mplugin = OpenMaya.MFnPlugin(mobject)
	mplugin.deregisterNode( chanExtractorId )



'''

// paste this MEL to test:

file -f -new; catchQuiet(`unloadPlugin "mfVertexCacheChanExtractor"`);
loadPlugin "D:/dev/projs/mfVertexCacheChanExtractor.py";

{
    string $ex = `createNode "mfVertexCacheChanExtractor"`;
    string $nodes[] = `polyPlane -sh 2 -sw 2 -ch 0`;
    connectAttr -f ($nodes[0] + ".worldMesh") ($ex + ".inMesh");
    select $ex;
    print `getAttr -s ($ex + ".outPoints")`; print "\n";
    getAttr ($ex + ".outPoints[0].outPointsX");
}

'''
0 Likes
695 Views
0 Replies
Replies (0)