Sunday, November 14, 2010

Deforming vertices along normals

This is a deformer that takes a bunch of vertices and deforms them along their averaged normal.

We create a "amount" attribute to tell the deformer how much to deform.

The deformer node is initialized like any other DG node by overriding the required functions.

However, the deform function needs to be overridden instead of the compute function. Maya calls the deform function.

In rare cases, the compute function needs to be overridden instead of the compute. In such cases, the deformation code should be put into the compute function, as Maya would only call compute, and deform function is ignored.

The input "multi" attribute is used to query the input mesh to the deformer. We need the input mesh to get the normal direction on each of the vertices.

e.g.
MStatus myNode::deform( MDataBlock &data, MItGeometry &iter, const MMatrix &mat, unsigned int multiIndex ) {
MArrayDataHandle inputArray = data.inputArrayValue( input ); // get the handle to mesh data.
inputArray.jumpToElement( multiIndex );

MFnMesh meshFn( inputArray.inputValue().child( inputGeom ).data(); // to get the data() as MFnMeshData and MFnMesh is used to manipulate data.

// the MItGeometry iterator is used to iterate through the points affected by the deformer and MFnMesh is used to retrieve the normal information.

#pragma omp parallel for // open MP parallelism.

for( iter.reset(); !iter.isDone(); iter.next() ) {
float weight = weightValue( data, multiIndex, iter.index() ); // convenience function to get the deformer weight at the point.
MPoint pt = iter.position();
MVector normalAtPoint;
meshFn.getVertexNormal( iter.index(), true, normalAtPoint );

//Now the point can be deformed along the normal components via

pt += normalAtPoint * displaceAmount; // displace amount can be calc'ed by varying attributes of the node. e.g. envelope, amount( user defined ), and weight.
iter.setPosition( pt );
}

This can be used in a variety of scenarios. Leave behind any comments or requests or suggestions.