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
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement