a related question: Re: [vtkusers] VTK <--> NumPy <--> VTK Integration

Prabhu Ramachandran prabhu at aero.iitm.ernet.in
Tue Feb 4 13:05:30 EST 2003


>>>>> "BC" == bryan cole <bryan.cole at teraview.co.uk> writes:

    BC> Thanks for your comments Prabhu. My problem is solved: It's
    BC> necessary to call vtkDataArray.SetNumberOfComponents() and
    BC> vtkDataArray.SetNumberOfTuples() **after**
    BC> vtkDataArray.SetVoidArray()

I don't think this is necessary.  My suspicion is that your data types
do not match in which case your array was converted to junk.  Here is
an example to prove my point:

 >>> import vtk
 >>> from Numeric import *
 >>> a = array([[1.,0,0],[2.,0,0],[3.,0,0]])
 >>> fa = vtk.vtkFloatArray()
 >>> fa.SetNumberOfComponents(3)
 >>> fa.SetNumberOfTuples(3)
 >>> fa.SetVoidArray (a.flat, len(a.flat), 1)
 >>> fa.GetTuple3(0)
 (0.0, 1.875, 0.0)  ## Wrong answer!
 >>> fa.GetTuple3(1)
 (0.0, 0.0, 0.0)    ## Wrong again!
 >>> b = a.astype(Float32)
 >>> fa.SetVoidArray (b.flat, len(b.flat), 1)
 >>> fa.GetTuple3(0)
 (1.0, 0.0, 0.0)    ## Works fine!
 >>> fa.GetTuple3(1)
 (2.0, 0.0, 0.0)


Remember though that you need to keep the NumPy array saved inside
Python.  If you delete the array the float array will again point to
junk!  Try it out by doing 'del b' and see.

cheers,
prabhu



More information about the vtkusers mailing list