Libraries
More objects and animations from Python libraries, such as chemistry, and how to write one.
Using libraries
A library is a Python package written in Manim, like manimchem (chemistry: molecules, Bohr atoms, orbitals, the periodic table). Every library is in everyone's editor, with nothing to add or sign in for. Each has its own group in the palette (manimchem's is Chemistry), with its objects' settings in the inspector, and its own group in the animation list (some of its animations play only on the objects they were made for).
- It runs in your browser, in Python: the first time a scene uses one, Python takes a few seconds to load. PubChem is the one site a library may read, straight from your browser.
- Download (Ctrl+S) puts the library's folder next to
Scenes.py, so the scenes render with Manim Community alone, plus anything inrequirements.txtit needs. - New objects and animations always use the newest version. Projects keep the version they were made with until you update them (the inspector offers the newest), and an older version that no project uses any more is deleted for good.
Sound
manimound's Sound (the palette's Sound group) plays a sound file you upload: MP3, WAV, OGG or M4A, up to 20 MB. It draws nothing. It plays from when it comes on screen: from the scene's start if it is there from the start, or from the step that brings it in (Play sound, or any animation that brings objects in). It plays to the end of the file or of the scene, whichever is first: add a wait step to hear all of it. Volume (dB) sets how loud it is.
Sounds show on the timeline's audio row, under the steps. They play in the editor, in presentations, and
in the videos you render; the Python download has the files in Assets/ and starts each with
Manim's self.add_sound(...), where Manim plays it to the end of the file.
Adding a library
The libraries come from ManimKit, a repository on GitHub with one folder per library. To add yours, add its folder there with a pull request:
- Fork ManimKit and put your files in
Libraries/<Name>, named after your package. Setauthorin manifest.json to your GitHub user name. - Run
python Build.py Libraries/<Name>to check it. - Open a pull request. Its checks run on GitHub, and Manimator looks at the code.
Once the pull request is merged, the library is in everyone's editor within minutes, credited to your GitHub name. A change to its folder later becomes its next version. ManimKit's contributing guide has the rules.
Writing a library
Write a Python package with Manim, the way you would for Manim on your computer, and add a
manifest.json that says which of its classes the editor offers and their settings.
What's in its folder
| File | What it is |
|---|---|
manifest.json | Its name, its package, and the objects and animations the editor offers, with their settings (below). |
<package>/ | The package: __init__.py, more .py
files, and the text data files it reads (.csv, .json, .mol...). |
README.md | Optional: how to use it. |
LICENSE.md | Optional: its license, which goes into downloads with it. |
Its files can be up to 12 MB in all.
manifest.json
{
"manimkit": 3,
"name": "manimchem",
"category": "Chemistry",
"version": "0.7.0",
"description": "Chemistry for Manim",
"license": "MIT",
"author": "SavithOnline",
"library": {"package": "manimchem", "requires": []},
"objects": [
{"class": "BohrAtom", "name": "Bohr atom",
"params": [
{"name": "e", "kind": "int", "default": 6, "min": 1, "max": 118, "label": "Electrons"}
]}
],
"animations": [
{"class": "OrbitElectrons", "name": "Orbit electrons", "only": ["BohrAtom"], "run_time": 3,
"params": [{"name": "turns", "kind": "float", "default": 1, "label": "Turns"}]}
]
}name,description: what it is. The description is up to 25 words.category: its group in the palette and the animation list (up to 24 characters; the name if left out).versionis your label for this version.author: your GitHub user name. The editor credits you by it.library.package: the name Python imports it by (Scenes.pyin the Python download does:from manimchem import *). It can't be one Python, Manim or Manimator uses.library.requires: packages it imports that the browser's Python has (biopython, mpmath, networkx, pandas, pillow, pyyaml, regex, scipy, shapely, sympy).objects: the classes the editor's palette offers (up to 20). Each has itsclass(PascalCase, not one of Manim's own names), anameanddescription, astyle(which color controls the editor shows:"none"for its own colors,"shape","line"or"text") and itsparams.animations: the animations the editor offers (up to 20):class,name,description,run_time(seconds),params,"introducer": trueif it brings its object onto the screen (like FadeIn) or"remover": trueif it takes it away, andonly: the objects it plays on (leave it out to play on any object).params: the settings the editor shows, up to 20 per class. The editor calls your class with each one, by itsname, always. Kinds:float,int(withmin,max,step,slider),bool,color,str,text,font,angle(degrees in the manifest and the editor, radians in your code),pointandvector([x, y]),directionandenum(withchoices). Each may have adefault, alabeland atooltip. An object may have onesoundsetting: a sound file the user uploads (no default), which the object plays from when it comes on screen; afloatsetting namedgainsets its volume in decibels. Your class gets the file's name (its path inAssets/in the Python download).nameand a few other words Manim uses itself can't be setting names.- A new version must keep the package name. Its objects, animations and settings may change.
The package
from manim import *
class OrbitElectrons(Animation):
def __init__(self, mobject, turns=1.0, **kwargs):
self.turns = turns
super().__init__(mobject, **kwargs)
def interpolate_mobject(self, alpha):
self.mobject.become(self.starting_mobject)
for i, shell in enumerate(self.mobject[1]):
shell.rotate(self.rate_func(alpha) * TAU * self.turns / (i + 1),
about_point=self.mobject.get_center())- Write plain Manim Community: subclasses of its objects and animations, with your own methods,
super()calls and animations'interpolate_mobject. - 3D: use Manim's
Sphere,Line3DandSurfacein aThreeDScene. OpenGL classes don't run in the browser. - Don't change Manim's private parts (names starting with
_): Manimator draws with its own engine. Use Manim's public options instead. - Read your own data files with
Path(__file__).parent. To read PubChem, make a synchronous request:js.XMLHttpRequestin the browser (sys.platform == "emscripten"),urllibelsewhere. - Manimator never runs your code on its server: it only checks that the files parse and define the classes the manifest names.