How To > Read and Write Geometry Data > Output Geometry Data To Text File - Part Two |
This is the second part of the Output Geometry Data tutorial. We will enhance the script developed in Part One to support smoothing groups, material ids, and edge visibility.
We define a macroScript that will be called ExportMesh . 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 will need a filter function to allow the picking of only specific object types. In this case, the function will return true only when the superclass of the object passed as parameter is GeometryClass and the class of the passed object is not TargetObject (TargetObject is the only Geometry object without a mesh).
Now, we can let the user pick a scene object and apply the filter to all objects below the mouse pointer. As you will note, the mouse pointer will allow scene objects to be picked only when their class matches the classes defined in the filter function.
If the user picked a valid object (in other words he did not right-click to cancel and did not press Esc ), we will execute the rest of the script. Otherwise, we will skip the following lines.
This is the same as the Part One code. We get the mesh from the picked object.
Instead of providing a hard-coded path, we let the user type in any file name.
Standard Open and Save File Dialogs
If the user specified a valid name (in other words he did not press Cancel), we continue with the execution.
out_file = createfileout_name num_verts = tmesh.numverts num_faces = tmesh.numfaces format "%,%\n" num_verts num_faces to:out_file
Just like in Part One, we create a new file and output the vertex and face count
Then we export the vertices just like in the compact version of the Part One script.
In the face loop, we will export some more data this time. Each face has a smoothing group value, a material id value, and three edge visibility values. We will export all of them in a single pass.
Then, we get the smoothing group value from the same face,
and the edge visibility for each of the three face edges.
Editable_Mesh : GeometryClass and TriMesh : Value
Now, that we have all the values, we output all of them in a single line. We add a new line character to the end of the line – this will give us a single line for each face.
To test the script, select Tools > Evaluate from the MAXScript Editor’s menu or alternatively, press Ctrl+E. Then, customize a toolbar and drag the script from the "HowTo" category. Press the button and pick a geometry object from the scene. You can pick any geometry object like Patch, NURBS, procedural object, and others.
Now, that we have the enhanced output, we can go on and enhance the corresponding input script to read back the new data format and create a new scene object with all the face properties.
Later, you can try to extend the script yourself by adding, for example, texture coordinates support.
How To ... Read Geometry Data From Text File - Part One