Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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.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
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)