Repository: ManimCommunity/manim

Issue:
Cannot perform successive apply_matrix() in LinearTransformationScene
## Description of bug / unexpected behavior
<!-- Add a clear and concise description of the problem you encountered. -->
4 months ago, I was able to run code to perform successive apply_matrix() in LinearTransformationScene, and created videos of them. I added self.wait(0) in between each.

However, after revisiting manim today, now it gives an error when performing the second apply_matrix(), even though the code is the same. It is able to perform one apply_matrix().

## Expected behavior
<!-- Add a clear and concise description of what you expected to happen. -->
The code was able to create a video last time.

## How to reproduce the issue
<!-- Provide a piece of code illustrating the undesired behavior. -->

<details><summary>Code for reproducing the problem</summary>

```py
from manim import *
class test(LinearTransformationScene):
    def __init__(self):
        LinearTransformationScene.__init__(
            self,
        )
    def construct(self):
        matrix = [[-0.3, 0.9], [0.7, 0.4]]
        self.apply_matrix(matrix)
        self.wait(0)
        
        matrix = [[1, -1], [1, 1]]
        self.apply_matrix(matrix)
        self.wait(0)

```

Or the code from this link's solutions:
https://stackoverflow.com/questions/69203723/how-to-apply-two-tranformations-one-after-the-other-in-manimce
</details>

## Logs
<details><summary>Terminal output</summary>
<!-- Add "-v DEBUG" when calling manim to generate more detailed logs -->

```
Manim Community v0.15.2

[05/09/22 00:05:39] INFO     Animation 0 : Using cached data (hash :                               cairo_renderer.py:75
                             2724823860_80563834_388161424)
                    INFO     Animation 1 : Using cached data (hash :                               cairo_renderer.py:75
                             1164547788_1764062066_2625276077)
┌─────────────────────────────── Traceback (most recent call last) ────────────────────────────────┐
│                                                                                                  │
│ C:\Users\a\Documents\github\.conda\envs\manim\lib\site-packages\manim\cli\render\comma │
│ nds.py:118 in render                                                                             │
│                                                                                                  │
│   115 │   │   for SceneClass in scene_classes_from_file(file):                                   │
│   116 │   │   │   try:                                                                           │
│   117 │   │   │   │   scene = SceneClass()                                                       │
│ > 118 │   │   │   │   scene.render()                                                             │
│   119 │   │   │   except Exception:                                                              │
│   120 │   │   │   │   error_console.print_exception()                                            │
│   121 │   │   │   │   sys.exit(1)                                                                │
│ C:\Users\a\Documents\github\.conda\envs\manim\lib\site-packages\manim\scene\scene.py:2 │
│ 22 in render                                                                                     │
│                                                                                                  │
│    219 │   │   """                                                                               │
│    220 │   │   self.setup()                                                                      │
│    221 │   │   try:                                                                              │
│ >  222 │   │   │   self.construct()                                                              │
│    223 │   │   except EndSceneEarlyException:                                                    │
│    224 │   │   │   pass                                                                          │
│    225 │   │   except RerunSceneException as e:                                                  │
│                                                                                                  │
│ D:\Documents\\manim\test.py:13 in construct                                │
│                                                                                                  │
│   10 │   │   self.wait(0)                                                                        │
│   11 │   │                                                                                       │
│   12 │   │   matrix = [[1, -1], [1, 1]]                                                          │
│ > 13 │   │   self.apply_matrix(matrix)                                                           │
│   14 │   │   self.wait(0)                                                                        │
│                                                                                                  │
│ C:\Users\a\Documents\github\.conda\envs\manim\lib\site-packages\manim\scene\vector_spa │
│ ce_scene.py:1042 in apply_matrix                                                                 │
│                                                                                                  │
│   1039 │   │   **kwargs                                                                          │
│   1040 │   │   │   Any valid keyword argument of self.apply_transposed_matrix()                  │
│   1041 │   │   """                                                                               │
│ > 1042 │   │   self.apply_transposed_matrix(np.array(matrix).T, **kwargs)                        │
│   1043 │                                                                                         │
│   1044 │   def apply_inverse(self, matrix, **kwargs):                                            │
│   1045 │   │   """                                                                               │
│                                                                                                  │
│ C:\Users\a\Documents\github\.conda\envs\manim\lib\site-packages\manim\scene\vector_spa │
│ ce_scene.py:1078 in apply_transposed_matrix                                                      │
│                                                                                                  │
│   1075 │   │   │   │   [angle_of_vector(func(RIGHT)), angle_of_vector(func(UP)) - np.pi / 2],    │
│   1076 │   │   │   )                                                                             │
│   1077 │   │   │   kwargs["path_arc"] = net_rotation                                             │
│ > 1078 │   │   self.apply_function(func, **kwargs)                                               │
│   1079 │                                                                                         │
│   1080 │   def apply_inverse_transpose(self, t_matrix, **kwargs):                                │
│   1081 │   │   """                                                                               │
│                                                                                                  │
│ C:\Users\a\Documents\github\.conda\envs\manim\lib\site-packages\manim\scene\vector_spa │
│ ce_scene.py:1141 in apply_function                                                               │
│                                                                                                  │
│   1138 │   │   │   + [                                                                           │
│   1139 │   │   │   │   self.get_vector_movement(function),                                       │
│   1140 │   │   │   │   self.get_transformable_label_movement(),                                  │
│ > 1141 │   │   │   │   self.get_moving_mobject_movement(function),                               │
│   1142 │   │   │   ]                                                                             │
│   1143 │   │   │   + [Animation(f_mob) for f_mob in self.foreground_mobjects]                    │
│   1144 │   │   │   + added_anims                                                                 │
│                                                                                                  │
│ C:\Users\a\Documents\github\.conda\envs\manim\lib\site-packages\manim\scene\vector_spa │
│ ce_scene.py:986 in get_moving_mobject_movement                                                   │
│                                                                                                  │
│    983 │   │   │   │   m.target = m.copy()                                                       │
│    984 │   │   │   target_point = func(m.get_center())                                           │
│    985 │   │   │   m.target.move_to(target_point)                                                │
│ >  986 │   │   return self.get_piece_movement(self.moving_mobjects)                              │
│    987 │                                                                                         │
│    988 │   def get_vector_movement(self, func):                                                  │
│    989 │   │   """                                                                               │
│                                                                                                  │
│ C:\Users\a\Documents\github\.conda\envs\manim\lib\site-packages\manim\scene\vector_spa │
│ ce_scene.py:957 in get_piece_movement                                                            │
│                                                                                                  │
│    954 │   │   Animation                                                                         │
│    955 │   │   │   The animation of the movement.                                                │
│    956 │   │   """                                                                               │
│ >  957 │   │   start = VGroup(*pieces)                                                           │
│    958 │   │   target = VGroup(*(mob.target for mob in pieces))                                  │
│    959 │   │   if self.leave_ghost_vectors:                                                      │
│    960 │   │   │   self.add(start.copy().fade(0.7))                                              │
│                                                                                                  │
│ C:\Users\a\Documents\github\.conda\envs\manim\lib\site-packages\manim\mobject\types\ve │
│ ctorized_mobject.py:1779 in __init__                                                             │
│                                                                                                  │
│   1776 │                                                                                         │
│   1777 │   def __init__(self, *vmobjects, **kwargs):                                             │
│   1778 │   │   super().__init__(**kwargs)                                                        │
│ > 1779 │   │   self.add(*vmobjects)                                                              │
│   1780 │                                                                                         │
│   1781 │   def __repr__(self):                                                                   │
│   1782 │   │   return (                                                                          │
│                                                                                                  │
│ C:\Users\a\Documents\github\.conda\envs\manim\lib\site-packages\manim\mobject\types\ve │
│ ctorized_mobject.py:1844 in add                                                                  │
│                                                                                                  │
│   1841 │   │   │   │   │   )                                                                     │
│   1842 │   │   """                                                                               │
│   1843 │   │   if not all(isinstance(m, (VMobject, OpenGLVMobject)) for m in vmobjects):         │
│ > 1844 │   │   │   raise TypeError("All submobjects must be of type VMobject")                   │
│   1845 │   │   return super().add(*vmobjects)                                                    │
│   1846 │                                                                                         │
│   1847 │   def __add__(self, vmobject):                                                          │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘
TypeError: All submobjects must be of type VMobject
```

<!-- Insert screenshots here (only when absolutely necessary, we prefer copy/pasted output!) -->

</details>


## System specifications

<details><summary>System Details</summary>

```

- OS- Windows 10
- RAM: 16GB
- Python version (`python/py/python3 --version`): 3.9.7
- Installed modules (provide output from `pip list`):
(too many installed modules to list, using manim 0.15.2)
```
</details>




Discussion:
Please include the full traceback in your report, not just the last line.
I have edited it to include the full log
Thank you!

The problem is caused by something modifying the `moving_mobjects` list and including an element which is not a mobject. There is a manual workaround of setting `self.moving_mobjects = []` after an `apply_matrix` call, but this is not a suitable fix overall; it should be investigated when the list is modified incorrectly.
The problem with the `moving_objects` list also arises when objects are animated before the matrix transform.

See this code: the circle is not transformed according to the matrix, but only shifted to the side:
```py
class NoSquishEx(LinearTransformationScene):
    def __init__(self, **kwargs):
        LinearTransformationScene.__init__(
            self,
            show_coordinates=True,
            leave_ghost_vectors=True,
            *kwargs
        )

    def construct(self):
        matrix = [[1, 1], [0, 1]]

        circ1=Square(side_length=2, color=GREEN).shift(UP*2+RIGHT*2)
        circ2=Circle(radius=1, color=RED).shift(UP*2+RIGHT*2)
        self.play(Create(circ1))
        self.remove(circ1)
        self.add_transformable_mobject(circ2)

        self.apply_matrix(matrix)

        self.wait()
```

While this code works as intended:
```py
class NoSquishEx(LinearTransformationScene):
    def __init__(self, **kwargs):
        LinearTransformationScene.__init__(
            self,
            show_coordinates=True,
            leave_ghost_vectors=True,
            *kwargs
        )

    def construct(self):
        matrix = [[1, 1], [0, 1]]

        circ=Circle(radius=1, color=GREEN).shift(UP*2+RIGHT*2)
        self.play(Create(circ))
        self.add_transformable_mobject(circ)

        self.moving_mobjects = []
        self.apply_matrix(matrix)

        self.wait()
```

[Discord: Why does adding a vector in Linear Transformation Scene affect whether objects are squished?](https://discord.com/channels/581738731934056449/1214627362793201754/1214627362793201754)

Resolving patch:
diff --git a/manim/scene/vector_space_scene.py b/manim/scene/vector_space_scene.py
--- a/manim/scene/vector_space_scene.py
+++ b/manim/scene/vector_space_scene.py
@@ -1003,8 +1003,11 @@ def get_piece_movement(self, pieces: list | tuple | np.ndarray):
         Animation
             The animation of the movement.
         """
-        start = VGroup(*pieces)
-        target = VGroup(*(mob.target for mob in pieces))
+
+        v_pieces = [piece for piece in pieces if isinstance(piece, VMobject)]
+        start = VGroup(*v_pieces)
+        target = VGroup(*(mob.target for mob in v_pieces))
+
         # don't add empty VGroups
         if self.leave_ghost_vectors and start.submobjects:
             # start.copy() gives a VGroup of Vectors
@@ -1091,6 +1094,7 @@ def apply_matrix(self, matrix: np.ndarray | list | tuple, **kwargs):
         **kwargs
             Any valid keyword argument of self.apply_transposed_matrix()
         """
+
         self.apply_transposed_matrix(np.array(matrix).T, **kwargs)
 
     def apply_inverse(self, matrix: np.ndarray | list | tuple, **kwargs):


Test patch:
diff --git a/tests/test_graphical_units/test_vector_scene.py b/tests/test_graphical_units/test_vector_scene.py
--- a/tests/test_graphical_units/test_vector_scene.py
+++ b/tests/test_graphical_units/test_vector_scene.py
@@ -1,6 +1,6 @@
 from __future__ import annotations
 
-from manim.scene.vector_space_scene import VectorScene
+from manim.scene.vector_space_scene import LinearTransformationScene, VectorScene
 from manim.utils.testing.frames_comparison import frames_comparison
 
 __module_test__ = "vector_scene"
@@ -14,3 +14,13 @@ def test_vector_to_coords(scene):
     scene.add(basis)
     scene.vector_to_coords(vector=vector)
     scene.wait()
+
+
+def test_apply_matrix():
+    scene = LinearTransformationScene(include_background_plane=False)
+    scene.setup()
+    matrix = [[-1, 1], [1, 1]]
+    # use short runtimes to speed up animation rendering
+    scene.apply_matrix(matrix, run_time=0.01)
+    scene.wait()
+    scene.apply_inverse(matrix, run_time=0.01)