First you need to write a text file, let’s name it materials.csv
I’ll assume it’s Comma-Separated-Variables [also readable/writable in Excel etc]
Provide the format consistently thus:
Name,R,G,B,A
then it’s easily read line by line and parsed into the Name, Red, Green, Blue [0 to 255] and Alpha [1.0 to 0] values.
If the Alpha is not important you can skip it.
Let’s assume an example of:
My Lovely Red,255,0,0,1
First set:
model=Sketchup.active_model mats=model.materials
Then:
path=UI.openpanel('Materials File...', Dir.pwd, 'materials.csv')
Now read its contents:
lines=IO.readlines(path)
Now process the lines:
lines.each{|line|
line.chomp! # remove \n at the end
next if line.empty?
next unless line =~ /[0-9]/ # no rgb
next if line =~ /^[#]/ # is commented out with a leading #
n,r,g,b,a = line.split(',')
mat=mats.add(n)
mat.color=Sketchup::Color.new(r.to_i, g.to_i, b.to_i)
mat.alpha= a.to_f if a
}
You should now have a material named ‘My Lovely Red’ that is colored red and is fully opaque.
If you process a long list ensure the model is empty and purged before starting.
Then, when its made the new materials, export the model’s materials as a new collection - you’ll get a SKM file in its folder for each material described in your CSV file…