CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pyglet

Cross-platform windowing and multimedia library for Python with OpenGL graphics, event handling, and audio/video playback

Overview
Eval results
Files

3d-models.mddocs/

3D Model Loading

Load and render 3D models in various formats.

Quick Reference

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()

Model Loading

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)
    """

Scene

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 models

Model

class pyglet.model.Model:
    """3D model instance"""

    # Properties
    vertex_lists: list  # Vertex data
    material: Material  # Material properties
    batch: Batch

    # Methods
    def draw()  # Render model

Material

class 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 texture

Examples

Load and Display

import 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()

Rotating Model

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()

Multiple Models

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()

Performance Tips

  1. Use batching: Pass batch to load() for efficient rendering
  2. Preload models: Load during initialization
  3. LOD: Use lower-poly models for distant objects
  4. Culling: Don't draw offscreen models
  5. Instancing: For many copies of same model

Common Issues

  1. Model not visible: Check camera position and model scale
  2. Texture missing: Ensure texture files in correct path
  3. Format support: Limited to OBJ by default, GLTF requires additional codecs
  4. Coordinate system: Different formats use different conventions
  5. Lighting: Models may need proper OpenGL lighting setup

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

docs

3d-models.md

app-clock.md

audio-video.md

graphics-rendering.md

gui.md

images-textures.md

index.md

input-devices.md

math.md

opengl.md

resource-management.md

sprites-shapes.md

text-rendering.md

windowing.md

tile.json