Monday, October 19, 2009

Maya Geometry Exporter

Maya object exporter I wrote. Getting the vertex order right for components was frustrating. Maya's a little inconsistent in this regard. Some MEL functions list vertex translation correctly but then the adjacent function for vertex normals lists them arbitrarily and so on. Oh well. Currently I'm using this to get models into an OpenGL game. If you'd like to use the code feel free. It's a little hacky and slow writing large models. I'll clean it up a bit later and add bones for articulated models.


// ******** global proc AOF() ******** //
// Simply opens export dialog box //
// //
/////////////////////////////////////////
global proc AOF()
{
fileBrowserDialog -m 1 -fc "ExportAOF" -an "Save AOF" -om "SaveAs";
}

// ***** global proc ExportAOF() ***** //
// Handles object parsing //
// //
/////////////////////////////////////////

global proc ExportAOF(string $filename, string $fileType)
{
//Setup file name
$fileType = "\.aof";
string $filename = $filename + $fileType;

//Open file for writing
$fileHandle = `fopen $filename "w"`;

//Setup Variables
string $selection[] = `ls -sl`;
int $m, $n;
//Process geometry
fwrite $fileHandle "#Vertex Data Format: [Position X 3] [Normal X 3] [UV X 2]\n";
PolySelectConvert 1;//convert selection to faces
string $faces[] = `ls -sl -fl`;

fwrite $fileHandle (`size $faces`+"\n");

for($m = 0; $m < `size $faces`; $m++)
{
select -r $faces[$m];

string $faceToVertex[]=`polyInfo -faceToVertex`;
string $tokens[];
tokenize $faceToVertex[0] " :\n\r" $tokens;
int $vertexOrder[];
clear $vertexOrder;
int $t;

for ($t = 2; $t < `size $tokens`; $t++)
{
$vertexOrder[`size $vertexOrder`] = $tokens[$t];
}

string $fverts[]={($selection[0]+"\.vtx["+$vertexOrder[0]+"]"),
($selection[0]+"\.vtx["+$vertexOrder[1]+"]"),
($selection[0]+"\.vtx["+$vertexOrder[2]+"]")
};

PolySelectConvert 1;
select -r $faces[$m];
for($j = 0; $j < `size $fverts`; $j++)
{
//vertex position
float $vpos[] = `xform -q -ws -t $fverts[$j]`;

//normal position
float $normal[3];
string $vertexFace[]= `polyListComponentConversion -fv -tvf $fverts[$j]`;
$vertexFace= `filterExpand -sm 70 -ex true $vertexFace`;

//check to see which face the vertex is associated with
//and retrieve that normal
for($n = 0; $n < `size $vertexFace`; $n++)
{
string $token[];
tokenize $vertexFace[$n] " []\r\n" $token;
if($token[2] == $m)
{
$normal=`polyNormalPerVertex -q -xyz $vertexFace[$n]`;
}
}
//UV position
select -r $faces[$m];
PolySelectConvert 4;
string $uvs[]=`ls -sl -fl`;
string $testUV;
float $UV[];
for($testUV in $uvs)
{
string $test[]=`polyListComponentConversion -fuv -tv $testUV`;
if($test[0]==$fverts[$j])
{
$UV=`polyEditUV -q $testUV`;
}
}
//Write out data
fwrite $fileHandle ($vpos[0]+" "+$vpos[1]+" "+$vpos[2]+" "+
$normal[0]+" "+$normal[1]+" "+$normal[2]+" "+
$UV[0]+" "+$UV[1]+"\n");
}
}
fclose $fileHandle;
}

No comments:

Post a Comment