Merge branch 'blender2.7'
[blender.git] / tests / python / eevee_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     # Enable Eevee features
16     scene = bpy.context.scene
17     eevee = scene.eevee
18
19     eevee.use_sss = True
20     eevee.use_ssr = True
21     eevee.use_ssr_refraction = True
22     eevee.use_gtao = True
23     eevee.use_dof = True
24
25     eevee.use_volumetric = True
26     eevee.use_volumetric_shadows = True
27     eevee.volumetric_tile_size = '2'
28
29     for mat in bpy.data.materials:
30         mat.use_screen_refraction = True
31         mat.use_screen_subsurface = True
32
33
34 # When run from inside Blender, render and exit.
35 try:
36     import bpy
37     inside_blender = True
38 except ImportError:
39     inside_blender = False
40
41 if inside_blender:
42     try:
43         setup()
44     except Exception as e:
45         print(e)
46         sys.exit(1)
47
48
49 def render_file(filepath, output_filepath):
50     dirname = os.path.dirname(filepath)
51     basedir = os.path.dirname(dirname)
52     subject = os.path.basename(dirname)
53
54     frame_filepath = output_filepath + '0001.png'
55
56     command = [
57         BLENDER,
58         "--background",
59         "-noaudio",
60         "--factory-startup",
61         "--enable-autoexec",
62         filepath,
63         "-E", "BLENDER_EEVEE",
64         "-P",
65         os.path.realpath(__file__),
66         "-o", output_filepath,
67         "-F", "PNG",
68         "-f", "1"]
69
70     try:
71         # Success
72         output = subprocess.check_output(command)
73         if os.path.exists(frame_filepath):
74             shutil.copy(frame_filepath, output_filepath)
75             os.remove(frame_filepath)
76         if VERBOSE:
77             print(" ".join(command))
78             print(output.decode("utf-8"))
79         return None
80     except subprocess.CalledProcessError as e:
81         # Error
82         if os.path.exists(frame_filepath):
83             os.remove(frame_filepath)
84         if VERBOSE:
85             print(" ".join(command))
86             print(e.output.decode("utf-8"))
87         if b"Error: engine not found" in e.output:
88             return "NO_ENGINE"
89         elif b"blender probably wont start" in e.output:
90             return "NO_START"
91         return "CRASH"
92     except BaseException as e:
93         # Crash
94         if os.path.exists(frame_filepath):
95             os.remove(frame_filepath)
96         if VERBOSE:
97             print(" ".join(command))
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("Eevee Test Report", output_dir, idiff)
126     report.set_pixelated(True)
127     report.set_reference_dir("eevee_renders")
128     report.set_compare_engines('eevee', 'cycles')
129     ok = report.run(test_dir, render_file)
130
131     sys.exit(not ok)
132
133
134 if not inside_blender and __name__ == "__main__":
135     main()