From da6b2b31335cefbce32289ac11fa5432666e96d8 Mon Sep 17 00:00:00 2001 From: Ondrej Samohel Date: Wed, 10 May 2023 13:22:38 +0200 Subject: [PATCH] :bug: fix use and detection of compatible integrations --- openpype/hosts/unreal/addon.py | 13 +++++- openpype/hosts/unreal/lib.py | 75 ++++++++++++++++++++++------------ 2 files changed, 61 insertions(+), 27 deletions(-) diff --git a/openpype/hosts/unreal/addon.py b/openpype/hosts/unreal/addon.py index 0c42755d37..4468ce036c 100644 --- a/openpype/hosts/unreal/addon.py +++ b/openpype/hosts/unreal/addon.py @@ -1,5 +1,8 @@ import os -from openpype.modules import OpenPypeModule, IHostAddon +from pathlib import Path + +from openpype.modules import IHostAddon, OpenPypeModule +from .lib import get_compatible_integration UNREAL_ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) @@ -19,9 +22,15 @@ class UnrealAddon(OpenPypeModule, IHostAddon): unreal_plugin_path = os.path.join( UNREAL_ROOT_DIR, "integration", f"UE_{ue_version}", "Ayon" ) + if not Path(unreal_plugin_path).exists(): + if compatible_versions := get_compatible_integration( + ue_version, Path(UNREAL_ROOT_DIR) / "integration" + ): + unreal_plugin_path = compatible_versions[-1] / "Ayon" + unreal_plugin_path = unreal_plugin_path.as_posix() if not env.get("AYON_UNREAL_PLUGIN") or \ - env.get("AYON_UNREAL_PLUGIN") != unreal_plugin_path: + env.get("AYON_UNREAL_PLUGIN") != unreal_plugin_path: env["AYON_UNREAL_PLUGIN"] = unreal_plugin_path # Set default environments if are not set via settings diff --git a/openpype/hosts/unreal/lib.py b/openpype/hosts/unreal/lib.py index 38976c3ef1..821b4daecc 100644 --- a/openpype/hosts/unreal/lib.py +++ b/openpype/hosts/unreal/lib.py @@ -321,6 +321,47 @@ def get_path_to_uat(engine_path: Path) -> Path: return engine_path / "Engine/Build/BatchFiles/RunUAT.sh" +def get_compatible_integration( + ue_version: str, integration_root: Path) -> List[Path]: + """Get path to compatible version of integration plugin. + + This will try to get the closest compatible versions to the one + specified in sorted list. + + Args: + ue_version (str): version of the current Unreal Engine. + integration_root (Path): path to built-in integration plugins. + + Returns: + list of Path: Sorted list of paths closest to the specified + version. + + """ + major, minor = ue_version.split(".") + integration_paths = [p for p in integration_root.iterdir() + if p.is_dir()] + + compatible_versions = [] + for i in integration_paths: + # parse version from path + try: + i_major, i_minor = re.search( + r"(?P\d+).(?P\d+)$", i.name).groups() + except AttributeError: + # in case there is no match, just skip to next + continue + + # consider versions with different major so different that they + # are incompatible + if int(major) != int(i_major): + continue + + compatible_versions.append(i) + + sorted(set(compatible_versions)) + return compatible_versions + + def get_path_to_cmdlet_project(ue_version: str) -> Path: cmd_project = Path( os.path.abspath(os.getenv("OPENPYPE_ROOT"))) @@ -334,31 +375,15 @@ def get_path_to_cmdlet_project(ue_version: str) -> Path: if cmd_project.exists(): return cmd_project / "CommandletProject/CommandletProject.uproject" - major, minor = ue_version.split(".") - integration_paths = [p for p in cmd_project.parent.iterdir() - if p.is_dir()] - - compatible_versions = [cmd_project] - for i in integration_paths: - - # parse version from path - i_major, i_minor = re.search( - r"(?P\d+).(?P\d+)$", i.name).groups() - - # consider versions with different major so different that they - # are incompatible - if int(major) != int(i_major): - continue - - compatible_versions.append(i) - - sorted(set(compatible_versions)) - - - - - - return cmd_project / "CommandletProject/CommandletProject.uproject" + if compatible_versions := get_compatible_integration( + ue_version, cmd_project.parent + ): + return compatible_versions[-1] / "CommandletProject/CommandletProject.uproject" # noqa: E501 + else: + raise RuntimeError( + ("There are no compatible versions of Unreal " + "integration plugin compatible with running version " + f"of Unreal Engine {ue_version}")) def get_path_to_ubt(engine_path: Path, ue_version: str) -> Path: