I’m working on a project with python and VTK. I successfully managed to combine three STL files, with a python STL libraray. After that, I visualize this combined.stl file in VTK like this:
stl_reader = vtk.vtkSTLReader()
stl_reader.SetFileName(PATH)
stl_mapper = vtk.vtkPolyDataMapper()
stl_mapper.SetInputConnection(stl_reader.GetOutputPort())
stl_actor = vtk.vtkActor()
stl_actor.SetMapper(stl_mapper)
add actor on renderer, renderer on window etc.
So far so good. But now I want to colorize/highlight one of those STL-objects. (All STLs together form a bigger object, so the positioning is important and has to stay the same. So the whole thing needs to stay one vtkActor at the end.)
I think it’s not possible to do the colorization in the combined STL file, because the combined file doesn’t keep track of the original objects (maybe there is also a solution here, please correct me if I’m wrong). So I guess I need a different solution.
I already tried it with this CompositePolyDataMapper example (https://lorensen.github.io/VTKExamples/site/Python/CompositeData/CompositePolyDataMapper/), but I don’t know how I can get from vtkSTLReader to an object I can put into the vtkMultiBlockDataSet.
Does someone know a solution for my problem?
Thank you in advance.
Advertisement
Answer
Ok, I could solve the problem by myself. If somebody has a similar problem the solution was pretty easy:
assembly = vtk.vtkAssembly()
filenames = ["example1.stl","example2.stl","example3.stl"]
for filename in filenames:
stl_reader = vtk.vtkSTLReader()
stl_reader.SetFileName(filename)
stl_mapper = vtk.vtkPolyDataMapper()
stl_mapper.SetInputConnection(stl_reader.GetOutputPort())
stl_actor = vtk.vtkActor()
if filename == "example1.stl":
stl_actor.GetProperty().SetColor(0.86,0.08,0.24)
else:
stl_actor.GetProperty().SetOpacity(0.4)
stl_actor.SetMapper(stl_mapper)
assembly.AddPart(stl_actor)
scene_renderer.AddActor(assembly)
The vtkAssembly class lets you add actors to it and if you interact with it, it works like one actor.