question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Creating own VMobjects

See original GitHub issue

I want to create an own VMobject. My code looks like this:

from manimlib.imports import *

class Myobject(VMobject):
    def __init__(self, offset, **kwargs):
        self.offset= offset
        VMobject.__init__(self, **kwargs)

    def generate_points(self):
        self.term = Square()
        self.term.set_style(fill_color=GREEN, fill_opacity=1)
        self.term.move_to(RIGHT*self.offset)
        self.add(self.term)


class MyExample(Scene):
    def construct(self):
        d1 = Square()
        d1.set_style(fill_color=GREEN, fill_opacity=1)
        self.add(d1)

        d2= Myobject(offset=3)
        self.add(d2)

but the image looks like this Myexample Problem: The color is lost. I guess I have to add something like init_colors(). I am grateful for any advice

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Elteoremadebeethovencommented, Jul 9, 2019

I correct my previous comment, the reason why it does not recognize the color is due to the structure of Mobject (manimlib/mobject/mobject.py):

class Mobject(Container):
    """
    Mathematical Object
    """
    CONFIG = {
        "color": WHITE,
        "name": None,
        "dim": 3,
        "target": None,
    }

    def __init__(self, *submobjects, **kwargs):
        Container.__init__(self, *submobjects, **kwargs)
        if not all([isinstance(m, Mobject) for m in submobjects]):
            raise Exception("All submobjects must be of type Mobject")
        self.submobjects = list(submobjects)
        self.color = Color(self.color)
        if self.name is None:
            self.name = self.__class__.__name__
        self.updaters = []
        self.updating_suspended = False
        self.reset_points()
        self.generate_points()  # <- generate_points it is before init_colors
        self.init_colors()

Then, the correct way is:

class Myobject(VMobject):
    def __init__(self, offset, **kwargs):
        self.offset= offset
        VMobject.__init__(self, **kwargs)

    def generate_points(self):
        self.term = Square()
        self.term.move_to(RIGHT*self.offset)
        self.add(self.term)

    def init_colors(self):
        self.set_style(fill_color=GREEN, fill_opacity=1)

Please close the issue.

1reaction
Elteoremadebeethovencommented, Jul 9, 2019
class Myobject(VMobject):
    def __init__(self, offset, **kwargs):
        self.offset= offset
        VMobject.__init__(self, **kwargs)
        self.generate_points() #<- You forgot to add this line

    def generate_points(self):
        self.term = Square()
        self.term.set_style(fill_color=GREEN, fill_opacity=1)
        self.term.move_to(RIGHT*self.offset)
        self.add(self.term)

However, a more correct way to do this is:

class Myobject(Square):
    CONFIG={
        "fill_color":GREEN,
        "fill_opacity":1
    }
    def __init__(self, offset, **kwargs):
        Square.__init__(self, **kwargs)
        self.move_to(RIGHT*offset)


class MyExample(Scene):
    def construct(self):
        d1 = Square()
        d1.set_style(fill_color=GREEN, fill_opacity=1)
        self.add(d1)

        d2= Myobject(offset=3)
        self.add(d2)
Read more comments on GitHub >

github_iconTop Results From Across the Web

VMobject - Manim Community v0.17.1 - Docs
Creates a smooth curve from given points and add it to the VMobject. If two points are passed in, the first is interpreted...
Read more >
How to use the manimlib.mobject.types.vectorized_mobject ...
To help you get started, we've selected a few manimlib.mobject.types.vectorized_mobject.VMobject examples, based on popular ways it is used in public ...
Read more >
Animate homemade mobject : r/manim - Reddit
I've created a custom mobject by inheriting from VMobject and pretty much everything is working as expected. I'm able to move it around...
Read more >
2. VM Objects - FreeBSD Documentation
This model creates a number of potential problems. The first is that you can wind up with a relatively deep stack of layered...
Read more >
vmobject | Flutter Package - Pub.dev
A widget auto rebuild every time the observable changed ... In somewhere, you can use ViewModel.of to get created objects already.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found