Node Wrangler: Improved accuracy on Align Nodes operator #104551
@ -2340,8 +2340,8 @@ class NWAlignNodes(Operator, NWBase):
|
|||||||
bl_options = {'REGISTER', 'UNDO'}
|
bl_options = {'REGISTER', 'UNDO'}
|
||||||
|
|
||||||
mode: EnumProperty(
|
mode: EnumProperty(
|
||||||
name='Align Mode',
|
name='Align Mode',
|
||||||
default='AUTOMATIC',
|
default='AUTOMATIC',
|
||||||
items=(
|
items=(
|
||||||
('AUTOMATIC', 'Auto-Align', ''),
|
('AUTOMATIC', 'Auto-Align', ''),
|
||||||
('HORIZONTAL', 'Align X', ''),
|
('HORIZONTAL', 'Align X', ''),
|
||||||
@ -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
|
||||||
@ -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
|
# Somehow hidden nodes would come out 10 units higher that non-hidden nodes when aligned, so this offset has to exist
|
||||||
weird_offset = 10
|
weird_offset = 10
|
||||||
|
|
||||||
# node.dimensions for reroutes indicate (16.0, 16.0) but using that in calculations puts reroutes off-center
|
# 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)
|
# 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):
|
with temporary_unframe(nodes=selection):
|
||||||
active_loc = None
|
active_loc = None
|
||||||
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,32 +2440,31 @@ 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:
|
||||||
node.location.x = mid_x
|
node.location.x = mid_x
|
||||||
node.location.y = current_pos - (0.5 * reroute_width)
|
node.location.y = current_pos - (0.5 * reroute_width)
|
||||||
|
|
||||||
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))
|
||||||
|
|
||||||
x_diff = mid_x - new_x_mid
|
x_diff = mid_x - new_x_mid
|
||||||
y_diff = mid_y - new_y_mid
|
y_diff = mid_y - new_y_mid
|
||||||
|
|
||||||
for node in selection:
|
for node in selection:
|
||||||
node.location.x += x_diff
|
node.location.x += x_diff
|
||||||
node.location.y += y_diff
|
node.location.y += y_diff
|
||||||
|
Loading…
Reference in New Issue
Block a user