Skip to content

Instantly share code, notes, and snippets.

@prideout
Created April 24, 2019 19:13
Show Gist options
  • Save prideout/282661ba42e56e00d9cc094e97811303 to your computer and use it in GitHub Desktop.
Save prideout/282661ba42e56e00d9cc094e97811303 to your computer and use it in GitHub Desktop.
filter out nodes and textures from glTF
#!/usr/bin/env python3
import json
import re
import sys
infile = 'Bistro_Exterior.gltf'
outfile = 'out.gltf'
name_pattern = 'BISTRO_RESEARCH_EXTERIOR_PARIS_BUILDING.*|BISTRO_RESEARCH_EXTERIOR_PARIS_STREET_.*'
name_matcher = re.compile(name_pattern)
if len(sys.argv) > 1:
infile = sys.argv[1]
textures = set()
def add_textures(material):
mr = material['pbrMetallicRoughness']
for key in mr:
if key.endswith('Texture'):
textures.add(mr[key]['index'])
for key in material:
if key.endswith('Texture'):
textures.add(material[key]['index'])
src = json.load(open(infile))
dst = {}
for key in src.keys():
if key == 'nodes':
nodes = dst[key] = src[key]
src_indices = nodes[0]['children']
dst_indices = []
for index in src_indices:
node = nodes[index]
name = node['name'].upper()
if name_matcher.match(name):
dst_indices.append(index)
mesh = src['meshes'][node['mesh']]
for prim in mesh['primitives']:
material = src['materials'][prim['material']]
add_textures(material)
nodes[0]['children'] = dst_indices
else:
dst[key] = src[key]
dst_textures = dst['textures'] = []
src_textures = src['textures']
for index in range(len(src_textures)):
if index in textures:
dst_textures.append(src_textures[index])
else:
dst_textures.append({})
json.dump(dst, open(outfile, 'w'), indent=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment