Skip to content

utils

bake_gizmos_recursively(in_group=None)

Converting a gizmo to group

Parameters:

Name Type Description Default
in_group Optional[Node]

group node or all nodes

None
Source code in client/ayon_nuke/api/utils.py
 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
def bake_gizmos_recursively(in_group=None):
    """Converting a gizmo to group

    Arguments:
        in_group (Optional[nuke.Node]): group node or all nodes
    """
    from .lib import maintained_selection
    if in_group is None:
        in_group = nuke.Root()
    # preserve selection after all is done
    with maintained_selection():
        # jump to the group
        with in_group:
            for node in nuke.allNodes():
                if is_node_gizmo(node) and not gizmo_is_nuke_default(node):
                    with node:
                        outputs = get_node_outputs(node)
                        group = node.makeGroup()
                        # Reconnect inputs and outputs if any
                        if outputs:
                            for n, pipes in outputs.items():
                                for i in pipes:
                                    n.setInput(i, group)
                        for i in range(node.inputs()):
                            group.setInput(i, node.input(i))
                        # set node position and name
                        group.setXYpos(node.xpos(), node.ypos())
                        name = node.name()
                        nuke.delete(node)
                        group.setName(name)
                        node = group

                if node.Class() == "Group":
                    bake_gizmos_recursively(node)

get_node_outputs(node)

Return a dictionary of the nodes and pipes that are connected to node

Source code in client/ayon_nuke/api/utils.py
41
42
43
44
45
46
47
48
49
50
51
52
def get_node_outputs(node):
    """
    Return a dictionary of the nodes and pipes that are connected to node
    """
    dep_dict = {}
    dependencies = node.dependent(nuke.INPUTS | nuke.HIDDEN_INPUTS)
    for d in dependencies:
        dep_dict[d] = []
        for i in range(d.inputs()):
            if d.input(i) == node:
                dep_dict[d].append(i)
    return dep_dict

gizmo_is_nuke_default(gizmo)

Check if gizmo is in default install path

Source code in client/ayon_nuke/api/utils.py
60
61
62
63
64
def gizmo_is_nuke_default(gizmo) -> bool:
    """Check if gizmo is in default install path"""
    plug_dir = os.path.join(os.path.dirname(
        nuke.env['ExecutablePath']), 'plugins')
    return gizmo.filename().startswith(plug_dir)

is_node_gizmo(node)

Return True if the node is a gizmo.

Source code in client/ayon_nuke/api/utils.py
55
56
57
def is_node_gizmo(node) -> bool:
    """Return True if the node is a gizmo."""
    return 'gizmo_file' in node.knobs()

render_single_frame(group_node)

Render a single frame for the given group node.

Parameters:

Name Type Description Default
group_node Node

The group node containing the write

required
Source code in client/ayon_nuke/api/utils.py
13
14
15
16
17
18
19
20
21
22
def render_single_frame(group_node: nuke.Node) -> None:
    """Render a single frame for the given group node.

    Args:
        group_node (nuke.Node): The group node containing the write
        node to render.
    """
    write_node = nuke.allNodes("Write", group=group_node)[0]
    frame = int(write_node["first"].value())
    nuke.execute(write_node.fullName(), frame, frame)

set_context_favorites(favorites=None)

Adding favorite folders to nuke's browser

Parameters:

Name Type Description Default
favorites dict

couples of {name:path}

None
Source code in client/ayon_nuke/api/utils.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def set_context_favorites(favorites=None):
    """Adding favorite folders to nuke's browser

    Arguments:
        favorites (dict): couples of {name:path}
    """
    favorites = favorites or {}
    icon_path = resources.get_resource("icons", "folder-favorite.png")
    for name, path in favorites.items():
        nuke.addFavoriteDir(
            name,
            path,
            nuke.IMAGE | nuke.SCRIPT | nuke.GEO,
            icon=icon_path)