pyglet.graphics

Submodules

Details

Low-level graphics rendering and abstractions.

This module provides efficient abstractions over OpenGL objects, such as Shaders and Buffers. It also provides classes for highly performant batched rendering and grouping.

See the Shaders and Rendering for details on how to use this graphics API.

class Batch

Manage a collection of drawables for batched rendering.

Many drawable pyglet objects accept an optional Batch argument in their constructors. By giving a Batch to multiple objects, you can tell pyglet that you expect to draw all of these objects at once, so it can optimise its use of OpenGL. Hence, drawing a Batch is often much faster than drawing each contained drawable separately.

The following example creates a batch, adds two sprites to the batch, and then draws the entire batch:

batch = pyglet.graphics.Batch()
car = pyglet.sprite.Sprite(car_image, batch=batch)
boat = pyglet.sprite.Sprite(boat_image, batch=batch)

def on_draw():
    batch.draw()

While any drawables can be added to a Batch, only those with the same draw mode, shader program, and group can be optimised together.

Internally, a Batch manages a set of VertexDomains along with information about how the domains are to be drawn. To implement batching on a custom drawable, get your vertex domains from the given batch instead of setting them up yourself.

__init__() None

Create a graphics batch.

draw() None

Draw the batch.

Return type:

None

draw_subset(
vertex_lists: Sequence[VertexList | IndexedVertexList],
) None

Draw only some vertex lists in the batch.

The use of this method is highly discouraged, as it is quite inefficient. Usually an application can be redesigned so that batches can always be drawn in their entirety, using draw.

The given vertex lists must belong to this batch; behaviour is undefined if this condition is not met.

Parameters:

vertex_lists (Sequence[VertexList | IndexedVertexList]) – Vertex lists to draw.

Return type:

None

get_domain(
indexed: bool,
instanced: bool,
mode: int,
group: Group,
attributes: dict[str, Any],
) VertexDomain | IndexedVertexDomain | InstancedVertexDomain | InstancedIndexedVertexDomain

Get, or create, the vertex domain corresponding to the given arguments.

mode is the render mode such as GL_LINES or GL_TRIANGLES

Return type:

VertexDomain | IndexedVertexDomain | InstancedVertexDomain | InstancedIndexedVertexDomain

invalidate() None

Force the batch to update the draw list.

This method can be used to force the batch to re-compute the draw list when the ordering of groups has changed. :rtype: None

New in version 1.2.

migrate(
vertex_list: VertexList | IndexedVertexList,
mode: int,
group: Group,
batch: Batch,
) None

Migrate a vertex list to another batch and/or group.

vertex_list and mode together identify the vertex list to migrate. group and batch are new owners of the vertex list after migration.

The results are undefined if mode is not correct or if vertex_list does not belong to this batch (they are not checked and will not necessarily throw an exception immediately).

batch can remain unchanged if only a group change is desired.

Parameters:
  • vertex_list (VertexList | IndexedVertexList) – A vertex list currently belonging to this batch.

  • mode (int) – The current GL drawing mode of the vertex list.

  • group (Group) – The new group to migrate to.

  • batch (Batch) – The batch to migrate to (or the current batch).

Return type:

None

update_shader(
vertex_list: VertexList | IndexedVertexList,
mode: int,
group: Group,
program: ShaderProgram,
) bool

Migrate a vertex list to another domain that has the specified shader attributes.

The results are undefined if mode is not correct or if vertex_list does not belong to this batch (they are not checked and will not necessarily throw an exception immediately).

Parameters:
  • vertex_list (VertexList | IndexedVertexList) – A vertex list currently belonging to this batch.

  • mode (int) – The current GL drawing mode of the vertex list.

  • group (Group) – The new group to migrate to.

  • program (ShaderProgram) – The new shader program to migrate to.

Return type:

bool

Returns:

False if the domain’s no longer match. The caller should handle this scenario.

group_children: dict[Group, list[Group]]
group_map: dict[Group, dict[Tuple[bool, int, int, str], VertexDomain]]
top_groups: list[Group]
class Group

Group of common OpenGL state.

Group provides extra control over how drawables are handled within a Batch. When a batch draws a drawable, it ensures its group’s state is set; this can include binding textures, shaders, or setting any other parameters. It also sorts the groups before drawing.

In the following example, the background sprite is guaranteed to be drawn before the car and the boat:

batch = pyglet.graphics.Batch()
background = pyglet.graphics.Group(order=0)
foreground = pyglet.graphics.Group(order=1)

background = pyglet.sprite.Sprite(background_image, batch=batch, group=background)
car = pyglet.sprite.Sprite(car_image, batch=batch, group=foreground)
boat = pyglet.sprite.Sprite(boat_image, batch=batch, group=foreground)

def on_draw():
    batch.draw()
__init__(order: int = 0, parent: Group | None = None) None

Initialize a rendering group.

Parameters:
  • order (int) – Set the order to render above or below other Groups. Lower orders are drawn first.

  • parent (Group | None) – Group to contain this Group; its state will be set before this Group’s state.

set_state() None

Apply the OpenGL state change.

The default implementation does nothing.

Return type:

None

set_state_recursive() None

Set this group and its ancestry.

Call this method if you are using a group in isolation: the parent groups will be called in top-down order, with this class’s set being called last.

Return type:

None

unset_state() None

Repeal the OpenGL state change.

The default implementation does nothing.

Return type:

None

unset_state_recursive() None

Unset this group and its ancestry.

The inverse of set_state_recursive.

Return type:

None

property batches: tuple[pyglet.graphics.Batch, ...]

Which graphics Batches this Group is a part of.

Read Only.

property order: int

Rendering order of this group compared to others.

Lower numbers are drawn first.

property visible: bool

Visibility of the group in the rendering pipeline.

Determines whether this Group is visible in any of the Batches it is assigned to. If False, objects in this Group will not be rendered.

class ShaderGroup

A group that enables and binds a ShaderProgram.

__init__(
program: ShaderProgram,
order: int = 0,
parent: Group | None = None,
) None

Initialize a rendering group.

Parameters:
  • order (int) – Set the order to render above or below other Groups. Lower orders are drawn first.

  • parent (Group | None) – Group to contain this Group; its state will be set before this Group’s state.

set_state() None

Apply the OpenGL state change.

The default implementation does nothing.

Return type:

None

unset_state() None

Repeal the OpenGL state change.

The default implementation does nothing.

Return type:

None

class TextureGroup

A group that enables and binds a texture.

TextureGroups are equal if their textures’ targets and names are equal.

__init__(
texture: Texture,
order: int = 0,
parent: Group | None = None,
) None

Create a texture group.

Parameters:
  • texture (Texture) – Texture to bind.

  • order (int) – Change the order to render above or below other Groups.

  • parent (Group | None) – Parent group.

set_state() None

Apply the OpenGL state change.

The default implementation does nothing.

Return type:

None

draw(size: int, mode: int, **data: Any) None

Draw a primitive immediately.

Warning:

This function is deprecated as of 2.0.4, and will be removed in the next release.

Parameters:
  • size (int) – Number of vertices given

  • mode (int) – OpenGL drawing mode, e.g. GL_TRIANGLES, avoiding quotes.

  • **data (Any) – keyword arguments for passing vertex attribute data. The keyword should be the vertex attribute name, and the argument should be a tuple of (format, data). For example: position=(‘f’, array)

Return type:

None

draw_indexed(
size: int,
mode: int,
indices: Sequence[int],
**data: Any,
) None

Draw a primitive with indexed vertices immediately.

Warning:

This function is deprecated as of 2.0.4, and will be removed in the next release.

Parameters:
  • size (int) – Number of vertices given

  • mode (int) – OpenGL drawing mode, e.g. GL_TRIANGLES

  • indices (Sequence[int]) – Sequence of integers giving indices into the vertex list.

  • **data (Any) – keyword arguments for passing vertex attribute data. The keyword should be the vertex attribute name, and the argument should be a tuple of (format, data). For example: position=(‘f’, array)

Return type:

None

get_default_batch() Batch

Batch used globally for objects that have no Batch specified.

Return type:

Batch

get_default_blit_shader() ShaderProgram

A default basic shader for blitting, provides no blending.

Return type:

ShaderProgram

get_default_shader() ShaderProgram

A default basic shader for default batches.

Return type:

ShaderProgram