How to move objects to layer by name RhinoCommon

Two questions here:

  1. How to move objects with a specific name to a specific layer.
  2. How to move selected objects to a layer with specific name (not index)

preferably in python

Thanks in advance.

Since you did not specify, I assume it’s rhino common

given you have a list of rhino objects and the index of the target layer

for rhObj in rhinoObjects:
    rhObj.Attributes.LayerIndex = layerIndex

to find the rhino objects

yourList = []

for obj in doc.Objects:
   if obj.Attributes.Name == "name":

For each rhino object you want to move to a specific layer:

rhinoObject.Attributes.LayerIndex = doc.Layers.FindName("layername").Index

this should do the trick.

1 Like

Hi @ivelin.peychev, i would suggest you first try simple scripts using RhinoScriptSyntax and create re-usable functions, then if you want to learn about the RhinoCommon code behind, open the lib file.

import rhinoscriptsyntax as rs

def MoveNamedObjectsToLayer(obj_name=None, layer_name=None):
    if not obj_name or not layer_name: return
    # get objects with obj_name
    obj_ids = rs.ObjectsByName(obj_name)
    if not obj_ids: return
     # find first layer which has a matching layer name
    for layer_id in rs.LayerIds():
        # strip the layer name from the layer path
        this_name = rs.LayerName(layer_id, fullpath=False)
        # if the layer name equals 'layer_name', change objs layer
        if this_name.strip() == layer_name.strip():
            rs.ObjectLayer(obj_ids, layer_id)
MoveNamedObjectsToLayer(obj_name="Ivelin", layer_name="Ivelin")


Hi Clement, tried that. You can’t do everything with RhinoScriptSyntax and some methods are confusing or simply not enough. If you want to do something more complex you can’t avoid using RhinoCommon and mixing GUID with Objects adds too much to my general confusion. :stuck_out_tongue_winking_eye: .
This is why I want to try doing all (or as much as it is meaningful) with RhinoCommon. I still select the objects by RhinoScriptSyntax and use ScriptContext (I don’t think I can avoid that). This was suggested to me a while ago by @AndersDeleuran. And I now see it really makes sense to just use RhinoCommon.

As for the particular task at hand. I thought you can get the name of the layer directly but apparently it is stored as index and you get the name by the index.

If I may add another question to this thread. How can I select the objects by only piece of the name string? Say move all objects that start with “MY_”.

Searching for Objects by name support wildcards. For example to get all objects which have a name starting with “MY_” you would do this:

rs.ObjectsByName("MY_*", select=True)

btw. if you want to get the RhinoCommon way of doing this, just lockup the method in the lib folder.


1 Like

I have a problem with this selecting even objects in hidden layers that were not selected. That might as well be because I’ve not added the boolean.

Hi @ivelin.peychev, the method does return object ids of objects found with the name, regardless of the object beeing selectable or not. In general, objects which are locked, hidden or objects on locked or hidden layers cannot be selected.


1 Like