Created
April 23, 2024 07:00
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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