Skip to content

armgit / manim

Public Imported from GitHub
0

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.py
index 0abe0fb77..196ce56c6 100644
--- a/manim/mobject/mobject.py
+++ b/manim/mobject/mobject.py
@@ -33,7 +33,7 @@ from colour import Color
from manim.mobject.opengl.opengl_compatibility import ConvertToOpenGL
-from .. import config
+from .. import config, logger
from ..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.py
index a1b0a97c8..2a8e772bf 100644
--- a/manim/mobject/opengl/opengl_mobject.py
+++ b/manim/mobject/opengl/opengl_mobject.py
@@ -12,7 +12,7 @@ import moderngl
import numpy as np
from colour import Color
-from manim import config
+from manim import config, logger
from manim.constants import *
from manim.utils.bezier import integer_interpolate, interpolate
from 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.py
index 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 together
in 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
--------