moderngl_window.scene

class moderngl_window.scene.Camera(fov: float = 60.0, aspect_ratio: float = 1.0, near: float = 1.0, far: float = 100.0)[source]

Simple camera class containing projection.

# create a camera
camera = Camera(fov=60.0, aspect_ratio=1.0, near=1.0, far=100.0)

# Get the current camera matrix as numpy array
print(camera.matrix)

# Get projection matrix as numpy array
print(camera.projection.matrix)
look_at(vec: vec3 | None = None, pos: tuple[float, float, float] | None = None) mat4x4[source]

Look at a specific point

Either vec or pos needs to be supplied.

Keyword Arguments:
  • vec (glm.vec3) – position

  • pos (tuple/list) – list of tuple [x, y, x] / (x, y, x)

Returns:

Camera matrix

Return type:

glm.mat4x4

property matrix: mat4x4

The current view matrix for the camera

Type:

glm.mat4

property pitch: float

The current pitch angle.

Type:

float

property projection: Projection3D

The 3D projection

Type:

Projection3D

set_position(x: float, y: float, z: float) None[source]

Set the 3D position of the camera.

Parameters:
  • x (float) – x position

  • y (float) – y position

  • z (float) – z position

set_rotation(yaw: float, pitch: float) None[source]

Set the rotation of the camera.

Parameters:
  • yaw (float) – yaw rotation

  • pitch (float) – pitch rotation

property yaw: float

The current yaw angle.

Type:

float

class moderngl_window.scene.KeyboardCamera(keys: ~moderngl_window.context.base.keys.BaseKeys, keymap: ~typing.Callable[[~moderngl_window.context.base.keys.BaseKeys], ~moderngl_window.utils.keymaps.KeyMap] = <function <lambda>>, fov: float = 60.0, aspect_ratio: float = 1.0, near: float = 1.0, far: float = 100.0)[source]

Bases: Camera

Camera controlled by mouse and keyboard. The class interacts with the key constants in the built in window types.

Creating a keyboard camera:

camera = KeyboardCamera(
    self.wnd.keys,
    fov=75.0,
    aspect_ratio=self.wnd.aspect_ratio,
    near=0.1,
    far=1000.0,
)

We can also interact with the belonging Projection3D instance.

# Update aspect ratio
camera.projection.update(aspect_ratio=1.0)

# Get projection matrix in bytes (f4)
camera.projection.tobytes()
key_input(key: str, action: str, modifiers: Any) None[source]

Process key inputs and move camera

Parameters:
  • key – The key

  • action – key action release/press

  • modifiers – key modifier states such as ctrl or shit

property matrix: mat4x4

The current view matrix for the camera

Type:

glm.mat4x4

property mouse_sensitivity: float

Mouse sensitivity (rotation speed).

This property can also be set:

camera.mouse_sensitivity = 2.5
Type:

float

move_backward(activate: bool) None[source]

The camera should be continiously moving backwards.

Parameters:

activate (bool) – Activate or deactivate this state

move_down(activate: bool) None[source]

The camera should be continiously moving down.

Parameters:

activate (bool) – Activate or deactivate this state

move_forward(activate: bool) None[source]

The camera should be continiously moving forward.

Parameters:

activate (bool) – Activate or deactivate this state

move_left(activate: bool) None[source]

The camera should be continiously moving to the left.

Parameters:

activate (bool) – Activate or deactivate this state

move_right(activate: bool) None[source]

The camera should be continiously moving to the right.

Parameters:

activate (bool) – Activate or deactivate this state

move_state(direction: int, activate: bool) None[source]

Set the camera position move state.

Parameters:
  • direction – What direction to update

  • activate – Start or stop moving in the direction

move_up(activate: bool) None[source]

The camera should be continiously moving up.

Parameters:

activate (bool) – Activate or deactivate this state

rot_state(dx: float, dy: float) None[source]

Update the rotation of the camera.

This is done by passing in the relative mouse movement change on x and y (delta x, delta y).

In the past this method took the viewport position of the mouse. This does not work well when mouse exclusivity mode is enabled.

Parameters:
  • dx – Relative mouse position change on x

  • dy – Relative mouse position change on y

property velocity: float

The speed this camera move based on key inputs

The property can also be modified:

camera.velocity = 5.0
Type:

float

class moderngl_window.scene.OrbitCamera(target: vec3 | tuple[float, float, float] = (0.0, 0.0, 0.0), radius: float = 2.0, angles: tuple[float, float] = (45.0, -45.0), **kwargs: Any)[source]

Bases: Camera

Camera controlled by the mouse to pan around the target.

The functions :py:function:`~camera.OrbitCamera.rot_state` and :py:function:`~camera.OrbitCamera.rot_state` are used to update the rotation and zoom.

Creating a orbit camera:

camera = OrbitCamera(
    target=(0., 0., 0.),
    radius=2.0
    fov=75.0,
    aspect_ratio=self.wnd.aspect_ratio,
    near=0.1,
    far=1000.0,
)

We can also interact with the belonging Projection3D instance.

# Update aspect ratio
camera.projection.update(aspect_ratio=1.0)

# Get projection matrix in bytes (f4)
camera.projection.tobytes()
property angle_x: float

camera angle x in degrees.

This property can also be set::

camera.angle_x = 45.

Type:

float

property angle_y: float

camera angle y in degrees.

This property can also be set::

camera.angle_y = 45.

Type:

float

property matrix: mat4x4

The current view matrix for the camera

Type:

glm.mat4

property mouse_sensitivity: float

Mouse sensitivity (rotation speed).

This property can also be set::

camera.mouse_sensitivity = 2.5

Type:

float

rot_state(dx: float, dy: float) None[source]

Update the rotation of the camera around the target point.

This is done by passing relative mouse change in the x and y axis (delta x, delta y)

Parameters:
  • dx – Relative mouse position change on x axis

  • dy – Relative mouse position change on y axis

property zoom_sensitivity: float

Mousewheel zooming sensitivity (zoom speed).

This property can also be set::

camera.zoom_sensitivity = 2.5

Type:

float

class moderngl_window.scene.Scene(name: str | None, **kwargs: Any)[source]

Generic scene

apply_mesh_programs(mesh_programs: list[MeshProgram] | None = None, clear: bool = True) None[source]

Applies mesh programs to meshes. If not mesh programs are passed in we assign default ones.

Parameters:
  • mesh_programs (list) – List of mesh programs to assign

  • clear (bool) – Clear all assigned mesh programs

calc_scene_bbox() None[source]

Calculate scene bbox

property ctx: Context

The current context

Type:

moderngl.Context

destroy() None[source]

Destroys the scene data and vertex buffers

draw(projection_matrix: mat4x4 | None, camera_matrix: mat4x4 | None, time: float = 0.0) None[source]

Draw all the nodes in the scene.

Parameters:
  • projection_matrix (ndarray) – projection matrix (bytes)

  • camera_matrix (ndarray) – camera_matrix (bytes)

  • time (float) – The current time

draw_bbox(projection_matrix: mat4x4 | None = None, camera_matrix: mat4x4 | None = None, children: float = True, color: tuple[float, float, float] = (0.75, 0.75, 0.75)) None[source]

Draw scene and mesh bounding boxes.

Parameters:
  • projection_matrix (glm.mat4) – mat4 projection

  • camera_matrix (glm.mat4) – mat4 camera matrix

  • children (bool) – Will draw bounding boxes for meshes as well

  • color (tuple) – Color of the bounding boxes

draw_wireframe(projection_matrix: mat4x4 | None = None, camera_matrix: mat4x4 | None = None, color: tuple[float, float, float, float] = (0.75, 0.75, 0.75, 1.0)) None[source]

Render the scene in wireframe mode.

Parameters:
  • projection_matrix (ndarray) – mat4 projection

  • camera_matrix (ndarray) – mat4 camera matrix

  • children (bool) – Will draw bounding boxes for meshes as well

  • color (tuple) – Color of the wireframes

find_material(name: str | None = None) Material | None[source]

Finds a Material

Keyword Arguments:

name (str) – Case sensitive material name

Returns:

A Material or None

find_node(name: str | None = None) Node | None[source]

Finds a Node

Keyword Arguments:

name (str) – Case sensitive name

Returns:

A Node or None if not found.

get_center() vec3[source]

Calculate the center of the scene using bounding boxes

property matrix: mat4x4

The current model matrix

This property is settable.

Type:

glm.mat4x4

prepare() None[source]

prepare the scene for rendering.

Calls apply_mesh_programs() assigning default meshprograms if needed and sets the model matrix.

release() None[source]

Destroys the scene data and vertex buffers

class moderngl_window.scene.Node(name: str | None = None, camera: Camera | None = None, mesh: Mesh | None = None, matrix: mat4x4 | None = None)[source]

A generic scene node containing a mesh or camera and/or a container for other nodes. Nodes and their children represents the scene tree.

add_child(node: Node) None[source]

Add a child to this node

Parameters:

node (Node) – Node to add as a child

calc_global_bbox(view_matrix: mat4x4, bbox_min: vec3 | None, bbox_max: vec3 | None) tuple[vec3, vec3][source]

Recursive calculation of scene bbox.

Keyword Arguments:
  • view_matrix (numpy.ndarray) – view matrix

  • bbox_min – min bbox values

  • bbox_max – max bbox values

calc_model_mat(parent_matrix: mat4x4) None[source]

Calculate the model matrix related to all parents.

Parameters:

parent_matrix – Matrix for parent node

property camera: Camera | None

The camera if present

Type:

Camera

property children: list[Node]

List of children

Type:

list

draw(projection_matrix: mat4x4, camera_matrix: mat4x4, time: float = 0.0) None[source]

Draw node and children.

Keyword Arguments:
  • projection_matrix – projection matrix

  • camera_matrix – camera_matrix

  • time – The current time

draw_bbox(projection_matrix: mat4x4 | None, camera_matrix: mat4x4 | None, program: Program, vao: VAO) None[source]

Draw bounding box around the node and children.

Keyword Arguments:
  • projection_matrix – projection matrix

  • camera_matrix – camera_matrix

  • program (moderngl.Program) – The program to render the bbox

  • vao – The vertex array representing the bounding box

draw_wireframe(projection_matrix: mat4x4 | None, camera_matrix: mat4x4 | None, program: Program) None[source]

Render the node as wireframe.

Keyword Arguments:
  • projection_matrix (bytes) – projection matrix

  • camera_matrix (bytes) – camera_matrix

  • program (moderngl.Program) – The program to render wireframe

property matrix: mat4x4 | None

Note matrix (local)

Type:

glm.mat4x4

property matrix_global: mat4x4 | None

The global node matrix containing transformations from parent nodes

Type:

glm.matx4

property mesh: Mesh | None

The mesh if present

Type:

Mesh

property name: str | None

Get or set the node name

Type:

str

class moderngl_window.scene.Mesh(name: str, vao: VAO | None = None, material: Material | None = None, attributes: dict[str, Any] | None = None, bbox_min: vec3 = vec3(0, 0, 0), bbox_max: vec3 = vec3(0, 0, 0))[source]

Mesh info and geometry

add_attribute(attr_type: str, name: str, components: int) None[source]

Add metadata about the mesh :param attr_type: POSITION, NORMAL etc :param name: The attribute name used in the program :param components: Number of floats

calc_global_bbox(view_matrix: mat4x4, bbox_min: vec3 | None, bbox_max: vec3 | None) tuple[vec3, vec3][source]

Calculates the global bounding.

Parameters:
  • view_matrix – View matrix

  • bbox_min – xyz min

  • bbox_max – xyz max

Returns:

Combined bbox

Return type:

bbox_min, bbox_max

draw(projection_matrix: mat4x4, model_matrix: mat4x4, camera_matrix: mat4x4, time: float = 0.0) None[source]

Draw the mesh using the assigned mesh program

Keyword Arguments:
  • projection_matrix (bytes) – projection_matrix

  • view_matrix (bytes) – view_matrix

  • camera_matrix (bytes) – camera_matrix

draw_bbox(proj_matrix: mat4x4, model_matrix: mat4x4, cam_matrix: mat4x4, program: Program, vao: VAO) None[source]

Renders the bounding box for this mesh.

Parameters:
  • proj_matrix – Projection matrix

  • model_matrix – View/model matrix

  • cam_matrix – Camera matrix

  • program – The moderngl.Program rendering the bounding box

  • vao – The vao mesh for the bounding box

draw_wireframe(proj_matrix: mat4x4, model_matrix: mat4x4, program: Program) None[source]

Render the mesh as wireframe.

proj_matrix: Projection matrix model_matrix: View/model matrix program: The moderngl.Program rendering the wireframe

has_normals() bool[source]
Returns:

Does the mesh have a normals?

Return type:

bool

has_uvs(layer: int = 0) bool[source]
Returns:

Does the mesh have texture coordinates?

Return type:

bool

class moderngl_window.scene.Material(name: str = '')[source]

Generic material

property color: tuple[float, float, float, float]

RGBA color

Type:

tuple[float, float, float, float]

property double_sided: bool

Material surface is double sided?

Type:

bool

property mat_texture: MaterialTexture | None

instance

Type:

MaterialTexture

property name: str

Name of the material

Type:

str

class moderngl_window.scene.MaterialTexture(texture: Texture | None = None, sampler: Sampler | None = None)[source]

Wrapper for textures used in materials. Contains a texture and a sampler object.

property sampler: Sampler | None

Sampler instance

Type:

moderngl.Sampler

property texture: Texture | None

Texture instance

Type:

moderngl.Texture

class moderngl_window.scene.MeshProgram(program: Program | None = None, **kwargs: Any)[source]

Describes how a mesh is rendered using a specific shader program

apply(mesh: Mesh) MeshProgram | None[source]

Determine if this MeshProgram should be applied to the mesh. Can return self or some MeshProgram instance to support dynamic MeshProgram creation

Parameters:

mesh – The mesh to inspect

property ctx: Context

The current context

Type:

moderngl.Context

draw(mesh: Mesh, projection_matrix: mat4x4, model_matrix: mat4x4, camera_matrix: mat4x4, time: float = 0.0) None[source]

Draw code for the mesh

Parameters:

mesh (Mesh) – The mesh to render

Keyword Arguments:
  • projection_matrix (numpy.ndarray) – projection_matrix (bytes)

  • model_matrix (numpy.ndarray) – view_matrix (bytes)

  • camera_matrix (numpy.ndarray) – camera_matrix (bytes)

  • time (float) – The current time

Mesh Programs

class moderngl_window.scene.programs.VertexColorProgram(program: Program | None = None, **kwargs: Any)[source]

Bases: MeshProgram

Vertex color program

apply(mesh: Mesh) MeshProgram | None[source]

Determine if this MeshProgram should be applied to the mesh. Can return self or some MeshProgram instance to support dynamic MeshProgram creation

Parameters:

mesh – The mesh to inspect

draw(mesh: Mesh, projection_matrix: mat4x4, model_matrix: mat4x4, camera_matrix: mat4x4, time: float = 0.0) None[source]

Draw code for the mesh

Parameters:

mesh (Mesh) – The mesh to render

Keyword Arguments:
  • projection_matrix (numpy.ndarray) – projection_matrix (bytes)

  • model_matrix (numpy.ndarray) – view_matrix (bytes)

  • camera_matrix (numpy.ndarray) – camera_matrix (bytes)

  • time (float) – The current time

class moderngl_window.scene.programs.ColorLightProgram(program: Program | None = None, **kwargs: Any)[source]

Bases: MeshProgram

Simple color program with light

apply(mesh: Mesh) MeshProgram | None[source]

Determine if this MeshProgram should be applied to the mesh. Can return self or some MeshProgram instance to support dynamic MeshProgram creation

Parameters:

mesh – The mesh to inspect

draw(mesh: Mesh, projection_matrix: mat4x4, model_matrix: mat4x4, camera_matrix: mat4x4, time: float = 0.0) None[source]

Draw code for the mesh

Parameters:

mesh (Mesh) – The mesh to render

Keyword Arguments:
  • projection_matrix (numpy.ndarray) – projection_matrix (bytes)

  • model_matrix (numpy.ndarray) – view_matrix (bytes)

  • camera_matrix (numpy.ndarray) – camera_matrix (bytes)

  • time (float) – The current time

class moderngl_window.scene.programs.TextureProgram(program: Program | None = None, **kwargs: Any)[source]

Bases: MeshProgram

Plan textured

apply(mesh: Mesh) MeshProgram | None[source]

Determine if this MeshProgram should be applied to the mesh. Can return self or some MeshProgram instance to support dynamic MeshProgram creation

Parameters:

mesh – The mesh to inspect

draw(mesh: Mesh, projection_matrix: mat4x4, model_matrix: mat4x4, camera_matrix: mat4x4, time: float = 0.0) None[source]

Draw code for the mesh

Parameters:

mesh (Mesh) – The mesh to render

Keyword Arguments:
  • projection_matrix (numpy.ndarray) – projection_matrix (bytes)

  • model_matrix (numpy.ndarray) – view_matrix (bytes)

  • camera_matrix (numpy.ndarray) – camera_matrix (bytes)

  • time (float) – The current time

class moderngl_window.scene.programs.TextureVertexColorProgram(program: Program | None = None, **kwargs: Any)[source]

Bases: MeshProgram

textured object with vertex color

apply(mesh: Mesh) MeshProgram | None[source]

Determine if this MeshProgram should be applied to the mesh. Can return self or some MeshProgram instance to support dynamic MeshProgram creation

Parameters:

mesh – The mesh to inspect

draw(mesh: Mesh, projection_matrix: mat4x4, model_matrix: mat4x4, camera_matrix: mat4x4, time: float = 0.0) None[source]

Draw code for the mesh

Parameters:

mesh (Mesh) – The mesh to render

Keyword Arguments:
  • projection_matrix (numpy.ndarray) – projection_matrix (bytes)

  • model_matrix (numpy.ndarray) – view_matrix (bytes)

  • camera_matrix (numpy.ndarray) – camera_matrix (bytes)

  • time (float) – The current time

class moderngl_window.scene.programs.TextureLightProgram(program: Program | None = None, **kwargs: Any)[source]

Bases: MeshProgram

Simple texture program

apply(mesh: Mesh) MeshProgram | None[source]

Determine if this MeshProgram should be applied to the mesh. Can return self or some MeshProgram instance to support dynamic MeshProgram creation

Parameters:

mesh – The mesh to inspect

draw(mesh: Mesh, projection_matrix: mat4x4, model_matrix: mat4x4, camera_matrix: mat4x4, time: float = 0.0) None[source]

Draw code for the mesh

Parameters:

mesh (Mesh) – The mesh to render

Keyword Arguments:
  • projection_matrix (numpy.ndarray) – projection_matrix (bytes)

  • model_matrix (numpy.ndarray) – view_matrix (bytes)

  • camera_matrix (numpy.ndarray) – camera_matrix (bytes)

  • time (float) – The current time

class moderngl_window.scene.programs.TextureLightColorProgram[source]

Bases: object

class moderngl_window.scene.programs.FallbackProgram(program: Program | None = None, **kwargs: Any)[source]

Bases: MeshProgram

Fallback program only rendering positions in white

apply(mesh: Mesh) MeshProgram | None[source]

Determine if this MeshProgram should be applied to the mesh. Can return self or some MeshProgram instance to support dynamic MeshProgram creation

Parameters:

mesh – The mesh to inspect

draw(mesh: Mesh, projection_matrix: mat4x4, model_matrix: mat4x4, camera_matrix: mat4x4, time: float = 0.0) None[source]

Draw code for the mesh

Parameters:

mesh (Mesh) – The mesh to render

Keyword Arguments:
  • projection_matrix (numpy.ndarray) – projection_matrix (bytes)

  • model_matrix (numpy.ndarray) – view_matrix (bytes)

  • camera_matrix (numpy.ndarray) – camera_matrix (bytes)

  • time (float) – The current time