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
We define a macroScript which 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.
We define the arrays to hold the vertex and face arrays like in Part One...
...but we also define 3 more arrays to store smoothing groups, material ids and edge visibility.
Instead of reading a fixed name, we will let the user pick a file name.
Standard Open and Save File Dialogs
As long as the file name picked is not undefined (in other words the user did not press Cancel), we 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:
We read the smoothing group value and the material id value from the file and append to the arrays.
Also, we read the 3 edge visibility values.
Editable_Mesh : GeometryClass and TriMesh : Value
In order to store the edge visibility for later application, we create a new array and append it to our edge 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.
To do so, we start a new loop...
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.
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 3 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
Finally, we have to update the internal mesh caches to get the changes reflected in the scene. This is a very important step!
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 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.
If you have managed to implement 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...
How To ... Output Geometry Data To Text File - Part Two