我在Ubuntu 18.04上安装了Paraview 5.6,并且我希望编写一个Python脚本来显示vtkUnstructuredGrid。
import numpy as np
from paraview.simple import *
import paraview.vtk as vtk
from paraview.vtk.numpy_interface import dataset_adapter as dsa
import paraview.vtk.util.numpy_support as vnp
node = np.array(
[[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 0.0],
[0.0, 1.0, 0.0]], dtype=np.float)
cell = np.array([[3, 1, 2, 0], [3, 3, 0, 2]], dtype=np.int)
NC = cell.shape[0]
points = vtk.vtkPoints()
points.SetData(vnp.numpy_to_vtk(node))
cells = vtk.vtkCellArray()
cells.SetCells(NC, vnp.numpy_to_vtkIdTypeArray(cell))
uGrid =vtk.vtkUnstructuredGrid()
uGrid.SetPoints(points)
uGrid.SetCells(vtk.VTK_TRIANGLE, cells)
# how to put uGrid into the following codes
view = GetActiveViewOrCreate('RenderView')
dispaly = Show()
render = Render()
Interact()
我找不到在Python脚本中实现此功能的任何示例。所以,我需要您的帮助,非常感谢。
更新:
我尝试编写以下源代码类:
import numpy as np
from paraview.simple import *
import vtk
import vtk.util.numpy_support as vnp
from vtkmodules.util.vtkAlgorithm import VTKPythonAlgorithmBase
from vtkmodules.numpy_interface import dataset_adapter as dsa
from paraview.util.vtkAlgorithm import smproxy, smproperty, smdomain
@smproxy.source(name="MeshSource", label="triangle mesh!")
class MeshSource(VTKPythonAlgorithmBase):
def __init__(self):
print("Initialize the source!")
VTKPythonAlgorithmBase.__init__(self,
nInputPorts=0,
nOutputPorts=1,
outputType='vtkUnstructuredGrid')
node = np.array(
[[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 0.0],
[0.0, 1.0, 0.0]], dtype=np.float)
cell = np.array([[3, 1, 2, 0], [3, 3, 0, 2]], dtype=np.int)
NN = node.shape[0]
NC = cell.shape[0]
points = vtk.vtkPoints()
points.SetData(vnp.numpy_to_vtk(node))
cells = vtk.vtkCellArray()
cells.SetCells(NC, vnp.numpy_to_vtkIdTypeArray(cell))
self.mesh = vtk.vtkUnstructuredGrid()
self.mesh.SetPoints(points)
self.mesh.SetCells(vtk.VTK_TRIANGLE, cells)
rho = vnp.numpy_to_vtk(np.zeros(NN))
rho.SetName('rho_A')
self.mesh.GetPointData().AddArray(rho)
self.Port = 0
def RequestData(self, request, inInfo, outInfo):
print("Request the data!")
output = vtk.vtkUnstructuredGrid.GetData(outInfo)
optput.ShallowCopy(self.mesh)
return 1
def UpdatePointData(self, rho):
print("Update the point data!")
rho = vnp.numpy_to_vtk(rho)
rho.SetName('rho_A')
self.mesh.GetPointData().AddArray(rho)
self.Modified()
source = MeshSource()
view = GetActiveViewOrCreate('RenderView')
display = Show(source, view)
Interact()
但是我遇到了一些错误:
Traceback (most recent call last):
File "test_triangle.py", line 55, in <module>
dispaly = Show(source, view)
File "/home/why/local/lib/python3.6/site-packages/paraview/simple.py", line 482, in Show
rep = controller.Show(proxy, proxy.Port, view)
File "/home/why/local/lib/python3.6/site-packages/paraview/servermanager.py", line 158, in __ConvertArgumentsAndCall
retVal = func(*newArgs)
TypeError: Show argument 1: method requires a vtkSMSourceProxy, a vtkPythonAlgorithm was provided.
我一定错过了什么。
VTKPythonAlgorithmBase
(你已经接近了,只需要将类与脚本分开,保存在单独的文件中,然后作为插件加载)。这两种方法都记录在ParaView指南的第12章中(https://www.paraview.org/paraview-guide/)。 - Cory QuammenGetClientSideObject()
返回客户端上的 VTK 数据对象。当您调用Connect
时,您正在连接到另一个服务器进程,因此GetClientSideObject()
将不会返回任何有用的信息。您可以使用paraview.servermanager.Fetch(GetActiveSource())
将管道浏览器中当前选定对象的 VTK 对象传输到客户端。返回的对象是完整的 VTK 数据对象。 - Cory Quammen