but results i get if select points “From other” using “indexof” and draw a line to visuelize i have noticed that 50% are correct and there is issue where it seams checking against same point and results that 50% seams to get allocated to same point.
i have used python as well but results are smae regardless if i use OTTB notes.
import math
from itertools import product
# Inputs: IN = Targets (List A), IN = Candidates (List B)
targets = IN[0]
candidates = IN[1]
def get_coords_safe(item):
"""Safely extracts X,Y,Z coordinates and original object, returns None if invalid."""
if item is None: return None
try:
# Return coordinates and the original item reference as a tuple
return (item.X, item.Y, item.Z, item)
except AttributeError:
return None
except Exception:
return None
# Prepare Input B data once for performance
b_data = [get_coords_safe(c) for c in candidates]
b_data = [d for d in b_data if d is not None] # Filter all invalid candidates
results = []
# --- EXPLICIT CROSS-PRODUCT LOGIC ---
# Use itertools.product to generate every possible pair (Target, Candidate)
# We also use enumerate to keep track of the original list indices (idx_a and idx_b)
for idx_a, t_data in enumerate(targets):
a_data = get_coords_safe(t_data)
if not a_data: continue # Skip if Target A is invalid
ax, ay, az, a_obj = a_data
min_dist_sq = float('inf')
best_idx_b = -1 # This tracks the original index of the winner in List B
best_point_b = None
# Iterate through every item in B again (the 'inner loop' of the cross product)
for idx_b, b_data_tuple in enumerate(b_data):
bx, by, bz, b_obj = b_data_tuple
# SQUARED DISTANCE: Avoids expensive math.sqrt() during comparison
d_sq = (bx - ax)**2 + (by - ay)**2 + (bz - az)**2
if d_sq < min_dist_sq:
min_dist_sq = d_sq
best_idx_b = idx_b # Update the index of the winner
best_point_b = b_obj
# Finalise result for this target A item
if best_idx_b != -1:
dist = math.sqrt(min_dist_sq) # Only perform sqrt once
# Result format: [Index A, Index B, Closest Point, Distance]
results.append([idx_a, best_idx_b, best_point_b, dist])
else:
results.append([idx_a, -1, None, None])
OUT = results
All closest points doesn’t really make sense.
You might ask the 5 closest points for example. Then you would sort the list and then get the 5 first item.
Im trying to get Number of points closest to BB centre point. Number of point varies on each BB.
Im partially there but not there at the same time as i have this weird behaviour.
This is how intended result should look like Marked in red it works 50% but seams like some points just reference to same BB center hence i cant figure out why its doing it or its me doing wrong here.
Note you will still want to deal with edge cases (i.e. a point on the limit of bounding box 1 and bounding box 2; a point N units from bounding box 2 and bounding box 3, etc.).
Sadly private support requires a consulting project via my employer (Autodesk), and I’m assuming you’d have reached out via those channels if you were working on such a project. General community knowledge contribution in the open forum is doable though.
To get around this, how about you pack the points into a Data.Remember node, then pack the bounding boxes into another Data.Remember node. Then save your graph as a new file, delete EVERYTHING leading into the Data.Remember nodes, and post that file. It’ll put points and bounding boxes into the public domain, but keep anything project specific out of circulation.