peng3d.resource - Resource loading system

class peng3d.resource.ResourceManager(peng: peng3d.Peng, basepath: str)[source]

Manager that allows for efficient and simple loading and management of different kinds of resources.

Currently supports textures and models out of the box, but extension is possible.

Textures can be queried by any part of the application, they are only loaded on the first request and then cached for every request following it.

The same caching and lazy-loading principle applies to models loaded via this system.

addCategory(name: str, size: Optional[int] = None) → int[source]

Adds a new texture category with the given name.

If the category already exists, it will be overridden.

addFromTex(name: str, img: <Mock name='mock.AbstractImage' id='140340377213200'>, category: str) → Tuple[int, int, Tuple][source]

Adds a new texture from the given image.

img may be any object that supports Pyglet-style copying in form of the blit_to_texture() method.

This can be used to add textures that come from non-file sources, e.g. Render-to-texture.

getMissingTexture() → <Mock name='mock.AbstractImage' id='140340377213200'>[source]

Returns a texture to be used as a placeholder for missing textures.

A default missing texture file is provided in the assets folder of the source distribution. It consists of a simple checkerboard pattern of purple and black, this image may be copied to any project using peng3d for similar behavior.

If this texture cannot be found, a pattern is created in-memory, simply a solid square of purple.

This texture will also be cached separately from other textures.

getModel(name: str) → peng3d.model.Model[source]

Gets the model object by the given name.

If it was loaded previously, a cached version will be returned. If it was not loaded, it will be loaded and inserted into the cache.

getModelData(name: str) → Dict[KT, VT][source]

Gets the model data associated with the given name.

If it was loaded, a cached copy will be returned. It it was not loaded, it will be loaded and cached.

getTex(name: str, category: str) → Tuple[int, int, Tuple][source]

Gets the texture associated with the given name and category.

category must have been created using addCategory() before.

If it was loaded previously, a cached version will be returned. If it was not loaded, it will be loaded and inserted into the cache.

See loadTex() for more information.

loadModel(name: str) → peng3d.model.Model[source]

Loads the model of the given name.

The model will also be inserted into the cache.

loadModelData(name: str) → Dict[KT, VT][source]

Loads the model data of the given name.

The model file must always be a .json file.

loadTex(name: str, category: str) → Tuple[int, int, Tuple][source]

Loads the texture of the given name and category.

All textures currently must be PNG files, although support for more formats may be added soon.

If the texture cannot be found, a missing texture will instead be returned. See getMissingTexture() for more information.

Currently, all texture mipmaps will be generated and the filters will be set to GL_NEAREST for the magnification filter and GL_NEAREST_MIPMAP_LINEAR for the minification filter. This results in a pixelated texture and not a blurry one.

resourceExists(name: str, ext: str = '') → bool[source]

Returns whether or not the resource with the given name and extension exists.

This must not mean that the resource is meaningful, it simply signals that the file exists.

resourceNameToPath(name: str, ext: str = '') → str[source]

Converts the given resource name to a file path.

A resource path is of the format <app>:<cat1>.<cat2>.<name> where cat1 and cat2 can be repeated as often as desired.

ext is the file extension to use, e.g. .png or similar.

As an example, the resource name peng3d:some.category.foo with the extension .png results in the path <basepath>/assets/peng3d/some/category/foo.png.

This resource naming scheme is used by most other methods of this class.

Note that it is currently not possible to define multiple base paths to search through.