MaxScript Help Please! (very basic)

Started by
2 comments, last by Telamon 17 years, 7 months ago
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).

Shedletsky's Bits: A Blog | ROBLOX | Twitter
Time held me green and dying
Though I sang in my chains like the sea...

Advertisement
To sweeten the deal, if you help me I will rating++ and be your friend (or not, as you like).

:-)

Shedletsky's Bits: A Blog | ROBLOX | Twitter
Time held me green and dying
Though I sang in my chains like the sea...

I figured out the function scope thing, I was being obtuse before. Now I don't understand why I get a function undefined error on getUVCoordByVert.

Current exporter script:

MacroScript ExportToRoblox category:"Roblox"(	fn GetGeometry o = (		Superclassof o == Geometryclass and classof o != TargetObject 	)		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	)		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) (getUVCoordByVert tmesh 1 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		)	))


Error:

-- Error occurred in V loop; filename: C:\Program Files\Autodesk\3dsMax8\UI\MacroScripts\Roblox-ExportToRoblox.mcr; position: 567--  Frame:--   V: 1--   getUVCoordByVert: undefined--   called in anonymous codeblock--  Frame:--   out_name: "C:\Documents and Settings\John Shedletsky\Desktop\girl.mesh"--   num_verts: 190--   GetGeometry: GetGeometry()--   tmesh: TriMesh--   out_file: <File:C:\Documents and Settings\John Shedletsky\Desktop\girl.mesh>--   num_faces: 148--   Obj: $Cylinder01>> MAXScript MacroScript Error Exception: -- Type error: Call needs function or class, got: undefined <<

Shedletsky's Bits: A Blog | ROBLOX | Twitter
Time held me green and dying
Though I sang in my chains like the sea...

I'm pretty sure nothing has changed, but now the sonabitch seems to be working.

Shedletsky's Bits: A Blog | ROBLOX | Twitter
Time held me green and dying
Though I sang in my chains like the sea...

This topic is closed to new replies.

Advertisement