9913f875689db8b9a29102a92ea3d3a1beea4ea7
[blender.git] / tests / python / opengl_draw_tests.py
1 #!/usr/bin/env python3
2 # Apache License, Version 2.0
3
4 import argparse
5 import os
6 import subprocess
7 import sys
8
9
10 def screenshot():
11     import bpy
12
13     output_path = sys.argv[-1]
14
15     # Force redraw and take screenshot.
16     bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)
17     bpy.ops.screen.screenshot(filepath=output_path, full=True)
18
19     bpy.ops.wm.quit_blender()
20
21
22 # When run from inside Blender, take screenshot and exit.
23 try:
24     import bpy
25     inside_blender = True
26 except ImportError:
27     inside_blender = False
28
29 if inside_blender:
30     screenshot()
31     sys.exit(0)
32
33
34 def render_file(filepath, output_filepath):
35     command = (
36         BLENDER,
37         "--no-window-focus",
38         "--window-geometry",
39         "0", "0", "1024", "768",
40         "-noaudio",
41         "--factory-startup",
42         "--enable-autoexec",
43         filepath,
44         "-P",
45         os.path.realpath(__file__),
46         "--",
47         output_filepath)
48
49     try:
50         # Success
51         output = subprocess.check_output(command)
52         if VERBOSE:
53             print(output.decode("utf-8"))
54         return None
55     except subprocess.CalledProcessError as e:
56         # Error
57         if os.path.exists(output_filepath):
58             os.remove(output_filepath)
59         if VERBOSE:
60             print(e.output.decode("utf-8"))
61         return "CRASH"
62     except BaseException as e:
63         # Crash
64         if os.path.exists(output_filepath):
65             os.remove(output_filepath)
66         if VERBOSE:
67             print(e)
68         return "CRASH"
69
70
71 def create_argparse():
72     parser = argparse.ArgumentParser()
73     parser.add_argument("-blender", nargs="+")
74     parser.add_argument("-testdir", nargs=1)
75     parser.add_argument("-outdir", nargs=1)
76     parser.add_argument("-idiff", nargs=1)
77     return parser
78
79
80 def main():
81     parser = create_argparse()
82     args = parser.parse_args()
83
84     global BLENDER, VERBOSE
85
86     BLENDER = args.blender[0]
87     VERBOSE = os.environ.get("BLENDER_VERBOSE") is not None
88
89     test_dir = args.testdir[0]
90     idiff = args.idiff[0]
91     output_dir = args.outdir[0]
92
93     from modules import render_report
94     report = render_report.Report("OpenGL Draw Test Report", output_dir, idiff)
95     ok = report.run(test_dir, render_file)
96
97     sys.exit(not ok)
98
99
100 if __name__ == "__main__":
101     main()