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