0
$\begingroup$

I'd like to Append an object using a python script, and do it in Blender2.8. I have found some code snippets to do this on 2.7x but having trouble on 2.8.

Below is what I tried. I get this error on the last line: AttributeError: 'bpy_prop_collection' object has no attribute 'link'

Thanks for any suggestions or code snippet. I am beginner level on Python, and intermediate level on Blender.

Matt


# current scene
scn = bpy.context.scene

# path to the blend
filepath = "//2019_template_01.blend"

# name of object(s) to append or link
obj_name = "Chassis"

# append, set to true to keep the link to the original file
link = False

# link all objects starting with 'Chassis'
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)
$\endgroup$
1
$\begingroup$

had the same problem! Here is the solution that worked for me:

change the last code snippet to following:

#link object to current scene
for obj in data_to.objects:

   if obj is not None:
      bpy.context.collection.objects.link(obj)

you can also add collections and their content from other blendfiles with data_to.collections:

for col in data_to.collections:
    bpy.context.scene.collection.children.link(col)
$\endgroup$

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.