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#if
but this is not working:
material_name=e2.material.name
After 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::Material
object, not aString
object, 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 onlyfalse
andnil
evaluate logically false. Everything else, including0
, an emptyArray
and emptyString
s, evaluate as logically true.So you do:
if mat = e2.material() mat_name = mat.name() else mat_name = "<Default>" end
You cannot call
.name()
on the singletonNilClass
object (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#if
test 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#if
test 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_name
should 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