If I print this:

print( bpy.data.collections['My Collection'].objects[:] )

... I get a list of the objects in the collection, but it's not an alphabetical list like the Outliner. It looks like the objects are ordered by the moment in which they were added to the collection.

  • What is the best way of reordering the data in the collection alphabetically so that is is identical to what is displayed in the Outliner?

  • Also, how can I do this while keeping the same Collection name?



I'm working on an addon that imports objects from a Kitbash Library in a different Blend file with collections for different types of objects. There's a collection for chains, another for cables, etc.

I have a modal operator that lets you scroll through the objects in the different categories and display one object at a time. Basically, I import the entire collection but I don't link it to the scene, I only link one object of index[i] which is controlled by the mouse scroll.

I know that mouse scrolling might not be suited for a kitbash library with tons of elements but I want to keep this feature for quick asses to user favorites at least and implement a visual library UI/panel similar to what is used in Decal Machine later.

For now, I want people to be able to customize the Kitbash Blend file directly, reorder the objects by renaming them, put in their own objects, etc. The problem is that if they add a new object and rename it so that it is first in a collection, for example, it won't actually be first when you run the scrolling operator, it will still be last because it was created and added last.

Basically, re-ordering elements in the outliner to their personal preference will do nothing when I access the collection with:

bpy.data.collections['Cable Kitbash'].objects[mouse_scroll_index]

I figured the best solution was to make sure the collection data is ordered alphabetically at some point in time, maybe during startup somehow or when the modal operator is first ran.

If anyone has a better idea I wouldn't mind hearing it.

  • 4
    $\begingroup$ sorted(bpy.data.collections["My Collection"].objects, key = lambda obj: obj.name)? Why you need to reorder it? $\endgroup$
    – HikariTW
    May 18, 2020 at 7:03
  • $\begingroup$ I added a wall of context. I didn't do it before because it is a lot of text and I figured people would be dissuaded from reading and helping me with my question. $\endgroup$ May 18, 2020 at 15:48
  • $\begingroup$ Oh and thanks a lot. If you don't mind posting your comment as an answer I would be happy to up-vote it. $\endgroup$ May 18, 2020 at 18:09
  • 4
    $\begingroup$ @HikariTW obj.name.lower() Related re sorting collections blender.stackexchange.com/questions/157562/… $\endgroup$
    – batFINGER
    May 18, 2020 at 19:24
  • 1
    $\begingroup$ Thanks for all the info guys, it was really helpful. $\endgroup$ May 19, 2020 at 17:45


You must log in to answer this question.

Browse other questions tagged .