A community-maintained Python framework for creating mathematical animations.
Fixed :class:`.Graph` stopping to update after animating additions/deletions of vertices or edges (#2232)
Benjamin Hackl2f9e7174f14b
diff --git a/manim/animation/composition.py b/manim/animation/composition.pyindex 8c444cd63..632e0c72c 100644--- a/manim/animation/composition.py+++ b/manim/animation/composition.py@@ -50,12 +50,16 @@ class AnimationGroup(Animation):return list(self.group)def begin(self) -> None:+ if self.suspend_mobject_updating:+ self.group.suspend_updating()for anim in self.animations:anim.begin()def finish(self) -> None:for anim in self.animations:anim.finish()+ if self.suspend_mobject_updating:+ self.group.resume_updating()def clean_up_from_scene(self, scene: Scene) -> None:for anim in self.animations:diff --git a/manim/mobject/graph.py b/manim/mobject/graph.pyindex 028a00267..004e459e1 100644--- a/manim/mobject/graph.py+++ b/manim/mobject/graph.py@@ -633,7 +633,9 @@ class Graph(VMobject, metaclass=ConvertToOpenGL):animation = anim_args.pop("animation", Create)vertex_mobjects = self.add_vertices(*args, **kwargs)- return AnimationGroup(*(animation(v, **anim_args) for v in vertex_mobjects))+ return AnimationGroup(+ *(animation(v, **anim_args) for v in vertex_mobjects), group=self+ )def _remove_vertex(self, vertex):"""Remove a vertex (as well as all incident edges) from the graph.@@ -704,7 +706,9 @@ class Graph(VMobject, metaclass=ConvertToOpenGL):animation = anim_args.pop("animation", Uncreate)mobjects = self.remove_vertices(*vertices)- return AnimationGroup(*(animation(mobj, **anim_args) for mobj in mobjects))+ return AnimationGroup(+ *(animation(mobj, **anim_args) for mobj in mobjects), group=self+ )def _add_edge(self,@@ -817,7 +821,9 @@ class Graph(VMobject, metaclass=ConvertToOpenGL):animation = anim_args.pop("animation", Create)mobjects = self.add_edges(*args, **kwargs)- return AnimationGroup(*(animation(mobj, **anim_args) for mobj in mobjects))+ return AnimationGroup(+ *(animation(mobj, **anim_args) for mobj in mobjects), group=self+ )def _remove_edge(self, edge: Tuple[Hashable]):"""Remove an edge from the graph.@@ -873,7 +879,9 @@ class Graph(VMobject, metaclass=ConvertToOpenGL):animation = anim_args.pop("animation", Uncreate)mobjects = self.remove_edges(*edges)- return AnimationGroup(*(animation(mobj, **anim_args) for mobj in mobjects))+ return AnimationGroup(+ *(animation(mobj, **anim_args) for mobj in mobjects), group=self+ )@staticmethoddef from_networkx(nxgraph: nx.classes.graph.Graph, **kwargs) -> "Graph":diff --git a/tests/test_graph.py b/tests/test_graph.pyindex bb5f6d8c8..d9bf2a9ee 100644--- a/tests/test_graph.py+++ b/tests/test_graph.py@@ -1,4 +1,4 @@-from manim import Graph, Text+from manim import Graph, Scene, Text, tempconfigdef test_graph_creation():@@ -82,3 +82,17 @@ def test_graph_remove_edges():assert str(G) == "Graph on 5 vertices and 0 edges"assert set(G._graph.edges()) == set()assert set(G.edges.keys()) == set()+++def test_custom_animation_mobject_list():+ G = Graph([1, 2, 3], [(1, 2), (2, 3)])+ scene = Scene()+ scene.add(G)+ assert scene.mobjects == [G]+ with tempconfig({"dry_run": True, "quality": "low_quality"}):+ scene.play(G.animate.add_vertices(4))+ assert str(G) == "Graph on 4 vertices and 2 edges"+ assert scene.mobjects == [G]+ scene.play(G.animate.remove_vertices(2))+ assert str(G) == "Graph on 3 vertices and 0 edges"+ assert scene.mobjects == [G]