2
$\begingroup$

I have a total of 7 objects with the 'Cube' as my active object -

'Cube', 'Camera', 'Light', 'A', 'B', 'C', 'D'

My main objective is to join A, B, C, D between them and create new objects. It is more like a combination problem. Thus, I will have six NEW joined objects along with the previous 7.

AB, AC, AD, BC, BD, CD

I am getting the objects of my interests by the following way

obs = [e for e in bpy.data.objects if e.type == 'MESH' and e != bpy.data.objects['Cube']]

# deselect every active object first
bpy.ops.object.select_all(action='DESELECT')

Now I have the objects and every active object deselected, how can I join two objects at a time without deleting or joining on the original objects?

Blender Version: 2.83

$\endgroup$
2
$\begingroup$

This will work only if all your objects share the same origin and have their transforms applied. Otherwise you will have to convert them using their transform matrices.

Similar to this answer.

We will use itertoolsand itertools.combinations to generate the wanted combinations.

Reference

enter image description here

Then use the bmesh module to concatenate the meshes. (Very important that all transforms be applied and origin at 0,0,0 !)

import bpy
import bmesh
import itertools

objects = bpy.context.selected_objects # Replace with your objects
objects_combinations = itertools.combinations(objects, 2)

for perm in enumerate(objects_combinations):
    this_perm = perm[1]
    new_mesh = bpy.data.meshes.new(this_perm[0].name + this_perm[1].name)
    
    new_bm = bmesh.new()
    new_bm.from_mesh(this_perm[0].data)
    new_bm.from_mesh(this_perm[1].data)
    new_bm.to_mesh(new_mesh)

    new_obj = bpy.data.objects.new(this_perm[0].name + this_perm[1].name, new_mesh)
    
    bpy.context.scene.collection.objects.link(new_obj)

    new_bm.free()

Result :

enter image description here

$\endgroup$
1
  • 2
    $\begingroup$ it worked without any issues. Thanks $\endgroup$ – Sourav Jul 28 '20 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.