Python - Dynamically change the opacity values of vtkPieceWiseFunction()

Hi everyone, this is my first post here. I used vtkplotter to show a 3d model starting from dicom scans with the following code:

import vtk
from vtkplotter import *
import numpy as np
from vtkmodules.vtkCommonDataModel import vtkPiecewiseFunction

from vtkmodules.vtkRenderingCore import vtkRenderer, vtkVolume, vtkVolumeProperty, vtkColorTransferFunction


volume = load('Letter:/path')

volumeColor = vtkColorTransferFunction()
r, g, b = getColor('white')
volumeColor.AddRGBPoint(-1024, r, g, b)
r, g, b = getColor('yellow')
volumeColor.AddRGBPoint(-1023, r, g, b)
r, g, b = getColor('blue')
volumeColor.AddRGBPoint(-1022, r, g, b)


volumeScalarOpacity = vtkPiecewiseFunction()
volumeScalarOpacity.AddPoint(-1024, 0.00)
volumeScalarOpacity.AddPoint(-1023, 0.5)
volumeScalarOpacity.AddPoint(-1022, 0.5)


volumeProperty = vtkVolumeProperty()
volumeProperty.SetColor(volumeColor)

volumeProperty.SetScalarOpacity(volumeScalarOpacity)
volumeProperty.SetInterpolationTypeToLinear()
volumeProperty.ShadeOn()
volumeProperty.SetAmbient(0.4)
volumeProperty.SetDiffuse(0.6)
volumeProperty.SetSpecular(0.2)


volume.SetProperty(volumeProperty)

show(volume)

How can I change the opacity values ​​dynamically with a slider?

Thanks in advance for your answers

You can try something like this:

from vedo import load, datadir
from vedo.applications import RayCaster
embryo = load(datadir+"embryo.tif") # vtkVolume
embryo.mode(1).c('jet').alpha([0,0.1,0.9]) # change properties
# volumeProperty = embryo.GetProperty()
plt = RayCaster(embryo) # Plotter instance
plt.show(viewup="z", bg='black', bg2='blackboard', axes=7)

or customize it the way you like using this. If you encounter any problems please submit an issue here.

I’ll try, thank you very much!