From dd25190f8181cdf53902b1ef65576e8b3543650f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Thu, 17 Mar 2022 16:45:02 +0100 Subject: [PATCH] Addon: automatically adjust job name for blend file name The Flamenco job name is set to the blend file name, unless it was already set to something else. --- addon/flamenco/__init__.py | 45 ++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/addon/flamenco/__init__.py b/addon/flamenco/__init__.py index f1a4404f..9648703b 100644 --- a/addon/flamenco/__init__.py +++ b/addon/flamenco/__init__.py @@ -14,6 +14,8 @@ bl_info = { "support": "COMMUNITY", } +from pathlib import Path + __is_first_load = "operators" not in locals() if __is_first_load: from . import operators, gui, job_types, comms, preferences @@ -41,6 +43,35 @@ def redraw(self, context): context.area.tag_redraw() +def _redraw_the_world(context): + for window in context.window_manager.windows: + for area in window.screen.areas: + area.tag_redraw() + + +def _default_job_name() -> str: + if not bpy.data.filepath: + return "" + return Path(bpy.data.filepath).stem + + +@bpy.app.handlers.persistent +def _set_flamenco_job_name(a, b) -> None: + scene = bpy.context.scene + if scene.flamenco_job_name: + return + scene.flamenco_job_name = _default_job_name() + _redraw_the_world(bpy.context) + + +@bpy.app.handlers.persistent +def _unset_flamenco_job_name(a, b) -> None: + scene = bpy.context.scene + if scene.flamenco_job_name != _default_job_name(): + return + scene.flamenco_job_name = "" + + def register() -> None: from . import dependencies @@ -49,6 +80,10 @@ def register() -> None: bpy.app.handlers.load_pre.append(discard_global_flamenco_data) bpy.app.handlers.load_factory_preferences_post.append(discard_global_flamenco_data) + bpy.app.handlers.load_post.append(_set_flamenco_job_name) + bpy.app.handlers.save_pre.append(_unset_flamenco_job_name) + bpy.app.handlers.save_post.append(_set_flamenco_job_name) + bpy.types.WindowManager.flamenco_bat_status = bpy.props.EnumProperty( items=[ ("IDLE", "IDLE", "Not doing anything."), @@ -83,16 +118,16 @@ def register() -> None: update=redraw, ) + # Placeholder to contain the result of a 'ping' to Flamenco Manager, + # so that it can be shown in the preferences panel. + bpy.types.WindowManager.flamenco_status_ping = bpy.props.StringProperty() + bpy.types.Scene.flamenco_job_name = bpy.props.StringProperty( name="Flamenco Job Name", default="", description="Name of the Flamenco job; an empty name will use the blend file name as job name", ) - # Placeholder to contain the result of a 'ping' to Flamenco Manager, - # so that it can be shown in the preferences panel. - bpy.types.WindowManager.flamenco_status_ping = bpy.props.StringProperty() - preferences.register() operators.register() gui.register() @@ -104,6 +139,8 @@ def unregister() -> None: bpy.app.handlers.load_pre.remove(discard_global_flamenco_data) bpy.app.handlers.load_factory_preferences_post.remove(discard_global_flamenco_data) + _unset_flamenco_job_name() + job_types.unregister() gui.unregister() operators.unregister()