screen_play_rendered_anim.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. # <pep8-80 compliant>
  19. # Originally written by Matt Ebb
  20. import bpy
  21. from bpy.types import Operator
  22. import os
  23. from bpy.app.translations import pgettext_tip as tip_
  24. def guess_player_path(preset):
  25. import sys
  26. if preset == 'INTERNAL':
  27. return bpy.app.binary_path
  28. elif preset == 'BLENDER24':
  29. player_path = "blender"
  30. if sys.platform == "darwin":
  31. test_path = "/Applications/blender 2.49.app/Contents/MacOS/blender"
  32. elif sys.platform[:3] == "win":
  33. test_path = "/Program Files/Blender Foundation/Blender/blender.exe"
  34. if os.path.exists(test_path):
  35. player_path = test_path
  36. elif preset == 'DJV':
  37. player_path = "djv_view"
  38. if sys.platform == "darwin":
  39. # TODO, crummy supporting only 1 version,
  40. # could find the newest installed version
  41. test_path = ("/Applications/djv-0.8.2.app"
  42. "/Contents/Resources/bin/djv_view")
  43. if os.path.exists(test_path):
  44. player_path = test_path
  45. elif preset == 'FRAMECYCLER':
  46. player_path = "framecycler"
  47. elif preset == 'RV':
  48. player_path = "rv"
  49. elif preset == 'MPLAYER':
  50. player_path = "mplayer"
  51. else:
  52. player_path = ""
  53. return player_path
  54. class PlayRenderedAnim(Operator):
  55. """Play back rendered frames/movies using an external player"""
  56. bl_idname = "render.play_rendered_anim"
  57. bl_label = "Play Rendered Animation"
  58. bl_options = {'REGISTER'}
  59. def execute(self, context):
  60. import subprocess
  61. from shlex import quote
  62. scene = context.scene
  63. rd = scene.render
  64. prefs = context.preferences
  65. fps_final = rd.fps / rd.fps_base
  66. preset = prefs.filepaths.animation_player_preset
  67. player_path = prefs.filepaths.animation_player
  68. # file_path = bpy.path.abspath(rd.filepath) # UNUSED
  69. is_movie = rd.is_movie_format
  70. # try and guess a command line if it doesn't exist
  71. if player_path == "":
  72. player_path = guess_player_path(preset)
  73. if is_movie is False and preset in {'FRAMECYCLER', 'RV', 'MPLAYER'}:
  74. # replace the number with '#'
  75. file_a = rd.frame_path(frame=0)
  76. # TODO, make an api call for this
  77. frame_tmp = 9
  78. file_b = rd.frame_path(frame=frame_tmp)
  79. while len(file_a) == len(file_b):
  80. frame_tmp = (frame_tmp * 10) + 9
  81. file_b = rd.frame_path(frame=frame_tmp)
  82. file_b = rd.frame_path(frame=int(frame_tmp / 10))
  83. file = ("".join((c if file_b[i] == c else "#")
  84. for i, c in enumerate(file_a)))
  85. del file_a, file_b, frame_tmp
  86. file = bpy.path.abspath(file) # expand '//'
  87. else:
  88. path_valid = True
  89. # works for movies and images
  90. file = rd.frame_path(frame=scene.frame_start, preview=scene.use_preview_range)
  91. file = bpy.path.abspath(file) # expand '//'
  92. if not os.path.exists(file):
  93. err_msg = tip_("File %r not found") % file
  94. self.report({'WARNING'}, err_msg)
  95. path_valid = False
  96. # one last try for full range if we used preview range
  97. if scene.use_preview_range and not path_valid:
  98. file = rd.frame_path(frame=scene.frame_start, preview=False)
  99. file = bpy.path.abspath(file) # expand '//'
  100. err_msg = tip_("File %r not found") % file
  101. if not os.path.exists(file):
  102. self.report({'WARNING'}, err_msg)
  103. cmd = [player_path]
  104. # extra options, fps controls etc.
  105. if scene.use_preview_range:
  106. frame_start = scene.frame_preview_start
  107. frame_end = scene.frame_preview_end
  108. else:
  109. frame_start = scene.frame_start
  110. frame_end = scene.frame_end
  111. if preset == 'INTERNAL':
  112. opts = [
  113. "-a",
  114. "-f", str(rd.fps), str(rd.fps_base),
  115. "-s", str(frame_start),
  116. "-e", str(frame_end),
  117. "-j", str(scene.frame_step),
  118. file,
  119. ]
  120. cmd.extend(opts)
  121. elif preset == 'DJV':
  122. opts = [file, "-playback_speed", str(int(fps_final))]
  123. cmd.extend(opts)
  124. elif preset == 'FRAMECYCLER':
  125. opts = [file, f"{scene.frame_start:d}-{scene.frame_end:d}"]
  126. cmd.extend(opts)
  127. elif preset == 'RV':
  128. opts = ["-fps", str(rd.fps), "-play", f"[ {file:s} ]"]
  129. cmd.extend(opts)
  130. elif preset == 'MPLAYER':
  131. opts = []
  132. if is_movie:
  133. opts.append(file)
  134. else:
  135. opts += [
  136. ("mf://" + file.replace("#", "?")),
  137. "-mf",
  138. f"fps={fps_final:4f}"
  139. ]
  140. opts += ["-loop", "0", "-really-quiet", "-fs"]
  141. cmd.extend(opts)
  142. else: # 'CUSTOM'
  143. cmd.append(file)
  144. # launch it
  145. print("Executing command:\n ", " ".join(quote(c) for c in cmd))
  146. # workaround for boost 1.46, can be eventually removed. bug: [#32350]
  147. env_copy = os.environ.copy()
  148. if preset == 'INTERNAL':
  149. env_copy["LC_ALL"] = "C"
  150. # end workaround
  151. try:
  152. subprocess.Popen(cmd, env=env_copy)
  153. except Exception as e:
  154. err_msg = tip_("Couldn't run external animation player with command %r\n%s") % (cmd, e)
  155. self.report(
  156. {'ERROR'},
  157. err_msg,
  158. )
  159. return {'CANCELLED'}
  160. return {'FINISHED'}
  161. classes = (
  162. PlayRenderedAnim,
  163. )