Script to give a bunch of layers random color

Hello!

Is there a way to create a script that, once i’ve opened a file with a bunch of layers, give them a randomized color?

I’ve tried to read some documentation but my skills in programming are equal to zero, so…

Really thank you =)

Hi Info -

You can find such a script in this post:

-wim

Every couple of years, when I change jobs, re-setup Rhino and want to open a SolidWorks assembly, I look for a way to randomize layer colors.
I usually look @pascal 's scripts page, or on @Helvetosaur 's page and then I search here for “random” “layer” and “color”. But I can’t find a good one anymore, the most promising is here.
But this script " RandomColorSelLayers.py" does not work correctly in the current WIP. I have to run it a few times to get the dialogue showing and then the layer colors are not refreshed and the viewports are frozen.
Can someone can have a look at the script?

Hi Marc, I’ll have a look - in the meantime, in the WIP:
image

The script will work if these variables match:

layers=rs.GetLayers("Select layers to randomize color") #<<<<<<<<<Here
if not layers: return
rs.EnableRedraw(False)
for layer in layers:
    r=random.randint(0,255)
    g=random.randint(0,255)
    b=random.randint(0,255)
    rs.LayerColor(layer,(r,g,b))
sc.sticky["RCSL_Choice"] = layers #<<<<<<<<<<<<<<<<<Here 

The original seems to have the last line referring to a non existent variable called “sel_choice” rather than “layers”.

-Pascal

Weird, my copy here looks like this:

"""Applies a random RGB color to selected layers.
Script by Mitch Heynick, 01.07.15
Revised 20.10.20 - fixed small bug"""
import rhinoscriptsyntax as rs
import scriptcontext as sc
import random

def RandomColorSelLayers():

    layers=rs.GetLayers("Select layers to randomize color")
    if not layers: return
    rs.EnableRedraw(False)
    for layer in layers:
        r=random.randint(0,255)
        g=random.randint(0,255)
        b=random.randint(0,255)
        rs.LayerColor(layer,(r,g,b))
RandomColorSelLayers()

Don’t recall having added a sticky component to that one… There was a bug fixed a few years ago, but I don’t know if it was that.

Thanks Pascal!
I noticed the new feature in the layer panel, it will be handy.
But an option to randomize layer colors in the layer’s panel Tools menu is really missing.
When we import an assembly the layers are always all white or black and sometimes there’s hundred of them.

I don’t see the difference with or without the line
sc.sticky["RCSL_Choice"] = layers

1 Like

Hi Mitch,
If I remove the line as your copy, it works but the layer colors don’t get updated in the layer panel unless I click on them.

Hi Marc,

Just tested seems to be working here in V7 and the WIP via EditPythonScript or a toolbar button (only tested the button in V7, probably not making new toolbar buttons in V8 anytime soon…)

However when run via ScriptEditor, yes, I see it fails to update the layer colors without a redraw. IMO this is a problem with ScriptEditor, not the script. You can workaround the problem by ading the following line at the end of the script:

rs.Redraw()

The colors then update immediately. But IMO this shouldn’t be necessary.
@eirannejad ?

1 Like