Skip to content

Commit

Permalink
Misc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
einarf committed Nov 29, 2024
1 parent 149c0d9 commit a44c11b
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 82 deletions.
4 changes: 4 additions & 0 deletions examples/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions moderngl_window/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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


Expand Down
61 changes: 9 additions & 52 deletions moderngl_window/context/base/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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"""
Expand Down
6 changes: 4 additions & 2 deletions moderngl_window/context/headless/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 0 additions & 2 deletions moderngl_window/integrations/imgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 0 additions & 2 deletions moderngl_window/integrations/imgui_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
30 changes: 15 additions & 15 deletions moderngl_window/loaders/scene/gltf2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
4 changes: 0 additions & 4 deletions moderngl_window/scene/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion moderngl_window/text/bitmapped/text_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


class TextWriter2D(BaseText):
"""Simple monspaced bitmapped text renderer"""
"""Simple monospaced bitmapped text renderer"""

def __init__(self) -> None:
super().__init__()
Expand Down

0 comments on commit a44c11b

Please sign in to comment.