Cross-platform windowing and multimedia library for Python with OpenGL graphics, event handling, and audio/video playback
Low-level graphics rendering with batched drawing, shader programs, vertex lists, and OpenGL state management.
# Batching (recommended)
batch = pyglet.graphics.Batch()
sprite = pyglet.sprite.Sprite(img, batch=batch)
batch.draw()
# Groups (state management)
group = pyglet.graphics.Group(order=0, parent=None)
TextureGroup(texture, order=0, parent=None)
ShaderGroup(program, order=0, parent=None)
# Custom shaders
vert = pyglet.graphics.shader.Shader(vert_source, 'vertex')
frag = pyglet.graphics.shader.Shader(frag_source, 'fragment')
program = pyglet.graphics.shader.ShaderProgram(vert, frag)
# Vertex lists
vlist = program.vertex_list(count, mode, batch=batch, group=group,
position=('f', data), colors=('f', data))class pyglet.graphics.Batch:
__init__()
def draw() # Render all
def invalidate() # Mark for update
def draw_subset(vertex_lists) # Draw specific items (inefficient)
# Default batch
pyglet.graphics.get_default_batch() -> Batchclass pyglet.graphics.Group:
__init__(order=0, parent=None)
order: int # Draw order (lower first)
parent: Group | None
visible: bool
batches: tuple # Read-only
def set_state() # Apply GL state
def unset_state() # Restore GL state
class TextureGroup(Group):
__init__(texture, order=0, parent=None)
class ShaderGroup(Group):
__init__(program, order=0, parent=None)class pyglet.graphics.shader.Shader:
__init__(source_string: str, shader_type: str)
# shader_type: 'vertex', 'fragment', 'geometry', 'compute',
# 'tesscontrol', 'tessevaluation'
def delete()
class pyglet.graphics.shader.ShaderProgram:
__init__(*shaders)
# Properties
id: int # OpenGL program ID
uniforms: dict
uniform_blocks: dict
attributes: dict
# Methods
def use()
def stop()
def __setitem__(key, value) # Set uniform: program['color'] = (1,0,0)
def __getitem__(key) # Get uniform
def delete()
# Create vertex lists
def vertex_list(count, mode, batch=None, group=None, **data)
def vertex_list_indexed(count, mode, indices, batch=None, group=None, **data)
def vertex_list_instanced(count, mode, instance_attributes, batch=None, group=None, **data)
def vertex_list_instanced_indexed(count, mode, indices, instance_attributes,
batch=None, group=None, **data)
class pyglet.graphics.shader.ComputeShaderProgram:
__init__(source: str)Shader Defaults:
pyglet.graphics.get_default_shader() -> ShaderProgram
pyglet.graphics.get_default_blit_shader() -> ShaderProgramFormat: (type_string, data_array)
Type String: '<count><type>'
f (float), d (double), B (ubyte), i (int), etc.Common Attributes:
position=('f', [x1, y1, x2, y2, ...]) # 2D positions
position=('f', [x1, y1, z1, x2, y2, z2, ...]) # 3D positions
colors=('B', [r1, g1, b1, a1, r2, g2, b2, a2, ...]) # RGBA bytes
colors=('f', [r1, g1, b1, a1, r2, g2, b2, a2, ...]) # RGBA floats
tex_coords=('f', [u1, v1, u2, v2, ...]) # Texture coordinates
normals=('f', [nx1, ny1, nz1, nx2, ny2, nz2, ...]) # Normalsclass pyglet.graphics.VertexList:
def draw(mode)
def delete()
def resize(count, index_count=None)
class pyglet.graphics.IndexedVertexList:
indices: list # Index buffer (read/write)
def draw(mode)
def delete()
def resize(count, index_count)from pyglet.gl import GL_TRIANGLES
from pyglet.graphics import shader
vertex_source = """
#version 330 core
in vec2 position;
in vec3 colors;
out vec3 vertex_colors;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
vertex_colors = colors;
}
"""
fragment_source = """
#version 330 core
in vec3 vertex_colors;
out vec4 fragColor;
void main() {
fragColor = vec4(vertex_colors, 1.0);
}
"""
# Create program
vert_shader = shader.Shader(vertex_source, 'vertex')
frag_shader = shader.Shader(fragment_source, 'fragment')
program = shader.ShaderProgram(vert_shader, frag_shader)
# Create vertex list
vertices = program.vertex_list(3, GL_TRIANGLES,
position=('f', (0, 0.5, -0.5, -0.5, 0.5, -0.5)),
colors=('f', (1, 0, 0, 0, 1, 0, 0, 0, 1))
)
@window.event
def on_draw():
window.clear()
vertices.draw(GL_TRIANGLES)For complete reference including buffer objects, uniform blocks, and advanced features, see original pyglet documentation.
Install with Tessl CLI
npx tessl i tessl/pypi-pyglet