Examples#
Some examples require external input files. Before you start, please follow the link in the Example Scripts section to download the zip file with model and result files.
Example 01 - Capturing Images#
Screenshots can be created using the CaptureImageTool
class. First, an object of this class needs to be created. All the required properties of the screenshot (e.g. resolution, type, etc.) are defined as attributes of this object. In this example, the type is set to ‘jpg’ and the resolution to 1200x800. The file path is defined for each screenshot on the fly inside the for
loop.
The code loops over the win_list
(a list of all windows in the session created using the getWindows()
Session method) to create a screenshot per window using the capture()
method.
1import hw
2
3sess = hw.Session()
4win_list = sess.getWindows()
5
6capture = hw.CaptureImageTool()
7capture.type = 'jpg'
8capture.width = 1200
9capture.height = 800
10
11for i, win in enumerate(win_list):
12 sess.setActive(hw.Window,window=win)
13 capture.file=f"C:/Temp/screenshot_{i+1}"
14 capture.capture()
Example 02 - Capturing Videos#
Capturing result animations can be done using the CaptureImageTool
class. For the example to work correctly, the code needs to be saved in a file and executed from the Python console using the magic command %run
.
The example starts by defining the required path variables and setting up the window type to “animation” (i.e. the HyperView client). It proceeds with loading d3plot result files from the demos folder in the installation and displaying a contour plot of displacement magnitude. The orientation/view matrix is defined by calling the HWC view command via the evalHWC()
function.
The key step follows - the creation of a CaptureImageTool
object. The returned cvt1
object is used in a loop to capture the videos in three different formats (“avi”, “amf”, “gif”). The same approach is applied again afterwards - a second CaptureImageTool
is created and used to generate the animation in the form of individual frame pictures in three different formats (“jpeg”, “png”, “bmp”).
1import hw
2import hw.hv as hv
3import os
4
5ALTAIR_HOME = os.path.abspath(os.environ["ALTAIR_HOME"])
6modelFile = os.path.join(
7 ALTAIR_HOME,
8 "demos",
9 "mv_hv_hg",
10 "animation",
11 "dyna",
12 "bumper",
13 "bumper_deck.key",
14)
15resultFile = os.path.join(
16 ALTAIR_HOME, "demos", "mv_hv_hg", "animation", "dyna", "bumper", "d3plot"
17)
18scriptDir = os.path.normpath(os.path.abspath(os.path.dirname(__file__)))
19exportDir = os.path.normpath(os.path.join(scriptDir, "Export"))
20
21if not os.path.exists(exportDir):
22 os.makedirs(exportDir)
23
24ses = hw.Session()
25ses.new()
26page = ses.get(hw.Page)
27win = ses.get(hw.Window)
28
29win.type = "animation"
30
31win.addModelAndResult(modelFile, result=resultFile)
32res = ses.get(hv.Result)
33resScalar = hv.ResultDefinitionScalar(dataType="Displacement", dataComponent="Mag")
34res.plot(resScalar)
35
36hw.evalHWC(
37"view projection orthographic | \
38view matrix -0.021537 0.988942 0.146732 \
39 0.000000 0.872753 0.090190 \
40 -0.479758 0.000000 -0.487686 \
41 0.117728 -0.865045 0.000000 \
42 -99.918198 345.190369 584.070618 1.000000 | \
43view clippingregion -284.806213 129.713852 528.670959 \
44 864.158752 -722.333801 601.748169"
45)
46
47model = ses.get(hv.Model)
48
49animTool = hw.AnimationTool()
50animTool.currentFrame = 26
51win.draw()
52
53cvt1 = hw.CaptureVideoTool()
54cvt1.dimension = "pixels"
55cvt1.width = 3000
56cvt1.height = 2000
57
58typeList1 = ["avi", "amf", "gif"]
59for t in typeList1:
60 cvt1.type = t
61 videoFile = os.path.join(exportDir, "exportVideoTest." + t)
62 print('Video file of type "' + t + '" = ' + videoFile)
63 cvt1.file = videoFile
64 cvt1.capture()
65
66cvt2 = hw.CaptureVideoTool()
67cvt2.top = 0.1
68cvt2.left = 0.3
69cvt2.bottom = 0.9
70cvt2.right = 0.7
71
72typeList2 = ["jpeg", "png", "bmp"]
73for t in typeList2:
74 print("Type = " + t)
75 cvt2.type = t
76 tmpExportDir = os.path.join(exportDir, t)
77 os.mkdir(os.path.join(exportDir, t))
78 videoFile = os.path.join(tmpExportDir, "exportVideoTest." + t)
79 print('Video file of type "' + t + '" = ' + videoFile)
80 cvt2.file = videoFile
81 cvt2.capture()
Figure 1. Export all possible video formats from HyperView