Merge pull request #4324 from ynput/feature/functions_for_current_context

General: Functions for current context
This commit is contained in:
Jakub Trllo 2023-02-01 12:39:53 +01:00 committed by GitHub
commit e250ed5cf5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 100 additions and 14 deletions

View file

@ -1,3 +1,4 @@
import os
import logging
import contextlib
from abc import ABCMeta, abstractproperty
@ -100,6 +101,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 +136,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):

View file

@ -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",

View file

@ -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,
@ -306,6 +307,58 @@ 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 isinstance(host, HostBase):
return host.name
return os.environ.get("AVALON_APP")
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 isinstance(host, HostBase):
return host.get_current_context()
return get_global_context()
def get_current_project_name():
host = registered_host()
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 isinstance(host, HostBase):
return host.get_current_asset_name()
return get_global_context()["asset_name"]
def get_current_task_name():
host = registered_host()
if isinstance(host, HostBase):
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 +369,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 +394,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 +416,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"])