How to merge two or more groups to one using Python API? Having a large amount of objects in several groups, not being instantiated, I would like to merge them all to a single group. What is the most efficient way to do it using Python API?

EDIT

A minimal example would be:


Create a scene with two cubes, add them to Group1, save the file as file1.blend.
Create a new file with another two cubes, add them to Group2, save the file as file2.blend.
Start a new file and load groups from both using

with bpy.data.libraries.load('file1.blend', link=False) as (data_src, data_dst):
    data_dst.groups = data_src.groups
with bpy.data.libraries.load('file2.blend', link=False) as (data_src, data_dst):
    data_dst.groups = data_src.groups

What is the most efficient way using Python API to merge Group1 and Group2 to a single group?
After trying some more elegant methods and failing, this is a brute force solution to what you're trying to achieve. It essentially appends all the objects from the provided list of files (without their groups), and then adds all the objects into one group called 'merged'. It's can probably be solved more elegantly with groups, but since this worked for your example, I stopped trying to find other workarounds.

import bpy
from os.path import join

for g in bpy.data.groups:
    g.user_clear()
    bpy.data.groups.remove( g )

baseDir = "path/to/blendfiles"

allObjects = []

files = ['f1.blend', 'f2.blend']
for f in files:
    fPath = join( baseDir, f )

    objects = []
    with bpy.data.libraries.load( fPath ) as (data_from, data_to):
        for name in data_from.objects:
            objects.append({'name': name})

    bpy.ops.wm.append( directory = fPath + "/Object/", files = objects )

mergedGroup = bpy.data.groups.new('merged')
for o in bpy.data.objects:
    mergedGroup.objects.link( o )