moderngl_window.loaders

class moderngl_window.loaders.base.BaseLoader(meta: ResourceDescription)[source]

Bases: object

Base loader class for all resources

property ctx: Context

ModernGL context

Type:

moderngl.Context

file_extensions: list[list[str]] = []

A list defining the file extensions accepted by this loader.

Example:

# Loader will match .xyz and .xyz.gz files.
file_extensions = [
    ['.xyz'],
    ['.xyz', '.gz'],
]
find_data(path: str | Path | None) Path | None[source]

Find resource using data finders.

This is mainly a shortcut method to simplify the task.

Parameters:

path – Path to resource

find_program(path: str | Path | None) Path | None[source]

Find resource using program finders.

This is mainly a shortcut method to simplify the task.

Parameters:

path – Path to resource

find_scene(path: str | Path | None) Path | None[source]

Find resource using scene finders.

This is mainly a shortcut method to simplify the task.

Parameters:

path – Path to resource

find_texture(path: str | Path | None) Path | None[source]

Find resource using texture finders.

This is mainly a shortcut method to simplify the task.

Parameters:

path – Path to resource

kind = 'unknown'

The kind of resource this loaded supports. This can be used when file extensions is not enough to decide what loader should be selected.

load() Any[source]

Loads a resource.

When creating a loader this is the only method that needs to be implemented.

Returns:

The loaded resource

classmethod supports_file(meta: ResourceDescription) bool[source]

Check if the loader has a supported file extension.

What extensions are supported can be defined in the file_extensions class attribute.

Text

class moderngl_window.loaders.data.text.Loader(meta: ResourceDescription)[source]

Bases: BaseLoader

file_extensions: list[list[str]] = [['.txt']]

A list defining the file extensions accepted by this loader.

Example:

# Loader will match .xyz and .xyz.gz files.
file_extensions = [
    ['.xyz'],
    ['.xyz', '.gz'],
]
kind = 'text'

The kind of resource this loaded supports. This can be used when file extensions is not enough to decide what loader should be selected.

load() str[source]

Load a file in text mode.

Returns:

The string contents of the file

Return type:

str

JSON

class moderngl_window.loaders.data.json.Loader(meta: ResourceDescription)[source]

Bases: BaseLoader

file_extensions: list[list[str]] = [['.json']]

A list defining the file extensions accepted by this loader.

Example:

# Loader will match .xyz and .xyz.gz files.
file_extensions = [
    ['.xyz'],
    ['.xyz', '.gz'],
]
kind = 'json'

The kind of resource this loaded supports. This can be used when file extensions is not enough to decide what loader should be selected.

load() dict[Any, Any][source]

Load a file as json

Returns:

The json contents

Return type:

dict

Binary

class moderngl_window.loaders.data.binary.Loader(meta: ResourceDescription)[source]

Bases: BaseLoader

kind = 'binary'

The kind of resource this loaded supports. This can be used when file extensions is not enough to decide what loader should be selected.

load() bytes[source]

Load a file in binary mode

Returns:

The bytes contents of the file

Return type:

bytes

Program (Separate)

class moderngl_window.loaders.program.separate.Loader(meta: ResourceDescription)[source]

Bases: BaseLoader

kind = 'separate'

The kind of resource this loaded supports. This can be used when file extensions is not enough to decide what loader should be selected.

load() Program | ComputeShader | ReloadableProgram[source]

Loads a shader program were each shader is a separate file.

This detected and dictated by the kind in the ProgramDescription.

Returns:

The Program instance

Return type:

moderngl.Program

meta: ProgramDescription

Program (Single)

class moderngl_window.loaders.program.single.Loader(meta: ResourceDescription)[source]

Bases: BaseLoader

kind = 'single'

The kind of resource this loaded supports. This can be used when file extensions is not enough to decide what loader should be selected.

load() Program[source]

Loads a shader program from a single glsl file.

Each shader type is separated by preprocessors

  • VERTEX_SHADER

  • FRAGMENT_SHADER

  • GEOMETRY_SHADER

  • TESS_CONTROL_SHADER

  • TESS_EVALUATION_SHADER

Example:

#version 330

#if defined VERTEX_SHADER

in vec3 in_position;
in vec2 in_texcoord_0;
out vec2 uv0;

void main() {
    gl_Position = vec4(in_position, 1);
    uv0 = in_texcoord_0;
}

#elif defined FRAGMENT_SHADER

out vec4 fragColor;
uniform sampler2D texture0;
in vec2 uv0;

void main() {
    fragColor = texture(texture0, uv0);
}
#endif
Returns:

The Program instance

Return type:

moderngl.Program

meta: ProgramDescription

GLTF2

class moderngl_window.loaders.scene.gltf2.Loader(meta: SceneDescription)[source]

Bases: BaseLoader

Loader for GLTF 2.0 files

file_extensions: list[list[str]] = [['.gltf'], ['.glb']]

A list defining the file extensions accepted by this loader.

Example:

# Loader will match .xyz and .xyz.gz files.
file_extensions = [
    ['.xyz'],
    ['.xyz', '.gz'],
]
kind = 'gltf'

The kind of resource this loaded supports. This can be used when file extensions is not enough to decide what loader should be selected.

load() Scene[source]

Load a GLTF 2 scene including referenced textures.

Returns:

The scene instance

Return type:

Scene

load_glb() None[source]

Loads a binary gltf file parsing its contents

load_gltf() None[source]

Loads a gltf json file parsing its contents

load_images() None[source]

Load images referenced in gltf metadata

load_materials() None[source]

Load materials referenced in gltf metadata

load_meshes() None[source]

Load meshes referenced in gltf metadata

load_node(meta: GLTFNode, parent: Node | None = None) Node[source]

Load a single node

load_nodes() None[source]

Load nodes referenced in gltf metadata

load_samplers() None[source]

Load samplers referenced in gltf metadata

load_textures() None[source]

Load textures referenced in gltf metadata

meta: SceneDescription
supported_extensions: list[str] = ['KHR_draco_mesh_compression', 'KHR_materials_unlit']

Supported GLTF extensions https://github.com/KhronosGroup/glTF/tree/master/extensions

Wavefront / OBJ

class moderngl_window.loaders.scene.wavefront.Loader(meta: SceneDescription)[source]

Bases: BaseLoader

Load wavefront/obj files

file_extensions: list[list[str]] = [['.obj'], ['.obj', '.gz'], ['.bin']]

A list defining the file extensions accepted by this loader.

Example:

# Loader will match .xyz and .xyz.gz files.
file_extensions = [
    ['.xyz'],
    ['.xyz', '.gz'],
]
kind = 'wavefront'

The kind of resource this loaded supports. This can be used when file extensions is not enough to decide what loader should be selected.

load() Scene[source]

Loads a wavefront/obj file including materials and textures

Returns:

The Scene instance

Return type:

Scene

meta: SceneDescription

STL

class moderngl_window.loaders.scene.stl.Loader(meta: ResourceDescription)[source]

Bases: BaseLoader

file_extensions: list[list[str]] = [['.stl'], ['.stl', '.gz']]

A list defining the file extensions accepted by this loader.

Example:

# Loader will match .xyz and .xyz.gz files.
file_extensions = [
    ['.xyz'],
    ['.xyz', '.gz'],
]
kind = 'stl'

The kind of resource this loaded supports. This can be used when file extensions is not enough to decide what loader should be selected.

load() Scene[source]

Loads and stl scene/file

Returns:

The Scene instance

Return type:

Scene

TextureArray

class moderngl_window.loaders.texture.array.Loader(meta: ResourceDescription)[source]

Bases: PillowLoader

kind = 'array'

The kind of resource this loaded supports. This can be used when file extensions is not enough to decide what loader should be selected.

load() TextureArray[source]

Load a texture array as described by the supplied TextureDescription`

Returns:

The TextureArray instance

Return type:

moderngl.TextureArray

meta: TextureDescription

TextureCube

class moderngl_window.loaders.texture.cube.Loader(meta: ResourceDescription)[source]

Bases: PillowLoader

kind = 'cube'

The kind of resource this loaded supports. This can be used when file extensions is not enough to decide what loader should be selected.

load() TextureCube[source]

Load a texture cube as described by the supplied TextureDescription`

Returns:

The TextureArray instance

Return type:

moderngl.TextureCube

meta: TextureDescription

Icon

class moderngl_window.loaders.texture.icon.IconLoader(meta: ResourceDescription)[source]

Bases: BaseLoader

find_icon() Path[source]

Find resource using texture finders.

This is mainly a shortcut method to simplify the task.

Parameters:

path – Path to resource

kind = 'icon'

The kind of resource this loaded supports. This can be used when file extensions is not enough to decide what loader should be selected.

meta: TextureDescription

Texture2D

class moderngl_window.loaders.texture.t2d.Loader(meta: ResourceDescription)[source]

Bases: PillowLoader

kind = '2d'

The kind of resource this loaded supports. This can be used when file extensions is not enough to decide what loader should be selected.

load() Texture[source]

Load a 2d texture as configured in the supplied TextureDescription

Returns:

The Texture instance

Return type:

moderngl.Texture

Pillow

class moderngl_window.loaders.texture.pillow.PillowLoader(meta: ResourceDescription)[source]

Bases: BaseLoader

Base loader using PIL/Pillow

image: Image
kind = '__unknown__'

The kind of resource this loaded supports. This can be used when file extensions is not enough to decide what loader should be selected.

load() Texture | TextureArray | TextureCube | Texture3D[source]

Loads a resource.

When creating a loader this is the only method that needs to be implemented.

Returns:

The loaded resource

meta: TextureDescription