-
Notifications
You must be signed in to change notification settings - Fork 1
/
pvsm2png.py
63 lines (49 loc) · 1.77 KB
/
pvsm2png.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Export png from cgns and paraview state file
# Usage:
# pvpython pvsm2png.py statefile.pvsm solution.cgns
# MIT license: Sebastien Lemaire
import os
import sys
import xml.etree.ElementTree as ET
from paraview.simple import *
def usage():
print("""Render image from state file (.pvsm) and cgns data
Usage:
pvpython {} state_file.pvsm solution.cgns""".format(sys.argv[0]))
exit()
if len(sys.argv) < 3:
usage()
if sys.argv[1].endswith(".pvsm"):
state_file = sys.argv[1]
cgns_file = sys.argv[2]
elif sys.argv[2].endswith(".pvsm"):
state_file = sys.argv[2]
cgns_file = sys.argv[1]
else:
print(".pvsm file not found")
usage()
export_filename = "{}_export.png".format(os.path.splitext(state_file)[0])
# Find source name in state file
state_root = ET.parse(state_file)
sources = state_root.findall("ServerManagerState/ProxyCollection[@name='sources']/Item")
source_name = None
for source in sources[::-1]:
if source.attrib['name'].endswith(".cgns"):
source_name = source.attrib['name']
break
if source_name == None:
source_name = state_root.find("ServerManagerState/ProxyCollection[@name='sources']/Item[last()]").attrib['name']
source_name = source_name.replace(".", "")
source_name = source_name.replace("-", "")
kwargs = {source_name+"FileNames": [cgns_file]}
# Load state file in paraview
print("Loading {} in ParaView using {} state file".format(cgns_file, state_file))
LoadState(state_file, LoadStateDataFileOptions='Choose File Names',
DataDirectory=os.path.splitext(cgns_file)[0], **kwargs)
# find view
renderView1 = FindViewOrCreate('RenderView1', viewtype='RenderView')
# save screenshot
SaveScreenshot(export_filename, renderView1)
print("{} saved".format(export_filename))