仮想計算機構

IT業界と無縁な派遣社員のブログ

manimでドラえもんを描く

はじめに


manim の練習としてキャラクターの描画を行います。

環境

OS : Windows 10
Python : 3.7.6
Manim Community : 0.13.0

プログラム

from manim import *
import math

class Manimemon(Scene):
    def construct(self):
        # face
        blue_face = Circle(color=BLUE,fill_opacity=1,radius=1.3).set_z_index(0)
        white_face = Circle(color=WHITE,fill_opacity=1).set_z_index(1).shift(0.2*DOWN)

        # eyes
        eye_w = 0.4
        eye_h = 0.5
        eye = VGroup(Ellipse(width=eye_w,height=eye_h,color=WHITE,fill_opacity=1).set_z_index(2),
                          Ellipse(width=eye_w,height=eye_h,color=BLACK).set_z_index(3))
        eyes = VGroup(eye,eye.copy()).arrange(buff=0).shift(0.9*UP)

        black_left_eye = Circle(radius=0.1,color=BLACK,fill_opacity=1)
        black_right_eye = Circle(radius=0.1,color=BLACK,fill_opacity=1)
        black_eyes = VGroup(black_right_eye,black_left_eye).set_z_index(4).arrange(buff=0.1).shift(0.9*UP)
        
        light_left_eye = Circle(radius=0.01,color=WHITE,fill_opacity=1).move_to(black_left_eye)
        light_right_eye = Circle(radius=0.01,color=WHITE,fill_opacity=1).move_to(black_right_eye)
        light_eyes = VGroup(light_left_eye,light_right_eye).set_z_index(4)

        # nose
        nose = Circle(color=RED,fill_opacity=1,radius=0.1).set_z_index(3).shift(0.6*UP)
        nasalis = Line(color=BLACK,start=DOWN*0.7,end=nose.get_center()).set_z_index(2)

        # mouse
        mouse = Arc(color=BLACK,radius=0.7,start_angle=3.14,angle=3.14).set_z_index(2).shift(DOWN*0.7+[0,0.7,0])

        # beard
        r1,r2 = 0.4,1
        beard_angles = [math.pi/3+math.pi/2, math.pi/2+math.pi/2, math.pi*2/3+math.pi/2,
                        math.pi/3-math.pi/2, math.pi/2-math.pi/2, math.pi*2/3-math.pi/2]
        beard = VGroup(*[Line(color=BLACK,
                                    start=[r1*math.cos(theta),r1*math.sin(theta),0],
                                    end=[r2*math.cos(theta),r2*math.sin(theta),0])
                               for theta in beard_angles]).set_z_index(2)

        # manimemon
        manimemon = VGroup(blue_face,white_face,eyes,black_eyes,light_eyes,
                           nose,nasalis,mouse,beard)
        
        self.play(AnimationGroup(*[GrowFromCenter(mo) for mo in manimemon],lag_ratio=0.4))
        self.play(Wiggle(manimemon))
        self.play(FadeOut(manimemon))

        code = '''
from manim import *
import math

class Manimemon(Scene):
    def construct(self):
        # face
        blue_face = Circle(color=BLUE,
                           fill_opacity=1,
                           radius=1.3).set_z_index(0)
        white_face = Circle(color=WHITE,
                            fill_opacity=1).set_z_index(1).shift(0.2*DOWN)

        # eyes
        eye_w = 0.4
        eye_h = 0.5
        eye = VGroup(Ellipse(width=eye_w,
                             height=eye_h,
                             color=WHITE,
                             fill_opacity=1).set_z_index(2),
                     Ellipse(width=eye_w,
                             height=eye_h,
                             color=BLACK).set_z_index(3))
        eyes = VGroup(eye,eye.copy()).arrange(buff=0).shift(0.9*UP)

        black_left_eye = Circle(radius=0.1,
                                color=BLACK,
                                fill_opacity=1)
        black_right_eye = Circle(radius=0.1,
                                 color=BLACK,
                                 fill_opacity=1)
        black_eyes = VGroup(black_right_eye,black_left_eye)
        black_eyes.set_z_index(4).arrange(buff=0.1).shift(0.9*UP)
        
        light_left_eye = Circle(radius=0.01,
                                color=WHITE,
                                fill_opacity=1).move_to(black_left_eye)
        light_right_eye = Circle(radius=0.01,
                                 color=WHITE,
                                 fill_opacity=1).move_to(black_right_eye)
        light_eyes = VGroup(light_left_eye,light_right_eye).set_z_index(4)

        # nose
        nose = Circle(color=RED,
                      fill_opacity=1,
                      radius=0.1).set_z_index(3).shift(0.6*UP)
        nasalis = Line(color=BLACK,
                       start=DOWN*0.7,
                       end=nose.get_center()).set_z_index(2)

        # mouse
        mouse = Arc(color=BLACK,
                    radius=0.7,
                    start_angle=math.pi,
                    angle=math.pi).set_z_index(2).shift(DOWN*0.7+[0,0.7,0])

        # beard
        r1,r2 = 0.4,1
        beard_angles = [math.pi*5/6, math.pi, math.pi*7/6,
                        -math.pi/6, 0, math.pi/6]
        beard = VGroup(*[Line(color=BLACK,
                              start=[r1*math.cos(theta),r1*math.sin(theta),0],
                              end=[r2*math.cos(theta),r2*math.sin(theta),0])
                         for theta in beard_angles]).set_z_index(2)

        # manimemon
        manimemon = VGroup(blue_face,white_face,eyes,black_eyes,light_eyes,
                           nose,nasalis,mouse,beard)
        
        self.play(AnimationGroup(*[GrowFromCenter(mo) for mo in manimemon],
                                 lag_ratio=0.4))
        self.play(Wiggle(manimemon))
        self.play(FadeOut(manimemon))
        '''
        rendered_code = Code(code=code,
                             background="window",
                             language="Python",
                             font="Monospace",
                             font_size=20).move_to(8*DOWN)
        self.play(rendered_code.animate.move_to(8*UP),
                  run_time=15)
        self.wait(1)
        self.play(FadeOut(rendered_code))

実行

> manim .\scene.py

実行結果