Spitting out Meshes & Animation Data to file.

This commit is contained in:
antc13
2016-01-12 15:39:29 +01:00
parent 0e90382fd1
commit 7b62dc19bf
7 changed files with 242 additions and 73 deletions
@@ -140,6 +140,7 @@
</DebugInformationFormat> </DebugInformationFormat>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary> <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<TreatWChar_tAsBuiltInType>true</TreatWChar_tAsBuiltInType> <TreatWChar_tAsBuiltInType>true</TreatWChar_tAsBuiltInType>
<Optimization>Disabled</Optimization>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
@@ -37,6 +37,11 @@
#include <maya/MTransformationMatrix.h> #include <maya/MTransformationMatrix.h>
#include <maya/MFnMatrixData.h> #include <maya/MFnMatrixData.h>
#include <maya/MItMeshFaceVertex.h> #include <maya/MItMeshFaceVertex.h>
#include <maya/MFnSkinCluster.h>
#include <maya/MDagPathArray.h>
#include <maya/MItGeometry.h>
#include <maya/MItMeshVertex.h>
#include <maya/MFnWeightGeometryFilter.h>
// Wrappers // Wrappers
+33 -13
View File
@@ -125,8 +125,7 @@ void Menu::ExportSelected(bool checked)
MFnDependencyNode thisNode(object); MFnDependencyNode thisNode(object);
cout << thisNode.name().asChar() << endl; cout << thisNode.name().asChar() << endl;
Mesh mesh; GetMeshData(object);
mesh.GetMeshData(object);
} }
if (m_ExportPath->text().isEmpty()) { if (m_ExportPath->text().isEmpty()) {
cout << "Please select a folder." << endl; cout << "Please select a folder." << endl;
@@ -195,17 +194,32 @@ void Menu::RemoveClipClicked(bool)
void Menu::ExportAll(bool) void Menu::ExportAll(bool)
{ {
MDagPath path; MDagPath path;
m_File.ASCIIFilePath("C:/Users/Nickelodion/Desktop/coolASCII.txt");
m_File.binaryFilePath("C:/Users/Nickelodion/Desktop/coolSoptunz.bin");
m_File.OpenFiles();
// Loop through all nodes in the scene // Loop through all nodes in the scene
MItDependencyNodes it(MFn::kInvalid); MItDependencyNodes it(MFn::kMesh);
for (; !it.isDone(); it.next()) { for (; !it.isDone(); it.next()) {
MObject node = it.thisNode(); MObject node = it.thisNode();
if (node.hasFn(MFn::kMesh)) { if (node.hasFn(MFn::kMesh)) {
MFnDependencyNode thisNode(node); MFnDependencyNode thisNode(node);
MPlugArray connections;
cout << thisNode.name().asChar() << endl; thisNode.findPlug("inMesh").connectedTo(connections, true, true);
Mesh mesh; bool next = false;
mesh.GetMeshData(node); for (unsigned int i = 0; i < connections.length(); i++) {
if(connections[i].node().apiType() == MFn::kSkinClusterFilter){
next = true;
break;
}
}
if (next)
continue;
cout << thisNode.name().asChar() << endl;
MGlobal::displayInfo("EXPORT ALL FUNCTION: " + thisNode.name() + " " + thisNode.findPlug("inMesh").asMObject().apiTypeStr());
GetMeshData(node);
} }
} }
if (m_ExportPath->text().isEmpty()) { if (m_ExportPath->text().isEmpty()) {
@@ -215,12 +229,9 @@ void Menu::ExportAll(bool)
cout << m_ExportPath->text().toLocal8Bit().constData() << endl; cout << m_ExportPath->text().toLocal8Bit().constData() << endl;
} }
m_File.ASCIIFilePath("C:/Users/Nickelodion/Desktop/coolASCII.txt");
m_File.binaryFilePath("C:/Users/Nickelodion/Desktop/coolSoptunz.bin");
if (m_ExportAnimationsButton->isChecked()) if (m_ExportAnimationsButton->isChecked())
GetSkeletonData(); GetSkeletonData();
m_File.CloseFiles();
} }
void Menu::CancelClicked(bool) void Menu::CancelClicked(bool)
@@ -257,7 +268,19 @@ void Menu::Button3Clicked(bool)
cout << "3 unchecked!" << endl; cout << "3 unchecked!" << endl;
} }
} }
void Menu::GetMeshData(MObject object)
{
std::vector<VertexLayout> vertexList;
std::vector<unsigned int> indexList;
Mesh mesh;
mesh.GetMeshData(object, vertexList, indexList);
for (auto aVertex : vertexList) {
m_File.writeToFiles(&aVertex);
}
m_File.writeToFiles(indexList.data(), indexList.size());
}
void Menu::GetMaterialData() void Menu::GetMaterialData()
{ {
this->m_MaterialHandler = new Material(); this->m_MaterialHandler = new Material();
@@ -296,8 +319,6 @@ void Menu::GetSkeletonData()
allAnimations.push_back(m_SkeletonHandler->GetAnimData(animationName, startFrame, endFrame)); allAnimations.push_back(m_SkeletonHandler->GetAnimData(animationName, startFrame, endFrame));
} }
m_File.OpenFiles();
//print out all bind poses //print out all bind poses
for (auto aBindPose : allBindPoses){ for (auto aBindPose : allBindPoses){
m_File.writeToFiles(&aBindPose); m_File.writeToFiles(&aBindPose);
@@ -315,7 +336,6 @@ void Menu::GetSkeletonData()
for (auto aAnimation : allAnimations) { for (auto aAnimation : allAnimations) {
m_File.writeToFiles(&aAnimation); m_File.writeToFiles(&aAnimation);
} }
m_File.CloseFiles();
} }
Menu::~Menu() Menu::~Menu()
+1
View File
@@ -46,6 +46,7 @@ public:
Menu(QDialog* dialog); Menu(QDialog* dialog);
~Menu(); ~Menu();
void GetMeshData(MObject object);
void GetMaterialData(); void GetMaterialData();
void GetSkeletonData(); void GetSkeletonData();
+141 -52
View File
@@ -7,21 +7,80 @@ Mesh::Mesh()
{ {
} }
std::map<int, Mesh::WeightInfo> Mesh::GetWeightData()
{
map<int, WeightInfo> weightMap;
void Mesh::GetMeshData(MObject object) MItDependencyNodes it(MFn::kSkinClusterFilter);
while (!it.isDone()) {
MObject object = it.item();
MFnSkinCluster skinCluster(object);
MDagPathArray influences;
unsigned int nrOfInfluences = skinCluster.influenceObjects(influences);
unsigned int index;
index = skinCluster.indexForOutputConnection(0);
MDagPath skinPath;
skinCluster.getPathAtIndex(index, skinPath);
MItGeometry geomIter(skinPath);
//for (unsigned int i = 0; i < nrOfInfluences; i++) {
// MGlobal::displayInfo(MString() + " Influence object name: " + influences[i].partialPathName().asChar());
//}
WeightInfo weightInfo;
while (!geomIter.isDone()) {
MObject comp = geomIter.component();
MFloatArray weights;
unsigned int influenceCount;
skinCluster.getWeights(skinPath, comp, weights, influenceCount);
MFnDependencyNode test(comp);
unsigned int nrOfWeights = 0;
for (unsigned int j = 0; j < weights.length() && nrOfWeights != 4; j++) {
if (weights[j] > 0.00001) {
weightInfo.BoneWeights[nrOfWeights] = weights[j];
weightInfo.BoneIndices[nrOfWeights] = j;
nrOfWeights++;
}
}
float totalWeight = 0.0f;
for (unsigned int i = 0; i < 4; i++) {
totalWeight += weightInfo.BoneWeights[i];
}
for (unsigned int i = 0; i < 4; i++) {
weightInfo.BoneWeights[i] /= totalWeight;
}
weightMap[geomIter.index()] = weightInfo;
for (unsigned int k = 0; k!=nrOfWeights; k++) {
//MGlobal::displayInfo(MString() + "influence: " + weightInfo.BoneIndices[k] + " weight: " + weightInfo.BoneWeights[k]);
}
geomIter.next();
}
it.next();
}
return weightMap;
}
void Mesh::GetMeshData(MObject object, std::vector<VertexLayout>& vertexList, std::vector<unsigned int>& indexList)
{ {
// In here, we retrieve triangulated polygons from the mesh // In here, we retrieve triangulated polygons from the mesh
MFnMesh mesh(object); MFnMesh mesh(object);
map<unsigned int, vector<unsigned int>> vertexToIndex;;
map<UINT, vector<UINT>> vertexToIndex;
vector<VertexLayout> verticesData;
vector<UINT>indexArray;
MIntArray intdexOffsetVertexCount, vertices, triangleList; MIntArray intdexOffsetVertexCount, vertices, triangleList;
MPointArray dummy; MPointArray dummy;
unsigned int vertexIndex;
UINT vertexIndex;
MVector normal; MVector normal;
MPoint pos; MPoint pos;
float2 UV; float2 UV;
@@ -31,62 +90,92 @@ void Mesh::GetMeshData(MObject object)
MFloatVectorArray biTangents; MFloatVectorArray biTangents;
MFloatVectorArray biNormals; MFloatVectorArray biNormals;
std::map<int, WeightInfo> vertexWeights = GetWeightData();
mesh.getTangents(biTangents, MSpace::kObject, NULL); mesh.getTangents(biTangents, MSpace::kObject, NULL);
mesh.getBinormals(biNormals, MSpace::kObject, NULL); mesh.getBinormals(biNormals, MSpace::kObject, NULL);
MItMeshFaceVertex faceVert(object); MItMeshFaceVertex faceVert(object);
int intDummy = 0; int intDummy = 0;
for (MItMeshPolygon meshPolyIter(object); !meshPolyIter.isDone(); meshPolyIter.next()) {
vector<UINT> localVertexToGlobalIndex;
meshPolyIter.getVertices(vertices);
meshPolyIter.getTriangles(dummy, triangleList); for (MItMeshPolygon meshPolyIter(object); !meshPolyIter.isDone(); meshPolyIter.next()) {
UINT indexOffset = verticesData.size();
//MGlobal::displayInfo("Befor Second Loop");
for (UINT i = 0; i < vertices.length(); i++) {
faceVert.setIndex(meshPolyIter.index(), i, intDummy, intDummy);
//MGlobal::displayInfo("In Second Loop");
faceVert.position().get(thisVertex.Pos);
faceVert.getNormal(normal);
thisVertex.Normal[0] = normal[0];
thisVertex.Normal[1] = normal[1];
thisVertex.Normal[2] = normal[2];
MFloatVector biTangent = biTangents[faceVert.tangentId()]; vector<unsigned int> localVertexToGlobalIndex;
//MVector tmp = faceVert.getTangent(MSpace::kObject, NULL); unsigned int indexOffset = vertexList.size();
//tmp.get(biTangent);
thisVertex.BiTangent[0] = biTangent[0];
thisVertex.BiTangent[1] = biTangent[1];
thisVertex.BiTangent[2] = biTangent[2];
MFloatVector biNormal = biNormals[faceVert.tangentId()]; meshPolyIter.getVertices(vertices);
//faceVert.getBinormal().get(biNormal); meshPolyIter.getTriangles(dummy, triangleList);
thisVertex.BiNormal[0] = biNormal[0]; MGlobal::displayInfo(MString() + "vertices.length(): " + vertices.length());
thisVertex.BiNormal[1] = biNormal[1]; //MGlobal::displayInfo("Befor Second Loop");
thisVertex.BiNormal[2] = biNormal[2]; for (unsigned int i = 0; i < vertices.length(); i++) {
vertexIndex = meshPolyIter.vertexIndex(i);
faceVert.setIndex(meshPolyIter.index(), i, intDummy, intDummy);
//MGlobal::displayInfo("In Second Loop");
faceVert.position().get(thisVertex.Pos);
faceVert.getNormal(normal);
thisVertex.Normal[0] = normal[0];
thisVertex.Normal[1] = normal[1];
thisVertex.Normal[2] = normal[2];
faceVert.getUV(UV); MFloatVector biTangent = biTangents[faceVert.tangentId()];
thisVertex.Uv[0] = UV[0]; //MVector tmp = faceVert.getTangent(MSpace::kObject, NULL);
thisVertex.Uv[1] = UV[1]; //tmp.get(biTangent);
thisVertex.BiTangent[0] = biTangent[0];
thisVertex.BiTangent[1] = biTangent[1];
thisVertex.BiTangent[2] = biTangent[2];
verticesData.push_back(thisVertex); MFloatVector biNormal = biNormals[faceVert.tangentId()];
localVertexToGlobalIndex.push_back(vertexIndex); //faceVert.getBinormal().get(biNormal);
thisVertex.BiNormal[0] = biNormal[0];
thisVertex.BiNormal[1] = biNormal[1];
thisVertex.BiNormal[2] = biNormal[2];
//cout << "Pos: " << thisVertex.Pos[0] << "/" << thisVertex.Pos[1] << "/" << thisVertex.Pos[2] << endl; faceVert.getUV(UV);
//cout << "Normals: " << thisVertex.Normal[0] << "/" << thisVertex.Normal[1] << "/" << thisVertex.Normal[2] << endl; thisVertex.Uv[0] = UV[0];
//cout << "Bi-Normals: " << thisVertex.BiNormal[0] << "/" << thisVertex.BiNormal[1] << "/" << thisVertex.BiNormal[2] << endl; thisVertex.Uv[1] = UV[1];
//cout << "Bi-Tangents: " << thisVertex.BiTangent[0] << "/" << thisVertex.BiTangent[1] << "/" << thisVertex.BiTangent[2] << endl;
//cout << "UV: " << thisVertex.Uv[0] << "/" << thisVertex.Uv[1] << endl;
}
//MGlobal::displayInfo("Befor Third Loop");
//for (UINT i = 0; i < triangleList.length(); i++) { thisVertex.BoneIndices[0] = vertexWeights[faceVert.vertId()].BoneIndices[0];
// UINT k = 0; thisVertex.BoneIndices[1] = vertexWeights[faceVert.vertId()].BoneIndices[1];
// while (localVertexToGlobalIndex[k] != triangleList[i]) thisVertex.BoneIndices[2] = vertexWeights[faceVert.vertId()].BoneIndices[2];
// k++; thisVertex.BoneIndices[3] = vertexWeights[faceVert.vertId()].BoneIndices[3];
// indexArray.push_back(indexOffset + k);
//} thisVertex.BoneWeights[0] = vertexWeights[faceVert.vertId()].BoneWeights[0];
} thisVertex.BoneWeights[1] = vertexWeights[faceVert.vertId()].BoneWeights[1];
thisVertex.BoneWeights[2] = vertexWeights[faceVert.vertId()].BoneWeights[2];
thisVertex.BoneWeights[3] = vertexWeights[faceVert.vertId()].BoneWeights[3];
std::vector<VertexLayout>::iterator it = std::find(vertexList.begin(), vertexList.end(), thisVertex);
if (it != vertexList.end()) {
localVertexToGlobalIndex.push_back(vertexIndex);
} else {
localVertexToGlobalIndex.push_back(vertexIndex);
vertexList.push_back(thisVertex);
}
//cout << "Pos: " << thisVertex.Pos[0] << "/" << thisVertex.Pos[1] << "/" << thisVertex.Pos[2] << endl;
//cout << "Normals: " << thisVertex.Normal[0] << "/" << thisVertex.Normal[1] << "/" << thisVertex.Normal[2] << endl;
//cout << "Bi-Normals: " << thisVertex.BiNormal[0] << "/" << thisVertex.BiNormal[1] << "/" << thisVertex.BiNormal[2] << endl;
//cout << "Bi-Tangents: " << thisVertex.BiTangent[0] << "/" << thisVertex.BiTangent[1] << "/" << thisVertex.BiTangent[2] << endl;
//cout << "UV: " << thisVertex.Uv[0] << "/" << thisVertex.Uv[1] << endl;
}
//MGlobal::displayInfo("Befor Third Loop");
MGlobal::displayInfo(MString() + "localVertexToGlobalIndex.size(): " + localVertexToGlobalIndex.size());
MGlobal::displayInfo(MString() + "vertexList.size(): " + vertexList.size());
for (unsigned int i = 0; i < triangleList.length(); i++) {
unsigned int k = 0;
MGlobal::displayInfo(MString() + "triangleList[i]: " + triangleList[i]);
if (localVertexToGlobalIndex.size() > 0) {
while (localVertexToGlobalIndex[k] != triangleList[i] && k < localVertexToGlobalIndex.size()) {
MGlobal::displayInfo(MString() + "localVertexToGlobalIndex[k]: " + localVertexToGlobalIndex[k]);
k++;
}
indexList.push_back(indexOffset + k);
}
}
}
} }
Mesh::~Mesh() Mesh::~Mesh()
+47 -6
View File
@@ -16,21 +16,62 @@ public:
float BiNormal[3]; float BiNormal[3];
float BiTangent[3]; float BiTangent[3];
float Uv[2]; float Uv[2];
float BoneIndices[4];
float BoneWeights[4];
virtual void WriteBinary(std::ostream& out) { virtual void WriteBinary(std::ostream& out)
{
out.write((char*)&Pos, sizeof(float) * 3);
out.write((char*)&Normal, sizeof(float) * 3);
out.write((char*)&BiNormal, sizeof(float) * 3);
out.write((char*)&BiTangent, sizeof(float) * 3);
out.write((char*)&Uv, sizeof(float) * 2);
out.write((char*)&BoneIndices, sizeof(float) * 4);
out.write((char*)&BoneWeights, sizeof(float) * 4);
}
} virtual void WriteASCII(std::ostream& out) const
virtual void WriteASCII(std::ostream& out) const { {
out << "New Vertex: " << endl;
out << Pos[0] << " " << Pos[1] << " " << Pos[2] << endl;
out << Normal[0] << " " << Normal[1] << " " << Normal[2] << endl;
out << BiNormal[0] << " " << BiNormal[1] << " " << BiNormal[2] << endl;
out << BiTangent[0] << " " << BiTangent[1] << " " << BiTangent[2] << endl;
out << Uv[0] << " " << Uv[1] << endl;
out << BoneIndices[0] << " " << BoneIndices[1] << " " << BoneIndices[2] << " " << BoneIndices[3] << endl;
out << BoneWeights[0] << " " << BoneWeights[1] << " " << BoneWeights[2] << " " << BoneWeights[3] << endl;
} }
bool operator==(const VertexLayout& right)
{
return
this->Pos[0] == right.Pos[0] && this->Pos[1] == right.Pos[1] && this->Pos[2] == right.Pos[2] &&
this->Normal[0] == right.Normal[0] && this->Normal[1] == right.Normal[1] && this->Normal[2] == right.Normal[2] &&
this->BiNormal[0] == right.BiNormal[0] && this->BiNormal[1] == right.BiNormal[1] && this->BiNormal[2] == right.BiNormal[2] &&
this->BiTangent[0] == right.BiTangent[0] && this->BiTangent[1] == right.BiTangent[1] && this->BiTangent[2] == right.BiTangent[2] &&
this->Uv[0] == right.Uv[0] && this->Uv[1] == right.Uv[1] &&
this->BoneIndices[0] == right.BoneIndices[0] && this->BoneIndices[1] == right.BoneIndices[1] && this->BoneIndices[2] == right.BoneIndices[2] && this->BoneIndices[3] == right.BoneIndices[3] &&
this->BoneWeights[0] == right.BoneWeights[0] && this->BoneWeights[1] == right.BoneWeights[1] && this->BoneWeights[2] == right.BoneWeights[2] && this->BoneWeights[3] == right.BoneWeights[3]
;
}
}; };
class Mesh class Mesh
{ {
public: public:
Mesh(); Mesh();
void GetMeshData(MObject Object); void GetMeshData(MObject Object, std::vector<VertexLayout>& vertexList, std::vector<unsigned int>& indexList);
~Mesh(); ~Mesh();
private:
struct WeightInfo {
float BoneIndices[4] = { 0 };
float BoneWeights[4] = { 0 };
};
std::map<int, WeightInfo> GetWeightData();
}; };
#endif #endif
+14 -2
View File
@@ -20,20 +20,32 @@ public:
MGlobal::displayInfo("WriteToFile::writeToFiles()"); MGlobal::displayInfo("WriteToFile::writeToFiles()");
if (ASCIIFile.is_open()) if (ASCIIFile.is_open())
{ {
MGlobal::displayInfo("Writing to ASCIIfile");
for (unsigned int i = startIndex; i < numOfElementToWrite + startIndex; i++) for (unsigned int i = startIndex; i < numOfElementToWrite + startIndex; i++)
ASCIIFile << toWrite[i] << endl; ASCIIFile << toWrite[i] << endl;
} }
if (binFile.is_open()) if (binFile.is_open())
{ {
MGlobal::displayInfo("Writing to binaryfile");
for (unsigned int i = startIndex; i < numOfElementToWrite + startIndex; i++) for (unsigned int i = startIndex; i < numOfElementToWrite + startIndex; i++)
toWrite[i].WriteBinary(binFile); toWrite[i].WriteBinary(binFile);
} }
} }
void writeToFiles(unsigned int* toWrite, unsigned int numOfElementToWrite = 1, unsigned int startIndex = 0)
{
MGlobal::displayInfo("WriteToFile::writeToFiles()");
if (ASCIIFile.is_open()) {
for (unsigned int i = startIndex; i < numOfElementToWrite + startIndex; i++)
ASCIIFile << toWrite[i] << endl;
}
if (binFile.is_open()) {
binFile.write((char*)toWrite, numOfElementToWrite * sizeof(int));
}
}
void OpenFiles(); void OpenFiles();
void CloseFiles(); void CloseFiles();