How to change color of several layers at once

hello everybody,

I’m looking for a plugin that could change the color of several layers at once.

I often import CAD in my models that are usualy mades with a lot of layers. those layers have their own color organization that don’t match mine. It’s because I use “color by layer” and “line color by material” to export colored CAD drawings.
so, I have to change color of the imported layers, and, I have to do it one by one.

do you know any plugin that could help me ?
I didn’t find one looking in commons plugins databases.

thank you by advance.
have a nice day.

Can you give more information about the layer names before and after ?

Example …
All CAD layer names begin with “Window” change to color(34,156,75)
All CAD layer names containing “Roof” change color to (127,127,127)
All CAD layer names ending in “Revision” change color to 100% Red
… etc.

Hi Dan,
I usually turn all the imported layers to a 70% grey. And I use colors for my own layers.
Names are always a mess because a lot of people works on the same CAD and every one have his own name logic.

Can you give me the number for RGB? (0…256)
Full gray is (127,127,127) which is halfway between Black and White (basically 50% each,) along the central axis of the RGB colorspace.

I’m not sure what you mean by 70%. Between the white and the black? (77,77,77)

Well … try this. You can adjust the Gray RGB components to suit.

Example_SetAllLayersGray.rb (878 Bytes)

module Example
  module SetAllLayersGray

    extend self

    @@loaded = false unless defined?(@@loaded)

    def gray_all_layers
      shade = Sketchup::Color.new(77,77,77)
      layers = Sketchup.active_model.layers.to_a
      choice = UI.messagebox('Set "Layer0" black?',MB_YESNO)
      if choice == IDYES
        zero = layers.delete(Sketchup.active_model.layers["Layer0"])
        zero.color= "Black" if !zero.nil?
      end
      layers.each do |layer|
        layer.color= shade
      end
    end

    def gray_all_layers_command
      choice = UI.messagebox('Set all layer colors to gray?',MB_YESNO)
      gray_all_layers() if choice == IDYES
    end

    unless @@loaded
      UI.menu('Plugins').add_item('Set All Layers Gray') {
        gray_all_layers_command()
      }
      @@loaded = true
    end

  end
end
2 Likes

Hi Dan,
I’ve never wrote ruby script before. I’ve no time to do it now. I’ll give it a try later. there are a few basic “protocols” that I need to learn before writing a script.

there is also a layer manager project from D.Bur that is now open source on the extension warehouse. perhaps it’s possible to start from that work and to add this color manager feature to it.

thank you for your help.

I posted an example for you above.

You would if you had learned it earlier! :smiley:
At least, that’s what I always try to say to myself…
For now, I just bookmark Dan’s post for my little collection of usefull little Ruby :gem: snippets
In time I will have time…

1 Like

Based on @DanRathbun code I made a quick and dirty plugin. So it is not bulletproof, and maybe not as nice as should be… but I hope it will work.
(Checked only in SU2019, but should work in earlier version too…)

Set_layers_color.rb (905 Bytes)
Just copy “Set_layers_color.rb” to your plugin folder (usually : c:\Users\your_user_name\AppData\Roaming\SketchUp\SketchUp 2019\SketchUp\Plugins\ ) and restart SU.
You will get a menu:“Set Layers Color”. Click on it!
image
The selected layers will be colored.
You can put anything but “0” beside the Layer name, then they will be colored by r,g,b chosen in the last 3 lines… (r,g and be should be 0…255)

module Example_dezmo
  module SetLayersColor

    extend self

    @@loaded = false unless defined?(@@loaded)

    def color_layers_layers    
### You can copy-paste this part to Ruby console to get same result without install
      layers = Sketchup.active_model.layers.to_a
      layer_names = layers.map{|l| l.name}
      defaults = Array.new(layers.length,"0") + ["77","77","77"]
      input = UI.inputbox(layer_names +["r","g","b"], defaults, "Select Layers and enter color!")
      if input
        shade = Sketchup::Color.new(Integer(input[-3]),Integer(input[-2]),Integer(input[-1]))
        i=0
        layers.each do |layer|
          layer.color= shade if input[i] != "0"
          i+=1
        end
      end
### You can copy-paste this part to Ruby console to get same result without install
    end

    def color_layers_command
      color_layers_layers()
    end

    unless @@loaded
      UI.menu('Plugins').add_item('Set Layers Color') {
        color_layers_command()
      }
      @@loaded = true
    end

  end
end

PS:Unfortunately “UI.inputbox” have it’s limits. So if you have plenty of layers this my not fit to the screen … :cry:

1 Like

Hi Dezmo.

thank you for what you’ve done according to Dan’s code.
you’re plugin works nice.

unfortunately, as you said, the number of layers is limited to the size of the window.

I can erase layers to keep only 30 to work on, and repeat the operation with the next 30 untill I’ts done. then I merge all the layers again in the same file.
It’s a little complicate but it save time compared to change color of layers 1 by 1.

usually, architectural CAD files that I work on have less than 30 layers (around 15/20). huges files of big projects a little messy can exceed 100 layers but not very much. work can be done in 4 times max.

is it the same way to change the line types ?

thank you again.

@botomup You are welcome!

However the line types can be set for several layers at once on the Layers Tray…
setdashes

… I have been modified the example for you. It is even more “quick & dirty” and just tested ones. It is definitely for SU2019 (or later) only ! set_layers_color&linestyle.rb (1.5 KB)

module Example_dezmo
  module SetLayersColorLineStyle

    extend self

    @@loaded = false unless defined?(@@loaded)

    def color_linetype_layers
### You can copy-paste this part to Ruby console to get same result without install
      layers = Sketchup.active_model.layers.to_a
      layer_names = layers.map{|l| l.name}
      line_styles = Sketchup.active_model.line_styles
      line_styles_names = line_styles.map{|l| l.name}
      list = Array.new((3+layers.length),"") + ["no change|"+line_styles_names.join('|')]
      defaults = Array.new(layers.length,"0") + ["77","77","77"] + ["no change"]
      input = UI.inputbox(layer_names +["r","g","b","line style"], defaults, list, "Select Layers and enter color and line style!")
      if input
        shade = Sketchup::Color.new(Integer(input[-4]),Integer(input[-3]),Integer(input[-2]))
        line_style = Sketchup.active_model.line_styles[input[-1]]
      puts input[-1]
        i=0
        layers.each do |layer|
          layer.color= shade if input[i] != "0"
          layer.line_style = line_style if (input[i] != "0" && input[-1] != "no change")
          i+=1
        end
      end
### You can copy-paste this part to Ruby console to get same result without install
    end

    def color_lt_layers_command
      color_linetype_layers()
    end

    unless @@loaded
      UI.menu('Plugins').add_item('Set Layers Color_LineStyle') {
        color_lt_layers_command()
      }
      @@loaded = true
    end

  end
end

1 Like

Nice work! I need to do this for Matt’s section cut technique…set all layers opacity to zero.

@Sonder has a great reason for something like this. Could you add an opacity setting to apply to selected layers?

2 Likes

Basically, you need to toggle between zero opacity and full, all though it might work better with one color (white) for all layers.
Preferably with a menu entry.


@DanRathbun

Nice job Dezmo !

once again you’re right. it’s possible to change several layers line style at once within SketchUp. so, this looks like a dead end.

Unfortunately for some reason the opacity (alpha) of layers can not be set by Ruby.

At least I could not find a good method… neither a reason why this is not working?
@DanRathbun do you have some idea?

For example here the 4th parameter (11) should be the opacity value:

Sketchup.active_model.layers[0].color = [255, 255, 0, 11]

Theoretically this will set the color and the desired opacity of the layer, but somehow this is not applied to the model…
If I’m query it, looks applied

Sketchup.active_model.layers[0].color 
>> Color(255, 255, 0, 11)

But no, it is not “represented” on UI, so not really applied.

I know from API doc:

# You can then assign colors to the material of DrawingElements.
# Note that this creates a new Material object, and the alpha value
# of the color does NOT get applied to the new Material. You must
# manually set the alpha to get transparent materials.


I have been tried to manually set the alpha to get transparency for code above e.g.:

shade.alpha = 11

…but I didn’t really expect any results, since there is no face here… and a layer have NO material …
Unfortunately my knowledge is “stoping” here.
Anybody else?

1 Like

There are issues logged with regard to layers, materials and opacity …

which led to the following API issue being logged

which led to the following API issue being logged

1 Like

I tried fiddling with your demo of demolayers, with no luck.

It does say ‘manually’ in the API for changing .alpha

I wonder if you can duplicate an existing Layer through Ruby.
Then, one would only have to create one (and set the opacity)

Collect all existing layers (x)
Duplicate the ‘Matt Method Layer’ x times
Putt all stuff in them

Set renderoptions (ColorByLayer, NoProfiles, Noedges)

Correct. SketchUp does not yet support layer transparency set via the API.

ALL layers ? zero would mean 100% transparent which is similar to Xray mode with background, foreground, section fill and backcolor all the same. Having Color By Layer on or off makes no difference.

But you cannot have sectioncuts with witeframe
https://mastersketchup.com/author/ljmvd/