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