Links:
https://3b1b.github.io/manim/getting_started/example_scenes.html
Install:
brew install ffmpeg
pip install manim
Create a new project :
manim init project my-project --default
or you can just start writing the code in a Python file.
then you should have this default code in main.py
from manim import *
class DefaultTemplate(Scene):
def construct(self):
circle = Circle() # create a circle
circle.set_fill(PINK, opacity=0.5) # set color and transparency
square = Square() # create a square
square.flip(RIGHT) # flip horizontally
square.rotate(-3 * TAU / 8) # rotate a certain amount
self.play(Create(square)) # animate the creation of the square
self.play(Transform(square, circle)) # interpolate the square into the circle
self.play(FadeOut(square)) # fade out animation
if don't have it just create main.py and put that code in it.
now run this command:
manim -pql main.py DefaultTemplate
Simply it generates a video file and start playing it right away.
Let's start from the start
Try this :
from manim import *
class DefaultTemplate(Scene):
def construct(self):
square = Square() # create a square
self.play(Create(square)) # animate the creation of the square
self.play(FadeOut(square)) # fade out animation
another example:
from manim import *
class DefaultTemplate(Scene):
def construct(self):
square = Square() # create a square
circle = Circle() # create a circle
self.play(Create(square)) # animate the creation of the square
self.play(square.animate.set_fill(PINK, opacity=0.7))
self.play(Transform(square, circle)) # interpolate the square into the circle)
Bar charts with and without animations:
class BarChartExample(Scene):
def construct(self):
chart = BarChart(
values=[-5, 40, -10, 20, -3],
bar_names=["one", "two", "three", "four", "five"],
y_range=[-20, 50, 10],
y_length=6,
x_length=10,
x_axis_config={"font_size": 36},
)
c_bar_lbls = chart.get_bar_labels(font_size=48)
self.add(chart, c_bar_lbls)
class BarChartExampleAnim(Scene):
def construct(self):
# Create the chart
chart = BarChart(
values=[-5, 40, -10, 20, -3],
bar_names=["one", "two", "three", "four", "five"],
y_range=[-20, 50, 10],
y_length=6,
x_length=10,
x_axis_config={"font_size": 36},
)
# Create labels
c_bar_lbls = chart.get_bar_labels(font_size=48)
# Animate bars one by one
self.play(LaggedStart(*[GrowFromEdge(bar, edge=DOWN) for bar in chart.bars], lag_ratio=0.2))
self.play(FadeIn(c_bar_lbls)) # Fade in labels after bars grow
self.wait(1)
More complex bar chart:
class ChangeBarValuesExample(Scene):
def construct(self):
values=[28, 0, 0, 0, 0, 0, 0, 0, 0, 0]
final_values = [33.1, 36, 29.25, 33.58, 4, 4.5, 5, 4.3, 7.2, 5.5]
chart = BarChart(
values,
y_range=[0, 40, 5],
y_length=6.5,
x_length=10,
y_axis_config={"font_size": 24},
)
self.play(Create(chart))
self.wait(2)
self.play(chart.animate.change_bar_values(final_values), run_time=3)
self.play(Create(chart.get_bar_labels(font_size=36)))
self.wait(3)
Now update dynamically:
class UpdatersExample(Scene):
def construct(self):
square = Square()
square.set_fill(BLUE_E, 1)
# Correct way to make a brace follow the square
brace = always_redraw(lambda: Brace(square, UP))
text, number = label = VGroup(
Text("Width = "),
DecimalNumber(
0,
show_ellipsis=True,
num_decimal_places=2,
include_sign=True,
)
)
label.arrange(RIGHT)
always(label.next_to, brace, UP)
f_always(number.set_value, square.get_width)
self.add(square, brace, label)
self.play(
square.animate.scale(2),
rate_func=there_and_back,
run_time=2,
)
Comments
Post a Comment