Merge branch 'master' into blender2.8
[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(" ".join(command))
81             print(output.decode("utf-8"))
82         return None
83     except subprocess.CalledProcessError as e:
84         # Error
85         if os.path.exists(frame_filepath):
86             os.remove(frame_filepath)
87         if VERBOSE:
88             print(" ".join(command))
89             print(e.output.decode("utf-8"))
90         if b"Error: engine not found" in e.output:
91             return "NO_ENGINE"
92         elif b"blender probably wont start" in e.output:
93             return "NO_START"
94         return "CRASH"
95     except BaseException as e:
96         # Crash
97         if os.path.exists(frame_filepath):
98             os.remove(frame_filepath)
99         if VERBOSE:
100             print(" ".join(command))
101             print(e)
102         return "CRASH"
103
104
105 def create_argparse():
106     parser = argparse.ArgumentParser()
107     parser.add_argument("-blender", nargs="+")
108     parser.add_argument("-testdir", nargs=1)
109     parser.add_argument("-outdir", nargs=1)
110     parser.add_argument("-idiff", nargs=1)
111     return parser
112
113
114 def main():
115     parser = create_argparse()
116     args = parser.parse_args()
117
118     global BLENDER, VERBOSE
119
120     BLENDER = args.blender[0]
121     VERBOSE = os.environ.get("BLENDER_VERBOSE") is not None
122
123     test_dir = args.testdir[0]
124     idiff = args.idiff[0]
125     output_dir = args.outdir[0]
126
127     from modules import render_report
128     report = render_report.Report("Cycles Test Report", output_dir, idiff)
129     report.set_pixelated(True)
130     report.set_compare_engines('cycles', 'eevee')
131     ok = report.run(test_dir, render_file)
132
133     sys.exit(not ok)
134
135
136 if __name__ == "__main__":
137     main()