MobjectMatrix cannot be instantiated with Mobjects that have no submojects
- 主要语言
- Python
- 星标
- 93.8k
- 派生
- 7.7k
- PR 合并指标
- PR 指标待抓取
描述
### Describe the bug
Due to the implementation of submobjects in the `Mobject` class, it is not possible to create a `MobjectMatrix` in some cases.
Specifically, when **all** Mobjects in the `matrix` parameter of the constructor have 0 submobjects (like `Square`, `Circle`, `Dot`...).
**Code**:
This is a working code extracted from the class `Mobject` that exemplifies the 0-length last dimension included in the numpy array. It returns a shape of `(1,2,len(self.submobjects))`, so `(1,2,0)`.
```python
import numpy as np
class Mobject(object):
def __init__(self, **kwargs):
self.submobjects: list[Mobject] = []
def __getitem__(self, value):
return self.submobjects.__getitem__(value)
def __len__(self):
return len(self.submobjects)
if __name__ == '__main__':
print(np.array([[Mobject(), Mobject()]]).shape) # (1,2,0)
```
Because of this, creating a `MobjectMatrix` with this Mobjects throws an error:
```python
a = Square().scale(0.3)
b = Square().scale(0.3)
c = Dot()
d = Circle().scale(0.3)
m1 = MobjectMatrix([[a,b], [c,d]]) # Throws error
self.add(m1)
```
Whereas if the `matrix` parameter includes one or more `Mobject` with submobjects (like `Tex`), the last dimension of the `np.array` is greater than 0 and the `MobjectMatrix` is drawn correctly.
```python
a = Square().scale(0.3)
b = Tex('\pi').scale(2) # Has items in submoject list
c = Dot()
d = Circle().scale(0.3)
m1 = MobjectMatrix([[a,b], [c,d]])
self.add(m1)
```
**Wrong display or Error traceback**:
```python
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
File :5
3 c = Dot()
4 d = Circle().scale(0.3)
----> 5 m1 = MobjectMatrix([[a,b], [c,d]]) # AttributeError
6 self.add(m1)
File ~\anaconda3\envs\animations\lib\site-packages\manimlib\mobject\matrix.py:94, in Matrix.__init__(self, matri
x, **kwargs)
92 matrix = self.matrix = np.array(matrix, ndmin=2)
93 mob_matrix = self.matrix_to_mob_matrix(matrix)
---> 94 self.organize_mob_matrix(mob_matrix)
95 # self.elements = VGroup(*mob_matrix.flatten())
96 self.elements = VGroup(*it.chain(*mob_matrix))
File ~\anaconda3\envs\animations\lib\site-packages\manimlib\mobject\matrix.py:120, in Matrix.organize_mob_matrix
(self, matrix)
118 for j, elem in enumerate(row):
119 mob = matrix[i][j]
--> 120 mob.move_to(
121 i * self.v_buff * DOWN + j * self.h_buff * RIGHT,
122 self.element_alignment_corner
123 )
124 return self
AttributeError: 'numpy.ndarray' object has no attribute 'move_to'
```
### Additional context
**OS:** Windows 10
**Manim:** `v1.6.1`
**Python:** `Python 3.9.13`
*For now, this works as a workaround*:
```python
a = Square().scale(0.3)
a.submobjects.append(None) # Workaround
b = Square().scale(0.3)
c = Dot()
d = Circle().scale(0.3)
m1 = MobjectMatrix([[a,b], [c,d]])
self.add(m1)
```
贡献指南
这个仓库没有索引到贡献指南
评估
这个 Issue 还没有评估数据。