tinygltf
tinygltf copied to clipboard
Feature: Add entries to existing tinygltf::Values
Hi @syoyo,
how are you doin'? Thanks for the quick merge the other day. I continued exploring and came across another small issue with tinygltf::Value
.
Consider a typical baking job that 1. reads an asset, 2. adds some extras (or extensions) and 3. saves the file. In this scenario we'd like to add entries to an existing object incrementally without replacing the whole object, because it may already contain entries from earlier stages (eg. the exporter). I couldn't find a (good) way to do that...
This PR adds a subscript operator to insert entries into objects. To maintain feature parity I also added a push method and subscript operators for arrays.
Let me know what you think...
We also could expose Resize()
and maybe even Reserve()
on arrays ... but I don't think that's necessary. I can add them if you want, though...
You are better first explicitly cast Value data to Array or Object
Apologies, but I don't understand what you mean. Could you elaborate or paste an example?
Hi @syoyo, could you do me a favour and bonk the button on this? For me it's an actual issue and it would be great to get it landed.
tinygltf::Value
must be rewritten to make it type safe and remove assert
in Value
class for better security.
For example, ArrayLen
, and const Value &Get(int idx) const
(and other non-type safe API) must be removed.
It'd be better to provide type safe API to Value, such like:
bool GetAsArray(Value::Array *arr) {
if (IsArray()) {
if (arr) { (*arr) = array_value_; return true}
}
return false;
}
// Do same thing for Value::Object type.
If you want to access array object in current Value API, you could do something like
if (v.IsArray()) {
Array arr = v.Get<Value::Array>();
for (item : arr) {
...
}
v = Value(arr); // update
}