Skip to content

validate_write_nodes

ValidateNukeWriteNode

Bases: OptionalPyblishPluginMixin, InstancePlugin

Validate Write node's knobs.

Compare knobs on write node inside the render group with settings. At the moment supporting only file knob.

Source code in client/ayon_nuke/plugins/publish/validate_write_nodes.py
 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
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
class ValidateNukeWriteNode(
    OptionalPyblishPluginMixin,
    pyblish.api.InstancePlugin
):
    """ Validate Write node's knobs.

    Compare knobs on write node inside the render group
    with settings. At the moment supporting only `file` knob.
    """

    order = pyblish.api.ValidatorOrder
    optional = True
    families = ["render"]
    label = "Validate write node"
    actions = [RepairNukeWriteNodeAction]
    hosts = ["nuke"]

    settings_category = "nuke"

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

        child_nodes = (
            instance.data.get("transientData", {}).get("childNodes")
            or instance
        )

        write_group_node = instance.data["transientData"]["node"]

        # get write node from inside of group
        write_node = None
        for x in child_nodes:
            if x.Class() == "Write":
                write_node = x

        if write_node is None:
            return

        correct_data = get_write_node_template_attr(write_group_node)

        check = []

        # Collect key values of same type in a list.
        values_by_name = defaultdict(list)
        for knob_data in correct_data["knobs"]:
            knob_type = knob_data["type"]
            knob_value = knob_data[knob_type]

            values_by_name[knob_data["name"]].append(knob_value)

        for knob_data in correct_data["knobs"]:
            knob_type = knob_data["type"]

            if (
                knob_type == "__legacy__"
            ):
                raise PublishXmlValidationError(
                    self, (
                        "Please update data in settings 'project_settings"
                        "/nuke/imageio/nodes/required_nodes'"
                    ),
                    key="legacy"
                )

            key = knob_data["name"]
            values = values_by_name[key]

            try:
                node_value = write_node[key].value()

            except NameError:
                self.log.warning("Missing knob %s in write node.", key)
                continue

            # fix type differences
            fixed_values = []
            for value in values:
                if type(node_value) in (int, float):
                    try:
                        if isinstance(value, list):
                            value = color_gui_to_int(value)
                        else:
                            value = float(value)
                            node_value = float(node_value)
                    except ValueError:
                        value = str(value)
                else:
                    value = str(value)
                    node_value = str(node_value)

                fixed_values.append(value)

            if (
                node_value not in fixed_values
                and key != "file"
                and key != "tile_color"
            ):
                check.append([key, fixed_values, write_node[key].value()])

        if check:
            self._make_error(check)

    def _make_error(self, check):
        # sourcery skip: merge-assign-and-aug-assign, move-assign-in-block
        dbg_msg = "Write node's knobs values are not correct!\n"
        msg_add = "Knob '{0}' > Expected: `{1}` > Current: `{2}`"

        details = [
            msg_add.format(item[0], item[1], item[2])
            for item in check
        ]
        xml_msg = "<br/>".join(details)
        dbg_msg += "\n\t".join(details)

        raise PublishXmlValidationError(
            self, dbg_msg, formatting_data={"xml_msg": xml_msg}
        )