Learn how to copy an object, save the object's current state (save_state), and restore it.
copy
manimlib.mobject.mobject.Mobject.copy(self)
Copy the object.
Returns: the cloned(copied) object
If you need to create 100 Dots, you can create 100 new Dot objects, but you can also create one, specify the size and color, and then use copy method to create the rest.
dots = VGroup(*[dot.copy() for i in range(0,100])
save_state, restore
manimlib.mobject.mobject.Mobject.save_state(self, use_deepcopy=False)
Save the current state of the object.
Used in pairs with the restore method.
The actual operation is to copy (or deepcopy) the object and save the object in a global variable called 'save_state'.
When restore method is called, the contents of 'save_state' are restored.
Parameters: use_deepcopy=False
Decide whether to use deepcopy.
If False, copy() is used.
If True, deepcopy() is used.
Returns: The object itself (the current state is stored in a variable called save_state)
restore
manimlib.mobject.mobject.Mobject.restore(self)
Restore the object to the state stored in the save_state variable.
Exception("Trying to restore without having saved") occurs if restore method is called when save_state method has never been called first.
Returns:
Object itself restored to the status of save_state
If you create a dot and move it back and forth, and you want to return to the original location,
- save the state with dot.save_state() when you created the dot,
- then return to the original location by calling dot.restore() when you want to come.
The example below is coded like that.
class RestoreTest(Scene):
def construct(self):
dot = Dot()
dot.save_state()
self.add(dot)
self.play(dot.shift, 2 * UP)
self.wait()
self.play(dot.shift, 5 * DOWN)
self.wait()
self.play(dot.restore)
self.wait()
Next: [05-2] Text, Equation
Go To: [99] Table of Contents
'Programming > Manim Lectures' 카테고리의 다른 글
[05-2-A] Text: for general text string (0) | 2020.06.06 |
---|---|
[05-2] Text, Equation (0) | 2020.06.06 |
[05-1-E]Shape changing methods: scale, rotate (0) | 2020.06.05 |
[05-1-D]Coloring methods: set_color, set_color_by_gradient (0) | 2020.06.05 |
[05-1-C]Aligning methods: align_to, arrange, arrange_in_grid (0) | 2020.06.05 |