Tutorial 24 - Overridden Types

By default the code generator will provide POD types for simple data, and USD types for tuple data (e.g. float and pxr::GfVec3f). Sometimes you may have your own favourite math library and want to use its data types directly rather than constantly using a _reinterpret_cast_ on the attribute values. To facilitate this, JSON data which contains type overrides for one or more of the attribute types may be provided so that the generated code will use those types directly.

OgnTutorialOverrideType.ogn

The ogn file shows the implementation of a node named “omni.graph.tutorials.OverrideType”, which has one input and one output attribute that use an overridden type for float[3].

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
{
    "OverrideType" : {
        "version": 1,
        "categories": "tutorials",
        "description": [
            "This is a tutorial node. It has an input and output of type float[3], an input and output of type",
            "double[3], and a type override specification that lets the node use Carbonite types for the generated",
            "data on the float[3] attributes only. Ordinarily all of the types would be defined in a seperate",
            "configuration file so that it can be shared for a project. In that case the type definition build flag",
            "would also be used so that this information does not have to be embedded in every .ogn file in",
            "the project. It is placed directly in the file here solely for instructional purposes.",
            " The compute is just a rotation of components from x->y, y->z, and z->x, for each input type."
        ],
        "uiName": "Tutorial Node: Overriding C++ Data Types",
        "inputs": {
            "typedData": {
                "type": "float[3]",
                "uiName": "Input value with a modified float type",
                "description": "The value to rotate"
             },
             "data": {
                "type": "double[3]",
                "uiName": "Input value with a standard double type",
                "description": "The value to rotate"
             }
        },
        "outputs": {
            "typedData": {
                "type": "float[3]",
                "uiName": "Output value with a modified float type",
                "description": "The rotated version of inputs::typedData"
            },
            "data": {
                "type": "double[3]",
                "uiName": "Output value with a standard double type",
                "description": "The rotated version of inputs::data"
            }
        },
        "$typeDefinitionDescription": "This redefines the generated output type for the float[3] type only.",
        "typeDefinitions": {
            "c++": {
                "float[3]": ["carb::Float3", ["carb/Types.h"]]
            }
        },
        "tests": [
            {
                "inputs:data": [1.0, 2.0, 3.0],
                "outputs:data": [2.0, 3.0, 1.0]
            }
        ]
    }
}

OgnTutorialOverrideType.cpp

The cpp file contains the implementation of the compute method. The default type implementation would have a return type of pxr::GfVec3f but this one uses the override type of carb::Float3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
//
// NVIDIA CORPORATION and its licensors retain all intellectual property
// and proprietary rights in and to this software, related documentation
// and any modifications thereto.  Any use, reproduction, disclosure or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA CORPORATION is strictly prohibited.
//
#include <OgnTutorialOverrideTypeDatabase.h>

// The include files required by the type definition override is in the database definition so it does not have to
// be directly included here.

namespace omni
{
namespace graph
{
namespace core
{
namespace tutorial
{

class OgnTutorialOverrideType
{
public:
    static bool compute(OgnTutorialOverrideTypeDatabase& db)
    {
        // Usually you would use an "auto" declaration. The type is explicit here to show how the override has
        // changed the generated type.
        const carb::Float3& inputTypedData = db.inputs.typedData();
        carb::Float3& outputTypedData = db.outputs.typedData();

        const pxr::GfVec3d& inputStandardData = db.inputs.data();
        pxr::GfVec3d& outputStandardData = db.outputs.data();

        // Rearrange the components as a simple way to verify that compute happened
        outputTypedData.x = inputTypedData.y;
        outputTypedData.y = inputTypedData.z;
        outputTypedData.z = inputTypedData.x;

        outputStandardData.Set(inputStandardData[1], inputStandardData[2], inputStandardData[0]);

        return true;
    }
};

// namespaces are closed after the registration macro, to ensure the correct class is registered
REGISTER_OGN_NODE()

} // namespace tutorial
} // namespace core
} // namespace graph
} // namespace omni