Tutorial 3 - ABI Override Node#
Although the .ogn format creates an easy-to-use interface to the ABI of the OmniGraph node and the associated data model, there may be cases where you want to override the ABI to perform special processing.
OgnTutorialABI.ogn#
The ogn file shows the implementation of a node named “omni.graph.tutorials.Abi”, in its first version, with a simple description. The single attribute serves mostly to provide a framework for the ABI discussion.
1{
2 "Abi" : {
3 "$comment": [
4 "Any key of a key/value pair that starts with a dollar sign, will be ignored by the parser.",
5 "Its values can be anything; a number, string, list, or object. Since JSON does not have a",
6 "mechanism for adding comments you can use this method instead."
7 ],
8 "version": 1,
9 "categories": ["tutorials", { "internal:abi": "Internal nodes that override the ABI functions" }],
10 "exclude": ["python"],
11 "description": ["This tutorial node shows how to override ABI methods on your node."],
12 "metadata": {
13 "$comment": "Metadata is key/value pairs associated with the node type.",
14 "$specialNames": "Kit may recognize specific keys. 'uiName' is a human readable version of the node name",
15 "uiName": "Tutorial Node: ABI Overrides"
16 },
17 "inputs": {
18 "$comment": "Namespaces inside inputs or outputs are possible, similar to USD namespacing",
19 "namespace:a_bool": {
20 "type": "bool",
21 "description": ["The input is any boolean value"],
22 "default": true
23 }
24 },
25 "outputs": {
26 "namespace:a_bool": {
27 "type": "bool",
28 "description": ["The output is computed as the negation of the input"],
29 "default": true
30 }
31 },
32 "tests": [ {"inputs:namespace:a_bool": true, "outputs:namespace:a_bool": false} ]
33 }
34}
OgnTutorialABI.cpp#
The cpp file contains the implementation of the node class with every possible ABI method replaced with customized processing. The node still functions the same as any other node, although it is forced to write a lot of extra boilerplate code to do so.
1// SPDX-FileCopyrightText: Copyright (c) 2020-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 <OgnTutorialABIDatabase.h>
11
12#include <string>
13#include <unordered_map>
14
15// Set this value true to enable messages when the ABI overrides are called
16const bool debug_abi{ true };
17#define DEBUG_ABI \
18 if (debug_abi) \
19 CARB_LOG_INFO
20
21// In most cases the generated compute method is all that a node will need to implement. If for
22// some reason the node wants to access the omni::graph::core::INodeType ABI directly and override
23// any of its behavior it can. This set of functions shows how every method in the ABI might be
24// overridden.
25//
26// Note that certain ABI functions, though listed, are not implemented as a proper implementation would be more
27// complicated than warranted for this simple example. All are rarely overridden; in fact no known cases exist.
28class OgnTutorialABI
29{
30 static std::unordered_map<std::string, std::string> s_metadata; // Alternative metadata implementation
31public:
32 // ----------------------------------------------------------------------
33 // Almost always overridden indirectly
34 //
35 // ABI compute method takes the interface definitions of both the evaluation context and the node.
36 // When using a .ogn generated class this function will be overridden by the generated code and the
37 // node will implement the generated version of this method "bool compute(OgnTutorialABIDatabase&)".
38 // Overriding this method directly, while possible, does not add any extra capabilities as the two
39 // parameters are also available through the database (contextObj = db.abi_context(), nodeObj = db.abi_node())
40 static bool compute(const GraphContextObj& contextObj, const NodeObj& nodeObj)
41 {
42 DEBUG_ABI("Computing the ABI node");
43
44 // The computation still runs the same as for the standard compute method by using the more
45 // complex ABI-based access patterns. First get the ABI-compliant structures needed.
46 NodeContextHandle nodeHandle = nodeObj.nodeContextHandle;
47
48 // Use the multiple-input template to access a pointer to the input boolean value
49 auto inputValueAttr = getAttributesR<ConstAttributeDataHandle>(
50 contextObj, nodeHandle, std::make_tuple(NameToken("inputs:namespace:a_bool")), kAccordingToContextIndex);
51 const bool* inputValue{ nullptr };
52 std::tie(inputValue) = getDataR<bool*>(contextObj, inputValueAttr);
53
54 // Use the single-output template to access a pointer to the output boolean value
55 auto outputValueAttr =
56 getAttributeW(contextObj, nodeHandle, NameToken("outputs:namespace:a_bool"), kAccordingToContextIndex);
57 bool* outputValue = getDataW<bool>(contextObj, outputValueAttr);
58
59 // Since the generated code isn't in control you are responsible for your own error checking
60 if (!inputValue)
61 {
62 // LCOV_EXCL_START : This tags a section of code for removal from the code coverage runs because
63 // the condition it checks cannot be easily reproduced in normal operation.
64 // Not having access to the generated database class with its error interface we have to resort
65 // to using the ABI for error logging.
66 nodeObj.iNode->logComputeMessageOnInstance(nodeObj, kAccordingToContextIndex,
67 omni::graph::core::ogn::Severity::eError,
68 "Failed compute: No input attribute");
69 return false;
70 // LCOV_EXCL_STOP
71 }
72 if (!outputValue)
73 {
74 // LCOV_EXCL_START
75 nodeObj.iNode->logComputeMessageOnInstance(nodeObj, kAccordingToContextIndex,
76 omni::graph::core::ogn::Severity::eError,
77 "Failed compute: No output attribute");
78 return false;
79 // LCOV_EXCL_STOP
80 }
81
82 // The actual computation of the node
83 *outputValue = !*inputValue;
84
85 return true;
86 }
87
88 // ----------------------------------------------------------------------
89 // Rarely overridden
90 //
91 // These will almost never be overridden, specifically because the low level implementation lives in
92 // the omni.graph.core extension and is not exposed through the ABI so it would be very difficult
93 // for a node to be able to do the right thing when this function is called.
94 // static void addInput
95 // static void addOutput
96 // static void addState
97
98 // ----------------------------------------------------------------------
99 // Rarely overridden
100 //
101 // This should almost never be overridden as the auto-generated code will handle the name.
102 // This particular override is used to bypass the unique naming feature of the .ogn name, where only names
103 // with a namespace separator (".") do not have the extension name added as a prefix to the unique name.
104 // This should only done for backward compatibility, and only until the node type name versioning is available.
105 // Note that when you do this you will still be able to create a node using the generated node type name, as
106 // that is required in order for the automated testing to work. The name returned here can be thought of as an
107 // alias for the underlying name.
108 static const char* getNodeType()
109 {
110 DEBUG_ABI("ABI override of getNodeType");
111 static const char* _nodeType{ "OmniGraphABI" };
112 return _nodeType;
113 }
114
115 // ----------------------------------------------------------------------
116 // Occasionally overridden
117 //
118 // When a node is created this will be called
119 static void initialize(const GraphContextObj&, const NodeObj&)
120 {
121 DEBUG_ABI("ABI override of initialize");
122 // There is no default behavior on initialize so nothing else is needed for this tutorial to function
123 }
124
125 // ----------------------------------------------------------------------
126 // Rarely overridden
127 //
128 // This method might be overridden to set up initial conditions when a node type is registered, or
129 // to replace initialization if the auto-generated version has some problem.
130 static void initializeType(const NodeTypeObj&)
131 {
132 DEBUG_ABI("ABI override of initializeType");
133 // The generated initializeType will always be called so nothing needs to happen here
134 }
135
136 // ----------------------------------------------------------------------
137 // Occasionally overridden
138 //
139 // This is called while registering a node type. It is used to initialize tasks that can be used for
140 // making compute more efficient by using Realm events.
141 static void registerTasks()
142 {
143 DEBUG_ABI("ABI override of registerTasks");
144 }
145
146 // ----------------------------------------------------------------------
147 // Occasionally overridden
148 //
149 // After a node is removed it will get a release call where anything set up in initialize() can be torn down
150 static void release(const NodeObj&)
151 {
152 DEBUG_ABI("ABI override of release");
153 // There is no default behavior on release so nothing else is needed for this tutorial to function
154 }
155
156 // ----------------------------------------------------------------------
157 // Occasionally overridden
158 //
159 // This is something you do want to override when you have more than version of your node.
160 // In it you would translate attribute information from older versions into the current one.
161 static bool updateNodeVersion(const GraphContextObj&, const NodeObj&, int oldVersion, int newVersion)
162 {
163 DEBUG_ABI("ABI override of updateNodeVersion from %d to %d", oldVersion, newVersion);
164 // There is no default behavior on updateNodeVersion so nothing else is needed for this tutorial to function
165 return oldVersion < newVersion;
166 }
167
168 // ----------------------------------------------------------------------
169 // Occasionally overridden
170 //
171 // When there is a connection change to this node which results in an extended type attribute being automatically
172 // resolved, this callback gives the node a change to resolve other extended type attributes. For example a generic
173 // 'Increment' node can resolve its output to an int only after its input has been resolved to an int. Attribute
174 // types are resolved using IAttribute::setResolvedType() or the utility functions such as
175 // INode::resolvePartiallyCoupledAttributes()
176 static void onConnectionTypeResolve(const NodeObj& nodeObj)
177 {
178 DEBUG_ABI("ABI override of onConnectionTypeResolve()");
179 // There is no default behavior on onConnectionTypeResolve so nothing else is needed for this tutorial to
180 // function
181 }
182
183 // ----------------------------------------------------------------------
184 // Rarely overridden
185 //
186 // You may want to provide an alternative method of metadata storage.
187 // This method can be used to intercept requests for metadata to provide those alternatives.
188 static size_t getAllMetadata(const NodeTypeObj& nodeType, const char** keyBuf, const char** valueBuf, size_t bufSize)
189 {
190 DEBUG_ABI("ABI override of getAllMetadata(%zu)", bufSize);
191 if (s_metadata.size() > bufSize)
192 {
193 CARB_LOG_ERROR("Not enough space for metadata - needed %zu, got %zu", s_metadata.size(), bufSize);
194 return 0;
195 }
196 size_t index = 0;
197 for (auto& metadata : s_metadata)
198 {
199 keyBuf[index] = metadata.first.c_str();
200 valueBuf[index] = metadata.second.c_str();
201 ++index;
202 }
203 return s_metadata.size();
204 }
205
206 // ----------------------------------------------------------------------
207 // Rarely overridden
208 //
209 // You may want to provide an alternative method of metadata storage.
210 // This method can be used to intercept requests for metadata to provide those alternatives.
211 static const char* getMetadata(const NodeTypeObj& nodeType, const char* key)
212 {
213 DEBUG_ABI("ABI override of getMetadata('%s')", key);
214 auto found = s_metadata.find(std::string(key));
215 if (found != s_metadata.end())
216 {
217 return (*found).second.c_str();
218 }
219 return nullptr;
220 }
221
222 // ----------------------------------------------------------------------
223 // Rarely overridden
224 //
225 // You may want to provide an alternative method of metadata storage.
226 // This method can be used to intercept requests for the number of metadata elements to provide those alternatives.
227 static size_t getMetadataCount(const NodeTypeObj& nodeType)
228 {
229 DEBUG_ABI("ABI override of getMetadataCount()");
230 return s_metadata.size();
231 }
232
233 // ----------------------------------------------------------------------
234 // Rarely overridden
235 //
236 // You may want to provide an alternative method of metadata storage.
237 // This method can be used to intercept requests to set metadata and use those alternatives.
238 static void setMetadata(const NodeTypeObj& nodeType, const char* key, const char* value)
239 {
240 DEBUG_ABI("ABI override of setMetadata('%s' = '%s')", key, value);
241 s_metadata[std::string(key)] = std::string(value);
242 }
243
244 // ----------------------------------------------------------------------
245 // Rarely overridden
246 //
247 // Subnode types are used when a single node type is shared among a set of other nodes, e.g. the way that
248 // PythonNode is the node type for all nodes implemented in Python, while the subNodeType is the actual node type.
249 // Overriding these functions let you implement different methods of managing those relationships.
250 //
251 static void addSubNodeType(const NodeTypeObj& nodeType, const char* subNodeTypeName, const NodeTypeObj& subNodeType)
252 {
253 DEBUG_ABI("ABI override of addSubNodeType('%s')", subNodeTypeName);
254 }
255
256 // ----------------------------------------------------------------------
257 // Rarely overridden
258 //
259 // Subnode types are used when a single node type is shared among a set of other nodes, e.g. the way that
260 // PythonNode is the node type for all nodes implemented in Python, while the subNodeType is the actual node type.
261 // Overriding these functions let you implement different methods of managing those relationships.
262 //
263 static NodeTypeObj getSubNodeType(const NodeTypeObj& nodeType, const char* subNodeTypeName)
264 {
265 DEBUG_ABI("ABI override of getSubNodeType('%s')", subNodeTypeName);
266 return NodeTypeObj{ nullptr, kInvalidNodeTypeHandle };
267 }
268
269 // ----------------------------------------------------------------------
270 // Rarely overridden
271 //
272 // Subnode types are used when a single node type is shared among a set of other nodes, e.g. the way that
273 // PythonNode is the node type for all nodes implemented in Python, while the subNodeType is the actual node type.
274 // Overriding these functions let you implement different methods of managing those relationships.
275 //
276 static NodeTypeObj createNodeType(const char* nodeTypeName, int version)
277 {
278 DEBUG_ABI("ABI override of createNodeType('%s')", nodeTypeName);
279 return NodeTypeObj{ nullptr, kInvalidNodeTypeHandle };
280 }
281};
282std::unordered_map<std::string, std::string> OgnTutorialABI::s_metadata;
283
284// ============================================================
285// The magic for recognizing and using the overridden ABI code is in here. The generated interface
286// is still available, as seen in the initializeType implementation, although it's not required.
287REGISTER_OGN_NODE()
Node Type Metadata#
This file introduces the metadata keyword, whose value is a dictionary of key/value pairs associated with the node type that may be extracted using the ABI metadata functions. These are not persisted in any files and so must be set either in the .ogn file or in an override of the initializeType() method in the node definition.
Exclusions#
Note the use of the exclude keyword in the .ogn file. This allows you to prevent generation of any of the default files. In this case, since the ABI is handling everything the Python database will not be able to access the node’s information so it is excluded.