create gltf models with python

Solutions on MaxInterview for create gltf models with python by the best coders in the world

showing results for - "create gltf models with python"
Alex
05 Sep 2019
1import struct
2import operator
3from gltflib import (
4    GLTF, GLTFModel, Asset, Scene, Node, Mesh, Primitive, Attributes, Buffer, BufferView, Accessor, AccessorType,
5    BufferTarget, ComponentType, GLBResource, FileResource)
6
7vertices = [
8    (-4774424.719997984, 4163079.2597148907, 671001.6353722484),
9    (-4748098.650098154, 4163079.259714891, 837217.8990777463),
10    (-4689289.5292739635, 4246272.966707474, 742710.4976137652)
11]
12
13vertex_bytearray = bytearray()
14for vertex in vertices:
15    for value in vertex:
16        vertex_bytearray.extend(struct.pack('f', value))
17bytelen = len(vertex_bytearray)
18mins = [min([operator.itemgetter(i)(vertex) for vertex in vertices]) for i in range(3)]
19maxs = [max([operator.itemgetter(i)(vertex) for vertex in vertices]) for i in range(3)]
20model = GLTFModel(
21    asset=Asset(version='2.0'),
22    scenes=[Scene(nodes=[0])],
23    nodes=[Node(mesh=0)],
24    meshes=[Mesh(primitives=[Primitive(attributes=Attributes(POSITION=0))])],
25    buffers=[Buffer(byteLength=bytelen, uri='vertices.bin')],
26    bufferViews=[BufferView(buffer=0, byteOffset=0, byteLength=bytelen, target=BufferTarget.ARRAY_BUFFER.value)],
27    accessors=[Accessor(bufferView=0, byteOffset=0, componentType=ComponentType.FLOAT.value, count=len(vertices),
28                        type=AccessorType.VEC3.value, min=mins, max=maxs)]
29)
30
31resource = FileResource('vertices.bin', data=vertex_bytearray)
32gltf = GLTF(model=model, resources=[resource])
33gltf.export('triangle.gltf')
34