Reputation: 43
Good morning, I would like to use pvpython to make a slice of a 3D volume inside a VTK file and then I'd want to save this image (possibly with a symmetric color palette centered in white).
To be specific, I'd want to make with python the following image taken with Paraview:
I'm new to pvpython or python, so I report my first script, but it's not working:
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from scipy.interpolate import griddata
from paraview import simple as pvs
import numpy as np
import vtk
import matplotlib
from vtk.util.numpy_support import vtk_to_numpy
reader = vtk.vtkDataSetReader()
reader.SetFileName("../deltau-deltau.vtk")
reader.ReadAllScalarsOn()
reader.Update()
plane = vtk.vtkPlane()
plane.SetOrigin(16, 0, 0)
plane.SetNormal(1, 0, 0)
cutter = vtk.vtkCutter()
cutter.SetCutFunction(plane)
cutter.SetInputConnection(reader.GetOutputPort())
cutter.Update()
data = cutter.GetOutput()
# Coordinates of nodes in the mesh
nodes_vtk_array= reader.GetOutput().GetPoints().GetData()
#Get the coordinates of the nodes and their variables
nodes_nummpy_array = vtk_to_numpy(nodes_vtk_array)
x=nodes_nummpy_array[:,0]
y=nodes_nummpy_array[:,1]
z=nodes_nummpy_array[:,2]
variable_numpy_array = vtk_to_numpy(data)
var = variable_numpy_array
# Figure bounds
npts = 100
xmin, xmax = min(x), max(x)
ymin, ymax = min(y), max(y)
zmin, zmax = min(z), max(z)
# Grid
xi = np.linspace(xmin, xmax, npts)
yi = np.linspace(ymin, ymax, npts)
zi = np.linspace(zmin, zmax, npts)
# The mesh is unstructured, so
var_plot = griddata((z, y), var, (zi[None,:], yi[:,None]), method='cubic')
plt.plot(zi, yi, var_plot)
#plt.colorbar()
plt.savefig("figure.png")
Upvotes: 3
Views: 1297
Reputation: 2498
First, note that there is a difference between ParaView scripting and VTK scripting. The script you provide seems to be pure VTK scripting. ParaView scripting is a bit more high level and more python-firendly than VTK.
If you can achieve your processing in ParaView, I advise you to use only ParaView scripting tools and not VTK scripting (i.e. do not import vtk
, do not create any vtkXXX()
object)
In ParaView, you can trace your actions to produce a python script, as explain here. This is useful to understand the python counterpart for a specific action. If you want to script a full analysis, you can also save a python state.
This documentation also provide examples of processing and analysing from the GUI and in python.
If you still prefer VTK scripting, please reword your post and explain what "not working" mean.
Upvotes: 2