Assign Objects to Individual Layers

Hi,

Does anyone know of a way either existing in Rhino 5 or with script to automatically assign all objects in a scene to a new layer of its own?

I ask because if you import a 3dm into Keyshot (not using the plugin within Rhino), the objects are either grouped by material as assigned in Rhino or by layer. This is less than ideal if you plan to animate and want to move objects, because as far as I can tell they cannot be ungrouped.

So a simple solution would be to assign each object to its own layer. It’s a bit brute force but it would solve the problem.

Hi Mathew,

This script will distribute all select-able objects (not hidden or locked) to individual layers with a random color and sequential name.
ObjectsToLayers.rvb (1 KB)

EDIT: I created an updated script as per request in another topic:

To run it you can drag-drop if over rhino.
Be aware that with many objects it can be time consuming; make sure to keep a good backup copy of your file.

Let me know what you think.

-Willem

Perfect! Exactly what I wanted, thanks very much Willem!

1 Like

Any way the layers can be named based on the object? Im importing a bunch of STLs into a file and need them to be randomly colored and all on their own self titled layer.

The original script you posted is fantastic, but for me its only halfway there.

Hi Ted,

Have a look at this version.
It will try of the (objects) for a layer have a name, and if so give the layer that name.
If there already ois a layer with that name; it is numbered.

ObjectsGroupsToNamedLayers.rvb (2.8 KB)

HTH
-Willem

1 Like

Hi Willem,

Sorry for bringing up an old tread. The script you wrote is almost what I really need, but instead of assigning the individual objects into layers, is it possible to assign layers based on the groups?

It doesn’t sound very hard, but my scripting is terrible. I don’t even now where to start! Your help would be greatly appreciated. Thanks!

-Eric

Hi Oscire,

My time is limited at the moment, so I cannot be of help.
Does the script not do what you need: grouped objects to layers?
Can you elaborate on what the exact function needs to be, it might be someone else can be of help.

-Willem

Hi Willem,

I am replying to this thread which is from 2014, but I would greatly appreciate if this “ObjectsGroupsToNamedLayer.rvb” could be made available again. This is a fantastic script idea which would be a huge help for my current project. Would you consider making this available again for use with Rhino 7? I am a long time user, but just joined this forum.

-Jon

Hi Jon - it looks like the script is available from

-Pascal

Hi Pascal,

The script “ObjectGroupsToLayer” is available and works great, but does not label the layer with the Rhino Object Name, instead OBJ_01, OBJ_02, etc. The script that appears to be unavailable is “ObjectGroupsToNamedLayer.rvb” will create layers and will name them exactly as the Rhino object name. That is if I’m understanding the description correctly.

-Jon

Ah, yes, ok- I can probably make something that works if Willem is busy, which I think he very often is…
@JD12 - is the idea

  • find all groups
  • make a layer for each group’s name
  • put the objects from each group onto the corresponding layer of their top-level group (if in more than one)
    ?

-Pascal

Wow, that would be great! Much appreciated.

Actually what I was hoping for was a script that allow me to take a bunch of uniquely named objects and then create and place them on their own layer. The layers would be named exactly the same as the object. Does that make sense?

-Jon

Hi Jon - see if this is it -

ObjectsToLayerByName.py (385 Bytes)

If the layer exists, then the object is put there, if not the layer is created - this could lead to multiple objects per layer as it stands now…
Also, this acts on selectable objects - not hidden or locked - it can do those too, or on a user selection - it’s all easy enough to change.

To use the Python script use RunPythonScript, or a macro:

_-RunPythonScript "Full path to py file inside double-quotes"

-Pascal

2 Likes

Works perfectly! Tested it on a few different files. Can’t thank you enough Pascal!

All the Best,

-Jon

1 Like

Hi Pascal, is there a python script that names all objects on a layer the name of object which has a name on that layer. Being more clear, only one object on the layer has a name and the script gives the same name to the other objects on the layer.
It would be the best if this acts only on selectable objects - not hidden or locked

Jacek

Hi Jacek - that is certainly do-able - do you want to be able to select one or morte layers, or just have it do all layers and not ask questions?

@Jacek2 - see if this does it:

import Rhino
import scriptcontext as sc
import rhinoscriptsyntax as rs

"""
 names all objects on a layer
 the name of object which has a name on that layer. Being more clear,
 only one object on the layer has a name and the script gives 
 the same name to the other objects on the layer.
It would be the best if this acts only on selectable objects - not hidden or locked

"""
def test():
    
    l_names = rs.GetLayers()
    if not l_names: return
    
    for name in l_names:
        ids = rs.ObjectsByLayer(name)
        if len(ids) == 0: continue

        for id in ids:
            if rs.ObjectName(id) is not None:
                for item in ids:
                    if rs.IsObjectSelectable(item):
                       rs.ObjectName(item, rs.ObjectName(id))
                break

test()

-Pascal

Thanks for such a quick response. It will be best if the script will work on selected layers. Most often I select between 5 and 10 layers.

Jacek

Hi Pascal, I would like to ask if you will be able to help me with this script?

Hi there,
this all is very interesting here and works nicely.
I m currently trying to find a way to do something similar and was wondering if there is any way to kind of
assign a batch of objects to single layers but preserve an order.
I.e. I have a pile of 100 objects of different dimensions and want each of those to be assigned to a single layer in an order of top object (in z direction) goes into layer 01 and the lowest object of the z direction goes into layer 100.
Would such thing be possible somehow ?
Thanks in advance for any tips
assigning obejcts to layers test.3dm (3.8 MB)

This can be done with a script, here is a kind of a sketch as to how this might be done…

import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino

rev_sort=False
prefix="Object_"

def ObjZ(obj,rev_sort=False):
    bb=rs.BoundingBox(obj)
    #rev_sort==True: BB top Z /  rev_sort=False: BB bottom Z
    if rev_sort: return bb[4].Z
    else: return bb[0].Z

def ObjectsToLayersByZ():
    msg="Select objects to sort and layer by Z"
    obj_ids=rs.GetObjects(msg,4+8+16+32+4096,preselect=True)
    if not obj_ids: return
    rs.EnableRedraw(False)
    obj_ids.sort(key=ObjZ)
    if rev_sort: obj_ids.reverse()
    for i,obj_id in enumerate(obj_ids):
        layer_name="{}{}".format(prefix,i)
        if not rs.IsLayer(layer_name): rs.AddLayer(layer_name)
        rs.ObjectLayer(obj_id,layer_name)
ObjectsToLayersByZ()

The above sorts bottom to top - the lowest object is on level (layer) 0 - if you change the line
rev_sort=False to rev_sort=True then the highest object will be on level (layer) 0.

Note this is really just a sketch to test, all of this can be modified including the layer name prefix etc.

ObjectsToLayersByZ.py (736 Bytes)

1 Like