Material name vs display_name

ThomThom said

Speaking of Names…
The Material class has two seemingly similar methods, #name and #display_name. The documentation says that #display_name is preferred, but doesn’t explain why.

The difference is that if a material name is wrapped in square brackets, like “[MyMayerial]”, then #name will return “[MyMayerial]” and display_name will return “MyMayerial”. The latter is how SketchUp will display the material name in the UI, while #name returns the real name.

If you refer to a material by name, then you must use the string you get from #name instead of #display_name. model.materials[ material.display_name ] will return nil for materials wrapped in square brackets because the look-up failed.

So what the documentation should be saying is that #display_name is preferred when you present the material name to the UI, while #name must be used to look-up materials in the Materials collection.

1 Like