Node Wrangler: Improved accuracy on Align Nodes operator #104551

Open
quackarooni wants to merge 18 commits from quackarooni/blender-addons:nw_rework_align_nodes into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
Showing only changes of commit 0c96a3fded - Show all commits

View File

@ -2366,6 +2366,23 @@ class NWAlignNodes(Operator, NWBase):
return nw_check(context) return nw_check(context)
@staticmethod
def get_midpoint(node, axis):
reroute_width = 10
weird_offset = 10
if axis == 'X':
width = reroute_width if (node.type == 'REROUTE') else node.dimensions.x
return node.location.x + (0.5 * width)
elif axis == 'Y':
if (node.type == 'REROUTE'):
return node.location.y - (0.5 * reroute_width)
elif node.hide:
return node.location.y - weird_offset
else:
return node.location.y - (0.5 * node.dimensions.y)
def execute(self, context): def execute(self, context):
selection = [node for node in context.selected_nodes if node.type != 'FRAME'] selection = [node for node in context.selected_nodes if node.type != 'FRAME']
active_node = context.active_node active_node = context.active_node
@ -2384,11 +2401,8 @@ class NWAlignNodes(Operator, NWBase):
if active_node in selection: if active_node in selection:
active_loc = copy(active_node.location) # make a copy, not a reference active_loc = copy(active_node.location) # make a copy, not a reference
# Check if nodes should be laid out horizontally or vertically x_locs = [self.get_midpoint(n, axis='X') for n in selection]
# use dimension to get center of node, not corner y_locs = [self.get_midpoint(n, axis='Y') for n in selection]
x_locs = [n.location.x + (0.5 * n.dimensions.x) for n in selection]
y_locs = [n.location.y - weird_offset if n.hide
else n.location.y - (0.5 * n.dimensions.y) for n in selection]
x_range = max(x_locs) - min(x_locs) x_range = max(x_locs) - min(x_locs)
y_range = max(y_locs) - min(y_locs) y_range = max(y_locs) - min(y_locs)
@ -2404,7 +2418,7 @@ class NWAlignNodes(Operator, NWBase):
selection.sort(key=lambda n: n.location.y - (n.dimensions.y / 2), reverse=True) selection.sort(key=lambda n: n.location.y - (n.dimensions.y / 2), reverse=True)
if self.mode != 'AUTOMATIC': if self.mode != 'AUTOMATIC':
horizontal = self.mode == 'HORIZONTAL' horizontal = (self.mode == 'HORIZONTAL')
# Alignment # Alignment
current_pos = 0 current_pos = 0
@ -2426,7 +2440,8 @@ class NWAlignNodes(Operator, NWBase):
for node in selection: for node in selection:
if node.type != 'REROUTE': if node.type != 'REROUTE':
node.location.x = mid_x - (0.5 * node.dimensions.x) node.location.x = mid_x - (0.5 * node.dimensions.x)
node.location.y = (current_pos - (0.5 * node.dimensions.y) + weird_offset) if node.hide else current_pos node.location.y = (current_pos - (0.5 * node.dimensions.y) +
weird_offset) if node.hide else current_pos
current_pos -= margin_y + node.dimensions.y current_pos -= margin_y + node.dimensions.y
else: else:
@ -2435,16 +2450,14 @@ class NWAlignNodes(Operator, NWBase):
current_pos -= margin_y + reroute_width current_pos -= margin_y + reroute_width
# If active node is selected, center nodes around it # If active node is selected, center nodes around it
if active_loc is not None: if active_loc is not None:
active_loc_diff = active_loc - active_node.location active_loc_diff = (active_loc - active_node.location)
for node in selection: for node in selection:
node.location += active_loc_diff node.location += active_loc_diff
else: else:
new_x_locs = [n.location.x + (0.5 * n.dimensions.x) for n in selection] new_x_locs = [self.get_midpoint(n, axis='X') for n in selection]
new_y_locs = [n.location.y - weird_offset if n.hide new_y_locs = [self.get_midpoint(n, axis='Y') for n in selection]
else n.location.y - (0.5 * n.dimensions.y) for n in selection]
new_x_mid = 0.5 * (max(new_x_locs) + min(new_x_locs)) new_x_mid = 0.5 * (max(new_x_locs) + min(new_x_locs))
new_y_mid = 0.5 * (max(new_y_locs) + min(new_y_locs)) new_y_mid = 0.5 * (max(new_y_locs) + min(new_y_locs))