1
$\begingroup$

Im trying to make a script to add a set of collections to the outliner as I often setup the same or very similar hierarchy for each my projects. In Blender 2.8 if you right click in the outliner and create a new collection the code is

bpy.ops.outliner.collection_new(nested=False)

If you try using that in a script you will get an error on the consol

RuntimeError: Operator bpy.ops.outliner.collection_new.poll() failed, context is incorrect

I have gone back and fourth through the inbuilt API reference to find an alternative but haven't came up with anything that works properly. I expect there must be some fundamental gap in my knowledge about how context works and how to pass correct context.

One thing which seems to sort of work is

bpy.ops.collection.create(name="Test123")

The problem with that is that the collection is not part of anything, it ends up as orphan data in the blend file and I don't know how to arrange it into the view layer. Once again, I expect there are some simple concepts of implementation I don't know, preventing this from working as expected.

$\endgroup$
4
$\begingroup$

This seems to work (not verified against any official documentation):

myCol = bpy.data.collections.new("Test123")
bpy.context.scene.collection.children.link(myCol)
$\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.