[vtkusers] Strange return of grid objects from a Python function
Guðni Karl Rosenkjær
grosenkj at eos.ubc.ca
Wed Aug 28 05:13:23 EDT 2013
Hi all,
After a good nights sleep, I figured out the problem. Since the numpy
array's that store the node coordinates aren't return from the function,
the returned object has a broken reference to the data. By deep-coping the
numpy data into the object, the problem is fixed.
The code that I had to change is:
nodCvtk = npsup.numpy_to_vtk(nodC,deep=1)
I hope this helps someone else.
Thanks,
Gudni
On Tue, Aug 27, 2013 at 11:33 AM, Guðni Karl Rosenkjær
<grosenkj at eos.ubc.ca>wrote:
> Hi all,
>
> I have written a python function to setup a curvilinear structured grid
> and returns the grid object at the end. In the returned grid object, I
> don't have the points as assigned rather very small values (order of e-50
> to e-300) that is effectively 0.
>
> The code is as follows:
> import vtk, numpy as np
> # The test function
> def testGrid():
> ''' Small test setting up a cuivilinear grid vts'''
> x = np.arange(-1,0.6,0.5)
> y = np.arange(-2,0.1,1.0)
> z = np.arange(2,-1.1,-0.5)
> gx,gy,gz = np.meshgrid(x,y,z)
> nodC =
> np.hstack((gx.T.reshape((np.prod(gx.shape),1)),gy.T.reshape((np.prod(gy.shape),1)),gz.T.reshape((np.prod(gz.shape),1))))
> nodCvtk = npsup.numpy_to_vtk(nodC)
> points = vtk.vtkPoints()
> points.SetData(nodCvtk)
>
> color = np.arange(np.prod(gx.shape-np.array([1,1,1])))
> cellColors = npsup.numpy_to_vtk(colorReSh)
> cellColors.SetName('Colors')
> sgrid = vtk.vtkStructuredGrid()
> sgrid.SetDimensions(gx.shape)
> sgrid.SetPoints(points)
> sgrid.GetCellData().AddArray(cellColors)
> def writeVTSFile(fileName,vtsObj):
> vtsWriter = vtk.vtkXMLStructuredGridWriter()
> vtsWriter.SetFileName(fileName)
> vtsWriter.SetInput(vtsObj)
> vtsWriter.Update()
> writeVTSFile('vts3DFile.vts',sgrid)
> return sgrid
>
> # Read the file
> vtsReader = vtk.vtkXMLStructuredGridReader()
> vtsReader.SetFileName('vts3DFile.vts')
> vtsReader.Update()
> sgridReader = vtsReader.GetOutput()
> # Excute the function
> sgridFunc = testGrid()
>
> # Print out the first point
> print sgridFunc.GetPoint(0)
> print sgridReader.GetPoint(0)
>
>
> The results from the print function are:
> (-1.6934276617628493e-54, 3.4178058e-316, -1.1548972446486807e-82)
> (-1.0, -2.0, 2.0)
>
> However, if I write the object to a file and read it everything is working
> order. Are there some special tricks to return a grid object from a python
> function?
>
> I am running on a Linux 64-bit Fedora 17, with self-compiled version of
> vtk 5.10.1 and python 2.7.
>
> Thanks,
> Gudni
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20130828/b61b8aea/attachment.htm>
More information about the vtkusers
mailing list