Skip to content

load_gizmo_ip

LoadGizmoInputProcess

Bases: LoaderPlugin

Loading colorspace soft effect exported from nukestudio

Source code in client/ayon_nuke/plugins/load/load_gizmo_ip.py
 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
class LoadGizmoInputProcess(load.LoaderPlugin):
    """Loading colorspace soft effect exported from nukestudio"""

    product_types = {"gizmo"}
    representations = {"*"}
    extensions = {"nk"}

    settings_category = "nuke"

    label = "Load Gizmo - Input Process"
    order = 0
    icon = "eye"
    color = "#cc0000"
    node_color = "0x7533c1ff"

    def load(self, context, name, namespace, data):
        """
        Loading function to get Gizmo as Input Process on viewer

        Arguments:
            context (dict): context of version
            name (str): name of the version
            namespace (str): namespace name
            data (dict): compulsory attribute > not used

        Returns:
            nuke node: containerized nuke node object
        """

        # get main variables
        version_entity = context["version"]

        version_attributes = version_entity["attrib"]
        first = version_attributes.get("frameStart")
        last = version_attributes.get("frameEnd")
        colorspace = version_attributes.get("colorSpace")

        namespace = namespace or context["folder"]["name"]
        object_name = "{}_{}".format(name, namespace)

        # prepare data for imprinting
        # add additional metadata from the version to imprint to metadata knob
        data_imprint = {
            "frameStart": first,
            "frameEnd": last,
            "version": version_entity["version"],
            "colorspaceInput": colorspace
        }

        for k in [
            "frameStart",
            "frameEnd",
            "handleStart",
            "handleEnd",
            "source",
            "fps"
        ]:
            data_imprint[k] = version_attributes[k]

        # getting file path
        file = self.filepath_from_context(context).replace("\\", "/")

        # adding nodes to node graph
        # just in case we are in group lets jump out of it
        nuke.endGroup()

        with maintained_selection():
            # add group from nk
            nuke.nodePaste(file)

            group_node = nuke.selectedNode()

            group_node["name"].setValue(object_name)

            # try to place it under Viewer1
            if not self.connect_active_viewer(group_node):
                nuke.delete(group_node)
                return

            return containerise(
                node=group_node,
                name=name,
                namespace=namespace,
                context=context,
                loader=self.__class__.__name__,
                data=data_imprint)

    def update(self, container, context):
        """Update the Loader's path

        Nuke automatically tries to reset some variables when changing
        the loader's path to a new file. These automatic changes are to its
        inputs:

        """

        # get main variables
        # Get version from io
        project_name = context["project"]["name"]
        version_entity = context["version"]
        repre_entity = context["representation"]

        # get corresponding node
        group_node = container["node"]

        file = get_representation_path(repre_entity).replace("\\", "/")

        version_attributes = version_entity["attrib"]
        first = version_attributes.get("frameStart")
        last = version_attributes.get("frameEnd")
        colorspace = version_attributes.get("colorSpace")

        data_imprint = {
            "representation": repre_entity["id"],
            "frameStart": first,
            "frameEnd": last,
            "version": version_entity["version"],
            "colorspaceInput": colorspace
        }

        for k in [
            "frameStart",
            "frameEnd",
            "handleStart",
            "handleEnd",
            "source",
            "fps"
        ]:
            data_imprint[k] = version_attributes[k]

        # capture pipeline metadata
        avalon_data = get_avalon_knob_data(group_node)

        # adding nodes to node graph
        # just in case we are in group lets jump out of it
        nuke.endGroup()

        with maintained_selection([group_node]):
            # insert nuke script to the script
            nuke.nodePaste(file)
            # convert imported to selected node
            new_group_node = nuke.selectedNode()
            # swap nodes with maintained connections
            with swap_node_with_dependency(
                    group_node, new_group_node) as node_name:
                new_group_node["name"].setValue(node_name)
                # set updated pipeline metadata
                set_avalon_knob_data(new_group_node, avalon_data)

        last_version_entity = ayon_api.get_last_version_by_product_id(
            project_name, version_entity["productId"], fields={"id"}
        )

        # change color of node
        if version_entity["id"] == last_version_entity["id"]:
            color_value = self.node_color
        else:
            color_value = "0xd88467ff"
        new_group_node["tile_color"].setValue(int(color_value, 16))

        self.log.info(
            "updated to version: {}".format(version_entity["version"])
        )

        return update_container(new_group_node, data_imprint)

    def connect_active_viewer(self, group_node):
        """
        Finds Active viewer and
        place the node under it, also adds
        name of group into Input Process of the viewer

        Arguments:
            group_node (nuke node): nuke group node object

        """
        group_node_name = group_node["name"].value()

        viewer = [n for n in nuke.allNodes() if "Viewer1" in n["name"].value()]
        if len(viewer) > 0:
            viewer = viewer[0]
        else:
            msg = str("Please create Viewer node before you "
                      "run this action again")
            self.log.error(msg)
            nuke.message(msg)
            return None

        # get coordinates of Viewer1
        xpos = viewer["xpos"].value()
        ypos = viewer["ypos"].value()

        ypos += 150

        viewer["ypos"].setValue(ypos)

        # set coordinates to group node
        group_node["xpos"].setValue(xpos)
        group_node["ypos"].setValue(ypos + 50)

        # add group node name to Viewer Input Process
        viewer["input_process_node"].setValue(group_node_name)

        # put backdrop under
        create_backdrop(
            label="Input Process",
            layer=2,
            nodes=[viewer, group_node],
            color="0x7c7faaff"
        )

        return True

    def get_item(self, data, trackIndex, subTrackIndex):
        return {key: val for key, val in data.items()
                if subTrackIndex == val["subTrackIndex"]
                if trackIndex == val["trackIndex"]}

    def byteify(self, input):
        """
        Converts unicode strings to strings
        It goes through all dictionary

        Arguments:
            input (dict/str): input

        Returns:
            dict: with fixed values and keys

        """

        if isinstance(input, dict):
            return {self.byteify(key): self.byteify(value)
                    for key, value in input.items()}
        elif isinstance(input, list):
            return [self.byteify(element) for element in input]
        elif isinstance(input, str):
            return str(input)
        else:
            return input

    def switch(self, container, context):
        self.update(container, context)

    def remove(self, container):
        node = container["node"]
        with viewer_update_and_undo_stop():
            nuke.delete(node)

byteify(input)

Converts unicode strings to strings It goes through all dictionary

Parameters:

Name Type Description Default
input dict / str

input

required

Returns:

Name Type Description
dict

with fixed values and keys

Source code in client/ayon_nuke/plugins/load/load_gizmo_ip.py
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
def byteify(self, input):
    """
    Converts unicode strings to strings
    It goes through all dictionary

    Arguments:
        input (dict/str): input

    Returns:
        dict: with fixed values and keys

    """

    if isinstance(input, dict):
        return {self.byteify(key): self.byteify(value)
                for key, value in input.items()}
    elif isinstance(input, list):
        return [self.byteify(element) for element in input]
    elif isinstance(input, str):
        return str(input)
    else:
        return input

connect_active_viewer(group_node)

Finds Active viewer and place the node under it, also adds name of group into Input Process of the viewer

Parameters:

Name Type Description Default
group_node nuke node

nuke group node object

required
Source code in client/ayon_nuke/plugins/load/load_gizmo_ip.py
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
def connect_active_viewer(self, group_node):
    """
    Finds Active viewer and
    place the node under it, also adds
    name of group into Input Process of the viewer

    Arguments:
        group_node (nuke node): nuke group node object

    """
    group_node_name = group_node["name"].value()

    viewer = [n for n in nuke.allNodes() if "Viewer1" in n["name"].value()]
    if len(viewer) > 0:
        viewer = viewer[0]
    else:
        msg = str("Please create Viewer node before you "
                  "run this action again")
        self.log.error(msg)
        nuke.message(msg)
        return None

    # get coordinates of Viewer1
    xpos = viewer["xpos"].value()
    ypos = viewer["ypos"].value()

    ypos += 150

    viewer["ypos"].setValue(ypos)

    # set coordinates to group node
    group_node["xpos"].setValue(xpos)
    group_node["ypos"].setValue(ypos + 50)

    # add group node name to Viewer Input Process
    viewer["input_process_node"].setValue(group_node_name)

    # put backdrop under
    create_backdrop(
        label="Input Process",
        layer=2,
        nodes=[viewer, group_node],
        color="0x7c7faaff"
    )

    return True

load(context, name, namespace, data)

Loading function to get Gizmo as Input Process on viewer

Parameters:

Name Type Description Default
context dict

context of version

required
name str

name of the version

required
namespace str

namespace name

required
data dict

compulsory attribute > not used

required

Returns:

Type Description

nuke node: containerized nuke node object

Source code in client/ayon_nuke/plugins/load/load_gizmo_ip.py
 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
def load(self, context, name, namespace, data):
    """
    Loading function to get Gizmo as Input Process on viewer

    Arguments:
        context (dict): context of version
        name (str): name of the version
        namespace (str): namespace name
        data (dict): compulsory attribute > not used

    Returns:
        nuke node: containerized nuke node object
    """

    # get main variables
    version_entity = context["version"]

    version_attributes = version_entity["attrib"]
    first = version_attributes.get("frameStart")
    last = version_attributes.get("frameEnd")
    colorspace = version_attributes.get("colorSpace")

    namespace = namespace or context["folder"]["name"]
    object_name = "{}_{}".format(name, namespace)

    # prepare data for imprinting
    # add additional metadata from the version to imprint to metadata knob
    data_imprint = {
        "frameStart": first,
        "frameEnd": last,
        "version": version_entity["version"],
        "colorspaceInput": colorspace
    }

    for k in [
        "frameStart",
        "frameEnd",
        "handleStart",
        "handleEnd",
        "source",
        "fps"
    ]:
        data_imprint[k] = version_attributes[k]

    # getting file path
    file = self.filepath_from_context(context).replace("\\", "/")

    # adding nodes to node graph
    # just in case we are in group lets jump out of it
    nuke.endGroup()

    with maintained_selection():
        # add group from nk
        nuke.nodePaste(file)

        group_node = nuke.selectedNode()

        group_node["name"].setValue(object_name)

        # try to place it under Viewer1
        if not self.connect_active_viewer(group_node):
            nuke.delete(group_node)
            return

        return containerise(
            node=group_node,
            name=name,
            namespace=namespace,
            context=context,
            loader=self.__class__.__name__,
            data=data_imprint)

update(container, context)

Update the Loader's path

Nuke automatically tries to reset some variables when changing the loader's path to a new file. These automatic changes are to its inputs:

Source code in client/ayon_nuke/plugins/load/load_gizmo_ip.py
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
def update(self, container, context):
    """Update the Loader's path

    Nuke automatically tries to reset some variables when changing
    the loader's path to a new file. These automatic changes are to its
    inputs:

    """

    # get main variables
    # Get version from io
    project_name = context["project"]["name"]
    version_entity = context["version"]
    repre_entity = context["representation"]

    # get corresponding node
    group_node = container["node"]

    file = get_representation_path(repre_entity).replace("\\", "/")

    version_attributes = version_entity["attrib"]
    first = version_attributes.get("frameStart")
    last = version_attributes.get("frameEnd")
    colorspace = version_attributes.get("colorSpace")

    data_imprint = {
        "representation": repre_entity["id"],
        "frameStart": first,
        "frameEnd": last,
        "version": version_entity["version"],
        "colorspaceInput": colorspace
    }

    for k in [
        "frameStart",
        "frameEnd",
        "handleStart",
        "handleEnd",
        "source",
        "fps"
    ]:
        data_imprint[k] = version_attributes[k]

    # capture pipeline metadata
    avalon_data = get_avalon_knob_data(group_node)

    # adding nodes to node graph
    # just in case we are in group lets jump out of it
    nuke.endGroup()

    with maintained_selection([group_node]):
        # insert nuke script to the script
        nuke.nodePaste(file)
        # convert imported to selected node
        new_group_node = nuke.selectedNode()
        # swap nodes with maintained connections
        with swap_node_with_dependency(
                group_node, new_group_node) as node_name:
            new_group_node["name"].setValue(node_name)
            # set updated pipeline metadata
            set_avalon_knob_data(new_group_node, avalon_data)

    last_version_entity = ayon_api.get_last_version_by_product_id(
        project_name, version_entity["productId"], fields={"id"}
    )

    # change color of node
    if version_entity["id"] == last_version_entity["id"]:
        color_value = self.node_color
    else:
        color_value = "0xd88467ff"
    new_group_node["tile_color"].setValue(int(color_value, 16))

    self.log.info(
        "updated to version: {}".format(version_entity["version"])
    )

    return update_container(new_group_node, data_imprint)