# How to show vtkUnstructuredGrid in pvpython of paraview?

**URL:** https://discourse.paraview.org/t/how-to-show-vtkunstructuredgrid-in-pvpython-of-paraview/1261
**Category:** ParaView Support
**Tags:** python
**Created:** [February 9, 2019, 4:53am UTC](https://discourse.paraview.org/t/how-to-show-vtkunstructuredgrid-in-pvpython-of-paraview/1261 "2019-02-09T04:53:07Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![weihuayi](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/w/3ec8ea/32.png) [@weihuayi](https://discourse.paraview.org/u/weihuayi)
#### Post date: [February 9, 2019, 4:53am UTC](https://discourse.paraview.org/t/how-to-show-vtkunstructuredgrid-in-pvpython-of-paraview/1261/1 "2019-02-09T04:53:07Z")

</div>

I install paraview 5.6 on my Ubuntu 18.04 system and I want to write a python script to show a 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([[1, 2, 0], [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()

```

I can not find any example from the internet to do such thing in python script. So I need your help, thanks very much.

---

<div class="post-metadata">

### Author: ![weihuayi](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/w/3ec8ea/32.png) [@weihuayi](https://discourse.paraview.org/u/weihuayi)
#### Post date: [February 9, 2019, 7:39am UTC](https://discourse.paraview.org/t/how-to-show-vtkunstructuredgrid-in-pvpython-of-paraview/1261/2 "2019-02-09T07:39:11Z")

</div>

The following code is my first try to solve this problem.

> 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(label=“Second order 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!")
> opt = vtk.vtkUnstructuredGrid.GetData(outInfo)
> opt.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’)  
> dispaly = Show(source, view)  
> Interact()

But I got some error :

> Traceback (most recent call last):  
> File “test\_triangle.py”, line 55, in   
> 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.

So I must miss something. Thanks your help!

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.paraview.org/user_avatar/discourse.paraview.org/cory.quammen/32/11193_2.png) [@cory.quammen](https://discourse.paraview.org/u/cory.quammen)
#### Post date: [February 11, 2019, 3:45pm UTC](https://discourse.paraview.org/t/how-to-show-vtkunstructuredgrid-in-pvpython-of-paraview/1261/3 "2019-02-11T15:45:45Z")

</div>

Hi, please see my response on Stackoverflow: [https://stackoverflow.com/questions/54603267/how-to-show-vtkunstructuredgrid-in-python-script-based-on-paraview/54633793#54633793](https://stackoverflow.com/questions/54603267/how-to-show-vtkunstructuredgrid-in-python-script-based-on-paraview/54633793#54633793)
