I’d like to stack about 10-12 different existing objects onto each other, as in the real world, as close as possible, but without intersections …
How’d I get there?
I’d like to stack about 10-12 different existing objects onto each other, as in the real world, as close as possible, but without intersections …
How’d I get there?
Kangaroo Mesh Collision.
However the solution may not be very tight. It’s a NP-hard (I believe it’s between NP-Hard and NPC) question after all.
I actually wouldn’t recommend the kangaroo mesh collision for this.
What are the solids like? Do they need to rotate, or is the stacking just translation?
mostly, or all, tubes. They should all fall down on the ground (0) and on each other and keep its position (needs to have friction, I suppose).
possibility to re-arrange is a plus.
Ah, in that case, it might be possible with Kangaroo, not using meshes, but the ‘Collider’ goal which can take lines as input and treat them as solid cylinders.
As it’s first-time use, does anyone have a newbie primer?
Here’s an example of stacking some cylinders:
cylinder_pile.gh (22.0 KB)
It’s without friction though, so might not be suitable for your application.
I used ChatGPT to generate a Python script that stacks objects along an axis. You can select the axis and spacing. I haven’t tested it out too much on things other than simple polysurfaces:
import rhinoscriptsyntax as rs
def get_face_midpoint(p, axis_index, get_max=False):
# Explode the object into faces
faces = rs.ExplodePolysurfaces(p, delete_input=False)
if not faces:
print("Cannot explode object:", p)
return None
corners = []
for face in faces:
bbox = rs.BoundingBox(face)
rs.DeleteObject(face) # Clean up exploded faces
if not bbox:
continue
# Calculate the midpoint of bbox[0] and bbox[6]
p1 = bbox[0]
p2 = bbox[6]
mid_x = (p1[0] + p2[0]) / 2.0
mid_y = (p1[1] + p2[1]) / 2.0
mid_z = (p1[2] + p2[2]) / 2.0
midpoint = [mid_x, mid_y, mid_z]
corners.append(midpoint)
if not corners:
return None
if get_max:
selected = max(corners, key=lambda c: c[axis_index])
else:
selected = min(corners, key=lambda c: c[axis_index])
return selected
def run_stack():
# Select objects
objs = rs.GetObjects("Select polysurfaces to stack", rs.filter.polysurface)
if not objs:
rs.MessageBox("No objects selected.")
return
# Choose axis
axis_choice = rs.GetString("Choose axis to stack along (X, Y, Z):", "Z").upper()
axes_map = {'X': (1, 0, 0), 'Y': (0, 1, 0), 'Z': (0, 0, 1)}
if axis_choice not in axes_map:
rs.MessageBox("Invalid axis, defaulting to Z.")
axis_choice = 'Z'
axis_vec = axes_map[axis_choice]
axis_idx = {'X': 0, 'Y': 1, 'Z': 2}[axis_choice]
spacing = rs.GetReal("Spacing between objects", 0)
# Gather face midpoints for each object
obj_info = []
for p in objs:
bottom = get_face_midpoint(p, axis_idx, get_max=False)
if bottom:
obj_info.append({'obj': p, 'bottom': bottom})
print("Object:", p, "bottom face at:", bottom)
else:
rs.MessageBox("Failed to find face for object: {}".format(p))
return
# Sort by bottom face along the chosen axis
obj_info.sort(key=lambda o: o['bottom'][axis_idx])
# Bottom of the lowest object
base_point = obj_info[0]['bottom']
base_coord = base_point[axis_idx]
# Stack objects
for i in range(1, len(obj_info)):
prev = obj_info[i - 1]
curr = obj_info[i]
prev_top = get_face_midpoint(prev['obj'], axis_idx, get_max=True)
prev_top_y = prev_top[axis_idx]
curr_bottom = curr['bottom']
curr_bottom_y = curr_bottom[axis_idx]
offset = (prev_top_y + spacing) - curr_bottom_y
move_vector = rs.VectorScale(axis_vec, offset)
rs.MoveObject(curr['obj'], move_vector)
# Update current bottom position after move
new_bottom = [curr['bottom'][0] + move_vector[0],
curr['bottom'][1] + move_vector[1],
curr['bottom'][2] + move_vector[2]]
curr['bottom'] = new_bottom
print("Moved object:", curr['obj'], "by:", move_vector)
rs.MessageBox("Stacking complete!")
# Run the script
run_stack()