6
$\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$
  • 3
    $\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
  • 1
    $\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
6
$\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$
  • 1
    $\begingroup$ Related The suggested change was bpy.context.scene_collection.objects.link() whereas bpy.context.scene.collection.link() may be the go. Not sure if context.collection and context.scene.collection will always be same? $\endgroup$ – batFINGER Nov 1 '18 at 7:00
  • 1
    $\begingroup$ @justi What is dir() inspection? $\endgroup$ – Justin Nov 1 '18 at 14:27
  • 1
    $\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
  • 1
    $\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
  • 1
    $\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.