How to Edit line width/thickness from line generated from vtk polydata?

Operating system: Windows 10
Slicer version: 5.0.3

I’m trying to code a python extension that plots a line using points read from a csv file. After creating the script to add the required values to a vtkPoints and vtkPolyLine objects, I was able to render the line using the following:

# Create a polydata to store everything in
polyData = vtk.vtkPolyData()

# Add the points to the dataset
polyData.SetPoints(points)

# Add the lines to the dataset
polyData.SetLines(cells)

slicer.modules.models.logic().AddModel(polyData)

Output:
image

I now want to edit the line thickness.

When I use vtk outside of slicer, I would just use the following:

# Setup actor and mapper
mapper = vtkPolyDataMapper()
mapper.SetInputData(polyData)

actor = vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetLineWidth(5)

# Setup render window, renderer, and interactor
renderer = vtkRenderer()
renderWindow = vtkRenderWindow()
renderWindow.SetWindowName('PolyLine')
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
renderer.AddActor(actor)
renderWindow.Render()
renderWindowInteractor.Start()

However, I’m not sure how to use actor and renderer with Slicer. Is there a way of using actors in a Slicer scene?

I know the Models module’s UI has a line width slider, so I’m probably just missing something. I’ve tried looking in the logic file for the Models module, but haven’t found anything I can use. Any help at all would be appreciated.

Slicer uses. concept called a “displayable manager”. You can search the code and this forum for MRMLDM and find discussions. This is the part of the code that manages mapping from the MRML scene to the mappers/actors at the vtk rendering system level.

1 Like

Thanks I’ll give that a try.

I found the solution. I used the following code to get the actor being used by the 3D renderer and then modify its line width property.

# Get renderer actor
actor = slicer.app.layoutManager().activeThreeDRenderer().GetActors().GetLastActor()

# Adjust line width property
actorProperty = actor.GetProperty()
actorProperty.SetLineWidth(3)