Skip to content

Commit 7eb8856

Browse files
henrikmidtibychopan050pre-commit-ci[bot]
authored
Add type annotations for most of camera and mobject.graphing (#4125)
Co-authored-by: Francisco Manríquez Novoa <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent aa0cd4f commit 7eb8856

File tree

10 files changed

+447
-321
lines changed

10 files changed

+447
-321
lines changed

manim/camera/camera.py

Lines changed: 118 additions & 90 deletions
Large diffs are not rendered by default.

manim/camera/moving_camera.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99

1010
__all__ = ["MovingCamera"]
1111

12+
from collections.abc import Iterable
13+
from typing import Any
14+
1215
import numpy as np
1316

1417
from .. import config
1518
from ..camera.camera import Camera
1619
from ..constants import DOWN, LEFT, RIGHT, UP
1720
from ..mobject.frame import ScreenRectangle
1821
from ..mobject.mobject import Mobject
19-
from ..utils.color import WHITE
22+
from ..utils.color import WHITE, ManimColor
2023

2124

2225
class MovingCamera(Camera):
@@ -32,10 +35,10 @@ class MovingCamera(Camera):
3235
def __init__(
3336
self,
3437
frame=None,
35-
fixed_dimension=0, # width
36-
default_frame_stroke_color=WHITE,
37-
default_frame_stroke_width=0,
38-
**kwargs,
38+
fixed_dimension: int = 0, # width
39+
default_frame_stroke_color: ManimColor = WHITE,
40+
default_frame_stroke_width: int = 0,
41+
**kwargs: Any,
3942
) -> None:
4043
"""Frame is a Mobject, (should almost certainly be a rectangle)
4144
determining which region of space the camera displays
@@ -121,7 +124,7 @@ def frame_center(self, frame_center: np.ndarray | list | tuple | Mobject):
121124
"""
122125
self.frame.move_to(frame_center)
123126

124-
def capture_mobjects(self, mobjects, **kwargs):
127+
def capture_mobjects(self, mobjects: Iterable[Mobject], **kwargs: Any) -> None:
125128
# self.reset_frame_center()
126129
# self.realign_frame_shape()
127130
super().capture_mobjects(mobjects, **kwargs)

manim/camera/multi_camera.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55
__all__ = ["MultiCamera"]
66

77

8-
from manim.mobject.types.image_mobject import ImageMobject
8+
from collections.abc import Iterable
9+
from typing import Any
10+
11+
from typing_extensions import Self
12+
13+
from manim.mobject.mobject import Mobject
14+
from manim.mobject.types.image_mobject import ImageMobjectFromCamera
915

1016
from ..camera.moving_camera import MovingCamera
1117
from ..utils.iterables import list_difference_update
@@ -16,10 +22,10 @@ class MultiCamera(MovingCamera):
1622

1723
def __init__(
1824
self,
19-
image_mobjects_from_cameras: ImageMobject | None = None,
20-
allow_cameras_to_capture_their_own_display=False,
21-
**kwargs,
22-
):
25+
image_mobjects_from_cameras: Iterable[ImageMobjectFromCamera] | None = None,
26+
allow_cameras_to_capture_their_own_display: bool = False,
27+
**kwargs: Any,
28+
) -> None:
2329
"""Initialises the MultiCamera
2430
2531
Parameters
@@ -29,7 +35,7 @@ def __init__(
2935
kwargs
3036
Any valid keyword arguments of MovingCamera.
3137
"""
32-
self.image_mobjects_from_cameras = []
38+
self.image_mobjects_from_cameras: list[ImageMobjectFromCamera] = []
3339
if image_mobjects_from_cameras is not None:
3440
for imfc in image_mobjects_from_cameras:
3541
self.add_image_mobject_from_camera(imfc)
@@ -38,7 +44,9 @@ def __init__(
3844
)
3945
super().__init__(**kwargs)
4046

41-
def add_image_mobject_from_camera(self, image_mobject_from_camera: ImageMobject):
47+
def add_image_mobject_from_camera(
48+
self, image_mobject_from_camera: ImageMobjectFromCamera
49+
) -> None:
4250
"""Adds an ImageMobject that's been obtained from the camera
4351
into the list ``self.image_mobject_from_cameras``
4452
@@ -53,20 +61,20 @@ def add_image_mobject_from_camera(self, image_mobject_from_camera: ImageMobject)
5361
assert isinstance(imfc.camera, MovingCamera)
5462
self.image_mobjects_from_cameras.append(imfc)
5563

56-
def update_sub_cameras(self):
64+
def update_sub_cameras(self) -> None:
5765
"""Reshape sub_camera pixel_arrays"""
5866
for imfc in self.image_mobjects_from_cameras:
5967
pixel_height, pixel_width = self.pixel_array.shape[:2]
60-
imfc.camera.frame_shape = (
61-
imfc.camera.frame.height,
62-
imfc.camera.frame.width,
63-
)
68+
# imfc.camera.frame_shape = (
69+
# imfc.camera.frame.height,
70+
# imfc.camera.frame.width,
71+
# )
6472
imfc.camera.reset_pixel_shape(
6573
int(pixel_height * imfc.height / self.frame_height),
6674
int(pixel_width * imfc.width / self.frame_width),
6775
)
6876

69-
def reset(self):
77+
def reset(self) -> Self:
7078
"""Resets the MultiCamera.
7179
7280
Returns
@@ -79,7 +87,7 @@ def reset(self):
7987
super().reset()
8088
return self
8189

82-
def capture_mobjects(self, mobjects, **kwargs):
90+
def capture_mobjects(self, mobjects: Iterable[Mobject], **kwargs: Any) -> None:
8391
self.update_sub_cameras()
8492
for imfc in self.image_mobjects_from_cameras:
8593
to_add = list(mobjects)
@@ -88,7 +96,7 @@ def capture_mobjects(self, mobjects, **kwargs):
8896
imfc.camera.capture_mobjects(to_add, **kwargs)
8997
super().capture_mobjects(mobjects, **kwargs)
9098

91-
def get_mobjects_indicating_movement(self):
99+
def get_mobjects_indicating_movement(self) -> list[Mobject]:
92100
"""Returns all mobjects whose movement implies that the camera
93101
should think of all other mobjects on the screen as moving
94102

0 commit comments

Comments
 (0)