Feedback

What's your question?

By: Asked

Combine meshes into one with Maya API ?

Hi,

I am trying to figure out a way to write an instancer, and I am exploring the chance to merge multiple meshes into one for preview purpose.

I was looking for a method in MFnMesh to append a geometry to another, but I didn'nt find it.

So what I am going to do, is to collect all the data structure of every mesh, then reconstruct the various arrays and execute a single MFnMesh::create() method.

I am not a pro so I maybe I am wrong. Does anyone a better method to do that ?

Thank you!

Add comment viewed 205 times Latest activity 11 months ago

or Cancel

1 answer

  • 1

julian [ Admin ]

Yeah that's a good way to do it. Its a little bit tricky building the face counts and connectivity arrays.

Here's some code that you may be able to take stuff from.

unsigned flockSize; // set to number of instances
MObject m_outGeom; // object will hold combined mesh    
MFnMesh fnM; // mesh function set

// verts for the combined outputMesh for the whole flock
///////////////////////
MFloatPointArray m_vertices; 


// connectivity and UV coord info of a source object
// fill these in using MFnMesh methods
///////////////////////
unsigned m_numVertices; number of verts in source object
MIntArray m_counts;
MIntArray m_connects;
MFloatArray m_Us;
MFloatArray m_Vs;
MIntArray m_uvCounts;
MIntArray m_uvIds;
///////////////////////

// connectivity and texture coords arrays of result object
///////////////////////
MIntArray flockFaceCounts;
MIntArray flockConnectivity;
MIntArray flockUVCounts;
MIntArray flockUVIds;
///////////////////////
unsigned index = 0;


////////////////// COUNTS //////////////////////
unsigned nCounts =  m_counts.length() ; 
flockFaceCounts.setLength(nCounts * flockSize);
for (unsigned i = 0;i<flockSize;i++) {
    for (unsigned j = 0;j<nCounts;j++) {
        flockFaceCounts.set(m_counts[j],index);
        index++;
    }
}
//////////////////////////////////////  

////////////////// CONNECTS //////////////////////
index = 0;      
unsigned nConnects = (m_connects.length() );
flockConnectivity.setLength(nConnects * flockSize);
for (unsigned i = 0;i<flockSize;i++) {
    unsigned startIndex = i*m_numVertices;
    for (unsigned j = 0;j<nConnects;j++) {
        flockConnectivity.set( ( startIndex + m_connects[j] ) ,index);
        index++;
    }
}
//////////////////////////////////////  


///////////////// CREATE MESH ///////////////////// 
MFnMeshData fnC;
m_outGeom = fnC.create(&st);checkError;
fnM.create( m_vertices.length(), flockFaceCounts.length(), m_vertices, flockFaceCounts, flockConnectivity, m_outGeom, &st );  checkError;
//////////////////////////////////////  


///////////////// ASSIGN UVS /////////////////////  
unsigned nUVCounts = m_uvCounts.length();
if (nUVCounts == nCounts) {

    for (unsigned i = 0;i<flockSize;i++) {// number of agents
        for (unsigned j = 0;j<nUVCounts;j++) { // number of polygons in one agent
            flockUVCounts.append(m_uvCounts[j]);
        }
    }

    unsigned nUVIds = (m_uvIds.length() );
    for (unsigned i = 0;i<flockSize;i++) {
        for (unsigned j = 0;j<nUVIds;j++) {
            flockUVIds.append(m_uvIds[j]);
        }
    }
    st = fnM.setUVs(m_Us, m_Vs);    checkError; 
    st = fnM.assignUVs ( flockUVCounts, flockUVIds );       checkError;     

} else {
    cerr << "nUVCounts counts don't match nCounts: " <<  nUVCounts << " " <<  nCounts << endl;
}
//////////////////////////////////////  

An optimization you can make later, is to check to see if the source object or the number of instances has changed each frame. If neither have changed, you can avoid rebuilding the mesh from scratch, and instead just put the new vertices in the existing object.

fnM.setObject(m_outGeom);
fnM.setPoints(m_vertices);  

Hope it helps

or Cancel