Skip to content

Commit c75669c

Browse files
committed
update trame picking example
1 parent 02983a3 commit c75669c

File tree

3 files changed

+42
-34
lines changed

3 files changed

+42
-34
lines changed

docs/guide/trame/picking.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ This example capture the usage of a vtkPiker and illustate the usage of method c
77
The code below highlight some important region and logic to understand.
88

99
::: code-group
10-
<<< ../../../examples/trame/picking/pick.py#vtk{py:line-numbers=42} [VTK Setup]
11-
<<< ../../../examples/trame/picking/pick.py#trameWidget{py:line-numbers=82} [Trame widget]
12-
<<< ../../../examples/trame/picking/pick.py#trameChange{py:line-numbers=118} [State change]
13-
<<< ../../../examples/trame/picking/pick.py#py2wasmCall{py:line-numbers=129} [Method call]
10+
<<< ../../../examples/trame/picking/pick.py#vtk{py:line-numbers=43} [VTK Setup]
11+
<<< ../../../examples/trame/picking/pick.py#trameWidget{py:line-numbers=83} [Trame widget]
12+
<<< ../../../examples/trame/picking/pick.py#trameChange{py:line-numbers=121} [State change]
13+
<<< ../../../examples/trame/picking/pick.py#py2wasmCall{py:line-numbers=132} [Method call]
1414
<<< ../../../examples/trame/picking/pick.py{py:line-numbers} [Full code (pick.py)]
1515
<<< ../../../examples/trame/picking/requirements.txt
1616
:::
1717

1818
| ![Window picking](/assets/images/trame/pick3.png) | ![Full car picking](/assets/images/trame/pick2.png) |
19-
| -- | -- |
19+
| -- | -- |
2020

2121
## Hight level explanation
2222

2323
1. Create a vtkPicker and register it so it can be created on the WASM side.
2424
2. Attach a listener on the interactor so you can capture on the server side the picking location (x,y).
2525
3. Call "Pick" on the picker on the WASM side.
26-
4. If something was found, lookup the actor by making another call and convert the result into an actual vtkObject on the server side.
26+
4. If something was found, lookup the actor by making another call and convert the result into an actual vtkObject on the server side.
2727
5. Apply some change on the scene and push the update view to the client.
2828

29-
The [__full working code__](https://github.com/Kitware/vtk-wasm/tree/main/examples/trame/picking) is also available.
29+
The [__full working code__](https://github.com/Kitware/vtk-wasm/tree/main/examples/trame/picking) is also available.

examples/trame/picking/pick.py

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ def _build_ui(self):
9494

9595
# => push picker to client
9696
view.register_vtk_object(self.picker)
97-
view.register_vtk_object(self.last_picked_property) # for pure client edit
97+
view.register_vtk_object(
98+
self.last_picked_property
99+
) # for pure client edit
98100

99101
# => attach interactor listener
100102
wasm_interactor_id = view.get_wasm_id(self.interactor)
@@ -131,35 +133,39 @@ async def _pick_actor(self, x, y):
131133
if self._picking_prending:
132134
return
133135

134-
self._picking_prending = True
135-
# Trigger a pick on client
136-
picked_worked = await self.ctx.wasm_view.invoke(
137-
self.picker, "Pick", (x, y, 0), self.renderer
138-
)
139-
if not picked_worked:
140-
self._picking_prending = False
141-
return
136+
try:
137+
self._picking_prending = True
138+
# Trigger a pick on client
139+
picked_worked = await self.ctx.wasm_view.invoke(
140+
self.picker, "Pick", (x, y, 0), self.renderer
141+
)
142+
if not picked_worked:
143+
return
142144

143-
# Restore previous state
144-
if self.last_picked_actor:
145-
self.last_picked_actor.property.DeepCopy(self.last_picked_property)
146-
self.last_picked_actor = None
145+
# Restore previous state
146+
if self.last_picked_actor:
147+
self.last_picked_actor.property.DeepCopy(self.last_picked_property)
148+
self.last_picked_actor = None
147149

148-
actor_info = await self.ctx.wasm_view.invoke(self.picker, "GetActor")
149-
actor = self.ctx.wasm_view.get_vtk_obj(actor_info.get("Id"))
150-
actor_prop = actor.property
150+
# trame-vtklocal>=0.13.0 auto unwrap vtk object
151+
actor = await self.ctx.wasm_view.invoke(self.picker, "GetActor")
152+
if actor is None:
153+
return
151154

152-
# Save current state and capture picked actor
153-
self.last_picked_property.DeepCopy(actor_prop)
154-
self.last_picked_actor = actor
155+
actor_prop = actor.property
155156

156-
# Highlight actor
157-
actor_prop.color = (1, 0, 1)
158-
actor_prop.EdgeVisibilityOn()
157+
# Save current state and capture picked actor
158+
self.last_picked_property.DeepCopy(actor_prop)
159+
self.last_picked_actor = actor
159160

160-
# Render
161-
self.ctx.wasm_view.update()
162-
self._picking_prending = False
161+
# Highlight actor
162+
actor_prop.color = (1, 0, 1)
163+
actor_prop.EdgeVisibilityOn()
164+
165+
finally:
166+
# Render
167+
self.ctx.wasm_view.update()
168+
self._picking_prending = False
163169

164170
# endregion py2wasmCall
165171

examples/trame/picking/requirements.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
trame>=3.9
44
trame-vuetify
5-
trame-vtklocal>=0.12.3
5+
trame-vtklocal>=0.13
66

7-
vtk==9.5.20250531.dev0
7+
# Either vtk version works
8+
vtk==9.5.20250607.dev0
9+
# vtk==9.5.0rc3

0 commit comments

Comments
 (0)