Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- version = Sketchup.version.to_i
- osx = Sketchup.platform == :platform_osx
- # select a colour list
- file = UI.openpanel(
- 'Select CSV File',
- osx ? '~' : ENV['HOME'],
- osx ? '*.csv' : 'CSV Files|*.csv||'
- )
- if version >= 17
- # extract folder name
- dir = File.basename(file, '.csv')
- # create array of name and hex value
- require 'csv.rb'
- csv_data = CSV.foreach(file, headers: true).map do |row|
- h = row.to_h
- n = h['Name'].split(' ').map(&:capitalize).join(' ').rstrip
- c = [ h['R'].to_i, h['G'].to_i, h['B'].to_i ]
- [n,c]
- end
- #create new material
- # Get a handle to all the materials in the current model.
- model = Sketchup.active_model
- mats = model.materials
- path = Sketchup.find_support_file('Materials')
- main = File.join(path, dir)
- Dir.mkdir(main) unless Dir.exist?(main)
- # process data
- mats.purge_unused
- @mat = mats.add('placeholder')
- csv_data.each do |name,colour|
- filename = File.join(main, name + '.skm')
- @mat.name = name
- @mat.color = Sketchup::Color.new(colour.entries)
- #sets enscape roughness parameter
- @mat.set_attribute("Enscape.Material","MaterialData","<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<SketchupMaterial xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" Version=\"4\">\r\n <Roughness>0.61</Roughness> <Specular>0.5</Specular></SketchupMaterial>")
- @mat.save_as(filename)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement