Skip to content

Commit

Permalink
Merge pull request #58 from ynput/enhancement/OP-8105_Validate-Context
Browse files Browse the repository at this point in the history
Max : Optional validator to check invalid context data
  • Loading branch information
moonyuet authored Mar 4, 2024
2 parents c5b6456 + 93d1fae commit d3d3596
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# -*- coding: utf-8 -*-
"""Validate if instance context is the same as current context."""
import pyblish.api
from ayon_core.pipeline.publish import (
RepairAction,
ValidateContentsOrder,
PublishValidationError,
OptionalPyblishPluginMixin
)
from ayon_core.hosts.max.api.action import SelectInvalidAction
from pymxs import runtime as rt


class ValidateInstanceInContext(pyblish.api.InstancePlugin,
OptionalPyblishPluginMixin):
"""Validator to check if instance context match current context.
When working in per-shot style you always publish data in context of
current context (shot). This validator checks if this is so. It is optional
so it can be disabled when needed.
Action on this validator will select invalid instances.
"""
order = ValidateContentsOrder
label = "Instance in same Context"
optional = True
hosts = ["max"]
actions = [SelectInvalidAction, RepairAction]

def process(self, instance):
if not self.is_active(instance.data):
return

folderPath = instance.data.get("folderPath")
task = instance.data.get("task")
context = self.get_context(instance)
if (folderPath, task) != context:
context_label = "{} > {}".format(*context)
instance_label = "{} > {}".format(folderPath, task)
message = (
"Instance '{}' publishes to different context than current "
"context: {}. Current context: {}".format(
instance.name, instance_label, context_label
)
)
raise PublishValidationError(
message=message,
description=(
"## Publishing to a different context data\n"
"There are publish instances present which are publishing "
"into a different folder path or task than your current context.\n\n"
"Usually this is not what you want but there can be cases "
"where you might want to publish into another context or "
"shot. If that's the case you can disable the validation "
"on the instance to ignore it."
)
)

@classmethod
def get_invalid(cls, instance):
invalid = []
folderPath = instance.data.get("folderPath")
task = instance.data.get("task")
context = cls.get_context(instance)
if (folderPath, task) != context:
invalid.append(rt.getNodeByName(instance.name))
return invalid

@classmethod
def repair(cls, instance):
context_asset = instance.context.data["folderPath"]
context_task = instance.context.data["task"]
instance_node = rt.getNodeByName(instance.data.get(
"instance_node", ""))
if not instance_node:
return
rt.SetUserProp(instance_node, "folderPath", context_asset)
rt.SetUserProp(instance_node, "task", context_task)

@staticmethod
def get_context(instance):
"""Return asset, task from publishing context data"""
context = instance.context
return context.data["folderPath"], context.data["task"]
13 changes: 11 additions & 2 deletions server_addon/max/server/settings/publishers.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,15 @@ class BasicValidateModel(BaseSettingsModel):


class PublishersModel(BaseSettingsModel):
ValidateFrameRange: BasicValidateModel = SettingsField(
ValidateInstanceInContext: BasicValidateModel = SettingsField(
default_factory=BasicValidateModel,
title="Validate Frame Range",
title="Validate Instance In Context",
section="Validators"
)
ValidateFrameRange: BasicValidateModel = SettingsField(
default_factory=BasicValidateModel,
title="Validate Frame Range"
)
ValidateAttributes: ValidateAttributesModel = SettingsField(
default_factory=ValidateAttributesModel,
title="Validate Attributes"
Expand Down Expand Up @@ -114,6 +118,11 @@ class PublishersModel(BaseSettingsModel):


DEFAULT_PUBLISH_SETTINGS = {
"ValidateInstanceInContext": {
"enabled": True,
"optional": True,
"active": True
},
"ValidateFrameRange": {
"enabled": True,
"optional": True,
Expand Down

0 comments on commit d3d3596

Please sign in to comment.