[SciPy-User] VTK examples

alexander baker baker.alexander@gmail....
Tue Feb 16 07:25:21 CST 2010


Works fine for me! (XP)

from vtk import *
   2
   3 import matplotlib
   4 matplotlib.use('Agg')
   5 from matplotlib.figure import Figure
   6 from matplotlib.backends.backend_agg import FigureCanvasAgg
   7 import pylab as p
   8
   9 # The vtkImageImporter will treat a python string as a void pointer
  10 importer = vtkImageImport()
  11 importer.SetDataScalarTypeToUnsignedChar()
  12 importer.SetNumberOfScalarComponents(4)
  13
  14 # It's upside-down when loaded, so add a flip filter
  15 imflip = vtkImageFlip()
  16 imflip.SetInput(importer.GetOutput())
  17 imflip.SetFilteredAxis(1)
  18
  19 # Map the plot as a texture on a cube
  20 cube = vtkCubeSource()
  21
  22 cubeMapper = vtkPolyDataMapper()
  23 cubeMapper.SetInput(cube.GetOutput())
  24
  25 cubeActor = vtkActor()
  26 cubeActor.SetMapper(cubeMapper)
  27
  28 # Create a texture based off of the image
  29 cubeTexture = vtkTexture()
  30 cubeTexture.InterpolateOn()
  31 cubeTexture.SetInput(imflip.GetOutput())
  32 cubeActor.SetTexture(cubeTexture)
  33
  34 ren = vtkRenderer()
  35 ren.AddActor(cubeActor)
  36
  37 renWin = vtkRenderWindow()
  38 renWin.AddRenderer(ren)
  39
  40 iren = vtkRenderWindowInteractor()
  41 iren.SetRenderWindow(renWin)
  42
  43 # Now create our plot
  44 fig = Figure()
  45 canvas = FigureCanvasAgg(fig)
  46 ax = fig.add_subplot(111)
  47 ax.grid(True)
  48 ax.set_xlabel('Hello from VTK!', size=16)
  49 ax.bar(xrange(10), p.rand(10))
  50
  51 # Powers of 2 image to be clean
  52 w,h = 1024, 1024
  53 dpi = canvas.figure.get_dpi()
  54 fig.set_figsize_inches(w / dpi, h / dpi)
  55 canvas.draw() # force a draw
  56
  57 # This is where we tell the image importer about the mpl image
  58 extent = (0, w - 1, 0, h - 1, 0, 0)
  59 importer.SetWholeExtent(extent)
  60 importer.SetDataExtent(extent)
  61 importer.SetImportVoidPointer(canvas.buffer_rgba(0,0), 1)
  62 importer.Update()
  63
  64 iren.Initialize()
  65 iren.Start()


Mobile: 07788 872118
Blog: www.alexfb.com

--
All science is either physics or stamp collecting.


On 16 February 2010 13:10, URI <zallen@urologicresearchinstitute.org> wrote:

> Can anyone get the example on this page to work?
>
> http://www.scipy.org/Cookbook/Matplotlib/VTK_Integration
>
> When I run it (in Windows 7) a window opens up but then I get a "Python has
> stopped working" dialoge.
>
> It seems to have a problem with the second to last line:
> iren.Initialize()
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20100216/89a6e171/attachment-0001.html 


More information about the SciPy-User mailing list