본문 바로가기

Programming/Manim Lectures

[05-1-F]Status methods: copy, save_state, restore

반응형

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

 

[05-2] Text, Equation

There are 3 classes for text and equation in Manim. Text TexMobject TextMobject Text class can only handle normal characters, not formulas. Instead, you can specify and use all fonts on your PC, and..

infograph.tistory.com

Go To: [99] Table of Contents

반응형