Copies and states¶
The film's code
import manimgx as m
class CopiesAndStatesHero(m.Scene):
def construct(self) -> None:
square = m.Square(color=m.BLUE, fill_opacity=0.6)
self.play(m.Create(square))
square.save_state()
copy = square.copy().set_color(m.YELLOW)
self.play(copy.animate.shift(3 * m.RIGHT))
target = square.generate_target()
target.shift(3 * m.LEFT).rotate(m.PI / 4).set_color(m.RED)
self.play(m.MoveToTarget(square))
self.play(m.Restore(square))
self.wait()
A mobject is one object in the scene: a variable that names it names that object, and a change to it shows wherever it is. To have two, copy it. To change a mobject and come back, keep its state first. To animate a change into a state that you prepare step by step, make its target.
copy ¶
A copy of the mobject, with its whole family.
Every mobject its attributes refer to is copied once, so an attribute that refers to one of its members refers to the copy's member. The copy runs the same updater functions, on itself, and is not in the scene. Copying is cheap: the copy shares the original's points and style until either one changes them.
Source
src/manimgx/mobject.py
Keep a state¶
save_state ¶
Code
import manimgx as m
class MobjectSaveStateExample(m.Scene):
def construct(self) -> None:
square = m.Square(color=m.BLUE, fill_opacity=0.5)
square.save_state()
self.add(square)
self.play(square.animate.set_color(m.YELLOW).shift(3 * m.LEFT))
self.play(square.animate.rotate(m.PI / 4).scale(2))
self.play(m.Restore(square))
Keep a copy of the mobject as it is now, to return to with restore or the Restore animation.
The copy is the mobject's saved_state; saving again replaces it.
Source
src/manimgx/mobject.py
restore ¶
Make the mobject what it was when save_state last kept it: its points and its style, member by member (see become).
Raises an exception if its state was never saved.
Source
src/manimgx/mobject.py
Prepare a state¶
generate_target ¶
Code
import manimgx as m
class MobjectGenerateTargetExample(m.Scene):
def construct(self) -> None:
circle = m.Circle(radius=1.5, color=m.BLUE).shift(3 * m.LEFT)
target = circle.generate_target()
target.set_fill(m.GREEN, opacity=0.5).shift(6 * m.RIGHT + m.UP)
target.scale(0.5)
self.add(circle)
self.play(m.MoveToTarget(circle))
Make the mobject's target: a copy of it, which you change into the state MoveToTarget then animates the mobject to.
Using animate makes a new target too, in place of this one.
Returns The target.
Source
src/manimgx/mobject.py
target ¶
The state MoveToTarget moves the mobject to: made by generate_target, or given when the mobject was made; None if neither.
Look like another¶
become ¶
Code
import manimgx as m
class MobjectBecomeExample(m.Scene):
def construct(self) -> None:
shape = m.Circle(radius=2, color=m.RED, fill_opacity=0.8)
shape.add_updater(lambda mob, dt: mob.rotate(dt))
self.add(shape)
self.wait()
square = m.Square(side_length=4, color=m.BLUE, fill_opacity=0.8)
shape.become(square)
self.wait(2) # the same mobject: its updater still turns it
Make the mobject look like another, at once, while staying itself: the same object in the scene, with its updaters.
The two families are first given the same structure; then each member takes its
counterpart's points and its colors, opacities, stroke widths, sheen and
texture (its joint and cap styles and 3D shading stay its own, unless they are
the same). With a match_* flag or stretch, a copy of mobject is first
fitted to this mobject.
mobjectThe mobject to look like.
match_heightWhether the copy is first scaled, in proportion, to this mobject's height.
match_widthWhether it is first scaled to this mobject's width.
match_depthWhether it is first scaled to this mobject's depth.
match_centerWhether it is first moved to this mobject's center.
stretchWhether it is first stretched to this mobject's width, height and depth, in place of the
match_*scalings.
Source
src/manimgx/mobject.py
4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 | |
match_points ¶
Take another mobject's shape: its points, member by member in family order; this mobject's style stays its own.
Members past the end of the shorter family stay as they are.
mobjectThe mobject whose shape to take.
Source
src/manimgx/mobject.py