Skip to content

Instantly share code, notes, and snippets.

@rp4ri
Last active March 12, 2023 06:45
Show Gist options
  • Save rp4ri/e0ce419ca77ef222c689b3f943955163 to your computer and use it in GitHub Desktop.
Save rp4ri/e0ce419ca77ef222c689b3f943955163 to your computer and use it in GitHub Desktop.
Manim Useful code snippets

Manim

0. Command line arguments

manimgl [file] [scene] [flags]

1. Simple scenes

Circle

from manimlib import *
import numpy as np

class MiEscena(Scene):
    def construct(self):
        circulo = Circle()
        self.play(ShowCreation(circulo))
        self.wait()

Scene with text

from manim import *

class MiEscena(Scene):
    def construct(self):
        texto = Text("Hola mundo")
        self.play(Write(texto))
        self.wait()

Scene with text and animation

from manim import *

class MiEscena(Scene):
    def construct(self):
        texto = Text("Hola mundo")
        self.play(Write(texto))
        self.wait()
        self.play(texto.animate.shift(UP))
        self.wait()

2. Common commands

Move object

circulo.move_to(RIGHT*2)

Animate movement

self.play(circulo.animate.shift(UP))

Animate movement

self.play(circulo.animate.move_to(RIGHT*2))

Add text to a scene

texto = Text("Mi Texto")
self.play(Write(texto))

Add tex to a scene

texto = Tex("Mi Texto")
self.play(Write(texto))

Change color

circulo.set_color(RED)

Change color with animation

self.play(circulo.animate.set_color(RED))

Gradient color

circulo.set_color_by_gradient(RED, BLUE)

Gradient color with animation

self.play(circulo.animate.set_submobject_colors_by_gradient(BLUE, RED))

Change height

circulo.set_height(2)

Change height with animation

self.play(circulo.animate.set_height(2))

Change width

circulo.set_width(2)

Change width with animation

self.play(circulo.animate.set_width(2))

Change stroke width

circulo.set_stroke(width=2)

Change stroke width with animation

self.play(circulo.animate.set_stroke(width=2))

Change stroke color

circulo.set_stroke(color=RED)

3. Group commands

Get group of objects

grupo = VGroup(circulo, texto)

Add object to group

grupo.add(circulo)

Remove object from group

grupo.remove(circulo)

Get object from group

circulo = grupo[0]

Get number of objects in group

numero = len(grupo)

4. Graphs

Plot function

from manimlib import *
import numpy as np

class MiEscena(Scene):
    def construct(self):
        grafica = Axes(
            x_range=[-2, 2, 1],
            y_range=[-2, 2, 1],
            x_length=6,
            y_length=6,
            axis_config={"include_tip": True},
        )
        grafica.add_coordinate_labels()
        funcion = grafica.get_graph(lambda x: np.sin(x), color=BLUE)
        self.play(ShowCreation(grafica))
        self.play(ShowCreation(funcion))
        self.wait()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment