简体   繁体   中英

plot vtk using python

I'm using python to plot a VTK data. I'm following this example: http://www.tfd.chalmers.se/~hani/kurser/OS_CFD_2007/presentation_rasmus.pdf and I'm using the same OpenFOAM tutorial elbow, with exactly the same archive elbow_200.vtk.

If I copy the code as it is, I got the error: AttributeError: 'vtkmodules.vtkFiltersCore.vtkFieldDataToAttributeD' object has no attribute 'SetInput'

Then I see that: https://vtk.org/doc/release/5.0/html/a01389.html#z1879_2 and realized that I have only SetInputFieldToDataObjectField.

I tried...

import os
from vtk import *
from vtk import vtkUnstructuredGridReader

#set the fileName for the current case
myFileName='elbow_200.vtk'

#Need a reader for unstructured grids
reader = vtkUnstructuredGridReader()

reader.SetFileName(myFileName)

reader.Update()

#In OpenFOAM all results are Field-data.
#This has no concept of cells or nodes.
#Need to filter to cells.


toCellFilter = vtkFieldDataToAttributeDataFilter()
toCellFilter.SetInputFieldToDataObjectField()
toCellFilter.SetInputFieldToCellDataField() #celulas
toCellFilter.SetOutputAttributeDataToCellData() 

#Assign here which field
#we are interested in.
toCellFilter.SetScalarComponent(0, 'alpha.air' ,0)
#This is all we need to do do calculations.
#To get 3D image, need some more components.
#First a window

renWin = vtkRenderWindow()

ren1 = vtkRenderer()
#Add renderer to window
renWin.AddRenderer(ren1)
#Add pressure data to the renderer.

#Mapping assigns data to colors and geometry.
mapper = vtkDataSetMapper()
mapper.SetInputData(toCellFilter.GetOutput())


#The object is assigned to an actor.
actor = vtkActor()
actor.SetMapper(mapper)
#Add actor to renderer.
ren1.AddActor(actor)
#Finally render image
renWin.Render()

Using the code above, I got this error:

2022-08-23 13:32:32.842 ( 831.111s) [ 8238F740] vtkExecutive.cxx:752 ERR| vtkCompositeDataPipeline (0x560cb23f6a40): Algorithm vtkFieldDataToAttributeDataFilter(0x560cb22945b0) returned failure for request: vtkInformation (0x560cb23f50b0) Debug: Off Modified Time: 1445 Reference Count: 1 Registered Events: (none) Request: REQUEST_DATA_OBJECT ALGORITHM_AFTER_FORWARD: 1 FORWARD_DIRECTION: 0

2022-08-23 13:32:32.920 ( 831.189s) [ 8238F740]vtkDemandDrivenPipeline:666 ERR| vtkCompositeDataPipeline (0x560cb23fe960): Input port 0 of algorithm vtkDataSetMapper(0x560cb2368110) has 0 connections but is not optional. 2022-08-23 13:32:32.920 ( 831.189s) [ 8238F740]vtkDemandDrivenPipeline:666 ERR| vtkCompositeDataPipeline (0x560cb23fe960): Input port 0 of algorithm vtkDataSetMapper(0x560cb2368110) has 0 connections but is not optional.

I tried this Plot vtk file using Python with no success..

The PDF presentation is of a very old version of VTK, where functions like SetInput() still existed. In the meantime, a lot has changed regarding how you create the VTK pipeline, see here for example. I guess you are using a more recent version of VTK.

My guess is that you have to change the line (from the original code from the presentation)

toCellFilter.SetInput(reader.GetOutput())

to the line

toCellFilter.SetInputData(reader.GetOutput())

and it should work.

Your code right now simply cannot work, because you don't give an input to your vtkFieldDataToAttributeDataFilter . That's also what the error output is trying to tell you: Algorithm vtkFieldDataToAttributeDataFilter(0x560cb22945b0) returned failure for request: vtkInformation . This means that the filter can't find its input, it doesn't get the vtkInformation object from the input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM