Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

`quality` argument improvements (#2325)

Marcin Serwin586db08daaa5
diff --git a/manim/_config/utils.py b/manim/_config/utils.py
index 4181d74a4..3823a58cc 100644
--- a/manim/_config/utils.py
+++ b/manim/_config/utils.py
@@ -116,19 +116,12 @@ def make_config_parser(custom_file: str = None) -> configparser.ConfigParser:
return parser
-def _determine_quality(args: argparse.Namespace) -> str:
- for quality in constants.QUALITIES:
- if quality == constants.DEFAULT_QUALITY:
- # Skip so we prioritize anything that overwrites the default quality.
- pass
- elif getattr(args, quality, None) or (
- hasattr(args, "quality")
- and args.quality is not None
- and args.quality == constants.QUALITIES[quality]["flag"]
- ):
+def _determine_quality(qual: str) -> str:
+ for quality, values in constants.QUALITIES.items():
+ if values["flag"] is not None and values["flag"] == qual:
return quality
- return constants.DEFAULT_QUALITY
+ return qual
class ManimConfig(MutableMapping):
@@ -273,6 +266,7 @@ class ManimConfig(MutableMapping):
"plugins",
"preview",
"progress_bar",
+ "quality",
"save_as_gif",
"save_sections",
"save_last_frame",
@@ -635,6 +629,10 @@ class ManimConfig(MutableMapping):
if val:
setattr(self, "media_width", val)
+ val = parser["CLI"].get("quality", fallback="", raw=True)
+ if val:
+ self.quality = _determine_quality(val)
+
return self
def digest_args(self, args: argparse.Namespace) -> "ManimConfig":
@@ -747,7 +745,7 @@ class ManimConfig(MutableMapping):
)
# Handle the quality flags
- self.quality = _determine_quality(args)
+ self.quality = _determine_quality(getattr(args, "quality", None))
# Handle the -r flag.
rflag = args.resolution
@@ -1134,6 +1132,8 @@ class ManimConfig(MutableMapping):
@quality.setter
def quality(self, qual: str) -> None:
+ if qual is None:
+ return
if qual not in constants.QUALITIES:
raise KeyError(f"quality must be one of {list(constants.QUALITIES.keys())}")
q = constants.QUALITIES[qual]
@@ -1383,7 +1383,7 @@ class ManimConfig(MutableMapping):
Traceback (most recent call last):
KeyError: 'video_dir {media_dir}/videos/{module_name}/{quality} requires the following keyword arguments: module_name'
>>> config.get_dir("video_dir", module_name="myfile").as_posix()
- 'my_media_dir/videos/myfile/1080p60.0'
+ 'my_media_dir/videos/myfile/1080p60'
Note the quality does not need to be passed as keyword argument since
:class:`ManimConfig` does store information about quality.
@@ -1402,7 +1402,7 @@ class ManimConfig(MutableMapping):
>>> config.get_dir(
... "partial_movie_dir", module_name="myfile", scene_name="myscene"
... ).as_posix()
- 'my_media_dir/videos/myfile/1080p60.0/partial_movie_files/myscene'
+ 'my_media_dir/videos/myfile/1080p60/partial_movie_files/myscene'
Standard f-string syntax is used. Arbitrary names can be used when
defining directories, as long as the corresponding values are passed to
@@ -1445,7 +1445,7 @@ class ManimConfig(MutableMapping):
all_args = {k: self._d[k] for k in dirs}
all_args.update(kwargs)
- all_args["quality"] = f"{self.pixel_height}p{self.frame_rate}"
+ all_args["quality"] = f"{self.pixel_height}p{self.frame_rate:g}"
path = self._d[key]
while "{" in path:
diff --git a/manim/cli/render/render_options.py b/manim/cli/render/render_options.py
index e66f1c288..490b50307 100644
--- a/manim/cli/render/render_options.py
+++ b/manim/cli/render/render_options.py
@@ -3,6 +3,8 @@ import re
import click
from cloup import option, option_group
+from manim.constants import QUALITIES
+
from ... import logger
@@ -59,15 +61,20 @@ render_options = option_group(
"-q",
"--quality",
default=None,
- type=click.Choice(["l", "m", "h", "p", "k"], case_sensitive=False),
- help="""
- Render quality at the follow resolution framerates, respectively:
- 854x480 30FPS,
- 1280x720 30FPS,
- 1920x1080 60FPS,
- 2560x1440 60FPS,
- 3840x2160 60FPS
- """,
+ type=click.Choice(
+ reversed([q["flag"] for q in QUALITIES.values() if q["flag"]]),
+ case_sensitive=False,
+ ),
+ help="Render quality at the follow resolution framerates, respectively: "
+ + ", ".join(
+ reversed(
+ [
+ f'{q["pixel_width"]}x{q["pixel_height"]} {q["frame_rate"]}FPS'
+ for q in QUALITIES.values()
+ if q["flag"]
+ ]
+ )
+ ),
),
option(
"-r",