Color values to skm code

@DanRathbun,

if creating 4000, would it be better to have Sketchup::Color.new() in the loop that has GC…

i.e.

  # c = Sketchup::Color.new(h['R'].to_i, h['G'].to_i, h['B'].to_i)
  c = [h['R'].to_i, h['G'].to_i, h['B'].to_i]

  # later...
  # material.color = colour
  material.color = Sketchup::Color.new(colour.entries)

@john_drivenupthewall - I’m not sure if it matters but this time it’s “only” 1900 colors.

oh! i also forgot to mention that the 1900 colors is broken up by color family in 9 separate csv files totaling 1900 colors. none of the csvs are over 400 colors.

With regard to …

n = h['Name'].rstrip.split(' ').map {|word| word.capitalize}.join(' ')

… I came across this example in the ol’ Pick Axe book …

text.gsub(/\b\w/) { $&.upcase }

I just used it yesterday and wrapped it a method named titlecase(text).


Whether 4000 or 1900, I’d not actually create any more than one internal material and color.
Add it, name it, set it’s color, save as it to disk …
Rename it, set the material’s color’s RGB components to new values, save as …
repeat …
No GC needed.

Would there be an issue with using one material (that I didn’t see ?)

good point, I normally use a single material and change it’s properties…

this was quick…

john

@john_drivenupthewall @DanRathbun - I tried the code in Ruby console and it returned the following error:

Error: #<NoMethodError: undefined method `rstrip’ for nil:NilClass>

:18:in `block in csv_colours' C:/Program Files/SketchUp/SketchUp 2020/Tools/RubyStdLib/csv.rb:1764:in `each' C:/Program Files/SketchUp/SketchUp 2020/Tools/RubyStdLib/csv.rb:1142:in `block in foreach' C:/Program Files/SketchUp/SketchUp 2020/Tools/RubyStdLib/csv.rb:1289:in `open' C:/Program Files/SketchUp/SketchUp 2020/Tools/RubyStdLib/csv.rb:1141:in `foreach' :16:in `each' :16:in `map' :16:in `csv_colours' :45:in `'

Thoughts?

try the updated version …

john

OR … perhaps there is a blank line causing the error ?

AND @lori
Make sure each csv file’s header has the Name header exactly like Name, and not name or NAME.

@DanRathbun @john_drivenupthewall - I tried a few of the csv files and made sure that each one had the header set to Name and still got the same error when using the latest version (v6 I believe) of the Ruby code.

  Error: #<NoMethodError: undefined method `split' for nil:NilClass>
<main>:17:in `block in <main>'
C:/Program Files/SketchUp/SketchUp 2020/Tools/RubyStdLib/csv.rb:1764:in `each'
C:/Program Files/SketchUp/SketchUp 2020/Tools/RubyStdLib/csv.rb:1142:in `block in foreach'
C:/Program Files/SketchUp/SketchUp 2020/Tools/RubyStdLib/csv.rb:1289:in `open'
C:/Program Files/SketchUp/SketchUp 2020/Tools/RubyStdLib/csv.rb:1141:in `foreach'
<main>:15:in `each'
<main>:15:in `map'
<main>:15:in `<main>'

I’m not sure where to go from here.

does it work on the .csv test file from above?

the test data looks like this…

ColorNO Name R G B HEX
250F-4 STONE BROWN 184 154 134 B89A86
270F-4 PEANUT BUTTER 202 162 127 CAA27F
280F-4 BURNT ALMOND 182 150 122 B6967A

you can PM me a couple of zipped folders, I can see if that the issue…

john

Could it have something to do with the fact that the csv files I’m using have only Name, R, G, B as the headers? I concatenated color number and color name from the original Excel spreadsheets when I created the csv’s so they’re different than the sample file.

@john_drivenupthewall - when I tried it with your sample csv this is the error that came up:

Error: #<NameError: undefined local variable or method `materials' for main:Object>
:31:in `'

Published ID Published Name may be the culprit…

checking…

@lori

swapping in this chunk will work with your ‘new’ headers, but won’t for standard headers…

      # for modified .csv files  and concatenated names
      # create array of name and hex value
      require 'csv.rb'
      csv_data = CSV.foreach(file, headers: true).map do |row|
        n = row[0].rstrip.split(' ').map(&:capitalize).join(' ')
        c = row[1..3].map(&:to_i)
        [n,c]
      end

I may find time to create a ‘catch all’ version, but it would be easier to use the original format and and concatenated names using ruby…

john

@john_drivenupthewall - Thanks for the swap. I tried it and it sort of worked. There were no error messages and all of the color names and values are showing in the Ruby console but only the last color in the csv pulled into the materials panel In Model. I’m sorry this has been such a colossal pain!

I delete them one by one as it runs to keep the performance up…

they should all be in your materials folder…

john

@john_drivenupthewall - they’re in there now! I guess I jumped the gun and didn’t let them process all the way. Woohoo! One quick thing though - the color names (ie. PPU25-08 HEIRLOOM SILVER) are supposed to be all uppercase. Is there a way for the script to not change the names and numbers? I realize all uppercase looks like yelling but that’s how they want it.

I hate YELLING so deliberately changed them…

  # n = row[0].rstrip.split(' ').map(&:capitalize).join(' ')
  n = row[0].rstrip

will fix that…

move the folders to trash and let it remake them all…

john

I’m with you on that. Yelling makes me crazy but the boss wants what the boss wants. Where do I delete the folders?

the new ones in materials?

the code creates them using

path = Sketchup.find_support_file('Materials')

so there should be a folder for each .csv file name in there…

your path will differ from mine, but run that snippet if you don’t know where to look…

john