Skip to content

Add import and export of iDynTree::Model from custom iDynTree format idyntree-model-json#1295

Merged
traversaro merged 2 commits intomasterfrom
fix1294
Mar 23, 2026
Merged

Add import and export of iDynTree::Model from custom iDynTree format idyntree-model-json#1295
traversaro merged 2 commits intomasterfrom
fix1294

Conversation

@traversaro
Copy link
Copy Markdown
Contributor

@traversaro traversaro commented Mar 18, 2026

Fix #1294 .

Whenever we worked with iDynTree::Model, we frequently have the need to export the state of the iDynTree::Model. This is important especially in environements that are tricky to debug, for example when iDynTree is running inside Creo, as part of https://github.com/icub-tech-iit/creo2urdf .

This PR adds the support for import and export of a idyntree-specific .json format, called in the docs, parameters and cli options idyntree-model-json.

Differently from urdf and sdf formats, the goal of this format is not interoperability with other systems (at least not the main goal) but just to have a reliable direct serialization of exactly the content of an iDynTree::Model.

For example, this is how a simple .urdf model is converted, using the new idyntree-model-convert cli tool, added as well in this PR:

Click to see the example of URDF to json
(idyntree) straversaro@gblaptop0007:~/idyntree$ cat ./src/tests/data/simple_model.urdf 
<?xml version="1.0"?>
<robot name="oneLink">
    <material name="blue">
        <color rgba="0 0 .8 1"/>
    </material>
    <link name="link1">
        <inertial>
            <mass value="1"/>
            <inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.01" iyz="0.0" izz="0.01"/>
        </inertial>
        <visual>
            <geometry>
                <cylinder length="0.6" radius="0.2"/>
            </geometry>
            <material name="blue" />
        </visual>
    </link>
</robot>
(idyntree) straversaro@gblaptop0007:~/idyntree$ idyntree-model-convert -i ./src/tests/data/simple_model.urdf -o simple_model.json
Converted model from './src/tests/data/simple_model.urdf' (urdf) to 'simple_model.json' (idyntree-model-json).
(idyntree) straversaro@gblaptop0007:~/idyntree$ cat ./simple_model.json 
{
  "additional_frames": [],
  "collision_solid_shapes": [
    {
      "link_name": "link1",
      "shapes": []
    }
  ],
  "default_base_link": "link1",
  "idyntree_model_json_version": 1,
  "joints": [],
  "links": [
    {
      "inertia": {
        "center_of_mass": [
          0.0,
          0.0,
          0.0
        ],
        "mass": 1.0,
        "rotational_inertia_wrt_frame_origin": [
          [
            0.01,
            0.0,
            0.0
          ],
          [
            0.0,
            0.01,
            0.0
          ],
          [
            0.0,
            0.0,
            0.01
          ]
        ]
      },
      "name": "link1"
    }
  ],
  "sensors": [],
  "visual_solid_shapes": [
    {
      "link_name": "link1",
      "shapes": [
        {
          "has_material": true,
          "has_name": false,
          "length": 0.6,
          "link_H_geometry": {
            "position": [
              0.0,
              0.0,
              0.0
            ],
            "rotation_matrix": [
              [
                1.0,
                0.0,
                0.0
              ],
              [
                0.0,
                1.0,
                0.0
              ],
              [
                0.0,
                0.0,
                1.0
              ]
            ]
          },
          "material": {
            "color": [
              0.0,
              0.0,
              0.8,
              1.0
            ],
            "has_color": true,
            "has_texture": false,
            "name": ""
          },
          "radius": 0.2,
          "type": "cylinder"
        }
      ]
    }
  ]
}

This permits to easily export any kind of iDynTree::Model without having to deal with the specific limitations of the specific file format.

Note: the format is by definition quite related to the iDynTree internals, and can change without backward compatibility from one version of iDynTree to another. The specific format version is tracked by the idyntree_model_json_version top level json attribute.

The nlohmann_json C++ dependency has been added to permit to import and export the .json files, the support for it is controlled via the IDYNTREE_USES_NLOHMANN_JSON option.

As it frequently happens with Agent-assisted coding, the amount of code in the PR may be challenging for review. For this reason, I strongly suggest any reviewer interested in the PR to ignore the src/model_io/codecs/src/ModelJSONImportExport.cpp import/export code, and instead check out the important test code, that is the one that actually ensures that everything works as expected.

@traversaro traversaro merged commit 7464dda into master Mar 23, 2026
32 checks passed
@traversaro traversaro deleted the fix1294 branch March 23, 2026 11:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add import and export of iDynTree::Model from custom iDynTree format

1 participant