Skip to content

Commit

Permalink
Merge branch 'main' into optimize-point-append
Browse files Browse the repository at this point in the history
  • Loading branch information
chopan050 authored May 22, 2024
2 parents 3bb9f15 + 0a2fbbe commit e7a28ad
Show file tree
Hide file tree
Showing 36 changed files with 2,137 additions and 785 deletions.
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Switched to ruff format:
24025b60d57301b0a59754c38d77bccd8ed69feb
13 changes: 4 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,11 @@ repos:
hooks:
- id: python-check-blanket-noqa
name: Precision flake ignores
- repo: https://github.com/psf/black
rev: 24.4.2
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.4
hooks:
- id: black
- repo: https://github.com/asottile/blacken-docs
rev: 1.16.0
hooks:
- id: blacken-docs
additional_dependencies: [black==24.4.0]
exclude: ^\.github/
- id: ruff-format
types: [python]
- repo: https://github.com/PyCQA/flake8
rev: 7.0.0
hooks:
Expand Down
2 changes: 1 addition & 1 deletion example_scenes/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def construct(self):
],
color=PURPLE_B,
fill_opacity=1,
stroke_width=0
stroke_width=0,
).shift(UP + 2 * RIGHT)
shapes = VGroup(triangle, square, circle, pentagon, pi)
self.play(SpiralIn(shapes, fade_in_fraction=0.9))
Expand Down
1 change: 0 additions & 1 deletion manim/_config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ def tempconfig(temp: ManimConfig | dict[str, Any]) -> Generator[None, None, None
8.0
>>> with tempconfig({"frame_height": 100.0}):
... print(config["frame_height"])
...
100.0
>>> config["frame_height"]
8.0
Expand Down
9 changes: 6 additions & 3 deletions manim/_config/cli_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,18 @@ def parse_cli_ctx(parser: configparser.SectionProxy) -> Context:
theme = parser["theme"] if parser["theme"] else None
if theme is None:
formatter = HelpFormatter.settings(
theme=HelpTheme(**theme_settings), **formatter_settings # type: ignore[arg-type]
theme=HelpTheme(**theme_settings),
**formatter_settings, # type: ignore[arg-type]
)
elif theme.lower() == "dark":
formatter = HelpFormatter.settings(
theme=HelpTheme.dark().with_(**theme_settings), **formatter_settings # type: ignore[arg-type]
theme=HelpTheme.dark().with_(**theme_settings),
**formatter_settings, # type: ignore[arg-type]
)
elif theme.lower() == "light":
formatter = HelpFormatter.settings(
theme=HelpTheme.light().with_(**theme_settings), **formatter_settings # type: ignore[arg-type]
theme=HelpTheme.light().with_(**theme_settings),
**formatter_settings, # type: ignore[arg-type]
)

return Context.settings(
Expand Down
5 changes: 3 additions & 2 deletions manim/mobject/geometry/tips.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ class ArrowTip(VMobject, metaclass=ConvertToOpenGL):
... RegularPolygon.__init__(self, n=5, **kwargs)
... self.width = length
... self.stretch_to_fit_height(length)
>>> arr = Arrow(np.array([-2, -2, 0]), np.array([2, 2, 0]),
... tip_shape=MyCustomArrowTip)
>>> arr = Arrow(
... np.array([-2, -2, 0]), np.array([2, 2, 0]), tip_shape=MyCustomArrowTip
... )
>>> isinstance(arr.tip, RegularPolygon)
True
>>> from manim import Scene, Create
Expand Down
3 changes: 2 additions & 1 deletion manim/mobject/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -1513,7 +1513,8 @@ def expand_vertex(self, g, vertex_id: str, depth: int):
*new_edges,
vertex_config=self.VERTEX_CONF,
positions={
k: g.vertices[vertex_id].get_center() + 0.1 * DOWN for k in new_vertices
k: g.vertices[vertex_id].get_center() + 0.1 * DOWN
for k in new_vertices
},
)
if depth < self.DEPTH:
Expand Down
14 changes: 12 additions & 2 deletions manim/mobject/graphing/coordinate_systems.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,9 @@ def add_coordinates(
ax = ThreeDAxes()
x_labels = range(-4, 5)
z_labels = range(-4, 4, 2)
ax.add_coordinates(x_labels, None, z_labels) # default y labels, custom x & z labels
ax.add_coordinates(
x_labels, None, z_labels
) # default y labels, custom x & z labels
ax.add_coordinates(x_labels) # only x labels
You can also specifically control the position and value of the labels using a dict.
Expand All @@ -405,7 +407,15 @@ def add_coordinates(
x_pos = [x for x in range(1, 8)]
# strings are automatically converted into a Tex mobject.
x_vals = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
x_vals = [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday",
]
x_dict = dict(zip(x_pos, x_vals))
ax.add_coordinates(x_dict)
"""
Expand Down
8 changes: 4 additions & 4 deletions manim/mobject/graphing/number_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ def number_to_point(self, number: float | np.ndarray) -> np.ndarray:
array([0., 0., 0.])
>>> number_line.number_to_point(1)
array([1., 0., 0.])
>>> number_line.number_to_point([1,2,3])
>>> number_line.number_to_point([1, 2, 3])
array([[1., 0., 0.],
[2., 0., 0.],
[3., 0., 0.]])
Expand Down Expand Up @@ -396,11 +396,11 @@ def point_to_number(self, point: Sequence[float]) -> float:
>>> from manim import NumberLine
>>> number_line = NumberLine()
>>> number_line.point_to_number((0,0,0))
>>> number_line.point_to_number((0, 0, 0))
0.0
>>> number_line.point_to_number((1,0,0))
>>> number_line.point_to_number((1, 0, 0))
1.0
>>> number_line.point_to_number([[0.5,0,0],[1,0,0],[1.5,0,0]])
>>> number_line.point_to_number([[0.5, 0, 0], [1, 0, 0], [1.5, 0, 0]])
array([0.5, 1. , 1.5])
"""
Expand Down
88 changes: 69 additions & 19 deletions manim/mobject/mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,63 @@ def __init__(
self.generate_points()
self.init_colors()

def _assert_valid_submobjects(self, submobjects: Iterable[Mobject]) -> Self:
"""Check that all submobjects are actually instances of
:class:`Mobject`, and that none of them is ``self`` (a
:class:`Mobject` cannot contain itself).
This is an auxiliary function called when adding Mobjects to the
:attr:`submobjects` list.
This function is intended to be overridden by subclasses such as
:class:`VMobject`, which should assert that only other VMobjects
may be added into it.
Parameters
----------
submobjects
The list containing values to validate.
Returns
-------
:class:`Mobject`
The Mobject itself.
Raises
------
TypeError
If any of the values in `submobjects` is not a :class:`Mobject`.
ValueError
If there was an attempt to add a :class:`Mobject` as its own
submobject.
"""
return self._assert_valid_submobjects_internal(submobjects, Mobject)

def _assert_valid_submobjects_internal(
self, submobjects: list[Mobject], mob_class: type[Mobject]
) -> Self:
for i, submob in enumerate(submobjects):
if not isinstance(submob, mob_class):
error_message = (
f"Only values of type {mob_class.__name__} can be added "
f"as submobjects of {type(self).__name__}, but the value "
f"{submob} (at index {i}) is of type "
f"{type(submob).__name__}."
)
# Intended for subclasses such as VMobject, which
# cannot have regular Mobjects as submobjects
if isinstance(submob, Mobject):
error_message += (
" You can try adding this value into a Group instead."
)
raise TypeError(error_message)
if submob is self:
raise ValueError(
f"Cannot add {type(self).__name__} as a submobject of "
f"itself (at index {i})."
)
return self

@classmethod
def animation_override_for(
cls,
Expand Down Expand Up @@ -414,12 +471,19 @@ def add(self, *mobjects: Mobject) -> Self:
>>> len(outer.submobjects)
1
Only Mobjects can be added::
>>> outer.add(3)
Traceback (most recent call last):
...
TypeError: Only values of type Mobject can be added as submobjects of Mobject, but the value 3 (at index 0) is of type int.
Adding an object to itself raises an error::
>>> outer.add(outer)
Traceback (most recent call last):
...
ValueError: Mobject cannot contain self
ValueError: Cannot add Mobject as a submobject of itself (at index 0).
A given mobject cannot be added as a submobject
twice to some parent::
Expand All @@ -433,12 +497,7 @@ def add(self, *mobjects: Mobject) -> Self:
[child]
"""
for m in mobjects:
if not isinstance(m, Mobject):
raise TypeError("All submobjects must be of type Mobject")
if m is self:
raise ValueError("Mobject cannot contain self")

self._assert_valid_submobjects(mobjects)
unique_mobjects = remove_list_redundancies(mobjects)
if len(mobjects) != len(unique_mobjects):
logger.warning(
Expand All @@ -464,10 +523,7 @@ def insert(self, index: int, mobject: Mobject) -> None:
mobject
The mobject to be inserted.
"""
if not isinstance(mobject, Mobject):
raise TypeError("All submobjects must be of type Mobject")
if mobject is self:
raise ValueError("Mobject cannot contain self")
self._assert_valid_submobjects([mobject])
self.submobjects.insert(index, mobject)

def __add__(self, mobject: Mobject):
Expand Down Expand Up @@ -520,13 +576,7 @@ def add_to_back(self, *mobjects: Mobject) -> Self:
:meth:`add`
"""
if self in mobjects:
raise ValueError("A mobject shouldn't contain itself")

for mobject in mobjects:
if not isinstance(mobject, Mobject):
raise TypeError("All submobjects must be of type Mobject")

self._assert_valid_submobjects(mobjects)
self.remove(*mobjects)
# dict.fromkeys() removes duplicates while maintaining order
self.submobjects = list(dict.fromkeys(mobjects)) + self.submobjects
Expand Down Expand Up @@ -2014,7 +2064,7 @@ def get_critical_point(self, direction: Vector3D) -> Point3D:
::
sample = Arc(start_angle=PI/7, angle = PI/5)
sample = Arc(start_angle=PI / 7, angle=PI / 5)
# These are all equivalent
max_y_1 = sample.get_top()[1]
Expand Down
Loading

0 comments on commit e7a28ad

Please sign in to comment.