Skip to content

Instantly share code, notes, and snippets.

@Equinox-
Last active Jan 27, 2021
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@geldrin 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

This comment has been minimized.

Copy link

@FeuerTiger86 FeuerTiger86 commented Aug 26, 2020

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.

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