manim.scene.scene.Scene

class Scene(renderer=None, camera_class=<class 'manim.camera.camera.Camera'>, always_update_mobjects=False, random_seed=0, **kwargs)[source]

Bases: manim.container.Container

A Scene is the canvas of your animation.

The primary role of Scene is to provide the user with tools to manage mobjects and animations. Generally speaking, a manim script consists of a class that derives from Scene whose Scene.construct() method is overridden by the user’s code.

Mobjects are displayed on screen by calling Scene.add() and removed from screen by calling Scene.remove(). All mobjects currently on screen are kept in Scene.mobjects. Animations are played by calling Scene.play().

A Scene is rendered internally by calling Scene.render(). This in turn calls Scene.setup(), Scene.construct(), and Scene.tear_down(), in that order.

It is not recommended to override the __init__ method in user Scenes. For code that should be ran before a Scene is rendered, use Scene.setup() instead.

Examples

Override the Scene.construct() method with your code.

class MyScene(Scene):
    def construct(self):
        self.play(Write(Text("Hello World!")))

Methods

add

Mobjects will be displayed, from background to foreground in the order with which they are added.

add_foreground_mobject

Adds a single mobject to the foreground, and internally to the list foreground_mobjects, and mobjects.

add_foreground_mobjects

Adds mobjects to the foreground, and internally to the list foreground_mobjects, and mobjects.

add_mobjects_from_animations

add_sound

This method is used to add a sound to the animation.

add_static_frames

bring_to_back

Removes the mobject from the scene and adds them to the back of the scene.

bring_to_front

Adds the passed mobjects to the scene again, pushing them to he front of the scene.

clear

Removes all mobjects present in self.mobjects and self.foreground_mobjects from the scene.

compile_animation_data

compile_animations

Creates _MethodAnimations from any _AnimationBuilders and updates animation kwargs with kwargs passed to play().

construct

Add content to the Scene.

get_attrs

Gets attributes of a scene given the attribute’s identifier/name.

get_mobject_family_members

Returns list of family-members of all mobjects in scene.

get_moving_and_static_mobjects

get_moving_mobjects

Gets all moving mobjects in the passed animation(s).

get_restructured_mobject_list

Given a list of mobjects and a list of mobjects to be removed, this filters out the removable mobjects from the list of mobjects.

get_run_time

Gets the total run time for a list of animations.

get_time_progression

You will hardly use this when making your own animations.

get_top_level_mobjects

Returns all mobjects which are not submobjects.

play

play_internal

This method is used to prep the animations for rendering, apply the arguments and parameters required to them, render them, and write them to the video file.

remove

Removes mobjects in the passed list of mobjects from the scene and the foreground, by removing them from “mobjects” and “foreground_mobjects”

remove_foreground_mobject

Removes a single mobject from the foreground, and internally from the list foreground_mobjects.

remove_foreground_mobjects

Removes mobjects from the foreground, and internally from the list foreground_mobjects.

render

Render this Scene.

restructure_mobjects

tl:wr

setup

This is meant to be implemented by any scenes which are commonly subclassed, and have some common setup involved before the construct method is called.

should_update_mobjects

Returns True if any mobject in Scene is being updated or if the scene has always_update_mobjects set to true.

tear_down

This is meant to be implemented by any scenes which are commonly subclassed, and have some common method to be invoked before the scene ends.

update_mobjects

Begins updating all mobjects in the Scene.

update_to_time

wait

wait_until

Like a wrapper for wait().

Attributes

camera

add(*mobjects)[source]

Mobjects will be displayed, from background to foreground in the order with which they are added.

Parameters

*mobjects (Mobject) – Mobjects to add.

Returns

The same scene after adding the Mobjects in.

Return type

Scene

add_foreground_mobject(mobject)[source]

Adds a single mobject to the foreground, and internally to the list foreground_mobjects, and mobjects.

Parameters

mobject (Mobject) – The Mobject to add to the foreground.

Returns

The Scene, with the foreground mobject added.

Return type

Scene

add_foreground_mobjects(*mobjects)[source]

Adds mobjects to the foreground, and internally to the list foreground_mobjects, and mobjects.

Parameters

*mobjects (Mobject) – The Mobjects to add to the foreground.

Returns

The Scene, with the foreground mobjects added.

Return type

Scene

add_sound(sound_file, time_offset=0, gain=None, **kwargs)[source]

This method is used to add a sound to the animation.

Parameters
  • sound_file (str) – The path to the sound file.

  • time_offset (int,float, optional) – The offset in the sound file after which the sound can be played.

  • gain

bring_to_back(*mobjects)[source]

Removes the mobject from the scene and adds them to the back of the scene.

Parameters

*mobjects (Mobject) – The mobject(s) to push to the back of the scene.

Returns

The Scene, with the mobjects pushed to the back of the scene.

Return type

Scene

bring_to_front(*mobjects)[source]

Adds the passed mobjects to the scene again, pushing them to he front of the scene.

Parameters

*mobjects (Mobject) – The mobject(s) to bring to the front of the scene.

Returns

The Scene, with the mobjects brought to the front of the scene.

Return type

Scene

clear()[source]

Removes all mobjects present in self.mobjects and self.foreground_mobjects from the scene.

Returns

The Scene, with all of its mobjects in self.mobjects and self.foreground_mobjects removed.

Return type

Scene

compile_animations(*args, **kwargs)[source]

Creates _MethodAnimations from any _AnimationBuilders and updates animation kwargs with kwargs passed to play(). :param *args: Animations to be played. :type *args: Tuple[Animation] :param **kwargs: Configuration for the call to play().

Returns

Animations to be played.

Return type

Tuple[Animation]

construct()[source]

Add content to the Scene.

From within Scene.construct(), display mobjects on screen by calling Scene.add() and remove them from screen by calling Scene.remove(). All mobjects currently on screen are kept in Scene.mobjects. Play animations by calling Scene.play().

Notes

Initialization code should go in Scene.setup(). Termination code should go in Scene.tear_down().

Examples

A typical manim script includes a class derived from Scene with an overridden Scene.contruct() method:

class MyScene(Scene):
    def construct(self):
        self.play(Write(Text("Hello World!")))
get_attrs(*keys)[source]

Gets attributes of a scene given the attribute’s identifier/name.

Parameters

*keys (str) – Name(s) of the argument(s) to return the attribute of.

Returns

List of attributes of the passed identifiers.

Return type

list

get_mobject_family_members()[source]

Returns list of family-members of all mobjects in scene. If a Circle() and a VGroup(Rectangle(),Triangle()) were added, it returns not only the Circle(), Rectangle() and Triangle(), but also the VGroup() object.

Returns

List of mobject family members.

Return type

list

get_moving_mobjects(*animations)[source]

Gets all moving mobjects in the passed animation(s).

Parameters

*animations (Animation) – The animations to check for moving mobjects.

Returns

The list of mobjects that could be moving in the Animation(s)

Return type

list

get_restructured_mobject_list(mobjects, to_remove)[source]

Given a list of mobjects and a list of mobjects to be removed, this filters out the removable mobjects from the list of mobjects.

Parameters
  • mobjects (list) – The Mobjects to check.

  • to_remove (list) – The list of mobjects to remove.

Returns

The list of mobjects with the mobjects to remove removed.

Return type

list

get_run_time(animations)[source]

Gets the total run time for a list of animations.

Parameters

animations (List[Animation, …]) – A list of the animations whose total run_time is to be calculated.

Returns

The total run_time of all of the animations in the list.

Return type

float

get_time_progression(run_time, description, n_iterations=None, override_skip_animations=False)[source]

You will hardly use this when making your own animations. This method is for Manim’s internal use.

Returns a CommandLine ProgressBar whose fill_time is dependent on the run_time of an animation, the iterations to perform in that animation and a bool saying whether or not to consider the skipped animations.

Parameters
  • run_time (float) – The run_time of the animation.

  • n_iterations (int, optional) – The number of iterations in the animation.

  • override_skip_animations (bool, optional) – Whether or not to show skipped animations in the progress bar.

Returns

The CommandLine Progress Bar.

Return type

time_progression

get_top_level_mobjects()[source]

Returns all mobjects which are not submobjects.

Returns

List of top level mobjects.

Return type

list

play_internal(skip_rendering=False)[source]

This method is used to prep the animations for rendering, apply the arguments and parameters required to them, render them, and write them to the video file.

Parameters
  • args – Animation or mobject with mobject method and params

  • kwargs – named parameters affecting what was passed in args, e.g. run_time, lag_ratio and so on.

remove(*mobjects)[source]

Removes mobjects in the passed list of mobjects from the scene and the foreground, by removing them from “mobjects” and “foreground_mobjects”

Parameters

*mobjects (Mobject) – The mobjects to remove.

remove_foreground_mobject(mobject)[source]

Removes a single mobject from the foreground, and internally from the list foreground_mobjects.

Parameters

mobject (Mobject) – The mobject to remove from the foreground.

Returns

The Scene, with the foreground mobject removed.

Return type

Scene

remove_foreground_mobjects(*to_remove)[source]

Removes mobjects from the foreground, and internally from the list foreground_mobjects.

Parameters

*to_remove (Mobject) – The mobject(s) to remove from the foreground.

Returns

The Scene, with the foreground mobjects removed.

Return type

Scene

render()[source]

Render this Scene.

restructure_mobjects(to_remove, mobject_list_name='mobjects', extract_families=True)[source]
tl:wr

If your scene has a Group(), and you removed a mobject from the Group, this dissolves the group and puts the rest of the mobjects directly in self.mobjects or self.foreground_mobjects.

In cases where the scene contains a group, e.g. Group(m1, m2, m3), but one of its submobjects is removed, e.g. scene.remove(m1), the list of mobjects will be edited to contain other submobjects, but not m1, e.g. it will now insert m2 and m3 to where the group once was.

Parameters
  • to_remove (Mobject) – The Mobject to remove.

  • mobject_list_name (str, optional) – The list of mobjects (“mobjects”, “foreground_mobjects” etc) to remove from.

  • extract_families (bool, optional) – Whether the mobject’s families should be recursively extracted.

Returns

The Scene mobject with restructured Mobjects.

Return type

Scene

setup()[source]

This is meant to be implemented by any scenes which are commonly subclassed, and have some common setup involved before the construct method is called.

should_update_mobjects()[source]

Returns True if any mobject in Scene is being updated or if the scene has always_update_mobjects set to true.

Returns

Return type

bool

tear_down()[source]

This is meant to be implemented by any scenes which are commonly subclassed, and have some common method to be invoked before the scene ends.

update_mobjects(dt)[source]

Begins updating all mobjects in the Scene.

Parameters

dt (int or float) – Change in time between updates. Defaults (mostly) to 1/frames_per_second

wait_until(stop_condition, max_time=60)[source]

Like a wrapper for wait(). You pass a function that determines whether to continue waiting, and a max wait time if that is never fulfilled.

Parameters
  • stop_condition (function) – The function whose boolean return value determines whether to continue waiting

  • max_time (int or float, optional) – The maximum wait time in seconds, if the stop_condition is never fulfilled.