InstallASEScript

From The DarkMod Wiki
Revision as of 09:13, 23 August 2008 by OrbWeaver (talk | contribs) (Added content)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

This is a small convenience script written in Python that can be used to quickly install an exported ASE while automatically making texture replacements, avoiding the time-consuming process of manually editing texture paths with a text editor after export.

The texture replacements are stored in a file with the same name as the .ase but with .tex appended, such as myobject.ase.tex. This is a simple text file which can be stored in the directory that Blender exports to, from which the ASE is then installed into the model hierarchy using the Python script. The replacement file format is:

/exported/texture/name //base/textures/doom3/texture/name
/exported/texture/name2 //base/textures/doom3/texture/name2

The Python script is then invoked like:

$ install-ase.py blender/export/myobject.ase ~/.doom3/darkmod/models/mymodels/

in order to install the exported ASE into the models hierarchy. In this case the replacement file would be blender/export/myobject.ase.tex

NOTE: So far I have only tested this on Linux. Python does work on Windows but the pathname processing stuff might be different.

The script

#!/usr/bin/python

import sys
import os.path

if len(sys.argv) != 3:
    print "Usage: install-ase.py <filename.ase> <destination-dir>"
    print "   A replacement file named <filename.ase.tex> must be present in "
    print "   the source directory."
    sys.exit(1)

# Check we can open the .ase file and its associated .ase.tex file containing
# texture replacements
try:
    aseFile = open(sys.argv[1], 'r')
except IOError:
    print "Unable to open input file: %s" % sys.argv[1]
    sys.exit(2)

# Build replacement mappings
texReplacements = { }
try:
    # Read the tex file
    texFile = open(sys.argv[1] + '.tex', 'r')
    for line in texFile:
        (first, second) = line.split()
        texReplacements[first] = second

    print "+ Making %d texture replacement(s):" % len(texReplacements.keys())
    for (k, v) in texReplacements.items():
        print "+   %s -> %s" % (k, v)
except IOError:
    print "+ No replacement file"

# Open the destination file
srcFileName = os.path.split(aseFile.name)[1]
destFileName = os.path.join(sys.argv[2], srcFileName)
print "+ Installing as %s" % destFileName
destFile = open(destFileName, 'w')

# Copy the source file to the destination, making replacements
for line in aseFile:
    for (old, new) in texReplacements.items():
        line = line.replace(old, new)
    destFile.write(line)
destFile.close()