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

问题描述 投票:1回答:1

我我的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.

我一定会错过一些东西。

python paraview pvpython
1个回答
1
投票

了解事情的关键是有在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)
© www.soinside.com 2019 - 2024. All rights reserved.