I've been learning MaxScript over the past two days and I'm thrilled that it is so great because it means that I don't have to touch the MAX SDK in order to write an exporter for our game. The first snag I ran into was exporting texture coords, since as I understand it max keeps per-face coords instead of per-vertex coords for textures (somewhat questionable design choice). I found a snippet of code from the internets that I think solves this. My real problem is that I don't know how to use functions inside of a MacroScript (I said it was basic)... I am erstwhile searching for example code on the net, but an initial search has proven fruitless. If someone could take a quick look at this, it would probably save me at least an hour.
This is a working exporter for verts, normals and faces.
MacroScript ExportToRoblox category:"Roblox"
(
fn GetGeometry o = (
Superclassof o == Geometryclass and classof o != TargetObject )
obj = pickobject filter:GetGeometry
if isValidNode obj then
(
tmesh = snapshotAsMesh obj
out_name = GetSaveFileName()
if out_name != undefined then
(
out_file = createfile out_name
num_verts = tmesh.numverts
num_faces = tmesh.numfaces
format "%,%\n" num_verts num_faces to:out_file
for v = 1 to num_verts do
format "%%," (getVert tmesh v) (getNormal tmesh v) to:out_file
format "\n" to:out_file
for f = 1 to num_faces do
(
face = getFace tmesh f
format "%," face to:out_file
)
close out_file
edit out_name
)
)
)
I want to add this function and call it from the line of code where I am writing the per-vertex info to the file:
fn getUVCoordByVert theMesh mapChannel vertIndex = (
local faces = (meshOp.getFacesUsingVert theMesh vertIndex) as array
mapVertices = #()
for theFace in faces do
(
theFaceDef = getFace theMesh theFace
theMapFace = meshOp.getMapFace theMesh mapChannel theFace
if theFaceDef.x == vertIndex then theMapVert = theMapFace.x as integer
if theFaceDef.y == vertIndex then theMapVert = theMapFace.y as integer
if theFaceDef.z == vertIndex then theMapVert = theMapFace.z as integer
if findItem mapVertices theMapVert == 0 do append mapVertices theMapVert
)
return mapVertices
)
The source of my confusion is that I don't know how the MacroScript decides what code to run. If I add a 2nd function inside the scope of the MacroScript clause, it doesn't work. Is the MaxScript interpreter 1-pass or multi-pass (i.e. do I need to have called functions appearing above callee functions in the source like in C, or is it like C# where it doesn't matter).