Merging r59113 through r59129 from trunk into soc-2013-depsgraph_mt
[blender-staging.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 == 'INTERNAL':
32         return bpy.app.binary_path
33     elif preset == 'BLENDER24':
34         player_path = "blender"
35
36         if sys.platform == "darwin":
37             test_path = "/Applications/blender 2.49.app/Contents/MacOS/blender"
38         elif sys.platform[:3] == "win":
39             test_path = "/Program Files/Blender Foundation/Blender/blender.exe"
40
41             if os.path.exists(test_path):
42                 player_path = test_path
43
44     elif preset == 'DJV':
45         player_path = "djv_view"
46
47         if sys.platform == "darwin":
48             # TODO, crummy supporting only 1 version,
49             # could find the newest installed version
50             test_path = ("/Applications/djv-0.8.2.app"
51                          "/Contents/Resources/bin/djv_view")
52             if os.path.exists(test_path):
53                 player_path = test_path
54
55     elif preset == 'FRAMECYCLER':
56         player_path = "framecycler"
57
58     elif preset == 'RV':
59         player_path = "rv"
60
61     elif preset == 'MPLAYER':
62         player_path = "mplayer"
63
64     else:
65         player_path = ""
66
67     return player_path
68
69
70 class PlayRenderedAnim(Operator):
71     """Play 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)  # UNUSED
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 is 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                 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 "#")
106                     for i, c in enumerate(file_a)))
107             del file_a, file_b, frame_tmp
108             file = bpy.path.abspath(file)  # expand '//'
109         else:
110             # works for movies and images
111             file = rd.frame_path(frame=scene.frame_start)
112             file = bpy.path.abspath(file)  # expand '//'
113             if not os.path.exists(file):
114                 self.report({'WARNING'}, "File %r not found" % file)
115
116         cmd = [player_path]
117         # extra options, fps controls etc.
118         if preset in {'BLENDER24', 'INTERNAL'}:
119             opts = ["-a",
120                     "-f", str(rd.fps), str(rd.fps_base),
121                     "-s", str(scene.frame_start),
122                     "-e", str(scene.frame_end),
123                     "-j", str(scene.frame_step),
124                     file]
125             cmd.extend(opts)
126         elif preset == 'DJV':
127             opts = [file, "-playback_speed", "%d" % int(rd.fps / rd.fps_base)]
128             cmd.extend(opts)
129         elif preset == 'FRAMECYCLER':
130             opts = [file, "%d-%d" % (scene.frame_start, scene.frame_end)]
131             cmd.extend(opts)
132         elif preset == 'RV':
133             opts = ["-fps", str(rd.fps), "-play", "[ %s ]" % file]
134             cmd.extend(opts)
135         elif preset == 'MPLAYER':
136             opts = []
137             if is_movie:
138                 opts.append(file)
139             else:
140                 opts += [("mf://%s" % file.replace("#", "?")),
141                          "-mf",
142                          "fps=%.4f" % (rd.fps / rd.fps_base),
143                          ]
144
145             opts += ["-loop", "0", "-really-quiet", "-fs"]
146             cmd.extend(opts)
147         else:  # 'CUSTOM'
148             cmd.append(file)
149
150         # launch it
151         print("Executing command:\n  %r" % " ".join(cmd))
152
153         # workaround for boost 1.46, can be eventually removed. bug: [#32350]
154         env_copy = os.environ.copy()
155         if preset == 'INTERNAL':
156             env_copy["LC_ALL"] = "C"
157         # end workaround
158
159         try:
160             subprocess.Popen(cmd, env=env_copy)
161         except Exception as e:
162             self.report({'ERROR'},
163                         "Couldn't run external animation player with command "
164                         "%r\n%s" % (" ".join(cmd), str(e)))
165             return {'CANCELLED'}
166
167         return {'FINISHED'}