Cycles: add animation denoising test, fix operator to work with single frames.
[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(output.decode("utf-8"))
60         return None
61     except subprocess.CalledProcessError as e:
62         # Error
63         if os.path.exists(frame_filepath):
64             os.remove(frame_filepath)
65         if VERBOSE:
66             print(e.output.decode("utf-8"))
67         if b"Error: engine not found" in e.output:
68             return "NO_ENGINE"
69         elif b"blender probably wont start" in e.output:
70             return "NO_START"
71         return "CRASH"
72     except BaseException as e:
73         # Crash
74         if os.path.exists(frame_filepath):
75             os.remove(frame_filepath)
76         if VERBOSE:
77             print(e)
78         return "CRASH"
79
80
81 def create_argparse():
82     parser = argparse.ArgumentParser()
83     parser.add_argument("-blender", nargs="+")
84     parser.add_argument("-testdir", nargs=1)
85     parser.add_argument("-outdir", nargs=1)
86     parser.add_argument("-idiff", nargs=1)
87     return parser
88
89
90 def main():
91     parser = create_argparse()
92     args = parser.parse_args()
93
94     global BLENDER, VERBOSE
95
96     BLENDER = args.blender[0]
97     VERBOSE = os.environ.get("BLENDER_VERBOSE") is not None
98
99     test_dir = args.testdir[0]
100     idiff = args.idiff[0]
101     output_dir = args.outdir[0]
102
103     from modules import render_report
104     report = render_report.Report("Cycles Test Report", output_dir, idiff)
105     report.set_pixelated(True)
106     ok = report.run(test_dir, render_file)
107
108     sys.exit(not ok)
109
110
111 if __name__ == "__main__":
112     main()