Skip to content

Instantly share code, notes, and snippets.

@Equinox-
Last active April 4, 2024 10:03
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save Equinox-/19d726d4c9cd59f8617429b3a6a8e16c to your computer and use it in GitHub Desktop.
Save Equinox-/19d726d4c9cd59f8617429b3a6a8e16c to your computer and use it in GitHub Desktop.
Very simple DDS plugin for GIMP that uses texconv to convert things behind the scenes to encode/decode BC7
1. Download the `file-dds-texconv.py` file. Edit BINARY to be the path to your texconv binary (located in the SE ModSDK, or elsewhere)
2. Copy the script into the GIMP plugins directory. For instance, `C:\Program Files\GIMP 2\lib\gimp\2.0\plug-ins`
#!/usr/bin/env python
import os, sys, tempfile, shutil, subprocess, time
import xml.etree.ElementTree as ET
import urlparse, urllib
BINARY="""C:/Your/Path/To/texconv.exe"""
# https://stackoverflow.com/questions/11687478/convert-a-filename-to-a-file-url
def path2url(path):
return urlparse.urljoin(
'file:', urllib.pathname2url(path))
from gimpfu import *
def save_dds_texconv(img, drawable, filename, raw_filename):
tmp = tempfile.mkdtemp("-gimp-dds-save")
name = os.path.splitext(os.path.basename(filename))[0]
try:
savePath = tmp + '\\' + name + '.png'
pdb['file-png-save-defaults'](img, drawable, savePath, path2url(savePath))
#pdb.gimp_message("Save path: " + savePath)
codec="BC7_UNORM"
if "_cm" in name.lower():
codec="BC7_UNORM_SRGB"
run_smart([BINARY, "-y", "-ft", "DDS", "-f", codec, "-o", tmp, savePath])
result = tmp + '\\' + name + '.dds'
#pdb.gimp_message('result file ' + result)
if os.path.isfile(result):
if os.path.isfile(filename):
os.remove(filename)
shutil.move(result, filename)
else:
pdb.gimp_message("failed to save DDS")
except Exception as ex:
pdb.gimp_message("Exception:\n" + str(ex))
finally:
shutil.rmtree(tmp)
return None
def run_smart(args):
try:
argline = " ".join(["\"" + x + "\"" for x in args])
#pdb.gimp_message("run: " + argline)
subprocess.check_call(args)
except Exception as ex:
pdb.gimp_message("Error: " + str(ex))
def load_dds_texconv(filename, raw_filename):
tmp = tempfile.mkdtemp("-gimp-dds-load")
name = os.path.splitext(os.path.basename(filename))[0]
try:
result = tmp + '\\' + name + '.png'
#pdb.gimp_message(result)
run_smart([BINARY, "-ft", "PNG", "-o", tmp, filename])
#pdb.gimp_message('load ' + result)
return pdb['file-png-load'](result, path2url(result))
except Exception as ex:
pdb.gimp_message("Exception:\n" + str(ex))
finally:
shutil.rmtree(tmp)
return None
def register_load_handlers():
gimp.register_load_handler('file-dds-texconv-load', 'dds', '')
pdb['gimp-register-file-handler-mime']('file-dds-texconv-load', 'image/vnd-ms.dds')
def register_save_handlers():
gimp.register_save_handler('file-dds-texconv-save', 'dds', '')
register(
'file-dds-texconv-save',
'save a DDS file with texconv',
'save a DDS file with texconv',
'Westin Miller',
'Westin Miller',
'2018',
'DDS',
'*',
[
(PF_IMAGE, "image", "Input image", None),
(PF_DRAWABLE, "drawable", "Input drawable", None),
(PF_STRING, "filename", "The name of the file", None),
(PF_STRING, "raw-filename", "The name of the file", None),
],
[],
save_dds_texconv,
on_query = register_save_handlers,
menu = '<Save>'
)
register(
'file-dds-texconv-load',
'load a DDS file with texconv',
'load a DDS file with texconv',
'Westin Miller',
'Westin Miller',
'2018',
'DDS',
None,
[
(PF_STRING, 'filename', 'The name of the file to load', None),
(PF_STRING, 'raw-filename', 'The name entered', None),
],
[(PF_IMAGE, 'image', 'Output image')],
load_dds_texconv,
on_query = register_load_handlers,
menu = "<Load>",
)
main()
@Nazosan
Copy link

Nazosan commented Feb 4, 2022

Is there any chance it could use a relative path and/or search for the binary in the same folder? That would be friendlier towards portable installations as well as making a local snapshot easier (newer isn't always better -- sometimes compatibility can be broken.)

EDIT: Also, maybe it could not bypass the built-in DDS script entirely for saving since it seems to be considerably less detailed in what it can do. For now I just changed the extension it registers as to .dds7 for simplicity's sake, but I'm sure there must be a better way.

@Equinox-
Copy link
Author

Equinox- commented Feb 4, 2022

there probably is a better way. The best way is to contribute BC7 support to GIMP proper.
Feel free to modify this however you want. If you want to fork it feel free to leave a comment with your changes and I can discuss merging them in to this gist.

@Nazosan
Copy link

Nazosan commented Feb 5, 2022

Sadly, Python programming is beyond me. What little I did was just simply change a couple of obvious things. But I have no idea how to not have an absolute path to the exe or anything like that.

@hoover67
Copy link

hey folks, thanks for this great plugin (which I'm using to create an rfactor2 Porsche GT3 skin which seems to use BC7).

The script works great, however DDS export from gimp now takes about a minute (not joking) where as before, with the old plugin, it took all of three seconds. The complexity of the skin in question hasn't increased in any way, but I noted that gimp seems to spend most of the time exporting to PNG first and only then calling texconv.exe in order to create the DDS file.

Has anyone else experienced this behaviour? I'm using gimp 2.10.32 on win10 (sadly, as rfactor2 doesn't properly support Linux yet).

Thanks in advance for your help & all the best,

Uwe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment