How To ... Read Geometry Data From Text File - Part One

This is a very basic example of importing mesh geometry data from an external text file created in the Output Geometry Data – Part One tutorial. The file contains only vertex and face lists.

Related Topics:

FileStream Values

Editable_Mesh : GeometryClass and TriMesh : Value

Natural Language:

MAXScript

vert_array = #()

face_array = #()

in_name = ((GetDir #export)+"/testmesh.dat")

in_file = openFile in_name

if in_file != undefined then

(

num_verts = readValue in_file

num_faces = readValue in_file

vert_array.count = num_verts

face_array.count = num_faces

for v = 1 to num_verts do

(

vert = readValue in_file

vert_array[v] = vert

)

for f = 1 to num_faces do

(

face = readValue in_file

face_array[f] = face

)

close in_file

new_mesh = mesh vertices:vert_array faces:face_array

)

 

Step-By-Step

vert_array = #()

face_array = #()

In the beginning, we define two emptry arrays to collect the vertex and face data into.

Array Values

in_name = ((GetDir #export)+"/testmesh.dat")

Then we define the imput file name just like in the Output Geometry – Part One tutorial. This is currently a hard-coded name in both the output and input script. We will let the user pick a file from an open file dialog in Part Two of this tutorial.

in_file = openFile in_name

Using the file name, we try to open the file for input. If the specified path exists, a fileStream value will be stored in the in_file variable. If the path does not exist, the value undefined will be stored instead.

FileStream Values

 

if in_file != undefined then

(

Knowing the above, we check to see if the file is valid or not. If the value isn’t undefined, the file has been opened successfully. If not, all of the next lines enclosed in the brackets will be ignored.

If Expression

Undefined Value

num_verts = readValue in_file

The file will contain the number of vertices and faces separated by a comma in the first line. Using the readValue method, we read the first value from the file. The value will be stored in the num_verts variable.

FileStream Values

 

num_faces = readValue in_file

Using the same readValue method, we read the next value from the file. The value will be stored in the num_faces variable.

vert_array.count = num_verts

face_array.count = num_faces

 

Now that we know the number of vertices and faces stored in the file, we initialize both arrays to those numbers. This will reserve enough memory for both arrays.

for v = 1 to num_verts do

(

Then we can read the vertex positions. We start a new loop that will change the variable v from 1 to the number of vertices stored in the file.

For Loop

 

vert = readValue in_file

Using the readValue method, we read Point3 values from the file. We store them in a user variable vert...

vert_array[v] = vert

...and then assign the value to the v-th position of the vert_array array.

)

At this point, we have all vertex positions read and put into the array.

for f = 1 to num_faces do

(

Using the num_faces variable, we can also read as many face definitions. We start another loop that will change the variable f from 1 to the number of faces stored in the file.

face = readValue in_file

Using the readValue method, we read Point3 values from the file specifying the vertex indices defining the face. We store them in a user variable face...

face_array[f] = face

...and then assign the value to the f-th position of the face_array array.

)

At this point, we have all face definitions read and put into the array.

close in_file

We can close the file...

new_mesh = mesh vertices:vert_array faces:face_array

...and finally construct a new mesh by providing the vertex and face arrays collected from the file.

Editable_Mesh : GeometryClass and TriMesh : Value

)

Using the Script

To test the script, use the Output script first to create a file. Then open this script, select File>Evaluate from the MAXScript Editor’s menu or alternatively, press Ctrl+E. The object exported to the file by the other script should appear in the scene. Note that it will have to correct position, but its pivot point will be at the world origin as we did not export local coordinates and object transformations.

Optimizing the code

As already mentioned, this is the basic code for geometry input. Still it can be made even more compact. Below is a shorter version of the same code. Please compare the two scripts and see why both of them do the same thing although expressed slightly differently.

vert_array = #()

face_array = #()

in_file = openFile ((GetDir #export)+"/testmesh.dat")

if in_file != undefined then

(

num_verts = readValue in_file

num_faces = readValue in_file

vert_array.count = num_verts

face_array.count = num_faces

for v = 1 to num_verts do vert_array[v] = (readValue in_file)

for f = 1 to num_faces do face_array[f] = (readValue in_file)

close in_file

new_mesh = mesh vertices:vert_array faces:face_array

)

Where to go from here

In Part Two of the Geometry Input tutorial, we will make the code more robust and add some more features to reflect the changes made to the Geometry Output Part Two tutorial.

Back to

"How To" Tutorials Index Page

How To ... Output Geometry Data To Text File - Part One

 

Next

How To ... Output Geometry Data To Text File - Part Two

How To ... Read Geometry Data From Text File - Part Two