Skip to content

Commit

Permalink
Adjust connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
robin-ynput committed Nov 11, 2024
1 parent 86f3c66 commit 005ed37
Showing 1 changed file with 37 additions and 30 deletions.
67 changes: 37 additions & 30 deletions client/ayon_flame/plugins/create/create_workfile.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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"
Expand All @@ -31,34 +61,16 @@ 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.
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())

Expand All @@ -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

Expand Down Expand Up @@ -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)
self._dump_instance_data(data)

0 comments on commit 005ed37

Please sign in to comment.