Entities Edge color apply
-
Can some one say why this is working: (where 's2' is Sketchup::Edge entity)
material_name=e2.material if mat cname=material_name.name else cname="<Default>" end#ifbut this is not working:
material_name=e2.material.nameAfter this code Ruby returns this error:
Error: #<NoMethodError: undefined method `name' for nil:NilClass>Why i cannot strait forward access material name.
-
Because the
material()method returns aSketchup::Materialobject, not aStringobject, But ONLY IF the entity has been assigned a material. If the entity has no material, then thematerial()method returnsnil.This is standard operating procedure in Ruby. It is common to test using
if, because in Ruby onlyfalseandnilevaluate logically false. Everything else, including0, an emptyArrayand emptyStrings, evaluate as logically true.So you do:
if mat = e2.material() mat_name = mat.name() else mat_name = "<Default>" endYou cannot call
.name()on the singletonNilClassobject (nil,) because it does NOT have a instance method called 'name'.) -
Thanks it helped
-
mat=e2.material if mat cname=mat.name ### OR .display_name ? else ### NO MATERIAL cname="<Default>" end#iftest for the existence of the material and then get it's name if it exists, if the material is '
nil'then there's no 'name' to get. -
Late to the party

-
Thanks TIG there can be made changes in my code even more later

-
@tig said:
mat=e2.material > if mat > cname=mat.name ### OR .display_name ? > else ### NO MATERIAL > cname="<Default>" > end#iftest for the existence of the material and then get it's name if it exists, if the material is '
nil'then there's no 'name' to get.#display_nameshould only be used to output the name of a material to the UI - not as an id.
http://www.thomthom.net/thoughts/2012/03/the-secrets-of-sketchups-materials/#speaking-of-names8230
Advertisement