Last Updated: February 25, 2016
·
1.08K
· somada141

VTK renders into IPython Notebook

This function takes a vtkRenderer object and displays the output directly into IPython Notebook

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)

Read article: (http://pyscience.wordpress.com/2014/09/03/ipython-notebook-vtk/)