3
$\begingroup$

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.

collections list

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?

$\endgroup$
3
  • 1
    $\begingroup$ Interesting. I don't think the inner workings of the outliner are available to the python API unfortunately. I would think this solution while dumb is the simplest. It will make heavy scenes choke though so I would maybe recommend replicate the hierarchy in a custom UI List if you really want to have your own outliner. Maybe you can do something with operators but you'd have to capture mouse position and calculate a bunch of hard position differentials and I don't think I have ever seen it done anywhere. Cheers $\endgroup$ Commented Jan 27 at 20:03
  • $\begingroup$ similar link 1 link 2 variations of the same. $\endgroup$ Commented Jan 28 at 3:57
  • $\begingroup$ Thanks guys, for your input. I also appreciate the links and for confirming that this is indeed the only workaround involving unlinking and linking, as all posts suggest essentially the same workaround. It's unfortunate that there aren't any more efficient solutions. $\endgroup$ Commented Jan 28 at 4:49

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.