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