-
|
Hello! I'm trying to understand how to reset/replace an existing view using just VTK in an asynchronous task. Basically an automatic import asyncio
import os
from trame.app import get_server, asynchronous
from trame.ui.vuetify import VAppLayout
from trame.widgets import vtk, vuetify
from vtkmodules.vtkRenderingCore import (
vtkActor,
vtkPolyDataMapper,
vtkRenderer,
vtkRenderWindow,
vtkRenderWindowInteractor,
)
from vtkmodules.vtkFiltersSources import vtkConeSource, vtkSphereSource
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkInteractionStyle import vtkInteractorStyleTrackballCamera
# Required for interactor initialization
from vtkmodules.vtkInteractionStyle import vtkInteractorStyleSwitch # noqa
# Required for rendering initialization, not necessary for
# local rendering, but doesn't hurt to include it
import vtkmodules.vtkRenderingOpenGL2 # noqa
# -----------------------------------------------------------------------------
# VTK pipeline
# -----------------------------------------------------------------------------
colors = vtkNamedColors()
def cone():
cone = vtkConeSource()
cone.SetResolution(60)
# Create a mapper and actor
mapper = vtkPolyDataMapper()
mapper.SetInputConnection(cone.GetOutputPort())
actor = vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetDiffuseColor(colors.GetColor3d('bisque'))
# Visualize
renderer = vtkRenderer()
renderWindow = vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtkRenderWindowInteractor()
interactor_style = vtkInteractorStyleTrackballCamera()
renderWindowInteractor.SetInteractorStyle(interactor_style)
renderWindowInteractor.SetRenderWindow(renderWindow)
renderer.AddActor(actor)
renderer.SetBackground(colors.GetColor3d('Salmon'))
renderer.ResetCamera()
renderWindow.SetSize(640, 480)
renderWindow.SetWindowName('Cone')
return renderWindow
def sphere():
sphere = vtkSphereSource()
# Create a mapper and actor
mapper = vtkPolyDataMapper()
mapper.SetInputConnection(sphere.GetOutputPort())
actor = vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetDiffuseColor(colors.GetColor3d('bisque'))
# Visualize
renderer = vtkRenderer()
renderWindow = vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtkRenderWindowInteractor()
interactor_style = vtkInteractorStyleTrackballCamera()
renderWindowInteractor.SetInteractorStyle(interactor_style)
renderWindowInteractor.SetRenderWindow(renderWindow)
renderer.AddActor(actor)
renderer.SetBackground(colors.GetColor3d('Salmon'))
renderer.ResetCamera()
renderWindow.SetSize(640, 480)
renderWindow.SetWindowName('Sphere')
return renderWindow
cone_window = cone()
sphere_window = sphere()
# -----------------------------------------------------------------------------
# Trame setup
# -----------------------------------------------------------------------------
server = get_server()
state, ctrl = server.state, server.controller
# Initial VTK window width and height values
state.vtk_window_width = 300
state.vtk_window_height = 300
# -----------------------------------------------------------------------------
# Background thread
# -----------------------------------------------------------------------------
@asynchronous.task
async def refresh_function(**kwargs):
counter = 1
while True:
with state:
if counter % 2 == 0:
ren_win = sphere_window
else:
ren_win = cone_window
view.replace_view(ren_win)
ctrl.view_update()
counter += 1
await asyncio.sleep(5)
# -----------------------------------------------------------------------------
# GUI
# -----------------------------------------------------------------------------
with VAppLayout(server) as layout:
with layout.root:
with vuetify.VContainer(fluid=True, classes="pa-0 fill-height"):
view = vtk.VtkLocalView(cone_window)
ctrl.view_update = view.update
ctrl.on_server_ready.add(refresh_function)
# -----------------------------------------------------------------------------
# Main
# -----------------------------------------------------------------------------
if __name__ == "__main__":
server.start() |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
|
I haven't tried yet, but controller callback can not be async function. Try to remove the You may need to run |
Beta Was this translation helpful? Give feedback.
-
|
I've tried the code with the latest trame but it seems that the problem still exists. |
Beta Was this translation helpful? Give feedback.
I haven't tried yet, but controller callback can not be async function.
Try to remove the
@asyncronous.taskdecorator and callctrl.on_server_ready.add_task(refresh_function)instead.You may need to run
pip install -U trame-serveras I've addedadd_taskonly a couple days ago.