How To - Practical Examples > How To ... Read/Write Geometry Data From/To Text File >

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

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.

Related Topics:

Defining MacroScripts

Picking Scene Nodes By Hit

NATURAL LANGUAGE
Load the script from Part One.
Create a filter function for geometry objects.
Let the user pick a scene object and apply the filter function.
If the user picked a valid object, get a save file dialog and ask for a target name.
If the file name is valid, export the vertices.
In the face export code, collect the smoothing groups values, material ids and edge visibility.
Output the collected values as a single line.
Close the file.
Open for review.

MAXSCRIPT
    macroScript ExportMesh category: "HowTo"
    (
    fn GetGeometry o = (
     Superclassof o == Geometryclass and classof o != TargetObject )
    obj = pickobject filter:GetGeometry
    if isValidNode obj then
    (
     tmesh =snapshotAsMesh obj
     out_name = GetSaveFileName()
     if out_name != undefined then
     (
      out_file = createfile out_name
      num_verts = tmesh.numverts
      num_faces = tmesh.numfaces
      format "%,%\n" num_verts num_faces to:out_file
      for v = 1 to num_verts do
       format "%," (getVert tmesh v) to:out_file
      format "\n" to:out_file
      for f = 1 to num_faces do
      (
       face = getFace tmesh f
       sgroup = getFaceSmoothGroup tmesh f
       matid = getFaceMatId tmesh f
       edge1 = getEdgeVis tmesh f 1
       edge2 = getEdgeVis tmesh f 2
       edge3 = getEdgeVis tmesh f 3
       format "%,%,%,%,%,%\n" face sgroup matid edge1 edge2 edge3 to:out_file
      )
      close out_file
      edit out_name
     )--end if
    )--end if
    )--end macroscript

## Step-By-Step

    MacroScript ExportMesh category:"HowTo" (

We define a macroScript which 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.

Defining Macro Scripts

    fn GetGeometry o = ( Superclassof o == Geometryclass and classof o != TargetObject )

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).

Defining Custom Functions

    obj = pickobject filter:GetGeometry

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 classed defined in the filter function.

Picking Scene Nodes By Hit

    if isValidNode obj then (

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.

isValidNode

    tmesh = snapshotAsMesh obj

This is the same as the Part One code. We get the mesh from the picked object.

    out_name = GetSaveFileName()

Instead of providing a hard-coded path, we let the user type in any file name.

Standard Open and Save File Dialogs

    if out_name != undefined then (

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

    for v = 1 to num_verts do format "%," (getVert tmesh v) to:out_file format "\n" to:out_file

Then we export the vertices just like in the compact version of the Part One script.

    for f = 1 to num_faces do (

In the face loop, we will export some more data this time. Each face has a smoothing group value, a material id value, and 3 edge visibility values. We will export all of them in a single pass.

    face = getFace tmesh f

First we get the face.

    sgroup = getFaceSmoothGroup tmesh f

Then we get the smoothing group value from the same face...

    matid = getFaceMatId tmesh f

...then the material id value...

    edge1 = getEdgeVis tmesh f 1 edge2 = getEdgeVis tmesh f 2 edge3 = getEdgeVis tmesh f 3

...and the edge visibility for each of the 3 face edges.

Editable_Mesh : GeometryClass and TriMesh : Value

    format "%,%,%,%,%,%\n" face sgroup matid edge1 edge2 edge3 to:out_file

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.

FileStream Values

    ) close out_file

When the face loop is ready, we close the output file...

    edit out_name

and open the new file to check the result.

    )--end if )--end if )--end macroscript

## Using the Script

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 etc.

SAMPLE OUTPUT OF A CUBE:
    8,12
    [-50,-50,0],[50,-50,0],[-50,50,0],[50,50,0],[-50,-50,100],[50,-50,100],[-50,50,100],[50,50,100],
    [1,3,4],2,2,true,true,false
    [4,2,1],2,2,true,true,false
    [5,6,8],4,1,true,true,false
    [8,7,5],4,1,true,true,false
    [1,2,6],8,5,true,true,false
    [6,5,1],8,5,true,true,false
    [2,4,8],16,4,true,true,false
    [8,6,2],16,4,true,true,false
    [4,3,7],32,6,true,true,false
    [7,8,4],32,6,true,true,false
    [3,1,5],64,3,true,true,false
    [5,7,3],64,3,true,true,false

## Where to go from here

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.

In a later step, you could try to extend the script yourself by adding, for example, texture coordinates support.

Back to

"How To" Tutorials Index Page

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

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

Next

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