8699862d24b4d872c4d16df81f123b2218bb6660
[blender.git] / release / scripts / startup / bl_operators / 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 sys
33
34     if preset == 'BLENDER24':
35         player_path = "blender"
36
37         if sys.platform == "darwin":
38             test_path = "/Applications/blender 2.49.app/Contents/MacOS/blender"
39         elif sys.platform[:3] == "win":
40             test_path = "/Program Files/Blender Foundation/Blender/blender.exe"
41
42             if os.path.exists(test_path):
43                 player_path = test_path
44
45     elif preset == 'DJV':
46         player_path = "djv_view"
47
48         if sys.platform == "darwin":
49             # TODO, crummy supporting only 1 version, could find the newest installed version
50             test_path = '/Applications/djv-0.8.2.app/Contents/Resources/bin/djv_view'
51             if os.path.exists(test_path):
52                 player_path = test_path
53
54     elif preset == 'FRAMECYCLER':
55         player_path = "framecycler"
56
57     elif preset == 'RV':
58         player_path = "rv"
59
60     elif preset == 'MPLAYER':
61         player_path = "mplayer"
62     
63     else:
64         player_path = ""
65
66     return player_path
67
68
69 class PlayRenderedAnim(bpy.types.Operator):
70     '''Plays back rendered frames/movies using an external player.'''
71     bl_idname = "render.play_rendered_anim"
72     bl_label = "Play Rendered Animation"
73     bl_options = {'REGISTER'}
74
75     def execute(self, context):
76         import subprocess
77
78         scene = context.scene
79         rd = scene.render
80         prefs = context.user_preferences
81
82         preset = prefs.filepaths.animation_player_preset
83         player_path = prefs.filepaths.animation_player
84         # file_path = bpy.path.abspath(rd.filepath)  # UNUSED
85         is_movie = rd.is_movie_format
86
87         # try and guess a command line if it doesn't exist
88         if player_path == '':
89             player_path = guess_player_path(preset)
90
91         if is_movie == False and preset in ('FRAMECYCLER', 'RV', 'MPLAYER'):
92             # replace the number with '#'
93             file_a = rd.frame_path(frame=0)
94
95             # TODO, make an api call for this
96             frame_tmp = 9
97             file_b = rd.frame_path(frame=frame_tmp)
98
99             while len(file_a) == len(file_b):
100                 frame_tmp = (frame_tmp * 10) + 9
101                 print(frame_tmp)
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 "#") for i, c in enumerate(file_a))
106         else:
107             # works for movies and images
108             file = rd.frame_path(frame=scene.frame_start)
109
110         file = bpy.path.abspath(file)  # expand '//'
111
112         cmd = [player_path]
113         # extra options, fps controls etc.
114         if preset == 'BLENDER24':
115             opts = ["-a", "-f", str(rd.fps), str(rd.fps_base), file]
116             cmd.extend(opts)
117         elif preset == 'DJV':
118             opts = [file, "-playback_speed", str(rd.fps)]
119             cmd.extend(opts)
120         elif preset == 'FRAMECYCLER':
121             opts = [file, "%d-%d" % (scene.frame_start, scene.frame_end)]
122             cmd.extend(opts)
123         elif preset == 'RV':
124             opts = ["-fps", str(rd.fps), "-play", "[ %s ]" % file]
125             cmd.extend(opts)
126         elif preset == 'MPLAYER':
127             opts = []
128             if is_movie:
129                 opts.append(file)
130             else:
131                 opts.append("mf://%s" % file.replace("#", "?"))
132                 opts += ["-mf", "fps=%.4f" % (rd.fps / rd.fps_base)]
133             opts += ["-loop", "0", "-really-quiet", "-fs"]
134             cmd.extend(opts)
135         else:  # 'CUSTOM'
136             cmd.append(file)
137             
138         if (player_path == "") or (os.path.exists(player_path)==False):
139             self.report({'ERROR'}, "Couldn't find an external animation player")
140         else:
141             # launch it
142             try:
143                 process = subprocess.Popen(cmd)
144             except:
145                 pass
146
147         return {'FINISHED'}