syoyo / tinygltf

Header only C++11 tiny glTF 2.0 library
MIT License
1.94k stars 395 forks source link

Empty nodes are wrongly serialized as null #457

Closed ptc-tgamper closed 7 months ago

ptc-tgamper commented 7 months ago

Describe the issue

GLTF allows nodes to be empty, but not to be null. But tinygltf serializes empty nodes as a json::null object. The underlying reason is that the default constructor for detail::json (be it nlohmann or rapidjson) initializes the json object to a null object.

To Reproduce

The resulting gltf is invalid:

{
  "asset": {
    "version": "2.0"
  },
  "nodes": [
    null
  ],
  "scenes": [
    {
      "nodes": [
        0
      ]
    }
  ]
}

Expected behaviour

The empty nodes should be serialised as an empty json object, i.e. {} instead of null.

Additional context:

Validation error message:

Incorrect type. Expected "object".
Matches a schema that is not allowed.
Type mismatch. Property value null is not a 'object'.glTF Validator(TYPE_MISMATCH)
Node

A node in the node hierarchy. When the node contains `skin`, all `mesh.primitives` **MUST** contain `JOINTS_0` and `WEIGHTS_0` attributes. A node **MAY** have either a `matrix` or any combination of `translation`/`rotation`/`scale` (TRS) properties. TRS properties are converted to matrices and postmultiplied in the `T * R * S` order to compose the transformation matrix; first the scale is applied to the vertices, then the rotation, and then the translation. If none are provided, the transform is the identity. When a node is targeted for animation (referenced by an animation.channel.target), `matrix` **MUST NOT** be present.
syoyo commented 7 months ago

Good catch!

This reminds me this issue: https://github.com/syoyo/tinygltf/pull/295

Probably we also need to apply this fix to other glTF objects: https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-gltf

ptc-tgamper commented 7 months ago

It seems an empty scene causes the same problem.

syoyo commented 7 months ago

You can contribute a fix! PR is much appreciated.

ptc-tgamper commented 7 months ago

Will do.

syoyo commented 7 months ago

🙏