1
$\begingroup$

I've got around 400 objects in a scene and I would like to join them in groups of 5 or 10 (ie. so there are either 80 objects or 40 objects respectively) using Python. Then unwrap the new objects with UV Smart Project, but I really just need help setting up the looping and grouping part.

$\endgroup$

1 Answer 1

1
$\begingroup$

I'm sure it can be better but here's something that should work... see that you can't make a list of objects and go joining them cause the list will reference stuff that is not there any more..? so we make a list of names and pick objects later that way

can remove the try: except:pass thing if you know the number of objects is a multiple of your step... also, see this only sorts / join objects by name

import bpy
step = 5
objs = bpy.context.view_layer.objects
names = [o.name for o in objs if o.type == 'MESH']

bpy.ops.object.select_all(action='DESELECT')

for i in range(0, len(names), step):
    objs.active = objs.get(names[i])
    for j in range(step):
        try: objs.get(names[i+j]).select_set(True)
        except: pass
    bpy.ops.object.join()
    bpy.ops.object.select_all(action='DESELECT')
$\endgroup$
1
  • $\begingroup$ This seems reasonable to me. Would just replace third line with objs = [o for o in bpy.context.selected_objects if o.type == "MESH"] since the Q asked for looping over the selected objects, and I assume they meant mesh objects since the goal is to join them. $\endgroup$
    – Gorgious
    Feb 18, 2022 at 6:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .