Vtk Interactive#

import panel as pn
import panel_material_ui as pmui
import pyvista as pv

from pyvista import examples

pn.extension('vtk', sizing_mode='stretch_width')

For this example we use the pyvista library to load a dataset and generate easily a VTK scene

m = examples.download_st_helens().warp_by_scalar()

# default camera position
cpos = [
    (567000.9232163235, 5119147.423216323, 6460.423216322832),
    (562835.0, 5114981.5, 2294.5),
    (-0.4082482904638299, -0.40824829046381844, 0.8164965809277649)
]

# pyvista plotter
pl = pv.Plotter(notebook=True);
actor = pl.add_mesh(m, smooth_shading=True, lighting=True)
pl.camera_position = cpos #set camera position

# save initial camera properties
renderer = list(pl.ren_win.GetRenderers())[0]
initial_camera = renderer.GetActiveCamera()
initial_camera_pos = {
    "focalPoint": initial_camera.GetFocalPoint(),
    "position": initial_camera.GetPosition(),
    "viewUp": initial_camera.GetViewUp()
}

# Panel creation using the VTK Scene created by the plotter pyvista
orientation_widget = True
enable_keybindings = True
vtkpan = pn.pane.VTK(
    pl.ren_win, margin=0, sizing_mode='stretch_both', orientation_widget=orientation_widget,
    enable_keybindings=enable_keybindings, min_height=600
)
vtkpan

Colorbars and actor selection#

# Creation of a mapping between Custom name and the VTK object reference
actor_ref = ["None", actor.__this__]
actor_names = ["None", 'St Helen']
actor_opts = {k:v for k,v in zip(actor_names, actor_ref)}

options = {}
actor_selection = pmui.Select(
    value=None, options=actor_opts, margin=10, label="Actor Selection"
)
actor_selection

VTK Scene Parameters (Background, Lights,…)#

# Scene Layout
color = ''.join(['#'] + ['{:02x}'.format(int(v*255)) for v in pl.background_color[:3]])
bind_and_orient = pmui.CheckBoxGroup(
    value=['Orientation Widget', 'Key Bindings'], options=['Orientation Widget', 'Key Bindings']
)
reset_camera = pmui.Button(label='Reset Camera')
background_color = pmui.ColorPicker(value=color, label='Background Color')
scene_props = pn.Column(bind_and_orient, reset_camera, background_color, sizing_mode='stretch_width')

# Light properties
light_box_title = pn.widgets.StaticText(value='Light properties')
light_type = pmui.Select(value='HeadLight', options=['HeadLight', 'SceneLight', 'CameraLight'])
light_intensity = pmui.FloatSlider(start=0, end=1, value=1, label="Intensity")
light_props = pn.Column(light_box_title, light_type, light_intensity, sizing_mode='stretch_width')

pn.Column(scene_props, light_props, max_width=320)

Actor Properties#

#layout actor props
opacity = pmui.FloatSlider(value=1, start=0, end=1, label='Opacity', disabled=True)
lighting = pmui.Toggle(value=True, label='Lighting', disabled=True)
interpolation = pmui.Select(value='Phong', options=['Flat','Phong'], label='Interpolation', disabled=True)
edges = pmui.Toggle(value=False, label='Show Edges', disabled=True)
edges_color = pmui.ColorPicker(value='#ffffff', label='Edges Color', disabled=True)
representation = pmui.Select(value='Surface', options=['Points','Wireframe','Surface'], label='Representation', disabled=True)
frontface_culling = pmui.Toggle(value=False, label='Frontface Culling', disabled=True)
backface_culling = pmui.Toggle(value=False, label='Backface Culling', disabled=True)
ambient = pmui.FloatSlider(value=0, start=-1, end=1, label='Ambient', disabled=True)
diffuse = pmui.FloatSlider(value=1, start=0, end=2, label='Diffuse', disabled=True)
specular = pmui.FloatSlider(value=0, start=0, end=10, label='Specular', disabled=True)
specular_power = pmui.FloatSlider(value=100, start=0, end=100, label='Specular Power', disabled=True)

actor_props = pn.Column(
    opacity, lighting, interpolation, edges, edges_color,
    representation, frontface_culling, backface_culling,
    ambient, diffuse, specular, specular_power
)

actor_props