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()
@geldrin
Copy link

geldrin commented Mar 3, 2019

Hi!

I'm getting the following error message (system cannot find the specified file) when trying to open any Space Engineers DDS files:

Procedure 'gimp-message' has been called with value 'Error: [Error 2] A rendszer nem találja a megadott fájlt' for argument 'message' (#1, type gchararray). This value is out of range.

Calling error for procedure 'gimp-message':
Procedure 'gimp-message' has been called with value 'Exception:
Procedure 'gimp-message' has been called with value 'Error: [Error 2] A rendszer nem találja a megadott fájlt' for argument 'message' (#1, type gchararray). This value is out of range.' for argument 'message' (#1, type gchararray). This value is out of range.

Opening  'C:\BackgroundCube.dds' failed:
DDS plug-In could not open image

I modified the script according to the readme:

BINARY="""c:\Program Files (x86)\Steam\steamapps\common\SpaceEngineersModSDK\Tools\TexturePacking\Tools\texconv.exe"""

I'm using Gimp 2.10.8 and the newest SE SDK.
Could you give me any advice to make this script work with the editor? Should I downgrade to a specific version?

@FeuerTiger86
Copy link

Great plugin, thank you!

I found one small issue, but was able to fix it: When exporting textures with transparent pixels, the color value was not preserved and they were set to black. This can be a problem when exporting normal maps with alpha channels. However, I noticed that this is because the plugin first exports a .png with the default settings. If "Save color values from transparent pixels" is not checked, these will be set to black (makes sense in a png for more efficient compression). Fortunately, if the png export settings are saved with the checkbox enabled, the plugin will use these settings and transparent pixels are saved correctly.

@WrinkleysRule
Copy link

WrinkleysRule commented Jan 1, 2022

Plugin permanently overides Gimp inbuilt DDS exporter so cannot chose another type of DDS file type without removing the
file-dds-texconv.py file from the plugin folder.

Solved -- select show all file types, select DDS if BC7 required, select DDS Image for all other variants

@uniqueum
Copy link

Hi, sorry, I honestly have no idea what texconv is and/or where I'd get it from -- I'm coming here from trying to mod some Horizon ZD textures and apparently anything below BC7 won't work for it. I downloaded texconv.exe from https://github.com/Microsoft/DirectXTex/releases and edited the path in the pythonscript as is said in the readme. However, trying to export a .dds from Gimp I now get this message: https://imgur.com/a/fQmdeyk

Any help or clue as to what software package I need in addition for this to work would be greatly appreciated.

Thank you and happy new year! :))

@Equinox-
Copy link
Author

Getting texconv from the DirectXTex releases page is correct. File not found is likely caused by putting the wrong path in the config file. Make sure you use forward slashes "/" instead of "" and make sure the file name is surrounded by triple double quotes """C:/path/to/texconv.exe"""

@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