Cleanup: newlines at EOF for Python modules
[blender.git] / release / scripts / startup / bl_operators / screen_play_rendered_anim.py
1 # ##### BEGIN GPL LICENSE BLOCK #####
2 #
3 #  This program is free software; you can redistribute it and/or
4 #  modify it under the terms of the GNU General Public License
5 #  as published by the Free Software Foundation; either version 2
6 #  of the License, or (at your option) any later version.
7 #
8 #  This program is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #  GNU General Public License for more details.
12 #
13 #  You should have received a copy of the GNU General Public License
14 #  along with this program; if not, write to the Free Software Foundation,
15 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 # ##### END GPL LICENSE BLOCK #####
18
19 # <pep8-80 compliant>
20
21 # Originally written by Matt Ebb
22
23 import bpy
24 from bpy.types import Operator
25 import os
26
27
28 def guess_player_path(preset):
29     import sys
30
31     if preset == 'INTERNAL':
32         return bpy.app.binary_path
33     elif preset == 'BLENDER24':
34         player_path = "blender"
35
36         if sys.platform == "darwin":
37             test_path = "/Applications/blender 2.49.app/Contents/MacOS/blender"
38         elif sys.platform[:3] == "win":
39             test_path = "/Program Files/Blender Foundation/Blender/blender.exe"
40
41             if os.path.exists(test_path):
42                 player_path = test_path
43
44     elif preset == 'DJV':
45         player_path = "djv_view"
46
47         if sys.platform == "darwin":
48             # TODO, crummy supporting only 1 version,
49             # could find the newest installed version
50             test_path = ("/Applications/djv-0.8.2.app"
51                          "/Contents/Resources/bin/djv_view")
52             if os.path.exists(test_path):
53                 player_path = test_path
54
55     elif preset == 'FRAMECYCLER':
56         player_path = "framecycler"
57
58     elif preset == 'RV':
59         player_path = "rv"
60
61     elif preset == 'MPLAYER':
62         player_path = "mplayer"
63
64     else:
65         player_path = ""
66
67     return player_path
68
69
70 class PlayRenderedAnim(Operator):
71     """Play back rendered frames/movies using an external player"""
72     bl_idname = "render.play_rendered_anim"
73     bl_label = "Play Rendered Animation"
74     bl_options = {'REGISTER'}
75
76     def execute(self, context):
77         import subprocess
78
79         scene = context.scene
80         rd = scene.render
81         prefs = context.user_preferences
82
83         preset = prefs.filepaths.animation_player_preset
84         player_path = prefs.filepaths.animation_player
85         # file_path = bpy.path.abspath(rd.filepath)  # UNUSED
86         is_movie = rd.is_movie_format
87
88         # try and guess a command line if it doesn't exist
89         if player_path == "":
90             player_path = guess_player_path(preset)
91
92         if is_movie is False and preset in {'FRAMECYCLER', 'RV', 'MPLAYER'}:
93             # replace the number with '#'
94             file_a = rd.frame_path(frame=0)
95
96             # TODO, make an api call for this
97             frame_tmp = 9
98             file_b = rd.frame_path(frame=frame_tmp)
99
100             while len(file_a) == len(file_b):
101                 frame_tmp = (frame_tmp * 10) + 9
102                 file_b = rd.frame_path(frame=frame_tmp)
103             file_b = rd.frame_path(frame=int(frame_tmp / 10))
104
105             file = ("".join((c if file_b[i] == c else "#")
106                     for i, c in enumerate(file_a)))
107             del file_a, file_b, frame_tmp
108             file = bpy.path.abspath(file)  # expand '//'
109         else:
110             path_valid = True
111             # works for movies and images
112             file = rd.frame_path(frame=scene.frame_start, preview=scene.use_preview_range)
113             file = bpy.path.abspath(file)  # expand '//'
114             if not os.path.exists(file):
115                 self.report({'WARNING'}, "File %r not found" % file)
116                 path_valid = False
117
118             #one last try for full range if we used preview range
119             if scene.use_preview_range and not path_valid:
120                 file = rd.frame_path(frame=scene.frame_start, preview=False)
121                 file = bpy.path.abspath(file)  # expand '//'
122                 if not os.path.exists(file):
123                     self.report({'WARNING'}, "File %r not found" % file)
124
125         cmd = [player_path]
126         # extra options, fps controls etc.
127         if scene.use_preview_range:
128             frame_start = scene.frame_preview_start
129             frame_end = scene.frame_preview_end
130         else:
131             frame_start = scene.frame_start
132             frame_end = scene.frame_end
133         if preset == 'INTERNAL':
134             opts = ["-a",
135                     "-f", str(rd.fps), str(rd.fps_base),
136                     "-s", str(frame_start),
137                     "-e", str(frame_end),
138                     "-j", str(scene.frame_step),
139                     file]
140             cmd.extend(opts)
141         elif preset == 'DJV':
142             opts = [file, "-playback_speed", "%d" % int(rd.fps / rd.fps_base)]
143             cmd.extend(opts)
144         elif preset == 'FRAMECYCLER':
145             opts = [file, "%d-%d" % (scene.frame_start, scene.frame_end)]
146             cmd.extend(opts)
147         elif preset == 'RV':
148             opts = ["-fps", str(rd.fps), "-play", "[ %s ]" % file]
149             cmd.extend(opts)
150         elif preset == 'MPLAYER':
151             opts = []
152             if is_movie:
153                 opts.append(file)
154             else:
155                 opts += [("mf://%s" % file.replace("#", "?")),
156                          "-mf",
157                          "fps=%.4f" % (rd.fps / rd.fps_base),
158                          ]
159
160             opts += ["-loop", "0", "-really-quiet", "-fs"]
161             cmd.extend(opts)
162         else:  # 'CUSTOM'
163             cmd.append(file)
164
165         # launch it
166         print("Executing command:\n  %r" % " ".join(cmd))
167
168         # workaround for boost 1.46, can be eventually removed. bug: [#32350]
169         env_copy = os.environ.copy()
170         if preset == 'INTERNAL':
171             env_copy["LC_ALL"] = "C"
172         # end workaround
173
174         try:
175             subprocess.Popen(cmd, env=env_copy)
176         except Exception as e:
177             self.report({'ERROR'},
178                         "Couldn't run external animation player with command "
179                         "%r\n%s" % (" ".join(cmd), str(e)))
180             return {'CANCELLED'}
181
182         return {'FINISHED'}
183
184
185 classes = (
186     PlayRenderedAnim,
187 )