Colored faces
-
I was wondering why a code of color only change only normal face , not including back face when I select all faces.Is there any mistakes ?
def gl_green model=Sketchup.active_model ss=model.selection materials=model.materials faces=[] ss.each{|e|faces << e if e.class==Sketchup;;Face} faces.each{|face| face.material=Sketchup;;Color.new(0,250,154)} faces.each{|face| face.material.alpha = 0.5} end
-
Use also:
face.back_material=
-
The two sides of a face have to be treated independently
face.material = a_material face.back_material = a_material
### orface.back_material = another_material
As they do not have to be the same.
Because you usually do not see the back-face inside 3d objects it's usual to leave it 'nil' == the default back-color...If a face is inside a group or component definition and it has no material*... then it displays using the
group/instance.material
]if any] both *front and back.
Note that this has not changed the face's material - only the way you see it. -
Thank you very much for your guiding ! It works !
For Face color
#*************************************************** # Glass #*************************************************** def gl_green model=Sketchup.active_model ss=model.selection materials=model.materials faces=[] ss.each{|e|faces << e if e.class==Sketchup;;Face} faces.each{|face| face.material=Sketchup;;Color.new(0,250,154)} faces.each{|face| face.back_material=Sketchup;;Color.new(0,250,154)} faces.each{|face| face.material.alpha = 0.5} faces.each{|face| face.back_material.alpha = 0.5} end
For Group color
#*************************************************** # Aluminium #*************************************************** def al_Light_Bronze model=Sketchup.active_model ss=model.selection materials=model.materials layers=model.layers #Now check what is in the selection... in this example we extract the groups groups=[] ss.each{|e|groups << e if e.class==Sketchup;;Group} #Now process the groups m = Sketchup.find_support_file "CW/Textures/Aluminium/AnodizedAluminium_Light-Bronze.jpg", "Plugins" groups.each{|group| group.material=material_add_texture("Light-Bronze", m, "*")} end
-
Change:
faces=[] ss.each{|e|faces << e if e.class==Sketchup;;Face}
To:
faces = ss.grep(Sketchup;;Face)
The
grep
method comes from theEnumerable
library module, that is mixed into many collection classes.
Thegrep
method is faster than anyway you could write the same functionality in Ruby.
Also you are repeating things in loops that only need be done ONCE.
And using multiple loops when only ONE loop is needed.Change:
faces.each{|face| face.material=Sketchup;;Color.new(0,250,154)} faces.each{|face| face.back_material=Sketchup;;Color.new(0,250,154)} faces.each{|face| face.material.alpha = 0.5} faces.each{|face| face.back_material.alpha = 0.5}
To:
matl = Sketchup;;Color.new(0,250,154) matl.alpha = 0.5 faces.each{|face| face.back_material = face.material = matl }
Last.. please use a 2 SPACE indent for code examples here.. TAB characters do not work well with bbCode boxes.
-
little benchmark: windows 8, sketchup 2013 maker, cpu 4900mq
code:
require 'benchmark' mod = Sketchup.active_model # Open model ent = mod.entities # All entities in model sel = mod.selection # Current selection arr = ent.to_a print "sel;",sel.to_a.size," == ent;",ent.to_a.size puts "",arr Benchmark.bm(20) do |x| x.report("ent.greb") { 100000.times { faces = ent.grep(Sketchup;;Edge) } } x.report("ent.select,is_a?") { 100000.times { faces = ent.select{|e| e.is_a? Sketchup;;Edge } } } x.report("ent.select,class") { 100000.times { faces = ent.select{|e| e.class==Sketchup;;Edge } } } x.report("ent.each,is_a?") { 100000.times { faces=[];ent.each{|e|faces << e if e.is_a? Sketchup;;Edge} } } x.report("ent.each,class") { 100000.times { faces=[];ent.each{|e|faces << e if e.class==Sketchup;;Edge} } } x.report("sel.greb") { 100000.times { faces = sel.grep(Sketchup;;Edge) } } x.report("sel.select,is_a?") { 100000.times { faces = sel.select{|e| e.is_a? Sketchup;;Edge } } } x.report("sel.select,class") { 100000.times { faces = sel.select{|e| e.class==Sketchup;;Edge } } } x.report("sel.each,is_a?") { 100000.times { faces=[];sel.each{|e|faces << e if e.is_a? Sketchup;;Edge} } } x.report("sel.each,class") { 100000.times { faces=[];sel.each{|e|faces << e if e.class==Sketchup;;Edge} } } x.report("arr.greb") { 100000.times { faces = arr.grep(Sketchup;;Edge) } } x.report("arr.select,is_a?") { 100000.times { faces = arr.select{|e| e.is_a? Sketchup;;Edge } } } x.report("arr.select,class") { 100000.times { faces = arr.select{|e| e.class==Sketchup;;Edge } } } x.report("arr.each,is_a?") { 100000.times { faces=[];arr.each{|e|faces << e if e.is_a? Sketchup;;Edge} } } x.report("arr.each,class") { 100000.times { faces=[];arr.each{|e|faces << e if e.class==Sketchup;;Edge} } } end
result:
user system total real ent.greb 0.672000 0.000000 0.672000 ( 0.681000) ent.select,is_a? 1.329000 0.000000 1.329000 ( 1.328000) ent.select,class 1.437000 0.000000 1.437000 ( 1.445000) ent.each,is_a? 1.266000 0.000000 1.266000 ( 1.264000) ent.each,class 1.390000 0.000000 1.390000 ( 1.391000) sel.greb 0.657000 0.000000 0.657000 ( 0.655000) sel.select,is_a? 1.343000 0.000000 1.343000 ( 1.330000) sel.select,class 1.469000 0.000000 1.469000 ( 1.452000) sel.each,is_a? 1.250000 0.000000 1.250000 ( 1.260000) sel.each,class 1.375000 0.000000 1.375000 ( 1.386000) arr.greb 0.391000 0.000000 0.391000 ( 0.401000) arr.select,is_a? 0.735000 0.000000 0.735000 ( 0.740000) arr.select,class 0.860000 0.000000 0.860000 ( 0.873000) arr.each,is_a? 0.953000 0.000000 0.953000 ( 0.960000) arr.each,class 1.078000 0.000000 1.078000 ( 1.090000)
data:
sel;28 == ent;28 #<Sketchup;;Edge;0x18cf0054> #<Sketchup;;Edge;0x18cf0018> #<Sketchup;;Edge;0x18cf0090> #<Sketchup;;Edge;0x18ceffdc> #<Sketchup;;Face;0x18cf00cc> #<Sketchup;;Group;0x18ce5e4c> #<Sketchup;;Edge;0x18eacf64> #<Sketchup;;Edge;0x18eacfdc> #<Sketchup;;Edge;0x18eacf28> #<Sketchup;;Edge;0x18eacfa0> #<Sketchup;;Face;0x18ead018> #<Sketchup;;Edge;0x18bd1254> #<Sketchup;;Edge;0x18bd11dc> #<Sketchup;;Edge;0x18bd1218> #<Sketchup;;Edge;0x18bd11a0> #<Sketchup;;Face;0x18bd1290> #<Sketchup;;Edge;0x18bd0bb0> #<Sketchup;;Edge;0x18bd0b74> #<Sketchup;;Edge;0x18bd0b38> #<Sketchup;;Edge;0x18bd0afc> #<Sketchup;;Face;0x18bd0bec> #<Sketchup;;Edge;0x18bcefb8> #<Sketchup;;Edge;0x18bcee8c> #<Sketchup;;Edge;0x18bcef7c> #<Sketchup;;Edge;0x18bceec8> #<Sketchup;;Edge;0x18bcef40> #<Sketchup;;Edge;0x18bcef04> #<Sketchup;;Face;0x18bceff4>
-
I changed a code from your suggestion , but faces change color without transparency.
What happens ?def gl_green model=Sketchup.active_model ss=model.selection faces = ss.grep(Sketchup;;Face) matl = Sketchup;;Color.new(0,250,154) matl.alpha = 0.5 faces.each{|face| face.back_material = face.material = matl } end
-
try changing matl = Sketchup::Color.new(0,250,154) to (0,250,154, 100)and
faces.each{|face| face.back_material = face.material = matl face.material.alpha = 0.5 }
edit: Updated code for backface as you wanted. And the 4rth argument is actually not needed in th Color object..
-
@jolran said:
try changing matl = Sketchup::Color.new(0,250,154) to (0,250,154, 100)and
faces.each{|face| > face.back_material = face.material = matl > face.material.alpha = 0.5 > }
edit: Updated code for backface as you wanted. And the 4rth argument is actually not needed in th Color object..
Setting alpha for face material inside the loop is not efficient. It'd be better set outside the loop.
-
Thank you so much !!! Love You All'
I changed as your suggestion , Jolran
It can change colors of glass now. I appreciate it !
There are many colors that I have to revise.
I will also change others as your example.
def gl_green model=Sketchup.active_model ss=model.selection faces = ss.grep(Sketchup;;Face) matl = Sketchup;;Color.new(0,250,154) faces.each{|face| face.back_material = face.material = matl face.material.alpha = 0.5 } end
-
def gl_green model=Sketchup.active_model ss=model.selection faces = ss.grep(Sketchup;;Face) matl = Sketchup;;Color.new(0,250,154) matl.alpha = 0.5 faces.each{|face| face.back_material = face.material = matl } end
No You are jumbling up 'material' and 'color'.
Make your material then give it a color, alpha etc - then give faces that material...
More like this...matl = model.materials.add('gl_green') matl.color = Sketchup;;Color.new(0,250,154) matl.alpha =0 .5 faces.each{|face| face.material = matl face.back_material = matl }
-
Hi , TIG
This is correct , right ? I test it and it gives a name of material !!
def gl_green model=Sketchup.active_model ss=model.selection faces = ss.grep(Sketchup;;Face) matl = model.materials.add('gl_green') matl.color = Sketchup;;Color.new(0,250,154) matl.alpha =0.5 faces.each{|face| face.material = matl face.back_material = matl } end
-
He's actually using one of the standard color set.
-
As Dan says you can create and set materials in several ways.
face.material='red'
works because 'red
' is a standard 'color', and a material will be auto-created from that if it doesn't exist.
But
face.material='redex'
might fail because that isn't a standard color - that is unless it already exists in the model.materials, and it has that very name...It's best to try and separate materials and colors in your thought process...
Make the new materials up front and then specify them by name after that...
` mat=model.materials.add('redex')
mat.color='red'
mat.alpha=0.33later
face.material=mat
or
face.material='redex'`
etc -
Thank You again Dan and TIG ! . I understand an idea now.
Advertisement