A community-maintained Python framework for creating mathematical animations.
Added logging for repeated mobject in VGroup (#2288)
Cindy Park276c112255de
diff --git a/manim/mobject/mobject.py b/manim/mobject/mobject.pyindex 0abe0fb77..196ce56c6 100644--- a/manim/mobject/mobject.py+++ b/manim/mobject/mobject.py@@ -33,7 +33,7 @@ from colour import Colorfrom manim.mobject.opengl.opengl_compatibility import ConvertToOpenGL-from .. import config+from .. import config, loggerfrom ..constants import *from ..utils.color import (BLACK,@@ -416,12 +416,29 @@ class Mobject:...ValueError: Mobject cannot contain self+ A given mobject cannot be added as a submobject+ twice to some parent::++ >>> parent = Mobject(name="parent")+ >>> child = Mobject(name="child")+ >>> parent.add(child, child)+ [...] WARNING ...+ parent+ >>> parent.submobjects+ [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")+ if any(mobjects.count(elem) > 1 for elem in mobjects):+ logger.warning(+ "Attempted adding some Mobject as a child more than once, "+ "this is not possible. Repetitions are ignored.",+ )+ mobjects = remove_list_redundancies(mobjects)self.submobjects = list_update(self.submobjects, mobjects)return self@@ -2771,7 +2788,14 @@ class Mobject:class Group(Mobject, metaclass=ConvertToOpenGL):- """Groups together multiple :class:`Mobjects <.Mobject>`."""+ """Groups together multiple :class:`Mobjects <.Mobject>`.++ Notes+ -----+ When adding the same mobject more than once, repetitions are ignored.+ Use :meth:`.Mobject.copy` to create a separate copy which can then+ be added to the group.+ """def __init__(self, *mobjects, **kwargs):super().__init__(**kwargs)diff --git a/manim/mobject/opengl/opengl_mobject.py b/manim/mobject/opengl/opengl_mobject.pyindex a1b0a97c8..2a8e772bf 100644--- a/manim/mobject/opengl/opengl_mobject.py+++ b/manim/mobject/opengl/opengl_mobject.py@@ -12,7 +12,7 @@ import modernglimport numpy as npfrom colour import Color-from manim import config+from manim import config, loggerfrom manim.constants import *from manim.utils.bezier import integer_interpolate, interpolatefrom manim.utils.color import *@@ -726,6 +726,11 @@ class OpenGLMobject:if self in mobjects:raise ValueError("OpenGLMobject cannot contain self")+ if any(mobjects.count(elem) > 1 for elem in mobjects):+ logger.warning(+ "Attempted adding some Mobject as a child more than once, "+ "this is not possible. Repetitions are ignored.",+ )for mobject in mobjects:if not isinstance(mobject, OpenGLMobject):raise TypeError("All submobjects must be of type OpenGLMobject")diff --git a/manim/mobject/types/vectorized_mobject.py b/manim/mobject/types/vectorized_mobject.pyindex b524822de..e2a679c7a 100644--- a/manim/mobject/types/vectorized_mobject.py+++ b/manim/mobject/types/vectorized_mobject.py@@ -1732,6 +1732,12 @@ class VGroup(VMobject, metaclass=ConvertToOpenGL):This can be used to group multiple :class:`~.VMobject` instances togetherin order to scale, move, ... them together.+ Notes+ -----+ When adding the same mobject more than once, repetitions are ignored.+ Use :meth:`.Mobject.copy` to create a separate copy which can then+ be added to the group.+Examples--------