17

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)
bpy.context.scene.objects.link(theObj)

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?

David
  • 49,291
  • 38
  • 159
  • 317
Justin
  • 1,972
  • 4
  • 24
  • 43
  • 4
    I'm voting to close this question as off-topic because it is about an incomplete development version – Duarte Farrajota Ramos Nov 28 '17 at 23:04
  • 2
    @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. – Ray Mairlot Oct 31 '18 at 15:06

1 Answers1

17

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:

bpy.context.collection.objects.link(theObj)
justi
  • 286
  • 3
  • 6
  • 2
    @justi What is dir() inspection? – Justin Nov 01 '18 at 14:27
  • 2
    @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' – justi Nov 05 '18 at 00:52
  • 2
    @Justin I did the dir() command on the bpy.context object: dir(bpy.context) and got attributes – justi Nov 05 '18 at 01:01
  • 2
    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. – batFINGER Nov 05 '18 at 01:39