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