How To > Read and Write Geometry Data > 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.
Editable_Mesh : GeometryClass and TriMesh : Value
In the beginning, we define two empty arrays to collect the vertex and face data into.
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.
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.
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.
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.
Using the same readValue method, we read the next value from the file. The value will be stored in the num_faces variable.
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.
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.
Using the readValue method, we read Point3 values from the file. We store them in a user variable 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.
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.
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...
...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.
...and finally construct a new mesh by providing the vertex and face arrays collected from the file.
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.
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.
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.
How To ... Output Geometry Data To Text File - Part One