[vtkusers] Re: passing vtkDataArray to python/Numeric arrays

Malcolm Drummond malcolm at geovision.co.za
Wed Oct 13 13:04:56 EDT 2004


You've got me there. The best I could do was a bit convoluted ...

import vtk
import struct
import array

# populate vtkDataArray
n = 10
da = vtk.vtkFloatArray()
da.SetNumberOfTuples(n)
for i in range(n):
 da.SetValue(i,float(i))

# make string
s = ''
s = s.join([struct.pack('f',da.GetValue(i)) for i in
range(da.GetNumberOfTuples())])

# now make array
a = array.array('f')
a.fromstring(s)

# check
print len(a), a

... but there must be a more direct way. Maybe one of the Python gurus can
show how.

Malcolm

----- Original Message -----
From: "Bryan Cole" <bryan.cole at teraview.com>
To: <vtkusers at public.kitware.com>
Sent: Wednesday, October 13, 2004 3:39 PM
Subject: [vtkusers] Re: passing vtkDataArray to python/Numeric arrays



>
> There's an array function: fromstring(string,typecode). I haven't used it
> myself but it looks like what you need.

But how do I get vtkDataArray as a binary string (to pass to
Numeric.fromstring())?

vtkDataArray::GetVoidPointer() returns a pointer in "SWIG" form to python,
i.e. as a python-string. fromstring() interprets this as a literal string
(instead of as a pointer in memory) as so gives the wrong result.

BC

>

_______________________________________________
This is the private VTK discussion list.
Please keep messages on-topic. Check the FAQ at:
<http://public.kitware.com/cgi-bin/vtkfaq>
Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers




More information about the vtkusers mailing list