From 0ebd4435e50e82657a06bb3c229f6e3b68af5697 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 17 Mar 2017 15:08:50 +0100 Subject: [PATCH] Flamenco: when opening non-existing file path, open parent instead This is very useful for opening render output directories from Blender, as those will only exist after rendering has started. --- blender_cloud/flamenco/__init__.py | 34 ++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/blender_cloud/flamenco/__init__.py b/blender_cloud/flamenco/__init__.py index ef0614f..f1b1231 100644 --- a/blender_cloud/flamenco/__init__.py +++ b/blender_cloud/flamenco/__init__.py @@ -364,8 +364,13 @@ class FLAMENCO_OT_copy_files(Operator, bpy.context.window_manager.flamenco_status = 'IDLE' -class FLAMENCO_OT_explore_file_path(Operator): - """Opens the Flamenco job storage path in a file explorer.""" +class FLAMENCO_OT_explore_file_path(FlamencoPollMixin, + Operator): + """Opens the Flamenco job storage path in a file explorer. + + If the path cannot be found, this operator tries to open its parent. + """ + bl_idname = 'flamenco.explore_file_path' bl_label = 'Open in file explorer' bl_description = __doc__.rstrip('.') @@ -374,15 +379,30 @@ class FLAMENCO_OT_explore_file_path(Operator): def execute(self, context): import platform - import subprocess - import os + import pathlib + + # Possibly open a parent of the path + to_open = pathlib.Path(self.path) + while to_open.parent != to_open: # while we're not at the root + if to_open.exists(): + break + to_open = to_open.parent + else: + self.report({'ERROR'}, 'Unable to open %s or any of its parents.' % self.path) + return {'CANCELLED'} + to_open = str(to_open) if platform.system() == "Windows": - os.startfile(self.path) + import os + os.startfile(to_open) + elif platform.system() == "Darwin": - subprocess.Popen(["open", self.path]) + import subprocess + subprocess.Popen(["open", to_open]) + else: - subprocess.Popen(["xdg-open", self.path]) + import subprocess + subprocess.Popen(["xdg-open", to_open]) return {'FINISHED'}