Skip to content

validate_script_attributes

ValidateScriptAttributes

Bases: OptionalPyblishPluginMixin, InstancePlugin

Validates file output.

Source code in client/ayon_nuke/plugins/publish/validate_script_attributes.py
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 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
 64
 65
 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
class ValidateScriptAttributes(
    OptionalPyblishPluginMixin,
    pyblish.api.InstancePlugin
):
    """ Validates file output. """

    order = pyblish.api.ValidatorOrder + 0.1
    families = ["workfile"]
    label = "Validate script attributes"
    hosts = ["nuke"]
    optional = True
    actions = [RepairAction]

    settings_category = "nuke"

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

        script_data = deepcopy(instance.context.data["scriptData"])

        # Task may be optional for an instance
        task_entity = instance.data.get("taskEntity")
        if task_entity:
            src_attributes = task_entity["attrib"]
        else:
            src_attributes = instance.data["folderEntity"]["attrib"]

        # These attributes will be checked
        attributes = [
            "fps",
            "frameStart",
            "frameEnd",
            "resolutionWidth",
            "resolutionHeight",
            "handleStart",
            "handleEnd"
        ]

        # get only defined attributes from folder or task data
        check_attributes = {
            attr: src_attributes[attr]
            for attr in attributes
            if attr in src_attributes
        }
        # fix frame values to include handles
        check_attributes["fps"] = float("{0:.4f}".format(
            check_attributes["fps"]))
        script_data["fps"] = float("{0:.4f}".format(
            script_data["fps"]))

        # Compare folder's values Nukescript X Database
        not_matching = []
        for attr in attributes:
            self.log.debug(
                "Task vs Script attribute \"{}\": {}, {}".format(
                    attr,
                    check_attributes[attr],
                    script_data[attr]
                )
            )
            if check_attributes[attr] != script_data[attr]:
                not_matching.append({
                    "name": attr,
                    "expected": check_attributes[attr],
                    "actual": script_data[attr]
                })

        # Raise error if not matching
        if not_matching:
            msg = "Following attributes are not set correctly: \n{}"
            attrs_wrong_str = "\n".join([
                (
                    "`{0}` is set to `{1}`, "
                    "but should be set to `{2}`"
                ).format(at["name"], at["actual"], at["expected"])
                for at in not_matching
            ])
            attrs_wrong_html = "<br/>".join([
                (
                    "-- __{0}__ is set to __{1}__, "
                    "but should be set to __{2}__"
                ).format(at["name"], at["actual"], at["expected"])
                for at in not_matching
            ])
            raise PublishXmlValidationError(
                self, msg.format(attrs_wrong_str),
                formatting_data={
                    "failed_attributes": attrs_wrong_html
                }
            )

    @classmethod
    def repair(cls, instance):
        cls.log.debug("__ repairing instance: {}".format(instance))
        WorkfileSettings().set_context_settings()