Node Wrangler: Improved accuracy on Align Nodes operator #104551
@ -2340,8 +2340,8 @@ class NWAlignNodes(Operator, NWBase):
|
||||
bl_options = {'REGISTER', 'UNDO'}
|
||||
|
||||
mode: EnumProperty(
|
||||
name='Align Mode',
|
||||
default='AUTOMATIC',
|
||||
name='Align Mode',
|
||||
default='AUTOMATIC',
|
||||
items=(
|
||||
('AUTOMATIC', 'Auto-Align', ''),
|
||||
('HORIZONTAL', 'Align X', ''),
|
||||
@ -2366,6 +2366,23 @@ class NWAlignNodes(Operator, NWBase):
|
||||
|
||||
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):
|
||||
selection = [node for node in context.selected_nodes if node.type != 'FRAME']
|
||||
active_node = context.active_node
|
||||
@ -2374,21 +2391,18 @@ class NWAlignNodes(Operator, NWBase):
|
||||
|
||||
# Somehow hidden nodes would come out 10 units higher that non-hidden nodes when aligned, so this offset has to exist
|
||||
weird_offset = 10
|
||||
|
||||
|
||||
# node.dimensions for reroutes indicate (16.0, 16.0) but using that in calculations puts reroutes off-center
|
||||
# At least on a purely visual basis, the dimensions of a reroute node seem to be closer to 10 units. (At least for 1.0 unit scale)
|
||||
reroute_width = 10
|
||||
reroute_width = 10
|
||||
|
||||
with temporary_unframe(nodes=selection):
|
||||
active_loc = None
|
||||
if active_node in selection:
|
||||
active_loc = copy(active_node.location) # make a copy, not a reference
|
||||
|
||||
# Check if nodes should be laid out horizontally or vertically
|
||||
# use dimension to get center of node, not corner
|
||||
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_locs = [self.get_midpoint(n, axis='X') for n in selection]
|
||||
y_locs = [self.get_midpoint(n, axis='Y') for n in selection]
|
||||
|
||||
x_range = max(x_locs) - min(x_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)
|
||||
|
||||
if self.mode != 'AUTOMATIC':
|
||||
horizontal = self.mode == 'HORIZONTAL'
|
||||
horizontal = (self.mode == 'HORIZONTAL')
|
||||
|
||||
# Alignment
|
||||
current_pos = 0
|
||||
@ -2426,32 +2440,31 @@ class NWAlignNodes(Operator, NWBase):
|
||||
for node in selection:
|
||||
if node.type != 'REROUTE':
|
||||
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
|
||||
else:
|
||||
node.location.x = mid_x
|
||||
node.location.x = mid_x
|
||||
node.location.y = current_pos - (0.5 * reroute_width)
|
||||
|
||||
current_pos -= margin_y + reroute_width
|
||||
|
||||
|
||||
# If active node is selected, center nodes around it
|
||||
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:
|
||||
node.location += active_loc_diff
|
||||
else:
|
||||
new_x_locs = [n.location.x + (0.5 * n.dimensions.x) for n in selection]
|
||||
new_y_locs = [n.location.y - weird_offset if n.hide
|
||||
else n.location.y - (0.5 * n.dimensions.y) for n in selection]
|
||||
|
||||
new_x_locs = [self.get_midpoint(n, axis='X') for n in selection]
|
||||
new_y_locs = [self.get_midpoint(n, axis='Y') for n in selection]
|
||||
|
||||
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))
|
||||
|
||||
|
||||
x_diff = mid_x - new_x_mid
|
||||
y_diff = mid_y - new_y_mid
|
||||
|
||||
|
||||
for node in selection:
|
||||
node.location.x += x_diff
|
||||
node.location.y += y_diff
|
||||
|
Loading…
Reference in New Issue
Block a user