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

How To > Read and Write Geometry Data > Read Geometry Data From Text File - Part Two

This is Part Two of the Geometry Import tutorial. It will be able to read the extended data written by the output script developed in How To ... Output Geometry Data To Text File - Part Two.

NATURAL LANGUAGE

Package as macroScript to let the user place the script on a toolbar, QuadMenu, keyboard shortcut, or Menu.

Create three more arrays to store smoothing groups, material ids, and edge visibility.

Let the user select a file to open.

If the user selected a valid file name, go on with the script.

After reading the vertices and faces as in Part One, also read the smoothing groups, material ids, and edge visibility.

Close the file.

Create a new mesh out of the collected vertices and faces.

Apply the smoothing groups, material ids, and edge visibility values to all faces.

Update the mesh to reflect the changes.

MAXSCRIPT

macroScript ImportMesh category: "HowTo"
(
 vert_array = #()
 face_array = #()
 sgroup_array = #()
 matid_array = #()
 edge_array = #()
 in_name = getOpenFileName()
 if in_name != undefined then
 (
  in_file = openFile in_name
  if in_file != undefined then
  (
   num_verts = readValue in_file
   num_faces = readValue in_file
   for v = 1 to num_verts do
    append vert_array (readValue in_file)
   for f = 1 to num_faces do
   (
    append face_array (readValue in_file)
    append sgroup_array (readValue in_file)
    append matid_array (readValue in_file)
    edge1 = readValue in_file
    edge2 = readValue in_file
    edge3 = readValue in_file
    append edge_array #(edge1, edge2, edge3)
   )
   close in_file
   new_mesh = mesh vertices:vert_array faces:face_array
   for f = 1 to num_faces do
   (
    setFaceSmoothGroup new_mesh f sgroup_array[f]
    setFaceMatID new_mesh f matid_array[f]
    setEdgeVis new_mesh f 1 edge_array[f][1]
    setEdgeVis new_mesh f 2 edge_array[f][2]
    setEdgeVis new_mesh f 3 edge_array[f][3]
   )
   update new_mesh
  )--end if
 )--end if
)--end macroscript

Step-By-Step

macroScript ImportMesh category: "HowTo"
(

We define a macroScript that will be called ImportMesh . To use the script, you can go to Customize and drag the script from the category "HowTo" to a toolbar, a menu, a quad menu, or assign to a keyboard shortcut.

Defining Macro Scripts

vert_array = #()
face_array = #()

We define the arrays to hold the vertex and face arrays like in Part One,

sgroup_array = #()
matid_array = #()
edge_array = #()

but we also define three more arrays to store smoothing groups, material ids, and edge visibility.

Array Values

in_name = getOpenFileName()

Instead of reading a fixed name, we will let the user pick a file name.

Standard Open and Save File Dialogs

if in_name != undefined then
(

As long as the file name picked is not undefined (the user did not press Cancel), we can continue.

in_file = openFile in_name
if in_file != undefined then
(
num_verts = readValue in_file
num_faces = readValue in_file
for v = 1 to num_verts do
append vert_array (readValue in_file)
for f = 1 to num_faces do
(
append face_array (readValue in_file)

This part of the reading code is identical to Part One, but we also have to read the additional data:

append sgroup_array (readValue in_file)
append matid_array (readValue in_file)

We read the smoothing group value and the material id value from the file and append to the arrays.

edge1 = readValue in_file
edge2 = readValue in_file
edge3 = readValue in_file

Also, we read the three edge visibility values.

Editable_Mesh : GeometryClass and TriMesh : Value

append edge_array #(edge1, edge2, edge3)

To store the edge visibility for later application, we create a new array and append it to our edge array.

)
close in_file
new_mesh = mesh vertices:vert_array faces:face_array

At this point, we have the mesh just like in Part One. Now, we have to apply the additional data to the resulting mesh.

for f = 1 to num_faces do
(

To do so, we start a new loop,

setFaceSmoothGroup new_mesh f sgroup_array[f]

Then, we set the smoothing group of the f-th face of the new_mesh object to the value stored in the sgroup_array at the f-th position.

setFaceMatID new_mesh f matid_array[f]

We set the material id of the f-th face of the new_mesh object to the value stored in the matid_array at the f-th position.

setEdgeVis new_mesh f 1 edge_array[f][1]
setEdgeVis new_mesh f 2 edge_array[f][2]
setEdgeVis new_mesh f 3 edge_array[f][3]

And, we set the edge visibility of each of the three edges in the f-th face of the new_mesh object to the values stored in the sub-array at f-th position in the edge_array

)
update new_mesh

Finally, we have to update the internal mesh caches to get the changes reflected in the scene. This is a very important step.

)
)
)

Using the Script

To test the script, use the Output script first to create a file. Then open this script, select Tools > Evaluate from the MAXScript Editor’s menu or alternatively, press Ctrl+E. The object exported to the file by the other script must 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.

Where to go from here

If you have implemented texture coordinates export in your Geometry Output script as suggested in the Part Two tutorial, you might also want to alter this script to read the additional data.

Back to

"How To" Tutorials Index Page

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

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

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