Launcher action to open Loader with context based on selection

Hullo,

The loader is a good tool for quickly browsing through the published product. It would be very convenience if we have a way to open the Loader with the context based on what we selected in the Launcher. I’ve created a Launcher action that do this. However, I’m facing two issues with this implementation.

  1. The Loader will crash after a few seconds
  2. The Loader won’t get to the selected context immediately. It would requires a second click to the same action to make the loader go to the right context.

Here is my code. What can I do to fix the two issues described above.

ayon-core/client/ayon_core/plugins/actions/open_loader.py

from ayon_core.pipeline import LauncherAction
from ayon_core.resources import get_resource


class ShowInLoader(LauncherAction):
    """Open AYON browser page to the current context."""
    name = "showInLoader"
    label = "Show in Loader"
    icon = get_resource("icons", "loader.png")
    order = 999
    _loader_window = None
    _project_entity = None
    _folder_entity = None


    def process(self, selection, **kwargs):
        """Launch Loader using context from the selection

        Args:
            selection (_type_): _description_
        """
        self._project_entity = selection.project_entity
        self._folder_entity = selection.folder_entity

        self.show_loader()


    def show_loader(self):
        if self._loader_window is None:
            from ayon_core.pipeline import install_ayon_plugins
            from ayon_core.tools.loader.ui import LoaderWindow

            libraryloader = LoaderWindow()
            self._loader_window = libraryloader

            install_ayon_plugins()

        self._loader_window.show()

        self._loader_window._controller.set_expected_selection(
            self._project_entity['name'],
            self._folder_entity['id'],
        )

        # Raise and activate the window
        # for MacOS
        self._loader_window.raise_()
        # for Windows
        self._loader_window.activateWindow()

What is the use case for this action? (other than saving few clicks on launching the loader)

So, your code copies the launcher_action.py and sets the expected selection after showing the window.

self._loader_window._controller.set_expected_selection(
            self._project_entity['name'],
            self._folder_entity['id'],
        )

Since the controller is a private attribute, I’d recommend not to set_expected_selection as it may cause unexpected results.

Hullo @mustafa_jafar,

You’re absolutely right! The purpose of this approach is to save a few clicks when launching the loader. Additionally, since the launcher is always open—artists use it to launch DCC—it already has the folder context, which propagates to the loader. This allows us to view the available products for a particular asset, quickly browse to the product using the file explorer, or even preview it with OpenRV.

I think this will better introduce people to the tray loader since it directly link the context of the launcher to the loader. As suppose to having to click multiple times on both windows.

Yesterday, I was able to resolve the crashing issue by using a singleton class inside

ayon-core/client/ayon_core/modules/launcher_action.py

class LoaderWindowSingleton:
    _loader_window_instance = None

    @classmethod
    def get_instance(cls):
        if cls._loader_window_instance is None:
            from ayon_core.pipeline import install_ayon_plugins
            from ayon_core.tools.loader.ui import LoaderWindow

            cls._loader_window_instance = LoaderWindow()
            install_ayon_plugins()

        return cls._loader_window_instance

This singleton class then imported by ayon-core/client/ayon_core/plugins/actions/open_loader.py.

This prevent the loader from being garbage collected.

However, I’m still stuck with the second issue where I’m unable to be in the selected context immediately, which might be relating to your comments on the controller being a private attribute.

What would be the recommended way for setting the context of the loader window in this scenario?