-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgltfutil.py
executable file
·55 lines (49 loc) · 1.6 KB
/
gltfutil.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2024 Lars Müller
# License: LGPLv2.1 or later (see LICENSE file for details)
import argparse
import json
import sys
import base64
import re
parser = argparse.ArgumentParser(
prog = "gltfutil",
description = "Utility for dealing with embedded images in glTF files")
parser.add_argument("action", choices = ["extract", "strip"])
parser.add_argument("-i", "--input",
help = "input file (default stdin)")
parser.add_argument("-o", "--output",
help = "output directory / file (default CWD / stdout)")
args = parser.parse_args()
gltf = None
if args.input:
with open(args.input) as f:
gltf = json.load(f)
else:
gltf = json.load(sys.stdin)
if args.action == "extract":
print("Extracting embedded base64-encoded images")
for i, image in enumerate(gltf["images"] or []):
m = re.match(r"data:image/(.+?);base64,(.+)", image["uri"])
if not m:
print(f"Skipping image {i}")
continue
format, data = m.group(1, 2)
path = (args.output or ".") + f"/{i}.{format}"
print(f"Writing {path}")
with open(path, "wb") as f:
f.write(base64.b64decode(data))
elif args.action == "strip":
for texture in gltf["textures"] or []:
if "source" in texture:
del texture["source"]
if "images" in gltf:
del gltf["images"]
if args.output:
with open(args.output, "w") as f:
json.dump(gltf, f, separators = (',', ':'))
else:
json.dump(gltf, sys.stdout, separators = (',', ':'))
else:
assert(False)