1#!/usr/local/bin/python3.8
2
3#
4# This source file is part of appleseed.
5# Visit https://appleseedhq.net/ for additional information and resources.
6#
7# This software is released under the MIT license.
8#
9# Copyright (c) 2010-2013 Francois Beaune, Jupiter Jazz Limited
10# Copyright (c) 2014-2018 Francois Beaune, The appleseedhq Organization
11#
12# Permission is hereby granted, free of charge, to any person obtaining a copy
13# of this software and associated documentation files (the "Software"), to deal
14# in the Software without restriction, including without limitation the rights
15# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16# copies of the Software, and to permit persons to whom the Software is
17# furnished to do so, subject to the following conditions:
18#
19# The above copyright notice and this permission notice shall be included in
20# all copies or substantial portions of the Software.
21#
22# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28# THE SOFTWARE.
29#
30
31from __future__ import print_function
32import argparse
33import datetime
34import os
35import subprocess
36import sys
37
38
39# -------------------------------------------------------------------------------------------------
40# Constants.
41# -------------------------------------------------------------------------------------------------
42
43DEFAULT_TOOL_FILENAME = "appleseed.cli.exe" if os.name == "nt" else "appleseed.cli"
44
45
46# -------------------------------------------------------------------------------------------------
47# Utility functions.
48# -------------------------------------------------------------------------------------------------
49
50def safe_mkdir(dir):
51    if not os.path.exists(dir):
52        os.mkdir(dir)
53
54
55def walk(directory, recursive):
56    if recursive:
57        for dirpath, dirnames, filenames in os.walk(directory):
58            yield dirpath, dirnames, filenames
59    else:
60        yield os.walk(directory).next()
61
62
63def should_skip(path):
64    return path.startswith("skip - ")
65
66
67def format_duration(duration):
68    total_seconds = duration.total_seconds()
69    hours = int(total_seconds / 3600)
70    minutes = int((total_seconds % 3600) / 60)
71    seconds = total_seconds % 60
72    return "{0:02}:{1:02}:{2:09.6f}".format(hours, minutes, seconds)
73
74
75# -------------------------------------------------------------------------------------------------
76# Render a given project file.
77# -------------------------------------------------------------------------------------------------
78
79def render_project_file(args, project_directory, project_filename):
80    project_filepath = os.path.join(project_directory, project_filename)
81
82    output_directory = os.path.join(project_directory, 'renders')
83    safe_mkdir(output_directory)
84
85    output_filename = os.path.splitext(project_filename)[0] + '.' + args.output_format
86    output_filepath = os.path.join(output_directory, output_filename)
87
88    log_filename = os.path.splitext(project_filename)[0] + '.txt'
89    log_filepath = os.path.join(output_directory, log_filename)
90
91    with open(log_filepath, "w", 0) as log_file:
92        print("rendering: {0}: ".format(project_filepath), end='')
93
94        command = '"{0}" -o "{1}" "{2}"'.format(args.tool_path, output_filepath, project_filepath)
95        if args.args:
96            command += ' {0}'.format(" ".join(args.args))
97
98        log_file.write("Command line:\n    {0}\n\n".format(command))
99
100        start_time = datetime.datetime.now()
101        result = subprocess.call(command, stderr=log_file, shell=True)
102        end_time = datetime.datetime.now()
103
104        if result == 0:
105            print("{0} [ok]".format(format_duration(end_time - start_time)))
106        else:
107            print("[failed]")
108
109
110# -------------------------------------------------------------------------------------------------
111# Render all project files in a given directory (possibly recursively).
112# Returns the number of rendered project files.
113# -------------------------------------------------------------------------------------------------
114
115def render_project_files(args):
116    rendered_file_count = 0
117
118    for dirpath, dirnames, filenames in walk(args.directory, args.recursive):
119        if should_skip(os.path.basename(dirpath)):
120            print("skipping:  {0}...".format(dirpath))
121            continue
122
123        for filename in filenames:
124            if os.path.splitext(filename)[1] == '.appleseed':
125                if should_skip(filename):
126                    print("skipping:  {0}...".format(os.path.join(dirpath, filename)))
127                    continue
128
129                render_project_file(args, dirpath, filename)
130                rendered_file_count += 1
131
132    return rendered_file_count
133
134
135# -------------------------------------------------------------------------------------------------
136# Entry point.
137# -------------------------------------------------------------------------------------------------
138
139def main():
140    parser = argparse.ArgumentParser(description="render multiple project files.")
141    parser.add_argument("-t", "--tool-path", metavar="tool-path",
142                        help="set the path to the appleseed.cli tool")
143    parser.add_argument("-f", "--format", dest="output_format", metavar="FORMAT", default="exr",
144                        help="set output format (e.g. png, exr)")
145    parser.add_argument("-r", "--recursive", action='store_true', dest="recursive",
146                        help="scan the specified directory and all its subdirectories")
147    parser.add_argument("-p", "--parameter", dest="args", metavar="ARG", nargs="*",
148                        help="forward additional arguments to appleseed")
149    parser.add_argument("directory", help="directory to scan")
150    args = parser.parse_args()
151
152    # If no tool path is provided, search for the tool in the same directory as this script.
153    if args.tool_path is None:
154        script_directory = os.path.dirname(os.path.realpath(__file__))
155        args.tool_path = os.path.join(script_directory, DEFAULT_TOOL_FILENAME)
156        print("setting tool path to {0}.".format(args.tool_path))
157
158    start_time = datetime.datetime.now()
159    rendered_file_count = render_project_files(args)
160    end_time = datetime.datetime.now()
161
162    print("rendered {0} project file(s) in {1}."
163          .format(rendered_file_count, format_duration(end_time - start_time)))
164
165
166if __name__ == "__main__":
167    main()
168