Skip to content

Instantly share code, notes, and snippets.

@abul4fia
abul4fia / ligth_theme.py
Last active May 15, 2024 18:13
Light theme for manim
# Put this file in the same folder than your manim script
# and start your script with:
#
# import light_theme
from manim import *
config.background_color = WHITE
# Those are objects which are WHITE by default
@abul4fia
abul4fia / 00README.md
Last active May 15, 2024 17:49
Finding sub-texts by shape in manim

Finding shapes in Tex mobjects

This module implements some utility functions to search some shapes (formulas, symbols) inside a Tex (or MathTex) mobject.

The approach is to compare the shapes (bezier curves) of the symbols, instead of the particular strings that produced them. This approach is generic and avoids the use of other manim mechanisms that would break under some circunstances, such as the presence of \frac{}{} in a formula.

The function you would probably want to use is group_shapes_in_tex(). Despite the confusing name, the goal is to find all the occurences of a given symbol (or group of symbols, or list of symbols) inside a Tex mobject, and return a VGroup containing them

@abul4fia
abul4fia / 00README.md
Last active May 6, 2024 20:23
Working with sections in manim

Working with sections in manim

Manim provides out of the box the possibility of marking parts of your scene as separate sections, by using self.next_section(). This will cause a separate video to be rendered for each section, and then all of them will be stitched together for the final result. The final result will be the same as if you hadn't used sections at all.

But the use of sections allows you to mark some of them with the parameter skip_animations=True, and in that case, that section will not be rendered. The code of the section will be run anyway (because it may be defining objects or variables needed in following sections), but for every self.play(), only the final frame will be computed (and not rendered). This ensures that, at the end of the section, all objects are at the same positions as if the animations were played, but skipping the actual rendering significantly reduces the time required to run the code.

In addition, the resulting video when skip_animations=True is used will be s

@abul4fia
abul4fia / example.py
Last active April 30, 2024 12:07
Generic formula transformation under control
"""
This is an example which uses the "low-level" interface, specifying
the indexes or slices() that should be transformed one into
another.
"""
class Test(Scene):
def construct(self):
m1 = MathTex(r"\frac{x}{y} = \frac{a}{b}\times5")
self.add(m1)
m2 = MathTex(r"x\cdot b = y\cdot a\times5")
@abul4fia
abul4fia / Animations.md
Created March 11, 2024 08:56
How Animation works (more or less)

How animations work (more or less)

To be frank, I don't know all the details about making your own custom Animation class. I know only a general idea, but the details are fuzzy. I usually end up using a trial-and-error approach trying different methods and reading the source code of similar animations, until it works. But the main idea is:

  1. An animation receives a mobject (it can be a group) on which it will operate. In the __init__ method usually it only stores that object into self.mobject, so every animation has that object, and other animation attributes such as the run time, rate function, etc.
  2. When used in a .play(), manim will call its begin() method. Usually you don't need to override that. The default implementation creates another inner object called self.starting_mobject which stores the initial state of the mobject being animated, and it is simply a deep copy of self.mobject (although you can override how that starting mobject is created by overriding `create_starting_mobj

OrthogonalConnection

The class OrthogonalConnection creates a polyline that goes from p1 to p2, composed only by horizontal and vertical segments. The shape of the line is controlled by the path parameter.

There are two ways to use this class:

  1. p1 and p2 are the start and end points of the line, and path is a string with the characters "-" and "|" indicating the horizontal and vertical segments of the line.

    For example "-|-" will start with a horizontal segment, that will go until the x coordinate intermediate between p1 and p2, then a vertical segment up to the y coordinate of p2, and finally a horizontal segment to p2.

@abul4fia
abul4fia / 00README.md
Last active April 14, 2024 11:18
Code utilities

Code utilities

The module code_utils.py contains functions useful to deal with code.

Currently it only contains find_in_code() function, described below

find_in_code(code, string, lines)

Finds all occurrences of the string string in the code object code, optionally restricting the search to the lines specified in the lines

@abul4fia
abul4fia / 00README.md
Last active March 7, 2024 13:14
Concise syntax for manim's ValueTrackers

Shortcuts for working with valuetrackers

  • Tired of typing v.get_value()?
    Type ~v instead thanks to this trick.

  • Tired of typing v.animate.set_value(n)?
    Type v@n instead thanks to this trick.

@abul4fia
abul4fia / 00README.md
Last active March 5, 2024 08:21
New LatexTip for latex-style arrows

LatexTip

Manim provides few styles of arrow tips, basically a filled triangle and the "stealth".

This python module implements Latex-style arrow tips. The shape is a perfect copy of the one used by Computer Modern fonts. It was produced by exporting to svg a latex document containing $\to$, and editing the resulting SVG using Inkscape to remove the arrow stem and leave only the tip. This SVG was read into manim to extract the array of points used to define this class.

This is an example of several arrows with several kinds of tips pointing in all directions (the code is the file example.py in this gist.

@abul4fia
abul4fia / example.py
Last active November 3, 2023 16:44
Generic sequences of animations run in paralell (for Manim)
from sequences import play_sequence_in_background, join_all
class Test(Scene):
def construct(self):
c = Circle()
sq = Square().to_corner(UR)
seq1 = play_sequence_in_background(self,
[
FadeIn(c, run_time=2),
ApplyMethod(c.shift, 3*RIGHT, run_time=3),