How to use pygltflib - 3 common examples

To help you get started, we’ve selected a few pygltflib examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github prideout / svg3d / test_gltf.py View on Github external
def create_meshes_from_glb(filename, scale, style, mesh_index=0):
    glb = pygltflib.GLTF2().load(filename)
    meshes = []
    for prim in glb.meshes[mesh_index].primitives:

        # Indices
        accessor = glb.accessors[prim.indices]
        assert accessor.type == "SCALAR"
        assert not accessor.sparse
        assert accessor.componentType == pygltflib.UNSIGNED_SHORT
        nindices = accessor.count
        bv = glb.bufferViews[accessor.bufferView]
        data = glb._glb_data[bv.byteOffset : bv.byteOffset + bv.byteLength]
        triangles = np.frombuffer(data, dtype=np.uint16)
        triangles = np.reshape(triangles, (-1, 3))
        assert nindices == len(triangles) * 3

        # Vertices
github prideout / svg3d / test_gltf.py View on Github external
accessor = glb.accessors[prim.indices]
        assert accessor.type == "SCALAR"
        assert not accessor.sparse
        assert accessor.componentType == pygltflib.UNSIGNED_SHORT
        nindices = accessor.count
        bv = glb.bufferViews[accessor.bufferView]
        data = glb._glb_data[bv.byteOffset : bv.byteOffset + bv.byteLength]
        triangles = np.frombuffer(data, dtype=np.uint16)
        triangles = np.reshape(triangles, (-1, 3))
        assert nindices == len(triangles) * 3

        # Vertices
        accessor = glb.accessors[prim.attributes.POSITION]
        assert accessor.type == "VEC3"
        assert not accessor.sparse
        assert accessor.componentType == pygltflib.FLOAT
        nvertices = accessor.count
        bv = glb.bufferViews[accessor.bufferView]
        data = glb._glb_data[bv.byteOffset : bv.byteOffset + bv.byteLength]
        vertices = np.frombuffer(data, dtype=np.float32)
        vertices = np.reshape(vertices, (-1, 3))
        assert nvertices == len(vertices)

        faces = scale * vertices[triangles]
        cull = lambda face_index, winding: None if winding < 0 else {}
        meshes.append(svg3d.Mesh(faces, cull, style=style))

    return meshes
github prideout / svg3d / test_gltf.py View on Github external
def create_meshes_from_glb(filename, scale, style, mesh_index=0):
    glb = pygltflib.GLTF2().load(filename)
    meshes = []
    for prim in glb.meshes[mesh_index].primitives:

        # Indices
        accessor = glb.accessors[prim.indices]
        assert accessor.type == "SCALAR"
        assert not accessor.sparse
        assert accessor.componentType == pygltflib.UNSIGNED_SHORT
        nindices = accessor.count
        bv = glb.bufferViews[accessor.bufferView]
        data = glb._glb_data[bv.byteOffset : bv.byteOffset + bv.byteLength]
        triangles = np.frombuffer(data, dtype=np.uint16)
        triangles = np.reshape(triangles, (-1, 3))
        assert nindices == len(triangles) * 3

        # Vertices
        accessor = glb.accessors[prim.attributes.POSITION]
        assert accessor.type == "VEC3"
        assert not accessor.sparse
        assert accessor.componentType == pygltflib.FLOAT
        nvertices = accessor.count
        bv = glb.bufferViews[accessor.bufferView]
        data = glb._glb_data[bv.byteOffset : bv.byteOffset + bv.byteLength]
        vertices = np.frombuffer(data, dtype=np.float32)

pygltflib

Python library for reading, writing and managing 3D objects in the Khronos Group gltf and gltf2 formats.

MIT
Latest version published 3 months ago

Package Health Score

61 / 100
Full package analysis

Similar packages