title: How to use numpy in the Programmable Filter in ParaView
body: <p>Assume, I have a ProgrammableFilter in paraview, which gets two inputs: mesh1 with data and mesh2 without. 
  Furthermore, I know the permutation of the points from mesh1 to mesh2. 
  Inside the filter, I can access the point values through</p>
  
  <pre><code>data0=inputs[0].GetPointData().GetArray('data')`
  </code></pre>
  
  <p>and obtain a part of the array using</p>
  
  <pre><code>subData=data0[0:6]
  </code></pre>
  
  <p>for example. But how could I add this <code>subData</code> to the output without a python loop?</p>
  
  <p>To experiment with the code, I created a (not so small) working example:</p>
  
  <pre><code>#!/usr/bin/python
  from paraview.simple import *
  import numpy as np
  import vtk
  from vtk.util.numpy_support import numpy_to_vtk
  
  #generate an arbitrary source with data
  mesh2=Sphere()
  mesh2.Center=[0.0, 0.0, 0.0]
  mesh2.EndPhi=360
  mesh2.EndTheta=360
  mesh2.PhiResolution=100
  mesh2.Radius=1.0
  mesh2.StartPhi=0.0
  mesh2.StartTheta=0.0
  mesh2.ThetaResolution=100
  mesh2.UpdatePipeline()
  
  #add the data
  mesh2Vtk=servermanager.Fetch(mesh2)
  nPointsSphere=mesh2Vtk.GetNumberOfPoints()
  mesh2Data=paraview.vtk.vtkFloatArray()
  mesh2Data.SetNumberOfValues(nPointsSphere)
  mesh2Data.SetName("mesh2Data")
  #TODO: use numpy here?? do this with a ProgrammableFilter ?
  data=np.random.rand(nPointsSphere,1)
  for k in range(nPointsSphere):
    mesh2Data.SetValue(k, data[k])
  mesh2Vtk.GetPointData().AddArray(mesh2Data)
  
  #send back to paraview server
  #from https://public.kitware.com/pipermail/paraview/2011-February/020120.html
  t=TrivialProducer()
  filter= t.GetClientSideObject()
  filter.SetOutput(mesh2Vtk)
  t.UpdatePipeline()
  w=CreateWriter('Sphere_withData.vtp')
  w.UpdatePipeline()
  Delete(w)
  
  #create mesh1 without data
  mesh1=Line()
  mesh1.Point1=[0,0,0]
  mesh1.Point2=[0,0,1]
  mesh1.Resolution=5
  mesh1.UpdatePipeline()
  
  progFilter=ProgrammableFilter(mesh1)
  progFilter.Input=[mesh1, t]
  progFilter.Script="curT=inputs[1].GetPointData().GetArray('mesh2Data')"\
    "\nglobIndices=range(0,6)"\
    "\nsubT=curT[globIndices]"\
    "\nswap=vtk.vtkFloatArray()"\
    "\nswap.SetNumberOfValues(len(globIndices))"\
    "\nswap.SetName('T')"\
    "\n#TODO: how can i avoid this loop, i.e. write output.GetPointData().AddArray(converToVTK(subT))"\
    "\nfor k in range(len(globIndices)):"\
    "\n  swap.SetValue(k,subT[k])"\
    "\noutput.PointData.AddArray(swap)"
  progFilter.UpdatePipeline()
  w=CreateWriter('Line_withData.vtp')
  w.UpdatePipeline()
  Delete(w)
  </code></pre>
  
  <p>I accepted the answer, because it looks right. The following two scripts even show the problem:
  base script 'run.py':</p>
  
  <pre><code>src1='file1.vtu'
  r1=XMLUnstructuredGridReader(FileName=src1)
  
  progFilter=ProgrammableFilter(r1)
  progFilter.Input=[r1]
  with open('script.py','r') as myFile:
    progFilter.Script=myFile.read()
  progFilter.UpdatePipeline()
  progData=progFilter.GetPointDataInformation()
  print progData.GetArray('T2').GetRange()
  </code></pre>
  
  <p>and the script for the programmable filter:</p>
  
  <pre><code>import vtk
  import vtk.numpy_interface.dataset_adapter as dsa
  import numpy as np
  globIndices=inputs[0].GetPointData().GetArray('T')
  subT=np.ones((globIndices.shape[0],1))
  subTVtk=dsa.VTKArray(subT)
  output.PointData.append(subTVtk, 'T2')
  </code></pre>
  
  <p>With this combination, I get the error messages:</p>
  
  <ul>
  <li><p>File "/usr/lib/python2.7/dist-packages/vtk/numpy_interface/dataset_adapter.py", line 652, in append
      self.VTKObject.AddArray(arr)</p>
  
  <p>TypeError: AddArray argument 1: method requires a VTK object</p></li>
  <li><p>File "run.py", line 15, in </p>
  
  <pre><code>print progData.GetArray('T2').GetRange()
  </code></pre>
  
  <p>AttributeError: 'NoneType' object has no attribute 'GetRange'</p></li>
  </ul>
  
  <p>The first error message stems seems to be the reason for the second one.</p>