I have been trying to visualize in Python an image as a partially translucent texture on a plane in VTK.
My approach is to use vtkThresholdTextureCoords()
to set the transparency based on the image values. However, I am struggling to apply the threshold. The error message states that there is no scalar data to the texture threshold
.
Here is the code snippet:
from vtkmodules.vtkIOImage import vtkImageReader2Factory
from vtkmodules.vtkRenderingCore import (
vtkActor,
vtkPolyDataMapper,
vtkRenderWindow,
vtkRenderWindowInteractor,
vtkRenderer,
vtkTexture
)
from vtkmodules.vtkFiltersSources import vtkPlaneSource
from vtkmodules.vtkFiltersTexture import vtkThresholdTextureCoords
from vtkmodules.vtkCommonColor import vtkNamedColors
import vtk
def main():
#fileName = get_program_parameters()
fileName = 'ice.png'
readerFactory = vtkImageReader2Factory()
textureFile = readerFactory.CreateImageReader2(fileName)
textureFile.SetFileName(fileName)
textureFile.Update()
texture = vtkTexture()
texture.SetInputConnection(textureFile.GetOutputPort())
texture.InterpolateOn()
plane = vtkPlaneSource()
plane.SetCenter(0.0,0.0,0.0)
plane.SetNormal(0.0,1.0,0.0)
texturePlane = vtk.vtkTextureMapToPlane()
texturePlane.SetInputConnection(plane.GetOutputPort())
thresh = vtkThresholdTextureCoords()
thresh.SetInputConnection(texturePlane.GetOutputPort())
thresh.ThresholdByUpper(0.1)
planeMapper = vtkPolyDataMapper()
planeMapper.SetInputConnection(thresh.GetOutputPort())
#planeMapper.SetInputConnection(plane.GetOutputPort()) # This does work
planeMapper.SetScalarRange(textureFile.GetOutput().GetScalarRange())
planeActor = vtkActor()
planeActor.SetMapper(planeMapper)
planeActor.SetTexture(texture)
# Create the RenderWindow, Renderer and Interactor.
renderer = vtkRenderer()
renWin = vtkRenderWindow()
renWin.AddRenderer(renderer)
iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
# Add the actors to the renderer, set the background and size.
renderer.AddActor(planeActor)
colors = vtkNamedColors()
renderer.SetBackground(colors.GetColor3d('DarkSlateGray'))
renWin.SetSize(640, 480)
renWin.SetWindowName('TexturePlane')
# render the image
renWin.Render()
renderer.ResetCamera()
renderer.GetActiveCamera().Elevation(-30)
renderer.GetActiveCamera().Roll(-20)
renderer.ResetCameraClippingRange()
renWin.Render()
iren.Start()
return