0
$\begingroup$

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()
$\endgroup$
4
  • 1
    $\begingroup$ the problem with your question is: it's not clear. imagine you have a line of objects, all on x axis with the same distance: e.g. .2. Is the result 1 object? or multiple? because if i first "check" e.g. object 2 and 3 and join them, then they aren't near enough to be joined by other objects, so the order of checking the objects would be important. $\endgroup$
    – Chris
    Jan 20, 2023 at 7:04
  • 1
    $\begingroup$ ... and do you mean the distance between the object-origins, or geometry? $\endgroup$
    – Robin Betts
    Jan 20, 2023 at 10:09
  • $\begingroup$ @RobinBetts both distances would be fine. Distance between geometry would be better but between the object origins are also fine. $\endgroup$
    – lianiu
    Jan 20, 2023 at 14:32
  • $\begingroup$ @Chris objects that are not near enough should then not be joined. The result would be multiple individual joined objects, if there are multiple objects in the scene that have large (a certain) distances between them. $\endgroup$
    – lianiu
    Jan 20, 2023 at 14:37

0

You must log in to answer this question.

Browse other questions tagged .