-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from sean1832/dev
Implement Custom Handler for send operation
- Loading branch information
Showing
11 changed files
with
150 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import bpy | ||
|
||
|
||
class CustomHandler: | ||
@staticmethod | ||
def load(text_block_name, class_name, template_url=None) -> type: | ||
text_block = bpy.data.texts.get(text_block_name) | ||
if not text_block: | ||
raise ImportError(f"Text block '{text_block_name}' not found") | ||
module = {} | ||
exec(text_block.as_string(), module) | ||
if not module: | ||
raise ImportError("Module not found.") | ||
CustomHandler = module.get(class_name, None) | ||
if not CustomHandler: | ||
refer_to_template = ( | ||
"" if not template_url else f" Please refer to template ({template_url})." | ||
) | ||
raise ImportError(f"{class_name} class not found.{refer_to_template}") | ||
return CustomHandler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Portal.blender: v0.2.0 | ||
# https://github.com/sean1832/portal.blender/blob/main/templates/recv_handler.py | ||
|
||
# This is a template for handling custom data received. | ||
# Attach this script to the 'Custom Handler' field in the 'Portal Server' panel | ||
# after selecting 'Receive' as the direction and 'Custom' as the data type. | ||
|
||
class MyRecvHandler: | ||
def __init__(self, payload, channel_name, channel_uuid): | ||
"""Constructor. (Do not modify this part)""" | ||
self.data = payload | ||
self.channel_name = channel_name | ||
self.channel_uuid = channel_uuid | ||
|
||
def handle(self) -> None: | ||
"""Handle received message.""" | ||
print(f"Received custom data: {self.data} \nfrom channel: {self.channel_uuid}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Portal.blender: v0.2.0 | ||
# https://github.com/sean1832/portal.blender/blob/main/templates/send_handler.py | ||
|
||
# This is a template for sending custom data on a specific event trigger. | ||
# Attach this script to the 'Custom Handler' field in the 'Portal Server' panel | ||
# after selecting 'Send' as the direction and 'Custom' as the event trigger type. | ||
|
||
import bpy | ||
import json | ||
import uuid | ||
|
||
class MySendEventHandler: | ||
def __init__(self, server_manager): | ||
"""Constructor. (Do not modify this part)""" | ||
self.scene_update_handler = None | ||
self.server_manager = server_manager | ||
|
||
def _send_data_on_event(self, scene): | ||
"""Send data when the event is triggered. (Do not modify this part)""" | ||
message_to_send = self._construct_data() | ||
if message_to_send: | ||
self.server_manager.data_queue.put(message_to_send) | ||
|
||
def _construct_data(self) -> str: | ||
"""Construct the custom data to be sent. Modify this part with your custom logic.""" | ||
return json.dumps({ | ||
"Key": "Insert your custom data to send here. This is just a placeholder.", | ||
"Nested": { | ||
"Array": [1, 2, 3, 4, 5], | ||
}, | ||
"UUID": str(uuid.uuid4()) | ||
}) | ||
|
||
def register(self): | ||
"""Register the event handler. Update the trigger event as needed.""" | ||
self.scene_update_handler = lambda scene: self._send_data_on_event(scene) | ||
|
||
# Modify this line to set your desired event trigger: | ||
# see doc: https://docs.blender.org/api/current/bpy.app.handlers.html#bpy.app.handlers.depsgraph_update_post | ||
bpy.app.handlers.depsgraph_update_post.append(self.scene_update_handler) # On scene update | ||
|
||
# Other trigger event examples (uncomment if needed): | ||
# --------------------------- | ||
# bpy.app.handlers.render_init.append(self.scene_update_handler) # On render start | ||
# bpy.app.handlers.render_complete.append(self.scene_update_handler) # On render completion | ||
# bpy.app.handlers.render_write.append(self.scene_update_handler) # On render frame write | ||
# bpy.app.handlers.save_post.append(self.scene_update_handler) # After saving the file | ||
|
||
def unregister(self): | ||
"""Unregister the event handler. Be sure to remove the specific event.""" | ||
if self.scene_update_handler: | ||
# Remove the active event trigger: | ||
bpy.app.handlers.depsgraph_update_post.remove(self.scene_update_handler) | ||
|
||
# Remove other events if previously registered (uncomment if used): | ||
# --------------------------- | ||
# bpy.app.handlers.render_init.remove(self.scene_update_handler) | ||
# bpy.app.handlers.render_complete.remove(self.scene_update_handler) | ||
# bpy.app.handlers.render_write.remove(self.scene_update_handler) | ||
# bpy.app.handlers.save_post.remove(self.scene_update_handler) | ||
|
||
self.scene_update_handler = None |