Scripting Export Mesh

From TexGen
Jump to navigationJump to search

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