Sign in to follow this  
Falconite

Maya Mesh Importer

Recommended Posts

Hey guys, I need help. I am currently using a maya exporter to get the mesh details into a file. The mesh exporter i am using is from robthebloke's site.(dunno if im allowed to give out websites). I need a way to set a selected mesh's vertices in maya from a input file. I have the modified vertices in a file. Any help on how to set/import this to the mesh in maya? Here is a snippet of my code i was trying. Any help would be GREAT. Thanks. -------------------------------------- float *xx; fnMesh.getPoints(vertices); xx=(float*)malloc(3*vertices.length()*sizeof(float)); for(il=0; il<3*vertices.length(); ++il) // Store the values of their co-ordinates in the mesh { fscanf(fp, "%g", xx+il); } for(i=0;i!=vertices.length() ; ++i) { MPoint OutPoint = vertices[i]; OutPoint.x = xx[i]; OutPoint.y = xx[i+1]; OutPoint.z = xx[i+2]; Check.append(OutPoint); fnMesh.setPoints(Check); } ---------------------------------------- Thanks a lot

Share this post


Link to post
Share on other sites
You didn't actually say what the problem is, so i'll take a guess ;)

I think you are trying to do this with the above code ? be warned that setpoints defaults to local space. It also assumes that the mesh is valid and/or been created correctly.


MPointArray points;
for(il=0; il != fnMesh.numVertices(); ++il)
{
float temp[3];
fscanf(fp,"%f%f%f",temp,temp+1,temp+2);
points.append( MPoint(temp[0],temp[1],temp[2]) );
}
fnMesh.setPoints(points);


You should be able to do the same thing using MItGeometry which is more generic (ie, works for nurbs, subdivs etc as well).

The only real caveat is that if you are trying to set the points in world space, you will need to initialise the function set using an MDagPath instead of an MObject.

/edit

I think i've spotted your problem

OutPoint.x = xx[3*i];
OutPoint.y = xx[3*i+1];
OutPoint.z = xx[3*i+2];


however you are doing more work than you really need...

Share this post


Link to post
Share on other sites
Rob,
Thank you so much for replying back. Really helps. Just absolutely love your website and is really very useful for n00b's like me! :D.

I tried your code change and i think it is the problem with the world/local space conversion. I used your code 'basic mesh exporter' and the 'BuildTransform' to convert it into world space. Here is the function which is doing my import.

The output of this when i run(i create a polygonal plane surface, select it and run my plugin command) is a completely deformed garbled shape(vertices are all repositioned in a weird way).

To explain a bit more : i export the plane to a file, my plugin calculates change(implementing cloth plugin) and outputs the new vertices on the file again. Now i need to reassign this to the plane mesh.

-----------------------------------------------

void export_hw::ChangeMesh(std::string &outfile,const MObject &obj)
{
MFnMesh fnMesh(obj);

std::string infile = "OUTPUT.roh";
if ( !fnMesh.isIntermediateObject() )
{
MPointArray vertices;
MPointArray vertices2;

int i = 0;
double x, y, z;
FILE *fp;
int il;

fp=fopen("OUTPUT.roh", "rt");
if(!fp)
{
return;
}
for(int k=0;k<fnMesh.parentCount();++k)
{
int i,j;
MPointArray PointC;
MObject _obj =fnMesh.parent(k);
MMatrix mat = BuildTransform(_obj);
for(i=0;i!=fnMesh.numVertices(); ++i)
{

//MPoint OutPoint = vertices[i];
//vertices2 =
float temp[3];
fscanf(fp,"%f%f%f",temp,temp+1,temp+2);
temp[0]*mat;
temp[1]*mat;
temp[2]*mat;

PointC.append(MPoint(temp[0],temp[1],temp[2]));
}
fnMesh.setPoints(PointC);
}
}
}


Please please tell me if where i am wrong if you can point out.
Thank you SO MUCH for your time and patience.
Really appreciate it.

Rgds,
Roh

Share this post


Link to post
Share on other sites
Phew....it seems to be working now...it was the problem with the local/world space and as you rightly pointed Rob, it had to do with the multiplication of part.

Thank you so much for your help.

For those who are interested. Here is the working code snippet.
---------------------------------------------------
MMatrix export_hw::BuildTransform(MObject& obj)
{
MFnTransform fn(obj);

// get matrix transform for this bone
MMatrix M = fn.transformationMatrix();

if (fn.parentCount()>0) {
MObject _obj =fn.parent(0);
return M* BuildTransform(_obj) ;
}
return M;
}

==
for(int k=0;k<fnMesh.parentCount();++k)
{
int i=0,j;

MPointArray PointC;
MObject _obj =fnMesh.parent(k);
MMatrix mat = BuildTransform(_obj);
for(i=0;i!=fnMesh.numVertices(); i++)
{
MPoint OutPoint = vertices[i];
OutPoint.x = xx[3*i];
OutPoint.y = xx[3*i+1];
OutPoint.z = xx[3*i+2];
OutPoint.x*mat;
OutPoint.y*mat;
OutPoint.z*mat;
PointC.append(MPoint(OutPoint));
}
fnMesh.setPoints(PointC);
}
==
-------------------------------------------------

Yay!

Share this post


Link to post
Share on other sites
It's not on my site, but generally this is the easier way to get and set stuff in world space.


MDagPathArray paths;
fnMesh.getAllPaths(paths);

for(int k=0;k!=paths.length();++k)
{
int i=0,j;

// initialise the mexh function set via a dag path
fnMesh.setObject( paths[k] );

// the path can return the world space matrix
MMatrix mat = paths[k].inclusiveMatrix();

// you can now get the world space vertices
MPointArray PointC;
fnMesh.getPoints(PointC,MSpace::kWorld);

// or alternatively, set the points in world space
fnMesh.setPoints(PointC,MSpace::kWorld);

// this will only work if you initialise the Function set from a dag path
// and not an MObject...
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this