Checks if all passed elements are an instance, or iterables of VMobject and then adds them to submobjects Parameters ---------- vmobjects List or iterable of VMobjects to add Returns ------- :class:`VGroup` Raises ------
(
self,
*vmobjects: VMobject | Iterable[VMobject],
)
| 2153 | ) |
| 2154 | |
| 2155 | def add( |
| 2156 | self, |
| 2157 | *vmobjects: VMobject | Iterable[VMobject], |
| 2158 | ) -> Self: |
| 2159 | """Checks if all passed elements are an instance, or iterables of VMobject and then adds them to submobjects |
| 2160 | |
| 2161 | Parameters |
| 2162 | ---------- |
| 2163 | vmobjects |
| 2164 | List or iterable of VMobjects to add |
| 2165 | |
| 2166 | Returns |
| 2167 | ------- |
| 2168 | :class:`VGroup` |
| 2169 | |
| 2170 | Raises |
| 2171 | ------ |
| 2172 | TypeError |
| 2173 | If one element of the list, or iterable is not an instance of VMobject |
| 2174 | |
| 2175 | Examples |
| 2176 | -------- |
| 2177 | The following example shows how to add individual or multiple `VMobject` instances through the `VGroup` |
| 2178 | constructor and its `.add()` method. |
| 2179 | |
| 2180 | .. manim:: AddToVGroup |
| 2181 | |
| 2182 | class AddToVGroup(Scene): |
| 2183 | def construct(self): |
| 2184 | circle_red = Circle(color=RED) |
| 2185 | circle_green = Circle(color=GREEN) |
| 2186 | circle_blue = Circle(color=BLUE) |
| 2187 | circle_red.shift(LEFT) |
| 2188 | circle_blue.shift(RIGHT) |
| 2189 | gr = VGroup(circle_red, circle_green) |
| 2190 | gr2 = VGroup(circle_blue) # Constructor uses add directly |
| 2191 | self.add(gr,gr2) |
| 2192 | self.wait() |
| 2193 | gr += gr2 # Add group to another |
| 2194 | self.play( |
| 2195 | gr.animate.shift(DOWN), |
| 2196 | ) |
| 2197 | gr -= gr2 # Remove group |
| 2198 | self.play( # Animate groups separately |
| 2199 | gr.animate.shift(LEFT), |
| 2200 | gr2.animate.shift(UP), |
| 2201 | ) |
| 2202 | self.play( #Animate groups without modification |
| 2203 | (gr+gr2).animate.shift(RIGHT) |
| 2204 | ) |
| 2205 | self.play( # Animate group without component |
| 2206 | (gr-circle_red).animate.shift(RIGHT) |
| 2207 | ) |
| 2208 | |
| 2209 | A `VGroup` can be created using iterables as well. Keep in mind that all generated values from an |
| 2210 | iterable must be an instance of `VMobject`. This is demonstrated below: |
| 2211 | |
| 2212 | .. manim:: AddIterableToVGroupExample |