CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   ParaView (https://www.cfd-online.com/Forums/paraview/)
-   -   [General] Generating isosurface from vtk file with python script (https://www.cfd-online.com/Forums/paraview/200133-generating-isosurface-vtk-file-python-script.html)

Leios March 25, 2018 08:53

Generating isosurface from vtk file with python script
 
I have a vtk file that is generated via auxiliary code and would like to generate isosurfaces of this (3D) data. I can read this in correctly and create the necessary isosurfaces manually, and can even do the same with the python shell when the gui is up by doing the following:

Code:

import paraview.simple as ps

# reads in a vtk file
test = ps.OpenDataFile("/path/to/test.vtk")
c = ps.Contour(Input=test, Isosurfaces=[0.5])

ps.Show(test)
ps.Show(c)
ps.Render()
ps.WriteImage("/path/to/check.png")

If I enter these commands line-by-line into the python shell, the correct isosurface is generated; however, if I run the script by using either the "Run Script" button or pvbatch, the isosurface is not created. In both cases, the vtk data is (test) is generated correctly. Interestingly, when I click the "Apply" button (with no other input on my end) on the isosurface in the paraview window, the isosurface is also generated correctly, which means that I must be missing some scriptable "Apply" button.

The closest solution to this can be found here: https://colorsfordirectors.wordpress...ython-scripts/

Code:

# 1. import the paraview.simple module (here renammed 'pvs')
import paraview.simple as pvs
 
# 2. create the filter with desired input
my_filter = pvs.FilterName(Input=my_data)
 
# 3. set properties
my_filter.PropertyOne = [0, 0, 0]
 
# 4. update the filter (optionnal)
my_filter.UpdatePipeline()

I added the UpdatePipeline() function to my script and this seemed to do nothing.

I will test this out on another paraview installation / linux distribution tomorrow when I get back into the office. I am currently running Arch and paraview 5.4.1 .

If anyone has any advice, I would love to hear it! I'm new to paraview python scripting and am having a bit of trouble figuring everything out. Thanks for reading and any help you can provide!

forgeanalytics March 26, 2018 12:33

You'll probably want something like the following:

Code:

import paraview.simple as ps

view = ps.GetActiveViewOrCreate('RenderView')

# reads in a vtk file
test = ps.OpenDataFile("/path/to/test.vtk")
test_display = ps.Show(test, view)

view.Update()

c = ps.Contour(Input=test, Isosurfaces=[0.5])
c_display = ps.Show(c, view)

# If you want to hide the original data (or don't show in first place)
# ps.Hide(test, view)

view.Update()

# set camera position...

ps.SaveScreenshot('/path/to/img.png', view, ImageResolution=[1920,1080])

# or ps.RenderAllViews()


ParaView has a Python script recorder at "Tools >> Start Trace" and "Tools >> Stop Trace" that you'll find useful (if you weren't using it already).

Let me know if you have any problems.


All times are GMT -4. The time now is 00:35.