5

I am allowed to define the backgroundColor of a renderer by calling

renderer = vtk.vtkRenderer()
renderer.SetBackground(0,255,0)

[![enter image description here][1]][1]

My question:

Is it possible to set the opacity of the background ?

You can see my problem (with multiple viewports) in my example image. I could get rid of this "cutting" by simply using transparent backgrounds...

thanks in advance!

user2463728
  • 309
  • 3
  • 12

3 Answers3

5

I finally found a solution :

you can set a layer for each renderer. Default is Layer 0 (not transparent). Everything greater than layer 0 will be transparent. But keep in mind to set at least a empty renderer (no volume or object) with a background-color on layer 0 to avoid ugly reflections.

renderer.SetBackground(255,255,255)
renderer.SetLayer(0)

# transparency layer
renderer.SetLayer(1)
renderer.addVolume(....)

Result:

enter image description here

user2463728
  • 309
  • 3
  • 12
4

For those who want to use transparency in a screenshot.

render_window = vtk.vtkRenderWindow()
render_window.AddRenderer(renderer)
render_window.SetAlphaBitPlanes(1)  # Enable usage of alpha channel
...
w2if = vtk.vtkWindowToImageFilter()
w2if.SetInput(render_win)
w2if.SetInputBufferTypeToRGBA()  # Also record the alpha (transparency) channel
...

More details in this official vtk example.


Edit: In addition, if the above is not working (e.g. the background is transparent but the actors are white/not rendered properly), you can try to enable depth peeling.

Before the above code snippet you should add

# the renderer, as in the question
renderer = vtk.vtkRenderer()
renderer.AddActor(actor)
renderer.SetBackground(0,255,0)
renderer.SetBackgroundAlpha(0.2)  # add background opacity value
renderer.SetUseDepthPeeling(1)  # only works with current GPUs
renderer.SetOcclusionRatio(0.2)  # 0. is better, but much slower

You might also need to increase the maximum number of peels (default is 4). Check this VTK Wiki page for more details.

Breno
  • 748
  • 8
  • 18
Nil
  • 2,345
  • 1
  • 26
  • 33
  • Neither this not the official vtk example work for me. – user3622450 Feb 22 '19 at 11:58
  • For some reason this makes my `vtkPNGWriter` object write everything almost transparently. Quite odd. Changing back to `w2if.SetInputBufferTypeToRGB()` corrects the transparency of the polygonal data, but makes the background opaque again. – Breno Aug 05 '21 at 23:18
0

for set background on vtkFullScreenRenderWindow function, you can use the File to find your ideal color.