TestPythonViewMatplotlibScript.py 1.8 KB
Newer Older
1
2
# Set up a basic scene for rendering.
from paraview.simple import *
3
import os
4
5
6
import sys

script = """
7
from paraview.vtk.util import numpy_support
8
9
10
11
import matplotlib.style
import matplotlib as mpl

mpl.style.use('classic')
12
13
14
15
16
17
18
19
20

# Utility to get next color
def getNextColor():
  colors = 'bgrcmykw'
  for c in colors:
    yield c

# This function must be defined. It is where specific data arrays are requested.
def setup_data(view):
21
  print ("Setting up data")
22
23

# This function must be defined. It is where the actual rendering commands for matplotlib go.
24
def render(view,width,height):
25
26
  from paraview import python_view
  figure = python_view.matplotlib_figure(width,height)
27

28
29
  ax = figure.add_subplot(111)
  numObjects = view.GetNumberOfVisibleDataObjects()
30
  print ("num visible objects: ", numObjects)
31
  for i, color in zip(range(0,numObjects), getNextColor()):
32
33
34
35
36
    dataObject = view.GetVisibleDataObjectForRendering(i)
    if dataObject:
      vtk_points = dataObject.GetPoints()
      if vtk_points:
        vtk_points_data = vtk_points.GetData()
37
        pts = numpy_support.vtk_to_numpy(vtk_points_data)
38
39
40
        x, y = pts[:,0], pts[:,1]
        ax.scatter(x, y, color=color)

41
  return python_view.figure_to_image(figure)
42
43
44
45
46
47
48
49
50
51
52
53
54
"""

view = CreateView("PythonView")
view.Script = script

cone = Cone()
Show(cone, view)

sphere = Sphere()
Show(sphere, view)

Render()

55
56
57
58
try:
  baselineIndex = sys.argv.index('-B')+1
  baselinePath = sys.argv[baselineIndex]
except:
59
  print ("Could not get baseline directory. Test failed.")
60
61
62
63
64
65

baseline_file = os.path.join(baselinePath, "TestPythonViewMatplotlibScript.png")
import vtk.test.Testing
vtk.test.Testing.VTK_TEMP_DIR = vtk.util.misc.vtkGetTempDir()
vtk.test.Testing.compareImage(view.GetRenderWindow(), baseline_file, threshold=25)
vtk.test.Testing.interact()
66
67
68
69
70
71

Delete(cone)
del cone

Delete(sphere)
del sphere