2021-01-20 07:57:24 +00:00
#!/usr/bin/env python3
import sys
import os
import os . path
2021-01-22 01:37:08 +00:00
import shutil
2021-01-20 07:57:24 +00:00
import tempfile
2021-01-20 09:40:09 +00:00
import argparse
2021-01-20 07:57:24 +00:00
import dds
2021-01-20 09:40:09 +00:00
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 " )
2021-02-23 11:19:11 +00:00
parser . add_argument ( ' --format ' , type = str , choices = [ ' auto ' , ' DXT1 ' , ' DXT5 ' , ' DXT5nm ' ] , default = " auto " , help = " output texture format (default: %(default)s ) " )
2021-01-22 01:37:08 +00:00
parser . add_argument ( ' --nomips ' , ' -m ' , dest = ' mips ' , action = ' store_false ' , help = " Do not generate mipmaps " )
2021-02-23 11:19:11 +00:00
parser . add_argument ( ' --noflip ' , dest = ' flip ' , action = ' store_false ' , help = " Do not flip the image " )
2021-01-22 01:37:08 +00:00
parser . add_argument ( ' --keep ' , ' -k ' , dest = ' delete ' , action = ' store_false ' , help = " Do not delete originals " )
2021-01-24 03:52:11 +00:00
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 ) " )
2021-01-20 09:40:09 +00:00
args = parser . parse_args ( )
dds . convertcmd = args . convertcmd
dds . compresscmd = args . compresscmd
with tempfile . TemporaryDirectory ( ) as tempDir :
for argv in args . files :
2021-01-20 07:57:24 +00:00
print ( f ' [ { argv } ]: converting to dds ' )
file = os . path . abspath ( argv )
output = os . path . splitext ( file ) [ 0 ] + " .dds "
2021-01-22 01:37:08 +00:00
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
2021-01-24 03:52:11 +00:00
2021-01-20 09:56:07 +00:00
if ( args . format == " auto " and dds . alpha ( file ) < 255 ) or args . format == " DXT5 " :
2021-01-20 07:57:24 +00:00
format = " -bc3 "
2021-02-23 11:19:11 +00:00
elif args . format == " DXT5nm " :
format = " -bc3n "
2021-01-20 07:57:24 +00:00
else :
format = " -bc1 "
2021-01-20 09:56:07 +00:00
2021-01-20 10:28:17 +00:00
dds . nvcompress ( format , tmpOutput , output , args . mips )
2021-01-22 01:37:08 +00:00
if ( os . path . basename ( file ) != os . path . basename ( output ) ) and args . delete :
2021-01-20 10:28:17 +00:00
os . remove ( file )