From 005ed37f1c11df8dc382a397f148cbf85fd06543 Mon Sep 17 00:00:00 2001 From: "robin@ynput.io" Date: Mon, 11 Nov 2024 13:59:46 -0500 Subject: [PATCH] Adjust connection. --- .../plugins/create/create_workfile.py | 67 ++++++++++--------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/client/ayon_flame/plugins/create/create_workfile.py b/client/ayon_flame/plugins/create/create_workfile.py index d81f2b3..033351e 100644 --- a/client/ayon_flame/plugins/create/create_workfile.py +++ b/client/ayon_flame/plugins/create/create_workfile.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- """Creator plugin for creating workfiles.""" import json +from contextlib import contextmanager from xml.etree import ElementTree as ET from ayon_core.pipeline import ( @@ -18,6 +19,35 @@ import ayon_flame.api as flapi +@contextmanager +def wiretap_connection(): + """ + Handle wiretap connection through + SDK to retrieve project metadata. + """ + wiretap_client = WireTapClient() + wiretap_client.init() + server = WireTapServerHandle("localhost:IFFFS") + + current_project = flapi.get_current_project() + project_node_handle = WireTapNodeHandle( + server, + f"/projects/{current_project.name}" + ) + + try: + yield project_node_handle + + finally: + # Force delete to avoid delayed crash + # https://help.autodesk.com/view/FLAME/2025/ENU/ + #?guid=Flame_API_Wiretap_SDK_FAQs_and_Troubleshooting_General_API_html + server.disconnect() + del wiretap_client + del project_node_handle + del server + + class CreateWorkfile(AutoCreator): """Workfile auto-creator.""" settings_category = "flame" @@ -31,19 +61,6 @@ class CreateWorkfile(AutoCreator): # https://forums.autodesk.com/t5/flame-forum/store-persistent-variable-with-flame-project/td-p/9437717 _METADATA_KEY = "Nickname" - def _get_project_metadata_handle(self): - """ Initialize project metadata setup. - - Returns: - object. Flame wiretap handle for current project - """ - wiretap_client = WireTapClient() - wiretap_client.init() - server = WireTapServerHandle("localhost:IFFFS") - - current_project = flapi.get_current_project() - project_node_handle = WireTapNodeHandle(server, f"/projects/{current_project.name}") - return wiretap_client, server, project_node_handle def _get_project_metadata(self): """ Returns the metadata stored at current project. @@ -51,14 +68,9 @@ def _get_project_metadata(self): Returns: xml.etree.ElementTree. The project metadata data. """ - client, server, handle = self._get_project_metadata_handle() - metadata = WireTapStr() - handle.getMetaData("XML", "", 1, metadata) - - server.disconnect() - del client - del handle - del server + with wiretap_connection() as handle: + metadata = WireTapStr() + handle.getMetaData("XML", "", 1, metadata) return ET.fromstring(metadata.c_str()) @@ -76,14 +88,9 @@ def _dump_instance_data(self, data): nickname_entry.text = json.dumps(data) updated = ET.tostring(metadata, encoding='unicode') - client, server, handle = self._get_project_metadata_handle() - new_metadata = WireTapStr(updated) - ok = handle.setMetaData("XML", new_metadata.c_str()) - - server.disconnect() - del client - del handle - del server + with wiretap_connection() as handle: + new_metadata = WireTapStr(updated) + ok = handle.setMetaData("XML", new_metadata.c_str()) return ok @@ -172,4 +179,4 @@ def update_instances(self, update_list): """ for created_inst, _ in update_list: data = created_inst.data_to_store() - self._dump_instance_data(data) \ No newline at end of file + self._dump_instance_data(data)