report an error when user blender versions other then 2.4x for animation playback.
[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 import os
25
26
27 def guess_player_path(preset):
28     import sys
29
30     if preset == 'BLENDER24':
31         player_path = "blender"
32
33         if sys.platform == "darwin":
34             test_path = "/Applications/blender 2.49.app/Contents/MacOS/blender"
35         elif sys.platform[:3] == "win":
36             test_path = "/Program Files/Blender Foundation/Blender/blender.exe"
37
38             if os.path.exists(test_path):
39                 player_path = test_path
40
41     elif preset == 'DJV':
42         player_path = "djv_view"
43
44         if sys.platform == "darwin":
45             # TODO, crummy supporting only 1 version,
46             # could find the newest installed version
47             test_path = ("/Applications/djv-0.8.2.app"
48                          "/Contents/Resources/bin/djv_view")
49             if os.path.exists(test_path):
50                 player_path = test_path
51
52     elif preset == 'FRAMECYCLER':
53         player_path = "framecycler"
54
55     elif preset == 'RV':
56         player_path = "rv"
57
58     elif preset == 'MPLAYER':
59         player_path = "mplayer"
60
61     else:
62         player_path = ""
63
64     return player_path
65
66
67 class PlayRenderedAnim(bpy.types.Operator):
68     '''Plays back rendered frames/movies using an external player.'''
69     bl_idname = "render.play_rendered_anim"
70     bl_label = "Play Rendered Animation"
71     bl_options = {'REGISTER'}
72
73     def execute(self, context):
74         import subprocess
75
76         scene = context.scene
77         rd = scene.render
78         prefs = context.user_preferences
79
80         preset = prefs.filepaths.animation_player_preset
81         player_path = prefs.filepaths.animation_player
82         # file_path = bpy.path.abspath(rd.filepath)  # UNUSED
83         is_movie = rd.is_movie_format
84
85         # try and guess a command line if it doesn't exist
86         if player_path == "":
87             player_path = guess_player_path(preset)
88
89         if is_movie == False and preset in {'FRAMECYCLER', 'RV', 'MPLAYER'}:
90             # replace the number with '#'
91             file_a = rd.frame_path(frame=0)
92
93             # TODO, make an api call for this
94             frame_tmp = 9
95             file_b = rd.frame_path(frame=frame_tmp)
96
97             while len(file_a) == len(file_b):
98                 frame_tmp = (frame_tmp * 10) + 9
99                 file_b = rd.frame_path(frame=frame_tmp)
100             file_b = rd.frame_path(frame=int(frame_tmp / 10))
101
102             file = ("".join((c if file_b[i] == c else "#")
103                     for i, c in enumerate(file_a)))
104         else:
105             # works for movies and images
106             file = rd.frame_path(frame=scene.frame_start)
107
108         file = bpy.path.abspath(file)  # expand '//'
109
110         cmd = [player_path]
111         # extra options, fps controls etc.
112         if preset == 'BLENDER24':
113             # -----------------------------------------------------------------
114             # Check blender is not 2.5x until it supports playback again
115             try:
116                 process = subprocess.Popen([player_path, '--version'],
117                                            stdout=subprocess.PIPE,
118                                            )
119             except:
120                 # ignore and allow the main execution to catch the problem.
121                 process = None
122
123             if process is not None:
124                 process.wait()
125                 out = process.stdout.read()
126                 process.stdout.close()
127                 out_split = out.strip().split()
128                 if out_split[0] == b'Blender':
129                     if not out_split[1].startswith(b'2.4'):
130                         self.report({'ERROR'},
131                                     "Blender %s doesn't support playback: %r" %
132                                     (out_split[1].decode(), player_path))
133                         return {'CANCELLED'}
134                 del out, out_split
135             del process
136             # -----------------------------------------------------------------
137
138             opts = ["-a", "-f", str(rd.fps), str(rd.fps_base), file]
139             cmd.extend(opts)
140         elif preset == 'DJV':
141             opts = [file, "-playback_speed", "%d" % int(rd.fps / rd.fps_base)]
142             cmd.extend(opts)
143         elif preset == 'FRAMECYCLER':
144             opts = [file, "%d-%d" % (scene.frame_start, scene.frame_end)]
145             cmd.extend(opts)
146         elif preset == 'RV':
147             opts = ["-fps", str(rd.fps), "-play", "[ %s ]" % file]
148             cmd.extend(opts)
149         elif preset == 'MPLAYER':
150             opts = []
151             if is_movie:
152                 opts.append(file)
153             else:
154                 opts += [("mf://%s" % file.replace("#", "?")),
155                          "-mf",
156                          "fps=%.4f" % (rd.fps / rd.fps_base),
157                          ]
158
159             opts += ["-loop", "0", "-really-quiet", "-fs"]
160             cmd.extend(opts)
161         else:  # 'CUSTOM'
162             cmd.append(file)
163
164         # launch it
165         print("Executing command:\n  %r" % " ".join(cmd))
166
167         try:
168             process = subprocess.Popen(cmd)
169         except Exception as e:
170             import traceback
171             self.report({'ERROR'},
172                         "Couldn't run external animation player with command "
173                         "%r\n%s" % (" ".join(cmd), str(e)))
174             return {'CANCELLED'}
175
176         return {'FINISHED'}