Shortcut to set Display Color to "By Layer"

Hi, a quick question: Often when I import CAD Files one of the first things I have to do is set Display Color, Linetype, Print Color and Print width to “By Layer” since that is how I organize my files. It takes quite a few clicks to do it and if a lot of geometry is selected the dropdowns are a bit laggy. Is there a shortcut to set all the parameters to “By Layer” ? Can these values be accessed through a command?

Thanks!

(this is what I try to solve, before and after)

image

image

This python script should do it, maybe:

import rhinoscriptsyntax as rs

all_objs=rs.AllObjects()
if all_objs:
    rs.EnableRedraw(False)
    for obj in all_objs:
        rs.ObjectColorSource(obj,0)
        rs.ObjectLinetypeSource(obj,0)
        rs.ObjectPrintColorSource(obj,0)
        rs.ObjectPrintWidthSource(obj,0)

Beware, this works on all objects in the file, even if they are hidden/locked or on off/locked layers…
SetAllObjAttribsByLayer.py (287 Bytes)

This one might be a bit faster to run if you have lots of objects:
SetAllObjAttribsByLayerRC.py (634 Bytes)

This is great thank you!

So if I want to only apply it to my selection I should be able to do that with rs.GetObjects I imagine, no?
Your first script is very clear to me but could you explain why the second one might be faster? What does rs.coercerhinoobject do?

Yes, exactly.

Well, each object in a Rhino document is stored in a table called the object table, it is attributed a unique object ID (GUID) so it can be found. Rhinoscriptsyntax methods mostly work on object ID’s, but behind the scenes they call RhinoCommon methods that need to get the “Rhino object” associated with the object id by looking it up in the object table.

This is what rs.coercerhinoobject() does, it is just a utility function to try and get the Rhino object from the ID by looking it up in the object table. Rhino objects have attributes, among them layer, display color, print width etc. These attributes can be changed as needed.

So, why would the second script be faster? The first script calls up 4 rhinoscriptsyntax methods for each object, each method will first use coercerhinoobject() behind the scenes to find the Rhino object based on its ID, modify its attributes and then write the changes back to the document. That means 4 times a lookup and 4 times a write back for each object.

The second script only looks up each object once, does all the attribute changes in RhinoCommon and then writes back the four attribute changes at once. So, it uses only 25% of the time spent by the first script looking up and writing back.

Each lookup and write operation takes a small amount of time. For a couple of objects you will not see the difference, for a couple of thousand you probably will.

1 Like

Makes sense… thanks for explaining!

Have a nice day

This macro should do what you want:

-properties pause Object Color Layer Linetype “By Layer” PrintColor Layer PrintLinewidth ByLayer enter enter

2 Likes

Nice, I didn’t know that the properties could be accessed this way through a command. That’s really helpful, thank you