manim
manim copied to clipboard
Add setitem to mobject
Motivation
We normally treat VGroup and mobjects as lists of submobjects. We should give them the same property like a list to get and modify a specific index of element.
Proposed changes
- In mobject.mobject.py, added setitem method under getitem
Test
Code:
def str_list_to_tex_list(self, f: list[str]) -> VGroup:
return VGroup(
*[Tex(s, **self.tex_kwargs)
for s in f]
)
def construct(self):
formulas = [
r"f(x)+g(x)=(m_{1}+m_{2})x+(b_{1}+b_{2})",
r"[f(x)+g(x)]=m_{1}+m_{2}", #
r"[f(x)+g(x)]=f'(x)+g'(x)", #
r"f(x)\cdot g(x)=(m_{1}x+b_{1})(m_{2}x+b_{2})",
r"=m_{1}m_{2}x^{2}+(m_{1}b_{2}+m_{2}b_{1})x+b_{1}b_{2}",
r"[f(x)\cdot g(x)]=2m_{1}m_{2}+m_{1}b_{2}+m_{2}b_{1}", #
r"[f(x)\cdot g(x)]=m_{1}(m_{2}+b_{2})+m_{2}(m_{1}+b_{1})", #
r"[f(x)\cdot g(x)]=f'(x)g(x)+g'(x)f(x)" #
]
formulas = self.str_list_to_tex_list(formulas).arrange(DOWN)
di = [2, 3, 6, 7, 8]
for i in di:
i = i - 1
cp = derivative_sign.copy().next_to(formulas[i], LEFT)
self.add(cp)
formulas[i] = VGroup(cp, formulas[i])
Result:
You'd want to use the method Mobject.replace_submobject instead of self.split().setitem.
In general, you don't want to directly edit the submoject list, since restructuring a mobject often requires other things like bounding boxes and updater lists to be updated.