6
$\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$
  • $\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$ – Todd McIntosh Jul 25 '15 at 22:42
  • $\begingroup$ I already know blend, path and group name $\endgroup$ – Boris Jul 25 '15 at 23:01
12
$\begingroup$

Here's a solution. It appends an instance of the group:

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
scene = bpy.context.scene
for group in data_dst.groups:
    ob = bpy.data.objects.new(group.name, None)
    ob.dupli_group = group
    ob.dupli_type = 'GROUP'
    scene.objects.link(ob)

Credit: solution is adapted from code from this answer: Import object without bpy.ops.wm.link_append


It's basically the same for objects:

import bpy

# current scene
scn = bpy.context.scene

# 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:
       scn.objects.link(obj)

Further information: https://www.blender.org/api/blender_python_api_current/bpy.types.BlendDataLibraries.html

$\endgroup$
  • 2
    $\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 Jul 26 '15 at 9:55
  • 3
    $\begingroup$ @Boris, There is a link argument, Set it to False to behave like Append $\endgroup$ – ideasman42 Sep 5 '15 at 12:52

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.