pyglet.graphics.draw

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__(
context: SurfaceContext | None = None,
initial_count: int = 32,
) None

Initialize a graphics batch.

Parameters:
  • context (SurfaceContext | None) – The SurfaceContext this batch will create resources against.

  • initial_count (int) – The initial vertex count of the buffers created by this batch.

delete_empty_domains() None

Deletes all empty domains and all of their buffers.

Should not need to be called through normal usage, as this will occur periodically.

Return type:

None

draw() None

Draw the batch.

If the draw list is dirty, a new one will be created and applied.

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

draw_with_options() Generator[BatchDrawOptions, Any, None]

A context manager for specifying draw options before drawing.

Can be used as a context manager:

with batch.draw_with_options() as options:
    options.camera = new_camera

Upon exit of the context, the draw list functions will be called.

Added in version 3.0.

Return type:

Generator[BatchDrawOptions, Any, None]

get_domain(
indexed: bool,
instanced: bool,
mode: GeometryMode,
group: Group,
attributes: dict[str, Any],
) VertexDomain

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

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.

Added in version 1.2.

Return type:

None

migrate(
vertex_list: VertexList | IndexedVertexList,
mode: GeometryMode,
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 (GeometryMode) – 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: GeometryMode,
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:
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[_DomainKey, VertexDomain]]
initial_count: int
top_groups: list[Group]
class BatchDrawOptions

A draw pass encompasses the starting data of a batched draw.

A user may fill some or none of the arguments.

__init__(
framebuffer: object | None = None,
camera: BaseCamera | None = None,
viewport: tuple | None = None,
scissor: tuple | CameraScissor | None = None,
clear_color: tuple[float, float, float, float] | None = None,
) None
resolve(
ctx: SurfaceContext,
) DrawPass

Resolves the draw options to give a final DrawPass.

Return type:

DrawPass

camera: BaseCamera | None = None

The camera to use at the start. Some passes may require drawing the same scene with a different camera.

clear_color: tuple[float, float, float, float] | None = None
framebuffer: object | None = None

The framebuffer render target. If not specified, the window framebuffer.

scissor: tuple | CameraScissor | None = None
viewport: tuple | None = None
class DrawContext

This temporary context is passed to Group states during batch draw.

The data in this object is only valid during the batch draw call.

__init__(
surface_ctx: SurfaceContextT,
backend_ctx: BackendContextT,
draw_pass: DrawPass,
renderer: BackendRenderer,
camera_stack: list[CameraScopeProtocol] = <factory>,
viewport_stack: list = <factory>,
scissor_stack: list = <factory>,
active_shader_program: ShaderProgram | None = None,
_applied_camera: CameraScopeProtocol | None = None,
) None
apply_camera_scope(*, commit: bool = True) None
Return type:

None

apply_clear_color(r: float, g: float, b: float, a: float) None
Return type:

None

apply_scissor() None
Return type:

None

apply_viewport() None
Return type:

None

begin() None
Return type:

None

property active_camera: CameraScopeProtocol
property active_scissor: tuple | None
active_shader_program: ShaderProgram | None = None
property active_viewport: tuple | None
backend_ctx: BackendContextT
camera_stack: list[CameraScopeProtocol]
draw_pass: DrawPass
renderer: BackendRenderer
scissor_stack: list
surface_ctx: SurfaceContextT
viewport_stack: list
class DrawPass

This is the resolved state of the DrawPass.

This class is guaranteed to have all the arguments filled after the backend resolves it.

__init__(
framebuffer: object | None,
camera: BaseCamera,
viewport: tuple,
scissor: CameraScissor,
clear_color: tuple[float, float, float, float],
) None
camera: BaseCamera
clear_color: tuple[float, float, float, float]
framebuffer: object | None
scissor: CameraScissor
viewport: tuple
class Group

Group of common 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.

add_comparison(value)
set_blend(
blend_src: BlendFactor,
blend_dst: BlendFactor,
blend_op: BlendOp = BlendOp.ADD,
)
set_camera(camera: CameraScopeProtocol) None

Set a camera scope for this group.

The camera object is applied during batch draw inside a draw context. If the camera/view provides an effective scissor area, a matching camera scissor state is attached automatically.

Return type:

None

set_depth_test(func: CompareOp) None
Return type:

None

set_scissor(scissor_object: ScissorProtocol) None
Return type:

None

set_shader_program(program: ShaderProgram)
set_shader_uniforms(
program: ShaderProgram,
uniforms: dict[str, Any],
) None
Return type:

None

set_state(state: State) None

Sets a state to be applied to the group.

If the state is an enforced state, setting a new state will not update any children.

Return type:

None

set_state_all(ctx: DrawContext) None

Calls all set states of the underlying Group.

Return type:

None

set_state_recursive(ctx: DrawContext) 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

set_texture(
texture: Texture,
texture_unit: int = 0,
set_id: int = 0,
) None

Set the texture state.

Parameters:
  • texture (Texture) – The Texture instance that this draw call uses.

  • texture_unit (int) – The binding unit this Texture/Sampler is bound to. In OpenGL this is the Active Texture (glActiveTexture). In Vulkan this is the Sampler binding number in the descriptor.

  • set_id (int) – The set that the sampler belongs to. Only applicable in Vulkan.

Return type:

None

set_textures(
textures: dict[str, Texture],
program: ShaderProgram,
first_texture_unit: int = 0,
set_id: int = 0,
) None

Set multiple texture states and matching sampler uniforms.

Parameters:
  • textures (dict[str, Texture]) – Mapping of shader sampler uniform names to Texture instances.

  • program (ShaderProgram) – The shader program that owns the sampler uniforms.

  • first_texture_unit (int) – The first binding unit to use. Each texture uses the next unit.

  • set_id (int) – The set that the sampler belongs to. Only applicable in Vulkan.

Return type:

None

set_uniform_buffer(
region: UniformBufferRegion,
binding_index: int | None = None,
) None

Set a Uniform Buffer Object region state.

Parameters:
  • region (UniformBufferRegion) – A region created by UniformBlock.create_ubo_region.

  • binding_index (int | None) – Optional binding point override. By default, the region uses the binding point assigned to its source uniform block.

Return type:

None

set_viewport(x, y, width, height)
unset_state_all(ctx: DrawContext) None

Calls all unset states of the underlying Group.

Return type:

None

unset_state_recursive(ctx: DrawContext) None

Unset this group and its ancestry.

The inverse of set_state_recursive.

Return type:

None

property batches: tuple[Batch, ...]

Which graphics Batches this Group is a part of.

Read Only.

property has_enforced_states: bool

If this group has states that automatically apply to children.

property order: int

Rendering order of this group compared to others.

Lower numbers are drawn first.

property states: tuple[State, ...]

The states that will apply to members of this group.

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.

get_default_batch() Batch

The built in batch object used for objects that have no specified batch.

Return type:

Batch