LoginSignup
2
0

More than 3 years have passed since last update.

manimの作法 その25

Posted at

概要

manimの作法、調べてみた。
animation、全部使ってみた。

サンプルコード

from manimlib.imports import *

def shift_up(mobject):
    return mobject.shift(2 * UP)

class CustomSquare(Square):
    def custom_method(self, color):
        self.set_color(color)
        self.shift(2 * UP)
        return self

class test(Scene):
    def construct(self):
        square = Square()
        anno = TextMobject("Show Creation")
        anno.shift(2 * DOWN)
        self.add(anno)
        self.play(ShowCreation(square))
        self.remove(square)
        self.remove(anno)

        square = Square()
        anno = TextMobject("Uncreate")
        anno.shift(2 * DOWN)
        self.add(anno)
        self.add(square)
        self.play(Uncreate(square))
        self.remove(square)
        self.remove(anno)

        square = Square(fill_opacity = 1.0)
        anno = TextMobject("Draw border then fill")
        anno.shift(2 * DOWN)
        self.add(anno)
        self.add(square)
        self.play(DrawBorderThenFill(square))
        self.remove(square)
        self.remove(anno)

        text = TextMobject("Hello World")
        anno = TextMobject("Write")
        anno.shift(2 * DOWN)
        self.add(anno)
        self.play(Write(text))
        self.remove(text)
        self.remove(anno)

        points = []
        for x in range(-5, 6):
            points.append(Dot(point = np.array([x, 0.0, 0.0])))
        group = VGroup(*points)
        anno = TextMobject("Show submobjects")
        anno.shift(2 * DOWN)
        self.add(anno)
        self.play(ShowIncreasingSubsets(group, run_time = 3.0))
        self.remove(group)
        self.remove(anno)

        points = []
        for x in range(-5, 6):
            points.append(Dot(point = np.array([x, 0.0, 0.0])))
        group = VGroup(*points)
        anno = TextMobject("Show submobjects one by one")
        anno.shift(2 * DOWN)
        self.add(anno)
        self.play(ShowSubmobjectsOneByOne(group, run_time = 3.0))
        self.remove(group)
        self.remove(anno)

        square = Square()
        circle = Circle()
        anno = TextMobject("Transform")
        anno.shift(2 * DOWN)
        self.add(anno)
        self.add(square)
        self.play(Transform(square, circle))
        square.generate_target()
        square.target.move_to(2 * UP)
        self.play(MoveToTarget(square))
        self.remove(square)
        self.remove(circle)
        self.remove(anno)

        square = Square()
        circle = Circle()
        anno = TextMobject("Replacement Transform")
        anno.shift(2 * DOWN)
        self.add(anno)
        self.add(square)
        self.play(ReplacementTransform(square, circle))
        circle.generate_target()
        circle.target.move_to(2 * UP)
        self.play(MoveToTarget(circle))
        self.remove(square)
        self.remove(circle)
        self.remove(anno)

        square = Square()
        circle = Circle()
        anno = TextMobject("Transform from copy")
        anno.shift(2 * DOWN)
        self.add(anno)
        self.add(square)
        self.play(TransformFromCopy(square, circle))
        self.remove(circle)
        self.wait(2)
        self.remove(square)
        self.remove(circle)
        self.remove(anno)

        square = Square()
        circle = Circle()
        anno = TextMobject("Clockwise transform")
        anno.shift(2 * DOWN)
        self.add(anno)
        self.add(square)
        self.play(ClockwiseTransform(square, circle))
        self.remove(square)
        self.remove(circle)
        self.remove(anno)

        square = Square()
        circle = Circle()
        anno = TextMobject("Counterclockwise transform")
        anno.shift(2 * DOWN)
        self.add(anno)
        self.add(square)
        self.play(CounterclockwiseTransform(square, circle))
        self.remove(square)
        self.remove(circle)
        self.remove(anno)

        square = Square()
        square.generate_target()
        square.target.shift(2 * UP)
        anno = TextMobject("Move to target")
        anno.shift(2 * DOWN)
        self.add(square)
        self.play(MoveToTarget(square))
        self.remove(square)
        self.remove(anno)

        square = CustomSquare()
        anno = TextMobject("Apply method")
        anno.shift(2 * DOWN)
        self.add(anno)
        self.add(square)
        self.play(ApplyMethod(square.custom_method, RED))
        self.remove(square)
        self.remove(anno)

        square = Square()
        anno = TextMobject("Apply pointwise function")
        anno.shift(2 * DOWN)
        self.add(anno)
        self.add(square)
        self.play(ApplyPointwiseFunction(lambda x: 2 * x + UP, square))
        self.remove(square)
        self.remove(anno)

        square = Square(fill_opacity = 1)
        anno = TextMobject("Fade to color")
        anno.shift(2 * DOWN)
        self.add(anno)
        self.add(square)
        self.play(FadeToColor(square, RED))
        self.remove(square)
        self.remove(anno)

        square = Square()
        anno = TextMobject("Scale in place")
        anno.shift(2 * DOWN)
        self.add(anno)
        self.add(square)
        self.play(ScaleInPlace(square, 0.5))
        self.remove(square)
        self.remove(anno)

        square = Square()
        anno = TextMobject("Shrink to center")
        anno.shift(2 * DOWN)
        self.add(anno)
        self.add(square)
        self.play(ShrinkToCenter(square))
        self.remove(square)
        self.remove(anno)

        square = Square()
        anno = TextMobject("Restore")
        anno.shift(2 * DOWN)
        self.add(anno)
        square.save_state()
        circle = Circle()
        self.play(Transform(square, circle))
        square.generate_target()
        square.target.shift(2 * UP)
        self.play(MoveToTarget(square))
        self.play(Restore(square))
        self.remove(square)
        self.remove(circle)
        self.remove(anno)

        square = Square()
        anno = TextMobject("Apply Function")
        anno.shift(2 * DOWN)
        self.add(anno)
        self.add(square)
        self.play(ApplyFunction(shift_up, square))
        self.remove(square)
        self.remove(anno)

        mat = [[1.0, 0.5], [1.0, 0.0]]
        square = Square()
        anno = TextMobject("Apply Matrix")
        anno.shift(2 * DOWN)
        self.add(anno)
        self.add(square)
        self.play(ApplyMatrix(mat, square))
        self.remove(square)
        self.remove(anno)

        square = Square()
        anno = TextMobject("Apply Complex Function")
        anno.shift(2 * DOWN)
        self.add(anno)
        self.add(square)
        self.play(ApplyComplexFunction(lambda complex_num: complex_num + 2 * np.complex(0, 1), square))
        self.remove(square)
        self.remove(anno)

        square = Square()
        circle = Circle()
        circle.shift(2 * UP + 2 * RIGHT)
        triangle = Triangle()
        triangle.shift(2 * UP + 2 * LEFT)
        anno = TextMobject("Cyclic Replace")
        anno.shift(2 * DOWN)
        self.add(anno)
        self.add(square)
        self.add(circle)
        self.add(triangle)
        self.play(CyclicReplace(square, circle, triangle))
        self.remove(square)
        self.remove(circle)
        self.remove(triangle)
        self.remove(anno)

        square = Square()
        anno = TextMobject("Fade In")
        anno.shift(2 * DOWN)
        self.add(anno)
        self.play(FadeIn(square))
        self.remove(square)
        self.remove(anno)

        square = Square()
        anno = TextMobject("Fade Out")
        anno.shift(2 * DOWN)
        self.add(anno)
        self.add(square)
        self.play(FadeOut(square))
        self.remove(square)
        self.remove(anno)

        square = Square()
        for label, edge in zip(["LEFT", "RIGHT", "UP", "DOWN"], [LEFT, RIGHT, UP, DOWN]):
            anno = TextMobject(f"Fade In from {label}")
            anno.shift(2 * DOWN)
            self.add(anno)
            self.play(FadeInFrom(square, edge))
            self.remove(anno, square)

        square = Square()
        for label, edge in zip(["LEFT", "RIGHT", "UP", "DOWN"], [LEFT, RIGHT, UP, DOWN]):
            anno = TextMobject(f"Fade Out and shift {label}")
            anno.shift(2 * DOWN)
            self.add(anno)
            self.play(FadeOutAndShift(square, edge))
            self.remove(anno, square)

        square = Square()
        for factor in [0.1, 0.5, 0.8, 1, 2, 5]:
            anno = TextMobject(f"Fade In from large scale\_factor={factor}")
            anno.shift(2 * DOWN)
            self.add(anno)
            self.play(FadeInFromLarge(square, scale_factor = factor))
            self.remove(anno, square)

        square = Square()
        for i in range(-6, 7, 2):
            anno = TextMobject(f"Fade In from point {i}")
            anno.shift(2 * DOWN)
            self.add(anno)
            self.play(FadeInFromPoint(square, point = i))
            self.remove(anno, square)

        for label, edge in zip(["LEFT", "RIGHT", "UP", "DOWN"], [LEFT, RIGHT, UP, DOWN]):
            anno = TextMobject(f"Grow from {label} edge")
            anno.shift(2 * DOWN)
            self.add(anno)
            square = Square()
            self.play(GrowFromEdge(square, edge))
            self.remove(anno, square)

        square = Square()
        anno = TextMobject("Grow from center")
        anno.shift(2 * DOWN)
        self.add(anno)
        self.play(GrowFromCenter(square))
        self.remove(square)
        self.remove(anno)

        square = Square()
        for diag in [UP + LEFT, UP + RIGHT, DOWN + LEFT, DOWN + RIGHT]:
            self.play(FadeInFrom(square, diag))
        self.remove(square)

        text = TextMobject(r"Hello World !\\This should be written word by word.")
        anno = TextMobject("Add text word by word")
        anno.shift(2 * DOWN)
        self.add(anno)
        self.play(AddTextWordByWord(text, run_time = 5.0))
        self.remove(text)
        self.remove(anno)


生成した動画

以上。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0