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 new animation TraceColor (Name for debate) and Graph.set_edge_color() #3533

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

MrDiver
Copy link
Collaborator

@MrDiver MrDiver commented Dec 17, 2023

This PR adds some functionality which should simplify the usage of Graphs and animating the edges.

  • A function get_edge() now returns the Edge no matter if it is flipped or not. But only for Graph not for DiGraph
  • An Animation named TraceColor now exists which can smoothly transition from one color to another over a shape
  • set_edge_color on GenericGraph is overriden with an Animation such that it produces a TraceColor effect when animating edge colors.
class SetEdgeColor(Scene):
    def construct(self):
        g = Graph(vertices=[1,2,3], edges=[(1,2), (2,3), (3,1)])
        self.add(g)
        self.play(AnimationGroup((g.animate.set_edge_color((u,v), RED) for u,v in g.edges), lag_ratio=0.5))
SetEdgeColor.mp4

Where TraceColor itself has the following behavior

TraceColorExample.mp4

There are still some problems with the z_indices of objects in the graph but the concept works

manim/mobject/graph.py Fixed Show fixed Hide fixed
manim/mobject/graph.py Outdated Show resolved Hide resolved
@JasonGrace2282 JasonGrace2282 added the new feature Enhancement specifically adding a new feature (feature request should be used for issues instead) label Dec 20, 2023
@MrDiver MrDiver requested a review from behackl April 1, 2024 03:25
Copy link
Contributor

@JasonGrace2282 JasonGrace2282 left a comment

Choose a reason for hiding this comment

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

I like the animation idea!
Left some comments and questions on the implementation. Additionally, it would be nice to add some tests to make sure the methods are working properly.

"""

def __init__(
self, mobject: "VMobject", color: ParsableManimColor, **kwargs
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason VMobject is in quotations?

@@ -151,6 +151,7 @@ def __init__(
self.suspend_mobject_updating: bool = suspend_mobject_updating
self.lag_ratio: float = lag_ratio
self._on_finish: Callable[[Scene], None] = _on_finish
self.mobject: VMobject
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
self.mobject: VMobject

Is this neccessary?

anim_args = {}
animation = anim_args.pop("animation", TraceColor)

mobj = self.get_edge(vertices) # Will error on DiGraph if edge is wrong way
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be a good idea to add a warning either in the code (via logger.warning) or in the docstring about DiGraph.
Same for the part below saying "this will not work on DiGraph"

The order of the vertices is relevant here.
"""
try:
return self._edges[edge]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return self._edges[edge]
return self.edges[edge]

Typo?

@@ -584,7 +587,7 @@ def __init__(
edge_config: dict | None = None,
) -> None:
super().__init__()

self.edges = {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
self.edges = {}
self.edges: dict[tuple[Hashable, Hashable], Mobject] = {}

This way linters can raise errors about setting/getting stuff from this attribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature Enhancement specifically adding a new feature (feature request should be used for issues instead)
Projects
Status: 👀 In review
Development

Successfully merging this pull request may close these issues.

None yet

2 participants