Tests: split off render report test code from Cycles tests.
[blender.git] / tests / python / cycles_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 render_file(filepath, output_filepath):
13     dirname = os.path.dirname(filepath)
14     basedir = os.path.dirname(dirname)
15     subject = os.path.basename(dirname)
16
17     custom_args = os.getenv('CYCLESTEST_ARGS')
18     custom_args = shlex.split(custom_args) if custom_args else []
19
20     # OSL and GPU examples
21     # custom_args += ["--python-expr", "import bpy; bpy.context.scene.cycles.shading_system = True"]
22     # custom_args += ["--python-expr", "import bpy; bpy.context.scene.cycles.device = 'GPU'"]
23
24     frame_filepath = output_filepath + '0001.png'
25
26     if subject == 'opengl':
27         command = [
28             BLENDER,
29             "--window-geometry", "0", "0", "1", "1",
30             "-noaudio",
31             "--factory-startup",
32             "--enable-autoexec",
33             filepath,
34             "-E", "CYCLES"]
35         command += custom_args
36         command += [
37             "-o", output_filepath,
38             "-F", "PNG",
39             '--python', os.path.join(basedir,
40                                      "util",
41                                      "render_opengl.py")]
42     elif subject == 'bake':
43         command = [
44             BLENDER,
45             "-b",
46             "-noaudio",
47             "--factory-startup",
48             "--enable-autoexec",
49             filepath,
50             "-E", "CYCLES"]
51         command += custom_args
52         command += [
53             "-o", output_filepath,
54             "-F", "PNG",
55             '--python', os.path.join(basedir,
56                                      "util",
57                                      "render_bake.py")]
58     else:
59         command = [
60             BLENDER,
61             "--background",
62             "-noaudio",
63             "--factory-startup",
64             "--enable-autoexec",
65             filepath,
66             "-E", "CYCLES"]
67         command += custom_args
68         command += [
69             "-o", output_filepath,
70             "-F", "PNG",
71             "-f", "1"]
72
73     try:
74         # Success
75         output = subprocess.check_output(command)
76         if os.path.exists(frame_filepath):
77             shutil.copy(frame_filepath, output_filepath)
78             os.remove(frame_filepath)
79         if VERBOSE:
80             print(output.decode("utf-8"))
81         return None
82     except subprocess.CalledProcessError as e:
83         # Error
84         if os.path.exists(frame_filepath):
85             os.remove(frame_filepath)
86         if VERBOSE:
87             print(e.output.decode("utf-8"))
88         if b"Error: engine not found" in e.output:
89             return "NO_ENGINE"
90         elif b"blender probably wont start" in e.output:
91             return "NO_START"
92         return "CRASH"
93     except BaseException as e:
94         # Crash
95         if os.path.exists(frame_filepath):
96             os.remove(frame_filepath)
97         if VERBOSE:
98             print(e)
99         return "CRASH"
100
101
102 def create_argparse():
103     parser = argparse.ArgumentParser()
104     parser.add_argument("-blender", nargs="+")
105     parser.add_argument("-testdir", nargs=1)
106     parser.add_argument("-outdir", nargs=1)
107     parser.add_argument("-idiff", nargs=1)
108     return parser
109
110
111 def main():
112     parser = create_argparse()
113     args = parser.parse_args()
114
115     global BLENDER, VERBOSE
116
117     BLENDER = args.blender[0]
118     VERBOSE = os.environ.get("BLENDER_VERBOSE") is not None
119
120     test_dir = args.testdir[0]
121     idiff = args.idiff[0]
122     output_dir = args.outdir[0]
123
124     from modules import render_report
125     report = render_report.Report("Cycles Test Report", output_dir, idiff)
126     report.set_pixelated(True)
127     ok = report.run(test_dir, render_file)
128
129     sys.exit(not ok)
130
131
132 if __name__ == "__main__":
133     main()