Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

fix usage of CONFIG and file_writer_config

leotrsb1a7475b7b19
diff --git a/manim/camera/camera.py b/manim/camera/camera.py
index 0216b5c71..bb0145bca 100644
--- a/manim/camera/camera.py
+++ b/manim/camera/camera.py
@@ -42,19 +42,19 @@ class Camera(object):
"""
CONFIG = {
"background_image": None,
- "pixel_height": PIXEL_HEIGHT,
- "pixel_width": PIXEL_WIDTH,
- "frame_rate": FRAME_RATE,
+ "pixel_height": config['pixel_height'],
+ "pixel_width": config['pixel_width'],
+ "frame_rate": config['frame_rate'],
# Note: frame height and width will be resized to match
# the pixel aspect ratio
- "frame_height": FRAME_HEIGHT,
- "frame_width": FRAME_WIDTH,
+ "frame_height": config['frame_height'],
+ "frame_width": config['frame_width'],
"frame_center": ORIGIN,
"background_color": BLACK,
"background_opacity": 1,
# Points in vectorized mobjects with norm greater
# than this value will be rescaled.
- "max_allowable_norm": FRAME_WIDTH,
+ "max_allowable_norm": config['frame_width'],
"image_mode": "RGBA",
"n_channels": 4,
"pixel_array_dtype": 'uint8',
diff --git a/manim/config.py b/manim/config.py
index 922131469..ef8c00782 100644
--- a/manim/config.py
+++ b/manim/config.py
@@ -16,7 +16,7 @@ from .utils.tex import TexTemplateFromFile, TexTemplate
from .logger import logger
from . import constants
-__all__ = ["config", "register_tex_template", "initialize_tex"]
+__all__ = ["file_writer_config", "config", "camera_config"]
def _parse_config(config_parser, args):
diff --git a/manim/scene/scene.py b/manim/scene/scene.py
index 7b6bd2574..766a4e13d 100644
--- a/manim/scene/scene.py
+++ b/manim/scene/scene.py
@@ -45,21 +45,16 @@ class Scene(Container):
"""
CONFIG = {
"camera_class": Camera,
- "CAMERA_CONFIG": {},
- "FILE_WRITER_CONFIG": {},
- "SKIP_ANIMATIONS": False,
+ "skip_animations": False,
"always_update_mobjects": False,
"random_seed": 0,
- "FROM_ANIMATION_NUMBER": None,
- "UPTO_ANIMATION_NUMBER": None,
- "LEAVE_PROGRESS_BARS": False,
}
def __init__(self, **kwargs):
Container.__init__(self, **kwargs)
- self.camera = self.camera_class(**self.CAMERA_CONFIG)
+ self.camera = self.camera_class(**camera_config)
self.file_writer = SceneFileWriter(
- self, **self.FILE_WRITER_CONFIG,
+ self, **file_writer_config,
)
self.mobjects = []
@@ -67,7 +62,7 @@ class Scene(Container):
self.foreground_mobjects = []
self.num_plays = 0
self.time = 0
- self.original_skipping_status = self.SKIP_ANIMATIONS
+ self.original_skipping_status = self.skip_animations
if self.random_seed is not None:
random.seed(self.random_seed)
np.random.seed(self.random_seed)
@@ -239,7 +234,7 @@ class Scene(Container):
**kwargs
"""
- if self.SKIP_ANIMATIONS and not ignore_skipping:
+ if self.skip_animations and not ignore_skipping:
return
if mobjects is None:
mobjects = list_update(
@@ -680,14 +675,14 @@ class Scene(Container):
ProgressDisplay
The CommandLine Progress Bar.
"""
- if self.SKIP_ANIMATIONS and not override_skip_animations:
+ if self.skip_animations and not override_skip_animations:
times = [run_time]
else:
step = 1 / self.camera.frame_rate
times = np.arange(0, run_time, step)
time_progression = ProgressDisplay(
times, total=n_iterations,
- leave=self.LEAVE_PROGRESS_BARS,
+ leave=file_writer_config['leave_progress_bars'],
ascii=False if platform.system() != 'Windows' else True
)
return time_progression
@@ -825,12 +820,12 @@ class Scene(Container):
raises an EndSceneEarlyException if they don't correspond.
"""
- if self.FROM_ANIMATION_NUMBER:
- if self.num_plays == self.FROM_ANIMATION_NUMBER:
- self.SKIP_ANIMATIONS = False
- if self.UPTO_ANIMATION_NUMBER:
- if self.num_plays >= self.UPTO_ANIMATION_NUMBER:
- self.SKIP_ANIMATIONS = True
+ if file_writer_config['from_animation_number']:
+ if self.num_plays == file_writer_config['from_animation_number']:
+ self.skip_animations = False
+ if file_writer_config['upto_animation_number']:
+ if self.num_plays >= file_writer_config['upto_animation_number']:
+ self.skip_animations = True
raise EndSceneEarlyException()
def handle_play_like_call(func):
@@ -855,7 +850,7 @@ class Scene(Container):
"""
def wrapper(self, *args, **kwargs):
self.update_skipping_status()
- allow_write = not self.SKIP_ANIMATIONS
+ allow_write = not self.skip_animations
self.file_writer.begin_animation(allow_write)
func(self, *args, **kwargs)
self.file_writer.end_animation(allow_write)
@@ -928,7 +923,7 @@ class Scene(Container):
self.mobjects_from_last_animation = [
anim.mobject for anim in animations
]
- if self.SKIP_ANIMATIONS:
+ if self.skip_animations:
# TODO, run this call in for each animation?
self.update_mobjects(self.get_run_time(animations))
else:
@@ -1071,7 +1066,7 @@ class Scene(Container):
if stop_condition is not None and stop_condition():
time_progression.close()
break
- elif self.SKIP_ANIMATIONS:
+ elif self.skip_animations:
# Do nothing
return self
else:
@@ -1140,7 +1135,7 @@ class Scene(Container):
"""
dt = 1 / self.camera.frame_rate
self.increment_time(len(frames) * dt)
- if self.SKIP_ANIMATIONS:
+ if self.skip_animations:
return
for frame in frames:
self.file_writer.write_frame(frame)
diff --git a/manim/scene/scene_file_writer.py b/manim/scene/scene_file_writer.py
index 1bfa7ed0e..064e21dfd 100644
--- a/manim/scene/scene_file_writer.py
+++ b/manim/scene/scene_file_writer.py
@@ -34,17 +34,6 @@ class SceneFileWriter(object):
"movie_file_extension" (str=".mp4")
The file-type extension of the outputted video.
"""
- CONFIG = {
- "WRITE_TO_MOVIE": False,
- "SAVE_PNGS": False,
- "PNG_MODE": "RGBA",
- "SAVE_LAST_FRAME": False,
- "MOVIE_FILE_EXTENSION": ".mp4",
- "GIF_FILE_EXTENSION": ".gif",
- "OUTPUT_FILE": None,
- "INPUT_FILE": "",
- "OUTPUT_DIRECTORY": None,
- }
def __init__(self, scene, **kwargs):
digest_config(self, kwargs)
@@ -61,12 +50,12 @@ class SceneFileWriter(object):
files will be written to and read from (within MEDIA_DIR).
If they don't already exist, they will be created.
"""
- module_directory = self.OUTPUT_DIRECTORY or self.get_default_module_directory()
- scene_name = self.OUTPUT_FILE or self.get_default_scene_name()
- if self.SAVE_LAST_FRAME or self.SAVE_PNGS:
- if config['MEDIA_DIR'] != "":
+ module_directory = self.get_default_module_directory()
+ scene_name = self.get_default_scene_name()
+ if file_writer_config['save_last_frame'] or file_writer_config['save_pngs']:
+ if file_writer_config['media_dir'] != "":
image_dir = guarantee_existence(os.path.join(
- config['MEDIA_DIR'],
+ file_writer_config['media_dir'],
"images",
module_directory,
))
@@ -85,13 +74,13 @@ class SceneFileWriter(object):
self.movie_file_path = os.path.join(
movie_dir,
add_extension_if_not_present(
- scene_name, self.MOVIE_FILE_EXTENSION
+ scene_name, file_writer_config['movie_file_extension']
)
)
self.gif_file_path = os.path.join(
movie_dir,
add_extension_if_not_present(
- scene_name, self.GIF_FILE_EXTENSION
+ scene_name, file_writer_config['gif_file_extension']
)
)
self.partial_movie_directory = guarantee_existence(os.path.join(
@@ -110,7 +99,7 @@ class SceneFileWriter(object):
str
The name of the directory.
"""
- filename = os.path.basename(self.INPUT_FILE)
+ filename = os.path.basename(file_writer_config['input_file'])
root, _ = os.path.splitext(filename)
return root
@@ -126,10 +115,7 @@ class SceneFileWriter(object):
str
The default scene name.
"""
- if self.OUTPUT_FILE:
- return self.OUTPUT_FILE
- else:
- return self.scene.__class__.__name__
+ return self.scene.__class__.__name__
def get_resolution_directory(self):
"""
@@ -191,7 +177,7 @@ class SceneFileWriter(object):
self.partial_movie_directory,
"{:05}{}".format(
self.scene.num_plays,
- self.MOVIE_FILE_EXTENSION,
+ file_writer_config['movie_file_extension'],
)
)
return result
@@ -299,7 +285,7 @@ class SceneFileWriter(object):
allow_write : bool, optional
Whether or not to write to a video file.
"""
- if self.WRITE_TO_MOVIE and allow_write:
+ if file_writer_config['write_to_movie'] and allow_write:
self.open_movie_pipe()
def end_animation(self, allow_write=False):
@@ -312,7 +298,7 @@ class SceneFileWriter(object):
allow_write : bool, optional
Whether or not to write to a video file.
"""
- if self.WRITE_TO_MOVIE and allow_write:
+ if file_writer_config['write_to_movie'] and allow_write:
self.close_movie_pipe()
def write_frame(self, frame):
@@ -325,9 +311,9 @@ class SceneFileWriter(object):
frame : np.array
Pixel array of the frame.
"""
- if self.WRITE_TO_MOVIE:
+ if file_writer_config['write_to_movie']:
self.writing_process.stdin.write(frame.tostring())
- if self.SAVE_PNGS:
+ if file_writer_config['save_pngs']:
path, extension = os.path.splitext(self.image_file_path)
Image.fromarray(frame).save(f'{path}{self.frame_count}{extension}')
self.frame_count += 1
@@ -370,11 +356,11 @@ class SceneFileWriter(object):
If save_last_frame is True, saves the last
frame in the default image directory.
"""
- if self.WRITE_TO_MOVIE:
+ if file_writer_config['write_to_movie']:
if hasattr(self, "writing_process"):
self.writing_process.terminate()
self.combine_movie_files()
- if self.SAVE_LAST_FRAME:
+ if file_writer_config['save_last_frame']:
self.scene.update_frame(ignore_skipping=True)
self.save_final_image(self.scene.get_image())
@@ -385,7 +371,9 @@ class SceneFileWriter(object):
buffer.
"""
file_path = self.get_next_partial_movie_path()
- temp_file_path = os.path.splitext(file_path)[0] + '_temp' + self.MOVIE_FILE_EXTENSION
+ temp_file_path = (os.path.splitext(file_path)[0]
+ + '_temp'
+ + file_writer_config['movie_file_extension'])
self.partial_movie_file_path = file_path
self.temp_partial_movie_file_path = temp_file_path
@@ -406,7 +394,7 @@ class SceneFileWriter(object):
]
# TODO, the test for a transparent background should not be based on
# the file extension.
- if self.MOVIE_FILE_EXTENSION == ".mov":
+ if file_writer_config['movie_file_extension'] == ".mov":
# This is if the background of the exported
# video should be transparent.
command += [
@@ -448,12 +436,12 @@ class SceneFileWriter(object):
# single piece.
kwargs = {
"remove_non_integer_files": True,
- "extension": self.MOVIE_FILE_EXTENSION,
+ "extension": file_writer_config['movie_file_extension'],
}
- if self.scene.FROM_ANIMATION_NUMBER is not None:
- kwargs["min_index"] = self.scene.FROM_ANIMATION_NUMBER
- if self.scene.UPTO_ANIMATION_NUMBER is not None:
- kwargs["max_index"] = self.scene.UPTO_ANIMATION_NUMBER
+ if file_writer_config['from_animation_number'] is not None:
+ kwargs["min_index"] = file_writer_config['from_animation_number']
+ if file_writer_config['upto_animation_number'] is not None:
+ kwargs["max_index"] = file_writer_config['upto_animation_number']
else:
kwargs["remove_indices_greater_than"] = self.scene.num_plays - 1
partial_movie_files = get_sorted_integer_files(
@@ -495,7 +483,7 @@ class SceneFileWriter(object):
if self.includes_sound:
sound_file_path = movie_file_path.replace(
- self.MOVIE_FILE_EXTENSION, ".wav"
+ file_writer_config['movie_file_extension'], ".wav"
)
# Makes sure sound file length will match video file
self.add_audio_segment(AudioSegment.silent(0))