3b1b / 3b1b/manim

MobjectMatrix cannot be instantiated with Mobjects that have no submojects

未關閉
#1,878 0 則留言 0 個 reaction 已指派 1 人 已被 @3b1b 認領 在 GitHub 檢視
bug
主要語言
Python
星號
93.9k
分支
7.7k
平均合併
2 天 13 小時
30 天內合併 PR
4

描述

### 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)
```

貢獻指南

這個儲存庫沒有索引到貢獻指南

研究方向

查看 manimlib/mobject/matrix.py,特別是 matrix_to_mob_matrix 方法以及其如何處理 submobjects。當所有 Mobjects 都有 zero submobjects 時會發生此問題,導致 numpy 產生最後一維為 0 的陣列。先從理解從 matrix 參數建立的 numpy 陣列形狀開始。修復可能涉及調整陣列的建構或存取方式,以避免將陣列本身視為 mobject。執行所提供的失敗範例來驗證錯誤並測試任何變更。

由索引模型根據 Issue 內容生成。

評估

領域
computer-graphics
Issue 類型
缺陷
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
描述清楚
新手友好度
55/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。