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 favorite 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 "OverrideType" : {
3 "version": 1,
4 "categories": "tutorials",
5 "scheduling": ["threadsafe"],
6 "description": [
7 "This is a tutorial node. It has an input and output of type float[3], an input and output of type",
8 "double[3], and a type override specification that lets the node use Carbonite types for the generated",
9 "data on the float[3] attributes only. Ordinarily all of the types would be defined in a separate",
10 "configuration file so that it can be shared for a project. In that case the type definition build flag",
11 "would also be used so that this information does not have to be embedded in every .ogn file in",
12 "the project. It is placed directly in the file here solely for instructional purposes.",
13 " The compute is just a rotation of components from x->y, y->z, and z->x, for each input type."
14 ],
15 "uiName": "Tutorial Node: Overriding C++ Data Types",
16 "inputs": {
17 "typedData": {
18 "type": "float[3]",
19 "uiName": "Input value with a modified float type",
20 "description": "The value to rotate"
21 },
22 "data": {
23 "type": "double[3]",
24 "uiName": "Input value with a standard double type",
25 "description": "The value to rotate"
26 }
27 },
28 "outputs": {
29 "typedData": {
30 "type": "float[3]",
31 "uiName": "Output value with a modified float type",
32 "description": "The rotated version of inputs::typedData"
33 },
34 "data": {
35 "type": "double[3]",
36 "uiName": "Output value with a standard double type",
37 "description": "The rotated version of inputs::data"
38 }
39 },
40 "$typeDefinitionDescription": "This redefines the generated output type for the float[3] type only.",
41 "typeDefinitions": {
42 "c++": {
43 "float[3]": ["carb::Float3", ["carb/Types.h"]]
44 }
45 },
46 "tests": [
47 {
48 "inputs:data": [1.0, 2.0, 3.0],
49 "outputs:data": [2.0, 3.0, 1.0]
50 }
51 ]
52 }
53}
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// SPDX-FileCopyrightText: Copyright (c) 2021-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: LicenseRef-NvidiaProprietary
3//
4// NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
5// property and proprietary rights in and to this material, related
6// documentation and any modifications thereto. Any use, reproduction,
7// disclosure or distribution of this material and related documentation
8// without an express license agreement from NVIDIA CORPORATION or
9// its affiliates is strictly prohibited.
10#include <OgnTutorialOverrideTypeDatabase.h>
11
12// The include files required by the type definition override is in the database definition so it does not have to
13// be directly included here.
14
15namespace omni
16{
17namespace graph
18{
19namespace core
20{
21namespace tutorial
22{
23
24class OgnTutorialOverrideType
25{
26public:
27 static bool compute(OgnTutorialOverrideTypeDatabase& db)
28 {
29 // Usually you would use an "auto" declaration. The type is explicit here to show how the override has
30 // changed the generated type.
31 const carb::Float3& inputTypedData = db.inputs.typedData();
32 carb::Float3& outputTypedData = db.outputs.typedData();
33
34 const pxr::GfVec3d& inputStandardData = db.inputs.data();
35 pxr::GfVec3d& outputStandardData = db.outputs.data();
36
37 // Rearrange the components as a simple way to verify that compute happened
38 outputTypedData.x = inputTypedData.y;
39 outputTypedData.y = inputTypedData.z;
40 outputTypedData.z = inputTypedData.x;
41
42 outputStandardData.Set(inputStandardData[1], inputStandardData[2], inputStandardData[0]);
43
44 return true;
45 }
46};
47
48// namespaces are closed after the registration macro, to ensure the correct class is registered
49REGISTER_OGN_NODE()
50
51} // namespace tutorial
52} // namespace core
53} // namespace graph
54} // namespace omni