Aliaksei Badnarchuk
Aliaksei Badnarchuk

Reputation: 1

"TypeError: object of type 'float' has no len()" in manim OpenGL when using LaggedStart

I have a project that I want to make a simulation for with manim. The simulation must take a complex number I set as self.c and apply the function self.f in a loop, drawing each step as a Dot3D, going up in the 3rd dimension.

I am using OpenGL to:

  1. Speed it up
  2. Better support 3d
  3. Use interactivity

The problem is coming up when using the LaggedStart method. I have a similar version of the project in 2D, without OpenGL. There everything works fine.

This is my code:

from manim import *
from manim.opengl import *

class Orbits3d(Scene):
    def construct(self):
        # Creating axes and surface picture
        self.iterations = 25
        self.ax = ThreeDAxes(
            x_range=[-3, 3, 0.25],
            y_range=[-2.5, 2.5, 0.25],
            z_range=[0, 25, ((25/self.iterations) if self.iterations < 100 else 25)],

            x_length=6*2,
            y_length=5*2,

            tips=False,
            x_axis_config={"include_numbers": True, "font_size": 14},
        )

        mandImg = OpenGLImageMobject("imgMand.png")
        mandImg.height = self.ax.c2p(0, 4, 0)[1] - self.ax.c2p(0, 0, 0)[1]
        self.add(mandImg)

        self.play(Write(self.ax))

        # Adjusting camera
        self.play(
            self.camera.animate.set_euler_angles(
                phi=45 * DEGREES,
                theta=30 * DEGREES
            )
        )

        self.c = 0

        self.memo = {}
        
        self.dots = OpenGLVGroup()
        
        for i in range(self.iterations + 1): # Creating dots that are moved in the simulation
            [val, clr] = self.f(i)
            dot = Dot3D(
                point=self.ax.coords_to_point(val.real, val.imag, (25/self.iterations)*i),
                radius=0.05,
                color=clr
            )

            self.dots.add(dot)

        self.add(self.dots)

        self.wait()

        # Test animation

        self.c = -0.25 + 0.25j

        self.anim()

        # Interactivity
        
        self.interactive_embed()

    def f(self, t): # Defining the iterative function
        if (t == 0):
            return [0, WHITE]
        
        if (t == 1):
            return [self.c, BLUE]
        
        if ((self.c, t) in self.memo):
            return self.memo[(self.c, t)]
        
        a = self.f(t-1)[0]
        r = (a * a) + self.c
        if (abs(r.real) > 2.5 or abs(r.imag) > 2.5):
            self.memo[(self.c, t)] = [2.5*((r.real)/abs(r.real))+2.5j*((r.imag)/abs(r.imag)), RED]
            return [2.5, RED]
        self.memo[(self.c, t)] = [r, WHITE]
        return [r, WHITE]
    
    def anim(self): # Animating the dot transformation
        animations = []
        for j in range(self.iterations+1):
            [val, clr] = self.f(j)

            animations.append(
                Transform(
                    self.dots[j],
                    Dot3D(
                        point=self.ax.coords_to_point(val.real, val.imag, (25/self.iterations)*j),
                        radius=0.05,
                        color=clr
                    ),
                    run_time=0.5,
                    rate_func=linear
                )
            )
            print(str(j)+'\n')
        
        self.play(LaggedStart(*animations, lag_ratio=0))

The (important(?) parts of the) traceback:

│ D:\Python\manim\openglmand.py:54 in construct                                                    
│   51 │   │
│   52 │   │   self.c = -0.25 + 0.25j
│   53 │   │
│ ❱ 54 │   │   self.anim()
│   55 │   │
│   56 │   │   self.interactive_embed()

│ D:\Python\manim\openglmand.py:95 in anim
│   92 │   │   │   )
│   93 │   │   │   print(str(j)+'\n')
│   94 │   │
│ ❱ 95 │   │   self.play(LaggedStart(*animations, lag_ratio=0))
│   96

│ C:\Users\alexe\anaconda3\envs\manim\lib\site-packages\manim\mobject\opengl\opengl_mobject.py:2484 in align_data
│   2481 │   │   │   │   │   continue
│   2482 │   │   │   │   arr1 = mob1.data[key]
│   2483 │   │   │   │   arr2 = mob2.data[key]
│ ❱ 2484 │   │   │   │   if len(arr2) > len(arr1):
│   2485 │   │   │   │   │   mob1.data[key] = resize_preserving_order(arr1, len(arr2))
│   2486 │   │   │   │   elif len(arr1) > len(arr2):
│   2487 │   │   │   │   │   mob2.data[key] = resize_preserving_order(arr2, len(arr1))

TypeError: object of type 'float' has no len()

The first line numbers may slightly differ in the code because of added comments

I had a similar error when importing the image, the mistake was not using the OpenGLImageMobject but the usual ImageMobject. As far as I see, all objects are using the OpenGL prefix.

Upvotes: -1

Views: 32

Answers (0)

Related Questions