In [1]:
def vtk_show(renderer, w=100, h=100):
    """
    Takes vtkRenderer instance and returns an IPython Image with the rendering.
    """
    renderWindow = vtkRenderWindow()
    renderWindow.SetOffScreenRendering(1)
    renderWindow.AddRenderer(renderer)
    renderWindow.SetSize(w, h)
    renderWindow.Render()
     
    windowToImageFilter = vtkWindowToImageFilter()
    windowToImageFilter.SetInput(renderWindow)
    windowToImageFilter.Update()
     
    writer = vtkPNGWriter()
    writer.SetWriteToMemory(1)
    writer.SetInputConnection(windowToImageFilter.GetOutputPort())
    writer.Write()
    data = str(buffer(writer.GetResult()))
    
    from IPython.display import Image
    return Image(data)
In [2]:
from vtk import (vtkSphereSource, vtkPolyDataMapper, vtkActor, vtkRenderer,
        vtkRenderWindow, vtkWindowToImageFilter, vtkPNGWriter)
 
sphereSource = vtkSphereSource()
sphereSource.SetPhiResolution(200)
sphereSource.SetThetaResolution(200)
mapper = vtkPolyDataMapper()
mapper.SetInputConnection(sphereSource.GetOutputPort())
 
actor = vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetColor(1,1,0)
 
renderer = vtkRenderer()
renderer.AddActor(actor)
renderer.SetBackground(1, 1, 1)

vtk_show(renderer, 200, 200)
Out[2]:
In [3]:
from vtk import vtkStructuredGridReader, vtkPlane, vtkCutter, vtkDataSetMapper, vtkContourFilter
r = vtkStructuredGridReader()
r.SetFileName("/nh/nest/u/ondrej/Downloads/density.vtk")
r.Update()

# Contour

iso = vtkContourFilter()
iso.SetInputConnection(r.GetOutputPort())
iso.SetValue(0, 0.26)

m = vtkPolyDataMapper()
m.SetInputConnection(iso.GetOutputPort())
m.SetScalarRange(r.GetOutput().GetScalarRange())

a1 = vtkActor()
a1.SetMapper(m)

# Plane

plane = vtkPlane()
plane.SetOrigin(r.GetOutput().GetCenter())
plane.SetNormal(-0.2, 0.2, 0.9579)

c = vtkCutter()
c.SetInputConnection(r.GetOutputPort())
c.SetCutFunction(plane)

m = vtkPolyDataMapper()
m.SetInputConnection(c.GetOutputPort())
m.SetScalarRange(r.GetOutput().GetScalarRange()) 

a2 = vtkActor()
a2.SetMapper(m)


# Render


renderer = vtkRenderer()
renderer.AddActor(a1)
renderer.AddActor(a2)
renderer.SetBackground(0.8, 1, 1)

vtk_show(renderer, 500, 500)
Out[3]:
In [4]:
import vtk
 
#Create a cube
cube=vtk.vtkCubeSource()
cube.SetXLength(40)
cube.SetYLength(30)
cube.SetZLength(20)
cubeMapper=vtk.vtkPolyDataMapper()
cubeMapper.SetInputConnection(cube.GetOutputPort())
 
#create a plane to cut,here it cuts in the XZ direction (xz normal=(1,0,0);XY =(0,0,1),YZ =(0,1,0)
plane=vtk.vtkPlane()
plane.SetOrigin(10,0,0)
plane.SetNormal(1,1,0)
 
#create cutter
cutter=vtk.vtkCutter()
cutter.SetCutFunction(plane)
cutter.SetInputConnection(cube.GetOutputPort())
cutter.Update()
cutterMapper=vtk.vtkPolyDataMapper()
cutterMapper.SetInputConnection( cutter.GetOutputPort())
 
#create plane actor
planeActor=vtk.vtkActor()
planeActor.GetProperty().SetColor(1.0,1,0)
planeActor.GetProperty().SetLineWidth(2)
planeActor.SetMapper(cutterMapper)
 
#create cube actor
cubeActor=vtk.vtkActor()
cubeActor.GetProperty().SetColor(0.5,1,0.5)
cubeActor.GetProperty().SetOpacity(0.5)
cubeActor.SetMapper(cubeMapper)
 
#create renderers and add actors of plane and cube
ren = vtk.vtkRenderer()
ren.AddActor(planeActor)
ren.AddActor(cubeActor)
ren.SetBackground(0,0,0)

vtk_show(ren, 200, 200)
Out[4]:
In [30]:
from vtk import (vtkStructuredGridReader, vtkPlane, vtkCutter, vtkDataSetMapper, vtkContourFilter,
        vtkCamera)

def get_plane_actor(r, n1, n2, n3):
    plane = vtkPlane()
    plane.SetOrigin(r.GetOutput().GetCenter())
    plane.SetNormal(n1, n2, n3)
    
    c = vtkCutter()
    c.SetInputConnection(r.GetOutputPort())
    c.SetCutFunction(plane)
    
    m = vtkPolyDataMapper()
    m.SetInputConnection(c.GetOutputPort())
    m.SetScalarRange(r.GetOutput().GetScalarRange()) 
    
    a = vtkActor()
    a.SetMapper(m)
    return a


r = vtkStructuredGridReader()
r.SetFileName("/nh/nest/u/ondrej/Downloads/density.vtk")
r.Update()

a1 = get_plane_actor(r, 0, 0, 1)
a2 = get_plane_actor(r, 0, 1, 0)
a3 = get_plane_actor(r, 1, 0, 0)

camera =vtkCamera()
camera.SetPosition(100, 100, 100)
camera.SetFocalPoint(0, 0, 0)

renderer = vtkRenderer()
renderer.SetActiveCamera(camera)
renderer.AddActor(a1)
renderer.AddActor(a2)
renderer.AddActor(a3)
renderer.SetBackground(0.8, 1, 1)

vtk_show(renderer, 500, 500)
Out[30]:
In [32]:
camera.SetPosition(0, 0, 100)
vtk_show(renderer, 500, 500)
Out[32]:
In [34]:
camera.SetPosition(0, 50, 100)
vtk_show(renderer, 500, 500)
Out[34]:
In [ ]: