From 8eebae23068b316b52eec14e3f16d459c5d35c87 Mon Sep 17 00:00:00 2001 From: Crisp Ness Date: Mon, 31 Jul 2023 10:23:18 +0200 Subject: [PATCH] New add-on: IDE script executor This add-on runs scripts edited elsewhere. This is the version that checks for a path file. --- development_idee.py | 86 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 development_idee.py diff --git a/development_idee.py b/development_idee.py new file mode 100644 index 0000000..8140c16 --- /dev/null +++ b/development_idee.py @@ -0,0 +1,86 @@ +# SPDX-License-Identifier: GPL-2.0-or-later + +bl_info = { + "name": "IDE script executor", + "description": "Runs python scripts saved by IDE / external editor", + "author": "Crisp Ness", + "version": (0, 1), + "blender": (3, 6, 0), + "location": "Preferences > Add-ons", + "warning": "", + "doc_url": "https://projects.blender.org/Crisp-Ness/doc/src/branch/main/idee_0.1.md", + "tracker_url": "https://projects.blender.org/Crisp-Ness/doc/issues", + "support": "COMMUNITY", + "category": "Development", +} + +from os import path, remove +import bpy + + +def idee_onerun(): + """ + Checks for the existence of the path file. + If found: reads path to script, removes path file, runs script. + Returns timer delay for next run. + """ + prefs = bpy.context.preferences.addons[__name__].preferences + + if not path.exists(prefs.pathfile): + return prefs.delay + + with open(prefs.pathfile) as pf: + path_to_script = pf.read().strip() + remove(prefs.pathfile) + + try: + bpy.utils.execfile(path_to_script) + except BaseException: + import traceback + traceback.print_exc() + + return prefs.delay + + +class IdeePrefs(bpy.types.AddonPreferences): + bl_idname = __name__ + + pathfile: bpy.props.StringProperty( + name="Path file", + description="File containing path to script", + subtype='DIR_PATH', + default="/tmp/idee.path", + ) + delay: bpy.props.FloatProperty( + name="Delay", + description="Delay in seconds between consecutive runs", + default=1.00, + min=0.01, + max=60.0, + precision=2, + ) + + def draw(self, context): + layout = self.layout + layout.use_property_split = True + box = layout.box() + box.row().prop(self, "pathfile") + box.row().prop(self, "delay") + + +def remove_pathfile(): + prefs = bpy.context.preferences.addons[__name__].preferences + if path.exists(prefs.pathfile): + remove(prefs.pathfile) + + +def register(): + bpy.utils.register_class(IdeePrefs) + remove_pathfile() + bpy.app.timers.register(idee_onerun, persistent=True) + + +def unregister(): + bpy.app.timers.unregister(idee_onerun) + remove_pathfile() + bpy.utils.unregister_class(IdeePrefs) -- 2.30.2