Skip to content

Instantly share code, notes, and snippets.

@promto-c
Created April 23, 2024 07:00
Show Gist options
  • Save promto-c/4051732ee10eaffb91cd0fca3abacc41 to your computer and use it in GitHub Desktop.
Save promto-c/4051732ee10eaffb91cd0fca3abacc41 to your computer and use it in GitHub Desktop.
A Python script to dynamically switch selected nodes between horizontal and vertical alignment using the 'L' shortcut, with configurable spacing.
import nuke
def toggle_nodes_alignment(spacing=100):
"""Toggle the alignment of selected nodes between horizontal and vertical.
Args:
spacing (int): The spacing between nodes after alignment.
"""
nodes = nuke.selectedNodes()
if not nodes:
return
# Check if nodes are aligned horizontally by comparing y positions
is_horizontal_align = all(node.ypos() == nodes[0].ypos() for node in nodes)
if is_horizontal_align:
# If aligned horizontally, align them vertically
align_nodes_vertically(nodes, spacing)
else:
# If not aligned horizontally, align them horizontally
align_nodes_horizontally(nodes, spacing)
def align_nodes_vertically(nodes, spacing=100):
"""Align nodes vertically with specified spacing.
Args:
nodes (list): A list of node objects to be aligned.
spacing (int): The vertical spacing between nodes.
"""
nodes.sort(key=lambda node: node.xpos())
x_pos = nodes[0].xpos()
for index, node in enumerate(nodes):
node.setXpos(x_pos)
node.setYpos(nodes[0].ypos() + index * spacing)
def align_nodes_horizontally(nodes, spacing=100):
"""Align nodes horizontally with specified spacing.
Args:
nodes (list): A list of node objects to be aligned.
spacing (int): The horizontal spacing between nodes.
"""
nodes.sort(key=lambda node: node.ypos())
y_pos = nodes[0].ypos()
for index, node in enumerate(nodes):
node.setYpos(y_pos)
node.setXpos(nodes[0].xpos() + index * spacing)
if __name__ == '__main__':
# Add the command to Nuke's menu with a keyboard shortcut
nuke.menu('Nuke').addCommand('Edit/Node/Toggle Nodes Alignment', toggle_nodes_alignment, 'L')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment