diff --git a/src/bonsai/bonsai/bim/module/demo/__init__.py b/src/bonsai/bonsai/bim/module/demo/__init__.py index 2dfb3941ae..705e0be046 100644 --- a/src/bonsai/bonsai/bim/module/demo/__init__.py +++ b/src/bonsai/bonsai/bim/module/demo/__init__.py @@ -36,8 +36,10 @@ from . import ui, prop, operator classes = ( operator.DemonstrateHelloWorld, operator.DemonstrateRenameProject, + operator.SendWebUiDemoMessage, prop.BIMDemoProperties, ui.BIM_PT_demo, + ui.BIM_PT_webui_demo, ) diff --git a/src/bonsai/bonsai/bim/module/demo/operator.py b/src/bonsai/bonsai/bim/module/demo/operator.py index dcc54a2a7e..7d8c98de3f 100644 --- a/src/bonsai/bonsai/bim/module/demo/operator.py +++ b/src/bonsai/bonsai/bim/module/demo/operator.py @@ -76,3 +76,17 @@ class DemonstrateRenameProject(bpy.types.Operator, tool.Ifc.Operator): # properties (such as an input field) or data from the scene (like the # actively selected object). core.demonstrate_rename_project(tool.Ifc, tool.Demo, name=bpy.context.scene.BIMDemoProperties.name) + + +class SendWebUiDemoMessage(bpy.types.Operator): + bl_idname = "bim.send_webui_demo_message" + bl_label = "Send Web UI Demo Message" + bl_description = "Sends a demo message to the currently connected Web UI" + + def execute(self, context): + # First, we need to make sure that we are connected to the Web UI + # We do that by checking the Web module properties' is_connected prop + # and calling the operator connect_websocket_server if we aren't connected to a Web UI + if not context.scene.WebProperties.is_connected: + bpy.ops.bim.connect_websocket_server(page="demo") + core.send_webui_demo_message(tool.Web, message=bpy.context.scene.BIMDemoProperties.webui_message) diff --git a/src/bonsai/bonsai/bim/module/demo/prop.py b/src/bonsai/bonsai/bim/module/demo/prop.py index 71b304f65c..6c6141a66c 100644 --- a/src/bonsai/bonsai/bim/module/demo/prop.py +++ b/src/bonsai/bonsai/bim/module/demo/prop.py @@ -59,3 +59,4 @@ class BIMDemoProperties(PropertyGroup): # an input text field in the ui.py. message: StringProperty(name="Message") show_hints: BoolProperty(name="Show Hints", default=False) + webui_message: StringProperty(name="Web UI Message", default="Hello, Web UI!") diff --git a/src/bonsai/bonsai/bim/module/demo/ui.py b/src/bonsai/bonsai/bim/module/demo/ui.py index d401a8aa35..5d6c62ac7e 100644 --- a/src/bonsai/bonsai/bim/module/demo/ui.py +++ b/src/bonsai/bonsai/bim/module/demo/ui.py @@ -130,3 +130,27 @@ class BIM_PT_demo(bpy.types.Panel): if self.props.show_hints: row = self.layout.row() row.label(text="Name cannot be blank!") + + +class BIM_PT_webui_demo(bpy.types.Panel): + bl_label = "Web UI Demo" + bl_idname = "BIM_PT_webui_demo" + bl_space_type = "PROPERTIES" + bl_region_type = "WINDOW" + bl_context = "scene" + # we specify that this panel is closed by default + bl_options = {"DEFAULT_CLOSED"} + # here we reference the demo panel to create a sub panel + # for the web UI demo + bl_parent_id = "BIM_PT_demo" + + def draw(self, context): + self.props = context.scene.BIMDemoProperties + + row = self.layout.row() + # we create an input field for the property webui_message + row.prop(self.props, "webui_message") + + row = self.layout.row() + # here we create a button referencing the operator send_webui_message + row.operator("bim.send_webui_demo_message") diff --git a/src/bonsai/bonsai/core/demo.py b/src/bonsai/bonsai/core/demo.py index 949a389157..77632d505e 100644 --- a/src/bonsai/bonsai/core/demo.py +++ b/src/bonsai/bonsai/core/demo.py @@ -72,3 +72,9 @@ def demonstrate_rename_project(ifc, demo, name=None): demo.hide_user_hints() else: demo.show_user_hints() + + +# here this core function uses the web tool to send a message to the web UI +# we call the send_webui_data method to send any kind of data to the web UI +def send_webui_demo_message(web, message="Hello"): + web.send_webui_data(data=message, data_key="demo_message", event="demo_data")