[vtkusers] importing unstructured grid data from numpy array

Eric E. Monson emonson at cs.duke.edu
Wed May 26 06:59:41 EDT 2010


Hey Florian,

That's what the numpy_to_vtkIdTypeArray() is for -- creating a vtkIdTypeArray. You can't just cast another type of VTK array to IdType, so that's why they have a special function for it. As another note, you can also get vtkIntArray out of numpy_to_vtk() if you specifically send in an int type numpy array.

Hope that helps,
-Eric

On May 26, 2010, at 4:14 AM, Florian Bruckner wrote:

> thanks for your help at first. 
> 
> your example works well, but I have still troubles when I want to insert
> the cell data. I'm still a python newbie, but I could figure out that
> numpy_to_vtk gives me a vtkFloatArray whereas the SetCells() function of
> my vtkCellArray requires a vtkIdTypeArray.
> 
> is there something like a typecast operator in python or is there a
> conversion function available somewhere?
> 
> my code looks like the following:
>>> tets = array([[0,1,2,3]])
>>> ccV = VN.numpy_to_vtk(tets, deep=True)
>>> cells = vtk.vtkCellArray()
>>> cells.SetCells(1, ccV)  ==> error
>>> ug.SetCells(VTK_TETRA, cells)
> 
> bye
> FloB
> 
> 
> On Tue, 2010-05-25 at 09:31 -0400, Eric E. Monson wrote:
>> Hey Florian,
>> 
>> There are pretty easy ways to do it, but I haven't used tvtk for a while, so I don't remember the comparison. You can do something like this:
>> 
>> # ===================================
>> from vtk.util import numpy_support as VN
>> import vtk
>> import numpy as N
> 
>> ug = vtk.vtkUnstructuredGrid()
>> pts = vtk.vtkPoints()
>> 
>> # Create the points array and set the UG to use them
>> pp = N.random.rand(10,3)
>> ppV = VN.numpy_to_vtk(pp, deep=True)
>> pts.SetData(ppV)
>> ug.SetPoints(pts)
>> 
>> # Add an attribute array (scalars) to the UG
>> aa = N.random.rand(10)
>> aaV = VN.numpy_to_vtk(aa,deep=True)
>> aaV.SetName('Attribute1')
>> ug.GetPointData().AddArray(aaV)
>> # ===================================
>> 
>> So it's not too bad. Also, note that there is a vtk_to_numpy() and a numpy_to_vtkIdTypeArray(), which often comes in quite handy for me.
>> 
>> Talk to you later,
>> -Eric
>> 
>> ------------------------------------------------------
>> Eric E Monson
>> Duke Visualization Technology Group
>> 
>> 
>> On May 25, 2010, at 5:44 AM, Florian Bruckner wrote:
>> 
>>> hello,
>>> 
>>> I would need a method to read unstructured grid data from a numpy array.
>>> Does anyone know a function like vtkImageInputFrom/ExportToArray.py that
>>> also handles unstructured grids? or is there a possibility to transform
>>> image data to unstructured grids?
>>> 
>>> i only found the tvtk library, but in order to use it I will have to
>>> install the whole ETS project. In addition the documentation of this
>>> library mention that it is more 'pytonic' to use tvtk but that it should
>>> also be possible to do the same things with pure vtk.
>>> 
>>> thanks 
>>> FloB
>>> 
>>> 
>>> _______________________________________________
>>> Powered by www.kitware.com
>>> 
>>> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>>> 
>>> Please keep messages on-topic and check the VTK 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