Sort Layers by Name

Hi all,

I am thinking if it is possible to sort layers’ sequence by layers’ name?
I mean, to actually moving layers up or down base on their names, not simply print out the sorted layer names.
I have a look at the ‘Layer Module’ but having found anything similar…

Appreciate for any suggestions,

Jack

I would also like to have the ability to sort the layer dialog through scripting (Rhinoscript). At present, after running a script which adds layers, the layer dialog header needs to be clicked twice to sort the layers alphabetically. An old request but worth a try… it used to work in V4.

1 Like

I don’t see a clean way to do this but I suppose you could add the layers to a temporary layer in the right order and add then them back:

import rhinoscriptsyntax as rs
from scriptcontext import doc
from System import Guid

sorted_layer_names = sorted([layer.FullPath for layer in doc.Layers 
  if layer.ParentLayerId == Guid.Empty], 
  key=lambda s: s.lower(), reverse=True)

tmp_layer_name = rs.AddLayer(Guid.NewGuid().ToString())
tmp = doc.Layers[doc.Layers.FindByFullPath(tmp_layer_name, False)]

for fp in sorted_layer_names:
    l = doc.Layers[doc.Layers.FindByFullPath(fp, False)]
    l.ParentLayerId = tmp.Id
    l.CommitChanges()
    l.ParentLayerId = Guid.Empty
    l.CommitChanges()

doc.Layers.Delete(tmp.LayerIndex, True)

This sample only considers the root layers so you’d have to walk the tree if you want to sort sub layers as well.

2 Likes

Hi Alain,

That is awesome!!
Thanks a lot~~ :smile: