Reputation: 17455
I'm trying to save multiples spheres to a file for the later visualization with ParaView.
What I have is a text file with the information about position and shape (radius) of each sphere. With Python and VTK I'm constructing a file to visualize the data in ParaView. Right now I'm able to save the center and radii of the spheres as a vtkUnstructuredGrid
and later in ParaView I'm adding a sphere glyph for the correct visualization. This approach is working but is very tedious to manually add the glyph each time I have to visualize the data. So I'm trying to figure out how to save the spheres as a whole into a vtk file.
I was playing around with vtkSphere
but can't find a way to save the sphere to a vtk file.
Now the question again is: How to save a vtkSphere to a VTK file?
I'm open to suggestions for alternative solutions to my problem.
Upvotes: 4
Views: 5197
Reputation: 41
Lib's answer is correct, but was missing one line of code to make it work. The vtkSphereSource needs to be updated after the changes are made for the file to work in Paraview.
sphere = vtk.vtkSphereSource()
sphere.SetRadius(4)
sphere.SetCenter(0,0,1)
sphere.Update()
writer = vtk.vtkPolyDataWriter()
writer.SetInputData(sphere.GetOutput())
writer.SetFileName('mysphere.vtk')
writer.Update()
This generates the file as expected.
Upvotes: 4
Reputation: 2956
If you want to save from Paraview, you can simply save the data after applying the glyph.
If you want to generate the spheres directly, use vtkSphereSource (instead of vtkSphere
which creates an implicit function, for example to be used with a clip filter or a glyph filter), then connect its output to a vtkwriter.
Here is the code for creating and writing a single sphere:
sphere = vtk.vtkSphereSource()
sphere.SetRadius(4)
sphere.SetCenter(0,0,1)
writer = vtk.vtkPolyDataWriter()
writer.SetInputData(sphere.GetOutput())
writer.SetFileName('mysphere.vtk')
writer.Update()
You can also create the glyphs directly with the vtk code, see http://www.vtk.org/Wiki/VTK/Examples/Cxx/Visualization/ProgrammableGlyphFilter (in this case, you use vtkSphere
as source for the glyph)
Upvotes: 4
Reputation: 1611
I think the necessary steps would be:
Combine the data see example http://www.vtk.org/Wiki/VTK/Examples/Cxx/Filtering/CombinePolyData
and write the result to a VTP File see example http://www.vtk.org/Wiki/VTK/Examples/Cxx/IO/WriteVTP
Upvotes: 1