Tests: speed up render tests by running multiple in the same process
[blender.git] / tests / python / workbench_render_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 setup():
13     import bpy
14
15     for scene in bpy.data.scenes:
16         scene.render.engine = 'BLENDER_WORKBENCH'
17
18     scene = bpy.context.scene
19     scene.display.shading.color_type = 'TEXTURE'
20
21
22 # When run from inside Blender, render and exit.
23 try:
24     import bpy
25     inside_blender = True
26 except ImportError:
27     inside_blender = False
28
29 if inside_blender:
30     try:
31         setup()
32     except Exception as e:
33         print(e)
34         sys.exit(1)
35
36
37 def render_files(filepaths, output_filepaths):
38     command = [
39         BLENDER,
40         "--background",
41         "-noaudio",
42         "--factory-startup",
43         "--enable-autoexec"]
44
45     for filepath, output_filepath in zip(filepaths, output_filepaths):
46         frame_filepath = output_filepath + '0001.png'
47         if os.path.exists(frame_filepath):
48             os.remove(frame_filepath)
49
50         command.extend([
51             filepath,
52             "-E", "BLENDER_WORKBENCH",
53             "-P",
54             os.path.realpath(__file__),
55             "-o", output_filepath,
56             "-F", "PNG",
57             "-f", "1"])
58
59     error = None
60     try:
61         # Success
62         output = subprocess.check_output(command)
63         if VERBOSE:
64             print(" ".join(command))
65             print(output.decode("utf-8"))
66     except subprocess.CalledProcessError as e:
67         # Error
68         if VERBOSE:
69             print(" ".join(command))
70             print(e.output.decode("utf-8"))
71         error = "CRASH"
72     except BaseException as e:
73         # Crash
74         if VERBOSE:
75             print(" ".join(command))
76             print(e.decode("utf-8"))
77         error = "CRASH"
78
79     # Detect missing filepaths and consider those errors
80     errors = []
81     for output_filepath in output_filepaths:
82         frame_filepath = output_filepath + '0001.png'
83         if os.path.exists(frame_filepath):
84             shutil.copy(frame_filepath, output_filepath)
85             os.remove(frame_filepath)
86             errors.append(None)
87         else:
88             errors.append(error)
89             error = 'SKIPPED'
90
91     return errors
92
93
94 def create_argparse():
95     parser = argparse.ArgumentParser()
96     parser.add_argument("-blender", nargs="+")
97     parser.add_argument("-testdir", nargs=1)
98     parser.add_argument("-outdir", nargs=1)
99     parser.add_argument("-idiff", nargs=1)
100     return parser
101
102
103 def main():
104     parser = create_argparse()
105     args = parser.parse_args()
106
107     global BLENDER, VERBOSE
108
109     BLENDER = args.blender[0]
110     VERBOSE = os.environ.get("BLENDER_VERBOSE") is not None
111
112     test_dir = args.testdir[0]
113     idiff = args.idiff[0]
114     output_dir = args.outdir[0]
115
116     from modules import render_report
117     report = render_report.Report("Workbench Test Report", output_dir, idiff)
118     report.set_pixelated(True)
119     report.set_reference_dir("workbench_renders")
120     report.set_compare_engines('workbench', 'eevee')
121     ok = report.run(test_dir, render_files)
122
123     sys.exit(not ok)
124
125
126 if not inside_blender and __name__ == "__main__":
127     main()