Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docstring for Mobject.rotate and Rotating class #4147

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
7c61e16
Add docstring for Mobject.rotate
irvanalhaq9 Feb 3, 2025
1e129f3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 3, 2025
750d721
Add docstring for Rotating class
irvanalhaq9 Feb 3, 2025
5c89706
Merge branch 'add-rotate-docs' of https://github.com/irvanalhaq9/mani…
irvanalhaq9 Feb 3, 2025
a5f7d95
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 3, 2025
7909467
Add 'See also' in the docs
irvanalhaq9 Feb 3, 2025
e280aff
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 3, 2025
1086b7a
Improve 'See also' in the docs
irvanalhaq9 Feb 3, 2025
bb7990b
Improve 'See also' in the docs
irvanalhaq9 Feb 3, 2025
65ccf5c
Add .animate propery in 'See also' in the docs
irvanalhaq9 Feb 3, 2025
dee17bb
Add reference for rotate() method in .animate docs
irvanalhaq9 Feb 3, 2025
4ef7893
Improve 'seealso' in override_animate docs
irvanalhaq9 Feb 3, 2025
e5a7953
Merge branch 'main' into add-rotate-docs
irvanalhaq9 Feb 4, 2025
f7e2588
Remove wrong parameter 'angle' for Rotating class in add_updater example
irvanalhaq9 Feb 4, 2025
5849201
Add example for 3D rotation by Rotating class
irvanalhaq9 Feb 4, 2025
40e8c5d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 4, 2025
be27667
Change line2d to arrow2d
irvanalhaq9 Feb 4, 2025
da71bd0
Add Rotate, rotate, .animate in 'See also' add_updater, and add add_u…
irvanalhaq9 Feb 4, 2025
e5aadf0
Add description for axis parameter in Rotating class, like Mobject.ro…
irvanalhaq9 Feb 5, 2025
f4987d8
Improve description for axis parameter in Mobject.rotate
irvanalhaq9 Feb 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions manim/animation/rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,65 @@


class Rotating(Animation):
"""Animation that rotates a Mobject.

Parameters
----------
mobject
The mobject to be rotated.
axis
The rotation axis as a numpy vector.
radians
The rotation angle in radians. Predefined constants such as ``DEGREES``
can also be used to specify the angle in degrees.
For example, ``PI`` (180 degrees) or ``120 * DEGREES`` (120 degrees).
about_point
The rotation center.
about_edge
If ``about_point`` is ``None``, this argument specifies
the direction of the bounding box point to be taken as
the rotation center.
run_time
The duration of the animation in seconds.
rate_func
The function defining the animation progress based on the relative
runtime (see :mod:`~.rate_functions`) .
**kwargs
Additional keyword arguments passed to :class:`~.Animation`.

Examples
--------
.. manim:: RotatingExample

class RotatingExample(Scene):
def construct(self):
circle = Circle(radius=1, color=BLUE)
line = Line(start=ORIGIN, end=RIGHT)
arrow = Arrow(start=ORIGIN, end=RIGHT, buff=0, color=GOLD)
self.add(circle, line, arrow)

anim_kwargs = {"rate_func": linear, "run_time": 1}
self.play(Rotating(arrow, radians=PI, about_point=arrow.get_start()), **anim_kwargs)
self.play(Rotating(arrow, radians=180 * DEGREES, about_point=arrow.get_start()), **anim_kwargs)

.. manim:: Rotating3D

class Rotating3D(ThreeDScene):
def construct(self):
axes = ThreeDAxes()
cube = Cube()
line2d = Arrow(start=[0, -1.2, 1], end=[0, 1.2, 1], color=YELLOW_E)
cube_group = VGroup(cube,line2d)
self.set_camera_orientation(gamma=0*DEGREES, phi=40*DEGREES, theta=40*DEGREES)
self.add(axes, cube_group)
self.play(Rotating(cube_group, radians=2*PI, axis=UP), run_time=3, rate_func=linear)

See also
--------
:class:`~.Rotate`, :meth:`~.Mobject.rotate`

"""

def __init__(
self,
mobject: Mobject,
Expand Down Expand Up @@ -80,6 +139,10 @@ def construct(self):
Rotate(Square(side_length=0.5), angle=2*PI, rate_func=linear),
)

See also
--------
:class:`~.Rotating`, :meth:`~.Mobject.rotate`

"""

def __init__(
Expand Down
68 changes: 64 additions & 4 deletions manim/mobject/mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ def construct(self):
will interpolate the :class:`~.Mobject` between its points prior to
``.animate`` and its points after applying ``.animate`` to it. This may
result in unexpected behavior when attempting to interpolate along paths,
or rotations.
or rotations (see :meth:`.rotate`).
If you want animations to consider the points between, consider using
:class:`~.ValueTracker` with updaters instead.

Expand Down Expand Up @@ -1011,7 +1011,7 @@ def dot_position(mobject):
label.add_updater(dot_position)
self.add(dot, label)

self.play(Rotating(dot, about_point=ORIGIN, angle=TAU, run_time=TAU, rate_func=linear))
self.play(Rotating(dot, about_point=ORIGIN, radians=TAU, run_time=TAU, rate_func=linear))

Copy link
Contributor Author

@irvanalhaq9 irvanalhaq9 Feb 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes an incorrect parameter in the documentation example of add_updater. The Rotating class uses radians instead of angle as its parameter.

.. manim:: DtUpdater

Expand Down Expand Up @@ -1282,7 +1282,67 @@ def rotate(
about_point: Point3DLike | None = None,
**kwargs,
) -> Self:
"""Rotates the :class:`~.Mobject` about a certain point."""
"""Rotates the :class:`~.Mobject` around a specified axis and point.

The rotation is counterclockwise by the given ``angle`` (following the right-hand rule).

.. note::
To animate a rotation, use :class:`~.Rotating` or :class:`~.Rotate`
instead of ``.animate.rotate(...)``.
The ``.animate.rotate(...)`` syntax only applies a transformation
from the initial state to the final rotated state
(interpolation between the two states), without showing proper rotational motion
based on the angle (from 0 to the given angle).

Parameters
----------
angle
The angle of rotation in radians. Predefined constants such as ``DEGREES``
can also be used to specify the angle in degrees.
For example, ``PI`` (180 degrees) or ``120 * DEGREES`` (120 degrees).
axis
Rotation axis (3D vector). Defaults to ``OUT`` (z-axis), producing 2D rotations
perpendicular to the screen. Use other axes (e.g., ``UP``) for 3D rotations.
about_point
The point about which the mobject rotates. If ``None``, rotation occurs around
the center of the mobject.
**kwargs
Additional keyword arguments passed to :meth:`apply_points_function_about_point`,
such as ``about_edge``.

Returns
-------
:class:`Mobject`
``self`` (for method chaining)

Examples
--------

.. manim:: RotateMethodExample
:save_last_frame:

class RotateMethodExample(Scene):
def construct(self):
circle = Circle(radius=1, color=BLUE)
line = Line(start=ORIGIN, end=RIGHT)
arrow1 = Arrow(start=ORIGIN, end=RIGHT, buff=0, color=GOLD)
group1 = VGroup(circle, line, arrow1)

group2 = group1.copy()
arrow2 = group2[2]
arrow2.rotate(angle=PI / 4, about_point=arrow2.get_start())

group3 = group1.copy()
arrow3 = group3[2]
arrow3.rotate(angle=111 * DEGREES, about_point=arrow3.get_start())

self.add(VGroup(group1, group2, group3).arrange(RIGHT, buff=1))

See also
--------
:class:`~.Rotating`, :class:`~.Rotate`, :attr:`~.Mobject.animate`, :meth:`apply_points_function_about_point`

"""
rot_matrix = rotation_matrix(angle, axis)
self.apply_points_function_about_point(
lambda points: np.dot(points, rot_matrix.T), about_point, **kwargs
Expand Down Expand Up @@ -3143,7 +3203,7 @@ def override_animate(method) -> types.FunctionType:

.. seealso::

:attr:`Mobject.animate`
:attr:`~.Mobject.animate`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a tilde (~) so that the rendered documentation displays only animate instead of Mobject.animate.

.. note::

Expand Down