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