Skip to content

Instantly share code, notes, and snippets.

@promto-c
Created July 12, 2024 19:48
Show Gist options
  • Save promto-c/590bb1f81ac1a0ff7527780e6ae9f565 to your computer and use it in GitHub Desktop.
Save promto-c/590bb1f81ac1a0ff7527780e6ae9f565 to your computer and use it in GitHub Desktop.
Python script for aligning selected nodes in Nuke horizontally or vertically based on user preferences. Includes functionality to toggle alignment and retrieve grid spacing settings from Nuke's preferences.
import nuke
from typing import List, Tuple
class NodeAlignment:
"""Handles the alignment of nodes in Nuke."""
DEFAULT_SPACING = 100
@staticmethod
def get_default_spacing() -> Tuple[int, int]:
"""Retrieves the default grid width and height from the preferences node.
Returns:
Tuple[int, int]: The default grid width and height values.
"""
preferences_node = nuke.toNode('preferences')
if preferences_node:
grid_width = int(preferences_node.knob('GridWidth').value())
grid_height = int(preferences_node.knob('GridHeight').value())
return grid_width, grid_height
return NodeAlignment.DEFAULT_SPACING, NodeAlignment.DEFAULT_SPACING
@staticmethod
def toggle_alignment(spacing: int = None) -> None:
"""Toggles the alignment of selected nodes between horizontal and vertical.
Args:
spacing (int, optional): The space between nodes when aligning. If not provided, uses the default grid width.
"""
grid_width, grid_height = NodeAlignment.get_default_spacing()
if spacing is None:
spacing = grid_width
nodes: List[nuke.Node] = nuke.selectedNodes()
if not nodes:
return
is_horizontal_align = all(node.ypos() == nodes[0].ypos() for node in nodes)
if is_horizontal_align:
NodeAlignment.align_vertically(nodes, grid_height)
else:
NodeAlignment.align_horizontally(nodes, grid_width)
@staticmethod
def align_vertically(nodes: List[nuke.Node], spacing: int) -> None:
"""Aligns the given nodes vertically.
Args:
nodes (list): List of nodes to align.
spacing (int): The space 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() + int(index * spacing))
@staticmethod
def align_horizontally(nodes: List[nuke.Node], spacing: int) -> None:
"""Aligns the given nodes horizontally.
Args:
nodes (list): List of nodes to align.
spacing (int): The space between nodes.
"""
nodes.sort(key=lambda node: node.ypos())
y_pos = nodes[0].ypos()
for index, node in enumerate(nodes):
node.setXpos(nodes[0].xpos() + int(index * spacing))
node.setYpos(y_pos)
if __name__ == '__main__':
nuke.menu('Nuke').addCommand(
'Edit/Node/Toggle Nodes Alignment',
NodeAlignment.toggle_alignment,
'L',
shortcutContext=2
)
import nuke
class NodeAlignment(object):
"""Handles the alignment of nodes in Nuke."""
DEFAULT_SPACING = 100
@staticmethod
def get_default_spacing():
"""Retrieves the default grid width and height from the preferences node.
Returns:
tuple: The default grid width and height values.
"""
preferences_node = nuke.toNode('preferences')
if preferences_node:
grid_width = int(preferences_node.knob('GridWidth').value())
grid_height = int(preferences_node.knob('GridHeight').value())
return grid_width, grid_height
return NodeAlignment.DEFAULT_SPACING, NodeAlignment.DEFAULT_SPACING
@staticmethod
def toggle_alignment(spacing=None):
"""Toggles the alignment of selected nodes between horizontal and vertical.
Args:
spacing (int, optional): The space between nodes when aligning. If not provided, uses the default grid width.
"""
grid_width, grid_height = NodeAlignment.get_default_spacing()
if spacing is None:
spacing = grid_width
nodes = nuke.selectedNodes()
if not nodes:
return
is_horizontal_align = all(node.ypos() == nodes[0].ypos() for node in nodes)
if is_horizontal_align:
NodeAlignment.align_vertically(nodes, grid_height)
else:
NodeAlignment.align_horizontally(nodes, grid_width)
@staticmethod
def align_vertically(nodes, spacing):
"""Aligns the given nodes vertically.
Args:
nodes (list): List of nodes to align.
spacing (int): The space 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() + int(index * spacing))
@staticmethod
def align_horizontally(nodes, spacing):
"""Aligns the given nodes horizontally.
Args:
nodes (list): List of nodes to align.
spacing (int): The space between nodes.
"""
nodes.sort(key=lambda node: node.ypos())
y_pos = nodes[0].ypos()
for index, node in enumerate(nodes):
node.setXpos(nodes[0].xpos() + int(index * spacing))
node.setYpos(y_pos)
if __name__ == '__main__':
nuke.menu('Nuke').addCommand(
'Edit/Node/Toggle Nodes Alignment',
NodeAlignment.toggle_alignment,
'L',
shortcutContext=2
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment