Skip to content

validate_document_settings

ValidateDocumentSettings

Bases: ContextPlugin, OptionalPyblishPluginMixin

Validate document resolution, color mode and bit depth.

Source code in client/ayon_photoshop/plugins/publish/validate_document_settings.py
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 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
class ValidateDocumentSettings(
    pyblish.api.ContextPlugin, OptionalPyblishPluginMixin
):
    """Validate document resolution, color mode and bit depth."""

    label = "Validate Document Settings"
    hosts = ["photoshop"]
    order = ValidateContentsOrder
    settings_category = "photoshop"
    actions = [ValidateDocumentSettingsRepair]

    optional = True
    active = True

    expected_dpi: int = 72
    expected_mode: str = "RGB"
    expected_bits: int = 8

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

        info = photoshop.stub().get_document_settings()
        if not info:
            raise PublishXmlValidationError(
                self,
                "Unable to read document settings from Photoshop.",
                formatting_data={
                    "details": "Unable to read document settings.",
                    "expected_dpi": self.expected_dpi,
                    "expected_mode": self.expected_mode,
                    "expected_bits": self.expected_bits,
                },
            )

        invalid: bool = False

        resolution = info.get("resolution")
        try:
            resolution_value = int(round(float(resolution)))
        except (ValueError, TypeError):
            resolution_value = None
        if resolution_value != int(self.expected_dpi):
            self.log.warning(
                f"Resolution is {resolution_value} dpi"
                f" (expected {self.expected_dpi} dpi)"
            )
            invalid = True

        mode = _normalize_mode(info.get("mode"))
        if mode != self.expected_mode.upper():
            mode_label = mode or info.get("mode")
            self.log.warning(
                f"Color mode is {mode_label} (expected {self.expected_mode})"
            )
            invalid = True

        bits = _normalize_bits(info.get("bitsPerChannel"))
        if bits != int(self.expected_bits):
            self.log.warning(
                f"Bit depth is {bits} (expected {self.expected_bits})"
            )
            invalid = True

        if invalid:
            raise PublishXmlValidationError(
                self,
                "Document settings are not compliant.",
                formatting_data={
                    "expected_dpi": self.expected_dpi,
                    "expected_mode": self.expected_mode,
                    "expected_bits": self.expected_bits,
                }
            )

ValidateDocumentSettingsRepair

Bases: Action

Repair document settings to match expected values.

Warning: Some conversions may be lossy (e.g., CMYK to RGB, higher to lower bit depth).

Source code in client/ayon_photoshop/plugins/publish/validate_document_settings.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class ValidateDocumentSettingsRepair(pyblish.api.Action):
    """Repair document settings to match expected values.

    Warning: Some conversions may be lossy (e.g., CMYK to RGB,
    higher to lower bit depth).
    """

    label = "Repair"
    icon = "wrench"
    on = "failed"

    def process(self, context, plugin):
        stub = photoshop.stub()

        result = stub.set_document_settings(
            resolution=plugin.expected_dpi,
            mode=plugin.expected_mode,
            bits=str(plugin.expected_bits)
        )

        if not result.get("success"):
            errors = result.get("errors") or [result.get("error", "Unknown")]
            self.log.error(
                "Failed to repair document settings: {}".format(
                    ", ".join(errors)
                )
            )
            return False

        self.log.info("Document settings repaired successfully")
        return True