Extracting Mesh

This is a quick note about how to extract polygonal mesh from an object in Maya API and Max SDK.

In Maya, given the MObject of something, first we check if it is a mesh by MObject::hasFn(), if it returns true, create an MFnMesh based on this MObject. i.e.

if(some_obj.hasFn(MFn::kMesh))
{
MFnMesh fmesh(some_obj);
// do something to the fmesh
}

MFnMesh is the mesh function set can do almost anything to the mesh. For example:

int n_poly = fmesh.numPolygons();
 int n_vert = fmesh.numVertices();

tells us how many faces or vertices the mesh has.

In Max, there are two kinds of mesh object: TriObject is strictly triangular mesh, and PolyObject is polygonal mesh. Once you have the INode, you must check if it can be converted to a specific kind of mesh object. If it is convertible, the actual mesh function and data can be accessed as a specific member belongs to the object. i.e.

char retrievePolyFrom(INode * node, MNMesh& out_mesh)
{
 Object *cvtobj = node->EvalWorldState(0).obj;

 if (cvtobj->CanConvertToType(Class_ID(POLYOBJ_CLASS_ID, 0)))
 {
 PolyObject *poly = (PolyObject *)cvtobj->ConvertToType(0, Class_ID(POLYOBJ_CLASS_ID, 0));

 out_mesh = poly->mm;
 return 1;
 }
 return 0;
}

This only works with (polygonal) MNMesh, which is PolyObject::mm. The (triangular) Mesh should be TriObject::mesh.

Now you can use the function set to retrieve or modify the mesh data. i.e.

int face_count = poly_mesh.F(0)->deg;

This returns how many vertices the fist face of the polygonal mesh has.

Advertisements

Leave a comment

Filed under Max SDK, Maya API

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