From 69be5ca81f1efdcd9c245eaf044628cbc4e342d8 Mon Sep 17 00:00:00 2001 From: alexmro Date: Sun, 11 Feb 2024 22:45:17 +0100 Subject: [PATCH] Implement the runtime Just something basic that can go through the node tree --- core/runtime.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ ui/view.py | 9 +++++++++ 2 files changed, 61 insertions(+) create mode 100644 core/runtime.py diff --git a/core/runtime.py b/core/runtime.py new file mode 100644 index 0000000..0bf0afe --- /dev/null +++ b/core/runtime.py @@ -0,0 +1,52 @@ +import bpy + +from datetime import datetime + +from ..ui.nodes.nodetree import MotorNodeTree +from ..ui.nodes.onstart import MotorOnStartNode +from ..ui.nodes.console import MotorConsoleNode +from ..ui.nodes.sockets import MotorActionSocket + + +def run_scene(): + """Interpret the entire node tree and run the scene""" + on_start_nodes = [] + + for node_group in bpy.data.node_groups: + if node_group.bl_idname == MotorNodeTree.bl_idname: + for node in node_group.nodes: + if node.bl_idname == MotorOnStartNode.bl_idname: + on_start_nodes.append(node) + + for start_node in on_start_nodes: + for output in start_node.outputs: + if output.enabled and output.is_output and output.is_linked: + for link in output.links: + if link.to_node.bl_idname == MotorConsoleNode.bl_idname: + to_node = link.to_node + message = "" + if output.bl_idname == MotorActionSocket.bl_idname: + message = f"'{start_node.bl_label}' output '{output.bl_label}'" + show_time_prop = to_node.get("show_time_prop") + type_prop = to_node.get("type_prop") + + if type_prop == 3: + message = "ERROR: " + message + elif type_prop == 2: + message = "WARNING: " + message + else: + message = "INFO: " + message + + if show_time_prop: + now = datetime.now() + dt_string = now.strftime("[%d/%m/%Y %H:%M:%S] ") + + message = dt_string + message + + print(message) + + return True + + +def stop_scene(): + print("Scene stopped") diff --git a/ui/view.py b/ui/view.py index 8075c69..0cb6d20 100644 --- a/ui/view.py +++ b/ui/view.py @@ -3,8 +3,11 @@ from bpy.types import Operator from bpy.utils import register_classes_factory +from ..core.runtime import run_scene, stop_scene + class MotorRunOperator(Operator): + """Operator that enters interactive mode and runs the scene""" bl_idname = "motor.run" bl_label = "Enter interactive mode" bl_description = "Enter interactive mode and disable UI" @@ -22,6 +25,7 @@ def invoke(self, context, event): def switch_interactive_mode(enabled): + """Enter or exit interactive mode. Start or stop running the scene""" area = bpy.context.area window = bpy.context.window if area.type == 'VIEW_3D': @@ -63,6 +67,11 @@ def switch_interactive_mode(enabled): else: bpy.ops.view3d.view_lock_clear() + if enabled: + run_scene() + else: + stop_scene() + classes = ( MotorRunOperator,