Difference between revisions of "Scripting Export Mesh"

From TexGen
Jump to navigationJump to search
Line 24: Line 24:
 
textile = GetTextile()
 
textile = GetTextile()
 
</pre>
 
</pre>
The variable <code>textile</code> now contains an instance of the [http://texgen.sourceforge.net/api/class_tex_gen_1_1_c_textile.html CTextile] class representing the textile model.
+
The variable <code>textile</code> now contains an instance of the [http://texgen.sourceforge.net/api/class_tex_gen_1_1_c_textile.html CTextile] class representing the textile model. Let's now extract a mesh for this textile, fortunately TexGen has some built-in functionality to do this:
 +
 
 +
<pre>
 +
# Create a mesh instance
 +
mesh = CMesh()
 +
# Add the volume of the textile to the mesh
 +
textile.AddVolumeToMesh(mesh)
 +
</pre>
 +
 
 +
We declare <code>mesh</code> to be an instance of [http://texgen.sourceforge.net/api/class_tex_gen_1_1_c_mesh.html CMesh] and then use it to store a volume mesh of the textile. So with just a few lines of code we now have a volume mesh of the textile stored in memory. The problem we are now faced with is how do we transfer this mesh from memory to the hard disk in the file format that we want so that it can be understood by our software of choice? Read on to find out.
 +
 
 +
==Defining the file format==
 +
 
 +
In this example I will be showing you how to save to the [http://www.vtk.org/pdf/file-formats.pdf VTK legacy file format]. I have chosen this file format because it is simple and well documented. Unfortunately, in a lot of cases, file formats are poorly documented (if at all) and are complex due to poor design.
 +
 
 +
Note that TexGen already has a function to export to the VTK XML file format along with a number of other common file formats, and can be called in a single line of code like so: <code>mesh.SaveToVTK("mesh.vtu")</code>. But in this tutorial I want to explain how to actually write the data out line by line so that you can hopefully translate that to any other file format of your choice.
 +
 
 +
==Writing the data to file==
 +
 
 +
Now that we have decide what format the data will be written in, let's get down to actually writing it. First we will open a file for writing:
 +
file = open("mesh.vtk", "w")
 +
See the [http://docs.python.org/tut/node9.html#SECTION009200000000000000000 Python tutorial] for more details on reading/writing to files from Python. Next we need to write the header:
 +
<pre>
 +
file.write("# vtk DataFile Version 2.0\n")
 +
file.write("Textile mesh data\n")
 +
file.write("ASCII\n")
 +
</pre>
 +
As defined in the [http://www.vtk.org/pdf/file-formats.pdf VTK legacy file format documentation].

Revision as of 09:51, 15 April 2008

Export a mesh

In the Scripting Guide section you learnt how to create TexGen models from Python scripts. In this tutorial I will explain how to get a volume mesh of the textile and save it to an arbitrary file format. This is a fairly common task but unfortunately one that must often be repeated due to the fact that there is very little standardisation on the file format for meshes. Each software package uses its own mesh file format and so we must learn to read and write to these different formats. Fortunately the Python language is ideal for performing these tasks with relative ease.

Standalone scripts

I am going to diverge a bit from this tutorial to talk about standalone scripts. In the previous guide it was assumed that the scripts you were writing would be run from within TexGen. However in some situations you may find it useful to simply run the scripts on their own from the command line without loading up the graphical user interface. In order to do this you must have installed Python and installed the non-bundle version of TexGen (see Windows Installation for more details). To run your script you can then type the following from the command line:

python myscript.py

However before your scripts will work on their own, you must import the TexGen modules at the start of your script. In order to do that simply add the following line to the top of your script:

from TexGen.Core import *

You don't need to do this if you go through the GUI because this step is done automatically for you. Note that by adding this line does not prevent you from running your scripts through the GUI.

Getting started

I'm going to assume you already have a textile model you want to get a mesh of and have saved it to a .tg3 file named "textile.tg3". So the first step is to load this model so that we can go about extracting a mesh from it:

from TexGen.Core import *
# Read in the textile
ReadFromXML('textile.tg3')

# Get a hold of the textile we just loaded in
textile = GetTextile()

The variable textile now contains an instance of the CTextile class representing the textile model. Let's now extract a mesh for this textile, fortunately TexGen has some built-in functionality to do this:

# Create a mesh instance
mesh = CMesh()
# Add the volume of the textile to the mesh
textile.AddVolumeToMesh(mesh)

We declare mesh to be an instance of CMesh and then use it to store a volume mesh of the textile. So with just a few lines of code we now have a volume mesh of the textile stored in memory. The problem we are now faced with is how do we transfer this mesh from memory to the hard disk in the file format that we want so that it can be understood by our software of choice? Read on to find out.

Defining the file format

In this example I will be showing you how to save to the VTK legacy file format. I have chosen this file format because it is simple and well documented. Unfortunately, in a lot of cases, file formats are poorly documented (if at all) and are complex due to poor design.

Note that TexGen already has a function to export to the VTK XML file format along with a number of other common file formats, and can be called in a single line of code like so: mesh.SaveToVTK("mesh.vtu"). But in this tutorial I want to explain how to actually write the data out line by line so that you can hopefully translate that to any other file format of your choice.

Writing the data to file

Now that we have decide what format the data will be written in, let's get down to actually writing it. First we will open a file for writing:

file = open("mesh.vtk", "w")

See the Python tutorial for more details on reading/writing to files from Python. Next we need to write the header:

file.write("# vtk DataFile Version 2.0\n")
file.write("Textile mesh data\n")
file.write("ASCII\n")

As defined in the VTK legacy file format documentation.