1a0d639bccd3395b3b0fcb37d0c9fc4227eb7ca6
[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_file(filepath, output_filepath):
38     dirname = os.path.dirname(filepath)
39     basedir = os.path.dirname(dirname)
40     subject = os.path.basename(dirname)
41
42     frame_filepath = output_filepath + '0001.png'
43
44     command = [
45         BLENDER,
46         "--background",
47         "-noaudio",
48         "--factory-startup",
49         "--enable-autoexec",
50         filepath,
51         "-E", "BLENDER_WORKBENCH",
52         "-P",
53         os.path.realpath(__file__),
54         "-o", output_filepath,
55         "-F", "PNG",
56         "-f", "1"]
57
58     try:
59         # Success
60         output = subprocess.check_output(command)
61         if os.path.exists(frame_filepath):
62             shutil.copy(frame_filepath, output_filepath)
63             os.remove(frame_filepath)
64         if VERBOSE:
65             print(" ".join(command))
66             print(output.decode("utf-8"))
67         return None
68     except subprocess.CalledProcessError as e:
69         # Error
70         if os.path.exists(frame_filepath):
71             os.remove(frame_filepath)
72         if VERBOSE:
73             print(" ".join(command))
74             print(e.output.decode("utf-8"))
75         if b"Error: engine not found" in e.output:
76             return "NO_ENGINE"
77         elif b"blender probably wont start" in e.output:
78             return "NO_START"
79         return "CRASH"
80     except BaseException as e:
81         # Crash
82         if os.path.exists(frame_filepath):
83             os.remove(frame_filepath)
84         if VERBOSE:
85             print(" ".join(command))
86             print(e)
87         return "CRASH"
88
89
90 def create_argparse():
91     parser = argparse.ArgumentParser()
92     parser.add_argument("-blender", nargs="+")
93     parser.add_argument("-testdir", nargs=1)
94     parser.add_argument("-outdir", nargs=1)
95     parser.add_argument("-idiff", nargs=1)
96     return parser
97
98
99 def main():
100     parser = create_argparse()
101     args = parser.parse_args()
102
103     global BLENDER, VERBOSE
104
105     BLENDER = args.blender[0]
106     VERBOSE = os.environ.get("BLENDER_VERBOSE") is not None
107
108     test_dir = args.testdir[0]
109     idiff = args.idiff[0]
110     output_dir = args.outdir[0]
111
112     from modules import render_report
113     report = render_report.Report("Workbench Test Report", output_dir, idiff)
114     report.set_pixelated(True)
115     report.set_reference_dir("workbench_renders")
116     report.set_compare_engines('workbench', 'eevee')
117     ok = report.run(test_dir, render_file)
118
119     sys.exit(not ok)
120
121
122 if not inside_blender and __name__ == "__main__":
123     main()