Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(widget): update box example listener with corners property #13

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions examples/vtk/widgets_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from vtkmodules.vtkInteractionStyle import vtkInteractorStyleSwitch # noqa

from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkCommonTransforms import vtkTransform
from vtkmodules.vtkFiltersSources import vtkConeSource
from vtkmodules.vtkInteractionWidgets import vtkBoxWidget2, vtkBoxRepresentation
from vtkmodules.vtkRenderingCore import (
Expand All @@ -28,7 +29,7 @@ def create_vtk_pipeline():
cone.SetResolution(20)
coneMapper = vtkPolyDataMapper()
coneMapper.SetInputConnection(cone.GetOutputPort())
coneActor = vtkActor()
coneActor = vtkActor(user_transform=vtkTransform())
coneActor.SetMapper(coneMapper)
coneActor.GetProperty().SetColor(colors.GetColor3d("BurlyWood"))

Expand All @@ -40,7 +41,7 @@ def create_vtk_pipeline():
renderer.SetBackground(colors.GetColor3d("Blue"))
renderer.AddActor(coneActor)

renwin = vtkRenderWindow()
renwin = vtkRenderWindow(off_screen_rendering=True)
renwin.AddRenderer(renderer)

# An interactor
Expand Down Expand Up @@ -91,9 +92,11 @@ def _on_widget_update(self, widget_state, **_):
if widget_state is None:
return

print(f"{widget_state=}")
# Get new widget corners from state.
self.widget.representation.corners = widget_state.get("corners")
# Compute user transform for the actor from the new corners.
self.widget.representation.GetTransform(self.actor.user_transform)

self.actor.user_transform.SetMatrix(widget_state.get("transform"))
self.html_view.update_throttle()

def toggle_listeners(self):
Expand All @@ -104,10 +107,10 @@ def toggle_listeners(self):
self.widget_id: {
"InteractionEvent": {
"widget_state": {
"transform": (
"corners": (
self.widget_id,
"WidgetRepresentation",
"Transform",
"Corners",
),
}
}
Expand All @@ -118,10 +121,10 @@ def one_time_update(self):
self.html_view.eval(
{
"widget_state": {
"transform": (
"corners": (
self.widget_id,
"WidgetRepresentation",
"Transform",
"Corners",
),
}
}
Expand All @@ -136,7 +139,7 @@ def _ui(self):
style="position: absolute; left: 1rem; top: 1rem; z-index: 10;",
)
html.Button(
"Update cut",
"Update transformation matrix",
click=self.one_time_update,
style="position: absolute; right: 1rem; top: 1rem; z-index: 10;",
)
Expand Down
Loading