如何在基于paraview的python脚本中显示vtkUnstructuredGrid?

How to show vtkUnstructuredGrid in python script based on paraview?

我在 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 脚本中执行此类操作的示例。所以我需要你的帮助,非常感谢。

更新:

我尝试对源代码 class 进行编码,如下所示:

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.

我一定错过了什么。

要理解的关键是 ParaView 中有两个级别的 Python 脚本可用。较低级别是您可以使用 VTK 创建或过滤数据的地方。更高级别让您控制 ParaView 的操作,例如,显示数据、设置显示属性等。您缺少的是脚本中两个级别之间的桥梁。

在您的原始示例中,您在 VTK 中创建了一个非结构化网格,效果很好。要将它放到 ParaView 可以使用它的地方,请添加以下内容:

# how to put uGrid into the following codes
view = GetActiveViewOrCreate('RenderView')

# create a trivial producer to bridge between the VTK object and ParaView
tp = TrivialProducer()
tp.GetClientSideObject().SetOutput(uGrid)

dispaly = Show(tp)

这将创建一个 ParaView 代理 (TrivialProducer),它是一个名为 vtkTrivialProducer 的简单 VTK 数据源的代理。它所做的只是获取一个数据集并将其传递给任何请求它的下游过滤器。 (注意:由于 GetClientSideObject(),这仅在您 运行 处于内置服务器模式时才有效,这很常见)。

您定义单元格的方式也有问题。单元格定义中的第一个条目需要是定义单元格的点数。所以将该行更改为

cell = np.array([[3, 1, 2, 0], [3, 3, 0, 2]], dtype=np.int)