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 shorter because the skipped sections will not appear in it.
This allows for a faster workflow because you can skip all sections except the one you are currently working on. That will increase the speed of the rendering, and also will make it easier to see the result because you don't need to watch the whole video again (or fast-forward it to the point of interest).
However, I find the syntax used by Manim for sections a bit clumsy, so in this gist, I propose two new approaches:
- Make the sections Python context managers. This allows you to write code like the one shown in
example1.py
. - Make the sections independent methods of the Scene. This allows you to write code like the one shown in
example2.py
.
Both approaches keep the advantages of working with Manim sections but provide a more convenient user interface, especially in terms of navigating the code. Using either of these approaches, your code is visually indented inside each section, which allows for easier navigation. In addition, your editor can fold entire sections, hiding them while you are not working on them.