Renaming objects by layer

I need a script to rename objects by the layer that they are in.
Can anybody help?
Thanks !

Hi,

You can try this :

import rhinoscriptsyntax as rs
objList = rs.GetObjects("Select objects to name")
for obj in objList:
    layer = rs.ObjectLayer(obj)
    layerName = rs.LayerName(layer)
    rs.ObjectName(obj, layerName)

Cool. Thanks Baptiste.

If you don’t need the entire layer name but only the sublayer name :

import rhinoscriptsyntax as rs
objList = rs.GetObjects("Select objects to name")
for obj in objList:
    layer = rs.ObjectLayer(obj)
    layerName = rs.LayerName(layer)
    subLayerNameList = layerName.split("::")
    newName = subLayerNameList[-1]
    rs.ObjectName(obj, newName)

or better :

import rhinoscriptsyntax as rs
objList = rs.GetObjects("Select objects to name")
for obj in objList:
    layer = rs.ObjectLayer(obj)
    layerName = rs.LayerName(layer, False)
    rs.ObjectName(obj, layerName)
1 Like

Perfect. I was wondering about that. I might need both depending on the project. Thanks again.

Come to think about it. Can I ask you to add it as an option, for example, when I run it it could ask “Sublayers only Y/N” …
and have both options in one script ?

I wrote this, it works but it remains a bit buggy.
Maybe someone more comfortable with the scriptcontext syntax could have a look and make it better…

import rhinoscriptsyntax as rs
import Rhino
import scriptcontext
def nameObjectsFromLayerName():
    go = Rhino.Input.Custom.GetObject()
    go.SetCommandPrompt("Select objects to name")
    boolOption = Rhino.Input.Custom.OptionToggle(True, "Off", "On")
    opList = go.AddOptionToggle("fullPath", boolOption)
    while True:
        getResult = go.GetMultiple(1,0)
        if getResult == Rhino.Input.GetResult.Cancel:
            return Rhino.Commands.Result.Cancel
        elif getResult == Rhino.Input.GetResult.Option:
            go.EnablePreSelect(False, True)
            print "fullPath =", boolOption.CurrentValue
        elif getResult == Rhino.Input.GetResult.Object:
            ids = [go.Object(i).ObjectId for i in range(go.ObjectCount)]        
            break 
    fullPath = boolOption.CurrentValue
    for obj in go.Objects():
        layer = rs.ObjectLayer(obj)
        layerName = rs.LayerName(layer, fullPath)
        rs.ObjectName(obj, layerName)
nameObjectsFromLayerName()

it works but it remains a bit buggy.

Baptiste, I tested it in several projects and it seems to work fine. Where did you find a buggy behavior ? I didn’t see any problem.

Hi,

Actually, it’s mostly a matter of coding. I’m not sure I went right to the point and with the proper method.

From the user side, you will discover that if objects are preselected, then you cannot change the option and whenever you toggle the option, you have to make your selection again even if you already did before. Not very convenient.

If it suits you, it’s alright. I will have a look on it to find a better solution.
I was just hoping somebody would give a hand on this.

I see. Don’t worry too much. It works for now.
Thanks