From b90e9b69c60ff03b165985350dd74b645793f824 Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Mon, 16 Jan 2023 21:44:24 +0100 Subject: [PATCH 1/6] added more specific functions for current context into host integration --- openpype/host/host.py | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/openpype/host/host.py b/openpype/host/host.py index 94416bb39a..56d01c667a 100644 --- a/openpype/host/host.py +++ b/openpype/host/host.py @@ -100,6 +100,30 @@ class HostBase(object): pass + def get_current_project_name(self): + """ + Returns: + Union[str, None]: Current project name. + """ + + return os.environ.get("AVALON_PROJECT") + + def get_current_asset_name(self): + """ + Returns: + Union[str, None]: Current asset name. + """ + + return os.environ.get("AVALON_ASSET") + + def get_current_task_name(self): + """ + Returns: + Union[str, None]: Current task name. + """ + + return os.environ.get("AVALON_ASSET") + def get_current_context(self): """Get current context information. @@ -111,19 +135,14 @@ class HostBase(object): Default implementation returns values from 'legacy_io.Session'. Returns: - dict: Context with 3 keys 'project_name', 'asset_name' and - 'task_name'. All of them can be 'None'. + Dict[str, Union[str, None]]: Context with 3 keys 'project_name', + 'asset_name' and 'task_name'. All of them can be 'None'. """ - from openpype.pipeline import legacy_io - - if legacy_io.is_installed(): - legacy_io.install() - return { - "project_name": legacy_io.Session["AVALON_PROJECT"], - "asset_name": legacy_io.Session["AVALON_ASSET"], - "task_name": legacy_io.Session["AVALON_TASK"] + "project_name": self.get_current_project_name(), + "asset_name": self.get_current_asset_name(), + "task_name": self.get_current_task_name() } def get_context_title(self): From 2c35bda38146d35f84c3468abf64710c5fb02ba4 Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Mon, 16 Jan 2023 21:45:09 +0100 Subject: [PATCH 2/6] added functions for global access to current context --- openpype/pipeline/context_tools.py | 44 +++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/openpype/pipeline/context_tools.py b/openpype/pipeline/context_tools.py index da0ce8ecf4..50ce827a39 100644 --- a/openpype/pipeline/context_tools.py +++ b/openpype/pipeline/context_tools.py @@ -306,6 +306,42 @@ def debug_host(): return host +def get_global_context(): + return { + "project_name": os.environ.get("AVALON_PROJECT"), + "asset_name": os.environ.get("AVALON_ASSET"), + "task_name": os.environ.get("AVALON_TASK"), + } + + +def get_current_context(): + host = registered_host() + if host is not None and hasattr(host, "get_current_context"): + return host.get_current_context() + return get_global_context() + + +def get_current_project_name(): + host = registered_host() + if host is not None and hasattr(host, "get_current_project_name"): + return host.get_current_project_name() + return get_global_context()["project_name"] + + +def get_current_asset_name(): + host = registered_host() + if host is not None and hasattr(host, "get_current_asset_name"): + return host.get_current_asset_name() + return get_global_context()["asset_name"] + + +def get_current_task_name(): + host = registered_host() + if host is not None and hasattr(host, "get_current_task_name"): + return host.get_current_task_name() + return get_global_context()["task_name"] + + def get_current_project(fields=None): """Helper function to get project document based on global Session. @@ -316,7 +352,7 @@ def get_current_project(fields=None): None: Project is not set. """ - project_name = legacy_io.active_project() + project_name = get_current_project_name() return get_project(project_name, fields=fields) @@ -341,12 +377,12 @@ def get_current_project_asset(asset_name=None, asset_id=None, fields=None): None: Asset is not set or not exist. """ - project_name = legacy_io.active_project() + project_name = get_current_project_name() if asset_id: return get_asset_by_id(project_name, asset_id, fields=fields) if not asset_name: - asset_name = legacy_io.Session.get("AVALON_ASSET") + asset_name = get_current_asset_name() # Skip if is not set even on context if not asset_name: return None @@ -363,7 +399,7 @@ def is_representation_from_latest(representation): bool: Whether the representation is of latest version. """ - project_name = legacy_io.active_project() + project_name = get_current_project_name() return version_is_latest(project_name, representation["parent"]) From b75768249c422665fdd380383424326829e00748 Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Mon, 16 Jan 2023 21:45:17 +0100 Subject: [PATCH 3/6] added function to get current host name --- openpype/pipeline/context_tools.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/openpype/pipeline/context_tools.py b/openpype/pipeline/context_tools.py index 50ce827a39..06538dd91f 100644 --- a/openpype/pipeline/context_tools.py +++ b/openpype/pipeline/context_tools.py @@ -306,6 +306,22 @@ def debug_host(): return host +def get_current_host_name(): + """Current host name. + + Function is based on currently registered host integration or environment + variant 'AVALON_APP'. + + Returns: + Union[str, None]: Name of host integration in current process or None. + """ + + host = registered_host() + if host is not None and hasattr(host, "name"): + return host.name + return os.environ.get("AVALON_APP") + + def get_global_context(): return { "project_name": os.environ.get("AVALON_PROJECT"), From 48937de52ce9092d5718e9cb549764887be6c5c6 Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Mon, 16 Jan 2023 22:07:25 +0100 Subject: [PATCH 4/6] added missing import --- openpype/host/host.py | 1 + 1 file changed, 1 insertion(+) diff --git a/openpype/host/host.py b/openpype/host/host.py index 56d01c667a..28d0a21b34 100644 --- a/openpype/host/host.py +++ b/openpype/host/host.py @@ -1,3 +1,4 @@ +import os import logging import contextlib from abc import ABCMeta, abstractproperty From fb56e169dc745d6b934dcd509367dee27b91b0f3 Mon Sep 17 00:00:00 2001 From: Jakub Trllo Date: Wed, 1 Feb 2023 12:34:21 +0100 Subject: [PATCH 5/6] check for source class instead of for function by name availability --- openpype/pipeline/context_tools.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/openpype/pipeline/context_tools.py b/openpype/pipeline/context_tools.py index 06538dd91f..6610fd7da7 100644 --- a/openpype/pipeline/context_tools.py +++ b/openpype/pipeline/context_tools.py @@ -11,6 +11,7 @@ import pyblish.api from pyblish.lib import MessageHandler import openpype +from openpype.host import HostBase from openpype.client import ( get_project, get_asset_by_id, @@ -317,7 +318,7 @@ def get_current_host_name(): """ host = registered_host() - if host is not None and hasattr(host, "name"): + if isinstance(host, HostBase): return host.name return os.environ.get("AVALON_APP") @@ -332,28 +333,28 @@ def get_global_context(): def get_current_context(): host = registered_host() - if host is not None and hasattr(host, "get_current_context"): + if isinstance(host, HostBase): return host.get_current_context() return get_global_context() def get_current_project_name(): host = registered_host() - if host is not None and hasattr(host, "get_current_project_name"): + if isinstance(host, HostBase): return host.get_current_project_name() return get_global_context()["project_name"] def get_current_asset_name(): host = registered_host() - if host is not None and hasattr(host, "get_current_asset_name"): + if isinstance(host, HostBase): return host.get_current_asset_name() return get_global_context()["asset_name"] def get_current_task_name(): host = registered_host() - if host is not None and hasattr(host, "get_current_task_name"): + if isinstance(host, HostBase): return host.get_current_task_name() return get_global_context()["task_name"] From 2313cd0507c73b5175cc5f353d74db64e016c367 Mon Sep 17 00:00:00 2001 From: Jakub Trllo Date: Wed, 1 Feb 2023 12:38:54 +0100 Subject: [PATCH 6/6] added context getter functions to pipeline init --- openpype/pipeline/__init__.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/openpype/pipeline/__init__.py b/openpype/pipeline/__init__.py index f5319c5a48..7a2ef59a5a 100644 --- a/openpype/pipeline/__init__.py +++ b/openpype/pipeline/__init__.py @@ -86,6 +86,12 @@ from .context_tools import ( registered_host, deregister_host, get_process_id, + + get_current_context, + get_current_host_name, + get_current_project_name, + get_current_asset_name, + get_current_task_name, ) install = install_host uninstall = uninstall_host @@ -176,6 +182,13 @@ __all__ = ( "register_host", "registered_host", "deregister_host", + "get_process_id", + + "get_current_context", + "get_current_host_name", + "get_current_project_name", + "get_current_asset_name", + "get_current_task_name", # Backwards compatible function names "install",