diff --git a/moderngl_window/loaders/scene/gltf2.py b/moderngl_window/loaders/scene/gltf2.py index da612cc..f237938 100644 --- a/moderngl_window/loaders/scene/gltf2.py +++ b/moderngl_window/loaders/scene/gltf2.py @@ -151,9 +151,7 @@ def load_glb(self): version = struct.unpack(" str: @@ -40,7 +50,7 @@ def name(self) -> str: return self._name @name.setter - def name(self, value: str): + def name(self, value: str) -> None: self._name = value @property @@ -49,7 +59,7 @@ def mesh(self) -> "Mesh": return self._mesh @mesh.setter - def mesh(self, value: "Mesh"): + def mesh(self, value: "Mesh") -> None: self._mesh = value @property @@ -58,25 +68,25 @@ def camera(self) -> "Camera": return self._camera @camera.setter - def camera(self, value): + def camera(self, value: "Camera") -> None: self._camera = value @property - def matrix(self) -> numpy.ndarray: + def matrix(self) -> glm.mat4: """numpy.ndarray: Note matrix (local)""" return self._matrix @matrix.setter - def matrix(self, value): + def matrix(self, value: glm.mat4) -> None: self._matrix = value @property - def matrix_global(self) -> numpy.ndarray: + def matrix_global(self) -> glm.mat4: """numpy.ndarray: The global node matrix containing transformations from parent nodes""" return self._matrix_global @matrix_global.setter - def matrix_global(self, value): + def matrix_global(self, value: glm.mat4) -> None: self._matrix_global = value @property @@ -84,7 +94,7 @@ def children(self) -> List["Node"]: """list: List of children""" return self._children - def add_child(self, node): + def add_child(self, node: "Node") -> None: """Add a child to this node Args: @@ -92,13 +102,13 @@ def add_child(self, node): """ self._children.append(node) - def draw(self, projection_matrix=None, camera_matrix=None, time=0): + def draw(self, projection_matrix: glm.mat4, camera_matrix: glm.mat4, time=0): """Draw node and children. Keyword Args: - projection_matrix (bytes): projection matrix - camera_matrix (bytes): camera_matrix - time (float): The current time + projection_matrix: projection matrix + camera_matrix: camera_matrix + time: The current time """ if self._mesh: self._mesh.draw( @@ -115,7 +125,9 @@ def draw(self, projection_matrix=None, camera_matrix=None, time=0): time=time, ) - def draw_bbox(self, projection_matrix, camera_matrix, program, vao): + def draw_bbox( + self, projection_matrix: glm.mat4, camera_matrix: glm.mat4, program: mod, vao + ): """Draw bounding box around the node and children. Keyword Args: @@ -146,7 +158,7 @@ def draw_wireframe(self, projection_matrix, camera_matrix, program): for child in self.children: child.draw_wireframe(projection_matrix, self._matrix_global, program) - def calc_global_bbox(self, view_matrix, bbox_min, bbox_max): + def calc_global_bbox(self, view_matrix: glm.mat4, bbox_min, bbox_max) -> tuple: """Recursive calculation of scene bbox. Keyword Args: @@ -167,7 +179,7 @@ def calc_global_bbox(self, view_matrix, bbox_min, bbox_max): return bbox_min, bbox_max - def calc_model_mat(self, model_matrix): + def calc_model_mat(self, model_matrix: glm.mat4) -> None: """Calculate the model matrix related to all parents. Args: @@ -184,5 +196,5 @@ def calc_model_mat(self, model_matrix): for child in self._children: child.calc_model_mat(model_matrix) - def __repr__(self): + def __repr__(self) -> str: return "".format(self.name) diff --git a/moderngl_window/scene/programs.py b/moderngl_window/scene/programs.py index 129d8fc..75aeed7 100644 --- a/moderngl_window/scene/programs.py +++ b/moderngl_window/scene/programs.py @@ -1,12 +1,16 @@ +from __future__ import annotations + +from typing import Self import os -import numpy +import glm import moderngl import moderngl_window from moderngl_window.conf import settings from moderngl_window.resources import programs from moderngl_window.meta import ProgramDescription +from .mesh import Mesh settings.PROGRAM_DIRS.append(os.path.join(os.path.dirname(__file__), "programs")) @@ -17,7 +21,7 @@ class MeshProgram: Describes how a mesh is rendered using a specific shader program """ - def __init__(self, program: moderngl.Program = None, **kwargs): + def __init__(self, program: moderngl.Program | None = None, **kwargs) -> None: """Initialize. Args: @@ -26,18 +30,18 @@ def __init__(self, program: moderngl.Program = None, **kwargs): self.program = program @property - def ctx(self): + def ctx(self) -> moderngl.Context: """moderngl.Context: The current context""" return moderngl_window.ctx() def draw( self, - mesh, - projection_matrix: numpy.ndarray = None, - model_matrix: numpy.ndarray = None, - camera_matrix: numpy.ndarray = None, + mesh: Mesh, + projection_matrix: glm.mat4, + model_matrix: glm.mat4, + camera_matrix: glm.mat4, time=0.0, - ): + ) -> None: """Draw code for the mesh Args: @@ -50,9 +54,10 @@ def draw( """ self.program["m_proj"].write(projection_matrix) self.program["m_mv"].write(model_matrix) + self.program["m_cam"].write(camera_matrix) mesh.vao.render(self.program) - def apply(self, mesh): + def apply(self, mesh: Mesh) -> Self | None: """ Determine if this ``MeshProgram`` should be applied to the mesh. Can return self or some ``MeshProgram`` instance to support dynamic ``MeshProgram`` creation @@ -68,7 +73,7 @@ def apply(self, mesh): class VertexColorProgram(MeshProgram): """Vertex color program""" - def __init__(self, program=None, **kwargs): + def __init__(self, program=None, **kwargs) -> None: super().__init__(program=None) self.program = programs.load( ProgramDescription(path="scene_default/vertex_color.glsl") @@ -77,17 +82,17 @@ def __init__(self, program=None, **kwargs): def draw( self, mesh, - projection_matrix=None, - model_matrix=None, - camera_matrix=None, - time=0, - ): + projection_matrix: glm.mat4, + model_matrix: glm.mat4, + camera_matrix: glm.mat4, + time=0.0, + ) -> None: self.program["m_proj"].write(projection_matrix) self.program["m_model"].write(model_matrix) self.program["m_cam"].write(camera_matrix) mesh.vao.render(self.program) - def apply(self, mesh): + def apply(self, mesh: Mesh) -> Self | None: if not mesh.material: return None @@ -103,7 +108,7 @@ def apply(self, mesh): class ColorLightProgram(MeshProgram): """Simple color program with light""" - def __init__(self, program=None, **kwargs): + def __init__(self, program=None, **kwargs) -> None: super().__init__(program=None) self.program = programs.load( ProgramDescription(path="scene_default/color_light.glsl") @@ -112,12 +117,11 @@ def __init__(self, program=None, **kwargs): def draw( self, mesh, - projection_matrix=None, - model_matrix=None, - camera_matrix=None, - time=0, - ): - + projection_matrix: glm.mat4, + model_matrix: glm.mat4, + camera_matrix: glm.mat4, + time=0.0, + ) -> None: if mesh.material: # if mesh.material.double_sided: # self.ctx.disable(moderngl.CULL_FACE) @@ -134,7 +138,7 @@ def draw( self.program["m_cam"].write(camera_matrix) mesh.vao.render(self.program) - def apply(self, mesh): + def apply(self, mesh: Mesh) -> Self | None: if not mesh.material: return None @@ -147,7 +151,7 @@ def apply(self, mesh): class TextureProgram(MeshProgram): """Plan textured""" - def __init__(self, program=None, **kwargs): + def __init__(self, program=None, **kwargs) -> None: super().__init__(program=None) self.program = programs.load( ProgramDescription(path="scene_default/texture.glsl") @@ -156,18 +160,18 @@ def __init__(self, program=None, **kwargs): def draw( self, mesh, - projection_matrix=None, - model_matrix=None, - camera_matrix=None, - time=0, - ): + projection_matrix: glm.mat4, + model_matrix: glm.mat4, + camera_matrix: glm.mat4, + time=0.0, + ) -> None: mesh.material.mat_texture.texture.use() self.program["m_proj"].write(projection_matrix) self.program["m_model"].write(model_matrix) self.program["m_cam"].write(camera_matrix) mesh.vao.render(self.program) - def apply(self, mesh): + def apply(self, mesh) -> Self | None: if not mesh.material: return None @@ -187,9 +191,9 @@ def apply(self, mesh): class TextureVertexColorProgram(MeshProgram): - """textred object with vertex color""" + """textured object with vertex color""" - def __init__(self, program=None, **kwargs): + def __init__(self, program: moderngl.Program | None = None, **kwargs) -> None: super().__init__(program=None) self.program = programs.load( ProgramDescription(path="scene_default/vertex_color_texture.glsl") @@ -197,19 +201,19 @@ def __init__(self, program=None, **kwargs): def draw( self, - mesh, - projection_matrix=None, - model_matrix=None, - camera_matrix=None, + mesh: Mesh, + projection_matrix: glm.mat4, + model_matrix: glm.mat4, + camera_matrix: glm.mat4, time=0, - ): + ) -> None: mesh.material.mat_texture.texture.use() self.program["m_proj"].write(projection_matrix) self.program["m_model"].write(model_matrix) self.program["m_cam"].write(camera_matrix) mesh.vao.render(self.program) - def apply(self, mesh): + def apply(self, mesh: Mesh) -> Self | None: if not mesh.material: return None @@ -233,7 +237,7 @@ class TextureLightProgram(MeshProgram): Simple texture program """ - def __init__(self, program=None, **kwargs): + def __init__(self, program: moderngl.Program | None = None, **kwargs) -> None: super().__init__(program=None) self.program = programs.load( ProgramDescription(path="scene_default/texture_light.glsl") @@ -241,12 +245,12 @@ def __init__(self, program=None, **kwargs): def draw( self, - mesh, - projection_matrix=None, - model_matrix=None, - camera_matrix=None, - time=0, - ): + mesh: Mesh, + projection_matrix: glm.mat4, + model_matrix: glm.mat4, + camera_matrix: glm.mat4, + time=0.0, + ) -> None: # if mesh.material.double_sided: # self.ctx.disable(moderngl.CULL_FACE) # else: @@ -259,7 +263,7 @@ def draw( self.program["m_cam"].write(camera_matrix) mesh.vao.render(self.program) - def apply(self, mesh): + def apply(self, mesh: Mesh) -> Self | None: if not mesh.material: return None @@ -284,7 +288,7 @@ class FallbackProgram(MeshProgram): Fallback program only rendering positions in white """ - def __init__(self, program=None, **kwargs): + def __init__(self, program: moderngl.Program | None = None, **kwargs) -> None: super().__init__(program=None) self.program = programs.load( ProgramDescription(path="scene_default/fallback.glsl") @@ -292,13 +296,12 @@ def __init__(self, program=None, **kwargs): def draw( self, - mesh, - projection_matrix=None, - model_matrix=None, - camera_matrix=None, - time=0, - ): - + mesh: Mesh, + projection_matrix: glm.mat4, + model_matrix: glm.mat4, + camera_matrix: glm.mat4, + time=0.0, + ) -> None: self.program["m_proj"].write(projection_matrix) self.program["m_model"].write(model_matrix) self.program["m_cam"].write(camera_matrix) @@ -310,5 +313,5 @@ def draw( mesh.vao.render(self.program) - def apply(self, mesh): + def apply(self, mesh: Mesh) -> Self | None: return self