Skip to content

Commit 94711f7

Browse files
Allow accessing ghost vectors in :class:.LinearTransformationScene (#3435)
* Fix CSV reader adding empty files Fixes issue #3311 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Added LinearTransformationScene.ghost_vectors * Added test and prevented empty VGroups as ghost vectors * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fixed typo in example * Added ability to join together multiple renders * Revert "Added ability to join together multiple renders" (wrong branch) This reverts commit dee29c3. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 4315549 commit 94711f7

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

manim/scene/vector_space_scene.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ def __init__(self, **kwargs):
572572
self,
573573
show_coordinates=True,
574574
leave_ghost_vectors=True,
575-
*kwargs
575+
**kwargs
576576
)
577577
578578
def construct(self):
@@ -616,6 +616,8 @@ def __init__(
616616
},
617617
}
618618

619+
self.ghost_vectors = VGroup()
620+
619621
self.foreground_plane_kwargs = {
620622
"x_range": np.array([-config["frame_width"], config["frame_width"], 1.0]),
621623
"y_range": np.array([-config["frame_width"], config["frame_width"], 1.0]),
@@ -741,6 +743,13 @@ def add_moving_mobject(
741743
mobject.target = target_mobject
742744
self.add_special_mobjects(self.moving_mobjects, mobject)
743745

746+
def get_ghost_vectors(self) -> VGroup:
747+
"""
748+
Returns all ghost vectors ever added to ``self``. Each element is a ``VGroup`` of
749+
two ghost vectors.
750+
"""
751+
return self.ghost_vectors
752+
744753
def get_unit_square(
745754
self, color: str = YELLOW, opacity: float = 0.3, stroke_width: float = 3
746755
):
@@ -996,8 +1005,11 @@ def get_piece_movement(self, pieces: list | tuple | np.ndarray):
9961005
"""
9971006
start = VGroup(*pieces)
9981007
target = VGroup(*(mob.target for mob in pieces))
999-
if self.leave_ghost_vectors:
1000-
self.add(start.copy().fade(0.7))
1008+
# don't add empty VGroups
1009+
if self.leave_ghost_vectors and start.submobjects:
1010+
# start.copy() gives a VGroup of Vectors
1011+
self.ghost_vectors.add(start.copy().fade(0.7))
1012+
self.add(self.ghost_vectors[-1])
10011013
return Transform(start, target, lag_ratio=0)
10021014

10031015
def get_moving_mobject_movement(self, func: Callable[[np.ndarray], np.ndarray]):
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from manim import RIGHT, UP, LinearTransformationScene, Vector, VGroup
2+
3+
__module_test__ = "vector_space_scene"
4+
5+
6+
def test_ghost_vectors_len_and_types():
7+
scene = LinearTransformationScene()
8+
scene.leave_ghost_vectors = True
9+
10+
# prepare vectors (they require a vmobject as their target)
11+
v1, v2 = Vector(RIGHT), Vector(RIGHT)
12+
v1.target, v2.target = Vector(UP), Vector(UP)
13+
14+
# ghost_vector addition is in this method
15+
scene.get_piece_movement((v1, v2))
16+
17+
ghosts = scene.get_ghost_vectors()
18+
assert len(ghosts) == 1
19+
# check if there are two vectors in the ghost vector VGroup
20+
assert len(ghosts[0]) == 2
21+
22+
# check types of ghost vectors
23+
assert isinstance(ghosts, VGroup) and isinstance(ghosts[0], VGroup)
24+
assert all(isinstance(x, Vector) for x in ghosts[0])

0 commit comments

Comments
 (0)