In [1]:
from tvtk.api import tvtk

def vtk_show(renderer, w=100, h=100):
    """
    Takes vtkRenderer instance and returns an IPython Image with the rendering.
    """
    renderWindow = tvtk.RenderWindow(off_screen_rendering=1, size=(w, h))
    renderWindow.add_renderer(renderer)
    renderWindow.render()
     
    windowToImageFilter = tvtk.WindowToImageFilter(input=renderWindow)
    windowToImageFilter.update()
     
    writer = tvtk.PNGWriter(write_to_memory=1, input_connection=windowToImageFilter.output_port)
    writer.write()
    data = writer.result.to_array().tostring()
    
    from IPython.display import Image
    return Image(data)

def get_plane(r, n1, n2, n3):
    plane = tvtk.Plane(origin=r.output.center, normal=(n1, n2, n3))
    c = tvtk.Cutter(input_connection=r.output_port, cut_function=plane)
    m = tvtk.PolyDataMapper(input_connection=c.output_port, scalar_range=r.output.scalar_range)
    a = tvtk.Actor(mapper=m)
    return a

def get_outline(r):
    s = tvtk.OutlineSource(bounds=r.output.bounds)
    m = tvtk.DataSetMapper(input_connection=s.output_port)
    a = tvtk.Actor(mapper=m)
    a.property.color = (0, 0, 0)
    return a

def get_renderer(l):
    renderer = tvtk.Renderer(background=(1, 1, 1))
    for a in l:
        renderer.add_actor(a)
    return renderer

In [2]:
r = tvtk.RectilinearGridReader()
r.file_name = "data/Venq.vtk"
r.update()

renderer = get_renderer([
    get_plane(r, 1, 0, 0),
    get_plane(r, 0, 1, 0),
    get_plane(r, 0, 0, 1),
    get_outline(r),
    #get_contour(r),
    ])
    
camera = renderer.active_camera
renderer.reset_camera()
camera.elevation(30)
camera.azimuth(30)

vtk_show(renderer, 500, 500)


---------------------------------------------------------------------------
TraitError                                Traceback (most recent call last)
/home/ondrej/repos/python-hpcmp2/opt/profile/jxbs/lib/python2.7/site-packages/tvtk/messenger.pyc in send(obj, event, *args, **kw_args)
    306 
    307 def send(obj, event, *args, **kw_args):
--> 308     _messenger.send(obj, event, *args, **kw_args)
    309 send.__doc__ = _messenger.send.__doc__
    310 

/home/ondrej/repos/python-hpcmp2/opt/profile/jxbs/lib/python2.7/site-packages/tvtk/messenger.pyc in send(self, source, event, *args, **kw_args)
    247                         inst = obj()
    248                         if inst:
--> 249                             getattr(inst, meth)(source, event, *args, **kw_args)
    250                         else:
    251                             # Oops, dead reference.

/home/ondrej/repos/python-hpcmp2/opt/profile/jxbs/lib/python2.7/site-packages/tvtk/tvtk_base.pyc in update_traits(self, obj, event)
    454                     setattr(self, name, warn)
    455                 else:
--> 456                     setattr(self, name, val)
    457         # Reset the warning state.
    458         vtk.vtkObject.SetGlobalWarningDisplay(warn)

/home/ondrej/repos/python-hpcmp2/opt/profile/jxbs/lib/python2.7/site-packages/traits/trait_handlers.pyc in error(self, object, name, value)
    167         """
    168         raise TraitError( object, name, self.full_info( object, name, value ),
--> 169                           value )
    170 
    171     def arg_error ( self, method, arg_num, object, name, value ):

TraitError: The 'alpha_bit_planes' trait of a RenderWindow instance must be 'f' or 'false' or 'n' or 'no' or 'off' or 'on' or 't' or 'true' or 'y' or 'yes' or 0 or 1, but a value of 8 <type 'int'> was specified.
Out[2]:

The exception is most probably a bug in tvtk and I reported it here: https://github.com/enthought/mayavi/issues/77


In [ ]: