autodesk-forks / MaterialX

MaterialX C++ and Python libraries
http://www.materialx.org/
Apache License 2.0
107 stars 23 forks source link

JS Bindings: Wrap all functions that return value pointers #1260

Closed frericp closed 3 years ago

frericp commented 3 years ago

This PR wraps all remaining functions that return a ValuePtr (ValueElement::getValue was already wrapped before). Calling these functions will now return the value directly, removing the need to call getData first. The original behavior is exposed through functions prefixed with _, e.g. _getValue.

While this aligns the JS bindings with the Python ones, I'm not particularly happy with the approach. The need to call getData doesn't justify the introduction of potentially error-prone wrapping code, for the following reasons:

--> Voting to not wrap the functions in the first place @sdunkel @muenstc @kohakukun

Update #1184

frericp commented 3 years ago

Still working on some more involved cases, so this isn't final yet. Ready for review

kohakukun commented 3 years ago

I really don't have a strong opinion about this. On the one hand, for the first two points, I dont think there is an issue. I think this a fairly stable API. On the technical part, I agree with you but, as a javascript user, I would find it very off-putting having to call the getData() from a function like getFloatValue :/

frericp commented 3 years ago

@bernardkwok @ashwinbhat We have discussed this topic internally, and came to the conclusion that wrapping the ValuePtr returning functions might not be beneficial enough to justify the drawbacks. However, we would like to take an approach that is consistent between Python and JS (and ideally C++, in general). Since the Python bindings are currently (partially) wrapping such functions, what is your opinion on how we should handle them?

If we decide to diverge from the C++ API here, how could we document this in a more obvious way than putting it in a readme? We have a paragraph on it here at the moment: https://github.com/autodesk-forks/MaterialX/blob/adsk_contrib/dev/source/JsMaterialX/README.md#value-getters

bernardkwok commented 3 years ago

@bernardkwok @ashwinbhat We have discussed this topic internally, and came to the conclusion that wrapping the ValuePtr returning functions might not be beneficial enough to justify the drawbacks. However, we would like to take an approach that is consistent between Python and JS (and ideally C++, in general). Since the Python bindings are currently (partially) wrapping such functions, what is your opinion on how we should handle them?

If we decide to diverge from the C++ API here, how could we document this in a more obvious way than putting it in a readme? We have a paragraph on it here at the moment: https://github.com/autodesk-forks/MaterialX/blob/adsk_contrib/dev/source/JsMaterialX/README.md#value-getters

It would still be good to get an opinion from the LucasFilm side if this hasn't been asked yet. As far as I see it seems that the Python wrappers wrap all types as "objects" which have methods on then, even for simple types like float, int etc.

It could be the deciding factor is what operations are possible at the "object level".

For something like an array, this is a reference to a array object, but could also be an array of references to the atomic type, or just unreferenced atomic values. Any form could work for querying and conversion to string which is common in C++, but not sure if this is really required for Python and Javascript.

This leaves editing operations and conversion from strings which would not be possible if it's not referenced.

I think one other factor to take into consideration is locale support which @ashwinbhat did some work on for Value. It could mean that at least for compound types we want an object to be able to translate to/from string representations.

My 2 cents at this point.

frericp commented 3 years ago

I'm closing this PR because the value wrappers turned out to be a legacy feature of the Python bindings that we'll probably want to deprecate.