Using manim to make a graph like network with slightly moving nodes

Viewed 1246

enter image description here

trying to recreate that by using valuetrackers

Here is my code and the err is also inside that pastebin Thank you for all the help since I cannot yet create an animation I thought about using this to make by linking the dots and lines both to some lists of valuetrackers and then finally storing the updation of these value trackers in an animation list and finally playing them was my goal

class Network3(Scene):
    def construct(self):
        screen_grid = ScreenGrid()
        self.add(screen_grid)
        #function to just random the x and y coordinates
        def randomize_xy():
            for i in range(0,no_of_dots):
                x_coord[i]=random.randint(-7,+7)
                y_coord[i]=random.randint(-4,+4)

            print(x_coord)
            print(y_coord)
        no_of_dots=20

        #make some dots
        dots=[]
        for i in range(0,no_of_dots):
            dots.append(Dot())

        #make initial list of coordinates 
        x_coord=[]
        y_coord=[]
        for i in range(0,no_of_dots):
            x_coord.append(random.randint(-7,7))
            y_coord.append(random.randint(-4,4))      

        # #make sure all the lengths are ok
        # print(f"length of dots lenght={len(dots)} of x_coord={len(x_coord)}") error of int base 10 is coming from here
        print("length of dots=",end="")
        print(str(len(dots))) #print(len(dots)) still causes an error !!!

        #add all dots at one point
        self.add(*dots)

        #make anim list to move everything to their position from origin
        animlist=[]
        for i in range(0,no_of_dots):
            animlist.extend([dots[i].move_to,[x_coord[i],y_coord[i],0]])

        #play
        self.play(*animlist)

        #make a list of value trackers
        x_trackers=[]
        y_trackers=[]

        for i in range(0,no_of_dots):
            x_tracker=ValueTracker(x_coord[i])
            y_tracker=ValueTracker(y_coord[i])

            x_trackers.append(x_tracker)
            y_trackers.append(y_tracker)

        #making lines
        lines=[]
        for i in range(0,no_of_dots-1):
            t1=[x_coord[i],y_coord[i],0]
            t2=[x_coord[i+1],y_coord[i+1],0]
            line=Line(t1,t2)
            lines.append(line)

        def update_func1(obj,i):
            temp1=np.array([x_trackers[i],y_trackers[i],0])
            temp2=np.array([x_trackers[i+1],y_trackers[i+1],0])
            line=Line(start=temp1,end=temp2)
            obj.become(line)

        #making a link between each line and value tracker 
        for i in range(0,no_of_dots-1):
            lines[i].add_updater(lambda m:update_func1(m,i))

        #adding again
        self.add(*lines)

        #making a link between each dot and value tracker 
        for i in range(0,no_of_dots):
            dots[i].add_updater(lambda m:m.move_to([x_trackers[i],y_trackers[i],0]))

        #now somehow have to change all the value tracker at once
        animlist2=[]
        for i in range(0,no_of_dots):
            animlist2.extend([x_trackers[i].set_value,random.randint(-7,+7)])
            animlist2.extend([y_trackers[i].set_value,random.randint(-4,+4)])

        #now play 
        self.play(*animlist2)

        #end
        self.wait(3)

Current error I am getting is:

Traceback (most recent call last):
  File "C:\manim\manimlib\extract_scene.py", line 155, in main
    scene = SceneClass(**scene_kwargs)
  File "C:\manim\manimlib\scene\scene.py", line 53, in __init__
    self.construct()
  File "network_graphic.py", line 584, in construct
    lines[i].add_updater(lambda m:update_func1(m,i))
  File "C:\manim\manimlib\mobject\mobject.py", line 192, in add_updater
    self.update(0)
  File "C:\manim\manimlib\mobject\mobject.py", line 159, in update
    updater(self)
  File "network_graphic.py", line 584, in <lambda>
    lines[i].add_updater(lambda m:update_func1(m,i))
  File "network_graphic.py", line 578, in update_func1
    line=Line(start=temp1,end=temp2)
  File "C:\manim\manimlib\mobject\geometry.py", line 431, in __init__
    self.set_start_and_end_attrs(start, end)
  File "C:\manim\manimlib\mobject\geometry.py", line 471, in set_start_and_end_attrs
    vect = normalize(rough_end - rough_start)
TypeError: unsupported operand type(s) for -: 'ValueTracker' and 'ValueTracker'
1 Answers

I think your best option is to use the functions with "dt", then watch my video if you have any doubts. This is a basic example Grant made in one of his very old videos, but I think you can use it for your idea.

class RandomMove(Scene):
    CONFIG = {
        "amplitude": 0.4,
        "jiggles_per_second": 1,
    }
    def construct(self):
        points = VGroup(*[
            Dot(radius=0.2) for _ in range(9)
        ])
        points.arrange_in_grid(buff=1)
        for submob in points:
            submob.jiggling_direction = rotate_vector(
                RIGHT, np.random.random() * TAU *1.5,
            )
            submob.jiggling_phase = np.random.random() * TAU *1.5

        def update_mob(mob, dt):
            for submob in mob:
                submob.jiggling_phase += dt * self.jiggles_per_second * TAU
                submob.shift(
                    self.amplitude *
                    submob.jiggling_direction *
                    np.sin(submob.jiggling_phase) * dt
                )

        points.add_updater(update_mob)
        self.add(points)
        self.wait(10)

Result here

Related