I am trying to join objects by distance with a script similiar to this post Join objects that share location
but instead of joining objects that share the origin, I need to join them by distance (distance between the geometry or the origin). E.g. if three objects are less than 0.3 metre apart from each other in the scene, the script will join them to one object. So if my scene has multiple objects with large distances between them, the result should be multiple individual joined objects. Objects that are not near enough to other objects should not be joined then. The script should do this with all objects in the scene that are near to each other (e.g. objects that are less than 0.3 metre apart from each other).
Does someone know how to make such a script? If joining by distance is not possible, then joining objects that share the parent would work too.
I found this script, which joines objects that share the origin. Maybe this can be useful.
import bpy
dic_obj_loc = {ob : tuple(ob.location) for ob in bpy.context.view_layer.objects if ob.visible_get() and ob.type == 'MESH'}
# get same location dict
rev_multidict = {}
for k, v in dic_obj_loc.items():
rev_multidict.setdefault(v, set()).add(k)
for k, objs in rev_multidict.items():
print(k, objs)
print()
if len(objs) == 1: continue
bpy.ops.object.select_all(action='DESELECT')
for obj in objs:
obj.select_set(True)
bpy.context.view_layer.objects.active = bpy.context.selected_objects[0]
bpy.ops.object.join()