Addon: add file browser buttons to file/directory path settings/properties

This commit is contained in:
Sybren A. Stüvel 2022-03-15 18:45:28 +01:00
parent 6764ee8259
commit 59f58d92b8
2 changed files with 65 additions and 5 deletions

View File

@ -41,6 +41,8 @@ class FLAMENCO_PT_job_submission(bpy.types.Panel):
col.prop(context.scene, "flamenco_job_name", text="Job Name")
row = col.row(align=True)
row.prop(context.scene, "flamenco_job_storage", text="Job Storage")
prop = row.operator("flamenco3.explore_file_path", text="", icon="WINDOW")
prop.path = context.scene.flamenco_job_storage
layout.separator()
@ -88,10 +90,19 @@ class FLAMENCO_PT_job_submission(bpy.types.Panel):
if not setting.get("visible", True):
return
row = layout.row(align=True)
if setting.get("editable", True):
self.draw_setting_editable(layout, propgroup, setting)
self.draw_setting_editable(row, propgroup, setting)
else:
self.draw_setting_readonly(context, layout, propgroup, setting)
self.draw_setting_readonly(context, row, propgroup, setting)
if str(setting.type) == "string" and str(setting.get("subtype", "")) in {
"dir_path",
"file_path",
"hashed_file_path",
}:
op = row.operator("flamenco3.explore_file_path", text="", icon="WINDOW")
op.path = getattr(propgroup, setting.key)
def draw_setting_editable(
self,
@ -99,13 +110,12 @@ class FLAMENCO_PT_job_submission(bpy.types.Panel):
propgroup: JobTypePropertyGroup,
setting: _AvailableJobSetting,
) -> None:
row = layout.row(align=True)
row.prop(propgroup, setting.key)
layout.prop(propgroup, setting.key)
setting_eval = setting.get("eval", "")
if not setting_eval:
return
props = row.operator("flamenco.eval_setting", text="", icon="SCRIPTPLUGINS")
props = layout.operator("flamenco.eval_setting", text="", icon="SCRIPTPLUGINS")
props.setting_key = setting.key
props.setting_eval = setting_eval

View File

@ -305,10 +305,60 @@ class FLAMENCO_OT_submit_job(FlamencoOpMixin, bpy.types.Operator):
return {"FINISHED"}
class FLAMENCO3_OT_explore_file_path(bpy.types.Operator):
"""Opens the given path in a file explorer.
If the path cannot be found, this operator tries to open its parent.
"""
bl_idname = "flamenco3.explore_file_path"
bl_label = "Open in file explorer"
bl_description = __doc__.rstrip(".")
path: bpy.props.StringProperty(
name="Path", description="Path to explore", subtype="DIR_PATH"
)
def execute(self, context):
import platform
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":
import os
os.startfile(to_open)
elif platform.system() == "Darwin":
import subprocess
subprocess.Popen(["open", to_open])
else:
import subprocess
subprocess.Popen(["xdg-open", to_open])
return {"FINISHED"}
classes = (
FLAMENCO_OT_fetch_job_types,
FLAMENCO_OT_ping_manager,
FLAMENCO_OT_eval_setting,
FLAMENCO_OT_submit_job,
FLAMENCO3_OT_explore_file_path,
)
register, unregister = bpy.utils.register_classes_factory(classes)