Trame performance with many actors or a large structured grid

Hello, based on the cone example from the tutorial, the following code works well for the values of the size of the grid of cones, and of the structured grid, but if the number of actors (one per cone) is multiplied by 27 (3 in each dimension) the browser don t display it; also if the value for the grid_size of the structured grid can be 10 or 8, but other values make the python server halt. Any idea how we can enlarge the grids? (complete code below)

from trame.widgets import vtk, vuetify
from trame.app import get_server
from trame.ui.vuetify import SinglePageLayout
from vtkmodules.vtkFiltersSources import vtkConeSource
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkCommonCore import vtkPoints
from vtkmodules.vtkCommonDataModel import vtkStructuredGrid
from vtkmodules.vtkRenderingCore import (
    vtkActor,
    vtkDataSetMapper,
    vtkPolyDataMapper,
    vtkRenderer,
    vtkRenderWindow,
    vtkRenderWindowInteractor,
)
from vtkmodules.vtkInteractionStyle import vtkInteractorStyleSwitch #noqa
import vtkmodules.vtkRenderingOpenGL2 #noqa
#import vtk
# -----------------------------------------------------------------------------
# VTK pipeline
# -----------------------------------------------------------------------------

renderer = vtkRenderer()
renderWindow = vtkRenderWindow()
renderWindow.AddRenderer(renderer)

renderWindowInteractor = vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
renderWindowInteractor.GetInteractorStyle().SetCurrentStyleToTrackballCamera()

colors = vtkNamedColors()

mini_grid_size = 6 
grid_size = 10  # You can change this to enlarge the grid # 8 and 10 works

# Create a single cone source (tetrahedron)
cone_source = vtkConeSource()
cone_source.SetResolution(3)
cone_source.SetHeight(0.471)
mapper = vtkPolyDataMapper()
mapper.SetInputConnection(cone_source.GetOutputPort())

# Create the 10x10x10 grid of tetrahedrons
for x in range(mini_grid_size):
    for y in range(mini_grid_size):
        for z in range(mini_grid_size):
            actor = vtkActor()
            actor.SetMapper(mapper)
            if (x + y + z) % 2 == 0:
                actor.GetProperty().SetColor(colors.GetColor3d("Tomato"))
            else:
                actor.GetProperty().SetColor(colors.GetColor3d("Peacock"))
            
            actor.SetPosition(x * 2, y * 2, z * 2)  # Adjust the multiplier for spacing
            renderer.AddActor(actor)

# Create a structured grid
structuredGrid = vtkStructuredGrid()
structuredGrid.SetDimensions(grid_size, grid_size, grid_size)

# Create points for the grid
points = vtkPoints()

# Populate the grid with points
for x in range(grid_size):
    for y in range(grid_size):
        for z in range(grid_size):
            points.InsertNextPoint(x, y, z)

structuredGrid.SetPoints(points)

# Blank cells based on the rule (x + y + z) % 5 == 0
for x in range(grid_size - 1):
    for y in range(grid_size - 1):
        for z in range(grid_size - 1):
            if (x + y + z) % 5 != 0:
                structuredGrid.BlankCell(x * grid_size * grid_size + y * grid_size + z)

# Create a mapper and actor
mapper2 = vtkDataSetMapper()
mapper2.SetInputData(structuredGrid)

actor = vtkActor()
actor.SetMapper(mapper2)
renderer.AddActor(actor)

renderer.ResetCamera()

#renderWindow.Render()
#renderWindowInteractor.Start()
# -----------------------------------------------------------------------------
# Get a server to work with
# -----------------------------------------------------------------------------

server = get_server()
server.client_type = "vue2"

# -----------------------------------------------------------------------------
# GUI
# -----------------------------------------------------------------------------

with SinglePageLayout(server) as layout:
    layout.title.set_text("Hello cones")
    
    with layout.content:
        with vuetify.VContainer(
            fluid=True,
            classes="pa-0 fill-height",
        ):
            view = vtk.VtkLocalView(renderWindow)
# -----------------------------------------------------------------------------
# Main
# -----------------------------------------------------------------------------

if __name__ == "__main__":
    server.start()

Any reason for not using the glyph mapper?
Also, I’m guessing the VtkLocalView is on purpose?

1 Like

Thank you so much, Sebastien. Yes using vtkGlyph3DMapper as in https://examples.vtk.org/site/Python/Snippets/PointToGlyph/ and VtkRemoteView let me have a grid of 100 x 100 x 100 cones with a smooth zooming and rotation feature in the web page. I think this is performant enough for me to dive into more development! Later I wish to move into VR too.
I was thinking that the Local rendering would be faster and use the GPU, but the Remote rendering solved the issue.

1 Like