Hey there,
It’s Robin from CFD Engine & I’m still chasing triangles 🙈
But let’s not talk about that, let’s talk about scripting your post-processing in ParaView and how you might get started instead.
The one sentence version of this email could be…
“write a Python script to do your post-pro & run it in batch”
and whilst that might be enough for some of you, for others it poses a few questions, like:
- How do I “write a Python script to do my post-pro” ?
- How do I “run it in batch” ?
I’ll take a stab at some simple answers to these questions, but first let’s take a moment for Python 🙏
Python for the win
In case you didn’t know, pretty much everything you do in the ParaView GUI can be done in a Python script using their Python API.
And whilst you can use Python scripts in GUI ParaView, they really come into their own with a couple of other ParaView tools:
pvbatch
– the ParaView script runner, great for unattended post-pro (especially if you’re using headless ParaView ). Feed it your Python script & let it get on with it;pvpython
– the interactive version of the above. If you want to use the Python API directly in a terminal (rather than using a script) then this is a great tool. Good for prototyping and debugging scripts that then go intopvbatch
.
Back to the questions – how do I write a Python script to do my post-pro, especially if I’m not familiar with Python?
Trace
My Python skillz are almost non-existent. I can just about patch something together, but I’m no Pythonista. If you’re the same, fear not, we have a secret weapon – Trace.
Open ParaView, click Tools > Start Trace (accept the defaults) and then do whatever you need to do in the GUI. When you’re done, click Tools > Stop Trace & up pops a “trace” of all the Python commands needed to repeat your steps.
Voilà – a Python script to automate your post-processing, without needing any real Python knowledge – super-useful.
You can use the output as-is, or tweak it as you see fit (especially as your Python skillz improve). I like to remove any mis-steps or intermediate renders to speed things up a bit & add a few variables to make it easier to tweak.
Trace can also be a useful way to surface additional commands to extend your basic scripts (as opposed to starting from scratch every time).
API Docs
Trace is awesome – but once you start to get more comfortable with using Python commands, the API documentation starts to come into it’s own.
You might find this list of commands particularly handy, it includes all of the available readers, writers, sources, filters & animation cues – more than you’ll ever need.
For each one, you get the names of the various parameters for that command, really useful when the parameter name is slightly different to the one used in the GUI. Plus you get a description of what they all do (it’s the same text that pops up as a tooltip in the GUI).
The “Trace & Docs” combo will get you almost anywhere, even as a Python noob. But it can still take a while to assemble really complex scenes. In that case you might want to make use of state files.
State Files
You’re probably already using state files (.pvsm
) to save your GUI sessions – handy for jumping back in at a later date, perhaps with another dataset.
For your trickier scenes, you could save a state file & use a simple Python script to read it in & write out your images, animations, or whatever you need.
You can create this little script using Trace of course 👍
My personal preference is to assemble the scene using the Python functions, but my scenes are pretty simple. I find the scripts are easier to read, understand & extend.
Your mileage may vary, especially if you’re creating complex scenes, in which case the state file may be your best bet.
Top Tip: A state file (
.pvsm
) can be a treasure trove of settings for things like camera positions, lookup tables, etc that come in handy when building a Python script. They’re not as easy to read as a script (they’re in XML format) but a bit of syntax highlighting & somecmd/ctrl-f
searching & you’ll find what you need.
What have I missed?
This has ended up much longer than I intended & I’ve barely scratched the surface 🤔
If automating ParaView is new to you, then you should have enough here to give it a try. Start small & build it up gradually until you have what you need.
But, if you have questions, please let me have them – they’ll help shape a follow-up issue. Similarly, if I’m doing this wrong, please let me know so I can share it with the class.
Until next week, stay safe,