summaryrefslogtreecommitdiffstats
path: root/dimension/dimension.in
blob: b5736cf682ec4775806e31cd9d1a75e58d6c255c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python3

#########################################################################
# Copyright (C) 2011 Tavian Barnes <tavianator@tavianator.com>          #
#                                                                       #
# This file is part of Dimension.                                       #
#                                                                       #
# Dimension is free software; you can redistribute it and/or modify it  #
# under the terms of the GNU General Public License as published by the #
# Free Software Foundation; either version 3 of the License, or (at     #
# your option) any later version.                                       #
#                                                                       #
# Dimension is distributed in the hope that it will be useful, but      #
# WITHOUT ANY WARRANTY; without even the implied warranty of            #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     #
# General Public License for more details.                              #
#                                                                       #
# You should have received a copy of the GNU General Public License     #
# along with this program.  If not, see <http://www.gnu.org/licenses/>. #
#########################################################################

import argparse
import os.path
import sys

# Display a progress bar
def progress_bar(str, progress):
  if not _args.quiet:
    print(str, end = ' ')
    sys.stdout.flush()

    term_width = terminal_width()
    width = term_width - (len(str) + 1)%term_width
    for i in range(width):
      progress.wait(i/width)
      print('.', end = '')
      sys.stdout.flush()

    print()
    sys.stdout.flush()

  progress.finish()

# Specialized parser to print --version output to stdout rather than stderr,
# to pass distcheck
class _DimensionArgumentParser(argparse.ArgumentParser):
  def exit(self, status = 0, message = None):
    if message:
      file = sys.stdout if status == 0 else sys.stderr
      file.write(message)
    sys.exit(status)

# Parse the command line
_parser = _DimensionArgumentParser(
  epilog = "@PACKAGE_STRING@\n"
           "@PACKAGE_URL@\n"
           "Copyright (C) 2009-2011 Tavian Barnes <@PACKAGE_BUGREPORT@>\n"
           "Licensed under the GNU General Public License",
  formatter_class = argparse.RawDescriptionHelpFormatter,
  conflict_handler = "resolve", # For -h as height instead of help
)

_parser.add_argument("-V", "--version", action = "version",
                     version = "@PACKAGE_STRING@")

_parser.add_argument("-w", "--width", action = "store", type = int,
                     default = 768, help = "image width")
_parser.add_argument("-h", "--height", action = "store", type = int,
                     default = 480, help = "image height")

_parser.add_argument("-v", "--verbose", action = "store_true",
                     help = "print more information")
_parser.add_argument("-q", "--quiet", action = "store_true",
                     help = "print less information")

_parser.add_argument("--threads", action = "store", type = int,
                     help = "the number of threads to render with")
_parser.add_argument("--quality", action = "store", type = int,
                     help = "the scene quality")

_parser.add_argument("-o", "--output", action = "store", type = str,
                     help = "the output image file")
_parser.add_argument("input", action = "store", type = str,
                     help = "the input scene description file")

# Debugging/testing options
_parser.add_argument("--strict", action = "store_true",
                     help = argparse.SUPPRESS)

_args = _parser.parse_args()

# Default output is basename(input).png
if _args.output is None:
  _noext = os.path.splitext(os.path.basename(_args.input))[0]
  _args.output = _noext + ".png"

# Imports available to scripts
from math import *
from dimension import *

# --strict option
die_on_warnings(_args.strict)

# Defaults
objects          = []
lights           = []
camera           = PerspectiveCamera()
default_texture  = Texture(finish = Ambient(0.1) + Diffuse(0.6))
default_interior = Interior()
background       = Black
sky_sphere       = None
recursion_limit  = None

# Execute the input script
if not _args.quiet:
  print("Parsing scene ...")

parse_timer = Timer()
with open(_args.input) as _fh:
  exec(compile(_fh.read(), _args.input, "exec"))
parse_timer.complete()

# Make the canvas
canvas = Canvas(width = _args.width, height = _args.height)
canvas.optimize_PNG()

# Make the scene object
scene = Scene(canvas  = canvas,
              objects = objects,
              lights  = lights,
              camera  = camera)
scene.default_texture = default_texture
scene.background      = background
if sky_sphere is not None:
  scene.sky_sphere = sky_sphere
if recursion_limit is not None:
  scene.recursion_limit = recursion_limit
if _args.threads is not None:
  scene.nthreads = _args.threads
if _args.quality is not None:
  scene.quality = _args.quality

# Raytrace the scene
if scene.nthreads == 1:
  render_message = "Rendering scene"
else:
  render_message = "Rendering scene (using %d threads)" % scene.nthreads
progress_bar(render_message, scene.raytrace_async())

# Write the output file
export_timer = Timer()
progress_bar("Writing %s" % _args.output, canvas.write_PNG_async(_args.output))
export_timer.complete()

# Print execution times
if _args.verbose:
  print()
  print("Parsing time:   ", parse_timer)
  print("Bounding time:  ", scene.bounding_timer)
  print("Rendering time: ", scene.render_timer)
  print("Exporting time: ", export_timer)