repack_gltf.py (1182B)
1 import json 2 3 def dump_sub_buffer_to_file(gltf, accessor, output): 4 position_attribute = gltf["accessors"][accessor] 5 buffer_view = gltf["bufferViews"][position_attribute["bufferView"]] 6 offset = buffer_view["byteOffset"] 7 length = buffer_view["byteLength"] 8 buffer = buffer_view["buffer"] 9 10 with open(gltf["buffers"][buffer]["uri"], "rb") as input: 11 input.seek(offset, 0) 12 output.write(input.read(length)) 13 14 def dump_sub_buffer(gltf, accessor, output_name): 15 with open(output_name, "wb") as output: 16 dump_sub_buffer_to_file(gltf, accessor, output) 17 18 def main(): 19 with open('unit_cube.gltf', 'r') as f: 20 unit_cube = json.load(f) 21 22 assert(len(unit_cube["meshes"]) == 1) 23 mesh = unit_cube["meshes"][0] 24 25 assert(len(mesh["primitives"]) == 1) 26 attributes = mesh["primitives"][0]["attributes"] 27 indices = mesh["primitives"][0]["indices"] 28 29 dump_sub_buffer(unit_cube, attributes["POSITION"], "unit_cube_positions.bin") 30 dump_sub_buffer(unit_cube, attributes["NORMAL"], "unit_cube_normals.bin") 31 dump_sub_buffer(unit_cube, indices, "unit_cube_indices.bin") 32 33 if __name__ == '__main__': 34 main()