Tutorial 17 - Python State Attributes Node
This node illustrates how you can use state attributes. These are attributes that are not meant to be connected to other nodes as they maintain a node’s internal state, persistent from one evaluation to the next.
As they are persistent, care must be taken that they be initialized properly. This can take the form of a reset flag, as seen on this node, state flag values with known defaults that describe the validity of the state attribute data, or using a checksum on inputs, among other possibilities.
State attributes can be both read and written, like output attributes. The presence of state attributes will also inform the evaluators on what type of parallel scheduling is appropriate.
These attributes provide a similar functionality to those found in Tutorial 13 - Python State Node, except that being node attributes the structure is visible to the outside world, making it easier to construct UI and visualizers for it.
OgnTutorialStateAttributesPy.ogn
The .ogn file containing the implementation of a node named “omni.graph.tutorials.StateAttributesPy”, with a couple of state attributes that both read and write values during the compute.
1{
2 "StateAttributesPy" : {
3 "version": 1,
4 "categories": "tutorials",
5 "description": [
6 "This is a tutorial node. It exercises state attributes to remember data from one execution to the next."
7 ],
8 "language": "python",
9 "metadata":
10 {
11 "uiName": "Tutorial Python Node: State Attributes"
12 },
13 "inputs": { "ignored": { "type": "bool", "description": "Ignore me" } },
14 "state": {
15 "reset": {
16 "type": "bool",
17 "description": [
18 "If true then the inputs are ignored and outputs are set to default values, then this",
19 "flag is set to false for subsequent executions."
20 ],
21 "default": true
22 },
23 "monotonic": {
24 "type": "int",
25 "description": "The monotonically increasing output value, reset to 0 when the reset value is true"
26 }
27 },
28 "$externalTest": [
29 "This test infrastructure does not yet support multiple successive executions, so",
30 "an external test script called 'test_tutorial_state_attributes_py.py' is used for that case.",
31 "This test illustrates how a state value can be initialized before the test runs and checked after",
32 "the test runs by using the _get and _set suffix on the state namespace. Having no suffix defaults to",
33 "_get, where the expected state value is checked after running."
34 ],
35 "tests": [
36 { "state_set:monotonic": 7, "state_get:reset": false, "state:monotonic": 8 }
37 ]
38 }
39}
OgnTutorialStateAttributesPy.py
The .py file contains the compute method that uses the state attributes to run the algorithm.
1"""
2Implementation of a Python node that uses state attributes to maintain information between evaluations.
3
4The node has a simple monotonically increasing state output "monotonic" that can be reset by setting the state
5attribute "reset" to true.
6"""
7
8
9class OgnTutorialStateAttributesPy:
10 """Use internal node state information in addition to inputs"""
11
12 @staticmethod
13 def compute(db) -> bool:
14 """Compute the output based on inputs and internal state"""
15
16 # State attributes are the only ones guaranteed to remember and modify their values.
17 # Care must be taken to set proper defaults when the node initializes, otherwise you could be
18 # starting in an unknown state.
19 if db.state.reset:
20 db.state.monotonic = 0
21 db.state.reset = False
22 else:
23 db.state.monotonic = db.state.monotonic + 1
24
25 return True
Test Script
The .ogn test infrastructure currently only supports single evaluation, which will not be sufficient to test state attribute manipulations. This test script runs multiple evaluations and verifies that the state information is updated as expected after each evaluation.
1"""
2Tests for the omnigraph.tutorial.stateAttributesPy node
3"""
4
5import omni.graph.core as og
6import omni.graph.core.tests as ogts
7
8
9class TestTutorialStateAttributesPy(ogts.OmniGraphTestCase):
10 """.ogn tests only run once while state tests require multiple evaluations, handled here"""
11
12 # ----------------------------------------------------------------------
13 async def test_tutorial_state_attributes_py_node(self):
14 """Test basic operation of the Python tutorial node containing state attributes"""
15 keys = og.Controller.Keys
16 (_, [state_node], _, _) = og.Controller.edit(
17 "/TestGraph",
18 {
19 keys.CREATE_NODES: ("StateNode", "omni.graph.tutorials.StateAttributesPy"),
20 keys.SET_VALUES: ("StateNode.state:reset", True),
21 },
22 )
23 await og.Controller.evaluate()
24 reset_attr = og.Controller.attribute("state:reset", state_node)
25 monotonic_attr = og.Controller.attribute("state:monotonic", state_node)
26
27 self.assertEqual(og.Controller.get(reset_attr), False, "Reset attribute set back to False")
28 self.assertEqual(og.Controller.get(monotonic_attr), 0, "Monotonic attribute reset to start")
29
30 await og.Controller.evaluate()
31 self.assertEqual(og.Controller.get(reset_attr), False, "Reset attribute still False")
32 self.assertEqual(og.Controller.get(monotonic_attr), 1, "Monotonic attribute incremented once")
33
34 await og.Controller.evaluate()
35 self.assertEqual(og.Controller.get(monotonic_attr), 2, "Monotonic attribute incremented twice")
36
37 og.Controller.set(reset_attr, True)
38 await og.Controller.evaluate()
39 self.assertEqual(og.Controller.get(reset_attr), False, "Reset again set back to False")
40 self.assertEqual(og.Controller.get(monotonic_attr), 0, "Monotonic attribute again reset to start")