demosys.project.base.BaseProject

demosys.project.base.BaseProject

The base project class we extend when creating a project configuration

The minimal implementation:

from demosys.project.base import BaseProject
from demosys.resources.meta import ProgramDescription, TextureDescription

class Project(BaseProject):
    # The effect packages to import using full python path
    effect_packages = [
        'myproject.efect_package1',
        'myproject.efect_package2',
        'myproject.efect_package2',
    ]
    # Resource description for global project resources (not loaded by effect packages)
    resources = [
        ProgramDescription(label='cube_textured', path="cube_textured.glsl'),
        TextureDescription(label='wood', path="wood.png', mipmap=True),
    ]

    def create_resources(self):
        # Override the method adding additional resources

        # Create some shared fbo
        size = (256, 256)
        self.shared_framebuffer = self.ctx.framebuffer(
            color_attachments=self.ctx.texture(size, 4),
            depth_attachement=self.ctx.depth_texture(size)
        )

        return self.resources

    def create_effect_instances(self):
        # Create and register instances of an effect class we loaded from the effect packages
        self.create_effect('cube1', 'CubeEffect')

        # Using full path to class
        self.create_effect('cube2', 'myproject.efect_package1.CubeEffect')

        # Passing variables to initializer
        self.create_effect('cube3', 'CubeEffect', texture=self.get_texture('wood'))

        # Assign resources manually
        cube = self.create_effect('cube1', 'CubeEffect')
        cube.program = self.get_program('cube_textured')
        cube.texture = self.get_texture('wood')
        cube.fbo = self.shared_framebuffer

These effects instances can then be obtained by the configured timeline class deciding when they should be rendered.

Create Methods

BaseProject.create_effect(label: str, name: str, *args, **kwargs) → demosys.effects.effect.Effect

Create an effect instance adding it to the internal effects dictionary using the label as key.

Parameters:
  • label (str) – The unique label for the effect instance
  • name (str) – Name or full python path to the effect class we want to instantiate
  • args – Positional arguments to the effect initializer
  • kwargs – Keyword arguments to the effect initializer
Returns:

The newly created Effect instance

BaseProject.create_effect_classes()

Registers effect packages defined in effect_packages.

BaseProject.create_resources() → List[demosys.resources.base.ResourceDescription]

Create resources for the project. Simply returns the resources list and can be implemented to modify what a resource list is programmatically.

Returns:List of resource descriptions to load
BaseProject.create_external_resources() → List[demosys.resources.base.ResourceDescription]

Fetches all resource descriptions defined in effect packages.

Returns:List of resource descriptions to load
BaseProject.create_effect_instances()

Create instances of effects. Must be implemented or NotImplementedError is raised.

Resource Methods

BaseProject.get_effect(label: str) → demosys.effects.effect.Effect

Get an effect instance by label

Parameters:label (str) – The label for the effect instance
Returns:Effect class instance
BaseProject.get_effect_class(class_name, package_name=None) → Type[demosys.effects.effect.Effect]

Get an effect class from the effect registry.

Parameters:class_name (str) – The exact class name of the effect
Keyword Arguments:
 package_name (str) – The python path to the effect package the effect name is located. This is optional and can be used to avoid issue with class name collisions.
Returns:Effect class
BaseProject.get_scene(label: str) → demosys.scene.scene.Scene

Gets a scene by label

Parameters:label (str) – The label for the scene to fetch
Returns:Scene instance
BaseProject.get_program(label: str) → moderngl.program.Program
BaseProject.get_texture(label: str) → Union[moderngl.texture.Texture, moderngl.texture_array.TextureArray, moderngl.texture_3d.Texture3D, moderngl.texture_cube.TextureCube]

Get a texture by label

Parameters:label (str) – The label for the texture to fetch
Returns:Texture instance
BaseProject.get_data()

Get a data resource by label

Parameters:label (str) – The labvel for the data resource to fetch
Returns:The requeted data object

Other Methods

BaseProject.load()

Loads this project instance

BaseProject.post_load()

Called after resources are loaded before effects starts rendering. It simply iterates each effect instance calling their post_load methods.

BaseProject.reload_programs()

Reload all shader programs with the reloadable flag set

BaseProject.get_runnable_effects() → List[demosys.effects.effect.Effect]

Returns all runnable effects in the project.

Returns:List of all runnable effects

Attributes

BaseProject.effect_packages = []

The effect packages to load

BaseProject.resources = []

Global project resource descriptions

BaseProject.ctx

The MondernGL context