From a44c11baf609b353de6a869c383b431a54ed6a58 Mon Sep 17 00:00:00 2001 From: Einar Forselv Date: Fri, 29 Nov 2024 03:56:56 +0100 Subject: [PATCH] Misc fixes --- examples/base.py | 4 ++ moderngl_window/__init__.py | 7 +-- moderngl_window/context/base/window.py | 61 +++----------------- moderngl_window/context/headless/window.py | 6 +- moderngl_window/integrations/imgui.py | 2 - moderngl_window/integrations/imgui_bundle.py | 2 - moderngl_window/loaders/scene/gltf2.py | 30 +++++----- moderngl_window/scene/node.py | 4 -- moderngl_window/text/bitmapped/text_2d.py | 2 +- 9 files changed, 36 insertions(+), 82 deletions(-) diff --git a/examples/base.py b/examples/base.py index 47ef9eb..257734b 100644 --- a/examples/base.py +++ b/examples/base.py @@ -31,6 +31,10 @@ def on_mouse_position_event(self, x: int, y: int, dx, dy): def on_resize(self, width: int, height: int): self.camera.projection.update(aspect_ratio=self.wnd.aspect_ratio) + def on_mouse_scroll_event(self, x_offset: float, y_offset: float) -> None: + velocity = self.camera.velocity + y_offset + self.camera.velocity = max(velocity, 1.0) + class OrbitCameraWindow(mglw.WindowConfig): """Base class with built in 3D orbit camera support diff --git a/moderngl_window/__init__.py b/moderngl_window/__init__.py index ab5b74c..dd34240 100644 --- a/moderngl_window/__init__.py +++ b/moderngl_window/__init__.py @@ -88,7 +88,6 @@ def activate_context( ContextRefs.WINDOW = window ContextRefs.CONTEXT = ctx if ctx is None: - assert window is not None, "The window parameter can not be None if ctx is None" ContextRefs.CONTEXT = window.ctx @@ -123,9 +122,9 @@ def get_window_cls(window: str = "") -> type[BaseWindow]: logger.info("Attempting to load window class: %s", window) win = import_string(window) - assert issubclass( - win, BaseWindow - ), f"{win} is not derived from moderngl_window.context.base.BaseWindow" + # assert issubclass( + # win, BaseWindow + # ), f"{win} is not derived from moderngl_window.context.base.BaseWindow" return win diff --git a/moderngl_window/context/base/window.py b/moderngl_window/context/base/window.py index 166f50a..0abe336 100644 --- a/moderngl_window/context/base/window.py +++ b/moderngl_window/context/base/window.py @@ -1292,7 +1292,7 @@ def load_texture_2d( Returns: moderngl.Texture: Texture instance """ - texture = resources.textures.load( + return resources.textures.load( TextureDescription( path=path, flip=flip, @@ -1304,10 +1304,6 @@ def load_texture_2d( **kwargs, ) ) - assert isinstance( - texture, moderngl.Texture - ), f"There was an error when loading the texture, {type(texture)} is not a moderngl.Texture" - return texture def load_texture_array( self, @@ -1346,7 +1342,7 @@ def load_texture_array( if "kind" not in kwargs: kwargs["kind"] = "array" - texture = resources.textures.load( + return resources.textures.load( TextureDescription( path=path, layers=layers, @@ -1357,10 +1353,6 @@ def load_texture_array( **kwargs, ) ) - assert isinstance( - texture, moderngl.TextureArray - ), f"Error when loading the texture, {type(texture)} is not a moderngl.TextureArray" - return texture def load_texture_cube( self, @@ -1403,7 +1395,7 @@ def load_texture_cube( Returns: moderngl.TextureCube: Texture instance """ - texture = resources.textures.load( + return resources.textures.load( TextureDescription( pos_x=pos_x, pos_y=pos_y, @@ -1422,11 +1414,6 @@ def load_texture_cube( ) ) - assert isinstance( - texture, moderngl.TextureCube - ), f"Error when loading the texture. {type(texture)} is not a moderngl.TextureCube" - return texture - def load_program( self, path: Optional[str] = None, @@ -1460,7 +1447,7 @@ def load_program( Returns: moderngl.Program: The program instance """ - prog = resources.programs.load( + return resources.programs.load( ProgramDescription( path=path, vertex_shader=vertex_shader, @@ -1473,11 +1460,6 @@ def load_program( ) ) - assert isinstance( - prog, moderngl.Program - ), f"There was an error when loading the program, {type(prog)} is not a moderngl.Program" - return prog - def load_compute_shader( self, path: str, defines: Optional[dict[str, Any]] = None, **kwargs: Any ) -> moderngl.ComputeShader: @@ -1490,15 +1472,10 @@ def load_compute_shader( Returns: moderngl.ComputeShader: The compute shader """ - shader = resources.programs.load( + return resources.programs.load( ProgramDescription(compute_shader=path, defines=defines, **kwargs) ) - assert isinstance( - shader, moderngl.ComputeShader - ), f"Error loading compute shader. {type(shader)} is not a moderngl.ComputeShader" - return shader - def load_text(self, path: str, **kwargs: Any) -> str: """Load a text file. @@ -1518,12 +1495,7 @@ def load_text(self, path: str, **kwargs: Any) -> str: if "kind" not in kwargs: kwargs["kind"] = "text" - text = resources.data.load(DataDescription(path=path, **kwargs)) - - assert isinstance( - text, str - ), f"There was an error when loading the text, {type(text)} is not a string" - return text + return resources.data.load(DataDescription(path=path, **kwargs)) def load_json(self, path: str, **kwargs: Any) -> dict[str, Any]: """Load a json file @@ -1544,12 +1516,7 @@ def load_json(self, path: str, **kwargs: Any) -> dict[str, Any]: if "kind" not in kwargs: kwargs["kind"] = "json" - json = resources.data.load(DataDescription(path=path, **kwargs)) - - assert isinstance( - json, dict - ), f"There was an error when loading the Texture, {type(json)} is not a dictionnary" - return json + return resources.data.load(DataDescription(path=path, **kwargs)) def load_binary(self, path: str, **kwargs: Any) -> bytes: """Load a file in binary mode. @@ -1570,12 +1537,7 @@ def load_binary(self, path: str, **kwargs: Any) -> bytes: if "kind" not in kwargs: kwargs["kind"] = "binary" - binary = resources.data.load(DataDescription(path=path, kind="binary")) - - assert isinstance( - binary, bytes - ), f"There was an error when loading the binary, {type(binary)} is not a binary file" - return binary + return resources.data.load(DataDescription(path=path, kind="binary")) def load_scene( self, @@ -1600,7 +1562,7 @@ def load_scene( Returns: Scene: The scene instance """ - scene = resources.scenes.load( + return resources.scenes.load( SceneDescription( path=path, cache=cache, @@ -1610,11 +1572,6 @@ def load_scene( ) ) - assert isinstance( - scene, Scene - ), f"There was an error when loading the scene, {type(scene)} is not a Scene" - return scene - def dummy_func(*args: Any, **kwargs: Any) -> None: """Dummy function used as the default for callbacks""" diff --git a/moderngl_window/context/headless/window.py b/moderngl_window/context/headless/window.py index 0c6fd02..2df7005 100644 --- a/moderngl_window/context/headless/window.py +++ b/moderngl_window/context/headless/window.py @@ -30,7 +30,8 @@ def __init__(self, **kwargs: Any): @property def fbo(self) -> moderngl.Framebuffer: """moderngl.Framebuffer: The default framebuffer""" - assert self._fbo is not None, "No default framebuffer defined" + if self._fbo is None: + raise RuntimeError("No framebuffer created yet") return self._fbo def init_mgl_context(self) -> None: @@ -81,7 +82,8 @@ def size(self, value: tuple[int, int]) -> None: def use(self) -> None: """Bind the window's framebuffer""" - assert self._fbo is not None, "No framebuffer defined, did you forget to call create_fbo()?" + if self._fbo is None: + raise RuntimeError("No framebuffer created yet") self._fbo.use() def clear( diff --git a/moderngl_window/integrations/imgui.py b/moderngl_window/integrations/imgui.py index ae644b9..0b9d896 100644 --- a/moderngl_window/integrations/imgui.py +++ b/moderngl_window/integrations/imgui.py @@ -115,8 +115,6 @@ def __init__(self, *args, **kwargs): if not self.ctx: raise ValueError("Missing moderngl context") - assert isinstance(self.ctx, moderngl.Context) - super().__init__() if hasattr(self, "wnd") and self.wnd: diff --git a/moderngl_window/integrations/imgui_bundle.py b/moderngl_window/integrations/imgui_bundle.py index 4348f8d..fa62d63 100644 --- a/moderngl_window/integrations/imgui_bundle.py +++ b/moderngl_window/integrations/imgui_bundle.py @@ -147,8 +147,6 @@ def __init__(self, *args, **kwargs): if not self.ctx: raise RuntimeError("Missing moderngl context") - assert isinstance(self.ctx, moderngl.Context) - super().__init__() if hasattr(self, "wnd") and self.wnd: diff --git a/moderngl_window/loaders/scene/gltf2.py b/moderngl_window/loaders/scene/gltf2.py index 5121bae..6983a57 100644 --- a/moderngl_window/loaders/scene/gltf2.py +++ b/moderngl_window/loaders/scene/gltf2.py @@ -422,10 +422,10 @@ def images_exist(self) -> None: class GLTFAsset: """Asset Information""" - def __init__(self, data: dict[str, str]): - self.version = data.get("version", "") - self.generator = data.get("generator", "") - self.copyright = data.get("copyright", "") + def __init__(self, data: dict[str, Any]): + self.version = data.get("version") + self.generator = data.get("generator") + self.copyright = data.get("copyright") class GLTFMesh: @@ -434,10 +434,10 @@ class Primitives: accessor: GLTFAccessor | None def __init__(self, data: dict[str, Any]): - self.attributes: dict[str, Any] = data.get("attributes", {}) - self.indices = data.get("indices", 0) + self.attributes: dict[str, Any] = data.get("attributes") + self.indices = data.get("indices") self.mode = data.get("mode") - self.material = data.get("material", 0) + self.material = data.get("material") self.accessor = None def __init__(self, data: dict[str, Any], meta: SceneDescription): @@ -776,7 +776,7 @@ def is_resource_node(self) -> bool: class GLTFMaterial: def __init__(self, data: dict[str, Any]): - self.name = data["name"] + self.name = data.get("name") # Defaults to true if not defined self.doubleSided = data.get("doubleSided") or True @@ -830,16 +830,16 @@ def load(self, path: Path) -> moderngl.Texture: class GLTFTexture: def __init__(self, data: dict[str, int]): - self.sampler = data["sampler"] - self.source = data["source"] + self.sampler: Optional[int] = data.get("sampler") + self.source: Optional[int] = data.get("source") class GLTFSampler: - def __init__(self, data: dict[str, int]): - self.magFilter = data["magFilter"] - self.minFilter = data["minFilter"] - self.wrapS = data["wrapS"] - self.wrapT = data["wrapT"] + def __init__(self, data): + self.magFilter = data.get("magFilter") + self.minFilter = data.get("minFilter") + self.wrapS = data.get("wrapS") + self.wrapT = data.get("wrapT") class GLTFCamera: diff --git a/moderngl_window/scene/node.py b/moderngl_window/scene/node.py index 6073e58..2a5a34b 100644 --- a/moderngl_window/scene/node.py +++ b/moderngl_window/scene/node.py @@ -200,10 +200,6 @@ def calc_global_bbox( for child in self._children: bbox_min, bbox_max = child.calc_global_bbox(view_matrix, bbox_min, bbox_max) - assert (bbox_max is not None) and ( - bbox_min is not None - ), "The bounding are not defined, please make sure your code is correct" - return bbox_min, bbox_max def calc_model_mat(self, model_matrix: glm.mat4) -> None: diff --git a/moderngl_window/text/bitmapped/text_2d.py b/moderngl_window/text/bitmapped/text_2d.py index 0a6c9ef..22108b4 100644 --- a/moderngl_window/text/bitmapped/text_2d.py +++ b/moderngl_window/text/bitmapped/text_2d.py @@ -15,7 +15,7 @@ class TextWriter2D(BaseText): - """Simple monspaced bitmapped text renderer""" + """Simple monospaced bitmapped text renderer""" def __init__(self) -> None: super().__init__()