The VTK
pane renders a VTK scene inside a panel, making it possible to interact with complex geometries in 3D. It allows keeping state synchronized between the vtkRenderWindow
defined on the python side and the one displayed in the pane through vtk-js. Here Python acts as a server, sending information about the scene to the client. The synchronization is done in only one direction: Python => JavaScript. Modifications done on the JavaScript side are not reflected back to the Python vtkRenderWindow
.
Parameters:
For details on other options for customizing the component see the layout and styling how-to guides.
axes
(dict): A dictionary with the parameters of the axes to construct in the 3d view. Must contain at leastxticker
,yticker
andzticker
.[x|y|z]ticker
is a dictionary which contains:ticks
(array of numbers) - required. Positions in the scene coordinates of the corresponding axe tickslabels
(array of strings) - optional. Label displayed respectively to theticks
positions.
If
labels
are not defined they are inferred from theticks
array.digits
: number of decimal digits whenticks
are converted tolabels
.fontsize
: size in pts of the ticks labels.show_grid
: boolean. If true (default) the axes grid is visible.grid_opacity
: float between 0-1. Defines the grid opacity.axes_opacity
: float between 0-1. Defines the axes lines opacity.
camera
(dict): A dictionary reflecting the current state of the VTK cameraenable_keybindings
(bool): A boolean to activate/deactivate keybindings. Bound keys are:s: set representation of all actors to surface
w: set representation of all actors to wireframe
v: set representation of all actors to vertex
r: center the actors and move the camera so that all actors are visible
The mouse must be over the pane to work
Warning: These keybindings may not work as expected in a notebook context, if they interact with already bound keysorientation_widget
(bool): A boolean to activate/deactivate the orientation widget in the 3D pane.ìnteractive_orientation_widget
(bool): If True the orientation widget is clickable and allows to rotate the scene in one of the orthographic projections.
Warning: if set to True, synchronization capabilities ofVTKRenderWindowSynchronized
panes could not work.object
(object): Must be avtkRenderWindow
instance.
Properties:
actors
: return the list of the vtkActors in the scenevtk_camera
: return the vtkCamera of the renderer held by the panel
Methods:
Several helpers to work with the VTK scene are exposed:
set_background(r: float, g: float, b: float)
: Set the color of the scene background as an RGB color, wherer
,g
, andb
are floats in the range (0, 1)synchronize()
: Synchronize the Python-sidevtkRenderWindow
object state with JavaScriptunlink_camera()
: Create a new vtkCamera object, allowing the panel to have its own camera.link_camera(other: VTK)
: Set both panels to share the same camera; any change in either panel's camera will be applied to the other
Warning after using any of these methods to modify the state of vtkRenderWindow, the synchronize()
method must be called for those changes to affect the display
Rendering VTK objects
Compared to working with VTK
directly, there are some differences when using it in Panel. As rendering of the objects and interactions with the view are handled by the VTK panel, we do not need to make a call to the Render
method of the vtkRenderWindow
(which would pop up the classical VTK window), nor do we need to specify a vtkRenderWindowInteractor
.
We can also add additional actors to the plot, then call the synchronize
method to update the panel:
Working with PyVista
Most of these examples use the PyVista library as a convenient interface to VTK.
Though these examples can generally be rewritten to depend only on VTK itself, pyvista
supports a Pythonic and concise syntax for the main features needed to work with VTK objects.
For instance, the VTK example above can be rewritten using PyVista as follows:
Link PyVista and Panel:
Create and add some objects to the PyVista plotter, displaying them using the VTK panel
Export the scene
The scene can be exported and the file generated can be loaded by the official vtk-js scene importer:
Advanced usage and interactivity
This examples will show :
where information about
scalars
used to color 3D objects can be found in the sceneutilization of the
orientation widget
how to add
axes
in the sceneadding a
player
widget to create an animation
Scene creation
First we create a scene with a nice spline inspired by this examples : Creating a Spline
By clicking on the three dots, a colorbar can be displayed
The orientation widget on the bottom right of the panel is clickable too, allowing you to orient the camera in a specific direction.
Adding axes to the VTK scene
Using the axes
parameter, it is possible to display axes in the 3D view.
The Axes
object (like orientation widget
and colorbar
) is a client-side object instantiated only on the JavaScript side, and is not synchronized back to the Python vtkRenderWindow
.
Working with Volumes
All previous panes use geometric examples, but VTK
can work with both geometric and volume objects:
As for other examples, we can add easily new objects in the pane.
For example, we can create 3 orthogonal slices and add them to the scene:
Create an animation
We will reuse the panel used for the spline.
For this animation, we will rotate the actor and roll the scalars on the spline tube: