PDA

View Full Version : Exporting vertex data



ta0soft
02-28-2011, 08:00 PM
Hello, I'm writing a simple script that exports raw vertex data to file. I need to loop through each vertex in the mesh and get the Vertex position, Normal direction, and UV coordinates, then write them to the file.

I've managed to export the vertex position, but I can't figure out how to retrieve the normal/uv data. I know how to use a vmap object agent to retrieve the uv's for each point in the mesh, but how would I use it to get the normal direction?

Here's my script so far, any advice would be greatly appreciated!



main
{
selmode(USER);

pCount = polycount();
if (pCount[4] < 1) // Selected polygons that contain three points
{
info("No 3 sided polygons selected/found");
return;
}

vMapObj = VMap(VMTEXTURE); // VMap object agent
if (vMapObj == nil)
{
info("No UV map found");
return;
}

fileName = "";

reqbegin("Ta0 Exporter");
reqsize(200, 200);

c1 = ctlfilename("Filename", fileName);
ctlposition(c1, 6, 6);

return if !reqpost();

fileName = getvalue(c1);

if (fileName == "")
{
info("Invalid filename");
return;
}

editbegin();

for (i = 1; i <= pointcount(); i++)
{
if(vMapObj.isMapped(points[i]))
{
values = vMapObj.getValue(points[i]);
info(values);
}
}

file = File(fileName, "w");

file.writeln("[Mesh]);

foreach (poly, polygons) // Loop through selected polygons
{
polyData = nil;
pointIDs = nil;
pointData = nil;

polyData = polyinfo(poly);

if (polyData[1] != surfaceName)
{
surfaceName = polyData[1];
file.writeln("Material=" + surfaceName);
}

for (i = 2; i <= size(polyData); i++)
{
// Add this Point ID to our arrays of IDs
pointIDs += polyData[i];
pointData += pointinfo(polyData[i]);
}

for (i = 1; i <= size(pointIDs); i++) // Loop through each point
{
file.writeln("Vertex=" + pointData[i]); // Write position to file
}
}

file.close();

editend();

info("File saved successfully");

reqend();
}

Dodgy
03-01-2011, 05:43 AM
Do you mean a normal from a Vmap, or just the point's normal from the geometry? The first should be just reading from a vmap, the other.... I'm not sure there's a simple way to get a point's normal other than by calculating it from the surrounding polygons.

ta0soft
03-01-2011, 10:11 AM
Well, what I'm trying to do is replicate an SMDExport.p plugin I used in lw9.2 but it doesn't work in lw9.5+ :(

SMD files contain raw uncompressed vertex data (position/normal/uv), so I figured if someone made a plugin that exports SMD, it wouldn't be hard to remake it using lscript.

I don't think the normals are stored in a vmap, lightwave calculates them based on the surface smoothness, but they must be stored somewhere. I just have no idea how to retrieve/calculate them using lscript.

I have more experience with c++ than I do with lscript, would it be better to make a plugin instead?

ta0soft
03-01-2011, 12:26 PM
I found the function polynormal(), but that only returns the surface normal for a specified polygon. There has to be a way to get the normals for individual vertices but I can't figure it out :(

warmiak
03-05-2011, 09:05 PM
There is no way to get at vertex normals ... in my plugin I had to calculate them myself based on the surface smoothness among other things.

As far as UVs ... it is a bit more complicated than just reading them from a VMAP.
You need to take into acount the fact that some of them can be discontinuous and you need to first read from the discontinuous VMAP and if there is a valid value present use that, otherwise use the original VMAP.

Furthermore, anytime you have a discontinuous uv or you have a sharp edge, you need to duplicate your vertices because that's the only way to handle sharp edges as well as discontinuous uvs.

ta0soft
03-06-2011, 01:06 AM
Thanks for the advice, there really should be an lscript function for getting point normals.

normalbaker.ls shows how to calculate them but it doesn't take the surface smoothness into account. DirectX has a few helper functions for calculating normals but your plugin works perfectly with smooth models.

I like SMD format because the files are easy to read and edit. I'm writing a game engine that supports SMD among other formats, and I model everything in lightwave so your plugin has been a godsend. I just wish it worked in lw10 -_-