simple Mayaexporter

Started by
3 comments, last by m4gnus 18 years, 10 months ago
atm i'm trying to write a simple exporter that creates a binary file and writes vertices+indices to it...it is based on Rob-the-bloke's simple exporterand the code i added is almost copied and pasted from the tuts his site...the only big change i did was switching from textfiles to binary files. these files consists of chunks that i defined in a headerfile. but my problem is that as soon as i export sth in maya creates the file, writes some stuff and crashes...i don't know where the problem could be...my code also contains the loops for getting the Uvs+Normals but the are not exported yet... my code:

std::ofstream ofs(file.fullName().asChar(),ios::out | ios::binary);

	// make sure it opened ok
	if(!ofs) 
	{
		std::cerr << "[ERROR] Could not open file for writing " << file.fullName().asChar() << std::endl;
	}
	else 
	{
		// use an iterator to iterate through all Meshnodes in the scene
		MItDependencyNodes it(MFn::kMesh);

		// loop through all nodes
		for( ; !it.isDone(); it.next() )
		{

			MFnMesh fn(it.item());

			if(!fn.isIntermediateObject()) 
			{
				//VERTICES:

				
				MPointArray vts;
				
				fn.getPoints(vts);
				//ofs << fn.name().asChar() << ":" << std::endl;  //<--Mesh

				ngVertexChunkHeader VertexChunkHeader;
				VertexChunkHeader.dwNumVertices=vts.length();
       		    VertexChunkHeader.dwVertexSize=sizeof(MPoint);
				VertexChunkHeader.fBoundingSphereRadius=5.0f;
				VertexChunkHeader.dwFVF=D3DFVF_XYZ;
				ngChunkHeader VertHeader;
				VertHeader.ChunkType=NG_VERTEXCHUNK;
				VertHeader.dwDataSize=VertexChunkHeader.dwNumVertices*VertexChunkHeader.dwVertexSize+sizeof(VertexChunkHeader);
				
				ofs.write(reinterpret_cast<char *>(&VertHeader),sizeof(VertHeader));
				ofs.write(reinterpret_cast<char *>(&VertexChunkHeader),sizeof(VertexChunkHeader));

				for(int i=0;i!=vts.length();i++) 
				{
					//it's vertices:
					//ofs << "{" << vts.x << ";" << vts.y << ";" << vts.z << "}" <<std::endl;
					struct Vert {
						float x,y,z;
					};
					Vert Vertex;
					Vertex.x=vts.x;
					Vertex.x=vts.y;
					Vertex.x=vts.z;
					ofs.write(reinterpret_cast<char *>(&Vertex),sizeof(Vertex));
				}
				
				//NORMALS:
				MFloatVectorArray normals;
				fn.getNormals(normals);
				for(int i=0;i!=vts.length();i++)   //<--iterate trough all normals
				{
					//it's Normals:
					//ofs << "{" << vts.x << ";" << vts.y << ";" << vts.z << "}" <<std::endl;

				}

				//UV-Coords:
				MStringArray uvsets; //a mesh can have multiple Uvsets!
				fn.getUVSetNames(uvsets);
				if( !uvsets.length() || !fn.numUVs(uvsets[0]) ) 
				{
  					cout << "0\n";

				} 
				for(int j=0;j!=uvsets.length();j++)  //for every UVSet
				{

					cout << uvsets.asChar() <<endl; //print out the name of the uvset

					MFloatArray ucoords;
					MFloatArray vcoords;

					fn.getUVs(ucoords,vcoords,&uvsets);
					int NumUvs;
					NumUvs=fn.numUVs(uvsets);

					for(int i=0;i!=NumUvs;i++)   //for every UVCoord
					{

						//ucoords= UCoords
						//vcoords= VCoords


					}

				} // for every UVset end

				//Indices:
				ngIndexChunkHeader IndexChunkHeader;
				IndexChunkHeader.IndexFormat=D3DFMT_INDEX16;
				IndexChunkHeader.dwIndexSize=sizeof(short);
				ngChunkHeader IndHeader;
				IndHeader.ChunkType=NG_INDEXCHUNK;
				
				MItMeshPolygon itPoly(it.item());
				std::vector<short> VertexIndices;
				while(!itPoly.isDone())  //for every Poly
				{
					int NumVerts=itPoly.polygonVertexCount(); //Vertices per Polygon
					if(NumVerts==3) 
					{
						for(int k=0;i<NumVerts;k++) //for every Vertex of the polygon
						{
							
							VertexIndices.push_back(short(itPoly.vertexIndex(i)));
							//itPoly.vertexIndex(i); outputs Vertexindices
							//itPoly.normalIndex(i); outputs Vertexnormalindices


						}
					}
					
				}
				IndexChunkHeader.dwNumIndices=VertexIndices.size();
				IndHeader.dwDataSize=sizeof(IndexChunkHeader.dwIndexSize)*IndexChunkHeader.dwNumIndices;
				ofs.write(reinterpret_cast<char *>(&IndHeader),sizeof(IndHeader));
				ofs.write(reinterpret_cast<char *>(&IndexChunkHeader),sizeof(IndexChunkHeader));
				for(int l=0;l<VertexIndices.size();l++) {
					std::vector<short>::pointer ptr = &VertexIndices[0];
				ofs.write(reinterpret_cast<char *>(ptr),sizeof(short)*VertexIndices.size());
				
				}//UV COORDS MISSING!

		
		ofs.close();
		}
		}
	}
	return MS::kSuccess;
}

regards, m4gnus
"There are 10 types of people in the world... those who understand binary and those who don't."
Advertisement
run it in debug and step through till it crahses...the let us know where that was and we might be able to help :o)
We have youth, how about a fountain of smart.e4 e5 f4 d5
i don't know how i can do this because it creates a dll that is loaded by maya as a plugin...
btw i haven't even used a debugger on other projects ...so i don't really know how to use it...especially in .net 2003 (i used VC 6 earlier)

regards,
m4gnus
"There are 10 types of people in the world... those who understand binary and those who don't."
If you have a DLL project, when you hit F5 DevStudio should ask you which exe you want to run. Pick the Maya exe. That should work fine.

Using the debugger is pretty simple. If you hit F9 on a line of code it will put a breakpoint there. This will stop the execution on that line. You can then use F10 to proceed line by line and see what happens and F11 will step into a function call on any given line. To get the program to continue running normally, just hit F5 again.

Also, you should be able to start debugging without doing any of this if it's crashing in your DLL. When it crashes, look in the "Call Stack" tab in DevStudio. It should show you which line it stopped on, as well as the previous calls.

Getting familiar with the debugger is a really helpful thing when programming :)

-John
- John
ok the debugger really helped me...the while loop for getting the indices of every face was an infinite loop because the itPoly.next(); was missing...but the error is also on Rob-the-bloke's homepage so i'll write him a short pm...

Edit: btw i also have a problem with the loading of the data...i think it's just a syntax error so it should be easy to solve...I'll open a new thread

thx


regards,
m4gnus
"There are 10 types of people in the world... those who understand binary and those who don't."

This topic is closed to new replies.

Advertisement