579af9b34b3e2f5d5c08374da83d135d9716c926
[blender.git] / release / scripts / op / screen_play_rendered_anim.py
1 # ***** BEGIN GPL LICENSE BLOCK *****
2 #
3 # Script copyright (C) Campbell J Barton
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software Foundation,
17 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 #
19 # ***** END GPL LICENCE BLOCK *****
20
21 # <pep8 compliant>
22
23 # History
24 #
25 # Originally written by Matt Ebb
26
27 import bpy
28 import os
29
30
31 def guess_player_path(preset):
32     import platform
33     try:
34         system = platform.system()
35     except UnicodeDecodeError:
36         import sys
37         system = sys.platform
38
39     if preset == 'BLENDER24':
40         player_path = "blender"
41
42         if system == 'Darwin':
43             test_path = "/Applications/blender 2.49.app/Contents/MacOS/blender"
44         elif system in ('Windows', 'win32'):
45             test_path = "/Program Files/Blender Foundation/Blender/blender.exe"
46
47             if os.path.exists(test_path):
48                 player_path = test_path
49
50     elif preset == 'DJV':
51         player_path = "djv_view"
52
53         if system == 'Darwin':
54             test_path = '/Applications/djv-0.8.2.app/Contents/Resources/bin/djv_view'
55             if os.path.exists(test_path):
56                 player_path = test_path
57
58     elif preset == 'FRAMECYCLER':
59         player_path = "framecycler"
60
61     elif preset == 'RV':
62         player_path = "rv"
63
64     elif preset == 'MPLAYER':
65         player_path = "mplayer"
66
67     return player_path
68
69
70 class PlayRenderedAnim(bpy.types.Operator):
71     '''Plays 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)
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 == 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                 print(frame_tmp)
103                 file_b = rd.frame_path(frame=frame_tmp)
104             file_b = rd.frame_path(frame=int(frame_tmp / 10))
105
106             file = "".join((c if file_b[i] == c else "#") for i, c in enumerate(file_a))
107         else:
108             # works for movies and images
109             file = rd.frame_path(frame=scene.frame_start)
110
111         file = bpy.path.abspath(file)  # expand '//'
112
113         cmd = [player_path]
114         # extra options, fps controls etc.
115         if preset == 'BLENDER24':
116             opts = ["-a", "-f", str(rd.fps), str(rd.fps_base), file]
117             cmd.extend(opts)
118         elif preset == 'DJV':
119             opts = [file, "-playback_speed", str(rd.fps)]
120             cmd.extend(opts)
121         elif preset == 'FRAMECYCLER':
122             opts = [file, "%d-%d" % (scene.frame_start, scene.frame_end)]
123             cmd.extend(opts)
124         elif preset == 'RV':
125             opts = ["-fps", str(rd.fps), "-play", "[ %s ]" % file]
126             cmd.extend(opts)
127         elif preset == 'MPLAYER':
128             opts = []
129             if is_movie:
130                 opts.append(file)
131             else:
132                 opts.append("mf://%s" % file.replace("#", "?"))
133                 opts += ["-mf", "fps=%.4f" % (rd.fps / rd.fps_base)]
134             opts += ["-loop", "0", "-really-quiet", "-fs"]
135             cmd.extend(opts)
136         else:  # 'CUSTOM'
137             cmd.append(file)
138
139         # launch it
140         try:
141             process = subprocess.Popen(cmd)
142         except:
143             pass
144             #raise OSError("Couldn't find an external animation player.")
145
146         return {'FINISHED'}
147
148
149 def register():
150     bpy.utils.register_module(__name__)
151
152
153 def unregister():
154     bpy.utils.unregister_module(__name__)
155
156 if __name__ == "__main__":
157     register()