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 8e834b4433 - Show all commits

View File

@ -2359,49 +2359,57 @@ class NWAlignNodes(Operator, NWBase):
# Check if nodes should be laid out horizontally or vertically
# use dimension to get center of node, not corner
x_locs = [n.location.x + (n.dimensions.x / 2) for n in selection]
y_locs = [n.location.y - (n.dimensions.y / 2) for n in selection]
x_locs = [n.location.x + (0.5 * n.dimensions.x) for n in selection]
y_locs = [n.location.y - (0.5 * n.dimensions.y) for n in selection]
x_range = max(x_locs) - min(x_locs)
y_range = max(y_locs) - min(y_locs)
mid_x = (max(x_locs) + min(x_locs)) / 2
mid_y = (max(y_locs) + min(y_locs)) / 2
mid_x = 0.5 * (max(x_locs) + min(x_locs))
mid_y = 0.5 * (max(y_locs) + min(y_locs))
horizontal = x_range > y_range
# Sort selection by location of node mid-point
if horizontal:
selection = sorted(selection, key=lambda n: n.location.x + (n.dimensions.x / 2))
selection.sort(key=lambda n: n.location.x + (n.dimensions.x / 2))
else:
selection = sorted(selection, 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)
# Alignment
current_pos = 0
for node in selection:
current_margin = margin
current_margin = current_margin * 0.5 if node.hide else current_margin # use a smaller margin for hidden nodes
if horizontal:
for node in selection:
current_margin = (0.5 * margin) if node.hide else margin
node.location.x = current_pos
node.location.y = mid_y + (0.5 * node.dimensions.y)
current_pos += current_margin + node.dimensions.x
node.location.y = mid_y + (node.dimensions.y / 2)
else:
for node in selection:
current_margin = (0.5 * margin) if node.hide else margin
node.location.y = current_pos
node.location.x = mid_x - (0.5 * node.dimensions.x)
current_pos -= (current_margin * 0.3) + node.dimensions.y # use half-margin for vertical alignment
node.location.x = mid_x - (node.dimensions.x / 2)
# If active node is selected, center nodes around it
if active_loc is not None:
active_loc_diff = active_loc - active_node.location
for node in selection:
node.location += active_loc_diff
else: # Position nodes centered around where they used to be
locs = ([n.location.x + (n.dimensions.x / 2) for n in selection]
) if horizontal else ([n.location.y - (n.dimensions.y / 2) for n in selection])
new_mid = (max(locs) + min(locs)) / 2
elif horizontal: # Position nodes centered around where they used to be
new_locs = [n.location.x + (0.5 * n.dimensions.x) for n in selection]
new_mid = 0.5 * (max(new_locs) + min(new_locs))
x_diff = mid_x - new_mid
for node in selection:
if horizontal:
node.location.x += (mid_x - new_mid)
node.location.x += x_diff
else:
node.location.y += (mid_y - new_mid)
new_locs = [n.location.y - (0.5 * n.dimensions.y) for n in selection]
new_mid = 0.5 * (max(new_locs) + min(new_locs))
y_diff = mid_y - new_mid
for node in selection:
node.location.y += y_diff
return {'FINISHED'}