Back_material of a face
-
Hallo!
I am going to set the material of a face in the following method. To avoid problems with the oriatation of the face's normal I want to set its back_material, too.
I tried it with the following code:def status_anzeigen #funktioniert, nur das mit dem alpha wert im material inplanung noch nicht so mod = Sketchup.active_model ents = mod.entities sel = mod.selection mats = mod.materials bau_mat = mats.add "im_bau" if not bau_mat = mats["im_bau"] bau_mat.color = [255,127,38] fertig_mat = mats.add "fertig" if not fertig_mat =mats["fertig"] fertig_mat.color = [211,211,211] inplanung = mats.add"noch_nicht_angefangen" if not inplanung = mats["noch_nicht_angefangen"] inplanung.color = [176,196,222] inplanung.alpha = 0.3 ents.each{|e| if e.typename == "Face" start = e.get_attribute "ablauf", "baubeginn" #attributes that were assigned to the faces before ende = e.get_attribute "ablauf", "bauende" #attributes that were assigned to the faces before if (e.material) #checking wehther the faces current material belongs to the ones created at the top of the method, if not, save the name of the current material as an attribut invalid_materials = ["im_bau", "fertig", "noch_nicht_angefangen"] unless invalid_materials.include?( e.material.name ) mat_vorher = e.material.name e.set_attribute("ablauf", "material", mat_vorher) end end if @woche.to_i >= start.to_i && @woche.to_i < ende.to_i #@woche variable of the class e.material = mats["im_bau"] e.back_material = mats["im_bau"] #why doesn't it work? end if @woche.to_i >= ende.to_i e.material =mats["fertig"] e.back_material =mats["fertig"] #why doesn't it work? end if @woche.to_i < start.to_i e.material = mats["noch_nicht_angefangen"] e.back_material = mats["noch_nicht_angefangen"] #why doesn't it work? end end } end
Only the "front" of the face was changed to the new material. Does anybody know what mistake I made?
Thanks for your time.
Tim -
Well making the backs and fronts match will improve appearance BUT it won't work with many 3rd party render apps which render all back-faces a default material ignoring their actual back material - and render the front correctly - which is then facing away!
To make all materials be on both sides is straightforward...
You seems to be making it more complicated than it needs to be ?
EDIT: ALSO as Dan points out, your code is also flawed in several places... e.g. '==' compares values, but '=' sets a value!
Collect all faces in the model
faces=[];Sketchup.active_model.entities.each{|e|faces << e if e.class==Sketchup::Face}
Now look at each face and decide what to do
faces.each{|face| if face.material face.back_material=face.material else # there's no front material if face.back_material face.material=face.back_material end end }
Now all faces have the same front and back material.
The front material [if any] takes precedence over the back material.
If there's no front-material then the back material is also used on the front. -
Lines 6, 8 and 10:
use the comparison operator
==
, not the assignment operator,=
. -
But that part of the code:
if @woche.to_i >= start.to_i && @woche.to_i < ende.to_i #@woche variable of the class e.material = mats["im_bau"] e.back_material = mats["im_bau"] #why doesn't it work? end if @woche.to_i >= ende.to_i e.material =mats["fertig"] e.back_material =mats["fertig"] #why doesn't it work? end if @woche.to_i < start.to_i e.material = mats["noch_nicht_angefangen"] e.back_material = mats["noch_nicht_angefangen"] #why doesn't it work? end
should have been working, shouldn't it?
I don't understand why the materials were assigned to the front, but not to the back of the face.
any idea? -
working meanwhile...
thanks for the help.
Advertisement