Python Dash STL Rendering with VTK

291 Views Asked by At

I am looking to create a virtual representation of a robot arm that I have modeled which would be rendered within a webpage. I was able to follow along with creating a simple webpage using the python Dash module and got some buttons and know to show values that could be used to move around the arm eventually. Now, I am working on the rendering .stl files I created using dash-vtk. However, I am unable to get any models to show on the rendered screen. Here is a copy of the code I copied from this website that brings in the .stl files properly:

import vtk
import dash_vtk
import dash
from dash import html
from dash import dcc
from dash.dependencies import Input, Output
from dash_vtk.utils import to_mesh_state

app = dash.Dash(
    __name__,
    # prevent_initial_callbacks=True,
)

server = app.server


app.layout = html.Div([
    dcc.Input(
        id="filename_1_input",
        value='assets/P004_InputArm.STL.stl',
    ),
    dcc.Loading(
        id="reactor_viewer",
        type="default",
    )]
)

@app.callback(
    Output("reactor_viewer", "children"),
    Input("filename_1_input", "value"),
)
def update_reactor(filename_1):

actors = []
for name in [filename_1]:
    reader = vtk.vtkSTLReader()
    reader.SetFileName(name)
    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputConnection(reader.GetOutputPort())

    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    actor.GetProperty().SetColor((1,0,0))

    actors.append(actor)

# how to make use of actors here
mesh_state = to_mesh_state(reader.GetOutput())
vtk_view = dash_vtk.View(
    dash_vtk.GeometryRepresentation(
        dash_vtk.Mesh(state=mesh_state),
    )
)

return html.Div(
    id="dash_vtk_viewer",
    style={"height": "calc(80vh - 16px)", "width": "100%"},
    children=html.Div(
        vtk_view, style={"height": "100%", "width": "100%"}
    )
)

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

I am also unsure as to how I can manipulate the position and rotation of the part (and potentially change its color as well). I have not worked at all with VTK up until this point so I'm a bit list with the terminology being thrown around. I have looked into the PyVista implementation of VTK and was able to get a rendered output with the same file, though not in the webpage. You can find that example here

TLDR; python dash webpage rendering a .stl file, model is not shown.

0

There are 0 best solutions below