Cross-platform windowing and multimedia library for Python with OpenGL graphics, event handling, and audio/video playback
Load and render 3D models in various formats.
import pyglet
from pyglet import model
# Load model
scene = model.load('model.obj', batch=batch)
# Or specific file
model_obj = model.load('character.obj', file=open('character.obj', 'rb'))
# Render
@window.event
def on_draw():
window.clear()
scene.draw()def pyglet.model.load(filename, file=None, decoder=None, batch=None) -> Scene:
"""
Load 3D model or scene.
Args:
filename: Path to model file
file: File-like object (optional)
decoder: Specific decoder (optional)
batch: Batch for rendering (optional)
Returns:
Scene: Loaded scene with models and nodes
Supported formats: .obj, .gltf, .glb (with pyglet.model.codecs.gltf)
"""class pyglet.model.Scene:
"""3D scene with models and scene graph"""
# Properties
models: list # Model instances
nodes: dict # Scene graph nodes
batch: Batch # Rendering batch
# Methods
def draw() # Render all modelsclass pyglet.model.Model:
"""3D model instance"""
# Properties
vertex_lists: list # Vertex data
material: Material # Material properties
batch: Batch
# Methods
def draw() # Render modelclass pyglet.model.Material:
"""Material properties"""
# Properties
name: str
diffuse: tuple # Diffuse color RGBA
ambient: tuple # Ambient color RGBA
specular: tuple # Specular color RGBA
emission: tuple # Emission color RGBA
shininess: float # Specular exponent
texture: Texture | None # Diffuse textureimport pyglet
from pyglet import model
from pyglet.gl import *
window = pyglet.window.Window(width=800, height=600)
batch = pyglet.graphics.Batch()
# Load model
scene = model.load('spaceship.obj', batch=batch)
# Setup camera
@window.event
def on_resize(width, height):
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(60.0, width/height, 0.1, 100.0)
glMatrixMode(GL_MODELVIEW)
@window.event
def on_draw():
window.clear()
glLoadIdentity()
glTranslatef(0, 0, -5) # Move back to see model
scene.draw()
pyglet.app.run()rotation = 0
def update(dt):
global rotation
rotation += dt * 45 # 45 degrees per second
pyglet.clock.schedule_interval(update, 1/60)
@window.event
def on_draw():
window.clear()
glLoadIdentity()
glTranslatef(0, 0, -5)
glRotatef(rotation, 0, 1, 0) # Rotate around Y axis
scene.draw()models = {
'player': model.load('player.obj', batch=batch),
'enemy': model.load('enemy.obj', batch=batch),
'item': model.load('item.obj', batch=batch),
}
@window.event
def on_draw():
window.clear()
glLoadIdentity()
glTranslatef(0, 0, -10)
# Draw player
glPushMatrix()
glTranslatef(-2, 0, 0)
models['player'].draw()
glPopMatrix()
# Draw enemy
glPushMatrix()
glTranslatef(2, 0, 0)
models['enemy'].draw()
glPopMatrix()
# Draw item
glPushMatrix()
glTranslatef(0, 2, 0)
glRotatef(time * 90, 0, 1, 0)
models['item'].draw()
glPopMatrix()For advanced 3D rendering, consider using a dedicated 3D engine or pyglet with custom shaders.
Install with Tessl CLI
npx tessl i tessl/pypi-pyglet@2.1.1