Search code examples
pythonmanim

How can I play an animation from a non-Scene class using manim?


I was trying to do a Neural Network animation, and I found a class online, which I modified to suit my needs. Basically, the class looks like:

class NeuralNetworkMobject(VGroup):
    ...
    ...
    def change_edge_color(...):
        edge = random.choice(n1.edges_out) # n1.edges_out is a VGroup, and edge is Line 
        # I want to play the edge fade in animation from this method.
        # I've tried to do:
        NeuralNetworkScene.play(FadeIn(edge)) # NeuralNetworkScene is the actual scene

But, the program gives an error, saying that:

  File "neural.py", line 171, in set_neuron_edge_color
    NeuralScene.play(FadeIn(n_edge))
  File "/usr/local/lib/python3.9/site-packages/manimlib/scene/scene.py", line 845, in wrapper
    self.update_skipping_status()
  AttributeError: 'FadeIn' object has no attribute 'update_skipping_status'

How can I play an animation form a non-Scene class?

Thanks,


Solution

  • So I tried everything I can, including instantiating the main scene and then trying to play the animation (which you obviously shouldn't) to directly accessing the Scene.play() method (which didn't work either). So, as a solution I came up with:

    class MainScene(Scene):
        def construct(self):
            ...
            neurons = NeuralNetworkMobject()
            your_animation_function(neurons)
    
        def your_animation_function(self, neurons):
            self.play(FadeIn(neurons))
            ...
    

    While this approach does solve the problem, just for curiosity I am asking that is it possible to play an animation from a non-Scene class?

    Thanks,