Transfer 'color by layer' to 'color by material'
-
Have you seen the plugin that does the subject?
Thanks in advance -
To clarify...
Do you mean, to take each layer's color, and use that color for a [new] material [probably named based on the parent-layer's name?], then apply that material to all entities which are assigned that layer.If so try this... copy+paste+enter in the Ruby Console.
mo=Sketchup.active_model cs={} ns=[] ls=mo.layers p ls[0].name ls.to_a[1..-1].each{|e| #skip Layer0 cs[e.name]=e.color ns<<e.name } mo.start_operation('lcol>>mcol',true) ms=mo.materials ns.each{|k| m=ms.add(k) unless m=ms[k] m.color=cs[k] } mo.entities.each{|e| e.material=ms[e.layer.name] unless e.layer==ls[0] #Layer0 } mo.definitions.each{|d| next if d.image? if d.group? d.entities.each{|e| e.material=ms[e.layer.name] unless e.layer==ls[0] #Layer0 } else #compo d.entities.each{|e| e.material=ms[e.layer.name] unless e.layer==ls[0] #Layer0 } d.instances.each{|e| e.material=ms[e.layer.name] unless e.layer==ls[0] #Layer0 } end } mo.commit_operation puts
It's one step undoable.
It currently skips processing 'Layer0' - but to include it change:
ls.to_a[1..-1].each
to readls.each
and add a new # in front of the 4 lines**#** unless e.layer==ls[0] #Layer0
Toggle between Material and Layer view settings to see the effect...
-
Works almost perfect. Please check PM.
Many thanks! -
Transparency [alpha] in colors used 'by layer' is not detectable - I think?
It always returns 255, even when it displays as less !
Neither is a layer's 'texture'.This version also processes 'back-face color'.
mo=Sketchup.active_model cs={} ns=[] ls=mo.layers p ls[0].name ls.to_a[1..-1].each{|e| #skip Layer0 cs[e.name]=e.color ns<<e.name } mo.start_operation('lcol>>mcol',true) ms=mo.materials ns.each{|k| m=ms.add(k) unless m=ms[k] m.color=cs[k] } mo.entities.each{|e| e.material=ms[e.layer.name] unless e.layer==ls[0] #Layer0 e.back_material=ms[e.layer.name] if e.is_a?(Sketchup;;Face) && e.layer!=ls[0] #Layer0 } mo.definitions.each{|d| next if d.image? if d.group? d.entities.each{|e| e.material=ms[e.layer.name] unless e.layer==ls[0] #Layer0 e.back_material=ms[e.layer.name] if e.is_a?(Sketchup;;Face) && e.layer!=ls[0] #Layer0 } else #compo d.entities.each{|e| e.material=ms[e.layer.name] unless e.layer==ls[0] #Layer0 e.back_material=ms[e.layer.name] if e.is_a?(Sketchup;;Face) && e.layer!=ls[0] #Layer0 } d.instances.each{|e| e.material=ms[e.layer.name] unless e.layer==ls[0] #Layer0 } end } mo.commit_operation puts
-
Amazing! Many thanks.
P.S. Please publish it it Store so it won't get buried in forum. -
Let's wait a day or two until it gets fine tuned...
Advertisement