Skip to content

loader

API for actions for loader tool.

Even though the api is meant for the loader tool, the api should be possible to use in a standalone way out of the loader tool.

To use add actions, make sure your addon does inherit from 'IPluginPaths' and implements 'get_loader_action_plugin_paths' which returns paths to python files with loader actions.

The plugin is used to collect available actions for the given context and to execute them. Selection is defined with 'LoaderActionSelection' object that also contains a cache of entities and project anatomy.

Implementing 'get_action_items' allows the plugin to define what actions are shown and available for the selection. Because for a single selection can be shown multiple actions with the same action identifier, the action items also have 'data' attribute which can be used to store additional data for the action (they have to be json-serializable).

The action is triggered by calling the 'execute_action' method. Which takes the action identifier, the selection, the additional data from the action item and form values from the form if any.

Using 'LoaderActionResult' as the output of 'execute_action' can trigger to show a message in UI or to show an additional form ('ActionForm') which would retrigger the action with the values from the form on submitting. That allows handling of multistep actions.

It is also recommended that the plugin does override the 'identifier' attribute. The identifier has to be unique across all plugins. Class name is used by default.

The selection wrapper currently supports the following types of entity types
  • version
  • representation

It is planned to add 'folder' and 'task' selection in the future.

It is possible to trigger 'execute_action' without ever calling

'get_action_items', that can be handy in automations.

The whole logic is wrapped into 'LoaderActionsContext'. It takes care of the discovery of plugins and wraps the collection and execution of action items. Method 'execute_action' on context also requires plugin identifier.

The flow of the logic is (in the loader tool): 1. User selects entities in the UI. 2. Right-click the selected entities. 3. Use 'LoaderActionsContext' to collect items using 'get_action_items'. 4. Show a menu (with submenus) in the UI. 5. If a user selects an action, the action is triggered using 'execute_action'. 5a. If the action returns 'LoaderActionResult', show a 'message' if it is filled and show a form dialog if 'form' is filled. 5b. If the user submitted the form, trigger the action again with the values from the form and repeat from 5a.

LoaderActionItem dataclass

Item of loader action.

Action plugins return these items as possible actions to run for a given context.

Because the action item can be related to a specific entity and not the whole selection, they also have to define the entity type and ids to be executed on.

Attributes:

Name Type Description
label str

Text shown in UI.

order int

Order of the action in UI.

group_label Optional[str]

Label of the group to which the action belongs.

icon Optional[dict[str, Any]

Icon definition.

data Optional[DataType]

Action item data.

identifier Optional[str]

Identifier of the plugin which created the action item. Is filled automatically. Is not changed if is filled -> can lead to different plugin.

Source code in client/ayon_core/pipeline/actions/loader.py
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
@dataclass
class LoaderActionItem:
    """Item of loader action.

    Action plugins return these items as possible actions to run for a given
        context.

    Because the action item can be related to a specific entity
        and not the whole selection, they also have to define the entity type
        and ids to be executed on.

    Attributes:
        label (str): Text shown in UI.
        order (int): Order of the action in UI.
        group_label (Optional[str]): Label of the group to which the action
            belongs.
        icon (Optional[dict[str, Any]): Icon definition.
        data (Optional[DataType]): Action item data.
        identifier (Optional[str]): Identifier of the plugin which
            created the action item. Is filled automatically. Is not changed
            if is filled -> can lead to different plugin.

    """
    label: str
    order: int = 0
    group_label: Optional[str] = None
    icon: Optional[dict[str, Any]] = None
    data: Optional[DataType] = None
    # Is filled automatically
    identifier: str = None

LoaderActionPlugin

Bases: ABC

Plugin for loader actions.

Plugin is responsible for getting action items and executing actions.

Source code in client/ayon_core/pipeline/actions/loader.py
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
class LoaderActionPlugin(ABC):
    """Plugin for loader actions.

    Plugin is responsible for getting action items and executing actions.

    """
    _log: Optional[logging.Logger] = None
    enabled: bool = True

    def __init__(self, context: "LoaderActionsContext") -> None:
        self._context = context
        self.apply_settings(context.get_studio_settings())

    def apply_settings(self, studio_settings: dict[str, Any]) -> None:
        """Apply studio settings to the plugin.

        Args:
            studio_settings (dict[str, Any]): Studio settings.

        """
        pass

    @property
    def log(self) -> logging.Logger:
        if self._log is None:
            self._log = Logger.get_logger(self.__class__.__name__)
        return self._log

    @property
    def identifier(self) -> str:
        """Identifier of the plugin.

        Returns:
            str: Plugin identifier.

        """
        return self.__class__.__name__

    @property
    def host_name(self) -> Optional[str]:
        """Name of the current host."""
        return self._context.get_host_name()

    @abstractmethod
    def get_action_items(
        self, selection: LoaderActionSelection
    ) -> list[LoaderActionItem]:
        """Action items for the selection.

        Args:
            selection (LoaderActionSelection): Selection.

        Returns:
            list[LoaderActionItem]: Action items.

        """
        pass

    @abstractmethod
    def execute_action(
        self,
        selection: LoaderActionSelection,
        data: Optional[DataType],
        form_values: dict[str, Any],
    ) -> Optional[LoaderActionResult]:
        """Execute an action.

        Args:
            selection (LoaderActionSelection): Selection wrapper. Can be used
                to get entities or get context of original selection.
            data (Optional[DataType]): Additional action item data.
            form_values (dict[str, Any]): Attribute values.

        Returns:
            Optional[LoaderActionResult]: Result of the action execution.

        """
        pass

host_name property

Name of the current host.

identifier property

Identifier of the plugin.

Returns:

Name Type Description
str str

Plugin identifier.

apply_settings(studio_settings)

Apply studio settings to the plugin.

Parameters:

Name Type Description Default
studio_settings dict[str, Any]

Studio settings.

required
Source code in client/ayon_core/pipeline/actions/loader.py
564
565
566
567
568
569
570
571
def apply_settings(self, studio_settings: dict[str, Any]) -> None:
    """Apply studio settings to the plugin.

    Args:
        studio_settings (dict[str, Any]): Studio settings.

    """
    pass

execute_action(selection, data, form_values) abstractmethod

Execute an action.

Parameters:

Name Type Description Default
selection LoaderActionSelection

Selection wrapper. Can be used to get entities or get context of original selection.

required
data Optional[DataType]

Additional action item data.

required
form_values dict[str, Any]

Attribute values.

required

Returns:

Type Description
Optional[LoaderActionResult]

Optional[LoaderActionResult]: Result of the action execution.

Source code in client/ayon_core/pipeline/actions/loader.py
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
@abstractmethod
def execute_action(
    self,
    selection: LoaderActionSelection,
    data: Optional[DataType],
    form_values: dict[str, Any],
) -> Optional[LoaderActionResult]:
    """Execute an action.

    Args:
        selection (LoaderActionSelection): Selection wrapper. Can be used
            to get entities or get context of original selection.
        data (Optional[DataType]): Additional action item data.
        form_values (dict[str, Any]): Attribute values.

    Returns:
        Optional[LoaderActionResult]: Result of the action execution.

    """
    pass

get_action_items(selection) abstractmethod

Action items for the selection.

Parameters:

Name Type Description Default
selection LoaderActionSelection

Selection.

required

Returns:

Type Description
list[LoaderActionItem]

list[LoaderActionItem]: Action items.

Source code in client/ayon_core/pipeline/actions/loader.py
594
595
596
597
598
599
600
601
602
603
604
605
606
607
@abstractmethod
def get_action_items(
    self, selection: LoaderActionSelection
) -> list[LoaderActionItem]:
    """Action items for the selection.

    Args:
        selection (LoaderActionSelection): Selection.

    Returns:
        list[LoaderActionItem]: Action items.

    """
    pass

LoaderActionResult dataclass

Result of loader action execution.

Attributes:

Name Type Description
message Optional[str]

Message to show in UI.

success bool

If the action was successful. Affects color of the message.

form Optional[ActionForm]

Form to show in UI.

form_values Optional[dict[str, Any]]

Values for the form. Can be used if the same form is re-shown e.g. because a user forgot to fill a required field.

Source code in client/ayon_core/pipeline/actions/loader.py
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
@dataclass
class LoaderActionResult:
    """Result of loader action execution.

    Attributes:
        message (Optional[str]): Message to show in UI.
        success (bool): If the action was successful. Affects color of
            the message.
        form (Optional[ActionForm]): Form to show in UI.
        form_values (Optional[dict[str, Any]]): Values for the form. Can be
            used if the same form is re-shown e.g. because a user forgot to
            fill a required field.

    """
    message: Optional[str] = None
    success: bool = True
    form: Optional[ActionForm] = None
    form_values: Optional[dict[str, Any]] = None

    def to_json_data(self) -> dict[str, Any]:
        form = self.form
        if form is not None:
            form = form.to_json_data()
        return {
            "message": self.message,
            "success": self.success,
            "form": form,
            "form_values": self.form_values,
        }

    @classmethod
    def from_json_data(cls, data: dict[str, Any]) -> "LoaderActionResult":
        form = data["form"]
        if form is not None:
            data["form"] = ActionForm.from_json_data(form)
        return LoaderActionResult(**data)

LoaderActionSelection

Selection of entities for loader actions.

Selection tells action plugins what exactly is selected in the tool and which ids.

Contains entity cache which can be used to get entities by their ids. Or to get project settings and anatomy.

Source code in client/ayon_core/pipeline/actions/loader.py
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
class LoaderActionSelection:
    """Selection of entities for loader actions.

    Selection tells action plugins what exactly is selected in the tool and
        which ids.

    Contains entity cache which can be used to get entities by their ids. Or
        to get project settings and anatomy.

    """
    def __init__(
        self,
        project_name: str,
        selected_ids: set[str],
        selected_type: LoaderSelectedType,
        *,
        project_anatomy: Optional[Anatomy] = None,
        project_settings: Optional[dict[str, Any]] = None,
        entities_cache: Optional[SelectionEntitiesCache] = None,
    ):
        self._project_name = project_name
        self._selected_ids = selected_ids
        self._selected_type = selected_type

        self._project_anatomy = project_anatomy
        self._project_settings = project_settings

        if entities_cache is None:
            entities_cache = SelectionEntitiesCache(project_name)
        self._entities_cache = entities_cache

    def get_entities_cache(self) -> SelectionEntitiesCache:
        return self._entities_cache

    def get_project_name(self) -> str:
        return self._project_name

    def get_selected_ids(self) -> set[str]:
        return set(self._selected_ids)

    def get_selected_type(self) -> str:
        return self._selected_type

    def get_project_settings(self) -> dict[str, Any]:
        if self._project_settings is None:
            self._project_settings = get_project_settings(self._project_name)
        return copy.deepcopy(self._project_settings)

    def get_project_anatomy(self) -> Anatomy:
        if self._project_anatomy is None:
            self._project_anatomy = Anatomy(
                self._project_name,
                project_entity=self.get_entities_cache().get_project(),
            )
        return self._project_anatomy

    project_name = property(get_project_name)
    selected_ids = property(get_selected_ids)
    selected_type = property(get_selected_type)
    project_settings = property(get_project_settings)
    project_anatomy = property(get_project_anatomy)
    entities = property(get_entities_cache)

    # --- Helper methods ---
    def versions_selected(self) -> bool:
        """Selected entity type is version.

        Returns:
            bool: True if selected entity type is version.

        """
        return self._selected_type == LoaderSelectedType.version

    def representations_selected(self) -> bool:
        """Selected entity type is representation.

        Returns:
            bool: True if selected entity type is representation.

        """
        return self._selected_type == LoaderSelectedType.representation

    def get_selected_version_entities(self) -> list[dict[str, Any]]:
        """Retrieve selected version entities.

        An empty list is returned if 'version' is not the selected
            entity type.

        Returns:
            list[dict[str, Any]]: List of selected version entities.

        """
        if self.versions_selected():
            return self.entities.get_versions(self.selected_ids)
        return []

    def get_selected_representation_entities(self) -> list[dict[str, Any]]:
        """Retrieve selected representation entities.

        An empty list is returned if 'representation' is not the selected
            entity type.

        Returns:
            list[dict[str, Any]]: List of selected representation entities.

        """
        if self.representations_selected():
            return self.entities.get_representations(self.selected_ids)
        return []

get_selected_representation_entities()

Retrieve selected representation entities.

An empty list is returned if 'representation' is not the selected entity type.

Returns:

Type Description
list[dict[str, Any]]

list[dict[str, Any]]: List of selected representation entities.

Source code in client/ayon_core/pipeline/actions/loader.py
466
467
468
469
470
471
472
473
474
475
476
477
478
def get_selected_representation_entities(self) -> list[dict[str, Any]]:
    """Retrieve selected representation entities.

    An empty list is returned if 'representation' is not the selected
        entity type.

    Returns:
        list[dict[str, Any]]: List of selected representation entities.

    """
    if self.representations_selected():
        return self.entities.get_representations(self.selected_ids)
    return []

get_selected_version_entities()

Retrieve selected version entities.

An empty list is returned if 'version' is not the selected entity type.

Returns:

Type Description
list[dict[str, Any]]

list[dict[str, Any]]: List of selected version entities.

Source code in client/ayon_core/pipeline/actions/loader.py
452
453
454
455
456
457
458
459
460
461
462
463
464
def get_selected_version_entities(self) -> list[dict[str, Any]]:
    """Retrieve selected version entities.

    An empty list is returned if 'version' is not the selected
        entity type.

    Returns:
        list[dict[str, Any]]: List of selected version entities.

    """
    if self.versions_selected():
        return self.entities.get_versions(self.selected_ids)
    return []

representations_selected()

Selected entity type is representation.

Returns:

Name Type Description
bool bool

True if selected entity type is representation.

Source code in client/ayon_core/pipeline/actions/loader.py
443
444
445
446
447
448
449
450
def representations_selected(self) -> bool:
    """Selected entity type is representation.

    Returns:
        bool: True if selected entity type is representation.

    """
    return self._selected_type == LoaderSelectedType.representation

versions_selected()

Selected entity type is version.

Returns:

Name Type Description
bool bool

True if selected entity type is version.

Source code in client/ayon_core/pipeline/actions/loader.py
434
435
436
437
438
439
440
441
def versions_selected(self) -> bool:
    """Selected entity type is version.

    Returns:
        bool: True if selected entity type is version.

    """
    return self._selected_type == LoaderSelectedType.version

LoaderActionsContext

Wrapper for loader actions and their logic.

Takes care about the public api of loader actions and internal logic like discovery and initialization of plugins.

Source code in client/ayon_core/pipeline/actions/loader.py
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
class LoaderActionsContext:
    """Wrapper for loader actions and their logic.

    Takes care about the public api of loader actions and internal logic like
        discovery and initialization of plugins.

    """
    def __init__(
        self,
        studio_settings: Optional[dict[str, Any]] = None,
        addons_manager: Optional[AddonsManager] = None,
        host: Optional[AbstractHost] = _PLACEHOLDER,
    ) -> None:
        self._log = Logger.get_logger(self.__class__.__name__)

        self._addons_manager = addons_manager
        self._host = host

        # Attributes that are re-cached on reset
        self._studio_settings = studio_settings
        self._plugins = None

    def reset(
        self, studio_settings: Optional[dict[str, Any]] = None
    ) -> None:
        """Reset context cache.

        Reset plugins and studio settings to reload them.

        Notes:
             Does not reset the cache of AddonsManger because there should not
                be a reason to do so.

        """
        self._studio_settings = studio_settings
        self._plugins = None

    def get_addons_manager(self) -> AddonsManager:
        if self._addons_manager is None:
            self._addons_manager = AddonsManager(
                settings=self.get_studio_settings()
            )
        return self._addons_manager

    def get_host(self) -> Optional[AbstractHost]:
        """Get current host integration.

        Returns:
            Optional[AbstractHost]: Host integration. Can be None if host
                integration is not registered -> probably not used in the
                host integration process.

        """
        if self._host is _PLACEHOLDER:
            from ayon_core.pipeline import registered_host

            self._host = registered_host()
        return self._host

    def get_host_name(self) -> Optional[str]:
        host = self.get_host()
        if host is None:
            return None
        return host.name

    def get_studio_settings(self) -> dict[str, Any]:
        if self._studio_settings is None:
            self._studio_settings = get_studio_settings()
        return copy.deepcopy(self._studio_settings)

    def get_action_items(
        self, selection: LoaderActionSelection
    ) -> list[LoaderActionItem]:
        """Collect action items from all plugins for given selection.

        Args:
            selection (LoaderActionSelection): Selection wrapper.

        """
        output = []
        for plugin_id, plugin in self._get_plugins().items():
            try:
                for action_item in plugin.get_action_items(selection):
                    if action_item.identifier is None:
                        action_item.identifier = plugin_id
                    output.append(action_item)

            except Exception:
                self._log.warning(
                    "Failed to get action items for"
                    f" plugin '{plugin.identifier}'",
                    exc_info=True,
                )
        return output

    def execute_action(
        self,
        identifier: str,
        selection: LoaderActionSelection,
        data: Optional[DataType],
        form_values: dict[str, Any],
    ) -> Optional[LoaderActionResult]:
        """Trigger action execution.

        Args:
            identifier (str): Identifier of the plugin.
            selection (LoaderActionSelection): Selection wrapper. Can be used
                to get what is selected in UI and to get access to entity
                cache.
            data (Optional[DataType]): Additional action item data.
            form_values (dict[str, Any]): Form values related to action.
                Usually filled if action returned response with form.

        """
        plugins_by_id = self._get_plugins()
        plugin = plugins_by_id[identifier]
        return plugin.execute_action(
            selection,
            data,
            form_values,
        )

    def _get_plugins(self) -> dict[str, LoaderActionPlugin]:
        if self._plugins is None:
            host_name = self.get_host_name()
            addons_manager = self.get_addons_manager()
            all_paths = [
                os.path.join(AYON_CORE_ROOT, "plugins", "loader")
            ]
            for addon in addons_manager.addons:
                if not isinstance(addon, IPluginPaths):
                    continue

                try:
                    if is_func_signature_supported(
                        addon.get_loader_action_plugin_paths,
                        host_name
                    ):
                        paths = addon.get_loader_action_plugin_paths(
                            host_name
                        )
                    else:
                        paths = addon.get_loader_action_plugin_paths()
                except Exception:
                    self._log.warning(
                        "Failed to get plugin paths for addon",
                        exc_info=True
                    )
                    continue

                if paths:
                    all_paths.extend(paths)

            result = discover_plugins(LoaderActionPlugin, all_paths)
            result.log_report()
            plugins = {}
            for cls in result.plugins:
                try:
                    plugin = cls(self)
                    if not plugin.enabled:
                        continue

                    plugin_id = plugin.identifier
                    if plugin_id not in plugins:
                        plugins[plugin_id] = plugin
                        continue

                    self._log.warning(
                        f"Duplicated plugins identifier found '{plugin_id}'."
                    )

                except Exception:
                    self._log.warning(
                        f"Failed to initialize plugin '{cls.__name__}'",
                        exc_info=True,
                    )
            self._plugins = plugins
        return self._plugins

execute_action(identifier, selection, data, form_values)

Trigger action execution.

Parameters:

Name Type Description Default
identifier str

Identifier of the plugin.

required
selection LoaderActionSelection

Selection wrapper. Can be used to get what is selected in UI and to get access to entity cache.

required
data Optional[DataType]

Additional action item data.

required
form_values dict[str, Any]

Form values related to action. Usually filled if action returned response with form.

required
Source code in client/ayon_core/pipeline/actions/loader.py
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
def execute_action(
    self,
    identifier: str,
    selection: LoaderActionSelection,
    data: Optional[DataType],
    form_values: dict[str, Any],
) -> Optional[LoaderActionResult]:
    """Trigger action execution.

    Args:
        identifier (str): Identifier of the plugin.
        selection (LoaderActionSelection): Selection wrapper. Can be used
            to get what is selected in UI and to get access to entity
            cache.
        data (Optional[DataType]): Additional action item data.
        form_values (dict[str, Any]): Form values related to action.
            Usually filled if action returned response with form.

    """
    plugins_by_id = self._get_plugins()
    plugin = plugins_by_id[identifier]
    return plugin.execute_action(
        selection,
        data,
        form_values,
    )

get_action_items(selection)

Collect action items from all plugins for given selection.

Parameters:

Name Type Description Default
selection LoaderActionSelection

Selection wrapper.

required
Source code in client/ayon_core/pipeline/actions/loader.py
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
def get_action_items(
    self, selection: LoaderActionSelection
) -> list[LoaderActionItem]:
    """Collect action items from all plugins for given selection.

    Args:
        selection (LoaderActionSelection): Selection wrapper.

    """
    output = []
    for plugin_id, plugin in self._get_plugins().items():
        try:
            for action_item in plugin.get_action_items(selection):
                if action_item.identifier is None:
                    action_item.identifier = plugin_id
                output.append(action_item)

        except Exception:
            self._log.warning(
                "Failed to get action items for"
                f" plugin '{plugin.identifier}'",
                exc_info=True,
            )
    return output

get_host()

Get current host integration.

Returns:

Type Description
Optional[AbstractHost]

Optional[AbstractHost]: Host integration. Can be None if host integration is not registered -> probably not used in the host integration process.

Source code in client/ayon_core/pipeline/actions/loader.py
675
676
677
678
679
680
681
682
683
684
685
686
687
688
def get_host(self) -> Optional[AbstractHost]:
    """Get current host integration.

    Returns:
        Optional[AbstractHost]: Host integration. Can be None if host
            integration is not registered -> probably not used in the
            host integration process.

    """
    if self._host is _PLACEHOLDER:
        from ayon_core.pipeline import registered_host

        self._host = registered_host()
    return self._host

reset(studio_settings=None)

Reset context cache.

Reset plugins and studio settings to reload them.

Notes

Does not reset the cache of AddonsManger because there should not be a reason to do so.

Source code in client/ayon_core/pipeline/actions/loader.py
653
654
655
656
657
658
659
660
661
662
663
664
665
666
def reset(
    self, studio_settings: Optional[dict[str, Any]] = None
) -> None:
    """Reset context cache.

    Reset plugins and studio settings to reload them.

    Notes:
         Does not reset the cache of AddonsManger because there should not
            be a reason to do so.

    """
    self._studio_settings = studio_settings
    self._plugins = None

LoaderSelectedType

Bases: StrEnum

Selected entity type.

Source code in client/ayon_core/pipeline/actions/loader.py
91
92
93
94
95
96
class LoaderSelectedType(StrEnum):
    """Selected entity type."""
    # folder = "folder"
    # task = "task"
    version = "version"
    representation = "representation"

LoaderSimpleActionPlugin

Bases: LoaderActionPlugin

Simple action plugin.

This action will show exactly one action item defined by attributes on the class.

Attributes:

Name Type Description
label Optional[str]

Label of the action item.

order int

Order of the action item.

group_label Optional[str]

Label of the group to which the action belongs.

icon Optional[dict[str, Any]]

Icon definition shown next to label.

Source code in client/ayon_core/pipeline/actions/loader.py
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
class LoaderSimpleActionPlugin(LoaderActionPlugin):
    """Simple action plugin.

    This action will show exactly one action item defined by attributes
        on the class.

    Attributes:
        label: Label of the action item.
        order: Order of the action item.
        group_label: Label of the group to which the action belongs.
        icon: Icon definition shown next to label.

    """

    label: Optional[str] = None
    order: int = 0
    group_label: Optional[str] = None
    icon: Optional[dict[str, Any]] = None

    @abstractmethod
    def is_compatible(self, selection: LoaderActionSelection) -> bool:
        """Check if plugin is compatible with selection.

        Args:
            selection (LoaderActionSelection): Selection information.

        Returns:
            bool: True if plugin is compatible with selection.

        """
        pass

    @abstractmethod
    def execute_simple_action(
        self,
        selection: LoaderActionSelection,
        form_values: dict[str, Any],
    ) -> Optional[LoaderActionResult]:
        """Process action based on selection.

        Args:
            selection (LoaderActionSelection): Selection information.
            form_values (dict[str, Any]): Values from a form if there are any.

        Returns:
            Optional[LoaderActionResult]: Result of the action.

        """
        pass

    def get_action_items(
        self, selection: LoaderActionSelection
    ) -> list[LoaderActionItem]:
        if self.is_compatible(selection):
            label = self.label or self.__class__.__name__
            return [
                LoaderActionItem(
                    label=label,
                    order=self.order,
                    group_label=self.group_label,
                    icon=self.icon,
                )
            ]
        return []

    def execute_action(
        self,
        selection: LoaderActionSelection,
        data: Optional[DataType],
        form_values: dict[str, Any],
    ) -> Optional[LoaderActionResult]:
        return self.execute_simple_action(selection, form_values)

execute_simple_action(selection, form_values) abstractmethod

Process action based on selection.

Parameters:

Name Type Description Default
selection LoaderActionSelection

Selection information.

required
form_values dict[str, Any]

Values from a form if there are any.

required

Returns:

Type Description
Optional[LoaderActionResult]

Optional[LoaderActionResult]: Result of the action.

Source code in client/ayon_core/pipeline/actions/loader.py
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
@abstractmethod
def execute_simple_action(
    self,
    selection: LoaderActionSelection,
    form_values: dict[str, Any],
) -> Optional[LoaderActionResult]:
    """Process action based on selection.

    Args:
        selection (LoaderActionSelection): Selection information.
        form_values (dict[str, Any]): Values from a form if there are any.

    Returns:
        Optional[LoaderActionResult]: Result of the action.

    """
    pass

is_compatible(selection) abstractmethod

Check if plugin is compatible with selection.

Parameters:

Name Type Description Default
selection LoaderActionSelection

Selection information.

required

Returns:

Name Type Description
bool bool

True if plugin is compatible with selection.

Source code in client/ayon_core/pipeline/actions/loader.py
830
831
832
833
834
835
836
837
838
839
840
841
@abstractmethod
def is_compatible(self, selection: LoaderActionSelection) -> bool:
    """Check if plugin is compatible with selection.

    Args:
        selection (LoaderActionSelection): Selection information.

    Returns:
        bool: True if plugin is compatible with selection.

    """
    pass

SelectionEntitiesCache

Cache of entities used as helper in the selection wrapper.

It is possible to get entities based on ids with helper methods to get entities, their parents or their children's entities.

The goal is to avoid multiple API calls for the same entity in multiple action plugins.

The cache is based on the selected project. Entities are fetched if are not in cache yet.

Source code in client/ayon_core/pipeline/actions/loader.py
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
class SelectionEntitiesCache:
    """Cache of entities used as helper in the selection wrapper.

    It is possible to get entities based on ids with helper methods to get
        entities, their parents or their children's entities.

    The goal is to avoid multiple API calls for the same entity in multiple
        action plugins.

    The cache is based on the selected project. Entities are fetched
        if are not in cache yet.
    """
    def __init__(
        self,
        project_name: str,
        project_entity: Optional[dict[str, Any]] = None,
        folders_by_id: Optional[dict[str, dict[str, Any]]] = None,
        tasks_by_id: Optional[dict[str, dict[str, Any]]] = None,
        products_by_id: Optional[dict[str, dict[str, Any]]] = None,
        versions_by_id: Optional[dict[str, dict[str, Any]]] = None,
        representations_by_id: Optional[dict[str, dict[str, Any]]] = None,
        task_ids_by_folder_id: Optional[dict[str, set[str]]] = None,
        product_ids_by_folder_id: Optional[dict[str, set[str]]] = None,
        version_ids_by_product_id: Optional[dict[str, set[str]]] = None,
        representation_ids_by_version_id: Optional[dict[str, set[str]]] = None,
    ):
        self._project_name = project_name
        self._project_entity = project_entity
        self._folders_by_id = folders_by_id or {}
        self._tasks_by_id = tasks_by_id or {}
        self._products_by_id = products_by_id or {}
        self._versions_by_id = versions_by_id or {}
        self._representations_by_id = representations_by_id or {}

        self._task_ids_by_folder_id = task_ids_by_folder_id or {}
        self._product_ids_by_folder_id = product_ids_by_folder_id or {}
        self._version_ids_by_product_id = version_ids_by_product_id or {}
        self._representation_ids_by_version_id = (
            representation_ids_by_version_id or {}
        )

    def get_project(self) -> dict[str, Any]:
        """Get project entity"""
        if self._project_entity is None:
            self._project_entity = ayon_api.get_project(self._project_name)
        return copy.deepcopy(self._project_entity)

    def get_folders(
        self, folder_ids: set[str]
    ) -> list[dict[str, Any]]:
        return self._get_entities(
            folder_ids,
            self._folders_by_id,
            "folder_ids",
            ayon_api.get_folders,
        )

    def get_tasks(
        self, task_ids: set[str]
    ) -> list[dict[str, Any]]:
        return self._get_entities(
            task_ids,
            self._tasks_by_id,
            "task_ids",
            ayon_api.get_tasks,
        )

    def get_products(
        self, product_ids: set[str]
    ) -> list[dict[str, Any]]:
        return self._get_entities(
            product_ids,
            self._products_by_id,
            "product_ids",
            ayon_api.get_products,
        )

    def get_versions(
        self, version_ids: set[str]
    ) -> list[dict[str, Any]]:
        return self._get_entities(
            version_ids,
            self._versions_by_id,
            "version_ids",
            ayon_api.get_versions,
        )

    def get_representations(
        self, representation_ids: set[str]
    ) -> list[dict[str, Any]]:
        return self._get_entities(
            representation_ids,
            self._representations_by_id,
            "representation_ids",
            ayon_api.get_representations,
        )

    def get_folders_tasks(
        self, folder_ids: set[str]
    ) -> list[dict[str, Any]]:
        task_ids = self._fill_parent_children_ids(
            folder_ids,
            "folderId",
            "folder_ids",
            self._task_ids_by_folder_id,
            ayon_api.get_tasks,
        )
        return self.get_tasks(task_ids)

    def get_folders_products(
        self, folder_ids: set[str]
    ) -> list[dict[str, Any]]:
        product_ids = self._get_folders_products_ids(folder_ids)
        return self.get_products(product_ids)

    def get_tasks_versions(
        self, task_ids: set[str]
    ) -> list[dict[str, Any]]:
        folder_ids = {
            task["folderId"]
            for task in self.get_tasks(task_ids)
        }
        product_ids = self._get_folders_products_ids(folder_ids)
        output = []
        for version in self.get_products_versions(product_ids):
            task_id = version["taskId"]
            if task_id in task_ids:
                output.append(version)
        return output

    def get_products_versions(
        self, product_ids: set[str]
    ) -> list[dict[str, Any]]:
        version_ids = self._fill_parent_children_ids(
            product_ids,
            "productId",
            "product_ids",
            self._version_ids_by_product_id,
            ayon_api.get_versions,
        )
        return self.get_versions(version_ids)

    def get_versions_representations(
        self, version_ids: set[str]
    ) -> list[dict[str, Any]]:
        repre_ids = self._fill_parent_children_ids(
            version_ids,
            "versionId",
            "version_ids",
            self._representation_ids_by_version_id,
            ayon_api.get_representations,
        )
        return self.get_representations(repre_ids)

    def get_tasks_folders(self, task_ids: set[str]) -> list[dict[str, Any]]:
        folder_ids = {
            task["folderId"]
            for task in self.get_tasks(task_ids)
        }
        return self.get_folders(folder_ids)

    def get_products_folders(
        self, product_ids: set[str]
    ) -> list[dict[str, Any]]:
        folder_ids = {
            product["folderId"]
            for product in self.get_products(product_ids)
        }
        return self.get_folders(folder_ids)

    def get_versions_products(
        self, version_ids: set[str]
    ) -> list[dict[str, Any]]:
        product_ids = {
            version["productId"]
            for version in self.get_versions(version_ids)
        }
        return self.get_products(product_ids)

    def get_versions_tasks(
        self, version_ids: set[str]
    ) -> list[dict[str, Any]]:
        task_ids = {
            version["taskId"]
            for version in self.get_versions(version_ids)
            if version["taskId"]
        }
        return self.get_tasks(task_ids)

    def get_representations_versions(
        self, representation_ids: set[str]
    ) -> list[dict[str, Any]]:
        version_ids = {
            repre["versionId"]
            for repre in self.get_representations(representation_ids)
        }
        return self.get_versions(version_ids)

    def _get_folders_products_ids(self, folder_ids: set[str]) -> set[str]:
        return self._fill_parent_children_ids(
            folder_ids,
            "folderId",
            "folder_ids",
            self._product_ids_by_folder_id,
            ayon_api.get_products,
        )

    def _fill_parent_children_ids(
        self,
        entity_ids: set[str],
        parent_key: str,
        filter_attr: str,
        parent_mapping: dict[str, set[str]],
        getter: Callable,
    ) -> set[str]:
        if not entity_ids:
            return set()
        children_ids = set()
        missing_ids = set()
        for entity_id in entity_ids:
            _children_ids = parent_mapping.get(entity_id)
            if _children_ids is None:
                missing_ids.add(entity_id)
            else:
                children_ids.update(_children_ids)
        if missing_ids:
            entities_by_parent_id = collections.defaultdict(set)
            for entity in getter(
                self._project_name,
                fields={"id", parent_key},
                **{filter_attr: missing_ids},
            ):
                child_id = entity["id"]
                children_ids.add(child_id)
                entities_by_parent_id[entity[parent_key]].add(child_id)

            for entity_id in missing_ids:
                parent_mapping[entity_id] = entities_by_parent_id[entity_id]

        return children_ids

    def _get_entities(
        self,
        entity_ids: set[str],
        cache_var: dict[str, Any],
        filter_arg: str,
        getter: Callable,
    ) -> list[dict[str, Any]]:
        if not entity_ids:
            return []

        output = []
        missing_ids: set[str] = set()
        for entity_id in entity_ids:
            entity = cache_var.get(entity_id)
            if entity_id not in cache_var:
                missing_ids.add(entity_id)
                cache_var[entity_id] = None
            elif entity:
                output.append(entity)

        if missing_ids:
            for entity in getter(
                self._project_name,
                **{filter_arg: missing_ids}
            ):
                output.append(entity)
                cache_var[entity["id"]] = entity
        return output

get_project()

Get project entity

Source code in client/ayon_core/pipeline/actions/loader.py
140
141
142
143
144
def get_project(self) -> dict[str, Any]:
    """Get project entity"""
    if self._project_entity is None:
        self._project_entity = ayon_api.get_project(self._project_name)
    return copy.deepcopy(self._project_entity)