PDA

View Full Version : New to maxscript, writing exporter


CyberSlag5k
03-12-2005, 03:21 AM
I'm attempting to write an exporter for max that will output mesh data (vertices, faces, normals, textures, materials, and keyframes) to a file in binary. I don't have any experience with maxscript, though I am a programmer by trade.

I'm just starting out with the script, and I'm getting an error when trying to output the list of vertices. An error I don't quite understand:

file = fopen "C:\\stuff.dat" "wb"

for i = 1 to tmesh.numverts do (

temp = getVert tmesh v

tempX = temp.x : Float

tempY = temp.y : Float

tempZ = temp.z : Float

WriteFloat file tempX

WriteFloat file tempy

WriteFloat file tempz

)

-- Syntax error: at ), expected <factor>

-- In line: )

Can somebody tell me what I've dont wrong here?

And how do I signal the text form that I am no longer commenting anything (so it will stop printing in red).

Thanks in advance.

Bobo
03-12-2005, 05:02 PM
I'm attempting to write an exporter for max that will output mesh data (vertices, faces, normals, textures, materials, and keyframes) to a file in binary. I don't have any experience with maxscript, though I am a programmer by trade.

I'm just starting out with the script, and I'm getting an error when trying to output the list of vertices. An error I don't quite understand:

file = fopen "C:\\stuff.dat" "wb"

for i = 1 to tmesh.numverts do (

temp = getVert tmesh v

tempX = temp.x : Float

tempY = temp.y : Float

tempZ = temp.z : Float

WriteFloat file tempX

WriteFloat file tempy

WriteFloat file tempz

)

-- Syntax error: at ), expected <factor>

-- In line: )

Can somebody tell me what I've dont wrong here?

And how do I signal the text form that I am no longer commenting anything (so it will stop printing in red).

Thanks in advance.


tempX = temp.x : Float

This does not sound right to me. In MAXScript, you never ever have to specify any types.
The value in tempX would be float anyway, because the 3 elements of a Point3 vector ARE floats.
I think you have confused the MAXScript parser with a syntax error it would not catch correctly....


theObj = pickObject() --pick a scene object with the mouse
if superclassof theObj == GeometryClass do --if really picked a valid geometry
(
tmesh = snapshotasmesh theObj --snapshot the TriMesh of the object
file = fopen "C:\\stuff.dat" "wb" --open the binary file for writing...
numVerts = tmesh.numverts --get the number of vertices
for v = 1 to numVerts do ( --loop through the vertices
temp = getVert tmesh v --get the v-th vertex
WriteFloat file temp.X --write the 3 components
WriteFloat file temp.y
WriteFloat file temp.z
)
fclose file --close the binary file


file = fopen "C:\\stuff.dat" "rb" --open the binary file
theVerts = #() --initialize an array to store the vertices
for v = 1 to numVerts do --loop through the vertices using the same count as above
append theVerts [ReadFloat file,ReadFloat file,ReadFloat file] --collect the values in the array
theMesh = mesh vertices:theVerts faces:#() --create a mesh with the vertices and no faces
select theMesh --select the mesh
theMesh.vertexTicks = true --and show the vertices as ticks in the viewport!
fclose file

)--end if theObj is valid



And to reset the formatting in your post, use the upper-left icon in the message editor...
"Remove Text Formatting"

Bobo
03-12-2005, 05:33 PM
Some more comments regarding my example:

*SnapshotAsMesh returns the WORLD STATE of the mesh, including any SpaceWarp influences. The vertex positions you are writing/reading would be in WORLD SPACE, so you might want to transform it back into object space if required.

*Writing out the number of vertices and faces as the first values to the file would be a good idea to be able to read back the file later... Right now, my example just reused the number from the export to do the import...

CyberSlag5k
03-13-2005, 01:49 AM
Thank you for your reply, Bobo. My exporter now runs and creates a file. I don't think it's working exactly the way I want it to. The shape my program draws is garbage, though I'm not sure whether that's the program or the exporter (still working that out).

In trying to find the source of the problem, I thought I'd write the file out in text so I could ensure that the proper values were being exported. So I just changed the "wb" to a "wt" for the fopen call. The thing is, the file it creates is the same size as the file the binary creates and both open as garbage in both notepad and MS word. This has me thinking there's a problem with the exporter.

Here's what I have now:


theObj = pickObject()

if superclassof theObj == GeometryClass do

(

tmesh = snapshotasmesh theObj

file = fopen "C:\\stuff2.dat" "wb"

num_verts = tmesh.numverts

num_faces = tmesh.numfaces

WriteLong file num_verts

WriteLong file num_faces

for v = 1 to num_verts do

(

temp = getVert tmesh v

WriteFloat file temp.x

WriteFloat file temp.y

WriteFloat file temp.z

)



for f = 1 to num_faces do

(

temp2 = getFace tmesh f

WriteLong file temp2.x

WriteLong file temp2.y

WriteLong file temp2.z

)

fclose file

)


I wasn't sure if I could reuse v and temp so I used f and temp2. I don't see why I couldn't but, ya never know with loose typing bindings like these.

Anyway, does everything look ok? Is there something wrong that would explain the file being written the same in both binary and ascii? They should at least be different sizes, I would think.

Also, I'm not sure what you mean about converting the vertex positions to object space. Is an object in WORLD STATE drawn with positions relative to the origin in max when they were drawn, and object drawn in OBJECT STATE drawn with positions relative to an origin as close as possible to the center of the object, or something? The latter sounds much more appealing to me, if that is really the case. How would I switch the object over to the object state, then?

Thanks again!

Bobo
03-13-2005, 02:14 AM
Looks like it is correct.

I wrote this quick importer and read the data saved by your exporter back into Max, getting the exactly same object:


file = fopen "C:\\stuff2.dat" "rb"
num_verts = ReadLong file
num_faces = ReadLong file
theVerts = #()
theFaces = #()
for v = 1 to num_verts do
append theVerts [(ReadFloat file),(ReadFloat file),(ReadFloat file)]
for f = 1 to num_faces do
append theFaces [(ReadLong file),(ReadLong file),(ReadLong file)]
fclose file
theMesh = mesh vertices:theVerts faces:theFaces
select theMesh

So I guess it is correct...


And yes, you can reuse the Temp variable in the second loop.
For loops define a local scope, so a variable declared inside the loop is implicitly local (unless there is already a global Temp declared. so it is a good practice to declare variables as explicitly local)


Cheers,
Bobo

CyberSlag5k
03-13-2005, 03:48 AM
But shouldn't opening the file with "wt" export the data in ASCII? I attempted this and it worte a file the exact same size that notepad couldn't read...

Anyway it's comforting to know that the exporter is in working order. Now I just need to find out where the bug is in my program (which shouldn't take too long, it's small).

And yeah, I should have known better about the variable thing. Regardless of if maxscript lets you re-type bind a variable, it is obviously going out of scope when it leaves the for loop.

Thanks, Bobo!

CGTalk Moderation
03-13-2005, 03:48 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.