Created
April 20, 2022 16:57
-
-
Save cwfitzgerald/10d9c310553e43963f1faaa01729dc99 to your computer and use it in GitHub Desktop.
BC7-ifier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import json | |
import os | |
import subprocess | |
from pathlib import Path | |
parser = argparse.ArgumentParser( | |
description="Process gltf into rust-compatible format") | |
parser.add_argument('file', type=Path) | |
parser.add_argument('--force', type=bool) | |
parser.add_argument('--remove', type=bool) | |
args = parser.parse_args() | |
print("loading") | |
with open(args.file, 'r') as f: | |
gltf = json.load(f) | |
conversion = {} | |
for image in gltf['images']: | |
orig_src_path = image['uri'] | |
(base, _, suffix) = orig_src_path.rpartition('.') | |
orig_dst_path = base + '.ktx2' | |
if suffix == 'ktx2' or suffix == 'dds': | |
print(f"{orig_src_path} file in gltf already converted") | |
continue | |
src_path = Path(args.file.parent, orig_src_path) | |
dst_path = Path(args.file.parent, orig_dst_path) | |
if os.path.exists(dst_path) and not args.force: | |
print(f"{dst_path} already exists, not converting.") | |
conversion[orig_src_path] = orig_dst_path | |
continue | |
print(f"Compressing {src_path} to {dst_path}") | |
subprocess.run(["compressonatorcli.exe", "-mipsize", "1", "-fd", "BC7", src_path, dst_path]) | |
if os.path.exists(str(dst_path)): | |
if args.remove: | |
os.remove(str(src_path)) | |
conversion[orig_src_path] = orig_dst_path | |
else: | |
print(f"ignoring unconverted file {dst_path}") | |
for image in gltf['images']: | |
value = conversion.get(image['uri']) | |
if value is not None: | |
print('Updating {} to {}'.format(image['uri'], value)) | |
image['uri'] = value | |
image['mimeType'] = 'image/ktx2' | |
else: | |
print('Cannot update as destination non-existant', image['uri']) | |
print("dumping") | |
with open(args.file, 'w') as f: | |
json.dump(gltf, f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment