How to change STL colors using vtk library

1.1k Views Asked by At

I am trying to manipulate an STL file so they appear as different colours on the screen.

Here is the code I have now, I am looping through multiple STL files. I think the only issue is my syntax, as I can not find a command that sets the data to the colour I want.

filenames = ['C1.stl','C2.stl']
print(filenames)

actors = []
for name in filenames:
    reader = vtk.vtkSTLReader()
    reader.SetFileName(name)
    mapper = vtk.vtkPolyDataMapper()
    if vtk.VTK_MAJOR_VERSION <= 5:
        mapper.SetInput(reader.GetOutput())
    else:
        mapper.SetInputConnection(reader.GetOutputPort())

    if name is 'C1.stl':
        mapper.getPointData().SetColor(1.0,0,1.0)

Any help would be greatly appreciated! Thank you in advance.

2

There are 2 best solutions below

0
On

I figured out the answer:

import vtk

filenames = ['C1.stl','C2.stl']
print(filenames)

actors = []
for name in filenames:
    reader = vtk.vtkSTLReader()
    reader.SetFileName(name)

    mapper = vtk.vtkPolyDataMapper()
    if vtk.VTK_MAJOR_VERSION <= 5:
        mapper.SetInput(reader.GetOutput())
    else:
        mapper.SetInputConnection(reader.GetOutputPort())


    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    actor.SetPosition([0.0, 0.0, 0.0])
    actor.SetScale([1.0, 1.0, 1.0])

#Changes the colour to purple for the first stl file 
    if name is 'C1.stl':
        actor.GetProperty().SetColor(1.0,0,1.0)

    actors.append(actor)

I needed to insert the code as part of my actor

0
On

With vedo:

from vedo import load, show
filenames = ['C1.stl','C2.stl']
acts = load(filenames) # list of Mesh(vtkActor)
acts[0].color([1.0,0,1.0]).scale(1).pos(1,2,3)
show(acts)