Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2022
32
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. version = Sketchup.version.to_i
  2. osx = Sketchup.platform == :platform_osx
  3. # select a colour list
  4. file = UI.openpanel(
  5. 'Select CSV File',
  6. osx ? '~' : ENV['HOME'],
  7. osx ? '*.csv' : 'CSV Files|*.csv||'
  8. )
  9.  
  10. if version >= 17
  11. # extract folder name
  12. dir = File.basename(file, '.csv')
  13.  
  14. # create array of name and hex value
  15. require 'csv.rb'
  16. csv_data = CSV.foreach(file, headers: true).map do |row|
  17. h = row.to_h
  18. n = h['Name'].split(' ').map(&:capitalize).join(' ').rstrip
  19. c = [ h['R'].to_i, h['G'].to_i, h['B'].to_i ]
  20. [n,c]
  21. end
  22.  
  23. #create new material
  24. # Get a handle to all the materials in the current model.
  25. model = Sketchup.active_model
  26. mats = model.materials
  27. path = Sketchup.find_support_file('Materials')
  28. main = File.join(path, dir)
  29. Dir.mkdir(main) unless Dir.exist?(main)
  30.  
  31. # process data
  32. mats.purge_unused
  33. @mat = mats.add('placeholder')
  34. csv_data.each do |name,colour|
  35. filename = File.join(main, name + '.skm')
  36. @mat.name = name
  37. @mat.color = Sketchup::Color.new(colour.entries)
  38. #sets enscape roughness parameter
  39. @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>")
  40. @mat.save_as(filename)
  41. end
  42. end
Advertisement
RAW Paste Data Copied
Advertisement