Skip to content

VTK helper like ParaView (vtk scene API) #179

Description

@jourdain

Create objects for more easily control visualization (View, Representation, ColorMap)

https://github.com/jourdain/vtk-scene

import random

from vtk_scene import View, GeometryRepresentation

from vtkmodules.vtkFiltersSources import vtkConeSource

from trame_common.obj.app import TrameApp # this will come from trame.app at some point
from trame.decorators import change
from trame.ui.vuetify3 import SinglePageLayout
from trame.widgets import vuetify3 as v3, vtk as vtkw

class App(TrameApp):
    def __init__(self, server=None):
        super().__init__(server)

        # VTK
        self.cone = vtkConeSource()
        self.view = View()
        self.rep = GeometryRepresentation(self.cone, name="cone")
        self.view.reset_camera()

        # GUI
        with SinglePageLayout(self.server, full_height=True) as self.ui:
            with self.ui.toolbar as toolbar:
                toolbar.density = "compact"
                v3.VSpacer()
                v3.VSlider(
                    v_model=("resolution", 6),
                    density="compact",
                    hide_details=True,
                    step=1,
                    min=3,
                    max=60,
                )
                v3.VBtn(
                    icon="mdi-crop-free",
                    click=self.ctrl.view_reset_camera,
                )
                v3.VCheckbox(
                    true_icon="mdi-eye-outline",
                    false_icon="mdi-eye-off-outline",
                    v_model=("show_cone", True),
                    density="compact",
                    hide_details=True,
                    classes="mr-4",
                )

            with self.ui.content:
                with v3.VContainer(fluid=True, classes="h-100 ma-0 pa-0"):
                    with vtkw.VtkRemoteView(self.view.render_window, interactive_ratio=1) as view:
                        self.ctrl.view_reset_camera = view.reset_camera
                        self.ctrl.view_update = view.update

        
    @change("resolution")
    def on_resolution(self, resolution, **_):
        self.cone.resolution = resolution
        if "cone" in self.view.representations:
            self.view.representations["cone"].actor.property.color = (random.random(), random.random(), random.random())
        self.ctrl.view_update()

    @change("show_cone")
    def on_show(self, show_cone, **_):
        if show_cone:
            self.view.representations += self.rep
            self.view.reset_camera()
        else:
            self.view.representations -= self.rep
        self.ctrl.view_update()

def main():
    app = App()
    app.server.start()

if __name__ == "__main__":
    main()

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions