You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
2.1 KiB
Python

#!/usr/bin/env python3
import sys
import os
import os.path
import shutil
import tempfile
import argparse
import dds
parser = argparse.ArgumentParser(description="Converts any number of textures to dds format. automatically chooses dxt1 or dxt5 depending on if the source image has anything in its alpha channel.")
parser.add_argument('files', type=str, nargs='*', help = "input texture files")
parser.add_argument('--format', type=str, choices=['auto', 'DXT1', 'DXT5', 'DXT5nm'], default = "auto", help = "output texture format (default: %(default)s)")
parser.add_argument('--nomips', '-m', dest='mips', action='store_false', help = "Do not generate mipmaps")
parser.add_argument('--noflip', dest='flip', action='store_false', help = "Do not flip the image")
parser.add_argument('--keep', '-k', dest='delete', action='store_false', help = "Do not delete originals")
parser.add_argument('--convertcmd', type=str, metavar='CMD', default=dds.convertcmd, help="name of imagemagick's convert tool (default: %(default)s)")
parser.add_argument('--compresscmd', type=str, metavar='CMD', default=dds.compresscmd, help="name of the nvidia dds compress tool (default: %(default)s)")
args = parser.parse_args()
dds.convertcmd = args.convertcmd
dds.compresscmd = args.compresscmd
with tempfile.TemporaryDirectory() as tempDir:
for argv in args.files:
print(f'[{argv}]: converting to dds')
file = os.path.abspath(argv)
output = os.path.splitext(file)[0] + ".dds"
if (os.path.splitext(file)[1] != ".dds") and args.flip:
tmpOutput = os.path.join(tempDir, os.path.basename(file))
dds.flip(file, tmpOutput)
else:
tmpOutput = file
if (args.format == "auto" and dds.alpha(file) < 255) or args.format == "DXT5":
format = "-bc3"
elif args.format == "DXT5nm":
format = "-bc3n"
else:
format = "-bc1"
dds.nvcompress(format, tmpOutput, output, args.mips)
if (os.path.basename(file) != os.path.basename(output)) and args.delete:
os.remove(file)