Skip to content

Commit

Permalink
Make it an option (default to false) to prerun a scene to calculate i…
Browse files Browse the repository at this point in the history
…ts number of frames
  • Loading branch information
3b1b committed Feb 3, 2023
1 parent 7c561d3 commit b25f022
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
7 changes: 7 additions & 0 deletions manimlib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ def parse_cli():
action="store_true",
help="Show progress bar for each animation",
)
parser.add_argument(
"--prerun",
action="store_true",
help="Calculate total framecount, to display in a progress bar, by doing " + \
"an initial run of the scene which skips animations."
)
parser.add_argument(
"--video_dir",
help="Directory to write video",
Expand Down Expand Up @@ -489,6 +495,7 @@ def get_configuration(args: Namespace) -> dict:
"presenter_mode": args.presenter_mode,
"leave_progress_bars": args.leave_progress_bars,
"show_animation_progress": args.show_animation_progress,
"prerun": args.prerun,
"embed_exception_mode": custom_config["embed_exception_mode"],
"embed_error_sound": custom_config["embed_error_sound"],
}
8 changes: 5 additions & 3 deletions manimlib/extract_scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ def get_scene_config(config):
}


def compute_total_frames(scene_class, scene_config):
def compute_total_frames(scene_class, scene_config, config):
"""
When a scene is being written to file, a copy of the scene is run with
skip_animations set to true so as to count how many frames it will require.
This allows for a total progress bar on rendering, and also allows runtime
errors to be exposed preemptively for long running scenes.
"""
if not config["prerun"]:
return -1
pre_config = copy.deepcopy(scene_config)
pre_config["file_writer_config"]["write_to_movie"] = False
pre_config["file_writer_config"]["save_last_frame"] = False
Expand All @@ -90,7 +92,7 @@ def get_scenes_to_render(scene_classes, scene_config, config):
if scene_class.__name__ == scene_name:
fw_config = scene_config["file_writer_config"]
if fw_config["write_to_movie"]:
fw_config["total_frames"] = compute_total_frames(scene_class, scene_config)
fw_config["total_frames"] = compute_total_frames(scene_class, scene_config, config)
scene = scene_class(**scene_config)
result.append(scene)
found = True
Expand All @@ -109,7 +111,7 @@ def get_scenes_to_render(scene_classes, scene_config, config):
for scene_class in scene_classes:
fw_config = scene_config["file_writer_config"]
if fw_config["write_to_movie"]:
fw_config["total_frames"] = compute_total_frames(scene_class, scene_config)
fw_config["total_frames"] = compute_total_frames(scene_class, scene_config, config)
scene = scene_class(**scene_config)
result.append(scene)
return result
Expand Down
8 changes: 4 additions & 4 deletions manimlib/scene/scene_file_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(
show_file_location_upon_completion: bool = False,
quiet: bool = False,
total_frames: int = 0,
progress_description_len: int = 40,
progress_description_len: int | None = None,
):
self.scene: Scene = scene
self.write_to_movie = write_to_movie
Expand All @@ -62,7 +62,8 @@ def __init__(
self.show_file_location_upon_completion = show_file_location_upon_completion
self.quiet = quiet
self.total_frames = total_frames
self.progress_description_len = progress_description_len
self.progress_description_len = progress_description_len or \
40 if total_frames > 0 else 80

# State during file writing
self.writing_process: sp.Popen | None = None
Expand Down Expand Up @@ -278,10 +279,9 @@ def open_movie_pipe(self, file_path: str) -> None:
command += [self.temp_file_path]
self.writing_process = sp.Popen(command, stdin=sp.PIPE)

if self.total_frames > 0 and not self.quiet:
if not self.quiet:
self.progress_display = ProgressDisplay(
range(self.total_frames),
# bar_format="{l_bar}{bar}|{n_fmt}/{total_fmt}",
leave=False,
ascii=True if platform.system() == 'Windows' else None,
dynamic_ncols=True,
Expand Down

0 comments on commit b25f022

Please sign in to comment.