10

I'm searching a way to append a group in Python.

It should behave exactly like the Append function (ShiftF1) works, all objects should be local and editable. The idea is to append a character (with rig etc.) via Python and not Link it (to be able to modify it without making everything local one by one).

Thanks

ideasman42
  • 47,387
  • 10
  • 141
  • 223
Boris
  • 605
  • 5
  • 8

1 Answers1

22

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

brockmann
  • 12,613
  • 4
  • 50
  • 93
Todd McIntosh
  • 9,421
  • 25
  • 50
  • 3
    Sorry but this don't make it. This is the solution i have already, that's why i specified "behave like the append operator(shift F1) with everything local/editable". Here it behave like a link (ctrl alt o). i ll edit my first post to be more clear – Boris Jul 26 '15 at 09:55
  • 6
    @Boris, There is a link argument, Set it to False to behave like Append – ideasman42 Sep 05 '15 at 12:52
  • In Blender 2.8 replace bpy.context.scene.objects.link(obj) with bpy.context.collection.objects.link(obj) – João Cartucho Jan 09 '20 at 18:10
  • @brockmann Re edit, TM chose tthe one? datablock type that has changed... groups become collections to make it 2.8+ compatico. – batFINGER Jun 03 '20 at 18:57
  • Ooops, thanks @batFINGER Added a collection example – brockmann Jun 04 '20 at 12:16
  • Is there a way to do this in Blender 4? My old way no longer works. – MisterLBlends Mar 02 '24 at 20:21