How to add a border to 3d widget

I want to add a border to 3d widget, but run the code below, nothing happened

slicer.app.layoutManager().threeDWidget(0).threeDView().setStyleSheet("border-bottom: 2px solid rgb(255, 0, 0)")

where am i wrong

@lassoan save me plz :rofl:

The VTK render window is painted by VTK, so style sheet will have no effect. You can draw the frame by using VTK by adding a 2D actor:

view = slicer.app.layoutManager().threeDWidget(0).threeDView()
color = [0.0, 1.0, 1.0]
lineThickness = 10.0

viewRenderer = view.renderWindow().GetRenderers().GetItemAsObject(0)

borderPoints = vtk.vtkPoints()
borderPoints.InsertNextPoint(  1e-4,   1e-4, 0)
borderPoints.InsertNextPoint(0.9999,   1e-4, 0)
borderPoints.InsertNextPoint(0.9999, 0.9999, 0)
borderPoints.InsertNextPoint(  1e-4, 0.9999, 0)

borderCells = vtk.vtkCellArray()
borderCells.InsertNextCell(5)
for i in range(5):
    borderCells.InsertCellPoint(i%5)

borderPolyData = vtk.vtkPolyData()
borderPolyData.SetPoints(borderPoints)
borderPolyData.SetLines(borderCells)

borderCoordinate = vtk.vtkCoordinate()
borderCoordinate.SetCoordinateSystemToNormalizedViewport()
borderCoordinate.SetViewport(viewRenderer)

borderPolyDataMapper = vtk.vtkPolyDataMapper2D()
borderPolyDataMapper.SetInputData(borderPolyData)
borderPolyDataMapper.SetTransformCoordinate(borderCoordinate)
borderPolyDataMapper.SetTransformCoordinateUseDouble(True)

highlightedBorderActor = vtk.vtkActor2D()
highlightedBorderActor.SetMapper(borderPolyDataMapper)
highlightedBorderActor.GetProperty().SetColor(color[0], color[1], color[2])
highlightedBorderActor.GetProperty().SetDisplayLocationToForeground()
highlightedBorderActor.GetProperty().SetLineWidth(lineThickness)
viewRenderer.AddActor2D(highlightedBorderActor)
view.renderWindow().Render()

1 Like

thanks @lassoan it works!

What about to disable it?
I´ve tried to repeat the process with lineThickness=0 but it doesn´t work for me. Is there any way to get this vtkActor2D to remove it?

Thanks on advance!

I answer to myself FYI:

for actor2D in viewRenderer.GetActors2D():
        viewRenderer.RemoveActor2D(actor2D)
1 Like