Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

NewPublisher: Prepared implementation of optional pyblish plugin #2943

Merged
merged 4 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion openpype/pipeline/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
PublishValidationError,
PublishXmlValidationError,
KnownPublishError,
OpenPypePyblishPluginMixin
OpenPypePyblishPluginMixin,
OptionalPyblishPluginMixin,
)

from .actions import (
Expand Down Expand Up @@ -105,6 +106,7 @@
"PublishXmlValidationError",
"KnownPublishError",
"OpenPypePyblishPluginMixin",
"OptionalPyblishPluginMixin",

# --- Actions ---
"LauncherAction",
Expand Down
2 changes: 2 additions & 0 deletions openpype/pipeline/publish/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
PublishXmlValidationError,
KnownPublishError,
OpenPypePyblishPluginMixin,
OptionalPyblishPluginMixin,
)

from .lib import (
Expand All @@ -18,6 +19,7 @@
"PublishXmlValidationError",
"KnownPublishError",
"OpenPypePyblishPluginMixin",
"OptionalPyblishPluginMixin",

"DiscoverResult",
"publish_plugins_discover",
Expand Down
62 changes: 62 additions & 0 deletions openpype/pipeline/publish/publish_plugins.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from openpype.lib import BoolDef
from .lib import load_help_content_from_plugin


Expand Down Expand Up @@ -108,3 +109,64 @@ def convert_attribute_values(cls, attribute_values):
plugin_values[key]
)
return attribute_values

def get_attr_values_from_data(self, data):
"""Get attribute values for attribute definitions from data.

Args:
data(dict): Data from instance or context.
"""
return (
data
.get("publish_attributes", {})
.get(self.__class__.__name__, {})
)


class OptionalPyblishPluginMixin(OpenPypePyblishPluginMixin):
"""Prepare mixin for optional plugins.

Defined active attribute definition prepared for published and
prepares method which will check if is active or not.

```
class ValidateScene(
pyblish.api.InstancePlugin, OptionalPyblishPluginMixin
):
def process(self, instance):
# Skip the instance if is not active by data on the instance
if not self.is_active(instance.data):
return
```
"""

@classmethod
def get_attribute_defs(cls):
"""Attribute definitions based on plugin's optional attribute."""

# Empty list if plugin is not optional
if not getattr(cls, "optional", None):
return []

# Get active value from class as default value
active = getattr(cls, "active", True)
# Return boolean stored under 'active' key with label of the class name
label = cls.label or cls.__name__
return [
BoolDef("active", default=active, label=label)
]

def is_active(self, data):
"""Check if plugins is active for instance/context based on their data.

Args:
data(dict): Data from instance or context.
"""
# Skip if is not optional and return True
if not getattr(self, "optional", None):
return True
attr_values = self.get_attr_values_from_data(data)
active = attr_values.get("active")
if active is None:
active = getattr(self, "active", True)
return active