Files

20 lines
1.3 KiB
Python
Raw Permalink Normal View History

import json,struct,sys,io
from pathlib import Path
from PIL import Image
p=Path(sys.argv[1]);data=p.read_bytes();assert data[:4]==b'glTF'
length=struct.unpack_from('<I',data,12)[0];doc=json.loads(data[20:20+length]);binary_offset=20+length+8
faces=0;vertices=0
for mesh in doc.get('meshes',[]):
for primitive in mesh.get('primitives',[]):
count=doc['accessors'][primitive['attributes']['POSITION']]['count'];vertices+=count
if primitive.get('mode',4)==4:faces+=doc['accessors'][primitive['indices']]['count']//3 if 'indices' in primitive else count//3
images=[]
for image in doc.get('images',[]):
if 'bufferView' in image:
view=doc['bufferViews'][image['bufferView']];start=binary_offset+view.get('byteOffset',0)
img=Image.open(io.BytesIO(data[start:start+view['byteLength']]))
images.append({'name':image.get('name'),'size':list(img.size),'format':img.format})
else:images.append({'name':image.get('name'),'external':True})
report={'file':p.name,'bytes':len(data),'meshes':len(doc.get('meshes',[])),'triangles':faces,'vertex_entries':vertices,'materials':len(doc.get('materials',[])),'embedded_images':images,'joint_counts':[len(s['joints']) for s in doc.get('skins',[])],'animations':[a.get('name') for a in doc.get('animations',[])]}
(p.parent/'asset_inspection.json').write_text(json.dumps(report,indent=2),encoding='utf-8');print(json.dumps(report))