A community-maintained Python framework for creating mathematical animations.
Add type hints to `scene_file_writer.py`, `section.py`, and `zoomed_scene.py` (#4133)
F. Muenkelaa0cd4fe8243
diff --git a/manim/camera/moving_camera.py b/manim/camera/moving_camera.pyindex 729887ec9..f17147765 100644--- a/manim/camera/moving_camera.py+++ b/manim/camera/moving_camera.py@@ -36,7 +36,7 @@ class MovingCamera(Camera):default_frame_stroke_color=WHITE,default_frame_stroke_width=0,**kwargs,- ):+ ) -> None:"""Frame is a Mobject, (should almost certainly be a rectangle)determining which region of space the camera displays"""diff --git a/manim/scene/moving_camera_scene.py b/manim/scene/moving_camera_scene.pyindex 686686f07..7487f05b0 100644--- a/manim/scene/moving_camera_scene.py+++ b/manim/scene/moving_camera_scene.py@@ -89,11 +89,12 @@ from __future__ import annotations__all__ = ["MovingCameraScene"]-from typing_extensions import Any+from typing import Anyfrom manim.animation.animation import Animationfrom manim.mobject.mobject import Mobject+from ..camera.camera import Camerafrom ..camera.moving_camera import MovingCamerafrom ..scene.scene import Scenefrom ..utils.family import extract_mobject_family_members@@ -115,7 +116,7 @@ class MovingCameraScene(Scene):"""def __init__(- self, camera_class: type[MovingCamera] = MovingCamera, **kwargs: Any+ self, camera_class: type[Camera] = MovingCamera, **kwargs: Any) -> None:super().__init__(camera_class=camera_class, **kwargs)@@ -131,7 +132,7 @@ class MovingCameraScene(Scene):"""moving_mobjects = super().get_moving_mobjects(*animations)all_moving_mobjects = extract_mobject_family_members(moving_mobjects)- movement_indicators = self.renderer.camera.get_mobjects_indicating_movement()+ movement_indicators = self.renderer.camera.get_mobjects_indicating_movement() # type: ignore[union-attr]for movement_indicator in movement_indicators:if movement_indicator in all_moving_mobjects:# When one of these is moving, the camera shoulddiff --git a/manim/scene/scene_file_writer.py b/manim/scene/scene_file_writer.pyindex d256afb73..257e4301c 100644--- a/manim/scene/scene_file_writer.py+++ b/manim/scene/scene_file_writer.py@@ -9,7 +9,7 @@ import shutilfrom fractions import Fractionfrom pathlib import Pathfrom queue import Queue-from tempfile import NamedTemporaryFile+from tempfile import NamedTemporaryFile, _TemporaryFileWrapperfrom threading import Threadfrom typing import TYPE_CHECKING, Any@@ -20,7 +20,6 @@ from PIL import Imagefrom pydub import AudioSegmentfrom manim import __version__-from manim.typing import PixelArray, StrPathfrom .. import config, loggerfrom .._config.logger_utils import set_file_logger@@ -38,11 +37,15 @@ from ..utils.sounds import get_full_sound_file_pathfrom .section import DefaultSectionType, Sectionif TYPE_CHECKING:+ from av.container.output import OutputContainer+ from av.stream import Stream+from manim.renderer.cairo_renderer import CairoRendererfrom manim.renderer.opengl_renderer import OpenGLRenderer+ from manim.typing import PixelArray, StrPath-def to_av_frame_rate(fps):+def to_av_frame_rate(fps: float) -> Fraction:epsilon1 = 1e-4epsilon2 = 0.02@@ -59,7 +62,9 @@ def to_av_frame_rate(fps):return Fraction(num, denom)-def convert_audio(input_path: Path, output_path: Path, codec_name: str):+def convert_audio(+ input_path: Path, output_path: Path | _TemporaryFileWrapper[bytes], codec_name: str+) -> None:with (av.open(input_path) as input_audio,av.open(output_path, "w") as output_audio,@@ -75,8 +80,7 @@ def convert_audio(input_path: Path, output_path: Path, codec_name: str):class SceneFileWriter:- """- SceneFileWriter is the object that actually writes the animations+ """SceneFileWriter is the object that actually writes the animationsplayed, into video files, using FFMPEG.This is mostly for Manim's internal use. You will rarely, if ever,have to use the methods for this class, unless tinkering with the very@@ -108,14 +112,14 @@ class SceneFileWriter:def __init__(self,renderer: CairoRenderer | OpenGLRenderer,- scene_name: StrPath,+ scene_name: str,**kwargs: Any,) -> None:self.renderer = rendererself.init_output_directories(scene_name)self.init_audio()self.frame_count = 0- self.partial_movie_files: list[str] = []+ self.partial_movie_files: list[str | None] = []self.subcaptions: list[srt.Subtitle] = []self.sections: list[Section] = []# first section gets automatically created for convenience@@ -124,7 +128,7 @@ class SceneFileWriter:name="autocreated", type_=DefaultSectionType.NORMAL, skip_animations=False)- def init_output_directories(self, scene_name: StrPath) -> None:+ def init_output_directories(self, scene_name: str) -> None:"""Initialise output directories.Notes@@ -231,9 +235,12 @@ class SceneFileWriter:),)- def add_partial_movie_file(self, hash_animation: str):- """Adds a new partial movie file path to `scene.partial_movie_files` and current section from a hash.- This method will compute the path from the hash. In addition to that it adds the new animation to the current section.+ def add_partial_movie_file(self, hash_animation: str | None) -> None:+ """Adds a new partial movie file path to ``scene.partial_movie_files``+ and current section from a hash.++ This method will compute the path from the hash. In addition to that it+ adds the new animation to the current section.Parameters----------@@ -256,7 +263,7 @@ class SceneFileWriter:self.partial_movie_files.append(new_partial_movie_file)self.sections[-1].partial_movie_files.append(new_partial_movie_file)- def get_resolution_directory(self):+ def get_resolution_directory(self) -> str:"""Get the name of the resolution directory directly containingthe video file.@@ -272,9 +279,11 @@ class SceneFileWriter:|--Tex|--texts|--videos- |--<name_of_file_containing_scene>- |--<height_in_pixels_of_video>p<frame_rate>- |--<scene_name>.mp4+ |--<name_of_file_containing_scene>+ |--<height_in_pixels_of_video>p<frame_rate>+ |--partial_movie_files+ |--<scene_name>.mp4+ |--<scene_name>.srtReturns-------@@ -286,11 +295,11 @@ class SceneFileWriter:return f"{pixel_height}p{frame_rate}"# Sound- def init_audio(self):+ def init_audio(self) -> None:"""Preps the writer for adding audio to the movie."""self.includes_sound = False- def create_audio_segment(self):+ def create_audio_segment(self) -> None:"""Creates an empty, silent, Audio Segment."""self.audio_segment = AudioSegment.silent()@@ -299,10 +308,9 @@ class SceneFileWriter:new_segment: AudioSegment,time: float | None = None,gain_to_background: float | None = None,- ):- """- This method adds an audio segment from an- AudioSegment type object and suitable parameters.+ ) -> None:+ """This method adds an audio segment from an AudioSegment type object+ and suitable parameters.Parameters----------@@ -310,8 +318,7 @@ class SceneFileWriter:The audio segment to addtime- the timestamp at which the- sound should be added.+ the timestamp at which the sound should be added.gain_to_backgroundThe gain of the segment from the background.@@ -341,13 +348,12 @@ class SceneFileWriter:def add_sound(self,- sound_file: str,+ sound_file: StrPath,time: float | None = None,gain: float | None = None,- **kwargs,- ):- """- This method adds an audio segment from a sound file.+ **kwargs: Any,+ ) -> None:+ """This method adds an audio segment from a sound file.Parameters----------@@ -387,8 +393,7 @@ class SceneFileWriter:def begin_animation(self, allow_write: bool = False, file_path: StrPath | None = None) -> None:- """- Used internally by manim to stream the animation to FFMPEG for+ """Used internally by manim to stream the animation to FFMPEG fordisplaying or writing to a file.Parameters@@ -400,9 +405,7 @@ class SceneFileWriter:self.open_partial_movie_stream(file_path=file_path)def end_animation(self, allow_write: bool = False) -> None:- """- Internally used by Manim to stop streaming to- FFMPEG gracefully.+ """Internally used by Manim to stop streaming to FFMPEG gracefully.Parameters----------@@ -412,7 +415,7 @@ class SceneFileWriter:if write_to_movie() and allow_write:self.close_partial_movie_stream()- def listen_and_write(self):+ def listen_and_write(self) -> None:"""For internal use only: blocks until new frame is available on the queue."""while True:num_frames, frame_data = self.queue.get()@@ -422,9 +425,8 @@ class SceneFileWriter:self.encode_and_write_frame(frame_data, num_frames)def encode_and_write_frame(self, frame: PixelArray, num_frames: int) -> None:- """- For internal use only: takes a given frame in ``np.ndarray`` format and- write it to the stream+ """For internal use only: takes a given frame in ``np.ndarray`` format and+ writes it to the stream"""for _ in range(num_frames):# Notes: precomputing reusing packets does not work!@@ -438,11 +440,9 @@ class SceneFileWriter:self.video_container.mux(packet)def write_frame(- self, frame_or_renderer: np.ndarray | OpenGLRenderer, num_frames: int = 1- ):- """- Used internally by Manim to write a frame to- the FFMPEG input buffer.+ self, frame_or_renderer: PixelArray | OpenGLRenderer, num_frames: int = 1+ ) -> None:+ """Used internally by Manim to write a frame to the FFMPEG input buffer.Parameters----------@@ -452,21 +452,27 @@ class SceneFileWriter:The number of times to write frame."""if write_to_movie():- frame: np.ndarray = (- frame_or_renderer.get_frame()- if config.renderer == RendererType.OPENGL- else frame_or_renderer- )+ if isinstance(frame_or_renderer, np.ndarray):+ frame = frame_or_renderer+ else:+ frame = (+ frame_or_renderer.get_frame()+ if config.renderer == RendererType.OPENGL+ else frame_or_renderer+ )msg = (num_frames, frame)self.queue.put(msg)if is_png_format() and not config["dry_run"]:- image: Image = (- frame_or_renderer.get_image()- if config.renderer == RendererType.OPENGL- else Image.fromarray(frame_or_renderer)- )+ if isinstance(frame_or_renderer, np.ndarray):+ image = Image.fromarray(frame_or_renderer)+ else:+ image = (+ frame_or_renderer.get_image()+ if config.renderer == RendererType.OPENGL+ else Image.fromarray(frame_or_renderer)+ )target_dir = self.image_file_path.parent / self.image_file_path.stemextension = self.image_file_path.suffixself.output_image(@@ -476,16 +482,17 @@ class SceneFileWriter:config["zero_pad"],)- def output_image(self, image: Image.Image, target_dir, ext, zero_pad: bool):+ def output_image(+ self, image: Image.Image, target_dir: StrPath, ext: str, zero_pad: bool+ ) -> None:if zero_pad:image.save(f"{target_dir}{str(self.frame_count).zfill(zero_pad)}{ext}")else:image.save(f"{target_dir}{self.frame_count}{ext}")self.frame_count += 1- def save_final_image(self, image: np.ndarray):- """- The name is a misnomer. This method saves the image+ def save_final_image(self, image: Image.Image) -> None:+ """The name is a misnomer. This method saves the imagepassed to it as an in the default image directory.Parameters@@ -502,13 +509,9 @@ class SceneFileWriter:self.print_file_ready_message(self.image_file_path)def finish(self) -> None:- """- Finishes writing to the FFMPEG buffer or writing images- to output directory.- Combines the partial movie files into the- whole scene.- If save_last_frame is True, saves the last- frame in the default image directory.+ """Finishes writing to the FFMPEG buffer or writing images to output directory.+ Combines the partial movie files into the whole scene.+ If save_last_frame is True, saves the last frame in the default image directory."""if write_to_movie():self.combine_to_movie()@@ -524,7 +527,7 @@ class SceneFileWriter:if self.subcaptions:self.write_subcaption_file()- def open_partial_movie_stream(self, file_path=None) -> None:+ def open_partial_movie_stream(self, file_path: StrPath | None = None) -> None:"""Open a container holding a video stream.This is used internally by Manim initialize the container holding@@ -563,8 +566,8 @@ class SceneFileWriter:stream.width = config.pixel_widthstream.height = config.pixel_height- self.video_container = video_container- self.video_stream = stream+ self.video_container: OutputContainer = video_container+ self.video_stream: Stream = streamself.queue: Queue[tuple[int, PixelArray | None]] = Queue()self.writer_thread = Thread(target=self.listen_and_write, args=())@@ -590,7 +593,7 @@ class SceneFileWriter:{"path": f"'{self.partial_movie_file_path}'"},)- def is_already_cached(self, hash_invocation: str):+ def is_already_cached(self, hash_invocation: str) -> bool:"""Will check if a file named with `hash_invocation` exists.Parameters@@ -615,9 +618,9 @@ class SceneFileWriter:self,input_files: list[str],output_file: Path,- create_gif=False,- includes_sound=False,- ):+ create_gif: bool = False,+ includes_sound: bool = False,+ ) -> None:file_list = self.partial_movie_directory / "partial_movie_file_list.txt"logger.debug(f"Partial movie files to combine ({len(input_files)} files): %(p)s",@@ -651,8 +654,7 @@ class SceneFileWriter:if config.transparent and config.movie_file_extension == ".webm":output_stream.pix_fmt = "yuva420p"if create_gif:- """- The following solution was largely inspired from this comment+ """The following solution was largely inspired from this commenthttps://github.com/imageio/imageio/issues/995#issuecomment-1580533018,and the following codehttps://github.com/imageio/imageio/blob/65d79140018bb7c64c0692ea72cb4093e8d632a0/imageio/plugins/pyav.py#L927-L996.@@ -716,7 +718,7 @@ class SceneFileWriter:partial_movies_input.close()output_container.close()- def combine_to_movie(self):+ def combine_to_movie(self) -> None:"""Used internally by Manim to combine the separatepartial movie files that make up a Scene into a singlevideo file for that Scene.@@ -836,7 +838,7 @@ class SceneFileWriter:with (self.sections_output_dir / f"{self.output_name}.json").open("w") as file:json.dump(sections_index, file, indent=4)- def clean_cache(self):+ def clean_cache(self) -> None:"""Will clean the cache by removing the oldest partial_movie_files."""cached_partial_movies = [(self.partial_movie_directory / file_name)@@ -858,7 +860,7 @@ class SceneFileWriter:" You can change this behaviour by changing max_files_cached in config.",)- def flush_cache_directory(self):+ def flush_cache_directory(self) -> None:"""Delete all the cached partial movie files"""cached_partial_movies = [self.partial_movie_directory / file_name@@ -872,7 +874,7 @@ class SceneFileWriter:{"par_dir": self.partial_movie_directory},)- def write_subcaption_file(self):+ def write_subcaption_file(self) -> None:"""Writes the subcaption file."""if config.output_file is None:return@@ -880,7 +882,7 @@ class SceneFileWriter:subcaption_file.write_text(srt.compose(self.subcaptions), encoding="utf-8")logger.info(f"Subcaption file has been written as {subcaption_file}")- def print_file_ready_message(self, file_path):+ def print_file_ready_message(self, file_path: StrPath) -> None:"""Prints the "File Ready" message to STDOUT."""config["output_file"] = file_pathlogger.info("\nFile ready at %(file_path)s\n", {"file_path": f"'{file_path}'"})diff --git a/manim/scene/section.py b/manim/scene/section.pyindex 728104f32..99e62c382 100644--- a/manim/scene/section.py+++ b/manim/scene/section.py@@ -59,7 +59,9 @@ class Section::meth:`.OpenGLRenderer.update_skipping_status`"""- def __init__(self, type_: str, video: str | None, name: str, skip_animations: bool):+ def __init__(+ self, type_: str, video: str | None, name: str, skip_animations: bool+ ) -> None:self.type_ = type_# None when not to be saved -> still keeps section aliveself.video: str | None = videodiff --git a/manim/scene/zoomed_scene.py b/manim/scene/zoomed_scene.pyindex 361c4eaf5..57c89b1ad 100644--- a/manim/scene/zoomed_scene.py+++ b/manim/scene/zoomed_scene.py@@ -49,44 +49,49 @@ from __future__ import annotations__all__ = ["ZoomedScene"]+from typing import TYPE_CHECKING, Anyfrom ..animation.transform import ApplyMethod+from ..camera.camera import Camerafrom ..camera.moving_camera import MovingCamerafrom ..camera.multi_camera import MultiCamerafrom ..constants import *from ..mobject.types.image_mobject import ImageMobjectFromCamera+from ..renderer.opengl_renderer import OpenGLCamerafrom ..scene.moving_camera_scene import MovingCameraScene+if TYPE_CHECKING:+ from manim.typing import Point3DLike, Vector3D+# Note, any scenes from old videos using ZoomedScene will almost certainly# break, as it was restructured.class ZoomedScene(MovingCameraScene):- """- This is a Scene with special configurations made for when+ """This is a Scene with special configurations made for whena particular part of the scene must be zoomed in on and displayedseparately."""def __init__(self,- camera_class=MultiCamera,- zoomed_display_height=3,- zoomed_display_width=3,- zoomed_display_center=None,- zoomed_display_corner=UP + RIGHT,- zoomed_display_corner_buff=DEFAULT_MOBJECT_TO_EDGE_BUFFER,- zoomed_camera_config={+ camera_class: type[Camera] = MultiCamera,+ zoomed_display_height: float = 3,+ zoomed_display_width: float = 3,+ zoomed_display_center: Point3DLike | None = None,+ zoomed_display_corner: Vector3D = UP + RIGHT,+ zoomed_display_corner_buff: float = DEFAULT_MOBJECT_TO_EDGE_BUFFER,+ zoomed_camera_config: dict[str, Any] = {"default_frame_stroke_width": 2,"background_opacity": 1,},- zoomed_camera_image_mobject_config={},- zoomed_camera_frame_starting_position=ORIGIN,- zoom_factor=0.15,- image_frame_stroke_width=3,- zoom_activated=False,- **kwargs,- ):+ zoomed_camera_image_mobject_config: dict[str, Any] = {},+ zoomed_camera_frame_starting_position: Point3DLike = ORIGIN,+ zoom_factor: float = 0.15,+ image_frame_stroke_width: float = 3,+ zoom_activated: bool = False,+ **kwargs: Any,+ ) -> None:self.zoomed_display_height = zoomed_display_heightself.zoomed_display_width = zoomed_display_widthself.zoomed_display_center = zoomed_display_center@@ -102,9 +107,8 @@ class ZoomedScene(MovingCameraScene):self.zoom_activated = zoom_activatedsuper().__init__(camera_class=camera_class, **kwargs)- def setup(self):- """- This method is used internally by Manim to+ def setup(self) -> None:+ """This method is used internally by Manim tosetup the scene for proper use."""super().setup()@@ -132,10 +136,8 @@ class ZoomedScene(MovingCameraScene):self.zoomed_camera = zoomed_cameraself.zoomed_display = zoomed_display- def activate_zooming(self, animate: bool = False):- """- This method is used to activate the zooming for- the zoomed_camera.+ def activate_zooming(self, animate: bool = False) -> None:+ """This method is used to activate the zooming for the zoomed_camera.Parameters----------@@ -144,7 +146,7 @@ class ZoomedScene(MovingCameraScene):of the zoomed camera."""self.zoom_activated = True- self.renderer.camera.add_image_mobject_from_camera(self.zoomed_display)+ self.renderer.camera.add_image_mobject_from_camera(self.zoomed_display) # type: ignore[union-attr]if animate:self.play(self.get_zoom_in_animation())self.play(self.get_zoomed_display_pop_out_animation())@@ -153,9 +155,8 @@ class ZoomedScene(MovingCameraScene):self.zoomed_display,)- def get_zoom_in_animation(self, run_time: float = 2, **kwargs):- """- Returns the animation of camera zooming in.+ def get_zoom_in_animation(self, run_time: float = 2, **kwargs: Any) -> ApplyMethod:+ """Returns the animation of camera zooming in.Parameters----------@@ -170,8 +171,11 @@ class ZoomedScene(MovingCameraScene):The animation of the camera zooming in."""frame = self.zoomed_camera.frame- full_frame_height = self.camera.frame_height- full_frame_width = self.camera.frame_width+ if isinstance(self.camera, OpenGLCamera):+ full_frame_width, full_frame_height = self.camera.frame_shape+ else:+ full_frame_height = self.camera.frame_height+ full_frame_width = self.camera.frame_widthframe.save_state()frame.stretch_to_fit_width(full_frame_width)frame.stretch_to_fit_height(full_frame_height)@@ -179,11 +183,9 @@ class ZoomedScene(MovingCameraScene):frame.set_stroke(width=0)return ApplyMethod(frame.restore, run_time=run_time, **kwargs)- def get_zoomed_display_pop_out_animation(self, **kwargs):- """- This is the animation of the popping out of the- mini-display that shows the content of the zoomed- camera.+ def get_zoomed_display_pop_out_animation(self, **kwargs: Any) -> ApplyMethod:+ """This is the animation of the popping out of the mini-display that+ shows the content of the zoomed camera.Returns-------@@ -195,15 +197,18 @@ class ZoomedScene(MovingCameraScene):display.replace(self.zoomed_camera.frame, stretch=True)return ApplyMethod(display.restore)- def get_zoom_factor(self):- """- Returns the Zoom factor of the Zoomed camera.- Defined as the ratio between the height of the- zoomed camera and the height of the zoomed mini- display.+ def get_zoom_factor(self) -> float:+ """Returns the Zoom factor of the Zoomed camera.++ Defined as the ratio between the height of the zoomed camera and+ the height of the zoomed mini display.+Returns-------floatThe zoom factor."""- return self.zoomed_camera.frame.height / self.zoomed_display.height+ zoom_factor: float = (+ self.zoomed_camera.frame.height / self.zoomed_display.height+ )+ return zoom_factordiff --git a/manim/utils/testing/_test_class_makers.py b/manim/utils/testing/_test_class_makers.pyindex 084aab487..fafd8cc76 100644--- a/manim/utils/testing/_test_class_makers.py+++ b/manim/utils/testing/_test_class_makers.py@@ -46,16 +46,16 @@ class DummySceneFileWriter(SceneFileWriter):def __init__(self,renderer: CairoRenderer | OpenGLRenderer,- scene_name: StrPath,+ scene_name: str,**kwargs: Any,) -> None:super().__init__(renderer, scene_name, **kwargs)self.i = 0- def init_output_directories(self, scene_name: StrPath) -> None:+ def init_output_directories(self, scene_name: str) -> None:pass- def add_partial_movie_file(self, hash_animation: str) -> None:+ def add_partial_movie_file(self, hash_animation: str | None) -> None:passdef begin_animation(diff --git a/mypy.ini b/mypy.iniindex 5affa9881..19cd3671a 100644--- a/mypy.ini+++ b/mypy.ini@@ -180,18 +180,9 @@ ignore_errors = True[mypy-manim.renderer.vectorized_mobject_rendering]ignore_errors = True-[mypy-manim.scene.moving_camera_scene]-ignore_errors = True--[mypy-manim.scene.scene_file_writer]-ignore_errors = True-[mypy-manim.scene.three_d_scene]ignore_errors = True-[mypy-manim.scene.zoomed_scene]-ignore_errors = True-[mypy-manim.utils.hashing]ignore_errors = True