The sorting function used by collection inputs
Hello,
I am just trying to script something where I need the objects in a collection in exactly the order that the "Collection Info" geometry node processes them when indexing objects for making instances. There doesn't seem to be an easy way to get the same order in Python.
While they are displayed sorted in the UI, they are not sorted by default in the data, and a naive implementation (e.g. just all lowercase) easily yields a different order.
I've looked around in the code and found this:
- source\blender\nodes\geometry\nodes\ node_geo_collection_info.cc -> there's a call to BLI_strcasecmp_natural
- source\blender\blenlib\intern\string.c has the definition for BLI_strcasecmp_natural
It would be great if there was a simple Python binding for exactly this comparator so I could use it for sorting. :)
2 Comments
I've ported the C++ version of the thing to Python. It might still have little mistakes, but works for me so far.
I don't know how to add code in here (the editor is too minimal for that), so please excuse this strange-looking paste where I couldn't think of another way to easily maintain the indentation :)
(code omitted because RCS is buggy)
You can test it out in a fresh Text Editor area in Blender. Go to Window -> Toggle System Console first. HTH!
Oh, I had also checked that this is indeed the sorting function that applies, again. It's in source\blender\nodes\geometry\nodes\ node_geo_collection_info.cc , there's a reference to BLI_strcasecmp_natural.
Because the original used char* pointers a lot and relied on them being 0-terminated, I've just added a \0 at the ends of the strings to compare.
....... Okay, this website just says comment not allowed. Very good programming. Let's try this instead: pastebin.com/FXAHMNVi
Agreed, Collection order can be deceiving and the collection order in the outliner uses some natural sorting which isn 100% the same as alphabetically sorting. Posted same problem to BSE blender.stackexchange.com/questions/279880/sort-objects-in-order-as-they-appear-in-the-outliner