AcademySoftwareFoundation / MaterialX

MaterialX is an open standard for the exchange of rich material and look-development content across applications and renderers.
http://www.materialx.org/
Apache License 2.0
1.82k stars 332 forks source link

Suggestion: provide string constants for all types #1935

Open pablode opened 1 month ago

pablode commented 1 month ago

Currently, many applications that use MaterialX repeatedly write out type strings:

const bool colorInput = (type == "color3" || type == "color4");

One problem with this is that the correct spelling of the types is not ensured by the compiler (e.g. one could by accident write float2 instead of vector2, bool instead of boolean, ..). Additionally, this paradigm does not work well with auto-completion.

Types.cpp already provides some string constants, but many others are missing: https://github.com/AcademySoftwareFoundation/MaterialX/blob/dfffe83f74e742edc3743f004da359c35cef5652/source/MaterialXCore/Types.cpp#L10-L23

Furthermore, the names are rather wordy. I believe that something short is to be preferred due to frequent use. As an example, Type String could be abbreviated and used as a prefix or namespace.

Above snippet would become:

const bool colorInput = (type == mx::TS_COLOR3 || type == mx::TS_COLOR4);
kwokcb commented 1 month ago

For "base" types you can use the templated method getTypeString<> in C++. An example usage can be found here or here

I do not think there are Python or Javascript equivalent wrappers however, but should help to prevent C++ typo errors.

The ones you mention are not concrete C++ structures (except color3).