Python GLTF Library

1.16.5 · active · verified Tue Apr 14

pygltflib is a Python library for reading, writing, and managing 3D objects in the Khronos Group glTF (GL Transmission Format) and glTF2 formats. It supports the entire GLTF v2 specification, including materials, animations, and extensions, with all attributes being type-hinted. The library is actively maintained, with frequent updates addressing features and bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a basic, empty glTF 2.0 file with a scene and a node, and then save it to disk. Real-world applications would involve populating the glTF object with meshes, materials, accessors, and buffer data. Ensure you have write permissions in the execution directory for the `gltf.save()` operation.

import os
from pygltflib import GLTF2, Scene, Node

# Create a new GLTF2 object
gltf = GLTF2()

# Create a scene
scene = Scene(name="My_New_Scene")
gltf.scenes.append(scene)
gltf.scene = 0 # Set the default scene to the first one (index 0)

# Create a simple node (e.g., an empty node)
node = Node(name="My_Node")
gltf.nodes.append(node)

# Link the node to the scene
scene.nodes.append(0) # Referencing the first node (index 0)

# Define a filename
output_filename = "my_simple_scene.gltf"

# Save the GLTF file
try:
    gltf.save(output_filename)
    print(f"GLTF file saved to {output_filename}")
except Exception as e:
    print(f"Error saving GLTF file: {e}")

# Clean up (optional)
if os.path.exists(output_filename):
    os.remove(output_filename)
    print(f"Cleaned up {output_filename}")

view raw JSON →