10
$\begingroup$

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

$\endgroup$
2
  • $\begingroup$ Would you know ahead of time the blend file and path, as well as the exact group name that you want to append? Or do you need some type of browsing interface? $\endgroup$ Commented Jul 25, 2015 at 22:42
  • $\begingroup$ I already know blend, path and group name $\endgroup$
    – Boris
    Commented Jul 25, 2015 at 23:01

1 Answer 1

22
$\begingroup$

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

$\endgroup$
6
  • 3
    $\begingroup$ 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 $\endgroup$
    – Boris
    Commented Jul 26, 2015 at 9:55
  • 6
    $\begingroup$ @Boris, There is a link argument, Set it to False to behave like Append $\endgroup$
    – ideasman42
    Commented Sep 5, 2015 at 12:52
  • $\begingroup$ In Blender 2.8 replace bpy.context.scene.objects.link(obj) with bpy.context.collection.objects.link(obj) $\endgroup$ Commented Jan 9, 2020 at 18:10
  • $\begingroup$ @brockmann Re edit, TM chose tthe one? datablock type that has changed... groups become collections to make it 2.8+ compatico. $\endgroup$
    – batFINGER
    Commented Jun 3, 2020 at 18:57
  • $\begingroup$ Ooops, thanks @batFINGER Added a collection example $\endgroup$
    – brockmann
    Commented Jun 4, 2020 at 12:16

You must log in to answer this question.

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