Here's a solution. It appends an instance of the group (Blender 2.7x only):
import bpy filepath = "/path/to/file.blend" group_name = "CubeGroup" # append, set to true to keep the link to the original file link = False # append all groups from the .blend file with bpy.data.libraries.load(filepath, link=link) as (data_src, data_dst): ## all groups # data_to.groups = data_from.groups # only append a single group we already know the name of data_dst.groups = [group_name] # add the group instance to the scene for group in data_dst.groups: ob = bpy.data.objects.new(group.name, None) ob.dupli_group = group ob.dupli_type = 'GROUP' bpy.context.scene.objects.link(ob) # Blender 2.7x
Credit: solution is based on this answer: Import object without bpy.ops.wm.link_append
It's basically the same for objects:
import bpy # path to the blend filepath = "/path/to/file.blend" # name of object(s) to append or link obj_name = "Cube" # append, set to true to keep the link to the original file link = False # link all objects starting with 'Cube' with bpy.data.libraries.load(filepath, link=link) as (data_from, data_to): data_to.objects = [name for name in data_from.objects if name.startswith(obj_name)] #link object to current scene for obj in data_to.objects: if obj is not None: #bpy.context.scene.objects.link(obj) # Blender 2.7x bpy.context.collection.objects.link(obj) # Blender 2.8x
As of Blender 2.8 groups have been replaced by the new collection system:
import bpy # path to the blend filepath = "/path/to/file.blend" # name of collection(s) to append or link coll_name = "MyCollection" # append, set to true to keep the link to the original file link = False # link all collections starting with 'MyCollection' with bpy.data.libraries.load(filepath, link=link) as (data_from, data_to): data_to.collections = [c for c in data_from.collections if c.startswith(coll_name)] # link collection to scene collection for coll in data_to.collections: if coll is not None: bpy.context.scene.collection.children.link(coll)
Further information: https://www.blender.org/api/current/bpy.types.BlendDataLibraries.html