When you create a collection manually, you can drag it around, but I'm not sure what the Python command for this is. I tried:
import bpy new_collection = bpy.data.collections.new("MyNewCollection") bpy.context.scene.collection.children.link(new_collection) bpy.context.view_layer.active_layer_collection = bpy.context.view_layer.layer_collection.children[-1] The following script will create a new collection below the last one, but I want it to appear at the top. I want MyNewCollection.005 to be above MyNewCollection.
The only clue I see is bpy.ops.outliner.collection_drop() in the Info Panel. The only workaround is to either manually drag it above the existing one or unlink all collections, then link the new one first and link the others back again as similarly employed/suggested in this thread and this thread.
import bpy new_collection = bpy.data.collections.new("MyNewCollection") # Unlink all collections from the scene (except the new one) scene_collections = bpy.context.scene.collection.children[:] for collection in scene_collections: bpy.context.scene.collection.children.unlink(collection) # Link the new collection first bpy.context.scene.collection.children.link(new_collection) # Link the rest of the collections back for collection in scene_collections: bpy.context.scene.collection.children.link(collection) It's weird. Is that the only right way to do it?
