Dash VTK⑨(Clickコールバック)

Clickコールバック

Clickコールバックを使うと、3Dビューのクリックした位置の情報を取得することができます。

まずは下記のURLからcow-nonormals.objというファイルをダウンロードし、ソースコードと同じフォルダに格納します。

データセット - https://github.com/plotly/dash-vtk/tree/master/demos/data

次のソースコードでは、ダウンロードした牛のデータを読み込んで3Dビューで表示します。

[ソースコード]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import json

import dash
import dash_vtk
import dash_html_components as html
from dash.dependencies import Input, Output

with open("datasets/cow-nonormals.obj", 'r') as file:
txt_content = file.read()

view = dash_vtk.View(
id="click-info-view",
pickingModes=["click"],
children=[
dash_vtk.GeometryRepresentation(id="cow-geometry", children=[
dash_vtk.Reader(
vtkClass="vtkOBJReader",
parseAsText=txt_content,
),
]),
],
)

# Dash setup
app = dash.Dash(__name__)
server = app.server

app.layout = html.Div([
html.Div(view, style={"width": "100%", "height": "400px"}),
html.B("Output of clickInfo (try clicking on the object above):"),
html.Pre(
id="click-info-output",
style={'overflowX': 'scroll'}
)
])


@app.callback(
Output('click-info-output', 'children'),
Input('click-info-view', 'clickInfo')
)
def display_clicked_content(click_info):
return json.dumps(click_info, indent=2)

if __name__ == "__main__":
app.run_server(debug=True)

コールバック関数(38~43行目)では、クリックした牛の部位の情報をjson形式で表示しています。

[ブラウザで表示]

牛が3Dビューで表示され、特定の部位をクリックするとその位置の情報が表示されます。

またドラッグすることにより角度を変えて表示したり、スクロールで拡大・縮小したりすることも可能です。