Skip to content

extract_backdrop

ExtractBackdropNode

Bases: Extractor

Extracting content of backdrop nodes

Will create nuke script only with containing nodes. Also it will solve Input and Output nodes.

Source code in client/ayon_nuke/plugins/publish/extract_backdrop.py
 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
class ExtractBackdropNode(publish.Extractor):
    """Extracting content of backdrop nodes

    Will create nuke script only with containing nodes.
    Also it will solve Input and Output nodes.

    """

    order = pyblish.api.ExtractorOrder
    label = "Extract Backdrop"
    hosts = ["nuke"]
    families = ["nukenodes"]

    settings_category = "nuke"

    def process(self, instance):
        tmp_nodes = []
        child_nodes = instance.data["transientData"]["childNodes"]
        # all connections outside of backdrop
        connections_in = instance.data["transientData"]["nodeConnectionsIn"]
        connections_out = instance.data["transientData"]["nodeConnectionsOut"]
        self.log.debug("_ connections_in: `{}`".format(connections_in))
        self.log.debug("_ connections_out: `{}`".format(connections_out))

        # Define extract output file path
        stagingdir = self.staging_dir(instance)
        filename = "{0}.nk".format(instance.name)
        path = os.path.join(stagingdir, filename)

        # maintain selection
        with maintained_selection():
            # create input child_nodes and name them as passing node (*_INP)
            for n, inputs in connections_in.items():
                for i, input in inputs:
                    inpn = nuke.createNode("Input")
                    inpn["name"].setValue("{}_{}_INP".format(n.name(), i))
                    n.setInput(i, inpn)
                    inpn.setXYpos(input.xpos(), input.ypos())
                    child_nodes.append(inpn)
                    tmp_nodes.append(inpn)

            reset_selection()

            # connect output node
            for n, output in connections_out.items():
                opn = nuke.createNode("Output")
                output.setInput(
                    next((i for i, d in enumerate(output.dependencies())
                          if d.name() in n.name()), 0), opn)
                opn.setInput(0, n)
                opn.autoplace()
                child_nodes.append(opn)
                tmp_nodes.append(opn)
                reset_selection()

            # select child_nodes to copy
            reset_selection()
            select_nodes(child_nodes)
            # create tmp nk file
            # save file to the path
            nuke.nodeCopy(path)

            # Clean up
            for tn in tmp_nodes:
                nuke.delete(tn)

            # restore original connections
            # reconnect input node
            for n, inputs in connections_in.items():
                for i, input in inputs:
                    n.setInput(i, input)

            # reconnect output node
            for n, output in connections_out.items():
                output.setInput(
                    next((i for i, d in enumerate(output.dependencies())
                          if d.name() in n.name()), 0), n)

        if "representations" not in instance.data:
            instance.data["representations"] = []

        # create representation
        representation = {
            'name': 'nk',
            'ext': 'nk',
            'files': filename,
            "stagingDir": stagingdir
        }
        instance.data["representations"].append(representation)

        self.log.debug("Extracted instance '{}' to: {}".format(
            instance.name, path))