Sort and arrange order of layers programmatically: request

A RhinoScript method for alphabetically sorting the layer list would be really helpful, the equivalent of pressing the name header in the layer panel.

This used to work in V4 by calling “Rhino.LayerNames True”, but has never worked in V5, hopefully it can be added to V6.

The inconvenience currently, is when a script adds layers to an alphabetically sorted layer list, the name header needs to be pressed twice after running the script before the list is correct.

But… Rhino.LayerNames(True) sorts the resulting list. You would want the layer panel UI (usually on the right side of Rhino) sorted?

I get this:

That does not do anything other than changing the view order, right?

Yes, it’s the view order in the ui that I was hoping to change programmatically, (RhinoScript)

The Rhino SDK does not provide a function that allows a developer to control the sorted order of layers.

If we were to provide an SDK function, is there any reason no to only sort by Layer name, ascending and descending?

I should also add that this wish is not applicable to this thread, which is trying to determine what RhinoScript methods, that are not implemented in rhinoscriptsyntax, need to be added.

– Dale

Hi @BrianM,

There is a way to make it work currently (sort Layers in Layer Panel) via RhinoScript. It uses a parenting trick described in this thread: Specifying location of layer in layer dialog using Rhinoscript

Obviously it would be more elegant to have a dedicated method to do this in RS, but maybe this would help you for now - see attached (True or False in the main Sub call will sort ascending or descending).

SortLayers.rvb (1.5 KB)


Request noted here: RH-41449. Also, thread was split.

Hi @Jarek ,

Thanks a lot for the sort script and the explanation comments helped me understand how you got it working.

Hi Dale,

Initially, the request was to mimic the sort action created by pressing the layer list header.

Having used the workaround script provided by @Jarek , which sorts the parent layers and single layers (no children), I can see a use for this method, which keeps the children layers unsorted, as well as a method to sort the complete list.

A third method to sort the children layers of a (current?) parent layer would be handy too.

Hi @BrianM,

Not sorting the children was not intentional - a bug that I fixed, so now the script should sort all layers as the Layers panel behavior: SortLayers.rvb (1.6 KB)

Sorting within selected layer and its children could be most likely accomplished by modifying that script. See if you can figure that out or would need help with that - I can take a stab at it, too.



1 Like

Hi Jarek

Ah … nice trick ! :slight_smile:
Very useful !

Thank you for sharing !

Thanks Jarek, the updated script works well. I attempted to mod your script to sort Children layers of a parent layer which is current, without success so far,

I’ve replaced getting the layer list (Rhino.LayerNames) with the following and tried a few different things but have hit a brick wall, any tips would be appreciated.

'get current layer and check fo children layers
arr_SubLayers = Rhino.LayerChildren(str_CurrentParent)

If IsNull(arr_SubLayers)Then
Rhino.Print “Layer has no children”
Exit Sub
arr_SubLayers = Rhino.SortStrings(arr_SubLayers, True) 'ascending
End If

Hi Brian,

I reworked the script to act on current layer only (and its all children or immediate children - use the proper argument).
Seems to be working fine except on files with tons of layers may take a couple seconds to refresh the panel…
It could probably be made faster by creating 2 temporary parent folders and juggling between them to sort, but ran out of time for now to implement this way. Hope this one helps for now.

SortLayersInCurrent.rvb (1.7 KB)


This script works well Jarek, the refresh time isn’t a problem. Thank you for the effort you’ve put into this, you have provided a variety of ways to keep the layer list tidy and I hope you get some use out of the methods too. :smile:

Glad to hear Brian; and yes - the main thing I got out of it is the ability to deselect all layers from script that I discovered by accident while writing these… It is not possible in Rhino 5 (added to V6 WIP as new method) but now with the un-parenting trcik can be done as well.

This request was added to YouTrack - sorry for not mentioning this earlier.

– Dale