Search code examples
pythonmanim

TextMobject is not defined in Manim


I tried adding text in Manim:

class FirstScene(Scene):
    def construct(self):
        text=TextMobject("text")
        self.add(text) 

but I get a TextMobject is not defined error. What should I do?


Solution

  • The error is quite straightforward.

    "TextMobject" is not defined

    This is a complaint that you TextMobject is not defined anywhere in your code, nor was it imported.

    EDIT

    After additional comments and information, the problem is that the manim library has updated and the current version has restructured its internal code organization. The guide that you linked to referenced an older version of manim where you would do from manimlib.imports import as if there was a separate imports.py.

    The updated version however, would require you to do: manimlib import *. This is confirmed by checking the official repository's guide. As well, this is the updated examples_scene.py, again from it's official repository.

    from manimlib import *
    
    class FirstScene(Scene):
        def construct(self):
            text=TextMobject("text")
            self.add(text) 
    

    If it complains about Scene not found, check that you have the latest version of the package installed (git clone again and re-install if you're using an outdated version). If you want to explicitly import it, the latest version points to Scene being at this location (https://github.com/3b1b/manim/blob/master/manimlib/scene/scene.py), so your import path would be manimlib.scene.scene:

    from manimlib.scene.scene import Scene
    

    However, should you use from manimlib import * this would have been imported as well without you making explicit imports.

    You can confirm this on the the package's __init__.py, linked here:

    ...
    from manimlib.scene.scene import *
    ...
    

    Either way, TextMobject should either be defined by you, or imported before you use it. I recommend you do an update, and then try again with the code above.

    EDIT 2

    In addition to the changes in how you import Scene, according to @giac's answer, TexMobject is renamed to Tex and TextMobject is renamed to TexText. I wouldn't count on it being still true or being the only changes so I recommend you check the official repository's guide if you stumbled here trying to get an answer.