17
$\begingroup$

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?

$\endgroup$
2
  • 4
    $\begingroup$ I'm voting to close this question as off-topic because it is about an incomplete development version $\endgroup$ Commented Nov 28, 2017 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$ Commented Oct 31, 2018 at 15:06

1 Answer 1

18
$\begingroup$

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)
$\endgroup$
4
  • 2
    $\begingroup$ @justi What is dir() inspection? $\endgroup$
    – Justin
    Commented Nov 1, 2018 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
    Commented Nov 5, 2018 at 0:52
  • 2
    $\begingroup$ @Justin I did the dir() command on the bpy.context object: dir(bpy.context) and got attributes $\endgroup$
    – justi
    Commented Nov 5, 2018 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
    Commented Nov 5, 2018 at 1:39

You must log in to answer this question.

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