# Trainz/Tutorial For Blender
## About this page

    The intention of this page is to enable people to export their Blender creations to Trainz using the Blender Exporter for Trainz. Here is no place to answer all your questions related to Blender or Content Creation, only things directly touching the exporter behavior will be described here. If you are stuck in Blender or have a question about Content Creation itself, then you will find a lot of explanations and experienced people under the following links:
    Blender Documentation
    Blender Community
    TRS2006 Content Creation Guide
    Trainz Classics Content Creation Guide
    Trainz Discussion Forums

## Getting started

    What you need to create content for Trainz with Blender is obviously Blender itself and the Blender Exporter for Trainz. The script was developed for Blender 2.45, but any later version should work also. To use the Blender Exporter for Trainz, you must unzip the archive and copy the included file trainz_exporter.py into the script directory of Blender. This is normally the folder scripts located in the subdirectory .blender, so if you install Blender on a Windows system using the default path, you have to copy the file trainz_exporter.py from the archive into the folder C:\Program Files\Blender Foundation\Blender\.blender\scripts.
Your Blender installation has a script directory. There are normally two locations to look one is as described above the other is under "application data" which is a hidden folder under "Documents and Settings" so go to documents and settings, tools, folder options, view, "show hidden fires and folders."
As an example on my machine this would be C:\Documents and Settings\Trainz\Application Data\Blender Foundation\Blender\.blender\scripts where trainz is the account name. You should see something like 100 .py files in there.
Every time Blender starts it scans this directory and include any found script into its menu structure. So if you start or restart Blender after copying the exporter script then you call it over the Blender menu File > Export > Trainz Export. After selecting what you want to create and if no problems occur you find yourself in Blenders file save dialog. Choose the desired location and filename and press Save to finally create the resource file. You should always check the Blender console for infos or warnings, as the exporter only stops by fatal faults.

## Blender settings

    We still have to figure out what are the best Blender settings to not be surprised if finally take a look at your creation in Trainz. Instead just only some basic hints:
   * In Blender you can't scale units like in GMAX(where you have to set in preferences that a system unit should be one meter), simply assume that one BU(Blender Unit) is one meter, that's it. If you want to use imperial units, then you might take a look at the at Blender Mesh Attribute Editor
   * If you want to move an object to an absolute position, then change to Object Mode (Hotkey TAB) and press N. In the now visible Transform Properties window you can enter the values you need. But beware, LocX, LocY and LocZ are local values, only if an object doesn't have a parent these are world coordinates. If you have already assigned a parent, then you will see and must enter the location relative to that parent.
   * Not all material properties visualized in Blender will have effect in Trainz, and some effects in Trainz are unable to visualize in Blender. So don't trust what you see in Blender and take a look at your creation in Trainz.
   * As textures only diffuse, opacity, reflection and normal maps have effect in Trainz. You can also export meshes without any texture.
   * The RIGHT side view shows you the LEFT side of your mesh unlike Gmax.

## Objects
### Meshes

    Meshes are the Blender objects you will see in Trainz. You can build your objects by manipulate Blender primitives or even place vertices and create faces. Just keep in mind the following things:
   * center your creation around the point of origin, as Trainz place this point at cursor position when you insert the asset
   * Faces can be double or single sided, in Blender this is a mesh and not a material option. You will recognize the difference only in Trainz, as Blender render faces always double sided.
   * every mesh need a material; you can use one material for different meshes
   * keep an eye on vertex and face count
    The Exporter only recognize meshes which are visible during export. Make sure all objects you want to export are visible and you have selected all needed layers too! There are two reasons for this behaviour: First, nothing will be exported what you not see(maybe a long forgotten test mesh). The second cause is that you can hold big or animated objects in one Blender file and export only the needed parts by changing visibility(here the layers comes into play).

### Bones

    As soon as you want to animate your asset, there is no way around using bones. You can create bones by placing a Lattice(Add > Lattice) and rename it to something like b.r.something as it is described for the bone naming conventions in CCG. If you use bones you need to build up a complete hierarchy, that mean starting from a root bone all other bones or meshes need to be child of this root bone or any subordinated bone. The Exporter denies work if you have loose meshes or bones, because unbound meshes will not shown in Trainz.
    The root bone should be located at the point of origin and should not be rotated. Best practice might be to place it in top view (Hotkey NumPad 7) and leave it untouched. The root bone also must not be animated; it serves as parent for any not animated mesh within this asset and for any bone which is not driven by/depending on an animated parent.
    To export bones they must be visible; so watch your Outliner and your Layers.

### Attachment Points

    Attachment points are used in Trainz to attach effects, tracks or even other meshes. To create an AP you have to add an Empty first(Add > Empty). Afterwards place and rotate it like you need and give it a name following the CCG-Rules for APs(a.whatsoever, do you remember? ;)). APs don't need a parent, as this information will get lost during export. To although made animation possible, Trainz use an extended naming schema. Assume you have a bone b.r.john and an AP a.doe. To tell Trainz that a.doe should be animated like b.r.john, you must rename the AP to a.r.john/a.doe. In your config.txt this AP is still to refer as a.doe.
    What is valid for meshes and bones, is also valid for attachment points: If they are not visible, they will not be exported.

## Materials
### Colors

    You can manipulate diffuse and specular color in Blender to give the material in Trainz the appearance you want. Both you find at the material panel, Col for diffuse and Spe for specular color:
    There are two other colors supported by Trainz: ambient and emissive.
    The ambient color in Blender work different from Trainz and as in most cases ambient and diffuse color will be equal, ambient color is copied from diffuse color during export. The emissive color is used for self illumination in Trainz and not provided in Blender. As things normally have the same color even if lighted or light, the Exporter here also copies the value of diffuse color.
    If you plan to use textures you should set the diffuse color to pure white, else your textures will appear darker in Trainz than expected.

### Opacity

    Also on the material panel you find the slider for Opacity, in Blender called Alpha(slider A down at the right side). A value of 1 means the material is completely visible, a value of 0 render your material completely invisible. If you use texture maps which also affect the opacity, then better leave the Alpha-slider at its right position as the over all opacity will not be higher then the material opacity.

### Specularity

    Specularity can be adjusted using the shader panel. The interesting sliders are specularity Spec and hardness Hard in the middle of the panel.
    Slider specularity maintain the intensity of specular color from 0(no specular highlight) up to 2(full specular highlight), while slider hardness define how concentrate the reflected light should be. A high hardness gives a hard specularity(imagine glass or an polished surface), while a low hardness gives a dull shine(like asphalt or rough/dirty metal). The hardness range(1..511) will be scaled down from the Exporter to 0..100, so if you want to copy GMax/Max Glossiness-values you have to multiply them roughly by 5.

### Self Illumination

    The brightness without to be lighted. You find the slider Emit in the lower right corner of shader panel. The more emissive you order, the more your material glow in diffuse color.

## Textures
### General

At first some common things to textures:
   * NEVER use packed picture formats, as they often distort colors, wasting resources while loading(to extract they need additional memory and CPU-time) and at last they have the same footprint in memory as an uncompressed picture format. A good format is Targa(*.tga), as it is uncompressed(don't use the RLE-option!) and has the ability to store also an alpha channel if needed.
   * Made your textures as small as possible. What ever should work is the creation of your texture twice as big as finally used and afterwards scale them down with enabled antialiasing. You get nearly the same detail grade as in the original map and need only 1/4 of its memory.
   * Ever us "power of 2" - values as map dimensions(i.e. 64x64, 128x128, 128x512). Such textures could processed very fast,because "power of 2" - numbers are the native language of your hardware. You don't have to stay by square maps, but also avoid an aspect ratio behind 1:8.
    In Blender you assign textures using the texture panel you find in the Material buttons window (Hotkey F5).
| 3 - Blender Texture panel - empty | 4 - Blender Texture panel - texture object created |
    After creating a TE-object you have to tell Blender where the mapping information should came from(Map Input) and which material property should be influenced(Map To).
| 5 - Blender Map Input panel | 6 - Blender Map To panel |
    The default map input choosen by Blender is Orco. That's a mapping not used by Trainz. For reflection maps you can choose Refl, for all the other used maps the Exporter only accept UV mapping. As mapping target you can choose one or more of the following options:
      * Col, used for diffuse maps
      * Alpha, used for opacity maps
      * Ref, used for reflection maps
      * Nor, used for normal(bump) maps
    You can assign other mapping targets and these textures will be exported, but you will not notice the slightest effect in Trainz.
    What's now left to do is of course loading the texture itself. In Blender you define the content of a texture using the Texture buttons window(Hotkey F6). The only texture type you can export to Trainz is Image, so you can ignore all other possibilities you have in Blender.
    Now select the appropriate image by pressing Load on the Image panel.
    There is only one option left, the option "no texture". The Blender Exporter for Trainz allows you to export models without any texture. This can speed up asset creation due to "quick checks" of the pure mesh directly in Trainz. To export without any texture you simply have to delete eventually assigned texture objects.

### Diffuse Textures

    Diffuse textures are the most used textures, they basically define the surface of objects. To use them you have to select button UV as input and button Col as output. The texture amount you can adjust with slider Col, located at the right middle of the Map To panel.

### Opacity Textures

    Alpha textures are used to simulate transparent materials or to make parts of your object invisible. You can create dirty windows or let a simple plane look like a bogey part. An alpha map normally comes together with a diffuse map, so the needed UV mapping should already be done. As target select Alpha. The texture amount is controlled by slider Var at the right side of Map To panel.
    You can combine diffuse and opacity texture if you use an image with alpha layer. You need to select the option UseAlpha at the Map Image panel and must select Col and Alpha as targets at the Map To panel.
    One little problem can occur if you work with opacity: Due to a bug in Trainz not completely opaque or transparent areas may overlap each other. You can take a look at the problem and one possible work around by downloading the two related examples from SourceForge.

### Reflection Textures

    These textures are a bit different in usage as the others, because Trainz map them automatically using spherical mapping. How this works you can imagine best if you create a smooth sphere and assign a reflection map. Which map part is shown depend on the direction in which the imaginary ray from the camera to the objects surface would be reflected. That's why you see the whole map on the surface of a smooth sphere.
    To use reflection maps you only need to press button Refl in Map Input panel and button Ref in the Map To panel of the designated texture. You will notice that no colors appear neither in material preview nor in render window. That's because Blender use reflective textures to modify the strength and not the color of reflection. But no fear, in Trainz you get your colors back ;).

### Hardness Textures

    You can assign hardness textures but you will be disappointed. They don't affect hardness, instead they will be typed as reflective texture during export. This behavior is copied from the original exporter, as any attempt to influence the "shinestrength" so far ends in a complete white unlighted object.

### Normal Textures

    A normal texture is used to fake highs and lows on an plain surface. To use it you need an UV Map Input and a Nor Map To. Please take care to use an normal map and not a bump map. Both are used for the same purpose, but they contain different data.

### Texture Combinations

    How a texture influence the object appearance in Trainz is controlled by so called "material name decorations". With one exception the Blender Exporter for Trainz select the appropriate decoration depending your assigned textures. This exception is m.billboard, as this material decoration is used for objects always face the camera. Because the Exporter has no chance to detect if you want this behavior, you have to decorate the material name for yourself with the ending m.billboard. General the Exporter don't overwrite a known material decoration, so if you want to ensure your material will be exported as m.gloss, then simply append m.gloss for yourself to the material name.
    a list of all currently supported texture combinations
    single textures:
      * diffuse texture
      * reflection texture
      * hardness texture(will appear as reflection texture)
    double textures:
      * diffuse & alpha texture
      * diffuse & reflection texture
      * diffuse & hardness texture(the same as diffuse & reflection)
      * diffuse & normal texture
    triple textures:
      * diffuse, reflection & alpha texture(in this case the alpha texture affects the reflection texture and not the diffuse texture)
      * diffuse, alpha & normal texture(no reflection or hardness texture allowed)
      * diffuse, reflection & normal texture(no alpha texture allowed)

## Events

    In case of animation export the original exporters ask you for an event file to bind events to specific frames. To use events with the Blender Exporter for Trainz is similar. Switching a Blender window to Text Editor window and create a new TX object via File > New (Hotkey Alt N). Enter for every event a line with frame number, event type and event name. If you finally rename this TX object to events, then the Exporter will parse this text and create the desired entries in your animation file.

## Examples

    Examples you find as additional files at the download section by SourceForge