As part of an existing add-on, I'm trying to create an object and add it to the scene using Python. I followed a Blender Cookbook tutorial found here: Three ways to create objects. I use the following code to do so:

emptyMesh = bpy.data.meshes.new('emptyMesh')
theObj = bpy.data.objects.new("object_name", emptyMesh)

But I get the following error when I try to link theObj to the scene objects:

AttributeError: 'bpy_prop_collection' object has no attribute 'link'

This works just fine in Blender 2.76-2.79, but doesn't work in the 2.80 test build I downloaded. Was there a change to the Python API? Or is this maybe a bug in the test build?

  • 4
    $\begingroup$ I'm voting to close this question as off-topic because it is about an incomplete development version $\endgroup$ – Duarte Farrajota Ramos Nov 28 '17 at 23:04
  • 2
    $\begingroup$ @Amir I really don't think it's necessary to change "Python in Blender" to "Blender's Python API". Either it's a general Python question in which case it's off-topic or it will be using Blender's API. $\endgroup$ – Ray Mairlot Oct 31 '18 at 15:06

In 2.8 Blender API, this is still not documented yet. After dir() inspection I've found a temporary solution, where you can replace the scene with the collection, as below:

| improve this answer | |
  • 2
    $\begingroup$ @justi What is dir() inspection? $\endgroup$ – Justin Nov 1 '18 at 14:27
  • 2
    $\begingroup$ @batFINGER yes, you are right - the suggested change is bpy.context.scene_collection.objects.link() but it doesn't work now. Why? Because it causes the AttributeError: 'Context' object has no attribute 'scene_collection' $\endgroup$ – justi Nov 5 '18 at 0:52
  • 2
    $\begingroup$ @Justin I did the dir() command on the bpy.context object: dir(bpy.context) and got attributes $\endgroup$ – justi Nov 5 '18 at 1:01
  • 2
    $\begingroup$ Yes I realize that, hence the suggestion of context.scene.collection and the query as to whether context.scene.collection and context.collection will necessarily be the same. $\endgroup$ – batFINGER Nov 5 '18 at 1:39

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.