[vtkusers] vtkImageActor erases other actors?
Andreas Pedroni
anpedroni at gmail.com
Sun Nov 18 06:43:23 EST 2018
Dear list
I would like to display a 3D object in front of a vtkImageActor (using Python 3.6 and vtk on Mac OS, see code below). However, the vtkImageActor erases the 3D object. I read in the manual that one should use AddProp() with the renderer to add a vtkImageActor to a render scene. But there is no AddProp() method for the renderer class. I’d be very thankful for any suggestions.
Best
Andreas
import vtk
import cv2
from vtk.util import numpy_support
IMAGEPATH = ‚/models3d/'
VIDEOPATH = "20180816-133303.mp4“
def main():
cap = cv2.VideoCapture(VIDEOPATH)
success, frame = cap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
vtk_array = numpy_support.numpy_to_vtk(frame.reshape((-1, 3), order='F'), deep=False)
# Create a vtkImage Object. This is filled with the vtkArray data later
vtk_image = vtk.vtkImageData()
vtk_image.SetDimensions(720, 1280, 1)
vtk_image.SetSpacing(720, 1280, 1)
vtk_image.SetOrigin(720, 1280, 0)
vtk_image.GetPointData().SetScalars(vtk_array)
# create the Texture for a plane
# The Screen where the movie is mapped onto
plane = vtk.vtkPlaneSource()
plane_mapper = vtk.vtkPolyDataMapper()
plane_mapper.SetInputConnection(plane.GetOutputPort())
plane_actor = vtk.vtkImageActor()
plane_actor.SetInputData(vtk_image)
plane_actor.SetScale(1, 0.5625, 1)
plane_actor.SetOrientation(180, 180, 90)
# add the spine object
obj_importer = vtk.vtkOBJReader()
obj_importer.SetFileName(IMAGEPATH + 'Spine.obj')
obj_importer.Update()
spine_mapper = vtk.vtkPolyDataMapper()
spine_mapper.SetInputConnection(obj_importer.GetOutputPort())
spine_actor = vtk.vtkActor()
spine_actor.SetMapper(spine_mapper)
spine_actor.SetOrientation(90, 10, 90)
spine_actor.SetScale(0.3, 0.3, 0.3)
# set up the renderer and the window
ren = vtk.vtkRenderer()
ren_win = vtk.vtkRenderWindow()
ren_win.AddRenderer(ren)
ren_win.SetSize(1500, 1500)
ren.AddActor(spine_actor)
ren.AddActor(plane_actor)
ren.SetBackground(0, 0, 0.1)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(ren_win)
# Initialize must be called prior to creating timer events.
iren.Initialize()
# start the interaction and timer
iren.Start()
if __name__ == '__main__':
main()
More information about the vtkusers
mailing list