Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

refactor copy/deepcopy. Closes #294 (#318)

Leo Torresfaa1863cf8da
diff --git a/manim/mobject/geometry.py b/manim/mobject/geometry.py
index d56712841..3ef7ead28 100644
--- a/manim/mobject/geometry.py
+++ b/manim/mobject/geometry.py
@@ -658,10 +658,6 @@ class Arrow(Line):
)
return self
- # TODO, should this be the default for everything?
- def copy(self):
- return self.deepcopy()
-
class Vector(Arrow):
CONFIG = {
diff --git a/manim/mobject/mobject.py b/manim/mobject/mobject.py
index bdb419426..71d446e76 100644
--- a/manim/mobject/mobject.py
+++ b/manim/mobject/mobject.py
@@ -123,23 +123,6 @@ class Mobject(Container):
)
def copy(self):
- # TODO, either justify reason for shallow copy, or
- # remove this redundancy everywhere
- # return self.deepcopy()
-
- copy_mobject = copy.copy(self)
- copy_mobject.points = np.array(self.points)
- copy_mobject.submobjects = [submob.copy() for submob in self.submobjects]
- copy_mobject.updaters = list(self.updaters)
- family = self.get_family()
- for attr, value in list(self.__dict__.items()):
- if isinstance(value, Mobject) and value in family and value is not self:
- setattr(copy_mobject, attr, value.copy())
- if isinstance(value, np.ndarray):
- setattr(copy_mobject, attr, np.array(value))
- return copy_mobject
-
- def deepcopy(self):
return copy.deepcopy(self)
def generate_target(self, use_deepcopy=False):
diff --git a/manim/mobject/probability.py b/manim/mobject/probability.py
index 49d09083d..a9fd5658d 100644
--- a/manim/mobject/probability.py
+++ b/manim/mobject/probability.py
@@ -213,6 +213,3 @@ class BarChart(VGroup):
bar_bottom = bar.get_bottom()
bar.stretch_to_fit_height((value / self.max_value) * self.height)
bar.move_to(bar_bottom, DOWN)
-
- def copy(self):
- return self.deepcopy()
diff --git a/manim/mobject/svg/brace.py b/manim/mobject/svg/brace.py
index 4effdf91c..e16e1d7c9 100644
--- a/manim/mobject/svg/brace.py
+++ b/manim/mobject/svg/brace.py
@@ -121,14 +121,6 @@ class BraceLabel(VMobject):
self.change_label(*text)
return self
- def copy(self):
- copy_mobject = copy.copy(self)
- copy_mobject.brace = self.brace.copy()
- copy_mobject.label = self.label.copy()
- copy_mobject.submobjects = [copy_mobject.brace, copy_mobject.label]
-
- return copy_mobject
-
class BraceText(BraceLabel):
CONFIG = {"label_constructor": TextMobject}
diff --git a/manim/mobject/types/image_mobject.py b/manim/mobject/types/image_mobject.py
index 1909f3489..f50f76352 100644
--- a/manim/mobject/types/image_mobject.py
+++ b/manim/mobject/types/image_mobject.py
@@ -36,9 +36,6 @@ class AbstractImageMobject(Mobject):
self.stretch_to_fit_height(self.height)
self.stretch_to_fit_width(self.height * w / h)
- def copy(self):
- return self.deepcopy()
-
class ImageMobject(AbstractImageMobject):
CONFIG = {
diff --git a/tests/test_copy.py b/tests/test_copy.py
new file mode 100644
index 000000000..d60125bfd
--- /dev/null
+++ b/tests/test_copy.py
@@ -0,0 +1,45 @@
+import os
+from manim import Mobject, BraceLabel, file_writer_config
+
+
+def test_mobject_copy():
+ """Test that a copy is a deepcopy."""
+ orig = Mobject()
+ orig.add(*[Mobject() for _ in range(10)])
+ copy = orig.copy()
+
+ assert orig is orig
+ assert orig is not copy
+ assert orig.submobjects is not copy.submobjects
+ for i in range(10):
+ assert orig.submobjects[i] is not copy.submobjects[i]
+
+
+def test_bracelabel_copy():
+ """Test that a copy is a deepcopy."""
+ # For this test to work, we need to tweak some folders temporarily
+ original_text_dir = file_writer_config["text_dir"]
+ original_tex_dir = file_writer_config["tex_dir"]
+ mediadir = os.path.join("tests", "tests_data", "deepcopy")
+ file_writer_config["text_dir"] = os.path.join(mediadir, "Text")
+ file_writer_config["tex_dir"] = os.path.join(mediadir, "Tex")
+
+ # Before the refactoring of Mobject.copy(), the class BraceLabel was the
+ # only one to have a non-trivial definition of copy. Here we test that it
+ # still works after the refactoring.
+ orig = BraceLabel(Mobject(), "label")
+ copy = orig.copy()
+
+ assert orig is orig
+ assert orig is not copy
+ assert orig.brace is not copy.brace
+ assert orig.label is not copy.label
+ assert orig.submobjects is not copy.submobjects
+ assert orig.submobjects[0] is orig.brace
+ assert copy.submobjects[0] is copy.brace
+ assert orig.submobjects[0] is not copy.brace
+ assert copy.submobjects[0] is not orig.brace
+
+ # Restore the original folders
+ file_writer_config["text_dir"] = original_text_dir
+ file_writer_config["tex_dir"] = original_tex_dir
diff --git a/tests/tests_data/deepcopy/Tex/b26c2d37c7d91fb6.aux b/tests/tests_data/deepcopy/Tex/b26c2d37c7d91fb6.aux
new file mode 100644
index 000000000..f23e54680
--- /dev/null
+++ b/tests/tests_data/deepcopy/Tex/b26c2d37c7d91fb6.aux
@@ -0,0 +1 @@
+\relax
diff --git a/tests/tests_data/deepcopy/Tex/b26c2d37c7d91fb6.dvi b/tests/tests_data/deepcopy/Tex/b26c2d37c7d91fb6.dvi
new file mode 100644
index 000000000..55efb62a0
Binary files /dev/null and b/tests/tests_data/deepcopy/Tex/b26c2d37c7d91fb6.dvi differ
diff --git a/tests/tests_data/deepcopy/Tex/b26c2d37c7d91fb6.svg b/tests/tests_data/deepcopy/Tex/b26c2d37c7d91fb6.svg
new file mode 100644
index 000000000..4f218a73a
--- /dev/null
+++ b/tests/tests_data/deepcopy/Tex/b26c2d37c7d91fb6.svg
@@ -0,0 +1,17 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<!-- This file was generated by dvisvgm 2.1.3 -->
+<svg height='6.9185pt' version='1.1' viewBox='161.597 -6.9185 20.5181 6.9185' width='20.5181pt' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'>
+<defs>
+<path d='M3.71606 -3.76588C3.53674 -4.1345 3.24782 -4.40349 2.7995 -4.40349C1.63387 -4.40349 0.398506 -2.93898 0.398506 -1.48443C0.398506 -0.547945 0.946451 0.109589 1.72354 0.109589C1.92279 0.109589 2.42092 0.0697385 3.01868 -0.637609C3.09838 -0.219178 3.44707 0.109589 3.92528 0.109589C4.27397 0.109589 4.50311 -0.119552 4.66252 -0.438356C4.83188 -0.797011 4.96139 -1.40473 4.96139 -1.42466C4.96139 -1.52428 4.87173 -1.52428 4.84184 -1.52428C4.74222 -1.52428 4.73225 -1.48443 4.70237 -1.34496C4.533 -0.697385 4.35367 -0.109589 3.94521 -0.109589C3.67621 -0.109589 3.64633 -0.368618 3.64633 -0.56787C3.64633 -0.787049 3.66625 -0.86675 3.77584 -1.30511C3.88543 -1.72354 3.90535 -1.82316 3.99502 -2.20174L4.35367 -3.59651C4.42341 -3.87547 4.42341 -3.89539 4.42341 -3.93524C4.42341 -4.10461 4.30386 -4.20423 4.1345 -4.20423C3.89539 -4.20423 3.74595 -3.98506 3.71606 -3.76588ZM3.06849 -1.18555C3.01868 -1.00623 3.01868 -0.986301 2.86924 -0.816936C2.43088 -0.268991 2.02242 -0.109589 1.74346 -0.109589C1.24533 -0.109589 1.10585 -0.657534 1.10585 -1.04608C1.10585 -1.54421 1.42466 -2.76961 1.6538 -3.2279C1.96264 -3.81569 2.41096 -4.18431 2.80946 -4.18431C3.45704 -4.18431 3.59651 -3.36737 3.59651 -3.3076S3.57659 -3.18804 3.56663 -3.13823L3.06849 -1.18555Z' id='g0-97'/>
+<path d='M2.38107 -6.80448C2.38107 -6.81445 2.38107 -6.91407 2.25156 -6.91407C2.02242 -6.91407 1.29514 -6.83437 1.03611 -6.81445C0.956413 -6.80448 0.846824 -6.79452 0.846824 -6.61519C0.846824 -6.49564 0.936488 -6.49564 1.08593 -6.49564C1.56413 -6.49564 1.58406 -6.4259 1.58406 -6.32628C1.58406 -6.25654 1.4944 -5.91781 1.44458 -5.70859L0.627646 -2.46077C0.508095 -1.96264 0.468244 -1.80324 0.468244 -1.45455C0.468244 -0.508095 0.996264 0.109589 1.7335 0.109589C2.90909 0.109589 4.1345 -1.37484 4.1345 -2.80946C4.1345 -3.71606 3.60648 -4.40349 2.80946 -4.40349C2.35118 -4.40349 1.94271 -4.11457 1.64384 -3.80573L2.38107 -6.80448ZM1.44458 -3.03861C1.50436 -3.25778 1.50436 -3.27771 1.59402 -3.3873C2.08219 -4.03487 2.53051 -4.18431 2.78954 -4.18431C3.14819 -4.18431 3.41719 -3.88543 3.41719 -3.24782C3.41719 -2.66002 3.08842 -1.51432 2.90909 -1.13574C2.58032 -0.468244 2.12204 -0.109589 1.7335 -0.109589C1.39477 -0.109589 1.066 -0.37858 1.066 -1.11582C1.066 -1.30511 1.066 -1.4944 1.2254 -2.12204L1.44458 -3.03861Z' id='g0-98'/>
+<path d='M1.86301 -2.30137C2.15193 -2.30137 2.88917 -2.3213 3.3873 -2.53051C4.08468 -2.82939 4.1345 -3.41719 4.1345 -3.55666C4.1345 -3.99502 3.75592 -4.40349 3.06849 -4.40349C1.96264 -4.40349 0.458281 -3.43711 0.458281 -1.69365C0.458281 -0.67746 1.04608 0.109589 2.02242 0.109589C3.44707 0.109589 4.28394 -0.946451 4.28394 -1.066C4.28394 -1.12578 4.22416 -1.19552 4.16438 -1.19552C4.11457 -1.19552 4.09465 -1.17559 4.03487 -1.09589C3.24782 -0.109589 2.16189 -0.109589 2.04234 -0.109589C1.26526 -0.109589 1.17559 -0.946451 1.17559 -1.26526C1.17559 -1.38481 1.18555 -1.69365 1.33499 -2.30137H1.86301ZM1.39477 -2.52055C1.78331 -4.03487 2.80946 -4.18431 3.06849 -4.18431C3.53674 -4.18431 3.80573 -3.89539 3.80573 -3.55666C3.80573 -2.52055 2.21171 -2.52055 1.80324 -2.52055H1.39477Z' id='g0-101'/>
+<path d='M2.57036 -6.80448C2.57036 -6.81445 2.57036 -6.91407 2.44085 -6.91407C2.21171 -6.91407 1.48443 -6.83437 1.2254 -6.81445C1.1457 -6.80448 1.03611 -6.79452 1.03611 -6.60523C1.03611 -6.49564 1.13574 -6.49564 1.28518 -6.49564C1.76339 -6.49564 1.77335 -6.40598 1.77335 -6.32628L1.74346 -6.12702L0.488169 -1.1457C0.458281 -1.03611 0.438356 -0.966376 0.438356 -0.806974C0.438356 -0.239103 0.876712 0.109589 1.34496 0.109589C1.67372 0.109589 1.92279 -0.0896638 2.09215 -0.448319C2.27148 -0.826899 2.39103 -1.40473 2.39103 -1.42466C2.39103 -1.52428 2.30137 -1.52428 2.27148 -1.52428C2.17186 -1.52428 2.16189 -1.48443 2.132 -1.34496C1.96264 -0.697385 1.77335 -0.109589 1.37484 -0.109589C1.07597 -0.109589 1.07597 -0.428394 1.07597 -0.56787C1.07597 -0.806974 1.08593 -0.856787 1.13574 -1.04608L2.57036 -6.80448Z' id='g0-108'/>
+</defs>
+<g id='page1'>
+<use x='161.597' xlink:href='#g0-108' y='0'/>
+<use x='164.765' xlink:href='#g0-97' y='0'/>
+<use x='170.031' xlink:href='#g0-98' y='0'/>
+<use x='174.307' xlink:href='#g0-101' y='0'/>
+<use x='178.946' xlink:href='#g0-108' y='0'/>
+</g>
+</svg>
\ No newline at end of file
diff --git a/tests/tests_data/deepcopy/Tex/b26c2d37c7d91fb6.tex b/tests/tests_data/deepcopy/Tex/b26c2d37c7d91fb6.tex
new file mode 100644
index 000000000..4c2651426
--- /dev/null
+++ b/tests/tests_data/deepcopy/Tex/b26c2d37c7d91fb6.tex
@@ -0,0 +1,10 @@
+\documentclass[preview]{standalone}\usepackage[english]{babel}\usepackage{amsmath}\usepackage{amssymb}\usepackage{dsfont}\usepackage{setspace}\usepackage{tipa}\usepackage{relsize}\usepackage{textcomp}\usepackage{mathrsfs}\usepackage{calligra}\usepackage{wasysym}\usepackage{ragged2e}\usepackage{physics}\usepackage{xcolor}\usepackage{microtype}\usepackage[utf8]{inputenc}\usepackage[T1]{fontenc}\linespread{1}
+\DisableLigatures{encoding = *, family = *}
+
+\begin{document}
+
+\begin{align*}
+label
+\end{align*}
+
+\end{document}
\ No newline at end of file
diff --git a/tests/tests_data/deepcopy/Tex/bb964ccc0ded6db8.aux b/tests/tests_data/deepcopy/Tex/bb964ccc0ded6db8.aux
new file mode 100644
index 000000000..f23e54680
--- /dev/null
+++ b/tests/tests_data/deepcopy/Tex/bb964ccc0ded6db8.aux
@@ -0,0 +1 @@
+\relax
diff --git a/tests/tests_data/deepcopy/Tex/bb964ccc0ded6db8.dvi b/tests/tests_data/deepcopy/Tex/bb964ccc0ded6db8.dvi
new file mode 100644
index 000000000..8be047e54
Binary files /dev/null and b/tests/tests_data/deepcopy/Tex/bb964ccc0ded6db8.dvi differ
diff --git a/tests/tests_data/deepcopy/Tex/bb964ccc0ded6db8.svg b/tests/tests_data/deepcopy/Tex/bb964ccc0ded6db8.svg
new file mode 100644
index 000000000..f57a02d91
--- /dev/null
+++ b/tests/tests_data/deepcopy/Tex/bb964ccc0ded6db8.svg
@@ -0,0 +1,16 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<!-- This file was generated by dvisvgm 2.1.3 -->
+<svg height='1.1955pt' version='1.1' viewBox='162.889 2.98878 17.9329 1.1955' width='17.9329pt' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'>
+<defs>
+<path d='M4.58281 -0.916563C4.58281 -1.18555 4.58281 -1.19552 4.3736 -1.19552C3.35741 -1.19552 1.11582 -0.71731 -0.239103 1.71357V1.87298C-0.239103 2.12204 -0.229141 2.132 0 2.132C0.18929 2.132 0.199253 2.12204 0.249066 2.0523C1.066 0.747198 2.44085 0.0398506 4.46326 0C4.58281 0 4.58281 -0.0697385 4.58281 -0.278954V-0.916563Z' id='g0-122'/>
+<path d='M4.72229 1.71357C3.36737 -0.727273 1.12578 -1.19552 0.109589 -1.19552C-0.0996264 -1.19552 -0.0996264 -1.18555 -0.0996264 -0.916563V-0.278954C-0.0996264 -0.0498132 -0.0996264 0 0.0398506 0C0.647572 0.00996264 1.63387 0.0896638 2.53051 0.52802C3.60648 1.066 4.01494 1.69365 4.26401 2.08219C4.2939 2.132 4.35367 2.132 4.48319 2.132C4.71233 2.132 4.72229 2.12204 4.72229 1.87298V1.71357Z' id='g0-123'/>
+<path d='M4.58281 -0.916563C4.58281 -1.1457 4.58281 -1.19552 4.44334 -1.19552C3.83562 -1.20548 2.84932 -1.28518 1.95268 -1.72354C0.876712 -2.26152 0.468244 -2.88917 0.219178 -3.27771C0.18929 -3.32752 0.129514 -3.32752 0 -3.32752C-0.229141 -3.32752 -0.239103 -3.31756 -0.239103 -3.06849V-2.90909C1.11582 -0.468244 3.35741 0 4.3736 0C4.58281 0 4.58281 -0.00996264 4.58281 -0.278954V-0.916563Z' id='g0-124'/>
+<path d='M4.72229 -3.06849C4.72229 -3.31756 4.71233 -3.32752 4.48319 -3.32752C4.2939 -3.32752 4.28394 -3.31756 4.23412 -3.24782C3.41719 -1.94271 2.04234 -1.23537 0.0199253 -1.19552C-0.0996264 -1.19552 -0.0996264 -1.12578 -0.0996264 -0.916563V-0.278954C-0.0996264 -0.00996264 -0.0996264 0 0.109589 0C1.12578 0 3.36737 -0.478207 4.72229 -2.90909V-3.06849Z' id='g0-125'/>
+</defs>
+<g id='page1'>
+<use x='162.889' xlink:href='#g0-124' y='4.18428'/>
+<use x='167.372' xlink:href='#g0-123' y='4.18428'/>
+<use x='171.856' xlink:href='#g0-122' y='4.18428'/>
+<use x='176.339' xlink:href='#g0-125' y='4.18428'/>
+</g>
+</svg>
\ No newline at end of file
diff --git a/tests/tests_data/deepcopy/Tex/bb964ccc0ded6db8.tex b/tests/tests_data/deepcopy/Tex/bb964ccc0ded6db8.tex
new file mode 100644
index 000000000..2c24ba153
--- /dev/null
+++ b/tests/tests_data/deepcopy/Tex/bb964ccc0ded6db8.tex
@@ -0,0 +1,10 @@
+\documentclass[preview]{standalone}\usepackage[english]{babel}\usepackage{amsmath}\usepackage{amssymb}\usepackage{dsfont}\usepackage{setspace}\usepackage{tipa}\usepackage{relsize}\usepackage{textcomp}\usepackage{mathrsfs}\usepackage{calligra}\usepackage{wasysym}\usepackage{ragged2e}\usepackage{physics}\usepackage{xcolor}\usepackage{microtype}\usepackage[utf8]{inputenc}\usepackage[T1]{fontenc}\linespread{1}
+\DisableLigatures{encoding = *, family = *}
+
+\begin{document}
+
+\begin{align*}
+\underbrace{}
+\end{align*}
+
+\end{document}
\ No newline at end of file