Mesh Density Deformer

This morning I was feeling adventurous and decided to make a maya deformer. Normally I write python api plugins but they seam to run slow so I decided to try c++. The deformer measures the density of the mesh and offsets the points along there normal based on the measurment. Here’s an example and the compute statment:

MStatus deform::compute( const MPlug& plug, MDataBlock& data )
 
{
    MStatus returnStatus;
 
 
    if( plug == aOutMesh )
    {
 
            MDataHandle offset_data = data.inputValue( offset, &returnStatus );
            float offset = offset_data.asFloat();
 
            MDataHandle mult_data = data.inputValue( mult, &returnStatus );
            float mult = mult_data.asFloat();
 
            MDataHandle inMeshDataHandle = data.inputValue(aInMesh);
            MObject inMesh = inMeshDataHandle.asMesh();
 
            MDataHandle outMeshDataHandle = data.outputValue(aOutMesh);
            MObject outMesh = outMeshDataHandle.asMesh();
 
            MFnMeshData meshDataFn;
            MObject newMeshObj = meshDataFn.create();
            MFnMesh newMeshFn;
            newMeshFn.copy( inMesh, newMeshObj );
             
            MFloatPointArray verts;
            newMeshFn.getPoints(verts,MSpace::kObject);
            MFloatVectorArray normals;
            newMeshFn.getNormals(normals,MSpace::kObject);
             
            MFloatPointArray out_verts;
            out_verts.setLength(verts.length());
            MItMeshVertex vert_it(newMeshObj);
            while (vert_it.isDone() == false)
            {
                int index = vert_it.index();
                MIntArray connected_verts;
                vert_it.getConnectedVertices(connected_verts);
 
                float avg_dist = 0;
 
                int numVerts = connected_verts.length();
                for( int i = 0; i < numVerts; i++ )
                {
                    avg_dist += verts[index].distanceTo(verts[connected_verts[i]]);
                }
 
                out_verts.set( verts[index]+normals[index]*(offset-(mult*(avg_dist/numVerts))),index);
                vert_it.next();
 
            }
            newMeshFn.setPoints(out_verts,MSpace::kObject);
            outMeshDataHandle.set(newMeshObj );
 
            data.setClean(plug);
        }
 
    return MS::kSuccess;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: