Search code examples
pythonfor-loopmanim

Grouping objects after loop in Manim


Let's suppose I need to draw several objects evenly spaced along a line. I use a for-cycle for this.

    for number in range(-90, 90, 10):
        object = object.copy()
        object.move_to(np.array([number, 0, 0]))

I want group them after the cycle using VGroup.

        objects = VGroup(object)
        self.play(ShowCreation(objects))

Unsurprisingly, my code drawing only the last of the objects. How to access the indices of all objects to draw them correctly? I am new to Python and struggling with indexing.


Solution

  • Here is my code, if someone is interested. Basically, it draws a grid-like image of Earth given a condition from xls-file.

    from openpyxl import load_workbook
    
            workbook = load_workbook('name.xlsx')
            worksheet = workbook.get_sheet_by_name('sheetname')
            earth = []
            for row_cells in worksheet.iter_rows():
                for cell in row_cells:
                     if cell.value == 1:
                         lat = cell.row - 73
                         lon = cell.column - 109
                         rect = Rectangle(height=1, width=1, stroke_width=7, color=BACKGROUND)
                         rect.set_fill(GREEN_B, opacity=1)
                         rect.move_to(np.array([lon, lat, 0]))
                         earth.append(rect)
                     else:
                         pass
    
            group_earth = VGroup(*earth)
            self.play(FadeIn(group_earth), run_time=3)
            self.wait(2)