[vtkusers] Extracting PolyData in Python

David Gobbi dgobbi at atamai.com
Thu Nov 16 09:45:35 EST 2006


Hi Pierre,

There are two tricks that you can use to link a Python array to a VTK array:

==

from vtk import *
from Numeric import *

# Trick 1: export a VTK array to a Numeric array by copying it

b = vtkDoubleArray()
b.SetNumberOfComponents(3)
b.SetNumberOfTuples(2)
b.SetTuple3(0, 1,2,3)
b.SetTuple3(1, 10,20,30)

a = zeros( ( b.GetNumberOfTuples(), b.GetNumberOfComponents() )  'd')
b.ExportToVoidPointer(a)

print a
array([[  1.,   2.,   3.],
       [ 10.,  20.,  30.]])

# Trick 2: convert a Numeric array as a VTK array (they will share memory)

a = zeros(50, 'd')

b = vtkDoubleArray()
b.SetVoidArray(a,50,1)

b.SetValue(0, 4.5)
print a[0]
4.5

==

 - David



Pierre Barbier de Reuille wrote:
> Hello,
>
> if I want to process some image in Python I can use
> vtkImageExportToPython and vtkImageImportFromPython to get some numpy
> array. However, I don't know what I can do if I want to do the same with
> PolyData. Like a list of polygons exactly as represented by the
> vtkPolyData (i.e. (n,id1,id2,...,idn, n,id1,id2,...,idn, ...) as
> explained in the doc). The problem being that, in Python, it is quite
> impratical to use the InitTraversal / GetNextCell when you have a lot of
> cells.
>
> Thanks,
>
> Pierre Barbier de Reuille
>
> _______________________________________________
> This is the private VTK discussion list. 
> Please keep messages on-topic. Check the FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>
>   




More information about the vtkusers mailing list