file_path
stringlengths
20
202
content
stringlengths
9
3.85M
size
int64
9
3.85M
lang
stringclasses
9 values
avg_line_length
float64
3.33
100
max_line_length
int64
8
993
alphanum_fraction
float64
0.26
0.93
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/OgnOnStageEventDatabase.py
"""Support for simplified access to data on nodes of type omni.graph.action.OnStageEvent Executes when the specified Stage Event occurs. Stage Events are emitted when certain USD stage-related actions are performed by the system: Saved: USD file saved. Selection Changed: USD Prim selection has changed. Hierarchy Changed: USD stage hierarchy has changed, e.g. a prim is added, deleted or moved. OmniGraph Start Play: OmniGraph started OmniGraph Stop Play: OmniGraph stopped Simulation Start Play: Simulation started Simulation Stop Play: Simulation stopped Animation Start Play: Animation playback has started Animation Stop Play: Animation playback has stopped """ import sys import traceback import omni.graph.core as og import omni.graph.core._omni_graph_core as _og import omni.graph.tools.ogn as ogn class OgnOnStageEventDatabase(og.Database): """Helper class providing simplified access to data on nodes of type omni.graph.action.OnStageEvent Class Members: node: Node being evaluated Attribute Value Properties: Inputs: inputs.eventName inputs.onlyPlayback Outputs: outputs.execOut Predefined Tokens: tokens.Saved tokens.SelectionChanged tokens.HierarchyChanged tokens.OmniGraphStart tokens.OmniGraphStop tokens.SimulationStart tokens.SimulationStop tokens.AnimationStart tokens.AnimationStop """ # Imprint the generator and target ABI versions in the file for JIT generation GENERATOR_VERSION = (1, 41, 3) TARGET_VERSION = (2, 139, 12) # This is an internal object that provides per-class storage of a per-node data dictionary PER_NODE_DATA = {} # This is an internal object that describes unchanging attributes in a generic way # The values in this list are in no particular order, as a per-attribute tuple # Name, Type, ExtendedTypeIndex, UiName, Description, Metadata, # Is_Required, DefaultValue, Is_Deprecated, DeprecationMsg # You should not need to access any of this data directly, use the defined database interfaces INTERFACE = og.Database._get_interface([ ('inputs:eventName', 'token', 0, None, 'The event of interest', {ogn.MetadataKeys.ALLOWED_TOKENS: 'Saved,Selection Changed,Hierarchy Changed,OmniGraph Start Play,OmniGraph Stop Play,Simulation Start Play,Simulation Stop Play,Animation Start Play,Animation Stop Play', 'default': 'Animation Start Play', 'displayGroup': 'parameters', ogn.MetadataKeys.LITERAL_ONLY: '1', ogn.MetadataKeys.ALLOWED_TOKENS_RAW: '{"Saved": "Saved", "SelectionChanged": "Selection Changed", "HierarchyChanged": "Hierarchy Changed", "OmniGraphStart": "OmniGraph Start Play", "OmniGraphStop": "OmniGraph Stop Play", "SimulationStart": "Simulation Start Play", "SimulationStop": "Simulation Stop Play", "AnimationStart": "Animation Start Play", "AnimationStop": "Animation Stop Play"}'}, True, "", False, ''), ('inputs:onlyPlayback', 'bool', 0, 'Only Simulate On Play', 'When true, the node is only computed while Stage is being played.', {ogn.MetadataKeys.LITERAL_ONLY: '1', ogn.MetadataKeys.DEFAULT: 'true'}, True, True, False, ''), ('outputs:execOut', 'execution', 0, None, 'The execution output', {}, True, None, False, ''), ]) class tokens: Saved = "Saved" SelectionChanged = "Selection Changed" HierarchyChanged = "Hierarchy Changed" OmniGraphStart = "OmniGraph Start Play" OmniGraphStop = "OmniGraph Stop Play" SimulationStart = "Simulation Start Play" SimulationStop = "Simulation Stop Play" AnimationStart = "Animation Start Play" AnimationStop = "Animation Stop Play" @classmethod def _populate_role_data(cls): """Populate a role structure with the non-default roles on this node type""" role_data = super()._populate_role_data() role_data.outputs.execOut = og.AttributeRole.EXECUTION return role_data class ValuesForInputs(og.DynamicAttributeAccess): LOCAL_PROPERTY_NAMES = {"eventName", "onlyPlayback", "_setting_locked", "_batchedReadAttributes", "_batchedReadValues"} """Helper class that creates natural hierarchical access to input attributes""" def __init__(self, node: og.Node, attributes, dynamic_attributes: og.DynamicAttributeInterface): """Initialize simplified access for the attribute data""" context = node.get_graph().get_default_graph_context() super().__init__(context, node, attributes, dynamic_attributes) self._batchedReadAttributes = [self._attributes.eventName, self._attributes.onlyPlayback] self._batchedReadValues = ["", True] @property def eventName(self): return self._batchedReadValues[0] @eventName.setter def eventName(self, value): self._batchedReadValues[0] = value @property def onlyPlayback(self): return self._batchedReadValues[1] @onlyPlayback.setter def onlyPlayback(self, value): self._batchedReadValues[1] = value def __getattr__(self, item: str): if item in self.LOCAL_PROPERTY_NAMES: return object.__getattribute__(self, item) else: return super().__getattr__(item) def __setattr__(self, item: str, new_value): if item in self.LOCAL_PROPERTY_NAMES: object.__setattr__(self, item, new_value) else: super().__setattr__(item, new_value) def _prefetch(self): readAttributes = self._batchedReadAttributes newValues = _og._prefetch_input_attributes_data(readAttributes) if len(readAttributes) == len(newValues): self._batchedReadValues = newValues class ValuesForOutputs(og.DynamicAttributeAccess): LOCAL_PROPERTY_NAMES = {"execOut", "_batchedWriteValues"} """Helper class that creates natural hierarchical access to output attributes""" def __init__(self, node: og.Node, attributes, dynamic_attributes: og.DynamicAttributeInterface): """Initialize simplified access for the attribute data""" context = node.get_graph().get_default_graph_context() super().__init__(context, node, attributes, dynamic_attributes) self._batchedWriteValues = { } @property def execOut(self): value = self._batchedWriteValues.get(self._attributes.execOut) if value: return value else: data_view = og.AttributeValueHelper(self._attributes.execOut) return data_view.get() @execOut.setter def execOut(self, value): self._batchedWriteValues[self._attributes.execOut] = value def __getattr__(self, item: str): if item in self.LOCAL_PROPERTY_NAMES: return object.__getattribute__(self, item) else: return super().__getattr__(item) def __setattr__(self, item: str, new_value): if item in self.LOCAL_PROPERTY_NAMES: object.__setattr__(self, item, new_value) else: super().__setattr__(item, new_value) def _commit(self): _og._commit_output_attributes_data(self._batchedWriteValues) self._batchedWriteValues = { } class ValuesForState(og.DynamicAttributeAccess): """Helper class that creates natural hierarchical access to state attributes""" def __init__(self, node: og.Node, attributes, dynamic_attributes: og.DynamicAttributeInterface): """Initialize simplified access for the attribute data""" context = node.get_graph().get_default_graph_context() super().__init__(context, node, attributes, dynamic_attributes) def __init__(self, node): super().__init__(node) dynamic_attributes = self.dynamic_attribute_data(node, og.AttributePortType.ATTRIBUTE_PORT_TYPE_INPUT) self.inputs = OgnOnStageEventDatabase.ValuesForInputs(node, self.attributes.inputs, dynamic_attributes) dynamic_attributes = self.dynamic_attribute_data(node, og.AttributePortType.ATTRIBUTE_PORT_TYPE_OUTPUT) self.outputs = OgnOnStageEventDatabase.ValuesForOutputs(node, self.attributes.outputs, dynamic_attributes) dynamic_attributes = self.dynamic_attribute_data(node, og.AttributePortType.ATTRIBUTE_PORT_TYPE_STATE) self.state = OgnOnStageEventDatabase.ValuesForState(node, self.attributes.state, dynamic_attributes) class abi: """Class defining the ABI interface for the node type""" @staticmethod def get_node_type(): get_node_type_function = getattr(OgnOnStageEventDatabase.NODE_TYPE_CLASS, 'get_node_type', None) if callable(get_node_type_function): return get_node_type_function() return 'omni.graph.action.OnStageEvent' @staticmethod def compute(context, node): def database_valid(): return True try: per_node_data = OgnOnStageEventDatabase.PER_NODE_DATA[node.node_id()] db = per_node_data.get('_db') if db is None: db = OgnOnStageEventDatabase(node) per_node_data['_db'] = db if not database_valid(): per_node_data['_db'] = None return False except: db = OgnOnStageEventDatabase(node) try: compute_function = getattr(OgnOnStageEventDatabase.NODE_TYPE_CLASS, 'compute', None) if callable(compute_function) and compute_function.__code__.co_argcount > 1: return compute_function(context, node) db.inputs._prefetch() db.inputs._setting_locked = True with og.in_compute(): return OgnOnStageEventDatabase.NODE_TYPE_CLASS.compute(db) except Exception as error: stack_trace = "".join(traceback.format_tb(sys.exc_info()[2].tb_next)) db.log_error(f'Assertion raised in compute - {error}\n{stack_trace}', add_context=False) finally: db.inputs._setting_locked = False db.outputs._commit() return False @staticmethod def initialize(context, node): OgnOnStageEventDatabase._initialize_per_node_data(node) initialize_function = getattr(OgnOnStageEventDatabase.NODE_TYPE_CLASS, 'initialize', None) if callable(initialize_function): initialize_function(context, node) per_node_data = OgnOnStageEventDatabase.PER_NODE_DATA[node.node_id()] def on_connection_or_disconnection(*args): per_node_data['_db'] = None node.register_on_connected_callback(on_connection_or_disconnection) node.register_on_disconnected_callback(on_connection_or_disconnection) @staticmethod def release(node): release_function = getattr(OgnOnStageEventDatabase.NODE_TYPE_CLASS, 'release', None) if callable(release_function): release_function(node) OgnOnStageEventDatabase._release_per_node_data(node) @staticmethod def release_instance(node, target): OgnOnStageEventDatabase._release_per_node_instance_data(node, target) @staticmethod def update_node_version(context, node, old_version, new_version): update_node_version_function = getattr(OgnOnStageEventDatabase.NODE_TYPE_CLASS, 'update_node_version', None) if callable(update_node_version_function): return update_node_version_function(context, node, old_version, new_version) return False @staticmethod def initialize_type(node_type): initialize_type_function = getattr(OgnOnStageEventDatabase.NODE_TYPE_CLASS, 'initialize_type', None) needs_initializing = True if callable(initialize_type_function): needs_initializing = initialize_type_function(node_type) if needs_initializing: node_type.set_metadata(ogn.MetadataKeys.EXTENSION, "omni.graph.action") node_type.set_metadata(ogn.MetadataKeys.UI_NAME, "On Stage Event") node_type.set_metadata(ogn.MetadataKeys.CATEGORIES, "graph:action,event") node_type.set_metadata(ogn.MetadataKeys.DESCRIPTION, "Executes when the specified Stage Event occurs. Stage Events are emitted when certain USD stage-related actions are performed by the system:\n Saved: USD file saved.\n Selection Changed: USD Prim selection has changed.\n Hierarchy Changed: USD stage hierarchy has changed, e.g. a prim is added, deleted or moved.\n OmniGraph Start Play: OmniGraph started\n OmniGraph Stop Play: OmniGraph stopped\n Simulation Start Play: Simulation started\n Simulation Stop Play: Simulation stopped\n Animation Start Play: Animation playback has started\n Animation Stop Play: Animation playback has stopped") node_type.set_metadata(ogn.MetadataKeys.LANGUAGE, "Python") __hints = node_type.get_scheduling_hints() if __hints is not None: __hints.compute_rule = og.eComputeRule.E_ON_REQUEST OgnOnStageEventDatabase.INTERFACE.add_to_node_type(node_type) node_type.set_has_state(True) @staticmethod def on_connection_type_resolve(node): on_connection_type_resolve_function = getattr(OgnOnStageEventDatabase.NODE_TYPE_CLASS, 'on_connection_type_resolve', None) if callable(on_connection_type_resolve_function): on_connection_type_resolve_function(node) NODE_TYPE_CLASS = None @staticmethod def register(node_type_class): OgnOnStageEventDatabase.NODE_TYPE_CLASS = node_type_class og.register_node_type(OgnOnStageEventDatabase.abi, 3) @staticmethod def deregister(): og.deregister_node_type("omni.graph.action.OnStageEvent")
14,352
Python
47.489865
790
0.651965
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/OgnForEachDatabase.py
"""Support for simplified access to data on nodes of type omni.graph.action.ForEach Executes the a loop body once for each element in the input array. The finished output is executed after all iterations are complete """ from typing import Any import carb import omni.graph.core as og import omni.graph.core._omni_graph_core as _og import omni.graph.tools.ogn as ogn class OgnForEachDatabase(og.Database): """Helper class providing simplified access to data on nodes of type omni.graph.action.ForEach Class Members: node: Node being evaluated Attribute Value Properties: Inputs: inputs.arrayIn inputs.execIn Outputs: outputs.arrayIndex outputs.element outputs.finished outputs.loopBody """ # Imprint the generator and target ABI versions in the file for JIT generation GENERATOR_VERSION = (1, 41, 3) TARGET_VERSION = (2, 139, 12) # This is an internal object that provides per-class storage of a per-node data dictionary PER_NODE_DATA = {} # This is an internal object that describes unchanging attributes in a generic way # The values in this list are in no particular order, as a per-attribute tuple # Name, Type, ExtendedTypeIndex, UiName, Description, Metadata, # Is_Required, DefaultValue, Is_Deprecated, DeprecationMsg # You should not need to access any of this data directly, use the defined database interfaces INTERFACE = og.Database._get_interface([ ('inputs:arrayIn', 'bool[],colord[3][],colord[4][],colorf[3][],colorf[4][],colorh[3][],colorh[4][],double[2][],double[3][],double[4][],double[],float[2][],float[3][],float[4][],float[],frame[4][],half[2][],half[3][],half[4][],half[],int64[],int[2][],int[3][],int[4][],int[],matrixd[3][],matrixd[4][],normald[3][],normalf[3][],normalh[3][],pointd[3][],pointf[3][],pointh[3][],quatd[4][],quatf[4][],quath[4][],texcoordd[2][],texcoordd[3][],texcoordf[2][],texcoordf[3][],texcoordh[2][],texcoordh[3][],timecode[],token[],transform[4][],uchar[],uint64[],uint[],vectord[3][],vectorf[3][],vectorh[3][]', 1, 'Input Array', 'The array to loop over', {}, True, None, False, ''), ('inputs:execIn', 'execution', 0, None, 'Input execution', {}, True, None, False, ''), ('outputs:arrayIndex', 'int', 0, None, 'The current or last index visited', {}, True, None, False, ''), ('outputs:element', 'bool,colord[3],colord[4],colorf[3],colorf[4],colorh[3],colorh[4],double,double[2],double[3],double[4],float,float[2],float[3],float[4],frame[4],half,half[2],half[3],half[4],int,int64,int[2],int[3],int[4],matrixd[3],matrixd[4],normald[3],normalf[3],normalh[3],pointd[3],pointf[3],pointh[3],quatd[4],quatf[4],quath[4],texcoordd[2],texcoordd[3],texcoordf[2],texcoordf[3],texcoordh[2],texcoordh[3],timecode,token,transform[4],uchar,uint,uint64,vectord[3],vectorf[3],vectorh[3]', 1, None, 'The current or last element of the array visited', {}, True, None, False, ''), ('outputs:finished', 'execution', 0, None, 'Executed when the loop is finished', {}, True, None, False, ''), ('outputs:loopBody', 'execution', 0, None, 'Executed for each element of the array', {}, True, None, False, ''), ]) @classmethod def _populate_role_data(cls): """Populate a role structure with the non-default roles on this node type""" role_data = super()._populate_role_data() role_data.inputs.execIn = og.AttributeRole.EXECUTION role_data.outputs.finished = og.AttributeRole.EXECUTION role_data.outputs.loopBody = og.AttributeRole.EXECUTION return role_data class ValuesForInputs(og.DynamicAttributeAccess): LOCAL_PROPERTY_NAMES = { } """Helper class that creates natural hierarchical access to input attributes""" def __init__(self, node: og.Node, attributes, dynamic_attributes: og.DynamicAttributeInterface): """Initialize simplified access for the attribute data""" context = node.get_graph().get_default_graph_context() super().__init__(context, node, attributes, dynamic_attributes) self._batchedReadAttributes = [] self._batchedReadValues = [] @property def arrayIn(self) -> og.RuntimeAttribute: """Get the runtime wrapper class for the attribute inputs.arrayIn""" return og.RuntimeAttribute(self._attributes.arrayIn.get_attribute_data(), self._context, True) @arrayIn.setter def arrayIn(self, value_to_set: Any): """Assign another attribute's value to outputs.arrayIn""" if isinstance(value_to_set, og.RuntimeAttribute): self.arrayIn.value = value_to_set.value else: self.arrayIn.value = value_to_set @property def execIn(self): data_view = og.AttributeValueHelper(self._attributes.execIn) return data_view.get() @execIn.setter def execIn(self, value): if self._setting_locked: raise og.ReadOnlyError(self._attributes.execIn) data_view = og.AttributeValueHelper(self._attributes.execIn) data_view.set(value) def _prefetch(self): readAttributes = self._batchedReadAttributes newValues = _og._prefetch_input_attributes_data(readAttributes) if len(readAttributes) == len(newValues): self._batchedReadValues = newValues class ValuesForOutputs(og.DynamicAttributeAccess): LOCAL_PROPERTY_NAMES = { } """Helper class that creates natural hierarchical access to output attributes""" def __init__(self, node: og.Node, attributes, dynamic_attributes: og.DynamicAttributeInterface): """Initialize simplified access for the attribute data""" context = node.get_graph().get_default_graph_context() super().__init__(context, node, attributes, dynamic_attributes) self._batchedWriteValues = { } @property def arrayIndex(self): data_view = og.AttributeValueHelper(self._attributes.arrayIndex) return data_view.get() @arrayIndex.setter def arrayIndex(self, value): data_view = og.AttributeValueHelper(self._attributes.arrayIndex) data_view.set(value) @property def element(self) -> og.RuntimeAttribute: """Get the runtime wrapper class for the attribute outputs.element""" return og.RuntimeAttribute(self._attributes.element.get_attribute_data(), self._context, False) @element.setter def element(self, value_to_set: Any): """Assign another attribute's value to outputs.element""" if isinstance(value_to_set, og.RuntimeAttribute): self.element.value = value_to_set.value else: self.element.value = value_to_set @property def finished(self): data_view = og.AttributeValueHelper(self._attributes.finished) return data_view.get() @finished.setter def finished(self, value): data_view = og.AttributeValueHelper(self._attributes.finished) data_view.set(value) @property def loopBody(self): data_view = og.AttributeValueHelper(self._attributes.loopBody) return data_view.get() @loopBody.setter def loopBody(self, value): data_view = og.AttributeValueHelper(self._attributes.loopBody) data_view.set(value) def _commit(self): _og._commit_output_attributes_data(self._batchedWriteValues) self._batchedWriteValues = { } class ValuesForState(og.DynamicAttributeAccess): """Helper class that creates natural hierarchical access to state attributes""" def __init__(self, node: og.Node, attributes, dynamic_attributes: og.DynamicAttributeInterface): """Initialize simplified access for the attribute data""" context = node.get_graph().get_default_graph_context() super().__init__(context, node, attributes, dynamic_attributes) def __init__(self, node): super().__init__(node) dynamic_attributes = self.dynamic_attribute_data(node, og.AttributePortType.ATTRIBUTE_PORT_TYPE_INPUT) self.inputs = OgnForEachDatabase.ValuesForInputs(node, self.attributes.inputs, dynamic_attributes) dynamic_attributes = self.dynamic_attribute_data(node, og.AttributePortType.ATTRIBUTE_PORT_TYPE_OUTPUT) self.outputs = OgnForEachDatabase.ValuesForOutputs(node, self.attributes.outputs, dynamic_attributes) dynamic_attributes = self.dynamic_attribute_data(node, og.AttributePortType.ATTRIBUTE_PORT_TYPE_STATE) self.state = OgnForEachDatabase.ValuesForState(node, self.attributes.state, dynamic_attributes)
8,947
Python
50.131428
676
0.65005
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/OgnOnTickDatabase.py
"""Support for simplified access to data on nodes of type omni.graph.action.OnTick Executes an output execution pulse at a regular multiple of the refresh rate """ import omni.graph.core as og import omni.graph.core._omni_graph_core as _og import omni.graph.tools.ogn as ogn class OgnOnTickDatabase(og.Database): """Helper class providing simplified access to data on nodes of type omni.graph.action.OnTick Class Members: node: Node being evaluated Attribute Value Properties: Inputs: inputs.framePeriod inputs.onlyPlayback Outputs: outputs.absoluteSimTime outputs.deltaSeconds outputs.frame outputs.isPlaying outputs.tick outputs.time outputs.timeSinceStart State: state.accumulatedSeconds state.frameCount """ # Imprint the generator and target ABI versions in the file for JIT generation GENERATOR_VERSION = (1, 41, 3) TARGET_VERSION = (2, 139, 12) # This is an internal object that provides per-class storage of a per-node data dictionary PER_NODE_DATA = {} # This is an internal object that describes unchanging attributes in a generic way # The values in this list are in no particular order, as a per-attribute tuple # Name, Type, ExtendedTypeIndex, UiName, Description, Metadata, # Is_Required, DefaultValue, Is_Deprecated, DeprecationMsg # You should not need to access any of this data directly, use the defined database interfaces INTERFACE = og.Database._get_interface([ ('inputs:framePeriod', 'uint', 0, 'Update Period (Ticks)', 'The number of updates between each output pulse.\nThe default 0 means every update, 1 means every other update.\nThis can be used to rate-limit updates.', {ogn.MetadataKeys.LITERAL_ONLY: '1', ogn.MetadataKeys.DEFAULT: '0'}, True, 0, False, ''), ('inputs:onlyPlayback', 'bool', 0, 'Only Simulate On Play', 'When true, the node is only computed while Stage is being played.', {ogn.MetadataKeys.LITERAL_ONLY: '1', ogn.MetadataKeys.DEFAULT: 'true'}, True, True, False, ''), ('outputs:absoluteSimTime', 'double', 0, 'Absolute Simulation Time (Seconds)', 'The accumulated total of elapsed times between rendered frames', {}, True, None, False, ''), ('outputs:deltaSeconds', 'double', 0, 'Delta (Seconds)', 'The number of seconds elapsed since the last output pulse', {}, True, None, False, ''), ('outputs:frame', 'double', 0, 'Animation Time (Frames)', 'The global animation time in frames, equivalent to (time * fps), during playback', {}, True, None, False, ''), ('outputs:isPlaying', 'bool', 0, 'Is Playing', 'True during global animation timeline playback', {}, True, None, False, ''), ('outputs:tick', 'execution', 0, 'Tick', 'The execution output', {}, True, None, False, ''), ('outputs:time', 'double', 0, 'Animation Time (Seconds)', 'The global animation time in seconds during playback', {}, True, None, False, ''), ('outputs:timeSinceStart', 'double', 0, 'Time Since Start (Seconds)', 'Elapsed time since the App started', {}, True, None, False, ''), ('state:accumulatedSeconds', 'double', 0, None, 'Accumulated time since the last output pulse', {ogn.MetadataKeys.DEFAULT: '0'}, True, 0, False, ''), ('state:frameCount', 'uint', 0, None, 'Accumulated frames since the last output pulse', {ogn.MetadataKeys.DEFAULT: '0'}, True, 0, False, ''), ]) @classmethod def _populate_role_data(cls): """Populate a role structure with the non-default roles on this node type""" role_data = super()._populate_role_data() role_data.outputs.tick = og.AttributeRole.EXECUTION return role_data class ValuesForInputs(og.DynamicAttributeAccess): LOCAL_PROPERTY_NAMES = { } """Helper class that creates natural hierarchical access to input attributes""" def __init__(self, node: og.Node, attributes, dynamic_attributes: og.DynamicAttributeInterface): """Initialize simplified access for the attribute data""" context = node.get_graph().get_default_graph_context() super().__init__(context, node, attributes, dynamic_attributes) self._batchedReadAttributes = [] self._batchedReadValues = [] @property def framePeriod(self): data_view = og.AttributeValueHelper(self._attributes.framePeriod) return data_view.get() @framePeriod.setter def framePeriod(self, value): if self._setting_locked: raise og.ReadOnlyError(self._attributes.framePeriod) data_view = og.AttributeValueHelper(self._attributes.framePeriod) data_view.set(value) @property def onlyPlayback(self): data_view = og.AttributeValueHelper(self._attributes.onlyPlayback) return data_view.get() @onlyPlayback.setter def onlyPlayback(self, value): if self._setting_locked: raise og.ReadOnlyError(self._attributes.onlyPlayback) data_view = og.AttributeValueHelper(self._attributes.onlyPlayback) data_view.set(value) def _prefetch(self): readAttributes = self._batchedReadAttributes newValues = _og._prefetch_input_attributes_data(readAttributes) if len(readAttributes) == len(newValues): self._batchedReadValues = newValues class ValuesForOutputs(og.DynamicAttributeAccess): LOCAL_PROPERTY_NAMES = { } """Helper class that creates natural hierarchical access to output attributes""" def __init__(self, node: og.Node, attributes, dynamic_attributes: og.DynamicAttributeInterface): """Initialize simplified access for the attribute data""" context = node.get_graph().get_default_graph_context() super().__init__(context, node, attributes, dynamic_attributes) self._batchedWriteValues = { } @property def absoluteSimTime(self): data_view = og.AttributeValueHelper(self._attributes.absoluteSimTime) return data_view.get() @absoluteSimTime.setter def absoluteSimTime(self, value): data_view = og.AttributeValueHelper(self._attributes.absoluteSimTime) data_view.set(value) @property def deltaSeconds(self): data_view = og.AttributeValueHelper(self._attributes.deltaSeconds) return data_view.get() @deltaSeconds.setter def deltaSeconds(self, value): data_view = og.AttributeValueHelper(self._attributes.deltaSeconds) data_view.set(value) @property def frame(self): data_view = og.AttributeValueHelper(self._attributes.frame) return data_view.get() @frame.setter def frame(self, value): data_view = og.AttributeValueHelper(self._attributes.frame) data_view.set(value) @property def isPlaying(self): data_view = og.AttributeValueHelper(self._attributes.isPlaying) return data_view.get() @isPlaying.setter def isPlaying(self, value): data_view = og.AttributeValueHelper(self._attributes.isPlaying) data_view.set(value) @property def tick(self): data_view = og.AttributeValueHelper(self._attributes.tick) return data_view.get() @tick.setter def tick(self, value): data_view = og.AttributeValueHelper(self._attributes.tick) data_view.set(value) @property def time(self): data_view = og.AttributeValueHelper(self._attributes.time) return data_view.get() @time.setter def time(self, value): data_view = og.AttributeValueHelper(self._attributes.time) data_view.set(value) @property def timeSinceStart(self): data_view = og.AttributeValueHelper(self._attributes.timeSinceStart) return data_view.get() @timeSinceStart.setter def timeSinceStart(self, value): data_view = og.AttributeValueHelper(self._attributes.timeSinceStart) data_view.set(value) def _commit(self): _og._commit_output_attributes_data(self._batchedWriteValues) self._batchedWriteValues = { } class ValuesForState(og.DynamicAttributeAccess): """Helper class that creates natural hierarchical access to state attributes""" def __init__(self, node: og.Node, attributes, dynamic_attributes: og.DynamicAttributeInterface): """Initialize simplified access for the attribute data""" context = node.get_graph().get_default_graph_context() super().__init__(context, node, attributes, dynamic_attributes) @property def accumulatedSeconds(self): data_view = og.AttributeValueHelper(self._attributes.accumulatedSeconds) return data_view.get() @accumulatedSeconds.setter def accumulatedSeconds(self, value): data_view = og.AttributeValueHelper(self._attributes.accumulatedSeconds) data_view.set(value) @property def frameCount(self): data_view = og.AttributeValueHelper(self._attributes.frameCount) return data_view.get() @frameCount.setter def frameCount(self, value): data_view = og.AttributeValueHelper(self._attributes.frameCount) data_view.set(value) def __init__(self, node): super().__init__(node) dynamic_attributes = self.dynamic_attribute_data(node, og.AttributePortType.ATTRIBUTE_PORT_TYPE_INPUT) self.inputs = OgnOnTickDatabase.ValuesForInputs(node, self.attributes.inputs, dynamic_attributes) dynamic_attributes = self.dynamic_attribute_data(node, og.AttributePortType.ATTRIBUTE_PORT_TYPE_OUTPUT) self.outputs = OgnOnTickDatabase.ValuesForOutputs(node, self.attributes.outputs, dynamic_attributes) dynamic_attributes = self.dynamic_attribute_data(node, og.AttributePortType.ATTRIBUTE_PORT_TYPE_STATE) self.state = OgnOnTickDatabase.ValuesForState(node, self.attributes.state, dynamic_attributes)
10,439
Python
44.991189
312
0.648051
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/OgnForLoopDatabase.py
"""Support for simplified access to data on nodes of type omni.graph.action.ForLoop Executes the a loop body once for each value within a range. When step is positive, the values in the range are determined by the formula: r[i] = start + step*i, i >= 0 & r[i] < stop. When step is negative the constraint is instead r[i] > stop. A step of zero is an error. The break input can be used to break out of the loop before the last index. The finished output is executed after all iterations are complete, or when the loop was broken """ import carb import omni.graph.core as og import omni.graph.core._omni_graph_core as _og import omni.graph.tools.ogn as ogn class OgnForLoopDatabase(og.Database): """Helper class providing simplified access to data on nodes of type omni.graph.action.ForLoop Class Members: node: Node being evaluated Attribute Value Properties: Inputs: inputs.breakLoop inputs.execIn inputs.start inputs.step inputs.stop Outputs: outputs.finished outputs.index outputs.loopBody outputs.value State: state.i """ # Imprint the generator and target ABI versions in the file for JIT generation GENERATOR_VERSION = (1, 41, 3) TARGET_VERSION = (2, 139, 12) # This is an internal object that provides per-class storage of a per-node data dictionary PER_NODE_DATA = {} # This is an internal object that describes unchanging attributes in a generic way # The values in this list are in no particular order, as a per-attribute tuple # Name, Type, ExtendedTypeIndex, UiName, Description, Metadata, # Is_Required, DefaultValue, Is_Deprecated, DeprecationMsg # You should not need to access any of this data directly, use the defined database interfaces INTERFACE = og.Database._get_interface([ ('inputs:breakLoop', 'execution', 0, 'Break', 'Breaks out of the loop when the loop body traversal reaches it', {}, True, None, False, ''), ('inputs:execIn', 'execution', 0, 'In', 'Input execution', {}, True, None, False, ''), ('inputs:start', 'int', 0, 'Start', 'The first value in the range', {}, True, 0, False, ''), ('inputs:step', 'int', 0, 'Step', 'The step size of the range', {ogn.MetadataKeys.DEFAULT: '1'}, True, 1, False, ''), ('inputs:stop', 'int', 0, 'Stop', 'The limiting value of the range', {}, True, 0, False, ''), ('outputs:finished', 'execution', 0, None, 'Executed when the loop is finished', {}, True, None, False, ''), ('outputs:index', 'int', 0, None, 'The current or last index within the range', {}, True, None, False, ''), ('outputs:loopBody', 'execution', 0, None, 'Executed for each index in the range', {}, True, None, False, ''), ('outputs:value', 'int', 0, None, 'The current or last value of the range', {}, True, None, False, ''), ('state:i', 'int', 0, None, 'The next position in the range or -1 when loop is not active', {ogn.MetadataKeys.DEFAULT: '-1'}, True, -1, False, ''), ]) @classmethod def _populate_role_data(cls): """Populate a role structure with the non-default roles on this node type""" role_data = super()._populate_role_data() role_data.inputs.breakLoop = og.AttributeRole.EXECUTION role_data.inputs.execIn = og.AttributeRole.EXECUTION role_data.outputs.finished = og.AttributeRole.EXECUTION role_data.outputs.loopBody = og.AttributeRole.EXECUTION return role_data class ValuesForInputs(og.DynamicAttributeAccess): LOCAL_PROPERTY_NAMES = { } """Helper class that creates natural hierarchical access to input attributes""" def __init__(self, node: og.Node, attributes, dynamic_attributes: og.DynamicAttributeInterface): """Initialize simplified access for the attribute data""" context = node.get_graph().get_default_graph_context() super().__init__(context, node, attributes, dynamic_attributes) self._batchedReadAttributes = [] self._batchedReadValues = [] @property def breakLoop(self): data_view = og.AttributeValueHelper(self._attributes.breakLoop) return data_view.get() @breakLoop.setter def breakLoop(self, value): if self._setting_locked: raise og.ReadOnlyError(self._attributes.breakLoop) data_view = og.AttributeValueHelper(self._attributes.breakLoop) data_view.set(value) @property def execIn(self): data_view = og.AttributeValueHelper(self._attributes.execIn) return data_view.get() @execIn.setter def execIn(self, value): if self._setting_locked: raise og.ReadOnlyError(self._attributes.execIn) data_view = og.AttributeValueHelper(self._attributes.execIn) data_view.set(value) @property def start(self): data_view = og.AttributeValueHelper(self._attributes.start) return data_view.get() @start.setter def start(self, value): if self._setting_locked: raise og.ReadOnlyError(self._attributes.start) data_view = og.AttributeValueHelper(self._attributes.start) data_view.set(value) @property def step(self): data_view = og.AttributeValueHelper(self._attributes.step) return data_view.get() @step.setter def step(self, value): if self._setting_locked: raise og.ReadOnlyError(self._attributes.step) data_view = og.AttributeValueHelper(self._attributes.step) data_view.set(value) @property def stop(self): data_view = og.AttributeValueHelper(self._attributes.stop) return data_view.get() @stop.setter def stop(self, value): if self._setting_locked: raise og.ReadOnlyError(self._attributes.stop) data_view = og.AttributeValueHelper(self._attributes.stop) data_view.set(value) def _prefetch(self): readAttributes = self._batchedReadAttributes newValues = _og._prefetch_input_attributes_data(readAttributes) if len(readAttributes) == len(newValues): self._batchedReadValues = newValues class ValuesForOutputs(og.DynamicAttributeAccess): LOCAL_PROPERTY_NAMES = { } """Helper class that creates natural hierarchical access to output attributes""" def __init__(self, node: og.Node, attributes, dynamic_attributes: og.DynamicAttributeInterface): """Initialize simplified access for the attribute data""" context = node.get_graph().get_default_graph_context() super().__init__(context, node, attributes, dynamic_attributes) self._batchedWriteValues = { } @property def finished(self): data_view = og.AttributeValueHelper(self._attributes.finished) return data_view.get() @finished.setter def finished(self, value): data_view = og.AttributeValueHelper(self._attributes.finished) data_view.set(value) @property def index(self): data_view = og.AttributeValueHelper(self._attributes.index) return data_view.get() @index.setter def index(self, value): data_view = og.AttributeValueHelper(self._attributes.index) data_view.set(value) @property def loopBody(self): data_view = og.AttributeValueHelper(self._attributes.loopBody) return data_view.get() @loopBody.setter def loopBody(self, value): data_view = og.AttributeValueHelper(self._attributes.loopBody) data_view.set(value) @property def value(self): data_view = og.AttributeValueHelper(self._attributes.value) return data_view.get() @value.setter def value(self, value): data_view = og.AttributeValueHelper(self._attributes.value) data_view.set(value) def _commit(self): _og._commit_output_attributes_data(self._batchedWriteValues) self._batchedWriteValues = { } class ValuesForState(og.DynamicAttributeAccess): """Helper class that creates natural hierarchical access to state attributes""" def __init__(self, node: og.Node, attributes, dynamic_attributes: og.DynamicAttributeInterface): """Initialize simplified access for the attribute data""" context = node.get_graph().get_default_graph_context() super().__init__(context, node, attributes, dynamic_attributes) @property def i(self): data_view = og.AttributeValueHelper(self._attributes.i) return data_view.get() @i.setter def i(self, value): data_view = og.AttributeValueHelper(self._attributes.i) data_view.set(value) def __init__(self, node): super().__init__(node) dynamic_attributes = self.dynamic_attribute_data(node, og.AttributePortType.ATTRIBUTE_PORT_TYPE_INPUT) self.inputs = OgnForLoopDatabase.ValuesForInputs(node, self.attributes.inputs, dynamic_attributes) dynamic_attributes = self.dynamic_attribute_data(node, og.AttributePortType.ATTRIBUTE_PORT_TYPE_OUTPUT) self.outputs = OgnForLoopDatabase.ValuesForOutputs(node, self.attributes.outputs, dynamic_attributes) dynamic_attributes = self.dynamic_attribute_data(node, og.AttributePortType.ATTRIBUTE_PORT_TYPE_STATE) self.state = OgnForLoopDatabase.ValuesForState(node, self.attributes.state, dynamic_attributes)
9,946
Python
42.060606
155
0.632013
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnOnLoaded.cpp
// Copyright (c) 2022-2023, 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 <OgnOnLoadedDatabase.h> #include <omni/graph/action/IActionGraph.h> namespace omni { namespace graph { namespace action { class OgnOnLoaded { bool m_triggered {false}; public: static bool compute(OgnOnLoadedDatabase& db) { auto& state = db.internalState<OgnOnLoaded>(); if (!state.m_triggered) { state.m_triggered = true; // This node does nothing but trigger downstream when first computed. When the execution evaluator receives // the first tick event it will ensure that OnLoaded nodes are run before any OnTick or other event node. // It's not possible to encapsulate the OnLoaded logic of the evaluator in this node due to the // special requirement that it run before others that are ready. auto iActionGraph = getInterface(); iActionGraph->setExecutionEnabled(outputs::execOut.token(), db.getInstanceIndex()); } return true; } }; REGISTER_OGN_NODE() } // action } // graph } // omni
1,483
C++
31.977777
119
0.699933
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnOnKeyboardInput.cpp
// Copyright (c) 2021-2023, 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 "ActionNodeCommon.h" #include <carb/input/IInput.h> #include <carb/input/InputTypes.h> #include <omni/graph/action/IActionGraph.h> #include <omni/kit/IAppWindow.h> #include <OgnOnKeyboardInputDatabase.h> using namespace carb::input; namespace omni { namespace graph { namespace action { constexpr size_t s_numNames = 106; static constexpr std::array<const char*, s_numNames> s_keyNames = { "Unknown", "Space", "Apostrophe", "Comma", "Minus", "Period", "Slash", "Key0", "Key1", "Key2", "Key3", "Key4", "Key5", "Key6", "Key7", "Key8", "Key9", "Semicolon", "Equal", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "LeftBracket", "Backslash", "RightBracket", "GraveAccent", "Escape", "Tab", "Enter", "Backspace", "Insert", "Del", "Right", "Left", "Down", "Up", "PageUp", "PageDown", "Home", "End", "CapsLock", "ScrollLock", "NumLock", "PrintScreen", "Pause", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "Numpad0", "Numpad1", "Numpad2", "Numpad3", "Numpad4", "Numpad5", "Numpad6", "Numpad7", "Numpad8", "Numpad9", "NumpadDel", "NumpadDivide", "NumpadMultiply", "NumpadSubtract", "NumpadAdd", "NumpadEnter", "NumpadEqual", "LeftShift", "LeftControl", "LeftAlt", "LeftSuper", "RightShift", "RightControl", "RightAlt", "RightSuper", "Menu" }; static std::array<NameToken, s_numNames> s_keyTokens; static std::array<NameToken, s_numNames> s_keyTokensLowercase; class OgnOnKeyboardInput { public: SubscriptionId m_keyEventId; // The subscription to the global input event source (one per authoring node) KeyboardEvent m_keyEvent; // The last key event received exec::unstable::Stamp m_keySetStamp; // The stamp set by the authoring node when the even occurs exec::unstable::SyncStamp m_keySetSyncStamp; // The stamp used by each instance static bool onKeyboardEvent(const InputEvent& e, void* userData) { if (e.deviceType != DeviceType::eKeyboard) { return false; } if (e.keyboardEvent.type != KeyboardEventType::eKeyPress && e.keyboardEvent.type != KeyboardEventType::eKeyRelease) return false; NodeHandle nodeHandle = reinterpret_cast<NodeHandle>(userData); auto iNode = carb::getCachedInterface<omni::graph::core::INode>(); NodeObj nodeObj = iNode->getNodeFromHandle(nodeHandle); // Copy the event data and request the next compute() auto& authoringState = OgnOnKeyboardInputDatabase::sInternalState<OgnOnKeyboardInput>(nodeObj, kAuthoringGraphIndex); authoringState.m_keyEvent = e.keyboardEvent; authoringState.m_keySetStamp.next(); iNode->requestCompute(nodeObj); return true; } static void initialize(const GraphContextObj& context, const NodeObj& nodeObj) { // First time look up all the tokens and their lowercase equivalents [[maybe_unused]] static bool callOnce = ([&context] { size_t i = 0; for (const char* name : s_keyNames) { s_keyTokens[i] = context.iToken->getHandle(name); std::string lower(strlen(name), '\0'); std::transform(name, name + strlen(name), lower.begin(), [](unsigned char c) {return ::tolower(c);}); s_keyTokensLowercase[i] = context.iToken->getHandle(lower.c_str()); ++i; } } (), true); omni::kit::IAppWindow* appWindow = omni::kit::getDefaultAppWindow(); if (!appWindow) { return; } Keyboard* keyboard = appWindow->getKeyboard(); if (!keyboard) { CARB_LOG_ERROR_ONCE("No Keyboard!"); return; } IInput* input = carb::getCachedInterface<IInput>(); if (!input) { CARB_LOG_ERROR_ONCE("No Input!"); return; } auto& authoringState = OgnOnKeyboardInputDatabase::sInternalState<OgnOnKeyboardInput>(nodeObj, kAuthoringGraphIndex); authoringState.m_keyEventId = input->subscribeToInputEvents((carb::input::InputDevice*)keyboard, kEventTypeAll, onKeyboardEvent, reinterpret_cast<void*>(nodeObj.nodeHandle), kSubscriptionOrderDefault); } static void release(const NodeObj& nodeObj) { auto const& authoringState = OgnOnKeyboardInputDatabase::sInternalState<OgnOnKeyboardInput>(nodeObj, kAuthoringGraphIndex); IInput* input = carb::getCachedInterface<IInput>(); if (!input) return; if (authoringState.m_keyEventId > 0) input->unsubscribeToInputEvents(authoringState.m_keyEventId); } static bool compute(OgnOnKeyboardInputDatabase& db) { if (checkNodeDisabledForOnlyPlay(db)) return true; static const NameToken emptyToken = db.stringToToken(""); auto iActionGraph = getInterface(); auto const& authoringState = OgnOnKeyboardInputDatabase::sInternalState<OgnOnKeyboardInput>(db.abi_node(), kAuthoringGraphIndex); auto& localState = db.internalState<OgnOnKeyboardInput>(); if (localState.m_keySetSyncStamp.makeSync(authoringState.m_keySetStamp)) { const NameToken& keyIn = db.inputs.keyIn(); auto& keyEvent = authoringState.m_keyEvent; size_t keyIndex = static_cast<size_t>(keyEvent.key); if (keyIndex >= s_keyTokens.size()) { db.logError("Invalid Key %zu detected", keyIndex); } else { if (keyIn == emptyToken || keyIn == s_keyTokens[keyIndex] || keyIn == s_keyTokensLowercase[keyIndex]) { // If the keyIn is one of the modifiers, the modifier requirement is always satisfied. // For example: LeftShift. We still want to fire pressed execution when LeftShift is pressed // although there is no modifier requirement And for case Shift-LeftShift, by pressing LeftShift, // the modifier requirement has been satisfied naturally bool shiftSatisfied = (keyEvent.key == KeyboardInput::eLeftShift || keyEvent.key == KeyboardInput::eRightShift) ? true : db.inputs.shiftIn() == static_cast<const bool>(keyEvent.modifiers & kKeyboardModifierFlagShift); bool ctrlSatisfied = (keyEvent.key == KeyboardInput::eLeftControl || keyEvent.key == KeyboardInput::eRightControl) ? true : db.inputs.ctrlIn() == static_cast<const bool>(keyEvent.modifiers & kKeyboardModifierFlagControl); bool altSatisfied = (keyEvent.key == KeyboardInput::eLeftAlt || keyEvent.key == KeyboardInput::eRightAlt) ? true : db.inputs.altIn() == static_cast<const bool>(keyEvent.modifiers & kKeyboardModifierFlagAlt); bool keyModifierConditionSatisfied = shiftSatisfied && ctrlSatisfied && altSatisfied; // Pressed case: all special keys pressed with keyIn last pressed if (keyEvent.type == KeyboardEventType::eKeyPress && keyModifierConditionSatisfied) { iActionGraph->setExecutionEnabled(outputs::pressed.token(), db.getInstanceIndex()); db.outputs.isPressed() = true; } // Release case 1: release keyIn while all other required special keys held else if (keyEvent.type == KeyboardEventType::eKeyRelease && keyModifierConditionSatisfied && db.outputs.isPressed()) { iActionGraph->setExecutionEnabled(outputs::released.token(), db.getInstanceIndex()); db.outputs.isPressed() = false; } db.outputs.keyOut() = keyIn; } // Release case 2: release any one of required special keys while keyIn and other special keys held and // the press key requirements have been met Check isPressed to make sure we only fire released event // once for each pressed event else if (keyEvent.type == KeyboardEventType::eKeyRelease && db.outputs.isPressed()) { if ((db.inputs.ctrlIn() && (keyEvent.key == KeyboardInput::eLeftControl || // check if ctrl required // and ctrl released keyEvent.key == KeyboardInput::eRightControl)) || (db.inputs.shiftIn() && (keyEvent.key == KeyboardInput::eLeftShift || // check if shift required // and shift released keyEvent.key == KeyboardInput::eRightShift)) || (db.inputs.altIn() && (keyEvent.key == KeyboardInput::eLeftAlt || // check if alt required and // alt released keyEvent.key == KeyboardInput::eRightAlt))) { iActionGraph->setExecutionEnabled(outputs::released.token(), db.getInstanceIndex()); db.outputs.isPressed() = false; } } } } return true; } // ---------------------------------------------------------------------------- static bool updateNodeVersion(const GraphContextObj& context, const NodeObj& nodeObj, int oldVersion, int newVersion) { if (oldVersion < newVersion) { INode const* iNode = nodeObj.iNode; if (oldVersion < 2) { // We added allowedTokens to keyIn, so uppercase the given string so that it matches // the upper case allowedToken list auto attrObj = iNode->getAttribute(nodeObj, "inputs:keyIn"); auto attrDataHandle = attrObj.iAttribute->getAttributeDataHandle(attrObj, kAccordingToContextIndex); RawPtr dataPtr{ nullptr }; size_t dataSize{ 0 }; context.iAttributeData->getDataReferenceW(attrDataHandle, context, dataPtr, dataSize); if (dataPtr) { NameToken* tokenData = (NameToken*)dataPtr; NameToken oldKeyIn = *tokenData; char const* oldKeyStr = context.iToken->getText(oldKeyIn); std::string newKeyStr = oldKeyStr; if (newKeyStr.size() >= 1) { if (std::find(begin(s_keyNames), end(s_keyNames), oldKeyStr) == end(s_keyNames)) { newKeyStr[0] = ::toupper(oldKeyStr[0]); if (std::find(begin(s_keyNames), end(s_keyNames), newKeyStr) != end(s_keyNames)) { CARB_LOG_INFO("Updating %s.inputs:keyIn from \"%s\" to \"%s\" to match allowedTokens", iNode->getPrimPath(nodeObj), oldKeyStr, newKeyStr.data()); NameToken const newKeyIn = context.iToken->getHandle(newKeyStr.data()); *tokenData = newKeyIn; } // If we didn't find the upper-cased key we can't do much more. We will rely on runtime // errors to inform the user. } } } } if (oldVersion < 3) { // We added inputs:onlyPlayback default true - to maintain previous behavior we should set this to false const bool val{ false }; nodeObj.iNode->createAttribute(nodeObj, "inputs:onlyPlayback", Type(BaseDataType::eBool), &val, nullptr, kAttributePortType_Input, kExtendedAttributeType_Regular, nullptr); } } return true; } }; REGISTER_OGN_NODE() } // namespace action } // namespace graph } // namespace omni
13,674
C++
35.564171
123
0.537078
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnRationalTimeSyncGate.cpp
// Copyright (c) 2023, 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 <OgnRationalTimeSyncGateDatabase.h> #include <omni/fabric/RationalTime.h> #include <omni/graph/action/IActionGraph.h> namespace omni { namespace graph { namespace action { class OgnRationalTimeSyncGate { public: // The current number of accumulated executions. uint32_t m_numAccumulatedExecIn{ 0 }; // The current synchronization value. fabric::RationalTime m_syncValue{ fabric::kInvalidTime }; static bool compute(OgnRationalTimeSyncGateDatabase& db) { auto nodeObj = db.abi_node(); const AttributeObj attr = nodeObj.iNode->getAttributeByToken(nodeObj, inputs::execIn.m_token); const auto accumulationThreshold = static_cast<uint32_t>(attr.iAttribute->getUpstreamConnectionCount(attr)); OgnRationalTimeSyncGate& state = db.internalState<OgnRationalTimeSyncGate>(); const fabric::RationalTime syncValue{ db.inputs.rationalTimeNumerator(), db.inputs.rationalTimeDenominator()}; const bool reset = syncValue != state.m_syncValue; state.m_numAccumulatedExecIn = reset ? 1 : std::min(state.m_numAccumulatedExecIn + 1, accumulationThreshold); state.m_syncValue = syncValue; db.outputs.rationalTimeNumerator() = syncValue.numerator; db.outputs.rationalTimeDenominator() = syncValue.denominator; if (state.m_numAccumulatedExecIn >= accumulationThreshold) { auto iActionGraph = getInterface(); iActionGraph->setExecutionEnabled(outputs::execOut.token(), db.getInstanceIndex()); } return true; } }; REGISTER_OGN_NODE() } // action } // graph } // omni
2,058
C++
35.767857
118
0.728377
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnBranch.cpp
// Copyright (c) 2021-2023, 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 <OgnBranchDatabase.h> #include <omni/graph/action/IActionGraph.h> namespace omni { namespace graph { namespace action { class OgnBranch { public: static bool compute(OgnBranchDatabase& db) { auto iActionGraph = getInterface(); bool condition = db.inputs.condition(); if (condition) iActionGraph->setExecutionEnabled(outputs::execTrue.token(), db.getInstanceIndex()); else iActionGraph->setExecutionEnabled(outputs::execFalse.token(), db.getInstanceIndex()); return true; } }; REGISTER_OGN_NODE() } // action } // graph } // omni
1,060
C++
24.878048
97
0.716981
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnForLoop.cpp
// Copyright (c) 2021-2023, 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 <OgnForLoopDatabase.h> #include <omni/graph/action/IActionGraph.h> namespace omni { namespace graph { namespace action { class OgnForLoop { public: static bool compute(OgnForLoopDatabase& db) { auto iActionGraph = getInterface(); bool const isBreakLoop = iActionGraph->getExecutionEnabled(inputs::breakLoop.token(), db.getInstanceIndex()); auto finishLoop = [&db, &iActionGraph]() { db.state.i() = -1; iActionGraph->setExecutionEnabled(outputs::finished.token(), db.getInstanceIndex()); }; if (isBreakLoop) { finishLoop(); return true; } int i = db.state.i(); // no existing loop state, initialize it to zero if (i == -1) i = 0; int step = db.inputs.step(); if (step == 0) { db.logError("Step can not be zero"); return false; } int start = db.inputs.start(); int stop = db.inputs.stop(); int rangeVal = start + step * i; if (step > 0) { if (rangeVal >= stop) { finishLoop(); return true; } } else { if (rangeVal <= stop) { finishLoop(); return true; } } // execute the loop body db.outputs.value() = rangeVal; db.outputs.index() = i; db.state.i() = i + 1; iActionGraph->setExecutionEnabledAndPushed(outputs::loopBody.token(), db.getInstanceIndex()); return true; } }; REGISTER_OGN_NODE() } // action } // graph } // omni
2,150
C++
22.9
117
0.56186
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnMultigate.cpp
// Copyright (c) 2021-2023, 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 <OgnMultigateDatabase.h> #include <carb/extras/StringUtils.h> #include <omni/graph/action/IActionGraph.h> namespace omni { namespace graph { namespace action { class OgnMultigate { public: // The output that will be activated on the next compute. uint32_t m_nextLevel{ 0 }; static bool setExecutionEnabled(NodeObj nodeObj, const char* attribName, InstanceIndex instIndex) { auto iNode = nodeObj.iNode; if (!iNode->getAttributeExists(nodeObj, attribName)) return false; AttributeObj attrObj = iNode->getAttribute(nodeObj, attribName); auto iActionGraph = getInterface(); iActionGraph->setExecutionEnabled(attrObj.iAttribute->getNameToken(attrObj), instIndex); return true; } static bool compute(OgnMultigateDatabase& db) { OgnMultigate& state = db.internalState<OgnMultigate>(); NodeObj nodeObj = db.abi_node(); auto iNode = nodeObj.iNode; bool hasSetExecution = false; // Set the execution values // lots of room to append digits to the output name std::array<char, 32> outputName; auto formatAttrName = [&outputName](uint32_t n) { carb::extras::formatString(outputName.data(), outputName.size(), "outputs:output%d", n); }; for (uint32_t i = 0;; i++) { formatAttrName(i); if (i == state.m_nextLevel && setExecutionEnabled(nodeObj, outputName.data(), db.getInstanceIndex())) { hasSetExecution = true; } else if (i != state.m_nextLevel && nodeObj.iNode->getAttributeExists(nodeObj, outputName.data())) { // keep looping while we are matching attributes (shouldn't be any holes in the sequence) } else { // Failure if (!hasSetExecution) { // We haven't found the desired output, so we'll reset to 0 formatAttrName(0); setExecutionEnabled(nodeObj, outputName.data(), db.getInstanceIndex()); state.m_nextLevel = 0; } break; } } state.m_nextLevel += 1; return true; } }; REGISTER_OGN_NODE() } // action } // graph } // omni
2,807
C++
30.550561
113
0.611685
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnSequence.cpp
// Copyright (c) 2021-2023, 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 <OgnSequenceDatabase.h> #include <omni/graph/action/IActionGraph.h> namespace omni { namespace graph { namespace action { class OgnSequence { public: // Which branch we should take next int m_nextOutput {0}; static bool compute(OgnSequenceDatabase& db) { auto iActionGraph = getInterface(); OgnSequence& state = db.internalState<OgnSequence>(); if (state.m_nextOutput == 0) { iActionGraph->setExecutionEnabledAndPushed(outputs::a.token(), db.getInstanceIndex()); } else { iActionGraph->setExecutionEnabled(outputs::b.token(), db.getInstanceIndex()); } state.m_nextOutput = (state.m_nextOutput + 1) % 2; return true; } }; REGISTER_OGN_NODE() } // action } // graph } // omni
1,256
C++
23.647058
98
0.681529
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnOnce.cpp
// Copyright (c) 2022-2023, 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 <OgnOnceDatabase.h> #include <omni/graph/action/IActionGraph.h> namespace omni { namespace graph { namespace action { class OgnOnce { bool m_open {false}; public: static bool compute(OgnOnceDatabase& db) { auto iActionGraph = getInterface(); auto& state = db.internalState<OgnOnce>(); bool isReset = iActionGraph->getExecutionEnabled(inputs::reset.token(), db.getInstanceIndex()); if (isReset) { state.m_open = false; return true; } if (!state.m_open) { state.m_open = true; iActionGraph->setExecutionEnabled(outputs::once.token(), db.getInstanceIndex()); } else { iActionGraph->setExecutionEnabled(outputs::after.token(), db.getInstanceIndex()); } return true; } }; REGISTER_OGN_NODE() } // action } // graph } // omni
1,356
C++
24.129629
103
0.653392
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnOnStageEvent.py
""" This is the implementation of the OGN node defined in OgnOnStageEvent.ogn """ from contextlib import suppress import carb import omni.graph.core as og import omni.kit.app import omni.usd from omni.graph.action import get_interface from omni.graph.action.ogn.OgnOnStageEventDatabase import OgnOnStageEventDatabase # global map of the token to python enum, should match the allowedTokens event_name_to_enum = { "Assets Loaded": omni.usd.StageEventType.ASSETS_LOADED, "Assets Load Aborted": omni.usd.StageEventType.ASSETS_LOAD_ABORTED, "Closed": omni.usd.StageEventType.CLOSED, "Closing": omni.usd.StageEventType.CLOSING, "Gizmo Tracking Changed": omni.usd.StageEventType.GIZMO_TRACKING_CHANGED, "MDL Param Loaded": omni.usd.StageEventType.MDL_PARAM_LOADED, "Opened": omni.usd.StageEventType.OPENED, "Open Failed": omni.usd.StageEventType.OPEN_FAILED, "Saved": omni.usd.StageEventType.SAVED, "Save Failed": omni.usd.StageEventType.SAVE_FAILED, "Selection Changed": omni.usd.StageEventType.SELECTION_CHANGED, "Hierarchy Changed": omni.usd.StageEventType.HIERARCHY_CHANGED, "Settings Loaded": omni.usd.StageEventType.SETTINGS_LOADED, "Settings Saving": omni.usd.StageEventType.SETTINGS_SAVING, "OmniGraph Start Play": omni.usd.StageEventType.OMNIGRAPH_START_PLAY, "OmniGraph Stop Play": omni.usd.StageEventType.OMNIGRAPH_STOP_PLAY, "Simulation Start Play": omni.usd.StageEventType.SIMULATION_START_PLAY, "Simulation Stop Play": omni.usd.StageEventType.SIMULATION_STOP_PLAY, "Animation Start Play": omni.usd.StageEventType.ANIMATION_START_PLAY, "Animation Stop Play": omni.usd.StageEventType.ANIMATION_STOP_PLAY, } class OgnOnStageEventInternalState: """Convenience class for maintaining per-node state information""" def __init__(self): """Instantiate the per-node state information.""" # This subscription object controls the lifetime of our callback, we will clean it up upon release self.sub = None # Set when the callback has triggered self.is_set = False # The last payload received self.payload: carb.dictionary.Item = None # The event_name we used to subscribe self.sub_event_type = "" # The node instance handle self.node = None # Counter to determine when an animation pause was detected self._was_paused = 0 # cache of the int-valued event types self._stop_play_events = [ int(e) for e in ( omni.usd.StageEventType.OMNIGRAPH_STOP_PLAY, omni.usd.StageEventType.SIMULATION_STOP_PLAY, omni.usd.StageEventType.ANIMATION_STOP_PLAY, ) ] self._start_play_events = [ int(e) for e in ( omni.usd.StageEventType.OMNIGRAPH_START_PLAY, omni.usd.StageEventType.SIMULATION_START_PLAY, omni.usd.StageEventType.ANIMATION_START_PLAY, ) ] self._timeline = omni.timeline.get_timeline_interface() def _on_stage_event(self, e: carb.events.IEvent): """The event callback""" if e is None: return # Maintain book-keeping for pause/resume so we can ignore them if e.type in self._stop_play_events: is_stopped = self._timeline.is_stopped() if not is_stopped: # This is actually a PAUSE, because the timeline is not stopped self._was_paused += 1 return elif (e.type in self._start_play_events) and (self._was_paused > 0): # This is actually an UNPAUSE, because we previously detected a PAUSE self._was_paused -= 1 return if e.type != int(self.sub_event_type): return self.is_set = True self.payload = e.payload # Tell the evaluator we need to be computed if self.node.is_valid(): self.node.request_compute() def first_time_subscribe(self, node: og.Node, event_type: omni.usd.StageEventType) -> bool: """Checked call to set up carb subscription Args: node: The node instance event_type: The stage event type Returns: True if we subscribed, False if we are already subscribed """ if self.sub is not None and self.sub_event_type != event_type: # event name changed since we last subscribed, unsubscribe self.sub.unsubscribe() self.sub = None if self.sub is None: # Add a subscription for the given event type. This is a pop subscription, so we expect a 1-frame # lag between send and receive self.sub = ( omni.usd.get_context() .get_stage_event_stream() .create_subscription_to_pop( self._on_stage_event, name=f"omni.graph.action.__onstageevent.{node.node_id()}" ) ) self.sub_event_type = event_type self.node = node return True return False def try_pop_event(self): """Pop the payload of the last event received, or None if there is no event to pop""" if self.is_set: self.is_set = False payload = self.payload self.payload = None return payload return None # ====================================================================== class OgnOnStageEvent: @staticmethod def internal_state(): """Returns an object that will contain per-node state information""" return OgnOnStageEventInternalState() @staticmethod def release(node): # Unsubscribe right away instead of waiting for GC cleanup, we don't want our callback firing # after the node has been released. with suppress(og.OmniGraphError): state = OgnOnStageEventDatabase.per_node_internal_state(node) if state.sub: state.sub.unsubscribe() state.sub = None @staticmethod def compute(db) -> bool: event_name = db.inputs.eventName if not event_name: return True state = db.internal_state # Check the validity of the input try: event_type = event_name_to_enum[event_name] except KeyError: db.log_error(f"{event_name} is not a recognized Stage Event") return False if state.first_time_subscribe(db.node, event_type): return True payload = state.try_pop_event() # Drop events if we are disabled, unless we are a 'stop' event - this is a special case because STOP only comes # after we are no longer playing. if ( db.inputs.onlyPlayback and (not db.node.get_graph().get_default_graph_context().get_is_playing()) and ( event_type not in ( omni.usd.StageEventType.OMNIGRAPH_STOP_PLAY, omni.usd.StageEventType.SIMULATION_STOP_PLAY, omni.usd.StageEventType.ANIMATION_STOP_PLAY, ) ) ): return True if payload is None: return True get_interface().set_execution_enabled("outputs:execOut") return True # ---------------------------------------------------------------------------- @staticmethod def update_node_version(context: og.GraphContext, node: og.Node, old_version: int, new_version: int): if old_version < new_version and old_version < 2: # We added inputs:onlyPlayback default true - to maintain previous behavior we should set this to false node.create_attribute( "inputs:onlyPlayback", og.Type(og.BaseDataType.BOOL), og.AttributePortType.ATTRIBUTE_PORT_TYPE_INPUT, False, ) return True
8,061
Python
37.390476
119
0.602531
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnOnMouseInput.cpp
// Copyright (c) 2021-2023, 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 "ActionNodeCommon.h" #include <carb/input/IInput.h> #include <carb/input/InputTypes.h> #include <omni/graph/action/IActionGraph.h> #include <omni/kit/IAppWindow.h> #include <OgnOnMouseInputDatabase.h> #include <thread> using namespace carb::input; namespace omni { namespace graph { namespace action { // Three buttons, normalized or absolute movements and scroll constexpr size_t s_numNames = 6; static std::array<NameToken, s_numNames> s_elementTokens; class OgnOnMouseInput { public: SubscriptionId m_mouseEventSubsId{ 0 }; MouseEvent m_mouseEvent; exec::unstable::Stamp m_elementSetStamp; // The stamp set by the authoring node when the event occurs exec::unstable::SyncStamp m_elementSetSyncStamp; // The stamp used by each instance static bool onMouseEvent(const InputEvent& e, void* userData) { if (e.deviceType != DeviceType::eMouse) { return false; } NodeHandle nodeHandle = reinterpret_cast<NodeHandle>(userData); auto iNode = carb::getCachedInterface<omni::graph::core::INode>(); NodeObj nodeObj = iNode->getNodeFromHandle(nodeHandle); if (!nodeObj.isValid()) return false; auto& authoringState = OgnOnMouseInputDatabase::sInternalState<OgnOnMouseInput>(nodeObj, kAuthoringGraphIndex); authoringState.m_elementSetStamp.next(); authoringState.m_mouseEvent = e.mouseEvent; iNode->requestCompute(nodeObj); return true; } static void initialize(const GraphContextObj& context, const NodeObj& nodeObj) { // First time initialization will fill up the token array [[maybe_unused]] static bool callOnce = ([] { s_elementTokens = { OgnOnMouseInputDatabase::tokens.LeftButton, OgnOnMouseInputDatabase::tokens.MiddleButton, OgnOnMouseInputDatabase::tokens.RightButton, OgnOnMouseInputDatabase::tokens.NormalizedMove, OgnOnMouseInputDatabase::tokens.PixelMove, OgnOnMouseInputDatabase::tokens.Scroll }; } (), true); omni::kit::IAppWindow* appWindow = omni::kit::getDefaultAppWindow(); if (!appWindow) return; // TODO: We may want to change the mouse into a camera one instead of appWindow one. Mouse* mouse = appWindow->getMouse(); if (!mouse) return; IInput* input = carb::getCachedInterface<IInput>(); if (!input) return; auto& authoringState = OgnOnMouseInputDatabase::sInternalState<OgnOnMouseInput>(nodeObj, kAuthoringGraphIndex); // Someone will consume the mouse event so cannot subscribe this as the last one. // TODO: Is it good to subscribe this to the front? authoringState.m_mouseEventSubsId = input->subscribeToInputEvents((carb::input::InputDevice*)mouse, kEventTypeAll, onMouseEvent, reinterpret_cast<void*>(nodeObj.nodeHandle), kSubscriptionOrderFirst); } static void release(const NodeObj& nodeObj) { IInput* input = carb::getCachedInterface<IInput>(); if (!input) return; auto const& authoringState = OgnOnMouseInputDatabase::sInternalState<OgnOnMouseInput>(nodeObj, kAuthoringGraphIndex); if (authoringState.m_mouseEventSubsId > 0) { input->unsubscribeToInputEvents(authoringState.m_mouseEventSubsId); } } static bool compute(OgnOnMouseInputDatabase& db) { if (checkNodeDisabledForOnlyPlay(db)) return true; auto const& authoringState = OgnOnMouseInputDatabase::sInternalState<OgnOnMouseInput>(db.abi_node(), kAuthoringGraphIndex); auto& localState = db.internalState<OgnOnMouseInput>(); if (localState.m_elementSetSyncStamp.makeSync(authoringState.m_elementSetStamp)) { NameToken const& elementIn = db.inputs.mouseElement(); size_t const eventIndex = static_cast<size_t>(authoringState.m_mouseEvent.type); bool eventMatched = false; if (eventIndex < 6) // Left/Middle/Right Buttons { eventMatched = s_elementTokens[eventIndex / 2] == elementIn; } else if (eventIndex == 6) // Move { eventMatched = elementIn == db.tokens.NormalizedMove || elementIn == db.tokens.PixelMove; } else if (eventIndex == 7) // Scroll { eventMatched = elementIn == db.tokens.Scroll; } else { db.logError("Invalid Input Event %zu detected", eventIndex); } float* deltaValue = db.outputs.value().data(); carb::Float2 eventValue{ 0.0f, 0.0f }; auto iActionGraph = getInterface(); if (eventMatched) { switch (authoringState.m_mouseEvent.type) { case MouseEventType::eLeftButtonDown: case MouseEventType::eMiddleButtonDown: case MouseEventType::eRightButtonDown: db.outputs.isPressed() = true; iActionGraph->setExecutionEnabled(outputs::pressed.token(), db.getInstanceIndex()); break; case MouseEventType::eLeftButtonUp: case MouseEventType::eMiddleButtonUp: case MouseEventType::eRightButtonUp: db.outputs.isPressed() = false; iActionGraph->setExecutionEnabled(outputs::released.token(), db.getInstanceIndex()); break; case MouseEventType::eMove: db.outputs.isPressed() = false; iActionGraph->setExecutionEnabled(outputs::valueChanged.token(), db.getInstanceIndex()); if (elementIn == db.tokens.NormalizedMove) { eventValue = authoringState.m_mouseEvent.normalizedCoords; } else { eventValue = authoringState.m_mouseEvent.pixelCoords; } break; case MouseEventType::eScroll: db.outputs.isPressed() = false; iActionGraph->setExecutionEnabled(outputs::valueChanged.token(), db.getInstanceIndex()); eventValue = authoringState.m_mouseEvent.scrollDelta; break; default: break; } } deltaValue[0] = eventValue.x; deltaValue[1] = eventValue.y; } return true; } }; REGISTER_OGN_NODE() } // namespace action } // namespace graph } // namespace omni
7,348
C++
35.381188
125
0.607648
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnSendCustomEvent.py
""" This is the implementation of the OGN node defined in OgnSendCustomEvent.ogn """ import codecs import pickle import carb.events import omni.kit.app from omni.graph.action import get_interface def registered_event_name(event_name): """Returns the internal name used for the given custom event name""" n = "omni.graph.action." + event_name return carb.events.type_from_string(n) payload_path = "!path" # ====================================================================== class OgnSendCustomEvent: """ This node triggers when the specified message bus event is received """ @staticmethod def compute(db) -> bool: """Compute the outputs from the current input""" event_name = db.inputs.eventName if not event_name: return True path = db.inputs.path input_bundle = db.inputs.bundle reg_event_name = registered_event_name(event_name) message_bus = omni.kit.app.get_app().get_message_bus_event_stream() payload = {} if input_bundle.valid: # Copy the contents of the input bundle into the event dict for attr in input_bundle.attributes: tp = attr.type arg_obj = (tp, attr.value) # Since we are python at both ends, easiest to pickle the attrib values so we # can re-animate them on the other side as_str = codecs.encode(pickle.dumps(arg_obj), "base64").decode() payload[attr.name] = as_str if path: payload[payload_path] = path message_bus.push(reg_event_name, payload=payload) get_interface().set_execution_enabled("outputs:execOut") return True
1,744
Python
27.606557
93
0.599197
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnGate.cpp
// Copyright (c) 2021-2023, 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 <OgnGateDatabase.h> #include <omni/graph/action/IActionGraph.h> namespace omni { namespace graph { namespace action { enum GateState { kGateStateClosed = 0, kGateStateOpen = 1, kGateStateUninitialized = 2, }; class OgnGate { public: int m_gate{ kGateStateUninitialized }; static bool compute(OgnGateDatabase& db) { auto iActionGraph = getInterface(); auto& state = db.internalState<OgnGate>(); // On our first ever compute call, initialize the gate state. if (state.m_gate == kGateStateUninitialized) { state.m_gate = db.inputs.startClosed() ? kGateStateClosed : kGateStateOpen; } // In compute one of toggle or enter will be enabled bool isToggle = iActionGraph->getExecutionEnabled(inputs::toggle.token(), db.getInstanceIndex()); if (isToggle) { // toggle the state state.m_gate = (state.m_gate + 1) % 2; } else { if (state.m_gate == kGateStateOpen) iActionGraph->setExecutionEnabled(outputs::exit.token(), db.getInstanceIndex()); } return true; } }; REGISTER_OGN_NODE() } // action } // graph } // omni
1,676
C++
25.203125
105
0.656921
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnOnCustomEvent.py
""" This is the implementation of the OGN node defined in OgnOnCustomEvent.ogn """ import codecs import pickle from contextlib import suppress import carb.events import omni.graph.core as og import omni.kit.app from omni.graph.action import get_interface from omni.graph.action.ogn.OgnOnCustomEventDatabase import OgnOnCustomEventDatabase def registered_event_name(event_name): """Returns the internal name used for the given custom event name""" n = "omni.graph.action." + event_name return carb.events.type_from_string(n) payload_path = "!path" class OgnOnCustomEventInternalState: """Convenience class for maintaining per-node state information""" def __init__(self): """Instantiate the per-node state information.""" # This subscription object controls the lifetime of our callback, it will be # cleaned up automatically when our node is destroyed self.sub = None # Set when the callback has triggered self.is_set = False # The last payload received self.payload = None # The event_name we used to subscribe self.sub_event_name = "" # The node instance handle self.node = None def on_event(self, custom_event): """The event callback""" if custom_event is None: return self.is_set = True self.payload = custom_event.payload # Tell the evaluator we need to be computed if self.node.is_valid(): self.node.request_compute() def first_time_subscribe(self, node: og.Node, event_name: str) -> bool: """Checked call to set up carb subscription Args: node: The node instance event_name: The name of the carb event Returns: True if we subscribed, False if we are already subscribed """ if self.sub is not None and self.sub_event_name != event_name: # event name changed since we last subscribed, unsubscribe self.sub.unsubscribe() self.sub = None if self.sub is None: # Add a subscription for the given event name. This is a pop subscription, so we expect a 1-frame # lag between send and receive reg_event_name = registered_event_name(event_name) message_bus = omni.kit.app.get_app().get_message_bus_event_stream() self.sub = message_bus.create_subscription_to_pop_by_type(reg_event_name, self.on_event) self.sub_event_name = event_name self.node = node return True return False def try_pop_event(self): """Pop the payload of the last event received, or None if there is no event to pop""" if self.is_set: self.is_set = False payload = self.payload self.payload = None return payload return None # ====================================================================== class OgnOnCustomEvent: """ This node triggers when the specified message bus event is received """ @staticmethod def internal_state(): """Returns an object that will contain per-node state information""" return OgnOnCustomEventInternalState() @staticmethod def compute(db) -> bool: event_name = db.inputs.eventName if not event_name: return True state = db.internal_state if state.first_time_subscribe(db.node, event_name): return True # Drop events if we are disabled if db.inputs.onlyPlayback and (not db.node.get_graph().get_default_graph_context().get_is_playing()): state.try_pop_event() return True payload = state.try_pop_event() if payload is None: return True # Copy the event dict contents into the output bundle db.outputs.bundle.clear() for name in payload.get_keys(): # Special 'path' entry gets copied to output attrib if name == payload_path: db.outputs.path = payload[name] continue as_str = payload[name] arg_obj = pickle.loads(codecs.decode(as_str.encode(), "base64")) attr_type, attr_value = arg_obj new_attr = db.outputs.bundle.insert((attr_type, name)) new_attr.value = attr_value get_interface().set_execution_enabled("outputs:execOut") return True # ---------------------------------------------------------------------------- @staticmethod def release(node): # Unsubscribe right away instead of waiting for GC cleanup, we don't want our callback firing # after the node has been released. with suppress(og.OmniGraphError): state = OgnOnCustomEventDatabase.per_node_internal_state(node) if state.sub: state.sub.unsubscribe() state.sub = None # ---------------------------------------------------------------------------- @staticmethod def update_node_version(context: og.GraphContext, node: og.Node, old_version: int, new_version: int): if old_version < new_version and old_version < 2: # We added inputs:onlyPlayback default true - to maintain previous behavior we should set this to false node.create_attribute( "inputs:onlyPlayback", og.Type(og.BaseDataType.BOOL), og.AttributePortType.ATTRIBUTE_PORT_TYPE_INPUT, False, ) return True
5,593
Python
34.18239
115
0.590917
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnDelay.cpp
// Copyright (c) 2021-2023, 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 <OgnDelayDatabase.h> #include <omni/graph/action/IActionGraph.h> namespace omni { namespace graph { namespace action { namespace { constexpr double kUninitializedStartTime = -1.; } class OgnDelay { double m_startTime{ kUninitializedStartTime }; // The value of the context time when we started latent state public: static bool compute(OgnDelayDatabase& db) { auto iActionGraph = getInterface(); const auto& contextObj = db.abi_context(); auto iContext = contextObj.iContext; auto& state = db.internalState<OgnDelay>(); double startTime = state.m_startTime; double now = iContext->getTimeSinceStart(contextObj); if (state.m_startTime > kUninitializedStartTime) { // We are being polled, check if we have slept long enough double duration = db.inputs.duration(); duration = std::max(duration, 0.); if (now - startTime >= duration) { state.m_startTime = kUninitializedStartTime; iActionGraph->endLatentState(db.getInstanceIndex()); iActionGraph->setExecutionEnabled(outputs::finished.token(), db.getInstanceIndex()); return true; } } else { // This is the first entry, start sleeping state.m_startTime = now; iActionGraph->startLatentState(db.getInstanceIndex()); return true; } // still sleeping return true; } }; REGISTER_OGN_NODE() } // action } // graph } // omni
2,040
C++
27.746478
112
0.646078
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnAddPrimRelationship.cpp
// Copyright (c) 2022, 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 "OgnAddPrimRelationshipDatabase.h" #include <omni/graph/core/PreUsdInclude.h> #include <pxr/usd/sdf/path.h> #include <pxr/usd/usd/common.h> #include <pxr/usd/usd/prim.h> #include <pxr/usd/usd/relationship.h> #include <pxr/usd/usdUtils/stageCache.h> #include <omni/graph/core/PostUsdInclude.h> #include <algorithm> namespace omni { namespace graph { namespace action { class OgnAddPrimRelationship { public: // Add relationship data on a prim static bool compute(OgnAddPrimRelationshipDatabase& db) { const auto& path = db.inputs.path(); const auto& target = db.inputs.target(); auto& isSuccessful = db.outputs.isSuccessful(); isSuccessful = false; if (!pxr::SdfPath::IsValidPathString(path) || !pxr::SdfPath::IsValidPathString(target)) { return false; } const char* relName = db.tokenToString(db.inputs.name()); if (!relName) { return false; } pxr::SdfPath primPath(path); pxr::SdfPath targetPath(target); long int stageId = db.abi_context().iContext->getStageId(db.abi_context()); pxr::UsdStageRefPtr stage = pxr::UsdUtilsStageCache::Get().Find(pxr::UsdStageCache::Id::FromLongInt(stageId)); pxr::UsdPrim prim = stage->GetPrimAtPath(primPath); if (!prim) { return false; } if (pxr::UsdRelationship myRel = prim.CreateRelationship(pxr::TfToken(relName))) { isSuccessful = myRel.AddTarget(targetPath); } return isSuccessful; } }; REGISTER_OGN_NODE() } } }
2,060
C++
26.118421
118
0.666505
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnOnPlaybackTick.cpp
// Copyright (c) 2021-2023, 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 <OgnOnPlaybackTickDatabase.h> #include <omni/graph/action/IActionGraph.h> namespace omni { namespace graph { namespace action { class OgnOnPlaybackTick { public: static bool compute(OgnOnPlaybackTickDatabase& db) { const auto& contextObj = db.abi_context(); auto iContext = contextObj.iContext; if (!iContext->getIsPlaying(contextObj)) { return true; } db.outputs.time() = iContext->getTime(contextObj); db.outputs.deltaSeconds() = iContext->getElapsedTime(contextObj); db.outputs.frame() = iContext->getFrame(contextObj); auto iActionGraph = getInterface(); iActionGraph->setExecutionEnabled(outputs::tick.token(), db.getInstanceIndex()); return true; } }; REGISTER_OGN_NODE() } // action } // graph } // omni
1,279
C++
26.234042
88
0.702893
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnMultisequence.cpp
// Copyright (c) 2022-2023, 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 <OgnMultisequenceDatabase.h> #include <carb/extras/StringUtils.h> #include <omni/graph/action/IActionGraph.h> namespace omni { namespace graph { namespace action { class OgnMultisequence { public: static bool setExecutionEnabled(NodeObj nodeObj, const char* attribName, bool andPushed, InstanceIndex instIndex) { auto iNode = nodeObj.iNode; if (!iNode->getAttributeExists(nodeObj, attribName)) return false; AttributeObj attrObj = iNode->getAttribute(nodeObj, attribName); auto iActionGraph = getInterface(); if (andPushed) iActionGraph->setExecutionEnabledAndPushed(attrObj.iAttribute->getNameToken(attrObj), instIndex); else iActionGraph->setExecutionEnabled(attrObj.iAttribute->getNameToken(attrObj), instIndex); return true; } // Which branch we should take next uint32_t m_nextOutput {0}; static bool compute(OgnMultisequenceDatabase& db) { OgnMultisequence& state = db.internalState<OgnMultisequence>(); NodeObj nodeObj = db.abi_node(); // Set the execution values uint32_t executionIndex = 0; // lots of room to append digits to the output name std::array<char, 32> outputName; auto formatAttrName = [&outputName](uint32_t n) { carb::extras::formatString(outputName.data(), outputName.size(), "outputs:output%d", n); }; for (uint32_t i = 0;; ++i) { formatAttrName(i); if (i == state.m_nextOutput && setExecutionEnabled(nodeObj, outputName.data(), true, db.getInstanceIndex())) { executionIndex = i; } else if (i != state.m_nextOutput && nodeObj.iNode->getAttributeExists(nodeObj, outputName.data())) { // keep looping while we are matching attributes (shouldn't be any holes in the sequence) } else { // Check for end of sequence if (executionIndex == i - 1) { formatAttrName(executionIndex); setExecutionEnabled(nodeObj, outputName.data(), false, db.getInstanceIndex()); state.m_nextOutput = 0; } else { ++state.m_nextOutput; } break; } } return true; } }; REGISTER_OGN_NODE() } // action } // graph } // omni
2,988
C++
30.463158
117
0.605087
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnSetPrimActive.cpp
// Copyright (c) 2022, 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 "OgnSetPrimActiveDatabase.h" #include <omni/graph/action/IActionGraph.h> #include <omni/graph/core/PreUsdInclude.h> #include <pxr/usd/sdf/path.h> #include <pxr/usd/usd/stage.h> #include <pxr/usd/usd/prim.h> #include <pxr/usd/usdUtils/stageCache.h> #include <omni/graph/core/PostUsdInclude.h> #include <omni/fabric/FabricUSD.h> namespace omni { namespace graph { namespace action { class OgnSetPrimActive { public: // ---------------------------------------------------------------------------- static bool compute(OgnSetPrimActiveDatabase& db) { PXR_NS::SdfPath sdfPath; if(db.inputs.primTarget().size() == 0) { const auto& primPath = db.inputs.prim(); if (pxr::SdfPath::IsValidPathString(primPath)) { sdfPath = pxr::SdfPath(primPath); } else { return true; } } else { if(db.inputs.primTarget().size() > 1) db.logWarning("Only one prim target is supported, the rest will be ignored"); sdfPath = omni::fabric::toSdfPath(db.inputs.primTarget()[0]); } // Find our stage const GraphContextObj& context = db.abi_context(); long stageId = context.iContext->getStageId(context); auto stage = pxr::UsdUtilsStageCache::Get().Find(pxr::UsdStageCache::Id::FromLongInt(stageId)); if (!stage) { db.logError("Could not find USD stage %ld", stageId); return false; } pxr::UsdPrim targetPrim = stage->GetPrimAtPath(sdfPath); if (!targetPrim) { db.logError("Could not find prim \"%s\" in USD stage", sdfPath.GetText()); return false; } bool ok = targetPrim.SetActive(db.inputs.active()); if (ok) { auto iActionGraph = getInterface(); iActionGraph->setExecutionEnabled(outputs::execOut.token(), db.getInstanceIndex()); return true; } db.logError("Failed to set %s active state", sdfPath.GetText()); return false; } }; REGISTER_OGN_NODE() } // namespace action } // namespace graph } // namespace omni
2,682
C++
29.488636
103
0.59918
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnTickN.py
# Copyright (c) 2022, 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. # from dataclasses import dataclass import omni.graph.core as og # Set True to enable verbose debugging prints to console DEBUG_PRINT = False # ============================================================================================================== @dataclass class OgnTickNState: count: int # -------------------------------------------------------------------------------------------------------------- class OgnTickN: @staticmethod def internal_state(): """Returns an object that will contain per-node state information""" return OgnTickNState(-1) @staticmethod def compute(db) -> bool: def print_state(state_string: str): print(f"{db.node.get_prim_path()} {state_string}") count = db.internal_state.count count += 1 if count == 0: _ = DEBUG_PRINT and print_state("START") finished, tick = og.ExecutionAttributeState.LATENT_PUSH, og.ExecutionAttributeState.DISABLED else: duration = db.inputs.duration if duration < count: # Finished finished, tick = og.ExecutionAttributeState.LATENT_FINISH, og.ExecutionAttributeState.DISABLED count = -1 _ = DEBUG_PRINT and print_state("FINISHED") else: # Still ticking finished = og.ExecutionAttributeState.DISABLED if (count % db.inputs.period) == 0: # noqa: S001 tick = og.ExecutionAttributeState.ENABLED else: tick = og.ExecutionAttributeState.DISABLED _ = DEBUG_PRINT and print_state("TICK") # Write outputs db.outputs.finished = finished db.outputs.tick = tick db.internal_state.count = count return True
2,263
Python
36.114754
112
0.572249
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnCountdown.py
# Copyright (c) 2022, 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. # import omni.graph.core as og # Set True to enable verbose debugging prints to console DEBUG_PRINT = False # ============================================================================================================== class OgnCountdown: @staticmethod def initialize(_, node): og.Controller.attribute("state:count", node).set(-1) @staticmethod def compute(db) -> bool: def print_state(state_string: str): print(f"{db.node.get_prim_path()} {state_string}") count = db.state.count duration = db.inputs.duration count += 1 tick_value = 1 alpha = 0 if count == 0: if DEBUG_PRINT: print_state("START") finished, tick = og.ExecutionAttributeState.LATENT_PUSH, og.ExecutionAttributeState.DISABLED else: if duration < count: # Finished finished, tick = og.ExecutionAttributeState.LATENT_FINISH, og.ExecutionAttributeState.DISABLED tick_value = count - 1 alpha = 1.0 count = -1 if DEBUG_PRINT: print_state("FINISHED") else: # Still ticking finished = og.ExecutionAttributeState.DISABLED tick_value = count alpha = count / max(duration, 1) period = db.inputs.period if (period == 0) or ((count % db.inputs.period) == 0): # noqa: S001 tick = og.ExecutionAttributeState.ENABLED else: tick = og.ExecutionAttributeState.DISABLED if DEBUG_PRINT: print_state("TICK") # Write outputs db.outputs.finished = finished db.outputs.tick = tick db.state.count = count db.outputs.alpha = alpha db.outputs.tickValue = tick_value return True
2,377
Python
34.492537
112
0.55995
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnOnClosing.cpp
// Copyright (c) 2022-2023, 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 <OgnOnClosingDatabase.h> #include <omni/graph/action/IActionGraph.h> namespace omni { namespace graph { namespace action { class OgnOnClosing { public: static bool compute(OgnOnClosingDatabase& db) { auto iActionGraph = getInterface(); // This node does nothing but trigger downstream when computed. When the execution evaluator receives the // pre-detach event it will ensure that only OnClosing nodes will be evaluated before cleaning up the graph. // It's not possible to encapsulate the closing logic of the evaluator in this node due to the // special requirement that other event nodes be disabled, which only applies to this one case. // (We don't want OnTick running during the closing). iActionGraph->setExecutionEnabled(outputs::execOut.token(), db.getInstanceIndex()); return true; } }; REGISTER_OGN_NODE() } // action } // graph } // omni
1,380
C++
33.524999
116
0.736232
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnOnMessageBusEvent.py
""" This is the implementation of the OGN node defined in OgnOnMessageBusEvent.ogn """ from contextlib import suppress from typing import Optional import carb import carb.dictionary import carb.events import numpy as np import omni.graph.core as og import omni.kit.app from omni.graph.action import get_interface from omni.graph.action.ogn.OgnOnMessageBusEventDatabase import OgnOnMessageBusEventDatabase class OgnOnMessageBusEventInternalState: """Convenience class for maintaining per-node state information""" def __init__(self): # This subscription object controls the lifetime of our callback, it will be # cleaned up automatically when our node is destroyed self.sub: carb.events.ISubscription = None # Set when the callback has triggered self.is_set = False # The last payload received self.payload: carb.dictionary.Item = None # The event_name we used to subscribe self.sub_event_name = "" # The node instance handle self.node: og.Node = None def on_event(self, custom_event: carb.events.IEvent): if custom_event is None: return self.is_set = True self.payload = custom_event.payload # Tell the evaluator we need to be computed if self.node.is_valid(): self.node.request_compute() def first_time_subscribe(self, node: og.Node, event_name: str) -> bool: """Checked call to set up carb subscription Args: node: The node instance event_name: The name of the carb event Returns: True if we subscribed, False if we are already subscribed """ if self.sub is not None and self.sub_event_name != event_name: # event name changed since we last subscribed, unsubscribe self.sub.unsubscribe() self.sub = None if self.sub is None: # Add a subscription for the given event name. This is a pop subscription, so we expect a 1-frame # lag between send and receive reg_event_name = carb.events.type_from_string(event_name) message_bus = omni.kit.app.get_app().get_message_bus_event_stream() self.sub = message_bus.create_subscription_to_pop_by_type(reg_event_name, self.on_event) self.sub_event_name = event_name self.node = node return True return False def try_pop_event(self) -> Optional[carb.dictionary.Item]: """Pop the payload of the last event received, or None if there is no event to pop""" if self.is_set: self.is_set = False payload = self.payload self.payload = None return payload return None # ====================================================================== class OgnOnMessageBusEvent: """ This node triggers when the specified message bus event is received """ @staticmethod def internal_state(): """Returns an object that will contain per-node state information""" return OgnOnMessageBusEventInternalState() @staticmethod def compute(db) -> bool: event_name = db.inputs.eventName if not event_name: return True state = db.internal_state if state.first_time_subscribe(db.node, event_name): return True # Drop events if we are disabled if db.inputs.onlyPlayback and (not db.node.get_graph().get_default_graph_context().get_is_playing()): state.try_pop_event() return True payload = state.try_pop_event() if payload is None: return True node: og.Node = db.node # Copy the event dict contents into dynamic attributes if they exist for key in payload.get_keys(): attr_name = f"outputs:{key}" if not node.get_attribute_exists(attr_name): continue attrib = node.get_attribute(attr_name) if attrib: value = payload[key] og_type: og.Type = attrib.get_resolved_type() is_array = og_type.array_depth > 0 is_tuple = og_type.tuple_count > 1 is_matrix = is_tuple and ( og_type.role in (og.AttributeRole.FRAME, og.AttributeRole.MATRIX, og.AttributeRole.TRANSFORM) ) if is_array: if is_matrix: dim = 2 if og_type.tuple_count == 4 else 3 if og_type.tuple_count == 9 else 4 value = np.array(value).reshape((-1, dim, dim)) elif is_tuple: dim = og_type.tuple_count value = np.array(value).reshape((-1, dim)) elif is_matrix: dim = 2 if og_type.tuple_count == 4 else 3 if og_type.tuple_count == 9 else 4 value = np.array(value).reshape((dim, dim)) try: og.Controller.set(attrib, value) except TypeError as exc: db.log_error(f"Failed to copy payload data {key} to attribute {attr_name}:\n{exc}") return False get_interface().set_execution_enabled("outputs:execOut") return True # ---------------------------------------------------------------------------- @staticmethod def release(node): # Unsubscribe right away instead of waiting for GC cleanup, we don't want our callback firing # after the node has been released. with suppress(og.OmniGraphError): state = OgnOnMessageBusEventDatabase.per_node_internal_state(node) if state.sub: state.sub.unsubscribe() state.sub = None
5,839
Python
36.435897
113
0.578352
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnOnImpulseEvent.cpp
// Copyright (c) 2021-2023, 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 <OgnOnImpulseEventDatabase.h> #include <omni/graph/action/IActionGraph.h> #include "ActionNodeCommon.h" namespace omni { namespace graph { namespace action { class OgnOnImpulseEvent { public: // ---------------------------------------------------------------------------- // Called by OG when our state attrib changes. static void onValueChanged(const AttributeObj& attrObj, const void* userData) { // state::enableImpulse has changed, so we need to compute ASAP NodeObj nodeObj = attrObj.iAttribute->getNode(attrObj); nodeObj.iNode->requestCompute(nodeObj); } // ---------------------------------------------------------------------------- static void initialize(const GraphContextObj& context, const NodeObj& nodeObj) { AttributeObj attribObj = nodeObj.iNode->getAttributeByToken(nodeObj, state::enableImpulse.m_token); attribObj.iAttribute->registerValueChangedCallback(attribObj, onValueChanged, true); } // ---------------------------------------------------------------------------- static bool compute(OgnOnImpulseEventDatabase& db) { if (checkNodeDisabledForOnlyPlay(db)) return true; bool enableImpulse = db.state.enableImpulse(); if (enableImpulse) { auto iActionGraph = getInterface(); iActionGraph->setExecutionEnabled(outputs::execOut.token(), db.getInstanceIndex()); db.state.enableImpulse() = false; } return true; } // ---------------------------------------------------------------------------- static bool updateNodeVersion(const GraphContextObj& context, const NodeObj& nodeObj, int oldVersion, int newVersion) { if (oldVersion < newVersion) { if (oldVersion < 2) { // We added inputs:onlyPlayback default true - to maintain previous behavior we should set this to false const bool val{ false }; nodeObj.iNode->createAttribute(nodeObj, "inputs:onlyPlayback", Type(BaseDataType::eBool), &val, nullptr, kAttributePortType_Input, kExtendedAttributeType_Regular, nullptr); } return true; } return false; } }; REGISTER_OGN_NODE() } // action } // graph } // omni
2,814
C++
34.632911
121
0.589552
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnCounter.cpp
// Copyright (c) 2022-2023 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 "OgnCounterDatabase.h" #include <omni/graph/action/IActionGraph.h> namespace omni { namespace graph { namespace action { class OgnCounter { public: static bool compute(OgnCounterDatabase& db) { auto iActionGraph = getInterface(); bool isReset = iActionGraph->getExecutionEnabled(inputs::reset.token(), omni::graph::core::kAccordingToContextIndex); if (isReset) { db.state.count() = 0; } else { db.state.count() = db.state.count() + 1; } db.outputs.count() = db.state.count(); iActionGraph->setExecutionEnabled(outputs::execOut.token(), db.getInstanceIndex()); return true; } }; REGISTER_OGN_NODE() } // namespace action } // namespace graph } // namespace omni
1,240
C++
27.204545
125
0.682258
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnForEach.cpp
// Copyright (c) 2022-2023, 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 <OgnForEachDatabase.h> #include <omni/graph/action/IActionGraph.h> namespace omni { namespace graph { namespace action { class OgnForEach { public: size_t m_arrayIndex {0}; static bool compute(OgnForEachDatabase& db) { auto iActionGraph = getInterface(); auto const& arrayIn = db.inputs.arrayIn(); size_t const arrayLen = arrayIn.size(); auto& state = db.internalState<OgnForEach>(); if (state.m_arrayIndex >= arrayLen) { iActionGraph->setExecutionEnabled(outputs::finished.token(), db.getInstanceIndex()); state.m_arrayIndex = 0; return true; } size_t currentIndex = state.m_arrayIndex++; iActionGraph->setExecutionEnabledAndPushed(outputs::loopBody.token(), db.getInstanceIndex()); db.outputs.arrayIndex() = static_cast<int>(currentIndex); ConstRawPtr arrayAttribPtr{nullptr}; size_t arraySize{0}; arrayIn.rawData(arrayAttribPtr, arraySize); // arrayAttribPtr is a pointer to the attribute data, the data being an array of something, // so we need to deref that pointer to get the pointer to the actual data. ConstRawPtr arrayData = *(ConstRawPtr*)arrayAttribPtr; auto& element = db.outputs.element(); // Ensure output attribute is writable uint8_t* out{ nullptr }; void** outPtr = (void**)(&out); auto hdl = element.abi_handle(); db.abi_context().iAttributeData->getDataW(outPtr, db.abi_context(), &hdl, 1); if (!outPtr) { db.logError("Could not make writable output"); return false; } // Determine the size of the element to copy const IAttributeType& iAttributeType = *carb::getCachedInterface<IAttributeType>(); Type const type = element.type(); size_t strideBytes = iAttributeType.baseDataSize(type) * type.componentCount; size_t offsetBytes = currentIndex * strideBytes; memcpy(out, arrayData + offsetBytes, strideBytes); return true; } static void onConnectionTypeResolve(const NodeObj& node) { auto const arrayIn = node.iNode->getAttributeByToken(node, inputs::arrayIn.token()); auto const element = node.iNode->getAttributeByToken(node, outputs::element.token()); auto const arrayInType = arrayIn.iAttribute->getResolvedType(arrayIn); auto const elementType = element.iAttribute->getResolvedType(element); if (elementType.baseType == BaseDataType::eUnknown) { std::array<AttributeObj, 2> attrs { arrayIn, element }; std::array<uint8_t, 2> tupleCounts { arrayInType.componentCount, arrayInType.componentCount }; // value type can not be an array because we don't support arrays-of-arrays std::array<uint8_t, 2> arrayDepths { 1, 0 }; std::array<AttributeRole, 2> rolesBuf { arrayInType.role, // Copy the attribute role from the array type to the value type AttributeRole::eUnknown }; node.iNode->resolvePartiallyCoupledAttributes(node, attrs.data(), tupleCounts.data(), arrayDepths.data(), rolesBuf.data(), attrs.size()); } } }; REGISTER_OGN_NODE() } // namespace action } // namespace graph } // namespace omni
3,959
C++
35.666666
109
0.634251
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnSwitchToken.cpp
// Copyright (c) 2022-2023, 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 <OgnSwitchTokenDatabase.h> #include <carb/events/EventsUtils.h> #include <carb/extras/StringUtils.h> #include <cstring> #include <omni/graph/action/IActionGraph.h> static constexpr size_t kMaxAttrNameLen{ 17 }; static constexpr char k3Dots[] = "..."; namespace omni { namespace graph { namespace action { using namespace omni::fabric; using namespace carb::events; class OgnSwitchToken { carb::ObjectPtr<ISubscription> m_nodeChangedSub; // ---------------------------------------------------------------------------- static AttributeObj getCorrespondingOutputAttrib(NodeObj nodeObj, char const* branchName) { char buffer[32]; char const* suffix = branchName + std::strlen("inputs:branch"); (void) carb::extras::formatString(buffer, sizeof(buffer), "outputs:output%s", suffix); AttributeObj outputAttrObj = nodeObj.iNode->getAttribute(nodeObj, buffer); if (!outputAttrObj.isValid()) throw std::runtime_error(formatString("Could not find attribute %s", buffer)); return outputAttrObj; } // ---------------------------------------------------------------------------- // Return a shorter version of the given string with ... in the middle static std::array<char, kMaxAttrNameLen + 1> ellipsisStr(char const* val, size_t valLen) { constexpr size_t snipSize{ (kMaxAttrNameLen - 3 / 2) }; std::array<char, kMaxAttrNameLen + 1> uiLabel{}; auto writeIter = std::copy(val, val + snipSize, uiLabel.begin()); writeIter = std::copy(k3Dots, k3Dots + 3, writeIter); std::copy(val + valLen - snipSize, val + valLen, writeIter); return uiLabel; } // ---------------------------------------------------------------------------- static void onValueChanged(const AttributeObj& attrObj, const void* userData) { // Get the new value NodeObj nodeObj = attrObj.iAttribute->getNode(attrObj); if (nodeObj.nodeHandle == kInvalidNodeHandle) return; auto graphObj = nodeObj.iNode->getGraph(nodeObj); if (!graphObj.isValid()) return; auto context = graphObj.iGraph->getDefaultGraphContext(graphObj); if (!context.isValid()) return; std::string name = attrObj.iAttribute->getName(attrObj); if (name.size() < 14) return; char const* suffix = name.data() + 13; auto const* pBranchVal = getDataR<NameToken>( context, attrObj.iAttribute->getConstAttributeDataHandle(attrObj, kAccordingToContextIndex)); if (!pBranchVal) return; Token branchTok{*pBranchVal}; if (!branchTok.size()) return; // Set metadata on corresponding output attrib try { AttributeObj outputAttrObj = getCorrespondingOutputAttrib(nodeObj, name.c_str()); char const* branchVal = branchTok.getText(); size_t const branchValLen = strlen(branchVal); if (branchValLen > kMaxAttrNameLen) { // Too long - instead use middle-ellipsis auto uiLabel = ellipsisStr(branchVal, branchValLen); outputAttrObj.iAttribute->setMetadata(outputAttrObj, kOgnMetadataUiName, uiLabel.data()); } else outputAttrObj.iAttribute->setMetadata(outputAttrObj, kOgnMetadataUiName, branchTok.getText()); } catch (std::exception const& ex) { nodeObj.iNode->logComputeMessageOnInstance(nodeObj, kAuthoringGraphIndex, ogn::Severity::eError, ex.what()); } } public: // ---------------------------------------------------------------------------- static void initialize(const GraphContextObj& context, const NodeObj& nodeObj) { auto& state = OgnSwitchTokenDatabase::sInternalState<OgnSwitchToken>(nodeObj); // Callback anytime an attribute is added to this node so we can monitor value changed state.m_nodeChangedSub = carb::events::createSubscriptionToPop( nodeObj.iNode->getEventStream(nodeObj).get(), [nodeObj](carb::events::IEvent* e) { switch (static_cast<INodeEvent>(e->type)) { case INodeEvent::eCreateAttribute: { carb::dictionary::IDictionary* iDict = carb::dictionary::getCachedDictionaryInterface(); auto name = iDict->get<char const*>(e->payload, "attribute"); if (name && std::strstr(name, "inputs:branch") == name) { AttributeObj attribObj = nodeObj.iNode->getAttribute(nodeObj, name); if (attribObj.isValid()) { attribObj.iAttribute->registerValueChangedCallback(attribObj, onValueChanged, false); } } } default: break; } }); // Hook up all the existing attributes to value changed callback size_t nAttribs = nodeObj.iNode->getAttributeCount(nodeObj); if (!nAttribs) return; std::vector<AttributeObj> allAttribs; allAttribs.resize(nAttribs); nodeObj.iNode->getAttributes(nodeObj, allAttribs.data(), nAttribs); for (auto& attribObj : allAttribs) { char const* name = attribObj.iAttribute->getName(attribObj); if (name && std::strstr(name, "inputs:branch") == name) { attribObj.iAttribute->registerValueChangedCallback(attribObj, onValueChanged, false); } } } // ---------------------------------------------------------------------------- static bool compute(OgnSwitchTokenDatabase& db) { NodeObj nodeObj = db.abi_node(); GraphContextObj context = db.abi_context(); NameToken const value = db.inputs.value(); auto iNode = nodeObj.iNode; GraphObj graphObj = iNode->getGraph(nodeObj); // Check which branch matches the input value size_t nAttribs = nodeObj.iNode->getAttributeCount(nodeObj); if (!nAttribs) return false; std::vector<AttributeObj> allAttribs; allAttribs.resize(nAttribs); nodeObj.iNode->getAttributes(nodeObj, allAttribs.data(), nAttribs); for (auto& attribObj : allAttribs) { char const* name = attribObj.iAttribute->getName(attribObj); if (name && std::strstr(name, "inputs:branch") == name) { auto const* pBranchVal = getDataR<NameToken>(context, attribObj.iAttribute->getConstAttributeDataHandle(attribObj, db.getInstanceIndex())); if (value == *pBranchVal) { try { AttributeObj outputAttrObj = getCorrespondingOutputAttrib(nodeObj, name); auto iActionGraph = getInterface(); iActionGraph->setExecutionEnabled(outputAttrObj.iAttribute->getNameToken(outputAttrObj), db.getInstanceIndex()); } catch (std::exception const& ex) { db.logError(ex.what()); return false; } break; } } } return true; } }; REGISTER_OGN_NODE() } // action } // graph } // omni
8,019
C++
37.743961
155
0.568649
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnOnVariableChange.cpp
// Copyright (c) 2022-2023, 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 <OgnOnVariableChangeDatabase.h> #include <omni/graph/action/IActionGraph.h> #include <omni/graph/core/ogn/string.h> #include "ActionNodeCommon.h" namespace omni { namespace graph { namespace action { using namespace omni::graph::core; using omni::graph::core::ogn::VariableAttribute; // unnamed namespace to avoid multiple declaration when linking namespace { bool compareAndCacheVariables(VariableAttribute var, std::vector<uint8_t>& cachedVariable, Type& cachedType) { if (!var.isValid()) return true; RawPtr varValuePtr{ nullptr }; size_t size{ 0 }; if (cachedType.arrayDepth > 0) { // for arrays, raw data returns the pointer to the base address of the array and the size of the pointer var.rawData(varValuePtr, size); if (varValuePtr) { // var.size() is the number of elements. size = var.size() * var.type().baseTypeSize(); varValuePtr = *reinterpret_cast<RawPtr*>(varValuePtr); } } else { var.rawData(varValuePtr, size); } if (!varValuePtr) size = 0; if ((cachedType != var.type()) || (size != cachedVariable.size()) || (memcmp(cachedVariable.data(), varValuePtr, size) != 0)) { cachedType = var.type(); cachedVariable.resize(size); memcpy(cachedVariable.data(), varValuePtr, size); return false; } return true; } } // namespace class OgnOnVariableChange { std::vector<uint8_t> m_cachedVariable{}; Type m_cachedType{}; public: // ---------------------------------------------------------------------------- static void initialize(GraphContextObj const& context, NodeObj const& nodeObj) { AttributeObj attrObj = nodeObj.iNode->getAttributeByToken(nodeObj, inputs::variableName.m_token); attrObj.iAttribute->registerValueChangedCallback(attrObj, onValueChanged, true); } // ---------------------------------------------------------------------------- // Called by OG when the value of the variableName changes static void onValueChanged(AttributeObj const& attrObj, void const* userData) { auto nodeObj = attrObj.iAttribute->getNode(attrObj); if (nodeObj.nodeHandle == kInvalidNodeHandle) return; NodeObj node = attrObj.iAttribute->getNode(attrObj); GraphObj graph = node.iNode->getGraph(nodeObj); size_t instCount = graph.iGraph->getInstanceCount(graph); //instanced or not if (instCount == 0) { auto& state = OgnOnVariableChangeDatabase::sInternalState<OgnOnVariableChange>(nodeObj, kAuthoringGraphIndex); state.m_cachedVariable.clear(); state.m_cachedType = Type(); } else { for (size_t idx = 0; idx < instCount; ++idx) { auto& state = OgnOnVariableChangeDatabase::sInternalState<OgnOnVariableChange>(nodeObj, {idx}); state.m_cachedVariable.clear(); state.m_cachedType = Type(); } } } static bool compute(OgnOnVariableChangeDatabase& db) { if (checkNodeDisabledForOnlyPlay(db)) return true; auto& state = db.internalState<OgnOnVariableChange>(); auto var = db.getVariable(db.inputs.variableName()); auto& cachedVariable = state.m_cachedVariable; auto& cachedType = state.m_cachedType; bool isClean = (cachedVariable.size() != 0); bool isSame = compareAndCacheVariables(var, cachedVariable, cachedType); if (isClean && !isSame) { auto iActionGraph = getInterface(); iActionGraph->setExecutionEnabled(outputs::changed.token(), db.getInstanceIndex()); } return true; } }; REGISTER_OGN_NODE() } // action } // graph } // omni
4,331
C++
29.942857
122
0.620411
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnOnObjectChange.cpp
// Copyright (c) 2021-2023, 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. // // clang-format off #include "UsdPCH.h" // clang-format on #include <OgnOnObjectChangeDatabase.h> #include "ActionNodeCommon.h" #include <omni/graph/action/IActionGraph.h> #include <omni/fabric/FabricUSD.h> #include <omni/graph/core/ArrayWrapper.h> // clang-format off #include <omni/usd/UsdContext.h> // clang-format on #include <mutex> namespace omni { namespace graph { namespace action { using namespace omni::graph::core; // The idea here is that each node instance will register as a notice listener when it's compute is called if the path // input has changed, or they haven't registered before. The watched path needs to be verified every compute because // input could be changing at any point. The listener must always be revoked in `release`. Any changes since the last // compute() will trigger the output exection. // class UsdNoticeListener; using UsdNoticeListenerRefPtr = pxr::TfRefPtr<UsdNoticeListener>; using UsdNoticeListenerWeakPtr = pxr::TfWeakPtr<UsdNoticeListener>; // Derive from Ref and Weak bases so that we don't crash during `release` (and we delete the listener) from a // different thread than the notices are being sent on. Instead we just decref the listener so that the internal // weak pointer will keep it alive until notice sending is completed class UsdNoticeListener : public pxr::TfRefBase, public pxr::TfWeakBase { public: enum class WatchType { eInvalid, ePrim, ePrimProperty }; static UsdNoticeListenerRefPtr New(long stageId) { return pxr::TfCreateRefPtr(new UsdNoticeListener(stageId)); } ~UsdNoticeListener() { pxr::TfNotice::Revoke(m_registerKey); } void registerForPath(pxr::SdfPath path, UsdNoticeListenerRefPtr ptr) { // Ensure our dirty flag isn't still set { const std::lock_guard<std::mutex> lock(m_noticeMutex); m_watchedPath = path; m_isDirty = false; m_noticedName = pxr::TfToken(); } // If we are given an empty path we can revoke if (path.IsEmpty()) { if (m_registerKey.IsValid()) pxr::TfNotice::Revoke(m_registerKey); return; } // Determine if the path actually exists on the stage m_watchType = WatchType::eInvalid; if (path.IsPrimPath()) { pxr::UsdPrim prim = m_stage->GetPrimAtPath(path); if (!prim) return; m_watchType = WatchType::ePrim; } else if (path.IsPrimPropertyPath()) { pxr::UsdProperty prop = m_stage->GetPropertyAtPath(path); if (!prop) return; m_watchType = WatchType::ePrimProperty; } // Register ourselves if (!m_registerKey.IsValid()) m_registerKey = pxr::TfNotice::Register(UsdNoticeListenerWeakPtr(ptr), &UsdNoticeListener::Handle); } void Handle(const class pxr::UsdNotice::ObjectsChanged& objectsChanged) { // If already dirty, don't worry about it if (m_isDirty) return; if(m_stage != objectsChanged.GetStage()) return; switch (m_watchType) { case WatchType::ePrim: { pxr::UsdPrim prim = m_stage->GetPrimAtPath(m_watchedPath); if (prim) { for (const auto& path : objectsChanged.GetChangedInfoOnlyPaths()) { const pxr::SdfPath& changePath = path.GetPrimPath(); if (m_stage->GetPrimAtPath(changePath) == prim) { const std::lock_guard<std::mutex> lock(m_noticeMutex); m_noticedName = path.GetNameToken(); m_isDirty = true; break; } } } break; } case WatchType::ePrimProperty: { pxr::UsdProperty prop = m_stage->GetPropertyAtPath(m_watchedPath); if (prop) { if (objectsChanged.AffectedObject(prop)) { const std::lock_guard<std::mutex> lock(m_noticeMutex); m_noticedName = m_watchedPath.GetNameToken(); m_isDirty = true; } } break; } case WatchType::eInvalid: break; } } pxr::UsdStageRefPtr m_stage; pxr::TfNotice::Key m_registerKey; pxr::SdfPath m_watchedPath; WatchType m_watchType{ WatchType::eInvalid }; bool m_isDirty{ false }; pxr::TfToken m_noticedName; std::mutex m_noticeMutex; private: UsdNoticeListener(long stageId) { m_stage = pxr::UsdUtilsStageCache::Get().Find(pxr::UsdStageCache::Id::FromLongInt(stageId)); if (!m_stage) { CARB_LOG_ERROR("Could not find USD stage"); return; } } }; // ============================================================================ class OgnOnObjectChange { public: UsdNoticeListenerRefPtr m_listener{ nullptr }; static bool compute(OgnOnObjectChangeDatabase& db) { auto& nodeObj = db.abi_node(); const auto& contextObj = db.abi_context(); if (checkNodeDisabledForOnlyPlay(db)) return true; pxr::SdfPath watchedPath = toSdfPath(db.inputs.prim.firstOrDefault()); if (!watchedPath.IsEmpty()) { auto name = db.inputs.name(); if (name != omni::fabric::kUninitializedToken) { auto nameToken = toTfToken(name); watchedPath = watchedPath.AppendProperty(nameToken); } } if (watchedPath.IsEmpty()) { auto const& path = db.inputs.path(); if (!pxr::SdfPath::IsValidPathString(path)) { if (!path.empty()) { db.logError("Invalid path %s", path.data()); return false; } } else watchedPath = pxr::SdfPath(path); } auto& state = db.internalState<OgnOnObjectChange>(); auto processChange = [&]() -> pxr::TfToken { // Find the listener for this node, or create one if it doesn't exist if (!state.m_listener) { long stageId = db.abi_context().iContext->getStageId(db.abi_context()); state.m_listener = UsdNoticeListener::New(stageId); state.m_listener->registerForPath(watchedPath, state.m_listener); return pxr::TfToken(); } if (state.m_listener->m_watchedPath != watchedPath) { // re-register for the new path state.m_listener->registerForPath(watchedPath, state.m_listener); return pxr::TfToken(); } { const std::lock_guard<std::mutex> lock(state.m_listener->m_noticeMutex); if (std::exchange(state.m_listener->m_isDirty, false)) { return state.m_listener->m_noticedName; } } return pxr::TfToken(); }; auto changed = processChange(); if (!changed.IsEmpty()) { auto iActionGraph = getInterface(); iActionGraph->setExecutionEnabled(outputs::changed.token(), db.getInstanceIndex()); db.outputs.propertyName() = omni::fabric::asInt(changed); } return true; } // ---------------------------------------------------------------------------- static void release(const NodeObj& nodeObj) { auto& state = OgnOnObjectChangeDatabase::sInternalState<OgnOnObjectChange>(nodeObj); state.m_listener.Reset(); } // ---------------------------------------------------------------------------- static bool updateNodeVersion(const GraphContextObj& context, const NodeObj& nodeObj, int oldVersion, int newVersion) { if (oldVersion < newVersion) { if (oldVersion < 2) { // We added inputs:onlyPlayback default true - to maintain previous behavior we should set this to false const bool val{ false }; nodeObj.iNode->createAttribute(nodeObj, "inputs:onlyPlayback", Type(BaseDataType::eBool), &val, nullptr, kAttributePortType_Input, kExtendedAttributeType_Regular, nullptr); } return true; } return false; } }; REGISTER_OGN_NODE() } // action } // graph } // omni
9,254
C++
30.587031
121
0.557921
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnOnTick.cpp
// Copyright (c) 2021-2023, 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 <OgnOnTickDatabase.h> #include <omni/graph/action/IActionGraph.h> namespace omni { namespace graph { namespace action { class OgnOnTick { public: static size_t computeVectorized(OgnOnTickDatabase& db, size_t count) { auto iActionGraph = getInterface(); const auto& contextObj = db.abi_context(); const IGraphContext* const iContext = contextObj.iContext; bool isPlaying = iContext->getIsPlaying(contextObj); auto elapsed = iContext->getElapsedTime(contextObj); auto const t = iContext->getTime(contextObj); auto const f = iContext->getFrame(contextObj); auto const ts = iContext->getTimeSinceStart(contextObj); auto const ast = iContext->getAbsoluteSimTime(contextObj); auto onlyPlayback = db.inputs.onlyPlayback.vectorized(count); auto framePeriod = db.inputs.framePeriod.vectorized(count); auto isPlayingOut = db.outputs.isPlaying.vectorized(count); auto time = db.outputs.time.vectorized(count); auto frame = db.outputs.frame.vectorized(count); auto timeSinceStart = db.outputs.timeSinceStart.vectorized(count); auto absoluteSimTime = db.outputs.absoluteSimTime.vectorized(count); auto deltaSeconds = db.outputs.deltaSeconds.vectorized(count); auto accumulatedSeconds = db.state.accumulatedSeconds.vectorized(count); auto frameCount = db.state.frameCount.vectorized(count); for (size_t idx = 0; idx < count; ++idx) { if (onlyPlayback[idx] && !isPlaying) { accumulatedSeconds[idx] = 0; frameCount[idx] = 0; continue; } ++frameCount[idx]; accumulatedSeconds[idx] += elapsed; if (frameCount[idx] > framePeriod[idx]) { frameCount[idx] = 0; deltaSeconds[idx] = accumulatedSeconds[idx]; accumulatedSeconds[idx] = 0; isPlayingOut[idx] = isPlaying; time[idx] = t; frame[idx] = f; timeSinceStart[idx] = ts; absoluteSimTime[idx] = ast; iActionGraph->setExecutionEnabled(outputs::tick.token(), db.getInstanceIndex() + idx); } } return count; } }; REGISTER_OGN_NODE() } // action } // graph } // omni
2,839
C++
32.411764
102
0.637548
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnFlipFlop.cpp
// Copyright (c) 2021-2023, 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 <OgnFlipFlopDatabase.h> #include <omni/graph/action/IActionGraph.h> namespace omni { namespace graph { namespace action { class OgnFlipFlop { public: // The flip-flop cycles output activation between 2 outputs static constexpr size_t kNumLevels = 2; // The output that will be activated on the next compute. 0 == A, 1 == B int m_nextLevel{ 0 }; static bool compute(OgnFlipFlopDatabase& db) { auto iActionGraph = getInterface(); OgnFlipFlop& state = db.internalState<OgnFlipFlop>(); if (state.m_nextLevel == 0) { iActionGraph->setExecutionEnabled(outputs::a.token(), db.getInstanceIndex()); db.outputs.isA() = true; } else { iActionGraph->setExecutionEnabled(outputs::b.token(), db.getInstanceIndex()); db.outputs.isA() = false; } state.m_nextLevel = (state.m_nextLevel + 1) % kNumLevels; return true; } }; REGISTER_OGN_NODE() } // action } // graph } // omni
1,472
C++
26.277777
89
0.669158
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/ActionNodeCommon.h
// Copyright (c) 2021-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. // namespace omni { namespace graph { namespace action { /** * Checks if the node with `inputs:onlyPlayback` should be disabled, because playback is not happening. * * @param[in] db The node OGN Database object * @return true if the node should be disabled */ template<typename NodeDb> bool checkNodeDisabledForOnlyPlay(NodeDb const& db) { return db.inputs.onlyPlayback() && (not db.abi_context().iContext->getIsPlaying(db.abi_context())); } } } }
897
C
27.062499
103
0.760312
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnSyncGate.cpp
// Copyright (c) 2022-2023, 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 <OgnSyncGateDatabase.h> #include <omni/graph/action/IActionGraph.h> namespace omni { namespace graph { namespace action { class OgnSyncGate { public: // The current number of accumulated executions. uint32_t m_numAccumulatedExecIn{ 0 }; // The current synchronization value. uint64_t m_syncValue{ 0 }; static bool compute(OgnSyncGateDatabase& db) { auto nodeObj = db.abi_node(); const AttributeObj attr = nodeObj.iNode->getAttributeByToken(nodeObj, inputs::execIn.m_token); const auto accumulationThreshold = static_cast<uint32_t>(attr.iAttribute->getUpstreamConnectionCount(attr)); OgnSyncGate& state = db.internalState<OgnSyncGate>(); const bool reset = db.inputs.syncValue() != state.m_syncValue; state.m_numAccumulatedExecIn = reset ? 1 : std::min(state.m_numAccumulatedExecIn + 1, accumulationThreshold); db.outputs.syncValue() = state.m_syncValue = db.inputs.syncValue(); if (state.m_numAccumulatedExecIn >= accumulationThreshold) { auto iActionGraph = getInterface(); iActionGraph->setExecutionEnabled(outputs::execOut.token(), db.getInstanceIndex()); } return true; } }; REGISTER_OGN_NODE() } // action } // graph } // omni
1,727
C++
32.882352
117
0.710481
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/nodes/OgnOnGamepadInput.cpp
// Copyright (c) 2022-2023, 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 "ActionNodeCommon.h" #include <carb/input/IInput.h> #include <carb/input/InputTypes.h> #include <omni/graph/action/IActionGraph.h> #include <omni/kit/IAppWindow.h> #include <OgnOnGamepadInputDatabase.h> #include <atomic> using namespace carb::input; namespace omni { namespace graph { namespace action { // This is different from carb::input::GamepadInput::eCount by 10 because we don't allow joysticks and triggers input. constexpr auto s_firstEventWeCareAbout = carb::input::GamepadInput::eA; constexpr size_t s_numNames = size_t(carb::input::GamepadInput::eCount) - size_t(s_firstEventWeCareAbout); static std::array<NameToken, s_numNames> s_elementTokens; class OgnOnGamepadInput { public: // Assume a zero subsId means not registered SubscriptionId m_gamepadConnectionSubsId{ 0 }; SubscriptionId m_elementEventSubsId{ 0 }; GamepadEvent m_elementEvent; std::atomic<bool> m_requestedRegistrationInCompute{ false }; // Dirty flag to ask compute() to potentially subscribe // to the new gamepad if needed exec::unstable::Stamp m_elementSetStamp; // The stamp set by the authoring node when the even occurs exec::unstable::SyncStamp m_elementSetSyncStamp; // The stamp used by each instance static bool trySwitchGamepadSubscription(const size_t newGamepadId, const NodeObj& nodeObj) { IInput* input = carb::getCachedInterface<IInput>(); if (!input) return false; auto& state = OgnOnGamepadInputDatabase::sInternalState<OgnOnGamepadInput>(nodeObj, kAuthoringGraphIndex); omni::kit::IAppWindow* appWindow = omni::kit::getDefaultAppWindow(); if (!appWindow) { return false; } Gamepad* gamepad = appWindow->getGamepad(newGamepadId); if (!gamepad) { CARB_LOG_WARN_ONCE("The new gamepad ID is not associated with any gamepad"); if (state.m_elementEventSubsId > 0) { input->unsubscribeToInputEvents(state.m_elementEventSubsId); } state.m_elementEventSubsId = 0; return false; } if (state.m_elementEventSubsId > 0) { input->unsubscribeToInputEvents(state.m_elementEventSubsId); } state.m_elementEventSubsId = input->subscribeToInputEvents((carb::input::InputDevice*)gamepad, kEventTypeAll, onGamepadEvent, reinterpret_cast<void*>(nodeObj.nodeHandle), kSubscriptionOrderDefault); return true; } static bool onGamepadEvent(const InputEvent& e, void* userData) { if (e.deviceType != DeviceType::eGamepad) { return false; } NodeHandle nodeHandle = reinterpret_cast<NodeHandle>(userData); auto iNode = carb::getCachedInterface<omni::graph::core::INode>(); NodeObj nodeObj = iNode->getNodeFromHandle(nodeHandle); if (!nodeObj.isValid()) return false; // Copy the event data and request the next compute() auto& authoringState = OgnOnGamepadInputDatabase::sInternalState<OgnOnGamepadInput>(nodeObj, kAuthoringGraphIndex); authoringState.m_elementSetStamp.next(); authoringState.m_elementEvent = e.gamepadEvent; iNode->requestCompute(nodeObj); return true; } static void onGamepadIdChanged(const AttributeObj& attrObj, const void* value) { NodeObj nodeObj{ attrObj.iAttribute->getNode(attrObj) }; if (nodeObj.nodeHandle == kInvalidNodeHandle) return; GraphObj graphObj{ nodeObj.iNode->getGraph(nodeObj) }; if (graphObj.graphHandle == kInvalidGraphHandle) return; GraphContextObj context{ graphObj.iGraph->getDefaultGraphContext(graphObj) }; if (context.contextHandle == kInvalidGraphContextHandle) return; ConstAttributeDataHandle attributeDataHandle = attrObj.iAttribute->getConstAttributeDataHandle(attrObj, kAccordingToContextIndex); const uint32_t gamepadId = *getDataR<uint32_t>(context, attributeDataHandle); // Change subscription target trySwitchGamepadSubscription(gamepadId, nodeObj); } static void initialize(const GraphContextObj& context, const NodeObj& nodeObj) { // First time look up all the tokens and their lowercase equivalents [[maybe_unused]] static bool callOnce = ([] { s_elementTokens = { OgnOnGamepadInputDatabase::tokens.FaceButtonBottom, OgnOnGamepadInputDatabase::tokens.FaceButtonRight, OgnOnGamepadInputDatabase::tokens.FaceButtonLeft, OgnOnGamepadInputDatabase::tokens.FaceButtonTop, OgnOnGamepadInputDatabase::tokens.LeftShoulder, OgnOnGamepadInputDatabase::tokens.RightShoulder, OgnOnGamepadInputDatabase::tokens.SpecialLeft, OgnOnGamepadInputDatabase::tokens.SpecialRight, OgnOnGamepadInputDatabase::tokens.LeftStickButton, OgnOnGamepadInputDatabase::tokens.RightStickButton, OgnOnGamepadInputDatabase::tokens.DpadUp, OgnOnGamepadInputDatabase::tokens.DpadRight, OgnOnGamepadInputDatabase::tokens.DpadDown, OgnOnGamepadInputDatabase::tokens.DpadLeft }; } (), true); // Get the default or stored gamepad ID when creating new nodes or loading a saved file auto gamepadIdAttr = nodeObj.iNode->getAttributeByToken(nodeObj, inputs::gamepadId.token()); IInput* input = carb::getCachedInterface<IInput>(); if (!input) return; // Happens normally in headless auto& authoringState = OgnOnGamepadInputDatabase::sInternalState<OgnOnGamepadInput>(nodeObj, kAuthoringGraphIndex); authoringState.m_requestedRegistrationInCompute.store(true); gamepadIdAttr.iAttribute->registerValueChangedCallback(gamepadIdAttr, onGamepadIdChanged, true); // This will allow user to connect gamepad after specifying gamepad ID authoringState.m_gamepadConnectionSubsId = input->subscribeToGamepadConnectionEvents( [](const carb::input::GamepadConnectionEvent& evt, void* userData) { NodeHandle nodeHandle = reinterpret_cast<NodeHandle>(userData); auto iNode = carb::getCachedInterface<omni::graph::core::INode>(); NodeObj nodeObj = iNode->getNodeFromHandle(nodeHandle); auto& state = OgnOnGamepadInputDatabase::sInternalState<OgnOnGamepadInput>(nodeObj); state.m_requestedRegistrationInCompute.store(true); // Since the subscription depends on another GamepadConnectionEvents in IAppWindwImplCommon, and we // cannot assume the execution order iNode->requestCompute(nodeObj); }, reinterpret_cast<void*>(nodeObj.nodeHandle)); } static void release(const NodeObj& nodeObj) { auto& authoringState = OgnOnGamepadInputDatabase::sInternalState<OgnOnGamepadInput>(nodeObj); IInput* input = carb::getCachedInterface<IInput>(); if (!input) return; if (authoringState.m_elementEventSubsId > 0) { input->unsubscribeToInputEvents(authoringState.m_elementEventSubsId); } if (authoringState.m_gamepadConnectionSubsId > 0) { input->unsubscribeToGamepadConnectionEvents(authoringState.m_gamepadConnectionSubsId); } } static bool compute(OgnOnGamepadInputDatabase& db) { auto& authoringState = OgnOnGamepadInputDatabase::sInternalState<OgnOnGamepadInput>(db.abi_node(), kAuthoringGraphIndex); auto& localState = db.internalState<OgnOnGamepadInput>(); // Retry subscribe gamepad when new gamepad is connected if (authoringState.m_requestedRegistrationInCompute.exchange(false)) { trySwitchGamepadSubscription(db.inputs.gamepadId(), db.abi_node()); } if (checkNodeDisabledForOnlyPlay(db)) return true; if (localState.m_elementSetSyncStamp.makeSync(authoringState.m_elementSetStamp)) { NameToken const& gamepadElementIn = db.inputs.gamepadElementIn(); // Offset the index by 10 since we excluded the joysticks and triggers. if (size_t(authoringState.m_elementEvent.input) < size_t(s_firstEventWeCareAbout)) return true; size_t elementIndex = size_t(authoringState.m_elementEvent.input) - size_t(s_firstEventWeCareAbout); if (elementIndex >= s_elementTokens.size()) { db.logError("Invalid Key %d detected", authoringState.m_elementEvent.input); return false; } auto iActionGraph = getInterface(); if (gamepadElementIn == s_elementTokens[elementIndex]) { if (authoringState.m_elementEvent.value == 1) { iActionGraph->setExecutionEnabled(outputs::pressed.token(), omni::graph::core::kAccordingToContextIndex); db.outputs.isPressed() = true; } else { iActionGraph->setExecutionEnabled(outputs::released.token(), omni::graph::core::kAccordingToContextIndex); db.outputs.isPressed() = false; } } } return true; } }; REGISTER_OGN_NODE() } // namespace action } // namespace graph } // namespace omni
10,224
C++
38.941406
126
0.654636
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/ogn/tests/TestOgnCounter.py
import omni.kit.test import omni.graph.core as og import omni.graph.core.tests as ogts import os class TestOgn(ogts.OmniGraphTestCase): async def test_generated(self): test_data = [{'inputs': [['inputs:execIn', 1, False]], 'outputs': [['outputs:count', 1, False]], 'state_set': [['state:count', 0, False]]}, {'inputs': [['inputs:execIn', 1, False]], 'outputs': [['outputs:count', 2, False]], 'state_set': [['state:count', 1, False]]}, {'inputs': [['inputs:execIn', 0, False], ['inputs:reset', 1, False]], 'outputs': [['outputs:count', 0, False]], 'state_set': [['state:count', 1, False]], 'state_get': [['state:count', 0, False]]}] test_node = None test_graph = None for i, test_run in enumerate(test_data): inputs = test_run.get('inputs', []) outputs = test_run.get('outputs', []) state_set = test_run.get('state_set', []) state_get = test_run.get('state_get', []) setup = test_run.get('setup', None) if setup is None or setup: await omni.usd.get_context().new_stage_async() test_graph = None elif not setup: self.assertTrue(test_graph is not None and test_graph.is_valid(), "Test is misconfigured - empty setup cannot be in the first test") if setup: (test_graph, test_nodes, _, _) = og.Controller.edit("/TestGraph", setup) self.assertTrue(test_nodes) test_node = test_nodes[0] elif setup is None: if test_graph is None: test_graph = og.Controller.create_graph("/TestGraph") self.assertTrue(test_graph is not None and test_graph.is_valid()) test_node = og.Controller.create_node( ("TestNode_omni_graph_action_Counter", test_graph), "omni.graph.action.Counter" ) self.assertTrue(test_graph is not None and test_graph.is_valid(), "Test graph invalid") self.assertTrue(test_node is not None and test_node.is_valid(), "Test node invalid") await og.Controller.evaluate(test_graph) values_to_set = inputs + state_set if values_to_set: for attribute_name, attribute_value, _ in inputs + state_set: og.Controller((attribute_name, test_node)).set(attribute_value) await og.Controller.evaluate(test_graph) for attribute_name, expected_value, _ in outputs + state_get: attribute = og.Controller.attribute(attribute_name, test_node) actual_output = og.Controller.get(attribute) expected_type = None if isinstance(expected_value, dict): expected_type = expected_value["type"] expected_value = expected_value["value"] ogts.verify_values(expected_value, actual_output, f"omni.graph.action.Counter User test case #{i+1}: {attribute_name} attribute value error") if expected_type: tp = og.AttributeType.type_from_ogn_type_name(expected_type) actual_type = attribute.get_resolved_type() if tp != actual_type: raise ValueError(f"omni.graph.action.Counter User tests - {attribute_name}: Expected {expected_type}, saw {actual_type.get_ogn_type_name()}") async def test_data_access(self): from omni.graph.action.ogn.OgnCounterDatabase import OgnCounterDatabase test_file_name = "OgnCounterTemplate.usda" usd_path = os.path.join(os.path.dirname(__file__), "usd", test_file_name) if not os.path.exists(usd_path): self.assertTrue(False, f"{usd_path} not found for loading test") (result, error) = await ogts.load_test_file(usd_path) self.assertTrue(result, f'{error} on {usd_path}') test_node = og.Controller.node("/TestGraph/Template_omni_graph_action_Counter") database = OgnCounterDatabase(test_node) self.assertTrue(test_node.is_valid()) node_type_name = test_node.get_type_name() self.assertEqual(og.GraphRegistry().get_node_type_version(node_type_name), 1) def _attr_error(attribute: og.Attribute, usd_test: bool) -> str: test_type = "USD Load" if usd_test else "Database Access" return f"{node_type_name} {test_type} Test - {attribute.get_name()} value error" self.assertTrue(test_node.get_attribute_exists("inputs:execIn")) attribute = test_node.get_attribute("inputs:execIn") db_value = database.inputs.execIn self.assertTrue(test_node.get_attribute_exists("inputs:reset")) attribute = test_node.get_attribute("inputs:reset") db_value = database.inputs.reset self.assertTrue(test_node.get_attribute_exists("outputs:count")) attribute = test_node.get_attribute("outputs:count") db_value = database.outputs.count self.assertTrue(test_node.get_attribute_exists("outputs:execOut")) attribute = test_node.get_attribute("outputs:execOut") db_value = database.outputs.execOut self.assertTrue(test_node.get_attribute_exists("state:count")) attribute = test_node.get_attribute("state:count") db_value = database.state.count
5,356
Python
55.389473
471
0.60717
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/_impl/templates/template_omni.graph.action.SwitchToken.py
# Copyright (c) 2022, 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. # from pathlib import Path from typing import List import omni.graph.core as og import omni.ui as ui from omni.kit.property.usd.custom_layout_helper import CustomLayoutFrame, CustomLayoutGroup, CustomLayoutProperty from omni.kit.window.property.templates import HORIZONTAL_SPACING class CustomLayout: def __init__(self, compute_node_widget): self.enable = True self.compute_node_widget = compute_node_widget self.controller = og.Controller() self.add_button: ui.Button = None self.remove_button: ui.Button = None self.node = self.controller.node(self.compute_node_widget._payload[-1]) def _get_input_attrib_names(self) -> List[str]: """Return the list of dynamic input attribs""" all_attribs = self.node.get_attributes() input_attrib_names = [] for attrib in all_attribs: attrib_name = attrib.get_name() name_prefix = attrib_name[:13] if name_prefix == "inputs:branch": input_attrib_names.append(attrib_name) return input_attrib_names def _get_max_suffix(self) -> int: """Return the maximum suffix of dynamic inputs or -1 if there are none""" names = self._get_input_attrib_names() if not names: return -1 return max(int(name[13:]) for name in names) def _on_click_add(self): next_suffix = f"{self._get_max_suffix() + 1:02}" new_attr = self.controller.create_attribute( self.node, f"inputs:branch{next_suffix}", og.Type(og.BaseDataType.TOKEN, 1, 0, og.AttributeRole.NONE), og.AttributePortType.ATTRIBUTE_PORT_TYPE_INPUT, ) new_attr.set_metadata(og.MetadataKeys.LITERAL_ONLY, "1") self.controller.create_attribute( self.node, f"outputs:output{next_suffix}", og.Type(og.BaseDataType.UINT, 1, 0, og.AttributeRole.EXECUTION), og.AttributePortType.ATTRIBUTE_PORT_TYPE_OUTPUT, ) self.compute_node_widget.rebuild_window() self.remove_button.enabled = True def _on_click_remove(self): max_suffix = self._get_max_suffix() if max_suffix < 0: return attrib_to_remove = self.node.get_attribute(f"outputs:output{max_suffix:02}") self.controller.remove_attribute(attrib_to_remove) attrib_to_remove = self.node.get_attribute(f"inputs:branch{max_suffix:02}") self.controller.remove_attribute(attrib_to_remove) self.compute_node_widget.rebuild_window() self.remove_button.enabled = max_suffix > 0 def _controls_build_fn(self, *args): max_suffix = self._get_max_suffix() icons_path = Path(__file__).absolute().parent.parent.parent.parent.parent.parent.joinpath("icons") with ui.HStack(height=0, spacing=HORIZONTAL_SPACING): ui.Spacer() self.add_button = ui.Button( image_url=f"{icons_path.joinpath('add.svg')}", width=22, height=22, style={"Button": {"background_color": 0x1F2124}}, clicked_fn=self._on_click_add, tooltip_fn=lambda: ui.Label("Add New Branch"), ) self.remove_button = ui.Button( image_url=f"{icons_path.joinpath('remove.svg')}", width=22, height=22, style={"Button": {"background_color": 0x1F2124}}, enabled=(max_suffix > 0), clicked_fn=self._on_click_remove, tooltip_fn=lambda: ui.Label("Remove Branch"), ) def apply(self, props): # Called by compute_node_widget to apply UI when selection changes def find_prop(name): return next((p for p in props if p.prop_name == name), None) frame = CustomLayoutFrame(hide_extra=True) names = self._get_input_attrib_names() with frame: with CustomLayoutGroup("Inputs"): for name in names: prop = find_prop(name) CustomLayoutProperty(prop.prop_name) CustomLayoutProperty(None, None, build_fn=self._controls_build_fn) return frame.apply(props)
4,701
Python
40.610619
113
0.61689
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/tests/test_action_graph_nodes_02.py
"""Action Graph Node Tests, Part 2""" import random from functools import partial from typing import List import carb import carb.input import numpy as np import omni.client import omni.graph.core as og import omni.graph.core.tests as ogts import omni.graph.tools.ogn as ogn import omni.kit.app import omni.kit.test from carb.input import GamepadInput, KeyboardEventType, KeyboardInput from omni.graph.core import ThreadsafetyTestUtils # ====================================================================== class TestActionGraphNodes(ogts.OmniGraphTestCase): """Tests action graph node functionality""" TEST_GRAPH_PATH = "/World/TestGraph" keys = og.Controller.Keys E = og.ExecutionAttributeState.ENABLED D = og.ExecutionAttributeState.DISABLED async def setUp(self): """Set up test environment, to be torn down when done""" await super().setUp() # ---------------------------------------------------------------------- @ThreadsafetyTestUtils.make_threading_test def test_ongamepadinput_node(self, test_instance_id: int = 0): """Test OnGamepadInput node""" # Obtain an interface to a few gamepads and carb input provider. input_provider = ThreadsafetyTestUtils.add_to_threading_cache( test_instance_id, carb.input.acquire_input_provider() ) gamepad_list = ThreadsafetyTestUtils.add_to_threading_cache( test_instance_id, [ input_provider.create_gamepad("Gamepad 0", "0"), input_provider.create_gamepad("Gamepad 1", "1"), input_provider.create_gamepad("Gamepad 2", "2"), ], ) # Connect the gamepads. for gamepad in gamepad_list: self.assertIsNotNone(gamepad) ThreadsafetyTestUtils.single_evaluation_first_test_instance( test_instance_id, partial(input_provider.set_gamepad_connected, gamepad, True) ) yield ThreadsafetyTestUtils.EVALUATION_WAIT_FRAME # Yielding to compute by waiting for the next app frame. # Instance a test graph setup. Note that we append the graph path with the test_instance_id # so that the graph can be uniquely identified in the thread-safety test! graph_path = self.TEST_GRAPH_PATH + str(test_instance_id) og.Controller.create_graph({"graph_path": graph_path, "evaluator_name": "execution"}) (_, (on_gamepad_input_node,), _, _) = og.Controller.edit( graph_path, {self.keys.CREATE_NODES: ("OnGamepadInput", "omni.graph.action.OnGamepadInput")} ) # Obtain necessary attributes. in_onlyplayback_attr = on_gamepad_input_node.get_attribute("inputs:onlyPlayback") in_gamepadid_attr = on_gamepad_input_node.get_attribute("inputs:gamepadId") in_gamepad_element_attr = on_gamepad_input_node.get_attribute("inputs:gamepadElementIn") out_pressed_attr = on_gamepad_input_node.get_attribute("outputs:pressed") out_released_attr = on_gamepad_input_node.get_attribute("outputs:released") out_ispressed_attr = on_gamepad_input_node.get_attribute("outputs:isPressed") # Define a list of all possible gamepad inputs. # FIXME: Note that the commented-out gamepad inputs produce errors in the OnGamepadInput's # compute method because those specific inputs are not being considered. Maybe change to # include these inputs and/or not spit out scary-looking error messages to users? possible_gamepad_inputs = ThreadsafetyTestUtils.add_to_threading_cache( test_instance_id, [ GamepadInput.A, GamepadInput.B, # GamepadInput.COUNT, GamepadInput.DPAD_DOWN, GamepadInput.DPAD_LEFT, GamepadInput.DPAD_RIGHT, GamepadInput.DPAD_UP, GamepadInput.LEFT_SHOULDER, GamepadInput.LEFT_STICK, # GamepadInput.LEFT_STICK_DOWN, # GamepadInput.LEFT_STICK_LEFT, # GamepadInput.LEFT_STICK_RIGHT, # GamepadInput.LEFT_STICK_UP, # GamepadInput.LEFT_TRIGGER, GamepadInput.MENU1, GamepadInput.MENU2, GamepadInput.RIGHT_SHOULDER, GamepadInput.RIGHT_STICK, # GamepadInput.RIGHT_STICK_DOWN, # GamepadInput.RIGHT_STICK_LEFT, # GamepadInput.RIGHT_STICK_RIGHT, # GamepadInput.RIGHT_STICK_UP, # GamepadInput.RIGHT_TRIGGER, GamepadInput.X, GamepadInput.Y, ], ) # Define a dict of all valid inputs:gamepadElementIn tokens, along with # their corresponding gamepad input. NOTE: The node's allowed token names # are a bit different from the carb.input.GamepadInput values, might make # sense to have them be the same? allowed_gamepad_element_tokens = ThreadsafetyTestUtils.add_to_threading_cache( test_instance_id, { "Face Button Bottom": GamepadInput.A, "Face Button Right": GamepadInput.B, "Face Button Left": GamepadInput.X, "Face Button Top": GamepadInput.Y, "Left Shoulder": GamepadInput.LEFT_SHOULDER, "Right Shoulder": GamepadInput.RIGHT_SHOULDER, "Special Left": GamepadInput.MENU1, "Special Right": GamepadInput.MENU2, "Left Stick Button": GamepadInput.LEFT_STICK, "Right Stick Button": GamepadInput.RIGHT_STICK, "D-Pad Up": GamepadInput.DPAD_UP, "D-Pad Right": GamepadInput.DPAD_RIGHT, "D-Pad Down": GamepadInput.DPAD_DOWN, "D-Pad Left": GamepadInput.DPAD_LEFT, }, ) # Wrap main driver code in the following generator (in order to leverage # the ThreadsafetyTestUtils.make_threading_test decorator). Note that for simplicity's sake the # fake gamepad IDs correspond directly with their index in the gamepad_list. def _test_ongamepadinput_node(quick_run: bool = True, num_inputs_to_test: int = 5): _possible_gamepad_inputs = [] _allowed_gamepad_element_tokens = {} # Codepath for accelerated test (won't take as long and still provide some code coverage). if quick_run: # Make sure that the input flags make sense. if num_inputs_to_test < 1: num_inputs_to_test = 1 elif num_inputs_to_test > 14: num_inputs_to_test = 14 # Define two sets of random indices that'll determine the combination of inputs:gamepadElementIn # tokens and emulated key inputs that we'll test for the current function call. Note that the # inputs:gamepadElementIn tokens we choose and the emulated keys need not coincide, resulting # in no output being generated by the OnGamepadInput node. Also note that because we want to # use the same set of random indices in each test instance (so that all test graph instances # run their tests/comparisons using the same combination of buttons/inputs), we add said indices # (or more specifically an internal method that's used to create those indices, so that they're # only created once for all test instances) to the overall threading cache. def create_random_indices(): rand_indices_0 = set() while len(rand_indices_0) < num_inputs_to_test: rand_indices_0.add(random.randrange(len(possible_gamepad_inputs))) rand_indices_1 = set() while len(rand_indices_1) < num_inputs_to_test: rand_indices_1.add(random.randrange(len(allowed_gamepad_element_tokens))) return rand_indices_0, rand_indices_1 rand_indices_0, rand_indices_1 = ThreadsafetyTestUtils.add_to_threading_cache( test_instance_id, create_random_indices() ) # Convert the sets into lists so that their elements can be accessible by index. rand_indices_0 = list(rand_indices_0) rand_indices_1 = list(rand_indices_1) # Create an abbreviated list of possible gamepad input elements. for r_i in rand_indices_0: _possible_gamepad_inputs.append(possible_gamepad_inputs[r_i]) # Create an abbreviated dict of allowed token-key input pairs. temp_keys_list = list(allowed_gamepad_element_tokens) temp_values_list = list(allowed_gamepad_element_tokens.values()) for r_i in rand_indices_1: _allowed_gamepad_element_tokens[temp_keys_list[r_i]] = temp_values_list[r_i] # Codepath for full test. else: _possible_gamepad_inputs = possible_gamepad_inputs _allowed_gamepad_element_tokens = allowed_gamepad_element_tokens # Set the gamepad id on each OnGamepadInput node. Note that multiple gamepad nodes are set to # track the same gamepad to look for potential concurrency issues. # for on_gamepad_input_node in on_gamepad_input_nodes: in_gamepadid_attr.set(test_instance_id % len(gamepad_list)) # noqa S001 # Loop through each allowed gamepad element token, and set it on the OnGamepadInput nodes' # corresponding input attributes. for allowed_token, allowed_input in _allowed_gamepad_element_tokens.items(): in_gamepad_element_attr.set(allowed_token) # Loop through each possible gamepad input, which we will emulate. for emulated_input in _possible_gamepad_inputs: # Loop through each possible input event type (0 == key is released, 1 == key is pressed) for event_type in [1, 0]: # Trigger each gamepad input. input_provider.buffer_gamepad_event( gamepad_list[test_instance_id % len(gamepad_list)], emulated_input, event_type # noqa S001 ) yield ThreadsafetyTestUtils.EVALUATION_WAIT_FRAME # Yielding to compute by waiting for the next app frame. # If the current emulated input matches the inputs:gamepadElementsIn attribute setting, # check that the nodes reacted appropriately. Otherwise check that the nodes did not # register the input. if emulated_input == allowed_input: if event_type == 1: self.assertEqual(out_pressed_attr.get(), self.E) self.assertEqual(out_released_attr.get(), self.D) self.assertTrue(out_ispressed_attr.get()) elif event_type == 0: self.assertEqual(out_pressed_attr.get(), self.D) self.assertEqual(out_released_attr.get(), self.E) self.assertFalse(out_ispressed_attr.get()) else: self.assertEqual(out_pressed_attr.get(), self.D) self.assertEqual(out_released_attr.get(), self.D) self.assertFalse(out_ispressed_attr.get()) # Test that the OnGamepadInput nodes works correctly when the onlyPlayback input is disabled. in_onlyplayback_attr.set(False) for _ in _test_ongamepadinput_node(): yield ThreadsafetyTestUtils.EVALUATION_WAIT_FRAME # Yielding to compute by waiting for the next app frame. # Test that the OnGamepadInput nodes works correctly when the onlyPlayback input is enabled. in_onlyplayback_attr.set(True) timeline = omni.timeline.get_timeline_interface() timeline.set_target_framerate(timeline.get_time_codes_per_seconds()) timeline.play() for _ in _test_ongamepadinput_node(): yield ThreadsafetyTestUtils.EVALUATION_WAIT_FRAME # Yielding to compute by waiting for the next app frame. timeline.stop() # Delete the gamepad node before destroying the gamepads themselves so that the nodes don't throw # any warnings about having invalid gamepadIds. og.Controller.delete_node(on_gamepad_input_node) # Disconnect and destroy the gamepads. for gamepad in gamepad_list: ThreadsafetyTestUtils.single_evaluation_last_test_instance( test_instance_id, partial(input_provider.set_gamepad_connected, gamepad, False) ) yield ThreadsafetyTestUtils.EVALUATION_WAIT_FRAME # Yielding to compute by waiting for the next app frame. ThreadsafetyTestUtils.single_evaluation_last_test_instance( test_instance_id, partial(input_provider.destroy_gamepad, gamepad) ) yield ThreadsafetyTestUtils.EVALUATION_WAIT_FRAME # Yielding to compute by waiting for the next app frame. # ---------------------------------------------------------------------- # The OnImpulseEvent node ALSO has a built-in test construct in its .ogn file located # at ../../nodes/OgnOnImpulseEvent.ogn (relative to the source location of the currently- # opened testing script). @ThreadsafetyTestUtils.make_threading_test def test_onimpulseevent_node(self, test_instance_id: int = 0): """Test OnImpulseEvent node""" # Instance a test graph setup. Note that we append the graph path with the test_instance_id # so that the graph can be uniquely identified in the thread-safety test! graph_path = self.TEST_GRAPH_PATH + str(test_instance_id) og.Controller.create_graph({"graph_path": graph_path, "evaluator_name": "execution"}) (_, (_, counter_node), _, _,) = og.Controller.edit( graph_path, { self.keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("Counter", "omni.graph.action.Counter"), ], self.keys.SET_VALUES: [("OnImpulse.inputs:onlyPlayback", False)], self.keys.CONNECT: ( "OnImpulse.outputs:execOut", "Counter.inputs:execIn", ), }, ) counter_controller = og.Controller(og.Controller.attribute("outputs:count", counter_node)) # After several updates, there should have been no compute calls. yield # Yielding to wait for compute to happen across all graph instances before continuing the test. yield # Yielding to wait for compute to happen across all graph instances before continuing the test. yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(counter_controller.get(), 0) # Change the OnImpulse node's state attribute. The node should now request compute. og.Controller.edit(graph_path, {self.keys.SET_VALUES: (graph_path + "/OnImpulse.state:enableImpulse", True)}) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(counter_controller.get(), 1) # More updates should not result in more computes. yield # Yielding to wait for compute to happen across all graph instances before continuing the test. yield # Yielding to wait for compute to happen across all graph instances before continuing the test. yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(counter_controller.get(), 1) # ---------------------------------------------------------------------- async def test_onkeyboardinput_node(self): """Test OnKeyboardInput node""" app = omni.kit.app.get_app() og.Controller.create_graph({"graph_path": self.TEST_GRAPH_PATH, "evaluator_name": "execution"}) (_, (on_keyboard_input_node,), _, _) = og.Controller.edit( self.TEST_GRAPH_PATH, {self.keys.CREATE_NODES: ("OnKeyboardInput", "omni.graph.action.OnKeyboardInput")} ) # Obtain necessary attributes. in_keyin_attr = on_keyboard_input_node.get_attribute("inputs:keyIn") in_playbackonly_attr = on_keyboard_input_node.get_attribute("inputs:onlyPlayback") out_keyout_attr = on_keyboard_input_node.get_attribute("outputs:keyOut") out_pressed_attr = on_keyboard_input_node.get_attribute("outputs:pressed") out_released_attr = on_keyboard_input_node.get_attribute("outputs:released") out_ispressed_attr = on_keyboard_input_node.get_attribute("outputs:isPressed") # Obtain an interface to the keyboard and carb input provider. keyboard = omni.appwindow.get_default_app_window().get_keyboard() input_provider = carb.input.acquire_input_provider() self.assertIsNotNone(keyboard) # Define a list of all possible carb.input.KeyboardInput inputs. Note that not all possible # keys are necessarily detectable by the OnKeyboardInput node (this list also includes the # UNKNOWN key). possible_key_inputs = [ KeyboardInput.A, KeyboardInput.APOSTROPHE, KeyboardInput.B, KeyboardInput.BACKSLASH, KeyboardInput.BACKSPACE, KeyboardInput.C, KeyboardInput.CAPS_LOCK, KeyboardInput.COMMA, KeyboardInput.D, KeyboardInput.DEL, KeyboardInput.DOWN, KeyboardInput.E, KeyboardInput.END, KeyboardInput.ENTER, KeyboardInput.EQUAL, KeyboardInput.ESCAPE, KeyboardInput.F, KeyboardInput.F1, KeyboardInput.F10, KeyboardInput.F11, KeyboardInput.F12, KeyboardInput.F2, KeyboardInput.F3, KeyboardInput.F4, KeyboardInput.F5, KeyboardInput.F6, KeyboardInput.F7, KeyboardInput.F8, KeyboardInput.F9, KeyboardInput.G, KeyboardInput.GRAVE_ACCENT, KeyboardInput.H, KeyboardInput.HOME, KeyboardInput.I, KeyboardInput.INSERT, KeyboardInput.J, KeyboardInput.K, KeyboardInput.KEY_0, KeyboardInput.KEY_1, KeyboardInput.KEY_2, KeyboardInput.KEY_3, KeyboardInput.KEY_4, KeyboardInput.KEY_5, KeyboardInput.KEY_6, KeyboardInput.KEY_7, KeyboardInput.KEY_8, KeyboardInput.KEY_9, KeyboardInput.L, KeyboardInput.LEFT, KeyboardInput.LEFT_ALT, KeyboardInput.LEFT_BRACKET, KeyboardInput.LEFT_CONTROL, KeyboardInput.LEFT_SHIFT, KeyboardInput.LEFT_SUPER, KeyboardInput.M, KeyboardInput.MENU, KeyboardInput.MINUS, KeyboardInput.N, KeyboardInput.NUMPAD_0, KeyboardInput.NUMPAD_1, KeyboardInput.NUMPAD_2, KeyboardInput.NUMPAD_3, KeyboardInput.NUMPAD_4, KeyboardInput.NUMPAD_5, KeyboardInput.NUMPAD_6, KeyboardInput.NUMPAD_7, KeyboardInput.NUMPAD_8, KeyboardInput.NUMPAD_9, KeyboardInput.NUMPAD_ADD, KeyboardInput.NUMPAD_DEL, KeyboardInput.NUMPAD_DIVIDE, KeyboardInput.NUMPAD_ENTER, KeyboardInput.NUMPAD_EQUAL, KeyboardInput.NUMPAD_MULTIPLY, KeyboardInput.NUMPAD_SUBTRACT, KeyboardInput.NUM_LOCK, KeyboardInput.O, KeyboardInput.P, KeyboardInput.PAGE_DOWN, KeyboardInput.PAGE_UP, KeyboardInput.PAUSE, KeyboardInput.PERIOD, KeyboardInput.PRINT_SCREEN, KeyboardInput.Q, KeyboardInput.R, KeyboardInput.RIGHT, KeyboardInput.RIGHT_ALT, KeyboardInput.RIGHT_BRACKET, KeyboardInput.RIGHT_CONTROL, KeyboardInput.RIGHT_SHIFT, KeyboardInput.RIGHT_SUPER, KeyboardInput.S, KeyboardInput.SCROLL_LOCK, KeyboardInput.SEMICOLON, KeyboardInput.SLASH, KeyboardInput.SPACE, KeyboardInput.T, KeyboardInput.TAB, KeyboardInput.U, KeyboardInput.UNKNOWN, KeyboardInput.UP, KeyboardInput.V, KeyboardInput.W, KeyboardInput.X, KeyboardInput.Y, KeyboardInput.Z, ] # Define a dictionary of token keys representing the possible inputs to the OnKeyboardInput node's # "keyIn" attribute, and values representing the corresponding carb.input.KeyboardInput. Note that # not all possible keys are necessarily allowed for detection by the OnKeyboardInput node (e.g. # "Unknown") allowed_token_key_inputs = { "A": KeyboardInput.A, "B": KeyboardInput.B, "C": KeyboardInput.C, "D": KeyboardInput.D, "E": KeyboardInput.E, "F": KeyboardInput.F, "G": KeyboardInput.G, "H": KeyboardInput.H, "I": KeyboardInput.I, "J": KeyboardInput.J, "K": KeyboardInput.K, "L": KeyboardInput.L, "M": KeyboardInput.M, "N": KeyboardInput.N, "O": KeyboardInput.O, "P": KeyboardInput.P, "Q": KeyboardInput.Q, "R": KeyboardInput.R, "S": KeyboardInput.S, "T": KeyboardInput.T, "U": KeyboardInput.U, "V": KeyboardInput.V, "W": KeyboardInput.W, "X": KeyboardInput.X, "Y": KeyboardInput.Y, "Z": KeyboardInput.Z, "Apostrophe": KeyboardInput.APOSTROPHE, "Backslash": KeyboardInput.BACKSLASH, "Backspace": KeyboardInput.BACKSPACE, "CapsLock": KeyboardInput.CAPS_LOCK, "Comma": KeyboardInput.COMMA, "Del": KeyboardInput.DEL, "Down": KeyboardInput.DOWN, "End": KeyboardInput.END, "Enter": KeyboardInput.ENTER, "Equal": KeyboardInput.EQUAL, "Escape": KeyboardInput.ESCAPE, "F1": KeyboardInput.F1, "F10": KeyboardInput.F10, "F11": KeyboardInput.F11, "F12": KeyboardInput.F12, "F2": KeyboardInput.F2, "F3": KeyboardInput.F3, "F4": KeyboardInput.F4, "F5": KeyboardInput.F5, "F6": KeyboardInput.F6, "F7": KeyboardInput.F7, "F8": KeyboardInput.F8, "F9": KeyboardInput.F9, "GraveAccent": KeyboardInput.GRAVE_ACCENT, "Home": KeyboardInput.HOME, "Insert": KeyboardInput.INSERT, "Key0": KeyboardInput.KEY_0, "Key1": KeyboardInput.KEY_1, "Key2": KeyboardInput.KEY_2, "Key3": KeyboardInput.KEY_3, "Key4": KeyboardInput.KEY_4, "Key5": KeyboardInput.KEY_5, "Key6": KeyboardInput.KEY_6, "Key7": KeyboardInput.KEY_7, "Key8": KeyboardInput.KEY_8, "Key9": KeyboardInput.KEY_9, "Left": KeyboardInput.LEFT, "LeftAlt": KeyboardInput.LEFT_ALT, "LeftBracket": KeyboardInput.LEFT_BRACKET, "LeftControl": KeyboardInput.LEFT_CONTROL, "LeftShift": KeyboardInput.LEFT_SHIFT, "LeftSuper": KeyboardInput.LEFT_SUPER, "Menu": KeyboardInput.MENU, "Minus": KeyboardInput.MINUS, "NumLock": KeyboardInput.NUM_LOCK, "Numpad0": KeyboardInput.NUMPAD_0, "Numpad1": KeyboardInput.NUMPAD_1, "Numpad2": KeyboardInput.NUMPAD_2, "Numpad3": KeyboardInput.NUMPAD_3, "Numpad4": KeyboardInput.NUMPAD_4, "Numpad5": KeyboardInput.NUMPAD_5, "Numpad6": KeyboardInput.NUMPAD_6, "Numpad7": KeyboardInput.NUMPAD_7, "Numpad8": KeyboardInput.NUMPAD_8, "Numpad9": KeyboardInput.NUMPAD_9, "NumpadAdd": KeyboardInput.NUMPAD_ADD, "NumpadDel": KeyboardInput.NUMPAD_DEL, "NumpadDivide": KeyboardInput.NUMPAD_DIVIDE, "NumpadEnter": KeyboardInput.NUMPAD_ENTER, "NumpadEqual": KeyboardInput.NUMPAD_EQUAL, "NumpadMultiply": KeyboardInput.NUMPAD_MULTIPLY, "NumpadSubtract": KeyboardInput.NUMPAD_SUBTRACT, "PageDown": KeyboardInput.PAGE_DOWN, "PageUp": KeyboardInput.PAGE_UP, "Pause": KeyboardInput.PAUSE, "Period": KeyboardInput.PERIOD, "PrintScreen": KeyboardInput.PRINT_SCREEN, "Right": KeyboardInput.RIGHT, "RightAlt": KeyboardInput.RIGHT_ALT, "RightBracket": KeyboardInput.RIGHT_BRACKET, "RightControl": KeyboardInput.RIGHT_CONTROL, "RightShift": KeyboardInput.RIGHT_SHIFT, "RightSuper": KeyboardInput.RIGHT_SUPER, "ScrollLock": KeyboardInput.SCROLL_LOCK, "Semicolon": KeyboardInput.SEMICOLON, "Slash": KeyboardInput.SLASH, "Space": KeyboardInput.SPACE, "Tab": KeyboardInput.TAB, "Up": KeyboardInput.UP, } # Define a list of all possible keyboard event types (for convenience). # We won't consider KEY_REPEAT and CHAR events here. keyboard_event_types = [ KeyboardEventType.KEY_PRESS, KeyboardEventType.KEY_RELEASE, ] # Create a list of all possible keyboard modifier combinations. Don't need permutations # since bitwise OR operator is commutative and associative. modifier_combinations = [ 0, # 0 carb.input.KEYBOARD_MODIFIER_FLAG_SHIFT, # 1 carb.input.KEYBOARD_MODIFIER_FLAG_CONTROL, # 2 carb.input.KEYBOARD_MODIFIER_FLAG_SHIFT | carb.input.KEYBOARD_MODIFIER_FLAG_CONTROL, # 3 carb.input.KEYBOARD_MODIFIER_FLAG_ALT, # 4 carb.input.KEYBOARD_MODIFIER_FLAG_SHIFT | carb.input.KEYBOARD_MODIFIER_FLAG_ALT, # 5 carb.input.KEYBOARD_MODIFIER_FLAG_ALT | carb.input.KEYBOARD_MODIFIER_FLAG_CONTROL, # 6 carb.input.KEYBOARD_MODIFIER_FLAG_SHIFT | carb.input.KEYBOARD_MODIFIER_FLAG_ALT | carb.input.KEYBOARD_MODIFIER_FLAG_CONTROL, # 7 ] # Create a list of tuples of lists of tuples and indices of all possible input key modifier # settings on the OnKeyboardInput node. node_input_modifier_attribute_combinations = [ ([("inputs:shiftIn", False), ("inputs:altIn", False), ("inputs:ctrlIn", False)], 0), ([("inputs:shiftIn", True), ("inputs:altIn", False), ("inputs:ctrlIn", False)], 1), ([("inputs:shiftIn", False), ("inputs:altIn", False), ("inputs:ctrlIn", True)], 2), ([("inputs:shiftIn", True), ("inputs:altIn", False), ("inputs:ctrlIn", True)], 3), ([("inputs:shiftIn", False), ("inputs:altIn", True), ("inputs:ctrlIn", False)], 4), ([("inputs:shiftIn", True), ("inputs:altIn", True), ("inputs:ctrlIn", False)], 5), ([("inputs:shiftIn", False), ("inputs:altIn", True), ("inputs:ctrlIn", True)], 6), ([("inputs:shiftIn", True), ("inputs:altIn", True), ("inputs:ctrlIn", True)], 7), ] # NOTE: Although the below test confirms that the OnKeyboardInput node works for all # input combinations, it takes a while to run and times out the test (which is typically # set to automatically crash after 300s). To account for this, each time we run the test # a random subset of allowed input tokens and input keys are chosen with which # we perform the test; this cuts down on computation time and still provides some # decent code coverage. # Wrap main test driver code in the following method. Note that in addition to testing # whether specific buttons activate their corresponding code path, we also check that # no other buttons can activate code paths they don't belong to. Check for all possible # key + special modifier press/release permutations. async def _test_onkeyboardinput_node( quick_run: bool = True, num_keys_to_test: int = 10, num_modifiers_to_test: int = 1 ): _possible_key_inputs = [] _allowed_token_key_inputs = {} _modifier_combinations = [] _node_input_modifier_attribute_combinations = [] # Codepath for accelerated test (won't time out and still provide some code coverage). if quick_run: # Make sure that the input flags make sense. if num_keys_to_test < 1: num_keys_to_test = 1 elif num_keys_to_test > 105: num_keys_to_test = 105 if num_modifiers_to_test < 1: num_modifiers_to_test = 1 elif num_modifiers_to_test > 8: num_modifiers_to_test = 8 # Define three sets of random indices that'll determine the combination of inputs:keyIn # tokens, emulated key inputs, and modifier values that we'll test for the current function call. # Note that the inputs:keyIn tokens we choose and the emulated keys need not coincide, resulting # in no output being generated by the OnKeyboardInput node. rand_indices_0 = set() while len(rand_indices_0) < num_keys_to_test: rand_indices_0.add(random.randrange(len(possible_key_inputs))) rand_indices_1 = set() while len(rand_indices_1) < num_keys_to_test: rand_indices_1.add(random.randrange(len(allowed_token_key_inputs))) rand_indices_2 = set() while len(rand_indices_2) < num_modifiers_to_test: rand_indices_2.add(random.randrange(len(modifier_combinations))) # Convert the sets into lists so that their elements can be accessible by index. rand_indices_0 = list(rand_indices_0) rand_indices_1 = list(rand_indices_1) rand_indices_2 = list(rand_indices_2) # Create an abbreviated list of possible key inputs. for i in range(0, num_keys_to_test): _possible_key_inputs.append(possible_key_inputs[rand_indices_0[i]]) # Create an abbreviated dict of allowed token-key input pairs. temp_keys_list = list(allowed_token_key_inputs) temp_values_list = list(allowed_token_key_inputs.values()) for i in range(0, num_keys_to_test): _allowed_token_key_inputs[temp_keys_list[rand_indices_1[i]]] = temp_values_list[rand_indices_1[i]] # Create abbreviated lists of modifier values and corresponing input attribute-value pairs. for rand_idx in rand_indices_2: _modifier_combinations.append(modifier_combinations[rand_idx]) _node_input_modifier_attribute_combinations.append( node_input_modifier_attribute_combinations[rand_idx] ) # Codepath for full test. else: _possible_key_inputs = possible_key_inputs _allowed_token_key_inputs = allowed_token_key_inputs _modifier_combinations = modifier_combinations _node_input_modifier_attribute_combinations = node_input_modifier_attribute_combinations # Loop through each possible inputs:keyIn token, and the set the inputs:keyIn attribute on the # OnKeyboardInput node. for token, _ in _allowed_token_key_inputs.items(): # noqa PLR1702 in_keyin_attr.set(token) # Loop through each possible modification combination, and set the corresponding input attributes # on the OnKeyboardInput node. Also store an index to represent the current modification state. for node_input_modifier_attribute_tuple in _node_input_modifier_attribute_combinations: node_input_modifier_attribute_list_in_tuple = node_input_modifier_attribute_tuple[0] for input_attr_value_modifier_pair in node_input_modifier_attribute_list_in_tuple: og.Controller.set( og.Controller.attribute(input_attr_value_modifier_pair[0], on_keyboard_input_node), input_attr_value_modifier_pair[1], ) # Loop through each possible input key. for key in _possible_key_inputs: # Loop through all possible modification combinations. for modifier in _modifier_combinations: # Loop through each possible keyboard event type. for event_type in keyboard_event_types: # Trigger the current keyboard event. input_provider.buffer_keyboard_key_event(keyboard, event_type, key, modifier) await app.next_update_async() # If the currently-pressed key matches the currently-set inputs:keyIn token's # corresponding KeyboardInput + modifiers, check that the OnKeyboardInput node gets # correctly activated. if ( _allowed_token_key_inputs[token] == key # noqa PLR1733 and node_input_modifier_attribute_tuple[1] == modifier ): # If the key has been pressed, check for the corresponding expected conditions. if event_type == KeyboardEventType.KEY_PRESS: self.assertEqual(out_keyout_attr.get(), token) self.assertEqual(out_pressed_attr.get(), self.E) self.assertEqual(out_released_attr.get(), self.D) self.assertTrue(out_ispressed_attr.get()) # If the key has been released, check for the corresponding expected conditions. elif event_type == KeyboardEventType.KEY_RELEASE: self.assertEqual(out_keyout_attr.get(), token) self.assertEqual(out_pressed_attr.get(), self.D) self.assertEqual(out_released_attr.get(), self.E) self.assertFalse(out_ispressed_attr.get()) else: self.assertEqual(out_pressed_attr.get(), self.D) self.assertEqual(out_released_attr.get(), self.D) self.assertFalse(out_ispressed_attr.get()) # Test that the OnKeyboardInput node works correctly when its onlyPlayback input is disabled. in_playbackonly_attr.set(False) await _test_onkeyboardinput_node() # Test that the OnKeyboardInput node works correctly when its onlyPlayback input is enabled. in_playbackonly_attr.set(True) timeline = omni.timeline.get_timeline_interface() timeline.set_target_framerate(timeline.get_time_codes_per_seconds()) timeline.play() await _test_onkeyboardinput_node() timeline.stop() # ---------------------------------------------------------------------- # NOTE: Even though the OnLoaded node is threadsafe (its compute method is very simple), # we don't adapt the below test to check for thread-safety conditions because it relies # on other nodes (omni.graph.action.SendCustomEvent) which are NOT threadsafe. async def test_onloaded_node(self): """Test OnLoaded node""" def registered_event_name(event_name): """Returns the internal name used for the given custom event name""" name = "omni.graph.action." + event_name return carb.events.type_from_string(name) events = [] def on_event(event): events.append(event.payload["!path"]) reg_event_name = registered_event_name("foo") message_bus = omni.kit.app.get_app().get_message_bus_event_stream() sub = message_bus.create_subscription_to_push_by_type(reg_event_name, on_event) self.assertIsNotNone(sub) og.Controller.create_graph({"graph_path": self.TEST_GRAPH_PATH, "evaluator_name": "execution"}) og.Controller.edit( self.TEST_GRAPH_PATH, { self.keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("OnLoaded", "omni.graph.action.OnLoaded"), ("Send1", "omni.graph.action.SendCustomEvent"), ("Send2", "omni.graph.action.SendCustomEvent"), ], self.keys.CONNECT: [ ("OnLoaded.outputs:execOut", "Send1.inputs:execIn"), ("OnTick.outputs:tick", "Send2.inputs:execIn"), ], self.keys.SET_VALUES: [ ("OnTick.inputs:onlyPlayback", False), ("Send1.inputs:eventName", "foo"), ("Send2.inputs:eventName", "foo"), ("Send1.inputs:path", "Loaded"), ("Send2.inputs:path", "Tick"), ], }, ) # Evaluate once so that graph is in steady state. await og.Controller.evaluate() # Verify Loaded came before OnTick. self.assertListEqual(events, ["Loaded", "Tick"]) # ---------------------------------------------------------------------- async def test_onmessagebusevent_node(self): """Test OnMessageBusEvent node""" og.Controller.create_graph({"graph_path": self.TEST_GRAPH_PATH, "evaluator_name": "execution"}) (_, (on_custom_event, _), _, _,) = og.Controller.edit( self.TEST_GRAPH_PATH, { self.keys.CREATE_NODES: [ ("OnCustomEvent", "omni.graph.action.OnMessageBusEvent"), ("Counter1", "omni.graph.action.Counter"), ], self.keys.CONNECT: [ ("OnCustomEvent.outputs:execOut", "Counter1.inputs:execIn"), ], self.keys.SET_VALUES: [ ("OnCustomEvent.inputs:onlyPlayback", False), ("OnCustomEvent.inputs:eventName", "testEvent"), ], }, ) # One compute for the first-time subscribe. await omni.kit.app.get_app().next_update_async() def get_all_supported_types() -> List[str]: """Helper to get all the types supported by the node""" types = [] for attr_type in ogn.supported_attribute_type_names(): if ( attr_type in ("any", "transform", "bundle", "target", "execution") or (attr_type[:9] == "transform") or attr_type.startswith("objectId") or attr_type.startswith("frame") ): continue types.append(attr_type) return types def assert_are_equal(expected_val, val): """Helper to assert two values are equal, sequence container type need not match""" if isinstance(expected_val, (list, tuple, np.ndarray)): for left, right in zip(expected_val, val): return assert_are_equal(left, right) if isinstance(val, np.ndarray): self.assertListEqual(expected_val, list(val)) else: self.assertEqual(expected_val, val) return True msg = carb.events.type_from_string("testEvent") payload = {} expected_vals = [] for sup_type in sorted(get_all_supported_types()): payload = {} name = sup_type.replace("[", "_").replace("]", "_") manager = ogn.get_attribute_manager_type(sup_type) # Create a dynamic output attribute on the node which matches the type of the test payload. og_type = og.AttributeType.type_from_ogn_type_name(sup_type) attrib = og.Controller.create_attribute( on_custom_event, f"outputs:{name}", sup_type, og.AttributePortType.ATTRIBUTE_PORT_TYPE_OUTPUT ) # Get a sample value in python format (nested tuples/lists). sample_val = manager.sample_values()[0] is_array = og_type.array_depth > 0 is_tuple = og_type.tuple_count > 1 is_matrix = is_tuple and ( og_type.role in (og.AttributeRole.FRAME, og.AttributeRole.MATRIX, og.AttributeRole.TRANSFORM) ) payload_val = sample_val # Convert the sample value into numpy format for use with OG API. if is_array: if is_matrix: payload_val = np.array(sample_val).flatten().flatten().tolist() elif is_tuple: payload_val = np.array(sample_val).flatten().tolist() elif is_matrix: payload_val = np.array(sample_val).flatten().tolist() payload[name] = payload_val expected_vals.append((attrib, sample_val)) # Push the message to kit message bus. omni.kit.app.get_app().get_message_bus_event_stream().push(msg, payload=payload) # Wait for one kit update to allow the event-push mechanism to trigger the node callback. await omni.kit.app.get_app().next_update_async() # Verify the value. out_val = og.Controller.get(attrib) try: assert_are_equal(out_val, out_val) except AssertionError as exc: raise AssertionError(f"{sample_val} != {out_val}") from exc
43,381
Python
49.561772
131
0.577234
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/tests/test_evaluation2.py
"""Action Graph Evaluation Tests Part 2""" import json import omni.graph.core as og import omni.graph.core.tests as ogts import omni.usd # ====================================================================== class TestActionGraphEvaluation2(ogts.OmniGraphTestCase): """Tests action graph evaluator functionality""" TEST_GRAPH_PATH = "/World/TestGraph" async def setUp(self): """Set up test environment, to be torn down when done""" await super().setUp() og.Controller.edit({"graph_path": self.TEST_GRAPH_PATH, "evaluator_name": "execution"}) # ---------------------------------------------------------------------- async def test_retrigger_latent(self): """Test that latent nodes can be re-triggered""" want_debug = False e_state = og.ExecutionAttributeState tick_count = 0 boop_count = 0 exec_in_latent_count = 0 max_ticks = 20 class CancelTickerPy: """Helper node type which does latent ticking and can be canceled, and has an independent counter "boop" """ @staticmethod def compute(context: og.GraphContext, node: og.Node): nonlocal tick_count nonlocal boop_count nonlocal exec_in_latent_count exec_in = node.get_attribute("inputs:execIn") exec_in_val = og.Controller.get(exec_in) cancel = node.get_attribute("inputs:cancel") cancel_val = og.Controller.get(cancel) boop = node.get_attribute("inputs:boop") boop_val = og.Controller.get(boop) want_debug and print( f"### {tick_count} execIn={exec_in_val} cancel={cancel_val} boop={boop_val}" ) # noqa: expression-not-assigned if cancel_val == e_state.ENABLED: # Finish latent by cancel og.Controller.set(node.get_attribute("outputs:canceled"), e_state.LATENT_FINISH) self.assertEqual(exec_in_val, e_state.DISABLED) self.assertEqual(boop_val, e_state.DISABLED) tick_count = 0 return True if exec_in_val == e_state.ENABLED: self.assertEqual(cancel_val, e_state.DISABLED) self.assertEqual(boop_val, e_state.DISABLED) if tick_count > 0: # execIn triggered while in latent - should not be possible exec_in_latent_count += 1 else: og.Controller.set(node.get_attribute("outputs:tick"), e_state.LATENT_PUSH) return True # we are ticking self.assertEqual(cancel_val, e_state.DISABLED) tick_count += 1 if tick_count < max_ticks: og.Controller.set(node.get_attribute("outputs:tick"), e_state.ENABLED) else: # Finish latent naturally og.Controller.set(node.get_attribute("outputs:execOut"), e_state.LATENT_FINISH) tick_count = 0 if boop_val == e_state.ENABLED: # We get here during latent ticking, if the boop input is enabled self.assertEqual(exec_in_val, e_state.DISABLED) self.assertEqual(cancel_val, e_state.DISABLED) boop_count += 1 return True @staticmethod def get_node_type() -> str: return "omni.graph.test.CancelTickerPy" @staticmethod def initialize_type(node_type: og.NodeType): node_type.add_input( "inputs:execIn", "execution", True, ) node_type.add_input( "inputs:cancel", "execution", True, ) node_type.add_input( "inputs:boop", "execution", True, ) node_type.add_output("outputs:tick", "execution", True) node_type.add_output("outputs:canceled", "execution", True) node_type.add_output("outputs:execOut", "execution", True) return True og.register_node_type(CancelTickerPy, 1) controller = og.Controller() keys = og.Controller.Keys (graph, (ticker, start, _, cancel, boop, _, _, counter), _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("Ticker", "omni.graph.test.CancelTickerPy"), ("Start", "omni.graph.action.OnImpulseEvent"), ("Start2", "omni.graph.action.OnImpulseEvent"), ("Cancel", "omni.graph.action.OnImpulseEvent"), ("Boop", "omni.graph.action.OnImpulseEvent"), ("Once", "omni.graph.action.Once"), ("Once2", "omni.graph.action.Once"), ("Counter", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("Start.outputs:execOut", "Ticker.inputs:execIn"), ("Start2.outputs:execOut", "Ticker.inputs:execIn"), ("Cancel.outputs:execOut", "Once.inputs:execIn"), ("Once.outputs:once", "Ticker.inputs:cancel"), ("Once.outputs:after", "Ticker.inputs:cancel"), ("Cancel.outputs:execOut", "Once2.inputs:execIn"), ("Boop.outputs:execOut", "Ticker.inputs:boop"), ("Once2.outputs:once", "Ticker.inputs:cancel"), ("Once2.outputs:after", "Ticker.inputs:cancel"), ("Ticker.outputs:tick", "Counter.inputs:execIn"), ], keys.SET_VALUES: [ ("Start.inputs:onlyPlayback", False), ("Start2.inputs:onlyPlayback", False), ("Cancel.inputs:onlyPlayback", False), ("Boop.inputs:onlyPlayback", False), ], }, ) # cancel, check nothing happens og.Controller.set(controller.attribute("state:enableImpulse", cancel), True) await controller.evaluate(graph) exec_out = og.Controller.get(controller.attribute("outputs:tick", ticker)) self.assertEqual(exec_out, e_state.DISABLED) # start ticking og.Controller.set(controller.attribute("state:enableImpulse", start), True) await controller.evaluate(graph) # Starts latent state await controller.evaluate(graph) # Tick 1 self.assertEqual(tick_count, 1) # Verify the tick has started exec_out = og.Controller.get(controller.attribute("outputs:tick", ticker)) self.assertEqual(exec_out, e_state.ENABLED) await controller.evaluate(graph) # Tick 2 self.assertEqual(tick_count, 2) exec_out = og.Controller.get(controller.attribute("outputs:tick", ticker)) self.assertEqual(exec_out, e_state.ENABLED) await controller.evaluate(graph) # Tick 3 self.assertEqual(tick_count, 3) # Boop - node keeps ticking og.Controller.set(controller.attribute("state:enableImpulse", boop), True) # Boop will trigger a compute, which increments boop + ticks AND the normal latent tick await controller.evaluate(graph) self.assertEqual(boop_count, 1) self.assertEqual(tick_count, 5) # Now check that the next tick can run WITHOUT inputs:boop being high await controller.evaluate(graph) self.assertEqual(boop_count, 1) # No change in boop count (OM-64856) self.assertEqual(tick_count, 6) # Now check that we can't re-trigger execIn self.assertEqual(exec_in_latent_count, 0) og.Controller.set(controller.attribute("state:enableImpulse", start), True) # Start will not trigger any compute because the node is latent await controller.evaluate(graph) self.assertEqual(exec_in_latent_count, 0) self.assertEqual(boop_count, 1) self.assertEqual(tick_count, 7) # Now check the normal tick proceeds as normal await controller.evaluate(graph) self.assertEqual(boop_count, 1) self.assertEqual(tick_count, 8) # Cancel counter_attr = controller.attribute("outputs:count", counter) count_0 = og.Controller.get(counter_attr) og.Controller.set(controller.attribute("state:enableImpulse", cancel), True) await controller.evaluate(graph) # latent finish await controller.evaluate(graph) # no action await controller.evaluate(graph) # no action count_1 = og.Controller.get(counter_attr) self.assertEqual(count_0 + 1, count_1) # ---------------------------------------------------------------------- async def test_cycle_break(self): """test that an illegal cycle issues a warning""" controller = og.Controller() keys = og.Controller.Keys (graph, (on_impulse, count_a, count_b), _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("A", "omni.graph.action.Counter"), ("B", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("OnImpulse.outputs:execOut", "A.inputs:execIn"), ("A.outputs:execOut", "B.inputs:execIn"), ("B.outputs:execOut", "A.inputs:execIn"), ], keys.SET_VALUES: [ ("OnImpulse.state:enableImpulse", True), ("OnImpulse.inputs:onlyPlayback", False), ], }, ) with ogts.ExpectedError(): await controller.evaluate(graph) og.Controller.set(controller.attribute("state:enableImpulse", on_impulse), True) with ogts.ExpectedError(): await controller.evaluate(graph) self.assertEqual(og.Controller.get(controller.attribute("outputs:count", count_a)), 2) self.assertEqual(og.Controller.get(controller.attribute("outputs:count", count_b)), 2) # ---------------------------------------------------------------------- async def test_dep_sort_fan_out(self): """Test that dependency sort works when there is data fan-out""" # +-------------+ # +-------->| | # | | SwitchTokenA| # | +--->+-------------+ # +----------+ | # |OnImpulse + | +--------------+ # +----------+ | | SwitchTokenB | # | +^-------------+ # +------+-+ +--------+ | # | ConstA +--->AppendB +---+ # +--------+ +--------+ controller = og.Controller() keys = og.Controller.Keys (graph, (_, const_a, _, switch_a, _), _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("ConstA", "omni.graph.nodes.ConstantToken"), ("AppendB", "omni.graph.nodes.AppendString"), ("SwitchTokenA", "omni.graph.action.SwitchToken"), ("SwitchTokenB", "omni.graph.action.SwitchToken"), ], keys.CONNECT: [ ("OnImpulse.outputs:execOut", "SwitchTokenA.inputs:execIn"), ("ConstA.inputs:value", "SwitchTokenA.inputs:value"), ("ConstA.inputs:value", "AppendB.inputs:value"), ("AppendB.outputs:value", "SwitchTokenB.inputs:value"), ], keys.SET_VALUES: [ ("OnImpulse.inputs:onlyPlayback", False), ("OnImpulse.state:enableImpulse", True), ("AppendB.inputs:suffix", {"value": "Foo", "type": "token"}), ], }, ) await controller.evaluate(graph) graph_state = og.OmniGraphInspector().as_json(graph, flags=["evaluation"]) graph_state_obj = json.loads(graph_state) trace = graph_state_obj["Evaluator"]["LastNonEmptyEvaluation"]["Trace"] # Verify the evaluation trace includes exactly what we expect expected_trace = [ const_a.get_prim_path(), switch_a.get_prim_path(), ] self.assertListEqual(expected_trace, trace) # ---------------------------------------------------------------------- async def test_exec_fan_out_shared_deps(self): """Test that dependency sort works when there is shared data in exec fan-out""" # +---------+ # +---------->| Write1 | # | +----^----+ # | | # | +----------+ # | | # +-----------+ | | # | OnImpulse +-----+-----+----> +---------+ # +-----------+ | | | Write2 | # | +----->+---------+ # | | # | | +---------+ # +-----+----->| Write3 | # | +---------+ # | ^ # +-------+ +---+----+---+ # | Const +----->| Inc | # +-------+ +--------+ controller = og.Controller() keys = og.Controller.Keys (graph, _, _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("Const", "omni.graph.nodes.ConstantDouble"), ("Inc", "omni.graph.nodes.Increment"), ("Write1", "omni.graph.nodes.WritePrimAttribute"), ("Write2", "omni.graph.nodes.WritePrimAttribute"), ("Write3", "omni.graph.nodes.WritePrimAttribute"), ], keys.CREATE_PRIMS: [ ("/World/TestPrim1", {"val": ("double", 1.0)}), ("/World/TestPrim2", {"val": ("double", 2.0)}), ("/World/TestPrim3", {"val": ("double", 3.0)}), ], keys.CONNECT: [ ("OnImpulse.outputs:execOut", "Write1.inputs:execIn"), ("OnImpulse.outputs:execOut", "Write2.inputs:execIn"), ("OnImpulse.outputs:execOut", "Write3.inputs:execIn"), ("Const.inputs:value", "Inc.inputs:value"), ("Inc.outputs:result", "Write1.inputs:value"), ("Inc.outputs:result", "Write2.inputs:value"), ("Inc.outputs:result", "Write3.inputs:value"), ], keys.SET_VALUES: [ ("OnImpulse.inputs:onlyPlayback", False), ("OnImpulse.state:enableImpulse", True), ("Const.inputs:value", 41.0), ("Inc.inputs:increment", 1.0), ("Write1.inputs:primPath", "/World/TestPrim1"), ("Write1.inputs:usePath", True), ("Write1.inputs:name", "val"), ("Write2.inputs:primPath", "/World/TestPrim2"), ("Write2.inputs:usePath", True), ("Write2.inputs:name", "val"), ("Write3.inputs:primPath", "/World/TestPrim3"), ("Write3.inputs:usePath", True), ("Write3.inputs:name", "val"), ], }, ) await controller.evaluate(graph) stage = omni.usd.get_context().get_stage() for i in (1, 2, 3): self.assertEqual(stage.GetAttributeAtPath(f"/World/TestPrim{i}.val").Get(), 42.0) # ---------------------------------------------------------------------- async def test_exec_fan_out_shared_deps2(self): """Test that dependency sort works when there is shared data in exec fan-out""" # ┌───────┐ ┌────────┐ # │Const1 ├───────────►│Append1 │ # └───────┘ │ ├──────────►┌───────────┐ # ┌──►└────────┘ │ WriteVar1 │ # ┌─────────────┐ │ ┌────►└───────────┘ # │ GraphTarget ├───┤ │ # └─────────────┘ └─►┌─────────┐ │ ┌───────────┐ # │ Append2 ├─────┼────►│ WriteVar2 │ # ┌────────┐ ┌─►└─────────┘ │ └───────────┘ # │ Const2 ├────────┘ │ ▲ # └────────┘ │ │ # │ │ # ┌──────────┐ │ │ # │ OnImpulse├────┴──────┘ # └──────────┘ controller = og.Controller() keys = og.Controller.Keys (graph, _, _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("Const1", "omni.graph.nodes.ConstantToken"), ("Const2", "omni.graph.nodes.ConstantToken"), ("Append1", "omni.graph.nodes.AppendPath"), ("Append2", "omni.graph.nodes.AppendPath"), ("GraphTarget", "omni.graph.nodes.GraphTarget"), ("WriteVar1", "omni.graph.core.WriteVariable"), ("WriteVar2", "omni.graph.core.WriteVariable"), ], keys.CREATE_VARIABLES: [ ("path1", og.Type(og.BaseDataType.TOKEN)), ("path2", og.Type(og.BaseDataType.TOKEN)), ], keys.CONNECT: [ ("OnImpulse.outputs:execOut", "WriteVar1.inputs:execIn"), ("OnImpulse.outputs:execOut", "WriteVar2.inputs:execIn"), ("GraphTarget.outputs:primPath", "Append1.inputs:path"), ("GraphTarget.outputs:primPath", "Append2.inputs:path"), ("Const1.inputs:value", "Append1.inputs:suffix"), ("Const2.inputs:value", "Append2.inputs:suffix"), ("Append1.outputs:path", "WriteVar1.inputs:value"), ("Append2.outputs:path", "WriteVar2.inputs:value"), ], keys.SET_VALUES: [ ("WriteVar1.inputs:variableName", "path1"), ("WriteVar2.inputs:variableName", "path2"), ("OnImpulse.inputs:onlyPlayback", False), ("OnImpulse.state:enableImpulse", True), ("Const1.inputs:value", "A"), ("Const2.inputs:value", "B"), ], }, ) await controller.evaluate(graph) context = graph.get_default_graph_context() graph_path = self.TEST_GRAPH_PATH variable = graph.find_variable("path1") self.assertEquals(variable.get(context), f"{graph_path}/A") variable = graph.find_variable("path2") self.assertEquals(variable.get(context), f"{graph_path}/B")
19,995
Python
45.719626
120
0.464766
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/tests/test_action_graph_nodes_03.py
"""Action Graph Node Tests, Part 3""" import random from functools import partial import carb.input import omni.graph.core as og import omni.graph.core.tests as ogts import omni.kit.app import omni.kit.test import omni.usd from carb.input import MouseEventType from omni.graph.core import ThreadsafetyTestUtils from pxr import Gf, OmniGraphSchemaTools, Sdf # ====================================================================== class TestActionGraphNodes(ogts.OmniGraphTestCase): """Tests action graph node functionality""" TEST_GRAPH_PATH = "/World/TestGraph" keys = og.Controller.Keys E = og.ExecutionAttributeState.ENABLED D = og.ExecutionAttributeState.DISABLED async def setUp(self): """Set up test environment, to be torn down when done""" await super().setUp() # ---------------------------------------------------------------------- @ThreadsafetyTestUtils.make_threading_test def test_onmouseinput_node(self, test_instance_id: int = 0): """Test OnMouseInput node""" # Instance a test graph setup. Note that we append the graph path with the test_instance_id # so that the graph can be uniquely identified in the thread-safety test! graph_path = self.TEST_GRAPH_PATH + str(test_instance_id) og.Controller.create_graph({"graph_path": graph_path, "evaluator_name": "execution"}) (_, (on_mouse_input_node,), _, _) = og.Controller.edit( graph_path, {self.keys.CREATE_NODES: ("OnMouseInput", "omni.graph.action.OnMouseInput")}, ) # Obtain necessary attributes. in_onlyplayback_attr = on_mouse_input_node.get_attribute("inputs:onlyPlayback") in_mouse_element_attr = on_mouse_input_node.get_attribute("inputs:mouseElement") out_pressed_attr = on_mouse_input_node.get_attribute("outputs:pressed") out_released_attr = on_mouse_input_node.get_attribute("outputs:released") out_valuechanged_attr = on_mouse_input_node.get_attribute("outputs:valueChanged") out_ispressed_attr = on_mouse_input_node.get_attribute("outputs:isPressed") out_value_attr = on_mouse_input_node.get_attribute("outputs:value") # Obtain an interface to the mouse and carb input provider. mouse = ThreadsafetyTestUtils.add_to_threading_cache( test_instance_id, omni.appwindow.get_default_app_window().get_mouse() ) input_provider = ThreadsafetyTestUtils.add_to_threading_cache( test_instance_id, carb.input.acquire_input_provider() ) # Define a list of tokens representing the possible inputs to the OnMouseInput node's "mouseElement" attribute. mouse_tokens = ThreadsafetyTestUtils.add_to_threading_cache( test_instance_id, ["Left Button", "Middle Button", "Right Button", "Normalized Move", "Pixel Move", "Scroll"], ) # Define a list of all possible mouse event types (for convenience). mouse_event_types = ThreadsafetyTestUtils.add_to_threading_cache( test_instance_id, [ MouseEventType.LEFT_BUTTON_DOWN, MouseEventType.LEFT_BUTTON_UP, MouseEventType.MIDDLE_BUTTON_DOWN, MouseEventType.MIDDLE_BUTTON_UP, MouseEventType.RIGHT_BUTTON_DOWN, MouseEventType.RIGHT_BUTTON_UP, MouseEventType.MOVE, MouseEventType.SCROLL, ], ) # Define some imaginary window dimensions for testing purposes. window_width = ThreadsafetyTestUtils.add_to_threading_cache(test_instance_id, 1440) window_height = ThreadsafetyTestUtils.add_to_threading_cache(test_instance_id, 720) # Wrap main test driver code in the following generator (to leverage the # ThreadsafetyTestUtils.make_threading_test decorator to its fullest). Note that # in addition to testing whether specific buttons activate their corresponding code # path, we also check that no other buttons can activate code paths they don't belong # to (e.g. that pressing the left mouse button doesn't get registered as a button # press when "inputs:mouseElement" is set to "Right Button"). def _test_onmouseinput_node(): # Loop through each possible "inputs.mouseElement" token, and set the input attribute # on the OnMouseInput nodes in each graph. for token in mouse_tokens: in_mouse_element_attr.set(token) # Loop through each possible mouse event type. for event_type in mouse_event_types: # Generate a new random test position for the mouse cursor. Note that we # add it to the threading cache so that each test graph instance runs # its tests/comparisons against the same randomly-generated position. pos = ThreadsafetyTestUtils.add_to_threading_cache( test_instance_id, (random.randint(0, window_width), random.randint(0, window_height)) ) # Trigger the current mouse event. input_provider.buffer_mouse_event( mouse, event_type, (pos[0] / window_width, pos[1] / window_height), 0, pos ) yield ThreadsafetyTestUtils.EVALUATION_WAIT_FRAME # Yielding to compute by waiting for the next app frame. # Test left/middle/right mouse button pressed functionality. Only left/middle/right # mouse button presses should activate this codepath. if ( (event_type == MouseEventType.LEFT_BUTTON_DOWN and token == "Left Button") or (event_type == MouseEventType.MIDDLE_BUTTON_DOWN and token == "Middle Button") or (event_type == MouseEventType.RIGHT_BUTTON_DOWN and token == "Right Button") ): self.assertEqual(out_pressed_attr.get(), self.E) self.assertEqual(out_released_attr.get(), self.D) self.assertEqual(out_valuechanged_attr.get(), self.D) self.assertTrue(out_ispressed_attr.get()) self.assertEqual(len(out_value_attr.get()), 2) self.assertEqual(out_value_attr.get()[0], 0) self.assertEqual(out_value_attr.get()[1], 0) # Test left/middle/right mouse button released functionality. Only left/middle/right mouse # button releases should activate this codepath. elif ( (event_type == MouseEventType.LEFT_BUTTON_UP and token == "Left Button") or (event_type == MouseEventType.MIDDLE_BUTTON_UP and token == "Middle Button") or (event_type == MouseEventType.RIGHT_BUTTON_UP and token == "Right Button") ): self.assertEqual(out_pressed_attr.get(), self.D) self.assertEqual(out_released_attr.get(), self.E) self.assertEqual(out_valuechanged_attr.get(), self.D) self.assertFalse(out_ispressed_attr.get()) self.assertEqual(len(out_value_attr.get()), 2) self.assertEqual(out_value_attr.get()[0], 0) self.assertEqual(out_value_attr.get()[1], 0) # Test mouse movement functionality with the "Normalized Move" option enabled. Only mouse movement # should activate this codepath. elif event_type == MouseEventType.MOVE and token == "Normalized Move": self.assertEqual(out_pressed_attr.get(), self.D) self.assertEqual(out_released_attr.get(), self.D) self.assertEqual(out_valuechanged_attr.get(), self.E) self.assertFalse(out_ispressed_attr.get()) self.assertEqual(len(out_value_attr.get()), 2) self.assertAlmostEqual(out_value_attr.get()[0], pos[0] / window_width) self.assertAlmostEqual(out_value_attr.get()[1], pos[1] / window_height) # Test mouse movement functionality with the "Pixel Move" option enabled. Only mouse movement # should activate this codepath. elif event_type == MouseEventType.MOVE and token == "Pixel Move": self.assertEqual(out_pressed_attr.get(), self.D) self.assertEqual(out_released_attr.get(), self.D) self.assertEqual(out_valuechanged_attr.get(), self.E) self.assertFalse(out_ispressed_attr.get()) self.assertEqual(len(out_value_attr.get()), 2) self.assertEqual(out_value_attr.get()[0], pos[0]) self.assertEqual(out_value_attr.get()[1], pos[1]) # Test mouse scrolling functionality with the "Scroll" option enabled. Only mouse scrolling # should activate this codepath. elif event_type == MouseEventType.SCROLL and token == "Scroll": self.assertEqual(out_pressed_attr.get(), self.D) self.assertEqual(out_released_attr.get(), self.D) self.assertEqual(out_valuechanged_attr.get(), self.E) self.assertFalse(out_ispressed_attr.get()) self.assertEqual(len(out_value_attr.get()), 2) self.assertAlmostEqual(out_value_attr.get()[0], pos[0] / window_width) self.assertAlmostEqual(out_value_attr.get()[1], pos[1] / window_height) # Non-activated codepath. else: self.assertEqual(out_pressed_attr.get(), self.D) self.assertEqual(out_released_attr.get(), self.D) self.assertEqual(out_valuechanged_attr.get(), self.D) self.assertFalse(out_ispressed_attr.get()) self.assertEqual(len(out_value_attr.get()), 2) self.assertEqual(out_value_attr.get()[0], 0) self.assertEqual(out_value_attr.get()[1], 0) # Test that the OnMouseInput node works correctly when its onlyPlayback input is disabled. in_onlyplayback_attr.set(False) for _ in _test_onmouseinput_node(): yield ThreadsafetyTestUtils.EVALUATION_WAIT_FRAME # Yielding to compute by waiting for the next app frame. # Test that the OnMouseInput node works correctly when its onlyPlayback input is enabled. in_onlyplayback_attr.set(True) timeline = omni.timeline.get_timeline_interface() timeline.set_target_framerate(timeline.get_time_codes_per_seconds()) timeline.play() for _ in _test_onmouseinput_node(): yield ThreadsafetyTestUtils.EVALUATION_WAIT_FRAME # Yielding to compute by waiting for the next app frame. timeline.stop() # ---------------------------------------------------------------------- @ThreadsafetyTestUtils.make_threading_test def test_onobjectchange_node(self, test_instance_id: int = 0): """Test OnObjectChange node""" usd_context = ThreadsafetyTestUtils.add_to_threading_cache(test_instance_id, omni.usd.get_context()) stage = ThreadsafetyTestUtils.add_to_threading_cache(test_instance_id, usd_context.get_stage()) cube = ThreadsafetyTestUtils.add_to_threading_cache( test_instance_id, ogts.create_cube(stage, "Cube", (0.6, 0.4, 0.0)) ) attr_position = ThreadsafetyTestUtils.add_to_threading_cache( test_instance_id, cube.CreateAttribute("xformOp:translate", Sdf.ValueTypeNames.Float3, False) ) attr_rotation = ThreadsafetyTestUtils.add_to_threading_cache( test_instance_id, cube.CreateAttribute("xformOp:rotateXYZ", Sdf.ValueTypeNames.Float3, False) ) # Instance a test graph setup. Note that we append the graph path with the test_instance_id # so that the graph can be uniquely identified in the thread-safety test! graph_path = self.TEST_GRAPH_PATH + str(test_instance_id) og.Controller.create_graph({"graph_path": graph_path, "evaluator_name": "execution"}) (graph, (on_object_change_node, flip_flop_node), _, _,) = og.Controller.edit( graph_path, { self.keys.CREATE_NODES: [ (graph_path + "/OnObjectChange", "omni.graph.action.OnObjectChange"), (graph_path + "/FlipFlop", "omni.graph.action.FlipFlop"), ], self.keys.SET_VALUES: [ (graph_path + "/OnObjectChange.inputs:onlyPlayback", False), (graph_path + "/OnObjectChange.inputs:prim", attr_position.GetPrimPath()), (graph_path + "/OnObjectChange.inputs:name", attr_position.GetName()), ], self.keys.CONNECT: ( graph_path + "/OnObjectChange.outputs:changed", graph_path + "/FlipFlop.inputs:execIn", ), }, ) outa = og.Controller.attribute("outputs:a", flip_flop_node) # Check the current FlipFlop state, and that it isn't changing until we move the cube. yield # Yielding to wait for compute to happen across all graph instances before continuing the test. ff_a_state_1 = og.Controller.get(outa) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(og.Controller.get(outa), ff_a_state_1) # Try changing a different (non-translate) attribute - should not trigger. ThreadsafetyTestUtils.single_evaluation_first_test_instance( test_instance_id, partial(attr_rotation.Set, Gf.Vec3f(180.0, 0.0, 0.0)) ) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(og.Controller.get(outa), ff_a_state_1) # Now change the translate attribute - should trigger. ThreadsafetyTestUtils.single_evaluation_first_test_instance( test_instance_id, partial(attr_position.Set, Gf.Vec3f(1.0, 0.0, 0.0)) ) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. ff_a_state_2 = og.Controller.get(outa) self.assertEqual(ff_a_state_2, 1 if not ff_a_state_1 else 0) property_path = og.Controller.get(og.Controller.attribute("outputs:propertyName", on_object_change_node)) self.assertEqual(property_path, attr_position.GetName()) # Look at the prim itself. og.Controller.edit( graph, {self.keys.SET_VALUES: (graph_path + "/OnObjectChange.inputs:name", "")}, ) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. # Now change the rotate attribute - should trigger. ThreadsafetyTestUtils.single_evaluation_first_test_instance( test_instance_id, partial(attr_rotation.Set, Gf.Vec3f(245.0, 0.0, 0.0)) ) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. ff_a_state_1 = og.Controller.get(outa) self.assertEqual(ff_a_state_1, 1 if not ff_a_state_2 else 0) property_path = og.Controller.get(og.Controller.attribute("outputs:propertyName", on_object_change_node)) self.assertEqual(property_path, attr_rotation.GetName()) # Now use the path input instead of the target. Do this for all OnObjectChange nodes! og.Controller.edit( graph, { self.keys.SET_VALUES: [ (graph_path + "/OnObjectChange.inputs:path", cube.GetPath().pathString), (graph_path + "/OnObjectChange.inputs:prim", []), ] }, ) # Compute once to set up the inputs. yield # Yielding to wait for compute to happen across all graph instances before continuing the test. # Change the rotate attribute - should trigger. ThreadsafetyTestUtils.single_evaluation_first_test_instance( test_instance_id, partial(attr_rotation.Set, Gf.Vec3f(0.0, 0.0, 0.0)) ) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. ff_a_state_2 = og.Controller.get(outa) self.assertEqual(ff_a_state_2, 1 if not ff_a_state_1 else 0) property_path = og.Controller.get(og.Controller.attribute("outputs:propertyName", on_object_change_node)) self.assertEqual(property_path, attr_rotation.GetName()) # ---------------------------------------------------------------------- @ThreadsafetyTestUtils.make_threading_test def test_onplaybacktick_node(self, test_instance_id: int = 0): """Test OnPlaybackTick node""" timeline = omni.timeline.get_timeline_interface() # The stage's default stage is probably 60, set to 30 for this specific case stage = omni.usd.get_context().get_stage() fps = 30.0 stage.SetTimeCodesPerSecond(fps) stage.SetStartTimeCode(1.0) stage.SetEndTimeCode(10.0) # Instance a test graph setup. Note that we append the graph path with the test_instance_id # so that the graph can be uniquely identified in the thread-safety test! graph_path = self.TEST_GRAPH_PATH + str(test_instance_id) og.Controller.create_graph({"graph_path": graph_path, "evaluator_name": "execution"}) (_, (on_p_tick_node, _), _, _,) = og.Controller.edit( graph_path, { self.keys.CREATE_NODES: [ ("OnPTick", "omni.graph.action.OnPlaybackTick"), ("FlipFlop", "omni.graph.action.FlipFlop"), ], self.keys.CONNECT: [ ("OnPTick.outputs:tick", "FlipFlop.inputs:execIn"), ], }, ) # Obtain necessary attributes. tick_time_attr = on_p_tick_node.get_attribute("outputs:time") tick_frame_attr = on_p_tick_node.get_attribute("outputs:frame") yield # Yielding to wait for compute to happen across all graph instances before continuing the test. # Check that the OnPlaybackTick node doesn't trigger when playback is not active. self.assertEqual(tick_time_attr.get(), 0) self.assertEqual(tick_frame_attr.get(), 0) # Check that the OnPlaybackTick node does trigger when playback is active, and the values are correct. timeline.play() yield ThreadsafetyTestUtils.EVALUATION_WAIT_FRAME # Yielding to compute by waiting for the next app frame. yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertAlmostEqual(tick_time_attr.get(), timeline.get_current_time(), places=5) self.assertAlmostEqual(tick_frame_attr.get(), tick_time_attr.get() * fps, places=5) # ---------------------------------------------------------------------- @ThreadsafetyTestUtils.make_threading_test def test_ontick_node(self, test_instance_id: int = 0): """Test OnTick node""" timeline = omni.timeline.get_timeline_interface() # The stage's default stage is probably 60, set to 30 for this specific case stage = omni.usd.get_context().get_stage() fps = 30.0 stage.SetTimeCodesPerSecond(fps) stage.SetStartTimeCode(1.0) stage.SetEndTimeCode(10.0) # Instance a test graph setup. Note that we append the graph path with the test_instance_id # so that the graph can be uniquely identified in the thread-safety test! graph_path = self.TEST_GRAPH_PATH + str(test_instance_id) og.Controller.create_graph({"graph_path": graph_path, "evaluator_name": "execution"}) (_, (on_tick_node, _), _, _,) = og.Controller.edit( graph_path, { self.keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("FlipFlop", "omni.graph.action.FlipFlop"), ], self.keys.CONNECT: [ ("OnTick.outputs:tick", "FlipFlop.inputs:execIn"), ], self.keys.SET_VALUES: ("OnTick.inputs:onlyPlayback", True), }, ) # Obtain necessary attributes. ontick_time_attr = on_tick_node.get_attribute("outputs:time") ontick_frame_attr = on_tick_node.get_attribute("outputs:frame") yield # Yielding to wait for compute to happen across all graph instances before continuing the test. # Check that the OnTick node doesn't trigger when playback is not active. self.assertEqual(ontick_time_attr.get(), 0) self.assertEqual(ontick_frame_attr.get(), 0) # Check that the OnTick node does trigger when playback is active, and the values are correct. timeline.play() yield ThreadsafetyTestUtils.EVALUATION_WAIT_FRAME # Yielding to compute by waiting for the next app frame. yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertAlmostEqual(ontick_time_attr.get(), timeline.get_current_time(), places=5) self.assertAlmostEqual(ontick_frame_attr.get(), ontick_time_attr.get() * fps, places=5) # ---------------------------------------------------------------------- @ThreadsafetyTestUtils.make_threading_test def test_onvariablechange_node(self, test_instance_id: int = 0): """Test OnVariableChange node""" # Instance a test graph setup. Note that we append the graph path with the test_instance_id # so that the graph can be uniquely identified in the thread-safety test! graph_path = self.TEST_GRAPH_PATH + str(test_instance_id) og.Controller.create_graph({"graph_path": graph_path, "evaluator_name": "execution"}) (graph, (on_variable_change_node, counter_node), _, _,) = og.Controller.edit( graph_path, { self.keys.CREATE_NODES: [ ("OnVariableChange", "omni.graph.action.OnVariableChange"), ("Counter", "omni.graph.action.Counter"), ], self.keys.SET_VALUES: [ ("OnVariableChange.inputs:onlyPlayback", False), ], self.keys.CONNECT: [("OnVariableChange.outputs:changed", "Counter.inputs:execIn")], }, ) graph_context = graph.get_default_graph_context() # Create a new float variable on each graph, and set the OnVariableChange nodes to track this single variable. var_name = "myVar" + str(test_instance_id) my_var = og.Controller.create_variable(graph, var_name, "FLOAT") yield # Yielding to wait for compute to happen across all graph instances before continuing the test. og.Controller.set(og.Controller.attribute("inputs:variableName", on_variable_change_node), var_name) # Check that the execution output is still set to zero. yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(og.Controller.get(og.Controller.attribute("outputs:changed", on_variable_change_node)), self.D) # Change the variable's value, check that the execution reads true now. my_var.set(graph_context, 5.2) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(og.Controller.get(og.Controller.attribute("outputs:changed", on_variable_change_node)), self.E) # Evaluate a second time to check that the execution gets set back to zero. yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(og.Controller.get(og.Controller.attribute("outputs:changed", on_variable_change_node)), self.D) # Make sure that the output execution remains off if we try setting the variable # to the same previous value. my_var.set(graph_context, 5.2) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(og.Controller.get(og.Controller.attribute("outputs:changed", on_variable_change_node)), self.D) # Check that setting the inputs:variableName to a nonexistant variable results in the output # execution pin remaining zero. og.Controller.set(og.Controller.attribute("inputs:variableName", on_variable_change_node), "nonexistant_name") yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(og.Controller.get(og.Controller.attribute("outputs:changed", on_variable_change_node)), self.D) # Set the onlyPlayback flag to true, run a similar set of tests to ensure that this setting # still works with the node. Note that a Counter node is used here to detect the variable change # because it's trickier to check against outputs:changed directly (harder to time everything with # all of the async stuff + when the timeline is running). og.Controller.set(og.Controller.attribute("inputs:variableName", on_variable_change_node), var_name) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. og.Controller.set(og.Controller.attribute("inputs:onlyPlayback", on_variable_change_node), True) timeline = ThreadsafetyTestUtils.add_to_threading_cache( test_instance_id, omni.timeline.get_timeline_interface() ) ThreadsafetyTestUtils.single_evaluation_first_test_instance( test_instance_id, partial(timeline.set_target_framerate, timeline.get_time_codes_per_seconds()) ) ThreadsafetyTestUtils.single_evaluation_first_test_instance(test_instance_id, partial(timeline.play)) my_var.set(graph_context, 4.6) yield ThreadsafetyTestUtils.EVALUATION_WAIT_FRAME # Yielding to compute by waiting for the next app frame. self.assertEqual(og.Controller.get(og.Controller.attribute("outputs:count", counter_node)), 2) my_var.set(graph_context, 4.6) yield ThreadsafetyTestUtils.EVALUATION_WAIT_FRAME # Yielding to compute by waiting for the next app frame. self.assertEqual(og.Controller.get(og.Controller.attribute("outputs:count", counter_node)), 2) yield ThreadsafetyTestUtils.EVALUATION_WAIT_FRAME # Yielding to compute by waiting for the next app frame. self.assertEqual(og.Controller.get(og.Controller.attribute("outputs:count", counter_node)), 2) my_var.set(graph_context, 4.6) yield ThreadsafetyTestUtils.EVALUATION_WAIT_FRAME # Yielding to compute by waiting for the next app frame. self.assertEqual(og.Controller.get(og.Controller.attribute("outputs:count", counter_node)), 2) og.Controller.set(og.Controller.attribute("inputs:variableName", on_variable_change_node), "nonexistant_name_2") yield ThreadsafetyTestUtils.EVALUATION_WAIT_FRAME # Yielding to compute by waiting for the next app frame. self.assertEqual(og.Controller.get(og.Controller.attribute("outputs:count", counter_node)), 2) ThreadsafetyTestUtils.single_evaluation_last_test_instance(test_instance_id, partial(timeline.stop)) # ---------------------------------------------------------------------- async def _run_test_variable_of_type(self, graph_id, var_type: og.Type, initial_value, changed_value): """Helper method to run a variable test with different variable types""" graph_path = self.TEST_GRAPH_PATH + graph_id og.Controller.create_graph({"graph_path": graph_path, "evaluator_name": "execution"}) (graph, (_, counter_node), _, _,) = og.Controller.edit( graph_path, { self.keys.CREATE_NODES: [ ("OnVariableChange", "omni.graph.action.OnVariableChange"), ("Counter", "omni.graph.action.Counter"), ], self.keys.SET_VALUES: [ ("OnVariableChange.inputs:onlyPlayback", False), ("OnVariableChange.inputs:variableName", "var"), ], self.keys.CONNECT: [("OnVariableChange.outputs:changed", "Counter.inputs:execIn")], self.keys.CREATE_VARIABLES: [ ("var", var_type), ], }, ) # test that nothing triggered on the initial run await omni.kit.app.get_app().next_update_async() self.assertEqual(og.Controller.get(og.Controller.attribute("outputs:count", counter_node)), 0) var = graph.find_variable("var") # set the initial value, expected a trigger var.set(graph.get_context(), initial_value) await omni.kit.app.get_app().next_update_async() self.assertEqual(og.Controller.get(og.Controller.attribute("outputs:count", counter_node)), 1) ogts.verify_values(var.get(graph.get_context()), initial_value, "expected initial value to be set") # change the value of the variable and makes sure the OnVariableChange triggers var.set(graph.get_context(), changed_value) await omni.kit.app.get_app().next_update_async() self.assertEqual(og.Controller.get(og.Controller.attribute("outputs:count", counter_node)), 2) ogts.verify_values(var.get(graph.get_context()), changed_value, "expeected changed value to be set") # ---------------------------------------------------------------------- async def test_onvariablechanged_by_type(self): """Tests OnVariableChange for different types of variables, including strings and arrays""" await self._run_test_variable_of_type("_int", og.Type(og.BaseDataType.INT), 1, 2) await self._run_test_variable_of_type("_int_array", og.Type(og.BaseDataType.INT, 1, 1), [1, 2, 3], [3, 4, 5]) await self._run_test_variable_of_type( "_int_array_size", og.Type(og.BaseDataType.INT, 1, 1), [1, 2, 3, 4, 5], [1, 2, 3] ) await self._run_test_variable_of_type("_tuple", og.Type(og.BaseDataType.INT, 3, 0), (1, 2, 3), (3, 4, 5)) await self._run_test_variable_of_type( "_string", og.Type(og.BaseDataType.UCHAR, 1, 1, og.AttributeRole.TEXT), "init", "changed" ) await self._run_test_variable_of_type( "_tuple_array", og.Type(og.BaseDataType.INT, 3, 1), [(1, 2, 3), (4, 5, 6)], [(3, 4, 5)] ) # ---------------------------------------------------------------------- async def test_onvariablechange_node_instances(self): """Tests that OnVariableChange node works with instancing""" controller = og.Controller() keys = og.Controller.Keys int_range = range(1, 100) stage = omni.usd.get_context().get_stage() for i in int_range: prim_name = f"/World/Prim_{i}" prim = stage.DefinePrim(prim_name) OmniGraphSchemaTools.applyOmniGraphAPI(stage, prim_name, self.TEST_GRAPH_PATH) prim.CreateAttribute("graph:variable:int_var", Sdf.ValueTypeNames.Int).Set(0) prim.CreateAttribute("graph_output", Sdf.ValueTypeNames.Int).Set(i) (_, _, _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnVariableChange", "omni.graph.action.OnVariableChange"), ("GraphTarget", "omni.graph.nodes.GraphTarget"), ("ReadPrimAttr", "omni.graph.nodes.ReadPrimAttribute"), ("WritePrimAttr", "omni.graph.nodes.WritePrimAttribute"), ("Add", "omni.graph.nodes.Add"), ("ConstantInt", "omni.graph.nodes.ConstantInt"), ], keys.CONNECT: [ ("OnVariableChange.outputs:changed", "WritePrimAttr.inputs:execIn"), ("GraphTarget.outputs:primPath", "WritePrimAttr.inputs:primPath"), ("GraphTarget.outputs:primPath", "ReadPrimAttr.inputs:primPath"), ("ReadPrimAttr.outputs:value", "Add.inputs:a"), ("ConstantInt.inputs:value", "Add.inputs:b"), ("Add.outputs:sum", "WritePrimAttr.inputs:value"), ], keys.CREATE_VARIABLES: [("int_var", og.Type(og.BaseDataType.INT))], keys.SET_VALUES: [ ("OnVariableChange.inputs:onlyPlayback", False), ("OnVariableChange.inputs:variableName", "int_var"), ("WritePrimAttr.inputs:usePath", True), ("WritePrimAttr.inputs:name", "graph_output"), ("ReadPrimAttr.inputs:usePath", True), ("ReadPrimAttr.inputs:name", "graph_output"), ("ConstantInt.inputs:value", 1), ], }, ) await omni.kit.app.get_app().next_update_async() # Setting value of int_var from 0 to 1. for i in int_range: prim_path = f"/World/Prim_{i}" prev_val = stage.GetPrimAtPath(prim_path).GetAttribute("graph_output").Get() stage.GetPrimAtPath(prim_path).GetAttribute("graph:variable:int_var").Set(1) await omni.kit.app.get_app().next_update_async() val = stage.GetPrimAtPath(prim_path).GetAttribute("graph_output").Get() self.assertEqual(val, prev_val + 1, msg=f"{prim_path}") # Setting value of int_var from 1 to 1. for i in int_range: prim_path = f"/World/Prim_{i}" prev_val = stage.GetPrimAtPath(prim_path).GetAttribute("graph_output").Get() stage.GetPrimAtPath(prim_path).GetAttribute("graph:variable:int_var").Set(1) await omni.kit.app.get_app().next_update_async() val = stage.GetPrimAtPath(prim_path).GetAttribute("graph_output").Get() self.assertEqual(val, prev_val + 1, msg=f"{prim_path}") # ---------------------------------------------------------------------- # The SendCustomEvent node is tested in tandem with the OnCustomEvent node above; it is also used # extensively in other tests, so we skip adding another dedicated test here. # ---------------------------------------------------------------------- @ThreadsafetyTestUtils.make_threading_test def test_sequence_node(self, test_instance_id: int = 0): """Test Sequence node""" # Instance a test graph setup. Note that we append the graph path with the test_instance_id # so that the graph can be uniquely identified in the thread-safety test! graph_path = self.TEST_GRAPH_PATH + str(test_instance_id) og.Controller.create_graph({"graph_path": graph_path, "evaluator_name": "execution"}) (_, (_, sequence_node, counter0_node, counter1_node), _, _,) = og.Controller.edit( graph_path, { self.keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("Sequence", "omni.graph.action.Sequence"), ("Counter0", "omni.graph.action.Counter"), ("Counter1", "omni.graph.action.Counter"), ], self.keys.SET_VALUES: [("OnTick.inputs:onlyPlayback", False)], self.keys.CONNECT: [ ("OnTick.outputs:tick", "Sequence.inputs:execIn"), ("Sequence.outputs:a", "Counter0.inputs:execIn"), ("Sequence.outputs:b", "Counter1.inputs:execIn"), ], }, ) # Obtain necessary attributes. in_exec_attr_0 = counter0_node.get_attribute("inputs:execIn") out_cnt_attr_0 = counter0_node.get_attribute("outputs:count") out_cnt_attr_1 = counter1_node.get_attribute("outputs:count") out_a_attr = sequence_node.get_attribute("outputs:a") out_b_attr = sequence_node.get_attribute("outputs:b") # Check that the Sequence node correctly executes through its outputs when input # execution is enabled via the OnTick node. This is done by checking whether # each counter has been incremented by 1, and if the last output pin on the Sequence # nodes remain enabled (regardless of the fact that they're not connected downstream). # Similar test to the Multisequence node. self.assertEqual(out_cnt_attr_0.get(), 0) self.assertEqual(out_cnt_attr_1.get(), 0) self.assertEqual(out_a_attr.get(), self.D) self.assertEqual(out_b_attr.get(), self.D) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_cnt_attr_0.get(), 1) self.assertEqual(out_cnt_attr_1.get(), 1) self.assertEqual(out_a_attr.get(), self.D) self.assertEqual(out_b_attr.get(), self.E) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_cnt_attr_0.get(), 2) self.assertEqual(out_cnt_attr_1.get(), 2) self.assertEqual(out_a_attr.get(), self.D) self.assertEqual(out_b_attr.get(), self.E) # Connect output pin b to the Counter1 node. This is because both pins a and b share # the same terminal input attribute, so both must have the same value. Since # b is the last attribute and must be set to 1 (due to logic inside this node's # compute method), output a must also take on this value. og.Controller.connect(out_b_attr, in_exec_attr_0) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_cnt_attr_0.get(), 4) self.assertEqual(out_cnt_attr_1.get(), 3) self.assertEqual(out_a_attr.get(), self.E) self.assertEqual(out_b_attr.get(), self.E) # ---------------------------------------------------------------------- async def test_setprimactive_node(self): await self._setprimactive_node(False) # ---------------------------------------------------------------------- async def test_setprimactive_node_with_target(self): await self._setprimactive_node(True) # ---------------------------------------------------------------------- async def _setprimactive_node(self, use_target_inputs): """ Test SetPrimActive node. If use_target_inputs is true, will use the prim target inputs rather than prim path """ usd_context = omni.usd.get_context() stage = usd_context.get_stage() (_, (_, set_prim_active_node), _, _,) = og.Controller.edit( self.TEST_GRAPH_PATH, { self.keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("SetPrimActive", "omni.graph.action.SetPrimActive"), ], self.keys.CREATE_PRIMS: [ ("/TestPrim", {}), ], self.keys.CONNECT: [("OnTick.outputs:tick", "SetPrimActive.inputs:execIn")], self.keys.SET_VALUES: [ ("OnTick.inputs:onlyPlayback", False), ("SetPrimActive.inputs:prim", "/TestPrim"), ("SetPrimActive.inputs:active", True), ], }, ) await og.Controller.evaluate() if use_target_inputs: rel = stage.GetRelationshipAtPath(f"{self.TEST_GRAPH_PATH}/SetPrimActive.inputs:primTarget") omni.kit.commands.execute("AddRelationshipTarget", relationship=rel, target="/TestPrim") await og.Controller.evaluate() # Check that the prim we're pointing to remains active when we trigger # graph evaluation. prim = stage.GetPrimAtPath("/TestPrim") self.assertTrue(prim.IsActive()) await og.Controller.evaluate() self.assertTrue(prim.IsActive()) # Now set the prim to be inactive. og.Controller.set(og.Controller.attribute("inputs:active", set_prim_active_node), False) await og.Controller.evaluate() self.assertFalse(prim.IsActive()) await og.Controller.evaluate() self.assertFalse(prim.IsActive()) # Again set to be active, make sure that the prim gets reactivated. og.Controller.set(og.Controller.attribute("inputs:active", set_prim_active_node), True) await og.Controller.evaluate() self.assertTrue(prim.IsActive()) # ---------------------------------------------------------------------- @ThreadsafetyTestUtils.make_threading_test def test_switchtoken_node(self, test_instance_id: int = 0): """Test SwitchToken node""" # Instance a test graph setup. Note that we append the graph path with the test_instance_id # so that the graph can be uniquely identified in the thread-safety test! graph_path = self.TEST_GRAPH_PATH + str(test_instance_id) og.Controller.create_graph({"graph_path": graph_path, "evaluator_name": "execution"}) (_, (_, switch_node), _, _,) = og.Controller.edit( graph_path, { self.keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("Switch", "omni.graph.action.SwitchToken"), ], self.keys.SET_VALUES: [ ("OnTick.inputs:onlyPlayback", False), ], self.keys.CONNECT: [ ("OnTick.outputs:tick", "Switch.inputs:execIn"), ], }, ) # Test that an execution connection is correctly triggering the downstream nodes once per evaluation. def add_input(index: int): og.Controller.create_attribute( switch_node, f"inputs:branch{index:02}", "token", og.AttributePortType.ATTRIBUTE_PORT_TYPE_INPUT, ) og.Controller.create_attribute( switch_node, f"outputs:output{index:02}", "execution", og.AttributePortType.ATTRIBUTE_PORT_TYPE_OUTPUT, ) og.Controller.set(og.Controller.attribute(f"inputs:branch{index:02}", switch_node), f"{index:02}") for index in range(5): add_input(index) for index in range(5): og.Controller.set(og.Controller.attribute("inputs:value", switch_node), f"{index:02}") yield # Yielding to wait for compute to happen across all graph instances before continuing the test. for index2 in range(5): expected_val = ( og.ExecutionAttributeState.DISABLED if index2 != index else og.ExecutionAttributeState.ENABLED ) self.assertEqual( og.Controller.get(og.Controller.attribute(f"outputs:output{index2:02}", switch_node)), expected_val, ) # ---------------------------------------------------------------------- @ThreadsafetyTestUtils.make_threading_test def test_syncgate_node(self, test_instance_id: int = 0): """Test SyncGate node""" # Instance a test graph setup. Note that we append the graph path with the test_instance_id # so that the graph can be uniquely identified in the thread-safety test! graph_path = self.TEST_GRAPH_PATH + str(test_instance_id) og.Controller.create_graph({"graph_path": graph_path, "evaluator_name": "execution"}) ( _, (on_impulse0_node, on_impulse1_node, on_impulse2_node, on_impulse3_node, sync_gate_node, counter_node), _, _, ) = og.Controller.edit( graph_path, { self.keys.CREATE_NODES: [ ("OnImpulse0", "omni.graph.action.OnImpulseEvent"), ("OnImpulse1", "omni.graph.action.OnImpulseEvent"), ("OnImpulse2", "omni.graph.action.OnImpulseEvent"), ("OnImpulse3", "omni.graph.action.OnImpulseEvent"), ("SyncGate", "omni.graph.action.SyncGate"), ("Counter", "omni.graph.action.Counter"), ], self.keys.CONNECT: [ ("OnImpulse0.outputs:execOut", "SyncGate.inputs:execIn"), ("OnImpulse1.outputs:execOut", "SyncGate.inputs:execIn"), ("OnImpulse2.outputs:execOut", "SyncGate.inputs:execIn"), ("OnImpulse3.outputs:execOut", "SyncGate.inputs:execIn"), ("SyncGate.outputs:execOut", "Counter.inputs:execIn"), ], self.keys.SET_VALUES: [ ("OnImpulse0.inputs:onlyPlayback", False), ("OnImpulse1.inputs:onlyPlayback", False), ("OnImpulse2.inputs:onlyPlayback", False), ("OnImpulse3.inputs:onlyPlayback", False), ("SyncGate.inputs:syncValue", 5), ], }, ) # Obtain necessary attributes. state_enable_impulse_attr_0 = on_impulse0_node.get_attribute("state:enableImpulse") state_enable_impulse_attr_1 = on_impulse1_node.get_attribute("state:enableImpulse") state_enable_impulse_attr_2 = on_impulse2_node.get_attribute("state:enableImpulse") state_enable_impulse_attr_3 = on_impulse3_node.get_attribute("state:enableImpulse") in_syncvalue_attr = sync_gate_node.get_attribute("inputs:syncValue") out_syncvalue_attr = sync_gate_node.get_attribute("outputs:syncValue") out_cnt_attr = counter_node.get_attribute("outputs:count") # First check that the SyncGate node only gets unblocked when it detects 4+ input # enabled executions, at which point the Counter node will begin to be incremented. # Also check that after the 1st graph evaluation the SyncGate's output syncValue gets # set to its input syncValue. state_enable_impulse_attr_0.set(True) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_syncvalue_attr.get(), 5) self.assertEqual(out_cnt_attr.get(), 0) state_enable_impulse_attr_1.set(True) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_syncvalue_attr.get(), 5) self.assertEqual(out_cnt_attr.get(), 0) state_enable_impulse_attr_2.set(True) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_syncvalue_attr.get(), 5) self.assertEqual(out_cnt_attr.get(), 0) state_enable_impulse_attr_3.set(True) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_syncvalue_attr.get(), 5) self.assertEqual(out_cnt_attr.get(), 1) state_enable_impulse_attr_0.set(True) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_syncvalue_attr.get(), 5) self.assertEqual(out_cnt_attr.get(), 2) # Now reset the SyncGate node's syncValue. Check that we once again need 4+ input executions # with the "enabled" status flowing into the SyncGate in order to unlock it. Also note # that we don't technically need each separate OnImpulse node to trigger to unlock the gate; # we could have a single OnImpulse node (per graph) send 4 separate impulses to open each gate # in each graph, since that would reach the threshold number of accumulated execIn states for # this setup (equal to 4 since we have 4 nodes connected to the SyncGate's inputs:execIn pin). in_syncvalue_attr.set(9) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. for i in range(0, 5): state_enable_impulse_attr_0.set(True) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_syncvalue_attr.get(), 9) if i < 3: self.assertEqual(out_cnt_attr.get(), 2) else: self.assertEqual(out_cnt_attr.get(), 2 + (i - 2)) # Now reset the syncValue again per node. This time only send 2 input impulses, then reset the syncValue # immediately after. Check that we'll again need to send 4 input impulses to open the corresponding gate # (which was essentially "relocked" with the syncValue change). in_syncvalue_attr.set(21) state_enable_impulse_attr_0.set(True) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_syncvalue_attr.get(), 21) self.assertEqual(out_cnt_attr.get(), 4) state_enable_impulse_attr_2.set(True) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_syncvalue_attr.get(), 21) self.assertEqual(out_cnt_attr.get(), 4) in_syncvalue_attr.set(25) for i in range(0, 5): state_enable_impulse_attr_2.set(True) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_syncvalue_attr.get(), 25) if i < 3: self.assertEqual(out_cnt_attr.get(), 4) else: self.assertEqual(out_cnt_attr.get(), 4 + (i - 2)) # ---------------------------------------------------------------------- async def test_countdown_node(self): """Test Countdown node""" duration = 5 period = 3 og.Controller.create_graph({"graph_path": self.TEST_GRAPH_PATH, "evaluator_name": "execution"}) (_, (_, _, counter0_node, counter1_node), _, _,) = og.Controller.edit( self.TEST_GRAPH_PATH, { self.keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("Countdown", "omni.graph.action.Countdown"), ("Counter0", "omni.graph.action.Counter"), ("Counter1", "omni.graph.action.Counter"), ], self.keys.CONNECT: [ ("OnTick.outputs:tick", "Countdown.inputs:execIn"), ("Countdown.outputs:finished", "Counter0.inputs:execIn"), ("Countdown.outputs:tick", "Counter1.inputs:execIn"), ], # Set the Countdown inputs so that the Counter0 node only gets incremented # once 4 update ticks have passed since the graph creation, and so that # the Counter1 node gets incremented every 2 ticks. self.keys.SET_VALUES: [ ("OnTick.inputs:onlyPlayback", False), ("Countdown.inputs:duration", duration), ("Countdown.inputs:period", period), ], }, ) # Obtain necessary attributes. out_cnt_attr_0 = counter0_node.get_attribute("outputs:count") out_cnt_attr_1 = counter1_node.get_attribute("outputs:count") # Test against a predetermined set of values to make sure that # this node doesn't break in the future. cnt0 = [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2] cnt1 = [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3] for i in range(0, 20): await og.Controller.evaluate() self.assertEqual(og.Controller.get(out_cnt_attr_0), cnt0[i]) self.assertEqual(og.Controller.get(out_cnt_attr_1), cnt1[i])
52,966
Python
54.462827
127
0.598629
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/tests/test_actiongraph.py
"""Basic tests of the action graph""" import omni.client import omni.graph.core as og import omni.graph.core.tests as ogts import omni.kit.test import omni.usd from pxr import Gf, Sdf # ====================================================================== class TestActionGraphNodes(ogts.OmniGraphTestCase): """Tests action graph node functionality""" TEST_GRAPH_PATH = "/World/TestGraph" async def setUp(self): """Set up test environment, to be torn down when done""" await super().setUp() og.Controller.edit({"graph_path": self.TEST_GRAPH_PATH, "evaluator_name": "execution"}) # ---------------------------------------------------------------------- async def test_basic(self): """exercise a basic network of execution nodes""" controller = og.Controller() keys = og.Controller.Keys # Test that an execution connection is correctly triggering the downstream node once per evaluation (graph, (_, flip_flop_node), _, _,) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [("OnTick", "omni.graph.action.OnTick"), ("FlipFlop", "omni.graph.action.FlipFlop")], keys.SET_VALUES: [("OnTick.inputs:onlyPlayback", False)], keys.CONNECT: ("OnTick.outputs:tick", "FlipFlop.inputs:execIn"), }, ) outa = controller.attribute("outputs:a", flip_flop_node) outb = controller.attribute("outputs:b", flip_flop_node) outisa = controller.attribute("outputs:isA", flip_flop_node) # first eval, 'a' await controller.evaluate(graph) self.assertEqual(og.Controller.get(outa), og.ExecutionAttributeState.ENABLED) self.assertEqual(og.Controller.get(outb), og.ExecutionAttributeState.DISABLED) self.assertTrue(og.Controller.get(outisa)) # second eval 'b' await controller.evaluate(graph) self.assertEqual(og.Controller.get(outa), og.ExecutionAttributeState.DISABLED) self.assertEqual(og.Controller.get(outb), og.ExecutionAttributeState.ENABLED) self.assertFalse(og.Controller.get(outisa)) # third eval 'a' await controller.evaluate(graph) self.assertEqual(og.Controller.get(outa), og.ExecutionAttributeState.ENABLED) self.assertEqual(og.Controller.get(outb), og.ExecutionAttributeState.DISABLED) self.assertTrue(og.Controller.get(outisa)) # Test that non-usd-backed node has execution attribute type correct _, on_tick_no_usd = og.cmds.CreateNode( graph=graph, node_path=f"{self.TEST_GRAPH_PATH}/OnTickNoUSD", node_type="omni.graph.action.OnTick", create_usd=False, ) og.cmds.CreateNode( graph=graph, node_path=f"{self.TEST_GRAPH_PATH}/FlipFlopNoUSD", node_type="omni.graph.action.FlipFlop", create_usd=False, ) controller.attribute("inputs:onlyPlayback", on_tick_no_usd, graph).set(False) og.cmds.ConnectAttrs( src_attr=f"{self.TEST_GRAPH_PATH}/OnTickNoUSD.outputs:tick", dest_attr=f"{self.TEST_GRAPH_PATH}/FlipFlopNoUSD.inputs:execIn", modify_usd=False, ) outa = controller.attribute("outputs:a", f"{self.TEST_GRAPH_PATH}/FlipFlopNoUSD") outb = controller.attribute("outputs:b", f"{self.TEST_GRAPH_PATH}/FlipFlopNoUSD") outisa = controller.attribute("outputs:isA", f"{self.TEST_GRAPH_PATH}/FlipFlopNoUSD") # first eval, 'a' await controller.evaluate(graph) self.assertEqual(og.Controller.get(outa), og.ExecutionAttributeState.ENABLED) self.assertEqual(og.Controller.get(outb), og.ExecutionAttributeState.DISABLED) self.assertTrue(og.Controller.get(outisa)) # second eval 'b' await controller.evaluate(graph) self.assertEqual(og.Controller.get(outa), og.ExecutionAttributeState.DISABLED) self.assertEqual(og.Controller.get(outb), og.ExecutionAttributeState.ENABLED) self.assertFalse(og.Controller.get(outisa)) # third eval 'a' await controller.evaluate(graph) self.assertEqual(og.Controller.get(outa), og.ExecutionAttributeState.ENABLED) self.assertEqual(og.Controller.get(outb), og.ExecutionAttributeState.DISABLED) self.assertTrue(og.Controller.get(outisa)) # ---------------------------------------------------------------------- async def test_notice(self): """Tests the OnObjectChange node""" usd_context = omni.usd.get_context() stage = usd_context.get_stage() controller = og.Controller() keys = og.Controller.Keys cube = ogts.create_cube(stage, "Cube", (0.6, 0.4, 0.0)) attr_position = cube.CreateAttribute("xformOp:translate", Sdf.ValueTypeNames.Float3, False) attr_rotation = cube.CreateAttribute("xformOp:rotateXYZ", Sdf.ValueTypeNames.Float3, False) (graph, (on_object_change_node, flip_flop_node), _, _,) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnObjectChange", "omni.graph.action.OnObjectChange"), ("FlipFlop", "omni.graph.action.FlipFlop"), ], keys.SET_VALUES: [ ("OnObjectChange.inputs:onlyPlayback", False), ("OnObjectChange.inputs:path", attr_position.GetPath().pathString), ], keys.CONNECT: ("OnObjectChange.outputs:changed", "FlipFlop.inputs:execIn"), }, ) outa = controller.attribute("outputs:a", flip_flop_node) # check current flipflop state, and that it isn't changing until we move the cube await controller.evaluate(graph) ff_a_state_1 = og.Controller.get(outa) await controller.evaluate(graph) self.assertEqual(og.Controller.get(outa), ff_a_state_1) # Try changing a different attr - should not trigger attr_rotation.Set(Gf.Vec3f(180.0, 0.0, 0.0)) await controller.evaluate(graph) self.assertEqual(og.Controller.get(outa), ff_a_state_1) # Now change the translate - should trigger attr_position.Set(Gf.Vec3f(1.0, 0.0, 0.0)) await controller.evaluate(graph) ff_a_state_2 = og.Controller.get(outa) self.assertEqual(ff_a_state_2, 1 if not ff_a_state_1 else 0) property_path = og.Controller.get(controller.attribute("outputs:propertyName", on_object_change_node)) self.assertEqual(property_path, attr_position.GetName()) # Look at prim itself controller.edit(graph, {keys.SET_VALUES: ("OnObjectChange.inputs:path", cube.GetPath().pathString)}) await controller.evaluate(graph) # Now change the rotate - should trigger attr_rotation.Set(Gf.Vec3f(245.0, 0.0, 0.0)) await controller.evaluate(graph) ff_a_state_1 = og.Controller.get(outa) self.assertEqual(ff_a_state_1, 1 if not ff_a_state_2 else 0) property_path = og.Controller.get(controller.attribute("outputs:propertyName", on_object_change_node)) self.assertEqual(property_path, attr_rotation.GetName()) # Now use a prim rel instead of the path input inputs_prim = stage.GetPrimAtPath(on_object_change_node.get_prim_path()).GetRelationship("inputs:prim") inputs_prim.AddTarget(cube.GetPath()) controller.edit(graph, {keys.SET_VALUES: ("OnObjectChange.inputs:path", "")}) # compute once to set up the inputs await controller.evaluate(graph) # change rotate - should trigger attr_rotation.Set(Gf.Vec3f(0.0, 0.0, 0.0)) await controller.evaluate(graph) ff_a_state_2 = og.Controller.get(outa) self.assertEqual(ff_a_state_2, 1 if not ff_a_state_1 else 0) property_path = og.Controller.get(controller.attribute("outputs:propertyName", on_object_change_node)) self.assertEqual(property_path, attr_rotation.GetName()) # ---------------------------------------------------------------------- async def test_onplaybacktick(self): """Test OnPlaybackTick and OnTick node""" controller = og.Controller() keys = og.Controller.Keys app = omni.kit.app.get_app() timeline = omni.timeline.get_timeline_interface() timeline.set_start_time(1.0) timeline.set_end_time(10.0) fps = 24.0 timeline.set_time_codes_per_second(fps) (graph, (on_p_tick_node, on_tick_node, _), _, _,) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnPTick", "omni.graph.action.OnPlaybackTick"), ("OnTick", "omni.graph.action.OnTick"), ("FlipFlop", "omni.graph.action.FlipFlop"), ], keys.CONNECT: [ ("OnPTick.outputs:tick", "FlipFlop.inputs:execIn"), ("OnTick.outputs:tick", "FlipFlop.inputs:execIn"), ], keys.SET_VALUES: ("OnTick.inputs:onlyPlayback", True), }, ) await controller.evaluate(graph) tick_time = controller.attribute("outputs:time", on_p_tick_node) tick_frame = controller.attribute("outputs:frame", on_p_tick_node) ontick_time = controller.attribute("outputs:time", on_tick_node) ontick_frame = controller.attribute("outputs:frame", on_tick_node) # Check that the OnPlaybackTick node doesn't trigger when playback is not active self.assertEqual(og.Controller.get(tick_time), 0) self.assertEqual(og.Controller.get(tick_frame), 0) self.assertEqual(og.Controller.get(ontick_time), 0) self.assertEqual(og.Controller.get(ontick_frame), 0) # Check that the OnPlaybackTick node does trigger when playback is active, and the values are correct timeline.play() await app.next_update_async() await controller.evaluate(graph) t0 = og.Controller.get(tick_time) f0 = og.Controller.get(tick_frame) self.assertAlmostEqual(t0, timeline.get_current_time(), places=5) self.assertAlmostEqual(f0, t0 * fps, places=5) t0 = og.Controller.get(ontick_time) f0 = og.Controller.get(ontick_frame) self.assertAlmostEqual(t0, timeline.get_current_time(), places=5) self.assertAlmostEqual(f0, t0 * fps, places=5) # ---------------------------------------------------------------------- async def test_non_action_subgraph(self): """Tests subgraphs in action graph""" controller = og.Controller() keys = og.Controller.Keys # Test that a push subgraph is ticked by the top level Action graph subgraph_path = self.TEST_GRAPH_PATH + "/push_sub" graph = omni.graph.core.get_current_graph() graph.create_subgraph(subgraph_path, evaluator="push") subgraph = graph.get_subgraph(subgraph_path) self.assertTrue(subgraph.is_valid()) sub_ff_node = self.TEST_GRAPH_PATH + "/push_sub/FlipFlop" controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("FlipFlop", "omni.graph.action.FlipFlop"), ], keys.CONNECT: ("OnTick.outputs:tick", "FlipFlop.inputs:execIn"), }, ) await og.Controller.evaluate() controller.edit(subgraph, {keys.CREATE_NODES: (sub_ff_node, "omni.graph.action.FlipFlop")}) sub_flipflop_node = subgraph.get_node(sub_ff_node) self.assertTrue(sub_flipflop_node.is_valid()) ffstate0 = og.Controller.get(controller.attribute("outputs:isA", sub_ff_node)) await og.Controller.evaluate() ffstate1 = og.Controller.get(controller.attribute("outputs:isA", sub_ff_node)) self.assertNotEqual(ffstate0, ffstate1) # ---------------------------------------------------------------------- async def test_custom_events(self): """Test SendCustomEvent and OnCustomEvent node""" controller = og.Controller() keys = og.Controller.Keys await omni.kit.app.get_app().next_update_async() (_, (_, _, event1_node, counter1_node, event2_node, counter2_node), _, _,) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("Send", "omni.graph.action.SendCustomEvent"), ("OnCustomEvent1", "omni.graph.action.OnCustomEvent"), ("Counter1", "omni.graph.action.Counter"), ("OnCustomEvent2", "omni.graph.action.OnCustomEvent"), ("Counter2", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("OnImpulse.outputs:execOut", "Send.inputs:execIn"), ("OnCustomEvent1.outputs:execOut", "Counter1.inputs:execIn"), ("OnCustomEvent2.outputs:execOut", "Counter2.inputs:execIn"), ], keys.SET_VALUES: [ ("OnCustomEvent1.inputs:onlyPlayback", False), ("OnCustomEvent2.inputs:onlyPlayback", False), ("OnImpulse.inputs:onlyPlayback", False), ("Send.inputs:eventName", "foo"), ("Send.inputs:path", "Test Path"), ("OnCustomEvent1.inputs:eventName", "foo"), ("OnCustomEvent2.inputs:eventName", "foo"), ], }, ) counter1_controller = og.Controller(og.Controller.attribute("outputs:count", counter1_node)) counter2_controller = og.Controller(og.Controller.attribute("outputs:count", counter2_node)) event1_controller = og.Controller(og.Controller.attribute("outputs:path", event1_node)) event2_controller = og.Controller(og.Controller.attribute("outputs:path", event2_node)) await omni.kit.app.get_app().next_update_async() self.assertEqual(counter1_controller.get(), 0) # trigger graph once, this will queue up the event for the next evaluation controller.edit(self.TEST_GRAPH_PATH, {keys.SET_VALUES: ("OnImpulse.state:enableImpulse", True)}) # Note that if this is a push subscription, the receivers will run this frame instead of next await omni.kit.app.get_app().next_update_async() self.assertEqual(counter1_controller.get(), 0) # This evaluation should trigger the receivers await omni.kit.app.get_app().next_update_async() # Verify that events were received self.assertEqual(counter1_controller.get(), 1) self.assertEqual(event1_controller.get(), "Test Path") self.assertEqual(counter2_controller.get(), 1) self.assertEqual(event2_controller.get(), "Test Path") # Verify the contents of the associated bundle # FIXME: Authored bundle is always empty? # bundle_contents = og.BundleContents(graph.get_default_graph_context(), event1_node, "outputs:bundle", True) # self.assertEqual(1, bundle_contents.size) # Modify the event name one receiver and sender and ensure it still works controller.edit( self.TEST_GRAPH_PATH, { keys.SET_VALUES: [("Send.inputs:eventName", "bar"), ("OnImpulse.state:enableImpulse", True)], }, ) await omni.kit.app.get_app().next_update_async() await omni.kit.app.get_app().next_update_async() # We changed the sender event name, so counter should _not_ have triggered again self.assertEqual(counter1_controller.get(), 1) # Change the receiver name to match controller.edit(self.TEST_GRAPH_PATH, {keys.SET_VALUES: ("OnCustomEvent1.inputs:eventName", "bar")}) await omni.kit.app.get_app().next_update_async() # trigger send again and verify we get it (1 frame lag for pop) controller.edit(self.TEST_GRAPH_PATH, {keys.SET_VALUES: ("OnImpulse.state:enableImpulse", True)}) await omni.kit.app.get_app().next_update_async() await omni.kit.app.get_app().next_update_async() self.assertEqual(counter1_controller.get(), 2) # ---------------------------------------------------------------------- async def test_request_driven_node(self): """Test that RequestDriven nodes are computed as expected""" controller = og.Controller() keys = og.Controller.Keys (graph, (_, counter_node), _, _,) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("Counter", "omni.graph.action.Counter"), ], keys.SET_VALUES: [("OnImpulse.inputs:onlyPlayback", False)], keys.CONNECT: ("OnImpulse.outputs:execOut", "Counter.inputs:execIn"), }, ) # After several updates, there should have been no compute calls await controller.evaluate(graph) await controller.evaluate(graph) await controller.evaluate(graph) counter_controller = og.Controller(og.Controller.attribute("outputs:count", counter_node)) self.assertEqual(counter_controller.get(), 0) # change OnImpulse state attrib. The node should now request compute controller.edit(self.TEST_GRAPH_PATH, {keys.SET_VALUES: ("OnImpulse.state:enableImpulse", True)}) await controller.evaluate(graph) self.assertEqual(counter_controller.get(), 1) # more updates should not result in more computes await controller.evaluate(graph) await controller.evaluate(graph) await controller.evaluate(graph) self.assertEqual(counter_controller.get(), 1) # ---------------------------------------------------------------------- async def test_stage_events(self): """Test OnStageEvent""" controller = og.Controller() keys = og.Controller.Keys (_, (_, _, _, counter_node, counter2_node, counter3_node), _, _,) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnStageEvent", "omni.graph.action.OnStageEvent"), ("OnStageEvent2", "omni.graph.action.OnStageEvent"), ("OnStageEvent3", "omni.graph.action.OnStageEvent"), ("Counter", "omni.graph.action.Counter"), ("Counter2", "omni.graph.action.Counter"), ("Counter3", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("OnStageEvent.outputs:execOut", "Counter.inputs:execIn"), ("OnStageEvent2.outputs:execOut", "Counter2.inputs:execIn"), ("OnStageEvent3.outputs:execOut", "Counter3.inputs:execIn"), ], keys.SET_VALUES: [ ("OnStageEvent.inputs:eventName", "Selection Changed"), ("OnStageEvent.inputs:onlyPlayback", False), ("OnStageEvent2.inputs:eventName", "Animation Stop Play"), ("OnStageEvent2.inputs:onlyPlayback", True), ("OnStageEvent3.inputs:eventName", "Animation Start Play"), ("OnStageEvent3.inputs:onlyPlayback", True), ], }, ) await omni.kit.app.get_app().next_update_async() self.assertEqual(og.Controller.get(controller.attribute("outputs:count", counter_node)), 0) self.assertEqual(og.Controller.get(controller.attribute("outputs:count", counter2_node)), 0) self.assertEqual(og.Controller.get(controller.attribute("outputs:count", counter3_node)), 0) selection = omni.usd.get_context().get_selection() selection.set_selected_prim_paths([self.TEST_GRAPH_PATH + "/OnStageEvent"], False) # 1 frame delay on the pop, 1 frame delay on the compute await omni.kit.app.get_app().next_update_async() await omni.kit.app.get_app().next_update_async() self.assertEqual(og.Controller.get(controller.attribute("outputs:count", counter_node)), 1) self.assertEqual(og.Controller.get(controller.attribute("outputs:count", counter2_node)), 0) self.assertEqual(og.Controller.get(controller.attribute("outputs:count", counter3_node)), 0) # Verify that start/stop events work when only-plackback is true timeline = omni.timeline.get_timeline_interface() timeline.set_start_time(1.0) timeline.set_end_time(10.0) timeline.play() await omni.kit.app.get_app().next_update_async() await omni.kit.app.get_app().next_update_async() self.assertEqual(og.Controller.get(controller.attribute("outputs:count", counter2_node)), 0) self.assertEqual(og.Controller.get(controller.attribute("outputs:count", counter3_node)), 1) await omni.kit.app.get_app().next_update_async() self.assertEqual(og.Controller.get(controller.attribute("outputs:count", counter2_node)), 0) self.assertEqual(og.Controller.get(controller.attribute("outputs:count", counter3_node)), 1) timeline.stop() await omni.kit.app.get_app().next_update_async() await omni.kit.app.get_app().next_update_async() self.assertEqual(og.Controller.get(controller.attribute("outputs:count", counter2_node)), 1) self.assertEqual(og.Controller.get(controller.attribute("outputs:count", counter3_node)), 1) await controller.evaluate() self.assertEqual(og.Controller.get(controller.attribute("outputs:count", counter2_node)), 1) self.assertEqual(og.Controller.get(controller.attribute("outputs:count", counter3_node)), 1) # ----------------------------------------------------------------------- async def test_add_prim_relationship(self): """Test AddPrimRelationship""" controller = og.Controller() # Check that we can add relationship to a prim and get that relationship # # +---------+ +---------------------+ # | ONTICK +-->| AddPrimRelationship + # +---------+ +---------------------+ # usd_context = omni.usd.get_context() stage = usd_context.get_stage() controller = og.Controller() keys = og.Controller.Keys controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("AddRel", "omni.graph.action.AddPrimRelationship"), ], keys.CREATE_PRIMS: [ ("/Test", {}), ("/Target", {}), ], keys.CONNECT: [("OnTick.outputs:tick", "AddRel.inputs:execIn")], keys.SET_VALUES: [ ("OnTick.inputs:onlyPlayback", False), ("AddRel.inputs:path", "/Test"), ("AddRel.inputs:name", "rel"), ("AddRel.inputs:target", "/Target"), ], }, ) prim = stage.GetPrimAtPath("/Test") await controller.evaluate() rel = prim.GetRelationship("rel") targets = rel.GetTargets() self.assertEqual(len(targets), 1) self.assertTrue(str(targets[0]) == "/Target") # ---------------------------------------------------------------------- async def test_switch(self): """Test the Switch nodes""" controller = og.Controller() keys = og.Controller.Keys # Test that an execution connection is correctly triggering the downstream node once per evaluation (graph, (_, switch_node), _, _,) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("Switch", "omni.graph.action.SwitchToken"), ], keys.SET_VALUES: [ ("OnTick.inputs:onlyPlayback", False), ], keys.CONNECT: [ ("OnTick.outputs:tick", "Switch.inputs:execIn"), ], }, ) def add_input(index: int): controller.create_attribute( switch_node, f"inputs:branch{index:02}", "token", og.AttributePortType.ATTRIBUTE_PORT_TYPE_INPUT, ) controller.create_attribute( switch_node, f"outputs:output{index:02}", "execution", og.AttributePortType.ATTRIBUTE_PORT_TYPE_OUTPUT, ) og.Controller.set(controller.attribute(f"inputs:branch{index:02}", switch_node), f"{index:02}") for index in range(5): add_input(index) for index in range(5): og.Controller.set(controller.attribute("inputs:value", switch_node), f"{index:02}") await controller.evaluate(graph) for index2 in range(5): expected_val = ( og.ExecutionAttributeState.DISABLED if index2 != index else og.ExecutionAttributeState.ENABLED ) self.assertEqual( og.Controller.get(controller.attribute(f"outputs:output{index2:02}", switch_node)), expected_val )
25,795
Python
45.987249
120
0.584028
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/tests/test_action_graph_compounds.py
"""Action Graph with Compounds""" import carb.events import omni.graph.core as og import omni.graph.core._unstable as ogu import omni.graph.core.tests as ogts import omni.kit.app # ====================================================================== class TestActionGraphCompounds(ogts.OmniGraphTestCase): """Tests action graph evaluator functionality""" TEST_GRAPH_PATH = "/World/TestGraph" async def setUp(self): """Set up test environment, to be torn down when done""" await super().setUp() og.Controller.edit({"graph_path": self.TEST_GRAPH_PATH, "evaluator_name": "execution"}) # ------------------------------------------------------------------------ async def test_create_action_subgraph_with_command(self): """Validates the create compound subgraph command in an action graph""" # "Add" will only compute if the subgraph is executed as a push graph OR # is flattened into the parent graph # ┌───────────────┐ # │Subgraph │ # ┌─────┐ │ ┌─────┐ │ # │Const├───►────► Add ├───►├────┐ # └─────┘ │ └──▲──┘ │ │ # │ │ │ │ # │ ┌─────┴┐ │ │ # │ │Const2│ │ │ # │ └──────┘ │ │ # │ │ │ # └───────────────┘ │ # ┌───────┐ ┌▼────────────┐ # │OnTick ├──────────────────────►WriteVariable│ # └───────┘ └─────────────┘ controller = og.Controller() keys = controller.Keys (graph, (_, _, const_2, add, _), _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("Const", "omni.graph.nodes.ConstantInt"), ("Const2", "omni.graph.nodes.ConstantInt"), ("Add", "omni.graph.nodes.Add"), ("WriteVariable", "omni.graph.core.WriteVariable"), ], keys.CREATE_VARIABLES: [ ("int_var", og.Type(og.BaseDataType.INT), 0), ], keys.CONNECT: [ ("Const.inputs:value", "Add.inputs:a"), ("Const2.inputs:value", "Add.inputs:b"), ("Add.outputs:sum", "WriteVariable.inputs:value"), ("OnTick.outputs:tick", "WriteVariable.inputs:execIn"), ], keys.SET_VALUES: [ ("OnTick.inputs:onlyPlayback", False), ("WriteVariable.inputs:variableName", "int_var"), ], }, ) await og.Controller.evaluate(graph) self.assertEquals(graph.get_variables()[0].get(graph.get_default_graph_context()), 0) controller.edit( self.TEST_GRAPH_PATH, {keys.SET_VALUES: [("Const.inputs:value", 22), ("Const2.inputs:value", 20)]} ) ogu.cmds.ReplaceWithCompoundSubgraph( nodes=[add.get_prim_path(), const_2.get_prim_path()], compound_name="Subgraph" ) await og.Controller.evaluate(graph) self.assertEquals(graph.get_variables()[0].get(graph.get_default_graph_context()), 42.0) # ------------------------------------------------------------------------ async def test_event_node_in_subgraph(self): """ Test that a compute-on-request node inside a subgraph works """ controller = og.Controller(update_usd=True) keys = controller.Keys (graph, (compound_node,), _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ( "CompoundOuter", { keys.CREATE_NODES: [ ( "CompoundInner", { keys.CREATE_NODES: [ ("OnEvent", "omni.graph.action.OnMessageBusEvent"), ("Counter", "omni.graph.action.Counter"), ], keys.CONNECT: [("OnEvent.outputs:execOut", "Counter.inputs:execIn")], keys.SET_VALUES: [ ("OnEvent.inputs:onlyPlayback", False), ("OnEvent.inputs:eventName", "testEvent"), ], }, ) ] }, ), ], }, ) # One compute for the first-time subscribe. await omni.kit.app.get_app().next_update_async() msg = carb.events.type_from_string("testEvent") counter_attr = og.Controller.attribute( f"{compound_node.get_prim_path()}/Subgraph/CompoundInner/Subgraph/Counter.outputs:count" ) await og.Controller.evaluate(graph) self.assertEqual(counter_attr.get(), 0) omni.kit.app.get_app().get_message_bus_event_stream().push(msg) # Wait for one kit update to allow the event-push mechanism to trigger the node callback. await omni.kit.app.get_app().next_update_async() self.assertEqual(counter_attr.get(), 1) await omni.kit.app.get_app().next_update_async() self.assertEqual(counter_attr.get(), 1) omni.kit.app.get_app().get_message_bus_event_stream().push(msg) await omni.kit.app.get_app().next_update_async() self.assertEqual(counter_attr.get(), 2)
5,943
Python
43.358209
110
0.434629
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/tests/test_action_graph_evaluation_02.py
"""Action Graph Evaluation Tests, Part 2""" from enum import Enum, auto import omni.graph.core as og import omni.graph.core.tests as ogts import omni.kit.test import omni.usd from omni.graph.action import get_interface from pxr import Gf # ====================================================================== class TestActionGraphEvaluation(ogts.OmniGraphTestCase): """Tests action graph evaluator functionality""" TEST_GRAPH_PATH = "/World/TestGraph" async def setUp(self): """Set up test environment, to be torn down when done""" await super().setUp() og.Controller.edit({"graph_path": self.TEST_GRAPH_PATH, "evaluator_name": "execution"}) # ---------------------------------------------------------------------- async def test_latent_fan_out(self): """Test latent nodes when part of parallel evaluation""" # +------------+ # +---->|TickCounterA| # | +------------+ # | # +--------++ +----------+ # +-> TickA +--->|FinishedA | # | +---------+ +----------+ # +---------+ +-----------+ | # |OnImpulse+-->|TickCounter+-+ # +---------+ +-----------+ | # | +---------+ +----------+ # +>| TickB +--->|FinishedB | # +--------++ +----------+ # | # | +------------+ # +---->|TickCounterB| # +------------+ controller = og.Controller() keys = og.Controller.Keys (graph, nodes, _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("TickA", "omni.graph.action.Countdown"), ("TickB", "omni.graph.action.Countdown"), ("TickCounter", "omni.graph.action.Counter"), ("TickCounterA", "omni.graph.action.Counter"), ("TickCounterB", "omni.graph.action.Counter"), ("FinishCounterA", "omni.graph.action.Counter"), ("FinishCounterB", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("OnImpulse.outputs:execOut", "TickCounter.inputs:execIn"), ("TickCounter.outputs:execOut", "TickA.inputs:execIn"), ("TickCounter.outputs:execOut", "TickB.inputs:execIn"), ("TickA.outputs:tick", "TickCounterA.inputs:execIn"), ("TickB.outputs:tick", "TickCounterB.inputs:execIn"), ("TickA.outputs:finished", "FinishCounterA.inputs:execIn"), ("TickB.outputs:finished", "FinishCounterB.inputs:execIn"), ], keys.SET_VALUES: [ ("TickA.inputs:duration", 2), ("TickB.inputs:duration", 2), ("OnImpulse.inputs:onlyPlayback", False), ("OnImpulse.state:enableImpulse", True), ], }, ) (_, _, _, tick_counter, tick_counter_a, tick_counter_b, finish_counter_a, finish_counter_b) = nodes def check_counts(t_def, t_a, t_b, f_a, f_b): for node, expected in ( (tick_counter, t_def), (tick_counter_a, t_a), (tick_counter_b, t_b), (finish_counter_a, f_a), (finish_counter_b, f_b), ): count = og.Controller.get(controller.attribute("outputs:count", node)) self.assertEqual(count, expected, node.get_prim_path()) await controller.evaluate(graph) check_counts(1, 0, 0, 0, 0) await controller.evaluate(graph) check_counts(1, 1, 1, 0, 0) await controller.evaluate(graph) check_counts(1, 2, 2, 0, 0) await controller.evaluate(graph) check_counts(1, 2, 2, 1, 1) # ---------------------------------------------------------------------- async def test_loop_cancel(self): """Test loop canceling""" # Check that a loop can be canceled. # We set up the loop to run for 7 iterations, but cancel when we hit iteration 2. # # +--------+ # +------------->|COUNTER2| # | finish +--------+ # +---------+ +----------+ +----------+ +-------+ +---------+ # | IMPULSE +-->| FOR-LOOP +--->| COMPARE +--->|BRANCH +--->| COUNTER1| # +---------+ +----------+ +----------+ +-------+ +---------+ # ^ | # | cancel | # +------------------------------+ controller = og.Controller() keys = og.Controller.Keys (graph, (_, for_loop, _, _, count_1, count_2), _, _,) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("ForLoop", "omni.graph.action.ForLoop"), ("Compare", "omni.graph.nodes.Compare"), ("Branch", "omni.graph.action.Branch"), ("Counter1", "omni.graph.action.Counter"), ("Counter2", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("OnImpulse.outputs:execOut", "ForLoop.inputs:execIn"), ("ForLoop.outputs:loopBody", "Branch.inputs:execIn"), ("ForLoop.outputs:index", "Compare.inputs:a"), ("ForLoop.outputs:finished", "Counter2.inputs:execIn"), ("Compare.outputs:result", "Branch.inputs:condition"), ("Branch.outputs:execFalse", "Counter1.inputs:execIn"), ("Branch.outputs:execTrue", "ForLoop.inputs:breakLoop"), ], keys.SET_VALUES: [ ("OnImpulse.inputs:onlyPlayback", False), ("Compare.inputs:b", 2, "int"), ("Compare.inputs:operation", "=="), ("ForLoop.inputs:stop", 6), ], }, ) await controller.evaluate(graph) # Trigger graph once. controller.edit(self.TEST_GRAPH_PATH, {keys.SET_VALUES: ("OnImpulse.state:enableImpulse", True)}) await controller.evaluate(graph) # Verify the loop body only counted 2 times, and finish once. c_1 = og.Controller.get(og.Controller.attribute("outputs:count", count_1)) c_2 = og.Controller.get(og.Controller.attribute("outputs:count", count_2)) index = og.Controller.get(og.Controller.attribute("outputs:index", for_loop)) self.assertEqual(index, 2) self.assertEqual(c_1, 2) self.assertEqual(c_2, 1) # ---------------------------------------------------------------------- async def test_loop_sideffects(self): """Test that nodes in loops can read results of external side effects""" # +----------+ +---------+ +-------+ +--------------------+ # | OnImpulse+----->| ForLoop +----->| Add +------>| WritePrimAttribute | # +----------+ +---------+ +-------+ +--------------------+ # ^ # +-----------------+ | # |ReadPrimAttribute +---+ # +-----------------+ controller = og.Controller() keys = og.Controller.Keys (graph, _, prims, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("Get", "omni.graph.nodes.ReadPrimAttribute"), ("ForLoop1", "omni.graph.action.ForLoop"), ("Add", "omni.graph.nodes.Add"), ("Set", "omni.graph.nodes.WritePrimAttribute"), ], keys.CREATE_PRIMS: ("/World/Accumulator", {"acc": ("int", 0)}), keys.SET_VALUES: [ ("Get.inputs:name", "acc"), ("Get.inputs:usePath", True), ("Get.inputs:primPath", "/World/Accumulator"), ("ForLoop1.inputs:start", 0), ("ForLoop1.inputs:stop", 5), ("Set.inputs:name", "acc"), ("Set.inputs:usePath", True), ("Set.inputs:primPath", "/World/Accumulator"), ("OnImpulse.inputs:onlyPlayback", False), ], keys.CONNECT: [ ("OnImpulse.outputs:execOut", "ForLoop1.inputs:execIn"), ("ForLoop1.outputs:loopBody", "Set.inputs:execIn"), ("Get.outputs:value", "Add.inputs:a"), ("ForLoop1.outputs:value", "Add.inputs:b"), ("Add.outputs:sum", "Set.inputs:value"), ], }, ) await controller.evaluate(graph) # Trigger graph evaluation once. # Should result in sum = 0 + 0 + 1 + 2 + 3 + 4. controller.edit(self.TEST_GRAPH_PATH, {keys.SET_VALUES: [("OnImpulse.state:enableImpulse", True)]}) await controller.evaluate(graph) self.assertEqual(prims[0].GetAttribute("acc").Get(), 10) # ---------------------------------------------------------------------- async def test_nested_forloop(self): """Test nested ForLoop nodes""" keys = og.Controller.Keys controller = og.Controller() (graph, (_, _, _, _, _, _, write_node), _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("Const", "omni.graph.nodes.ConstantInt2"), ("Add", "omni.graph.nodes.Add"), ("Branch", "omni.graph.action.Branch"), ("For", "omni.graph.action.ForLoop"), ("For2", "omni.graph.action.ForLoop"), ("Write1", "omni.graph.nodes.WritePrimAttribute"), ], keys.CREATE_PRIMS: ( "/World/TestPrim", { "val1": ("int[2]", Gf.Vec2i(1, 1)), }, ), keys.SET_VALUES: [ ("OnTick.inputs:onlyPlayback", False), ("Const.inputs:value", [1, 2]), ("Write1.inputs:name", "val1"), ("Write1.inputs:primPath", "/World/TestPrim"), ("Write1.inputs:usePath", True), ("For.inputs:stop", 3), ("For2.inputs:start", 4), ("For2.inputs:step", 2), ("For2.inputs:stop", 10), ("Branch.inputs:condition", True), ], keys.CONNECT: [ ("OnTick.outputs:tick", "For.inputs:execIn"), ("For.outputs:loopBody", "Branch.inputs:execIn"), ("Branch.outputs:execTrue", "For2.inputs:execIn"), ("For2.outputs:loopBody", "Write1.inputs:execIn"), ("For2.outputs:value", "Add.inputs:a"), ("Const.inputs:value", "Add.inputs:b"), ("Add.outputs:sum", "Write1.inputs:value"), ], }, ) await controller.evaluate(graph) context = omni.usd.get_context() stage = context.get_stage() # For2 should loop over the range [4, 6, 8, 10). self.assertEqual(9, write_node.get_compute_count()) # [1, 2] + 8 = [9, 10]. self.assertListEqual([9, 10], list(stage.GetAttributeAtPath("/World/TestPrim.val1").Get())) # ---------------------------------------------------------------------- async def test_om_63924(self): """Test OM-63924 bug is fixed""" # The problem here was that if there was fan in to a node which was # computed once and then totally unwound before the other history was # processed, there would never be a deferred activation and so the 2nd # compute would never happen. Instead we want to only unwind one history # at a time to ensure each one is fully evaluated. i = 2 class OnForEachEventPy: """Helper Python node that implements ForEach logic""" @staticmethod def compute(context: og.GraphContext, node: og.Node): """Compute method""" nonlocal i inputs_go = node.get_attribute("inputs:go") go_val = og.Controller.get(inputs_go) if not go_val: return True if i > 0: og.Controller.set( node.get_attribute("outputs:execOut"), og.ExecutionAttributeState.ENABLED_AND_PUSH ) og.Controller.set(node.get_attribute("outputs:syncValue"), i) i -= 1 return True @staticmethod def get_node_type() -> str: """Get node type""" return "omni.graph.test.OnForEachEventPy" @staticmethod def initialize_type(node_type: og.NodeType): """Initialize node attributes""" node_type.add_input( "inputs:go", "bool", False, ) node_type.add_output("outputs:execOut", "execution", True) node_type.add_output("outputs:syncValue", "uint64", True) return True og.register_node_type(OnForEachEventPy, 1) class NoOpPy: """Helper Python node that performs no internal operation""" @staticmethod def compute(context: og.GraphContext, node: og.Node): """Compute method""" og.Controller.set(node.get_attribute("outputs:execOut"), og.ExecutionAttributeState.ENABLED) return True @staticmethod def get_node_type() -> str: """Get node type""" return "omni.graph.test.NoOpPy" @staticmethod def initialize_type(node_type: og.NodeType): """Initialize node attributes""" node_type.add_input( "inputs:execIn", "execution", True, ) node_type.add_output("outputs:execOut", "execution", True) return True og.register_node_type(NoOpPy, 1) controller = og.Controller() keys = og.Controller.Keys (graph, (for_each, _, _, _, _, no_op_2), _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("PostProcessDispatcher", "omni.graph.test.OnForEachEventPy"), ("TSA1", "omni.graph.action.SyncGate"), ("TSA0", "omni.graph.action.SyncGate"), ("TestSyncAccum", "omni.graph.action.SyncGate"), ("TestPrimBbox", "omni.graph.test.NoOpPy"), ("NoOpPy2", "omni.graph.test.NoOpPy"), ], keys.CONNECT: [ ("PostProcessDispatcher.outputs:execOut", "TSA0.inputs:execIn"), ("PostProcessDispatcher.outputs:execOut", "TSA1.inputs:execIn"), ("TSA1.outputs:execOut", "TestSyncAccum.inputs:execIn"), ("TSA0.outputs:execOut", "TestPrimBbox.inputs:execIn"), ("TestPrimBbox.outputs:execOut", "TestSyncAccum.inputs:execIn"), ("TestSyncAccum.outputs:execOut", "NoOpPy2.inputs:execIn"), ("PostProcessDispatcher.outputs:syncValue", "TSA1.inputs:syncValue"), ("PostProcessDispatcher.outputs:syncValue", "TSA0.inputs:syncValue"), ("PostProcessDispatcher.outputs:syncValue", "TestSyncAccum.inputs:syncValue"), ], }, ) og.Controller.set(controller.attribute("inputs:go", for_each), True) await controller.evaluate(graph) # Verify the final sync gate triggered due to being computed 2x. exec_out = og.Controller.get(controller.attribute("outputs:execOut", no_op_2)) self.assertEqual(exec_out, og.ExecutionAttributeState.ENABLED) # ---------------------------------------------------------------------- async def test_retrigger_latent(self): """Test that latent nodes can be re-triggered""" want_debug = False e_state = og.ExecutionAttributeState tick_count = 0 boop_count = 0 exec_in_latent_count = 0 max_ticks = 20 class CancelTickerPy: """Helper node type which does latent ticking and can be canceled, and has an independent counter "boop" """ @staticmethod def compute(context: og.GraphContext, node: og.Node): """Compute method""" nonlocal tick_count nonlocal boop_count nonlocal exec_in_latent_count exec_in = node.get_attribute("inputs:execIn") exec_in_val = og.Controller.get(exec_in) cancel = node.get_attribute("inputs:cancel") cancel_val = og.Controller.get(cancel) boop = node.get_attribute("inputs:boop") boop_val = og.Controller.get(boop) if want_debug: print(f"### {tick_count} execIn={exec_in_val} cancel={cancel_val} boop={boop_val}") if cancel_val == e_state.ENABLED: # Finish latent by cancel. og.Controller.set(node.get_attribute("outputs:canceled"), e_state.LATENT_FINISH) self.assertEqual(exec_in_val, e_state.DISABLED) self.assertEqual(boop_val, e_state.DISABLED) tick_count = 0 return True if exec_in_val == e_state.ENABLED: self.assertEqual(cancel_val, e_state.DISABLED) self.assertEqual(boop_val, e_state.DISABLED) if tick_count > 0: # execIn triggered while in latent - should not be possible. exec_in_latent_count += 1 else: og.Controller.set(node.get_attribute("outputs:tick"), e_state.LATENT_PUSH) return True # We are ticking. self.assertEqual(cancel_val, e_state.DISABLED) tick_count += 1 if tick_count < max_ticks: og.Controller.set(node.get_attribute("outputs:tick"), e_state.ENABLED) else: # Finish latent naturally. og.Controller.set(node.get_attribute("outputs:execOut"), e_state.LATENT_FINISH) tick_count = 0 if boop_val == e_state.ENABLED: # We get here during latent ticking, if the boop input is enabled. self.assertEqual(exec_in_val, e_state.DISABLED) self.assertEqual(cancel_val, e_state.DISABLED) boop_count += 1 return True @staticmethod def get_node_type() -> str: """Get node type""" return "omni.graph.test.CancelTickerPy" @staticmethod def initialize_type(node_type: og.NodeType): """Initialize node attributes""" node_type.add_input( "inputs:execIn", "execution", True, ) node_type.add_input( "inputs:cancel", "execution", True, ) node_type.add_input( "inputs:boop", "execution", True, ) node_type.add_output("outputs:tick", "execution", True) node_type.add_output("outputs:canceled", "execution", True) node_type.add_output("outputs:execOut", "execution", True) return True og.register_node_type(CancelTickerPy, 1) controller = og.Controller() keys = og.Controller.Keys (graph, (ticker, start, _, cancel, boop, _, _, counter), _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("Ticker", "omni.graph.test.CancelTickerPy"), ("Start", "omni.graph.action.OnImpulseEvent"), ("Start2", "omni.graph.action.OnImpulseEvent"), ("Cancel", "omni.graph.action.OnImpulseEvent"), ("Boop", "omni.graph.action.OnImpulseEvent"), ("Once", "omni.graph.action.Once"), ("Once2", "omni.graph.action.Once"), ("Counter", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("Start.outputs:execOut", "Ticker.inputs:execIn"), ("Start2.outputs:execOut", "Ticker.inputs:execIn"), ("Cancel.outputs:execOut", "Once.inputs:execIn"), ("Once.outputs:once", "Ticker.inputs:cancel"), ("Once.outputs:after", "Ticker.inputs:cancel"), ("Cancel.outputs:execOut", "Once2.inputs:execIn"), ("Boop.outputs:execOut", "Ticker.inputs:boop"), ("Once2.outputs:once", "Ticker.inputs:cancel"), ("Once2.outputs:after", "Ticker.inputs:cancel"), ("Ticker.outputs:tick", "Counter.inputs:execIn"), ], keys.SET_VALUES: [ ("Start.inputs:onlyPlayback", False), ("Start2.inputs:onlyPlayback", False), ("Cancel.inputs:onlyPlayback", False), ("Boop.inputs:onlyPlayback", False), ], }, ) # Cancel, check nothing happens. og.Controller.set(controller.attribute("state:enableImpulse", cancel), True) await controller.evaluate(graph) exec_out = og.Controller.get(controller.attribute("outputs:tick", ticker)) self.assertEqual(exec_out, e_state.DISABLED) # Start ticking. og.Controller.set(controller.attribute("state:enableImpulse", start), True) await controller.evaluate(graph) # Starts latent state. await controller.evaluate(graph) # Tick 1 self.assertEqual(tick_count, 1) # Verify the tick has started. exec_out = og.Controller.get(controller.attribute("outputs:tick", ticker)) self.assertEqual(exec_out, e_state.ENABLED) await controller.evaluate(graph) # Tick 2 self.assertEqual(tick_count, 2) exec_out = og.Controller.get(controller.attribute("outputs:tick", ticker)) self.assertEqual(exec_out, e_state.ENABLED) await controller.evaluate(graph) # Tick 3 self.assertEqual(tick_count, 3) # Boop - node keeps ticking. og.Controller.set(controller.attribute("state:enableImpulse", boop), True) # Boop will trigger a compute, which increments boop + ticks AND the normal latent tick. await controller.evaluate(graph) self.assertEqual(boop_count, 1) self.assertEqual(tick_count, 5) # Now check that the next tick can run WITHOUT inputs:boop being high. await controller.evaluate(graph) self.assertEqual(boop_count, 1) # No change in boop count (OM-64856). self.assertEqual(tick_count, 6) # Now check that we can't re-trigger execIn. self.assertEqual(exec_in_latent_count, 0) og.Controller.set(controller.attribute("state:enableImpulse", start), True) # Start will not trigger any compute because the node is latent. await controller.evaluate(graph) self.assertEqual(exec_in_latent_count, 0) self.assertEqual(boop_count, 1) self.assertEqual(tick_count, 7) # Unset the impulse. og.Controller.set(controller.attribute("state:enableImpulse", start), False) # Now check the normal tick proceeds as normal. await controller.evaluate(graph) self.assertEqual(boop_count, 1) self.assertEqual(tick_count, 8) # Cancel. counter_attr = controller.attribute("outputs:count", counter) count_0 = og.Controller.get(counter_attr) og.Controller.set(controller.attribute("state:enableImpulse", cancel), True) await controller.evaluate(graph) # Latent finish. await controller.evaluate(graph) # No action. await controller.evaluate(graph) # No action. count_1 = og.Controller.get(counter_attr) self.assertEqual(count_0 + 1, count_1) # ---------------------------------------------------------------------- async def test_simple_expression(self): """Test ActionGraph simple expression of add nodes""" keys = og.Controller.Keys controller = og.Controller() (graph, _, _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("Const", "omni.graph.nodes.ConstantDouble3"), ("Add", "omni.graph.nodes.Add"), ("Add2", "omni.graph.nodes.Add"), ("Add3", "omni.graph.nodes.Add"), ("PostAdd", "omni.graph.nodes.Add"), ("Write1", "omni.graph.nodes.WritePrimAttribute"), ("Write2", "omni.graph.nodes.WritePrimAttribute"), ("Write3", "omni.graph.nodes.WritePrimAttribute"), ], keys.CREATE_PRIMS: ( "/World/TestPrim", { "val1": ("double[3]", Gf.Vec3d(1, 1, 1)), "val2": ("double[3]", Gf.Vec3d(2, 2, 2)), "val3": ("double[3]", Gf.Vec3d(2, 2, 2)), }, ), keys.SET_VALUES: [ ("OnTick.inputs:onlyPlayback", False), ("Const.inputs:value", [1, 2, 3]), ("Write1.inputs:name", "val1"), ("Write1.inputs:primPath", "/World/TestPrim"), ("Write1.inputs:usePath", True), ("Write2.inputs:name", "val2"), ("Write2.inputs:primPath", "/World/TestPrim"), ("Write2.inputs:usePath", True), ("Write3.inputs:name", "val3"), ("Write3.inputs:primPath", "/World/TestPrim"), ("Write3.inputs:usePath", True), ], keys.CONNECT: [ ("OnTick.outputs:tick", "Write1.inputs:execIn"), ("Write1.outputs:execOut", "Write2.inputs:execIn"), ("Write2.outputs:execOut", "Write3.inputs:execIn"), ("Const.inputs:value", "Add.inputs:a"), ("Const.inputs:value", "Add.inputs:b"), ("Const.inputs:value", "Add2.inputs:a"), ("Const.inputs:value", "Add2.inputs:b"), ("Add.outputs:sum", "Add3.inputs:a"), ("Add2.outputs:sum", "Add3.inputs:b"), ("Add3.outputs:sum", "Write1.inputs:value"), ("Add3.outputs:sum", "Write2.inputs:value"), ("Add3.outputs:sum", "PostAdd.inputs:a"), ("Add3.outputs:sum", "PostAdd.inputs:b"), ("PostAdd.outputs:sum", "Write3.inputs:value"), ], }, ) await controller.evaluate(graph) context = omni.usd.get_context() stage = context.get_stage() await omni.kit.app.get_app().next_update_async() self.assertListEqual([4, 8, 12], list(stage.GetAttributeAtPath("/World/TestPrim.val1").Get())) self.assertListEqual([4, 8, 12], list(stage.GetAttributeAtPath("/World/TestPrim.val2").Get())) self.assertListEqual([8, 16, 24], list(stage.GetAttributeAtPath("/World/TestPrim.val3").Get())) # ---------------------------------------------------------------------- async def test_stateful_flowcontrol_evaluation(self): """Test that stateful flow control nodes are fully evaluated""" # b # +----------+ +---------+ # +--->| Sequence +-->|Counter1 | # | +----------+ +---------+ # +-----------+ | # | OnImpulse +-+ # +-----------+ | # | +----------+ +----------+ # +--->| ForLoop1 +-->| Counter2 | # +----------+ +----------+ # finished controller = og.Controller() keys = og.Controller.Keys (graph, (_, _, counter1_node, _, counter2_node), _, _,) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("Sequence", "omni.graph.action.Sequence"), ("Counter1", "omni.graph.action.Counter"), ("ForLoop1", "omni.graph.action.ForLoop"), ("Counter2", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("OnImpulse.outputs:execOut", "Sequence.inputs:execIn"), ("Sequence.outputs:b", "Counter1.inputs:execIn"), ("OnImpulse.outputs:execOut", "ForLoop1.inputs:execIn"), ("ForLoop1.outputs:finished", "Counter2.inputs:execIn"), ], keys.SET_VALUES: [("OnImpulse.inputs:onlyPlayback", False), ("ForLoop1.inputs:stop", 10)], }, ) await controller.evaluate(graph) # Trigger graph once. controller.edit(self.TEST_GRAPH_PATH, {keys.SET_VALUES: ("OnImpulse.state:enableImpulse", True)}) await controller.evaluate(graph) # Verify that counter was called in spite of sequence 'a' being disconnected. self.assertEqual(og.Controller.get(controller.attribute("outputs:count", counter1_node)), 1) # Verify that counter was called in spite of there being no loopBody - execution evaluator has to still trigger # the loop 11 times despite there being no downstream connection. self.assertEqual(og.Controller.get(controller.attribute("outputs:count", counter2_node)), 1) # ---------------------------------------------------------------------- async def test_unresolve_on_disconnect(self): """Tests unresolving attribs when action node is disconnected""" controller = og.Controller() keys = og.Controller.Keys (graph, (_, for_each_node, _, _), _, _,) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("ForEach", "omni.graph.action.ForEach"), ("IntArrayPrim", "omni.graph.nodes.ReadPrimAttribute"), ("IntSinkPrim", "omni.graph.nodes.WritePrimAttribute"), ], keys.CREATE_PRIMS: [ ("/World/IntArray", {"myintarray": ("int[]", range(10))}), ("/World/IntSink", {"myint": ("int", 0)}), ], keys.CONNECT: [ ("OnTick.outputs:tick", "ForEach.inputs:execIn"), ("IntArrayPrim.outputs:value", "ForEach.inputs:arrayIn"), ("ForEach.outputs:element", "IntSinkPrim.inputs:value"), ], keys.SET_VALUES: [ ("IntArrayPrim.inputs:name", "myintarray"), ("IntArrayPrim.inputs:primPath", "/World/IntArray"), ("IntArrayPrim.inputs:usePath", True), ("IntSinkPrim.inputs:name", "myint"), ("IntSinkPrim.inputs:primPath", "/World/IntSink"), ("IntSinkPrim.inputs:usePath", True), ("OnTick.inputs:onlyPlayback", False), ], }, ) await controller.evaluate(graph) array_attr = controller.attribute("inputs:arrayIn", for_each_node) element_attr = controller.attribute("outputs:element", for_each_node) self.assertEqual(element_attr.get_resolved_type(), og.Type(og.BaseDataType.INT, 1, 0)) # When we disconnect all data connections, they should unresolve even though we still # have the execution connection in place. controller.edit( self.TEST_GRAPH_PATH, { keys.DISCONNECT: [ ("IntArrayPrim.outputs:value", "ForEach.inputs:arrayIn"), ("ForEach.outputs:element", "IntSinkPrim.inputs:value"), ] }, ) await controller.evaluate(graph) self.assertEqual(array_attr.get_resolved_type(), og.Type(og.BaseDataType.UNKNOWN)) self.assertEqual(element_attr.get_resolved_type(), og.Type(og.BaseDataType.UNKNOWN)) async def test_actiongraph_abi(self): """Tests IActionGraph ABI functions""" i_ag = get_interface() g_exec_out = 0 class State(Enum): ENABLED = auto() ENABLED_AND_PUSH = auto() START = auto() END = auto() class _TestActionGraphPy: """Helper node to test behavior""" class _State: val = None instance_states = {} @staticmethod def initialize(context, node): _TestActionGraphPy.instance_states[node] = _TestActionGraphPy._State() @staticmethod def compute(_: og.GraphContext, node: og.Node) -> bool: """Compute method""" nonlocal g_exec_out if not i_ag.get_latent_state(): self.assertTrue(i_ag.get_execution_enabled("inputs:execIn")) self.assertFalse(i_ag.get_execution_enabled("inputs:execUnused")) self.assertFalse(i_ag.get_execution_enabled("inputs:boolUnused")) state = _TestActionGraphPy.instance_states[node] if state.val == State.ENABLED_AND_PUSH: # Must be re-entering i_ag.set_execution_enabled("outputs:finished_from_pushed") state.val = None return True if state.val == State.START: # Must be in latent state tick self.assertTrue(i_ag.get_latent_state()) i_ag.end_latent_state() i_ag.set_execution_enabled("outputs:finished_from_latent") state.val = None return True if g_exec_out == State.ENABLED: i_ag.set_execution_enabled("outputs:execOut") elif g_exec_out == State.ENABLED_AND_PUSH: i_ag.set_execution_enabled_and_pushed("outputs:execOut") elif g_exec_out == State.START: self.assertFalse(i_ag.get_latent_state()) i_ag.start_latent_state() elif g_exec_out == State.END: i_ag.end_latent_state() state.val = g_exec_out return True @staticmethod def get_node_type() -> str: """Get node type""" return "omni.graph.action._TestActionGraphPy" @staticmethod def initialize_type(node_type: og.NodeType): """Initialize node attributes""" node_type.add_input("inputs:execIn", "execution", True) node_type.add_input("inputs:execUnused", "execution", True) node_type.add_input("inputs:boolUnused", "bool", True) node_type.add_output("outputs:execOut", "execution", True) node_type.add_output("outputs:finished_from_latent", "execution", True) node_type.add_output("outputs:finished_from_pushed", "execution", True) og.register_node_type(_TestActionGraphPy, 1) controller = og.Controller() keys = og.Controller.Keys (graph, (_, _, _, _, _), _, _,) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("Test", "omni.graph.action._TestActionGraphPy"), ("CounterOut", "omni.graph.action.Counter"), ("CounterLatent", "omni.graph.action.Counter"), ("CounterPushed", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("OnTick.outputs:tick", "Test.inputs:execIn"), ("Test.outputs:execOut", "CounterOut.inputs:execIn"), ("Test.outputs:finished_from_latent", "CounterLatent.inputs:execIn"), ("Test.outputs:finished_from_pushed", "CounterPushed.inputs:execIn"), ], keys.SET_VALUES: [ ("OnTick.inputs:onlyPlayback", False), ], }, ) await controller.evaluate(graph) self.assertEqual(og.Controller.get(f"{self.TEST_GRAPH_PATH}/CounterOut.outputs:count"), 0) g_exec_out = State.ENABLED await controller.evaluate(graph) self.assertEqual(og.Controller.get(f"{self.TEST_GRAPH_PATH}/CounterOut.outputs:count"), 1) g_exec_out = State.ENABLED_AND_PUSH await controller.evaluate(graph) self.assertEqual(og.Controller.get(f"{self.TEST_GRAPH_PATH}/CounterOut.outputs:count"), 2) self.assertEqual(og.Controller.get(f"{self.TEST_GRAPH_PATH}/CounterPushed.outputs:count"), 1) g_exec_out = State.START await controller.evaluate(graph) self.assertEqual(og.Controller.get(f"{self.TEST_GRAPH_PATH}/CounterOut.outputs:count"), 2) self.assertEqual(og.Controller.get(f"{self.TEST_GRAPH_PATH}/CounterPushed.outputs:count"), 1) self.assertEqual(og.Controller.get(f"{self.TEST_GRAPH_PATH}/CounterLatent.outputs:count"), 0) g_exec_out = State.END await controller.evaluate(graph) self.assertEqual(og.Controller.get(f"{self.TEST_GRAPH_PATH}/CounterOut.outputs:count"), 2) self.assertEqual(og.Controller.get(f"{self.TEST_GRAPH_PATH}/CounterPushed.outputs:count"), 1) self.assertEqual(og.Controller.get(f"{self.TEST_GRAPH_PATH}/CounterLatent.outputs:count"), 1) with ogts.ExpectedError(): with self.assertRaises(RuntimeError): i_ag.start_latent_state() # ------------------------------------------------------------------------ async def test_node_self_destruction(self): """Test that we don't crash if a very bad node causes a resync of the stage""" # Executing this node may cause errors and fail the test as the underlying # authoring node is destroyed while the task is executing. We just verify that only # the expected error is generated. class _TestBadNodePy: @staticmethod def compute(_: og.GraphContext, node: og.Node) -> bool: context = omni.usd.get_context() stage = context.get_stage() stage.RemovePrim(node.get_prim_path()) @staticmethod def get_node_type() -> str: """Get node type""" return "omni.graph.action._TestBadNodePy" @staticmethod def initialize_type(node_type: og.NodeType): """Initialize node attributes""" node_type.add_input("inputs:execIn", "execution", True) node_type.add_output("outputs:execOut", "execution", True) og.register_node_type(_TestBadNodePy, 1) controller = og.Controller() keys = og.Controller.Keys (graph, _, _, _,) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("Test", "omni.graph.action._TestBadNodePy"), ], keys.CONNECT: [ ("OnTick.outputs:tick", "Test.inputs:execIn"), ], keys.SET_VALUES: [ ("OnTick.inputs:onlyPlayback", False), ], }, ) with ogts.ExpectedError(): # Authoring node for Test was lost await controller.evaluate(graph)
42,238
Python
45.776301
120
0.487168
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/tests/test_action_graph_nodes_01.py
"""Action Graph Node Tests, Part 1""" import time import carb import omni.graph.core as og import omni.graph.core.tests as ogts import omni.kit.app import omni.kit.test import omni.usd from omni.graph.core import ThreadsafetyTestUtils from pxr import Gf, OmniGraphSchemaTools, Sdf, Vt # ====================================================================== class TestActionGraphNodes(ogts.OmniGraphTestCase): """Tests action graph node functionality""" TEST_GRAPH_PATH = "/World/TestGraph" keys = og.Controller.Keys E = og.ExecutionAttributeState.ENABLED D = og.ExecutionAttributeState.DISABLED async def setUp(self): """Set up test environment, to be torn down when done""" await super().setUp() # ----------------------------------------------------------------------- async def test_addprimrelationship_node(self): """Test AddPrimRelationship node""" # Check that we can add a relationship to a prim and get that relationship. # # +---------+ +---------------------+ # | ONTICK +-->| AddPrimRelationship + # +---------+ +---------------------+ # usd_context = omni.usd.get_context() stage = usd_context.get_stage() og.Controller.create_graph({"graph_path": self.TEST_GRAPH_PATH, "evaluator_name": "execution"}) og.Controller.edit( self.TEST_GRAPH_PATH, { self.keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("AddRel", "omni.graph.action.AddPrimRelationship"), ], self.keys.CREATE_PRIMS: [ ("/Test", {}), ("/Target", {}), ], self.keys.CONNECT: [("OnTick.outputs:tick", "AddRel.inputs:execIn")], self.keys.SET_VALUES: [ ("OnTick.inputs:onlyPlayback", False), ("AddRel.inputs:path", "/Test"), ("AddRel.inputs:name", "rel"), ("AddRel.inputs:target", "/Target"), ], }, ) prim = stage.GetPrimAtPath("/Test") await og.Controller.evaluate() rel = prim.GetRelationship("rel") targets = rel.GetTargets() self.assertEqual(len(targets), 1) self.assertTrue(str(targets[0]) == "/Target") # ---------------------------------------------------------------------- # The Branch node has a built-in test construct in its .ogn file located at ../../nodes/OgnBranch.ogn # (relative to the source location of the currently-opened testing script) AND is used in other testing # methods, so we skip adding extra node-specific tests for it here. # ---------------------------------------------------------------------- @ThreadsafetyTestUtils.make_threading_test def test_counter_node(self, test_instance_id: int = 0): """Test Counter node""" # Instance a test graph setup. Note that we append the graph path with the test_instance_id # so that the graph can be uniquely identified in the thread-safety test! graph_path = self.TEST_GRAPH_PATH + str(test_instance_id) og.Controller.create_graph({"graph_path": graph_path, "evaluator_name": "execution"}) (_, (ontick_node, counter_node), _, _,) = og.Controller.edit( graph_path, { self.keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("Counter", "omni.graph.action.Counter"), ], self.keys.SET_VALUES: [("OnTick.inputs:onlyPlayback", False)], self.keys.CONNECT: ("OnTick.outputs:tick", "Counter.inputs:execIn"), }, ) # Obtain necessary attributes. in_exec_attr = counter_node.get_attribute("inputs:execIn") in_reset_attr = counter_node.get_attribute("inputs:reset") state_cnt_attr = counter_node.get_attribute("state:count") out_exec_attr = counter_node.get_attribute("outputs:execOut") out_cnt_attr = counter_node.get_attribute("outputs:count") out_tick_attr = ontick_node.get_attribute("outputs:tick") # Check that the counter node gets correctly incremented when executing. self.assertEqual(state_cnt_attr.get(), 0) self.assertEqual(out_cnt_attr.get(), 0) self.assertEqual(out_exec_attr.get(), self.D) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(state_cnt_attr.get(), 1) self.assertEqual(out_cnt_attr.get(), 1) self.assertEqual(out_exec_attr.get(), self.E) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(state_cnt_attr.get(), 2) self.assertEqual(out_cnt_attr.get(), 2) self.assertEqual(out_exec_attr.get(), self.E) # Check that the counter node doesn't increment when not executing. og.Controller.disconnect( out_tick_attr, in_exec_attr, ) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(state_cnt_attr.get(), 2) self.assertEqual(out_cnt_attr.get(), 2) self.assertEqual(out_exec_attr.get(), self.E) # Check that the reset flag for the Counter node instance works correctly when # inputs:execIn is set to 0 (i.e. when the Counter node is NOT supposed to be # executing). og.Controller.connect(out_tick_attr, in_reset_attr) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(state_cnt_attr.get(), 0) self.assertEqual(out_cnt_attr.get(), 0) self.assertEqual(out_exec_attr.get(), self.E) # ---------------------------------------------------------------------- @ThreadsafetyTestUtils.make_threading_test def test_delay_node(self, test_instance_id: int = 0): """Test Delay node""" # Instance a test graph setup. Note that we append the graph path with the test_instance_id # so that the graph can be uniquely identified in the thread-safety test! graph_path = self.TEST_GRAPH_PATH + str(test_instance_id) og.Controller.create_graph({"graph_path": graph_path, "evaluator_name": "execution"}) (_, (on_impulse_node, _, counter_node), _, _,) = og.Controller.edit( graph_path, { self.keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("Delay", "omni.graph.action.Delay"), ("Counter", "omni.graph.action.Counter"), ], self.keys.CONNECT: [ ("OnImpulse.outputs:execOut", "Delay.inputs:execIn"), ("Delay.outputs:finished", "Counter.inputs:execIn"), ], self.keys.SET_VALUES: [ ("OnImpulse.inputs:onlyPlayback", False), ("Delay.inputs:duration", 0.01), ], }, ) # Obtain necessary attributes. out_cnt_attr = counter_node.get_attribute("outputs:count") state_enable_impulse_attr = on_impulse_node.get_attribute("state:enableImpulse") # Trigger the graph(s) once. state_enable_impulse_attr.set(True) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. # Downstream execution is delayed, so the counter node won't be incremented. self.assertEqual(out_cnt_attr.get(), 0) # Wait to ensure that the delay finishes before checking that the counter node # has indeed been incremented. time.sleep(0.02) yield ThreadsafetyTestUtils.EVALUATION_WAIT_FRAME # Yielding to compute by waiting for the next app frame. self.assertEqual(out_cnt_attr.get(), 1) # ---------------------------------------------------------------------- @ThreadsafetyTestUtils.make_threading_test def test_flipflop_node(self, test_instance_id: int = 0): """Test FlipFlop node""" # Instance a test graph setup. Note that we append the graph path with the test_instance_id # so that the graph can be uniquely identified in the thread-safety test! graph_path = self.TEST_GRAPH_PATH + str(test_instance_id) og.Controller.create_graph({"graph_path": graph_path, "evaluator_name": "execution"}) (graph, (_, flip_flop_node), _, _,) = og.Controller.edit( graph_path, { self.keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("FlipFlop", "omni.graph.action.FlipFlop"), ], self.keys.SET_VALUES: [("OnTick.inputs:onlyPlayback", False)], self.keys.CONNECT: ( "OnTick.outputs:tick", "FlipFlop.inputs:execIn", ), }, ) # Obtain necessary attributes. out_a_attr = flip_flop_node.get_attribute("outputs:a") out_b_attr = flip_flop_node.get_attribute("outputs:b") out_isa_attr = flip_flop_node.get_attribute("outputs:isA") # First eval, 'a'. yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_a_attr.get(), self.E) self.assertEqual(out_b_attr.get(), self.D) self.assertTrue(out_isa_attr.get()) # Second eval 'b'. yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_a_attr.get(), self.D) self.assertEqual(out_b_attr.get(), self.E) self.assertFalse(out_isa_attr.get()) # Third eval 'a'. yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_a_attr.get(), self.E) self.assertEqual(out_b_attr.get(), self.D) self.assertTrue(out_isa_attr.get()) # Test that non-usd-backed FlipFlop nodes correctly # set their execution-type attributes. # Make sure that the node paths are prefaced with the path to the # graph that they should reside in (so that each node creation command # can be processed to produce unique nodes for each test instance)! _, on_tick_no_usd = og.cmds.CreateNode( graph=graph, node_path=f"{graph_path}/OnTickNoUSD", node_type="omni.graph.action.OnTick", create_usd=False, ) _, flip_flop_no_usd = og.cmds.CreateNode( graph=graph, node_path=f"{graph_path}/FlipFlopNoUSD", node_type="omni.graph.action.FlipFlop", create_usd=False, ) # Obtain necessary attributes. out_a_attr = flip_flop_no_usd.get_attribute("outputs:a") out_b_attr = flip_flop_no_usd.get_attribute("outputs:b") out_isa_attr = flip_flop_no_usd.get_attribute("outputs:isA") on_tick_no_usd.get_attribute("inputs:onlyPlayback").set(False) # Make sure that the node attribute paths are prefaced with the graph # path that they reside in (so that each instanced node attribute can # be uniquely processed)! og.cmds.ConnectAttrs( src_attr=f"{graph_path}/OnTickNoUSD.outputs:tick", dest_attr=f"{graph_path}/FlipFlopNoUSD.inputs:execIn", modify_usd=False, ) # First eval, 'a'. yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_a_attr.get(), self.E) self.assertEqual(out_b_attr.get(), self.D) self.assertTrue(out_isa_attr.get()) # Second eval 'b'. yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_a_attr.get(), self.D) self.assertEqual(out_b_attr.get(), self.E) self.assertFalse(out_isa_attr.get()) # Third eval 'a'. yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_a_attr.get(), self.E) self.assertEqual(out_b_attr.get(), self.D) self.assertTrue(out_isa_attr.get()) # ---------------------------------------------------------------------- # The ForEach node has a built-in test construct in its .ogn file located at ../../nodes/OgnForEach.ogn # (relative to the source location of the currently-opened testing script), so we skip adding extra # node-specific tests for it here. # ---------------------------------------------------------------------- @ThreadsafetyTestUtils.make_threading_test def test_forloop_node(self, test_instance_id: int = 0): """Test ForLoop node""" context = omni.usd.get_context() stage = context.get_stage() # Since we want to use the same prim across all graph instances in the # thread-safety test, we add it to the threading cache like so: prim = ThreadsafetyTestUtils.add_to_threading_cache(test_instance_id, stage.DefinePrim("/World/TestPrim")) ThreadsafetyTestUtils.add_to_threading_cache( test_instance_id, prim.CreateAttribute("val1", Sdf.ValueTypeNames.Int2, False).Set(Gf.Vec2i(1, 1)), ) # Instance a test graph setup. Note that we append the graph path with the test_instance_id # so that the graph can be uniquely identified in the thread-safety test! graph_path = self.TEST_GRAPH_PATH + str(test_instance_id) og.Controller.create_graph({"graph_path": graph_path, "evaluator_name": "execution"}) (_, (_, _, _, _, _, _, write_node, finish_counter), _, _) = og.Controller.edit( graph_path, { self.keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("Const", "omni.graph.nodes.ConstantInt2"), ("StopNum", "omni.graph.nodes.ConstantInt"), ("Add", "omni.graph.nodes.Add"), ("Branch", "omni.graph.action.Branch"), ("For", "omni.graph.action.ForLoop"), ("Write1", "omni.graph.nodes.WritePrimAttribute"), ("FinishCounter", "omni.graph.action.Counter"), ], self.keys.SET_VALUES: [ ("OnTick.inputs:onlyPlayback", False), ("Const.inputs:value", [1, 2]), ("StopNum.inputs:value", 3), ("Write1.inputs:name", "val1"), ("Write1.inputs:primPath", "/World/TestPrim"), ("Write1.inputs:usePath", True), ("Branch.inputs:condition", True), ], self.keys.CONNECT: [ ("OnTick.outputs:tick", "For.inputs:execIn"), ("StopNum.inputs:value", "For.inputs:stop"), ("For.outputs:loopBody", "Branch.inputs:execIn"), ("For.outputs:finished", "FinishCounter.inputs:execIn"), ("Branch.outputs:execTrue", "Write1.inputs:execIn"), ("For.outputs:value", "Add.inputs:a"), ("Const.inputs:value", "Add.inputs:b"), ("Add.outputs:sum", "Write1.inputs:value"), ], }, ) # Evaluate the graph(s). yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertListEqual([3, 4], list(stage.GetAttributeAtPath("/World/TestPrim.val1").Get())) self.assertEqual(3, write_node.get_compute_count()) self.assertEqual(1, finish_counter.get_compute_count()) # This tests make sure that a for loop writing arrays to diferent prims # work as expected (OM-84129) async def test_foreach_node_write_multiple_prim(self, test_instance_id: int = 0): """Test the foreach node writing arrays to output prims""" og.Controller.create_graph({"graph_path": self.TEST_GRAPH_PATH, "evaluator_name": "execution"}) (_, _, (prim1, prim2), _) = og.Controller.edit( self.TEST_GRAPH_PATH, { self.keys.CREATE_PRIMS: [ ("/World/Prim1", {"graph_output": ("Int[]", [])}), ("/World/Prim2", {"graph_output": ("Int[]", [])}), ], self.keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("For", "omni.graph.action.ForEach"), ("Write", "omni.graph.nodes.WritePrimAttribute"), ("MakeArray", "omni.graph.nodes.ConstructArray"), ], self.keys.SET_VALUES: [ ("For.inputs:arrayIn", {"type": "token[]", "value": ["/World/Prim1", "/World/Prim2"]}), ("OnTick.inputs:onlyPlayback", False), ("Write.inputs:name", "graph_output"), ("Write.inputs:usePath", True), ("Write.inputs:usdWriteBack", True), ("MakeArray.inputs:arraySize", 1), ], self.keys.CONNECT: [ ("OnTick.outputs:tick", "For.inputs:execIn"), ("For.outputs:loopBody", "Write.inputs:execIn"), ("For.outputs:element", "Write.inputs:primPath"), ("For.outputs:arrayIndex", "MakeArray.inputs:input0"), ("MakeArray.outputs:array", "Write.inputs:value"), ], }, ) await omni.kit.app.get_app().next_update_async() prim1_out = prim1.GetAttribute("graph_output") prim2_out = prim2.GetAttribute("graph_output") self.assertEqual(prim1_out.Get(), Vt.IntArray([0])) self.assertEqual(prim2_out.Get(), Vt.IntArray([1])) # ---------------------------------------------------------------------- # The Gate node has a built-in test construct in its .ogn file located at ../../nodes/OgnGate.ogn # (relative to the source location of the currently-opened testing script) AND is used in other # testing methods, so we skip adding extra node-specific tests for it here. # ---------------------------------------------------------------------- @ThreadsafetyTestUtils.make_threading_test def test_multigate_node(self, test_instance_id: int = 0): """Test Multigate node""" # Instance a test graph setup. Note that we append the graph path with the test_instance_id # so that the graph can be uniquely identified in the thread-safety test! graph_path = self.TEST_GRAPH_PATH + str(test_instance_id) (_, (_, multigate_node), _, _) = og.Controller.edit( {"graph_path": graph_path, "evaluator_name": "execution"}, { self.keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("Multigate", "omni.graph.action.Multigate"), ], self.keys.SET_VALUES: [("OnTick.inputs:onlyPlayback", False)], self.keys.CONNECT: [ ("OnTick.outputs:tick", "Multigate.inputs:execIn"), ], }, ) # Add 5 extra outputs to the Multigate node. for i in range(1, 6): og.Controller.create_attribute( multigate_node, f"outputs:output{i}", og.Type(og.BaseDataType.UINT, 1, 0, og.AttributeRole.EXECUTION), og.AttributePortType.ATTRIBUTE_PORT_TYPE_OUTPUT, ) # Obtain necessary attributes. out_attr_0 = multigate_node.get_attribute("outputs:output0") out_attr_1 = multigate_node.get_attribute("outputs:output1") out_attr_2 = multigate_node.get_attribute("outputs:output2") out_attr_3 = multigate_node.get_attribute("outputs:output3") out_attr_4 = multigate_node.get_attribute("outputs:output4") out_attr_5 = multigate_node.get_attribute("outputs:output5") # Check that the Multigate node correctly cycles through each of its outputs. # Note that we trigger an execution through the Multigate node via the OnTick node, # whose onlyPlayback input we've set to False in order to trigger an execution each # time we evaluate the graph(s). for i in range(0, 6): yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(multigate_node.get_attribute(f"outputs:output{(i + 0) % 6}").get(), self.E) self.assertEqual(multigate_node.get_attribute(f"outputs:output{(i + 1) % 6}").get(), self.D) self.assertEqual(multigate_node.get_attribute(f"outputs:output{(i + 2) % 6}").get(), self.D) self.assertEqual(multigate_node.get_attribute(f"outputs:output{(i + 3) % 6}").get(), self.D) self.assertEqual(multigate_node.get_attribute(f"outputs:output{(i + 4) % 6}").get(), self.D) self.assertEqual(multigate_node.get_attribute(f"outputs:output{(i + 5) % 6}").get(), self.D) # Next try removing some output attributes during evaluation and test if the # Multigate node correctly cycles through. for _ in range(4): yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_attr_0.get(), self.D) self.assertEqual(out_attr_1.get(), self.D) self.assertEqual(out_attr_2.get(), self.D) self.assertEqual(out_attr_3.get(), self.E) self.assertEqual(out_attr_4.get(), self.D) self.assertEqual(out_attr_5.get(), self.D) multigate_node.remove_attribute("outputs:output4") # The Multigate node cycles back to 0 instead of going to 5 since it thinks that it's # reached the end of its outputs list (i.e. it expects to jump from pin 3 to 4, but b/c # there is no such pin it goes back to 0 rather than 5). yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_attr_0.get(), self.E) self.assertEqual(out_attr_1.get(), self.D) self.assertEqual(out_attr_2.get(), self.D) self.assertEqual(out_attr_3.get(), self.D) self.assertEqual(out_attr_5.get(), self.D) # Further showing that executing 4 times brings us back to pin 0 rather than pin 5. for _ in range(4): yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_attr_0.get(), self.E) self.assertEqual(out_attr_1.get(), self.D) self.assertEqual(out_attr_2.get(), self.D) self.assertEqual(out_attr_3.get(), self.D) self.assertEqual(out_attr_5.get(), self.D) # Execute the graph(s) once, then remove the currently-enabled output pin. The Multigate node will think # that it's reached the end of the outputs list, and cycle back. Because we removed pin 1, it'll go # back to pin 0 and never cycle through the other outputs since it cannot make the jump from # pin 0 to 2 (as mentioned previously). yield # Yielding to wait for compute to happen across all graph instances before continuing the test. multigate_node.remove_attribute("outputs:output1") for _ in range(3): yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_attr_0.get(), self.E) self.assertEqual(out_attr_2.get(), self.D) self.assertEqual(out_attr_3.get(), self.D) self.assertEqual(out_attr_5.get(), self.D) # ---------------------------------------------------------------------- @ThreadsafetyTestUtils.make_threading_test def test_multisequence_node(self, test_instance_id: int = 0): """Test Multisequence node""" # Instance a test graph setup. Note that we append the graph path with the test_instance_id # so that the graph can be uniquely identified in the thread-safety test! graph_path = self.TEST_GRAPH_PATH + str(test_instance_id) og.Controller.create_graph({"graph_path": graph_path, "evaluator_name": "execution"}) ( _, (on_tick_node, multisequence_node, counter0_node, counter1_node, counter2_node), _, _, ) = og.Controller.edit( graph_path, { self.keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("Multisequence", "omni.graph.action.Multisequence"), ("Counter0", "omni.graph.action.Counter"), ("Counter1", "omni.graph.action.Counter"), ("Counter2", "omni.graph.action.Counter"), ], self.keys.SET_VALUES: [("OnTick.inputs:onlyPlayback", False)], self.keys.CONNECT: [ ("OnTick.outputs:tick", "Multisequence.inputs:execIn"), ], }, ) # Add 3 extra outputs to the Multisequence node. for j in range(1, 4): og.Controller.create_attribute( multisequence_node, f"outputs:output{j}", og.Type(og.BaseDataType.UINT, 1, 0, og.AttributeRole.EXECUTION), og.AttributePortType.ATTRIBUTE_PORT_TYPE_OUTPUT, ) # Obtain necessary attributes. out_attr_0 = multisequence_node.get_attribute("outputs:output0") out_attr_1 = multisequence_node.get_attribute("outputs:output1") out_attr_2 = multisequence_node.get_attribute("outputs:output2") out_attr_3 = multisequence_node.get_attribute("outputs:output3") in_exec_attr_0 = counter0_node.get_attribute("inputs:execIn") in_exec_attr_1 = counter1_node.get_attribute("inputs:execIn") in_exec_attr_2 = counter2_node.get_attribute("inputs:execIn") out_cnt_attr_0 = counter0_node.get_attribute("outputs:count") out_cnt_attr_1 = counter1_node.get_attribute("outputs:count") out_cnt_attr_2 = counter2_node.get_attribute("outputs:count") in_onlyplayback_attr = on_tick_node.get_attribute("inputs:onlyPlayback") # Connect Multisequence node output attributes to the counter nodes. og.Controller.connect(out_attr_0, in_exec_attr_0) og.Controller.connect(out_attr_2, in_exec_attr_1) og.Controller.connect(out_attr_1, in_exec_attr_2) # Check that the Multisequence node correctly executes through its outputs when input # execution is enabled via the OnTick node. This is done by checking whether # each counter has been incremented by 1, and if the last output pin on the # Multisequence node remains enabled (regardless of the fact that it's not connected # downstream). yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_cnt_attr_0.get(), 1) self.assertEqual(out_cnt_attr_1.get(), 1) self.assertEqual(out_cnt_attr_2.get(), 1) self.assertEqual(out_attr_0.get(), self.D) self.assertEqual(out_attr_1.get(), self.D) self.assertEqual(out_attr_2.get(), self.D) self.assertEqual(out_attr_3.get(), self.E) # Connect the Counter2 node to another Multisequence output pin. og.Controller.connect(out_attr_3, in_exec_attr_2) # Once again evaluate the graph(s). In this situation the Counter2 node should be incremented twice # (since it's connected to 2 separate Multisequence output pins). Also Multisequence output pins # 1 AND 3 should both be enabled by the end of the execution; this is because pin 3 would # typically be the last output that gets enabled, but because pin 3 shares a downstream # node with pin 1 (that node being Counter2), both outputs need to be enabled by the end. yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_cnt_attr_0.get(), 2) self.assertEqual(out_cnt_attr_1.get(), 2) self.assertEqual(out_cnt_attr_2.get(), 3) self.assertEqual(out_attr_0.get(), self.D) self.assertEqual(out_attr_1.get(), self.E) self.assertEqual(out_attr_2.get(), self.D) self.assertEqual(out_attr_3.get(), self.E) # Set the OnTick node to only trigger downstream execution when playback is enabled; check # that in this situation the Multisequence node correctly skips executing through its outputs # (i.e. that the Counter nodes don't get incremented). The state of the Multisequence's output # pins should not have changed since the last graph evaluation. in_onlyplayback_attr.set(True) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_cnt_attr_0.get(), 2) self.assertEqual(out_cnt_attr_1.get(), 2) self.assertEqual(out_cnt_attr_2.get(), 3) self.assertEqual(out_attr_0.get(), self.D) self.assertEqual(out_attr_1.get(), self.E) self.assertEqual(out_attr_2.get(), self.D) self.assertEqual(out_attr_3.get(), self.E) # Try removing an output attribute from the Multisequence node, check that all other # outputs that come before in the list get triggered. In this example we remove outputs2, # which means that outputs3 won't get triggered at all (as evidenced by the fact that the # Counter2 node only gets incremented once by output1). og.Controller.disconnect(out_attr_2, in_exec_attr_1) multisequence_node.remove_attribute("outputs:output2") in_onlyplayback_attr.set(False) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_cnt_attr_0.get(), 3) self.assertEqual(out_cnt_attr_1.get(), 2) self.assertEqual(out_cnt_attr_2.get(), 4) self.assertEqual(out_attr_0.get(), self.D) self.assertEqual(out_attr_1.get(), self.E) self.assertEqual(out_attr_3.get(), self.D) # ---------------------------------------------------------------------- @ThreadsafetyTestUtils.make_threading_test def test_once_node(self, test_instance_id: int = 0): """Test Once node""" # Instance a test graph setup. Note that we append the graph path with the test_instance_id # so that the graph can be uniquely identified in the thread-safety test! graph_path = self.TEST_GRAPH_PATH + str(test_instance_id) og.Controller.create_graph({"graph_path": graph_path, "evaluator_name": "execution"}) (_, (ontick_node, once_node), _, _,) = og.Controller.edit( graph_path, { self.keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("Once", "omni.graph.action.Once"), ], self.keys.SET_VALUES: [("OnTick.inputs:onlyPlayback", False)], self.keys.CONNECT: ("OnTick.outputs:tick", "Once.inputs:execIn"), }, ) # Obtain necessary attributes. in_exec_attr = once_node.get_attribute("inputs:execIn") in_reset_attr = once_node.get_attribute("inputs:reset") out_once_attr = once_node.get_attribute("outputs:once") out_after_attr = once_node.get_attribute("outputs:after") out_tick_attr = ontick_node.get_attribute("outputs:tick") # Check that the Once node controls flow of execution by passing flow # differently the first time it's executed compared to all subsequent # executions. self.assertEqual(out_once_attr.get(), self.D) self.assertEqual(out_after_attr.get(), self.D) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_once_attr.get(), self.E) self.assertEqual(out_after_attr.get(), self.D) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_once_attr.get(), self.D) self.assertEqual(out_after_attr.get(), self.E) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_once_attr.get(), self.D) self.assertEqual(out_after_attr.get(), self.E) # Check that the reset flag works correctly when inputs:execIn is set to 0 (i.e. when # the Once node is NOT supposed to be executing). og.Controller.disconnect(out_tick_attr, in_exec_attr) og.Controller.connect(out_tick_attr, in_reset_attr) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_once_attr.get(), self.D) self.assertEqual(out_after_attr.get(), self.D) og.Controller.disconnect(out_tick_attr, in_reset_attr) og.Controller.connect(out_tick_attr, in_exec_attr) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_once_attr.get(), self.E) self.assertEqual(out_after_attr.get(), self.D) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. self.assertEqual(out_once_attr.get(), self.D) self.assertEqual(out_after_attr.get(), self.E) # Check that when both the execIn and reset input attributes get triggered, the latter # overrides the former and execution flow does not pass through outputs:after. # FIXME: Something about the 2nd connection here is messing up the data model such # that inputs:reset is being read as 0 inside the node og.Controller.connect(out_tick_attr, in_reset_attr) yield # Yielding to wait for compute to happen across all graph instances before continuing the test. # self.assertEqual(out_once_attr.get(), self.D) # self.assertEqual(out_after_attr.get(), self.D) # ---------------------------------------------------------------------- # NOTE: Even though the OnClosing node is threadsafe (its compute method is very simple), # we don't adapt the below test to check for thread-safety conditions because it relies # on other nodes (omni.graph.action.SendCustomEvent and omni.graph.nodes.GraphTarget) # which are NOT threadsafe. async def test_onclosing_node(self): """Test OnClosing node""" og.Controller.edit({"graph_path": self.TEST_GRAPH_PATH, "evaluator_name": "execution"}) # Testing OnClosing is tricky because OG is being destroyed when it happens - # so test by sending a custom event when the network is triggered # and then checking if we got that event. def registered_event_name(event_name): """Returns the internal name used for the given custom event name""" name = "omni.graph.action." + event_name return carb.events.type_from_string(name) got_event = [0] def on_event(_): got_event[0] = got_event[0] + 1 reg_event_name = registered_event_name("foo") message_bus = omni.kit.app.get_app().get_message_bus_event_stream() sub = message_bus.create_subscription_to_push_by_type(reg_event_name, on_event) self.assertIsNotNone(sub) async def set_up_graph(): controller = og.Controller() keys = og.Controller.Keys controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnClosing", "omni.graph.action.OnClosing"), ("Send", "omni.graph.action.SendCustomEvent"), ("GraphTarget", "omni.graph.nodes.GraphTarget"), ], keys.CONNECT: [ ("OnClosing.outputs:execOut", "Send.inputs:execIn"), ("GraphTarget.outputs:primPath", "Send.inputs:path"), ], keys.SET_VALUES: [("Send.inputs:eventName", "foo")], }, ) await set_up_graph() # Evaluate once so that the standalone graph is in steady state. await omni.kit.app.get_app().next_update_async() self.assertEqual(got_event[0], 0) # Close the stage. usd_context = omni.usd.get_context() (result, _) = await usd_context.close_stage_async() self.assertTrue(result) # Check our handler was called. self.assertEqual(got_event[0], 1) # Reset the counter. got_event[0] = 0 # Now check that the same works with instanced graphs. await usd_context.new_stage_async() await set_up_graph() og.cmds.SetEvaluationMode( graph=og.get_graph_by_path(self.TEST_GRAPH_PATH), new_evaluation_mode=og.GraphEvaluationMode.GRAPH_EVALUATION_MODE_INSTANCED, ) stage = usd_context.get_stage() prims = [stage.DefinePrim(f"/prim_{i}") for i in range(0, 100)] for prim in prims: OmniGraphSchemaTools.applyOmniGraphAPI(stage, prim.GetPath(), self.TEST_GRAPH_PATH) # Wait an update for the graphs to get set up. await omni.kit.app.get_app().next_update_async() # Close the stage. (result, _) = await usd_context.close_stage_async() self.assertTrue(result) # Check that our handler was called. self.assertEqual(got_event[0], len(prims)) # ---------------------------------------------------------------------- async def test_oncustomevent_and_sendcustomevent_nodes(self): """Test OnCustomEvent and SendCustomEvent nodes""" controller = og.Controller() controller.create_graph({"graph_path": self.TEST_GRAPH_PATH, "evaluator_name": "execution"}) (_, (_, _, event1_node, counter1_node, event2_node, counter2_node), _, _,) = controller.edit( self.TEST_GRAPH_PATH, { self.keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("Send", "omni.graph.action.SendCustomEvent"), ("OnCustomEvent1", "omni.graph.action.OnCustomEvent"), ("Counter1", "omni.graph.action.Counter"), ("OnCustomEvent2", "omni.graph.action.OnCustomEvent"), ("Counter2", "omni.graph.action.Counter"), ], self.keys.CONNECT: [ ("OnImpulse.outputs:execOut", "Send.inputs:execIn"), ("OnCustomEvent1.outputs:execOut", "Counter1.inputs:execIn"), ("OnCustomEvent2.outputs:execOut", "Counter2.inputs:execIn"), ], self.keys.SET_VALUES: [ ("OnCustomEvent1.inputs:onlyPlayback", False), ("OnCustomEvent2.inputs:onlyPlayback", False), ("OnImpulse.inputs:onlyPlayback", False), ("Send.inputs:eventName", "foo"), ("Send.inputs:path", "Test Path"), ("OnCustomEvent1.inputs:eventName", "foo"), ("OnCustomEvent2.inputs:eventName", "foo"), ], }, ) counter1_controller = og.Controller(og.Controller.attribute("outputs:count", counter1_node)) counter2_controller = og.Controller(og.Controller.attribute("outputs:count", counter2_node)) event1_controller = og.Controller(og.Controller.attribute("outputs:path", event1_node)) event2_controller = og.Controller(og.Controller.attribute("outputs:path", event2_node)) await omni.kit.app.get_app().next_update_async() self.assertEqual(counter1_controller.get(), 0) # Trigger graph once, this will queue up the event for the next evaluation. controller.edit(self.TEST_GRAPH_PATH, {self.keys.SET_VALUES: ("OnImpulse.state:enableImpulse", True)}) # Note that if this is a push subscription, the receivers will run this frame instead of next. await omni.kit.app.get_app().next_update_async() self.assertEqual(counter1_controller.get(), 0) # This evaluation should trigger the receivers. await omni.kit.app.get_app().next_update_async() # Verify that events were received. self.assertEqual(counter1_controller.get(), 1) self.assertEqual(event1_controller.get(), "Test Path") self.assertEqual(counter2_controller.get(), 1) self.assertEqual(event2_controller.get(), "Test Path") # Verify the contents of the associated bundle. # FIXME: Authored bundle is always empty? # bundle_contents = og.BundleContents(graph.get_default_graph_context(), event1_node, "outputs:bundle", True) # self.assertEqual(1, bundle_contents.size) # Modify the event name one receiver and sender and ensure it still works. controller.edit( self.TEST_GRAPH_PATH, { self.keys.SET_VALUES: [("Send.inputs:eventName", "bar"), ("OnImpulse.state:enableImpulse", True)], }, ) await omni.kit.app.get_app().next_update_async() await omni.kit.app.get_app().next_update_async() # We changed the sender event name, so counter should NOT have triggered again. self.assertEqual(counter1_controller.get(), 1) # Change the receiver name to match. controller.edit(self.TEST_GRAPH_PATH, {self.keys.SET_VALUES: ("OnCustomEvent1.inputs:eventName", "bar")}) await omni.kit.app.get_app().next_update_async() # Trigger send again and verify we get it (1 frame lag for pop). controller.edit(self.TEST_GRAPH_PATH, {self.keys.SET_VALUES: ("OnImpulse.state:enableImpulse", True)}) await omni.kit.app.get_app().next_update_async() await omni.kit.app.get_app().next_update_async() self.assertEqual(counter1_controller.get(), 2)
42,965
Python
50.089179
117
0.59218
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/tests/test_action_graph_evaluation_01.py
"""Action Graph Evaluation Tests, Part 1""" import asyncio import json import omni.graph.core as og import omni.graph.core.tests as ogts import omni.kit.test import omni.usd # ====================================================================== class TestActionGraphEvaluation(ogts.OmniGraphTestCase): """Tests action graph evaluator functionality""" TEST_GRAPH_PATH = "/World/TestGraph" async def setUp(self): """Set up test environment, to be torn down when done""" await super().setUp() og.Controller.edit({"graph_path": self.TEST_GRAPH_PATH, "evaluator_name": "execution"}) # ---------------------------------------------------------------------- async def test_active_latent(self): """Exercise a latent node that executes downstream nodes while latent""" # +--------+ +----------+finished+-------------+ # | OnTick+-->| Countdown+-------->FinishCounter| # +--------+ | | +-------------+ # | +-+ # +----------+ | +------------+ +------------+ +------------+ # +-----> TickCounter+----->TickCounter2+---->TickCounter3| # tick +------------+ +------------+ +------------+ controller = og.Controller() keys = og.Controller.Keys (graph, nodes, _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("Countdown", "omni.graph.action.Countdown"), ("FinishCounter", "omni.graph.action.Counter"), ("TickCounter", "omni.graph.action.Counter"), ("TickCounter2", "omni.graph.action.Counter"), ("TickCounter3", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("OnTick.outputs:tick", "Countdown.inputs:execIn"), ("Countdown.outputs:finished", "FinishCounter.inputs:execIn"), ("Countdown.outputs:tick", "TickCounter.inputs:execIn"), ("TickCounter.outputs:execOut", "TickCounter2.inputs:execIn"), ("TickCounter2.outputs:execOut", "TickCounter3.inputs:execIn"), ], keys.SET_VALUES: [("Countdown.inputs:duration", 3), ("OnTick.inputs:onlyPlayback", False)], }, ) (_, _, finish_counter, tick_counter, _, tick_counter_3) = nodes finish_counter_controller = og.Controller(og.Controller.attribute("outputs:count", finish_counter)) tick_counter_controller = og.Controller(og.Controller.attribute("outputs:count", tick_counter)) tick_counter_3_controller = og.Controller(og.Controller.attribute("outputs:count", tick_counter_3)) await controller.evaluate(graph) self.assertEqual(finish_counter_controller.get(), 0) await controller.evaluate(graph) self.assertEqual(tick_counter_controller.get(), 1) await controller.evaluate(graph) self.assertEqual(finish_counter_controller.get(), 0) self.assertEqual(tick_counter_controller.get(), 2) await controller.evaluate(graph) self.assertEqual(finish_counter_controller.get(), 0) self.assertEqual(tick_counter_3_controller.get(), 3) await controller.evaluate(graph) self.assertEqual(finish_counter_controller.get(), 1) self.assertEqual(tick_counter_3_controller.get(), 3) await controller.evaluate(graph) self.assertEqual(finish_counter_controller.get(), 1) self.assertEqual(tick_counter_3_controller.get(), 3) # ---------------------------------------------------------------------- async def test_async_nodes(self): """Test asynchronous action nodes""" # Check that a nested loop state is maintained when executing a latent delay. # # +---------+ +----------+ +----------+ +-------+ +--------+ # | IMPULSE +-->| FOR-LOOP +--->| FOR-LOOP +--->| DELAY +--->| COUNTER| # +---------+ +----------+ +----------+ +-------+ +--------+ # controller = og.Controller() keys = og.Controller.Keys (graph, (_, _, _, _, counter_node, _, _), _, _,) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("ForLoop1", "omni.graph.action.ForLoop"), ("ForLoop2", "omni.graph.action.ForLoop"), ("Delay", "omni.graph.action.Delay"), ("Counter", "omni.graph.action.Counter"), ("OnTick", "omni.graph.action.OnTick"), ("Counter2", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("OnImpulse.outputs:execOut", "ForLoop1.inputs:execIn"), ("ForLoop1.outputs:loopBody", "ForLoop2.inputs:execIn"), ("ForLoop2.outputs:loopBody", "Delay.inputs:execIn"), ("Delay.outputs:finished", "Counter.inputs:execIn"), ("OnTick.outputs:tick", "Counter2.inputs:execIn"), ], keys.SET_VALUES: [ ("OnTick.inputs:onlyPlayback", False), ("OnImpulse.inputs:onlyPlayback", False), ("Delay.inputs:duration", 0.1), ("ForLoop1.inputs:stop", 2), ("ForLoop2.inputs:stop", 5), ], }, ) await controller.evaluate(graph) # Trigger graph once. controller.edit(self.TEST_GRAPH_PATH, {keys.SET_VALUES: ("OnImpulse.state:enableImpulse", True)}) await controller.evaluate(graph) # In delay now, no count. counter_controller = og.Controller(og.Controller.attribute("outputs:count", counter_node)) self.assertEqual(counter_controller.get(), 0) # Wait to ensure the first 5 delays compute. for _ in range(5): await asyncio.sleep(0.2) await controller.evaluate(graph) count_val = counter_controller.get() self.assertGreater(count_val, 4) # Wait and verify the remainder go through. for _ in range(5): await asyncio.sleep(0.1) await controller.evaluate(graph) self.assertEqual(counter_controller.get(), 10) # ---------------------------------------------------------------------- async def test_chained_stateful_nodes(self): """Test that chaining loop nodes works""" controller = og.Controller() keys = og.Controller.Keys (graph, (_, _, _, counter_node), _, _,) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("ForLoop1", "omni.graph.action.ForLoop"), ("ForLoop2", "omni.graph.action.ForLoop"), ("Counter", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("OnTick.outputs:tick", "ForLoop1.inputs:execIn"), ("ForLoop1.outputs:loopBody", "ForLoop2.inputs:execIn"), ("ForLoop2.outputs:loopBody", "Counter.inputs:execIn"), ], keys.SET_VALUES: [ ("OnTick.inputs:onlyPlayback", False), ("ForLoop1.inputs:stop", 5), ("ForLoop2.inputs:stop", 5), ], }, ) await controller.evaluate(graph) self.assertEqual(og.Controller.get(controller.attribute("outputs:count", counter_node)), 5 * 5) # ---------------------------------------------------------------------- async def test_cycle_break(self): """Test that an illegal cycle issues a warning""" controller = og.Controller() keys = og.Controller.Keys (graph, (on_impulse, count_a, count_b), _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("A", "omni.graph.action.Counter"), ("B", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("OnImpulse.outputs:execOut", "A.inputs:execIn"), ("A.outputs:execOut", "B.inputs:execIn"), ("B.outputs:execOut", "A.inputs:execIn"), ], keys.SET_VALUES: [ ("OnImpulse.state:enableImpulse", True), ("OnImpulse.inputs:onlyPlayback", False), ], }, ) with ogts.ExpectedError(): await controller.evaluate(graph) og.Controller.set(controller.attribute("state:enableImpulse", on_impulse), True) with ogts.ExpectedError(): await controller.evaluate(graph) self.assertEqual(og.Controller.get(controller.attribute("outputs:count", count_a)), 2) self.assertEqual(og.Controller.get(controller.attribute("outputs:count", count_b)), 2) # ---------------------------------------------------------------------- async def test_dep_sort_fan_out(self): """Test that dependency sort works when there is data fan-out""" # +-------------+ # +-------->| | # | | SwitchTokenA| # | +--->+-------------+ # +----------+ | # |OnImpulse +----|------+ +--------------+ # +----------+ | +---------->| SwitchTokenB | # | +^-------------+ # +------+-+ +--------+ | # | ConstA +--->AppendB +---+ # +--------+ +--------+ controller = og.Controller() keys = og.Controller.Keys (graph, (_, _, _, _, _), _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("ConstA", "omni.graph.nodes.ConstantToken"), ("AppendB", "omni.graph.nodes.AppendString"), ("SwitchTokenA", "omni.graph.action.SwitchToken"), ("SwitchTokenB", "omni.graph.action.SwitchToken"), ], keys.CONNECT: [ ("OnImpulse.outputs:execOut", "SwitchTokenA.inputs:execIn"), ("OnImpulse.outputs:execOut", "SwitchTokenB.inputs:execIn"), ("ConstA.inputs:value", "SwitchTokenA.inputs:value"), ("ConstA.inputs:value", "AppendB.inputs:value"), ("AppendB.outputs:value", "SwitchTokenB.inputs:value"), ], keys.SET_VALUES: [ ("OnImpulse.inputs:onlyPlayback", False), ("OnImpulse.state:enableImpulse", True), ("AppendB.inputs:suffix", {"value": "Foo", "type": "token"}), ], }, ) await controller.evaluate(graph) graph_state = og.OmniGraphInspector().as_json(graph, flags=["evaluation"]) graph_state_obj = json.loads(graph_state) trace = graph_state_obj["Evaluator"]["Instances"][0]["LastNonEmptyEvaluation"]["Trace"] # The switches can run in any order self.assertTrue( trace in (["SwitchTokenA", "AppendB", "SwitchTokenB"], ["AppendB", "SwitchTokenB", "SwitchTokenA"]) ) # ---------------------------------------------------------------------- async def test_diamond_fan(self): """Test latent nodes in parallel fan-out which fan-in to a downstream node""" # +--------++ +----------+ # +--> TickA +--->|FinishedA |---+ # | +---------+ +----------+ | # +---------+ +-----------+ | | +------------+ # |OnImpulse+-->|TickCounter+-+ +-->|MergeCounter| # +---------+ +-----------+ | | +------------+ # | +---------+ +----------+ | # +-->| TickB +--->|FinishedB |--+ # +--------++ +----------+ # | +---------+ # +-->| TickC | # +--------++ controller = og.Controller() keys = og.Controller.Keys (graph, nodes, _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("TickCounter", "omni.graph.action.Counter"), ("TickA", "omni.graph.action.Countdown"), ("TickB", "omni.graph.action.Countdown"), ("TickC", "omni.graph.action.Countdown"), ("FinishCounterA", "omni.graph.action.Counter"), ("FinishCounterB", "omni.graph.action.Counter"), ("MergeCounter", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("OnImpulse.outputs:execOut", "TickCounter.inputs:execIn"), ("TickCounter.outputs:execOut", "TickA.inputs:execIn"), ("TickCounter.outputs:execOut", "TickB.inputs:execIn"), ("TickCounter.outputs:execOut", "TickC.inputs:execIn"), ("TickA.outputs:finished", "FinishCounterA.inputs:execIn"), ("TickB.outputs:finished", "FinishCounterB.inputs:execIn"), ("FinishCounterA.outputs:execOut", "MergeCounter.inputs:execIn"), ("FinishCounterB.outputs:execOut", "MergeCounter.inputs:execIn"), ], keys.SET_VALUES: [ ("TickA.inputs:duration", 1), ("TickB.inputs:duration", 1), ("TickC.inputs:duration", 1), ("OnImpulse.inputs:onlyPlayback", False), ("OnImpulse.state:enableImpulse", True), ], }, ) (_, tick_counter, _, _, tick_c, finish_counter_a, finish_counter_b, merge_counter) = nodes def check_counts(t_c, f_a, f_b, m_c, tick_c_count): for node, expected in ( (tick_counter, t_c), (finish_counter_a, f_a), (finish_counter_b, f_b), (merge_counter, m_c), ): count = og.Controller.get(controller.attribute("outputs:count", node)) self.assertEqual(count, expected, node.get_prim_path()) self.assertEqual(tick_c.get_compute_count(), tick_c_count) self.assertEqual(tick_c.get_compute_count(), 0) # Set up latent tickers. await controller.evaluate(graph) check_counts(1, 0, 0, 0, 1) # Latent ticks. await controller.evaluate(graph) check_counts(1, 0, 0, 0, 2) # Both branches complete. await controller.evaluate(graph) check_counts(1, 1, 1, 2, 3) # No count changes + no additional computes of tickC. await controller.evaluate(graph) check_counts(1, 1, 1, 2, 3) # ---------------------------------------------------------------------- async def test_diamond_latent_fan(self): """Test latent nodes in parallel fan-out which fan-in to a latent downstream node""" # +--------++ # +--> TickA +--+ # | +---------+ | # +---------+ | | +-------+ +-------+ # |OnImpulse+-->+ +-->|TickD +-+--->|CountF | # +---------+ | | +-------+ | +-------+ # | +--------+ | +--->+-------+ # +-->| TickB +--+ |TickE | # | +--------+ +--->+-------+ # | +--------+ | # +-->| TickC +----------------+ # +--------+ # Note that when TickA triggers TickD into latent state, then TickB hits TickD subsequently. This subsequent # evaluation is _transient_. Meaning that TickB will not block on a new copy of TickD. # This is because there is only one TickD so there can be only one state (latent or not). controller = og.Controller() keys = og.Controller.Keys (graph, nodes, _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("TickA", "omni.graph.action.Countdown"), ("TickB", "omni.graph.action.Countdown"), ("TickC", "omni.graph.action.Countdown"), ("TickD", "omni.graph.action.Countdown"), ("TickE", "omni.graph.action.Countdown"), ("CountF", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("OnImpulse.outputs:execOut", "TickA.inputs:execIn"), ("OnImpulse.outputs:execOut", "TickB.inputs:execIn"), ("OnImpulse.outputs:execOut", "TickC.inputs:execIn"), ("TickA.outputs:finished", "TickD.inputs:execIn"), ("TickB.outputs:finished", "TickD.inputs:execIn"), ("TickC.outputs:finished", "TickE.inputs:execIn"), ("TickD.outputs:finished", "TickE.inputs:execIn"), ("TickD.outputs:finished", "CountF.inputs:execIn"), ], keys.SET_VALUES: [ ("TickA.inputs:duration", 1), ("TickB.inputs:duration", 1), ("TickC.inputs:duration", 2), ("TickD.inputs:duration", 1), ("TickE.inputs:duration", 1), ("OnImpulse.inputs:onlyPlayback", False), ("OnImpulse.state:enableImpulse", True), ], }, ) (_, tick_a, tick_b, tick_c, tick_d, tick_e, count_f) = nodes def check_counts(i, t_a, t_b, t_c, t_d, t_e): for node, expected in ((tick_a, t_a), (tick_b, t_b), (tick_c, t_c), (tick_d, t_d), (tick_e, t_e)): self.assertEqual(node.get_compute_count(), expected, f"Check {i} for {node.get_prim_path()}") # A, B, C, D, E compute_counts = [ (1, 1, 1, 0, 0), # 0. fan out to trigger A, B, C into latent state (2, 2, 2, 0, 0), # 1. A, B, C tick (3, 3, 3, 2, 0), # 2. A, B end latent, D into latent via A or B, D ticks via A or B, C ticks (3, 3, 4, 3, 2), # 3. (3, 3, 4, 3, 3), # 4. (3, 3, 4, 3, 3), # 5. (3, 3, 4, 3, 3), # 6. ] for i, c_c in enumerate(compute_counts): await controller.evaluate(graph) check_counts(i, *c_c) # Verify that CountF has computed 1x due to the fan-in at TickD NOT acting like separate threads. self.assertEqual(count_f.get_compute_count(), 1) # ---------------------------------------------------------------------- async def test_dynamic_exec_pins(self): """Test that adding execution pins to a non-action node works""" controller = og.Controller() keys = og.Controller.Keys (_, (on_tick, to_string), _, _,) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("ToString", "omni.graph.nodes.ToString"), ], keys.SET_VALUES: [ ("ToString.inputs:value", 42, "double"), ("OnTick.inputs:onlyPlayback", False), ], }, ) # Verify to_string has not been computed. await controller.evaluate() self.assertEqual(0, to_string.get_compute_count()) self.assertEqual(1, on_tick.get_compute_count()) # Add execution attribs and verify it still doesn't get computed. attrib = og.Controller.create_attribute( to_string, "inputs:execIn", "execution", og.AttributePortType.ATTRIBUTE_PORT_TYPE_INPUT, ) self.assertIsNotNone(attrib) await controller.evaluate() self.assertEqual(0, to_string.get_compute_count()) self.assertEqual(2, on_tick.get_compute_count()) # Hook up to OnTick and verify it is now computing. controller.edit( self.TEST_GRAPH_PATH, { keys.CONNECT: [ ("OnTick.outputs:tick", "ToString.inputs:execIn"), ] }, ) for i in range(10): await controller.evaluate() self.assertEqual(i + 1, to_string.get_compute_count()) self.assertEqual(i + 3, on_tick.get_compute_count()) # ---------------------------------------------------------------------- async def test_exec_fan_out(self): """Test that fanning out from an exec port works""" controller = og.Controller() keys = og.Controller.Keys (graph, nodes, _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("FF1", "omni.graph.action.FlipFlop"), ("FF2", "omni.graph.action.FlipFlop"), ("FF11", "omni.graph.action.FlipFlop"), ("FF12", "omni.graph.action.FlipFlop"), ], keys.SET_VALUES: [ ("OnTick.inputs:onlyPlayback", False), ], keys.CONNECT: [ ("OnTick.outputs:tick", "FF1.inputs:execIn"), ("OnTick.outputs:tick", "FF2.inputs:execIn"), ("FF1.outputs:a", "FF11.inputs:execIn"), ("FF1.outputs:a", "FF12.inputs:execIn"), ], }, ) # 1. OnTick triggers FF1 which triggers FF11 and FF12, then FF2. # 2. OnTick triggers FF1 and FF2. # 3. OnTick triggers FF1 which triggers FF11 and FF12, then FF2. await controller.evaluate(graph) flip_flops = nodes[1:] ff_state = [og.Controller.get(controller.attribute("outputs:isA", node)) for node in flip_flops] self.assertEqual(ff_state, [True, True, True, True]) await controller.evaluate(graph) ff_state = [og.Controller.get(controller.attribute("outputs:isA", node)) for node in flip_flops] self.assertEqual(ff_state, [False, False, True, True]) await controller.evaluate(graph) ff_state = [og.Controller.get(controller.attribute("outputs:isA", node)) for node in flip_flops] self.assertEqual(ff_state, [True, True, False, False]) # ---------------------------------------------------------------------- async def test_exec_fan_out_shared_deps(self): """Test that dependency sort works when there is shared data in exec fan-out""" # +---------+ # +---------->| Write1 | # | +----^----+ # | | # | +----------+ # | | # +-----------+ | | # | OnImpulse +-----+-----+----> +---------+ # +-----------+ | | | Write2 | # | +----->+---------+ # | | # | | +---------+ # +-----+----->| Write3 | # | +---------+ # | ^ # +-------+ +---+----+---+ # | Const +----->| Inc | # +-------+ +--------+ controller = og.Controller() keys = og.Controller.Keys (graph, _, _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("Const", "omni.graph.nodes.ConstantDouble"), ("Inc", "omni.graph.nodes.Increment"), ("Write1", "omni.graph.nodes.WritePrimAttribute"), ("Write2", "omni.graph.nodes.WritePrimAttribute"), ("Write3", "omni.graph.nodes.WritePrimAttribute"), ], keys.CREATE_PRIMS: [ ("/World/TestPrim1", {"val": ("double", 1.0)}), ("/World/TestPrim2", {"val": ("double", 2.0)}), ("/World/TestPrim3", {"val": ("double", 3.0)}), ], keys.CONNECT: [ ("OnImpulse.outputs:execOut", "Write1.inputs:execIn"), ("OnImpulse.outputs:execOut", "Write2.inputs:execIn"), ("OnImpulse.outputs:execOut", "Write3.inputs:execIn"), ("Const.inputs:value", "Inc.inputs:value"), ("Inc.outputs:result", "Write1.inputs:value"), ("Inc.outputs:result", "Write2.inputs:value"), ("Inc.outputs:result", "Write3.inputs:value"), ], keys.SET_VALUES: [ ("OnImpulse.inputs:onlyPlayback", False), ("OnImpulse.state:enableImpulse", True), ("Const.inputs:value", 41.0), ("Inc.inputs:increment", 1.0), ("Write1.inputs:primPath", "/World/TestPrim1"), ("Write1.inputs:usePath", True), ("Write1.inputs:name", "val"), ("Write2.inputs:primPath", "/World/TestPrim2"), ("Write2.inputs:usePath", True), ("Write2.inputs:name", "val"), ("Write3.inputs:primPath", "/World/TestPrim3"), ("Write3.inputs:usePath", True), ("Write3.inputs:name", "val"), ], }, ) await controller.evaluate(graph) stage = omni.usd.get_context().get_stage() for i in (1, 2, 3): self.assertEqual(stage.GetAttributeAtPath(f"/World/TestPrim{i}.val").Get(), 42.0) # ---------------------------------------------------------------------- async def test_exec_sort_failure(self): """Test that sorting dependencies with non-trivial authored graph""" # Our global sort excludes exec nodes, so a global topo (Kahn) sort will fail such that Inc3 doesn't get # computed until after Add2, so instead we sort each dep network independently. This test verifies the case # where that matters. # # +-----------------------------> Write1(var) +----------------------------------------+ # | ^ | | # | | | v # OnTick --------------------+ | +-----------Inc------------+ Write2(var2) # | | | ^ # v | | | # Read1(var)------------> Add1 --Inc2--+ v | # Inc3 --------------> Add2 ---------------+ controller = og.Controller() keys = og.Controller.Keys (_, (on_tick, a_1, a_2, _, _, _, _, _, _), _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("A1", "omni.graph.nodes.Add"), ("A2", "omni.graph.nodes.Add"), ("Write1", "omni.graph.core.WriteVariable"), ("Write2", "omni.graph.core.WriteVariable"), ("Read1", "omni.graph.core.ReadVariable"), ("Inc", "omni.graph.nodes.Increment"), ("Inc2", "omni.graph.nodes.Increment"), ("Inc3", "omni.graph.nodes.Increment"), ], keys.CREATE_VARIABLES: [ ("var", og.Type(og.BaseDataType.DOUBLE)), ("var2", og.Type(og.BaseDataType.DOUBLE)), ], keys.SET_VALUES: [ ("OnTick.inputs:onlyPlayback", False), ("Inc3.inputs:value", {"type": "double", "value": 42.0}), ("Write1.inputs:variableName", "var"), ("Write2.inputs:variableName", "var2"), ("Read1.inputs:variableName", "var"), ], keys.CONNECT: [ ("OnTick.outputs:tick", "Write1.inputs:execIn"), ("OnTick.outputs:timeSinceStart", "A1.inputs:a"), ("Read1.outputs:value", "A1.inputs:b"), ("A1.outputs:sum", "Inc2.inputs:value"), ("Inc2.outputs:result", "Write1.inputs:value"), ("Write1.outputs:execOut", "Write2.inputs:execIn"), ("Write1.outputs:value", "Inc.inputs:value"), ("Inc.outputs:result", "A2.inputs:a"), ("Inc3.outputs:result", "A2.inputs:b"), ("A2.outputs:sum", "Write2.inputs:value"), ], }, ) await omni.kit.app.get_app().next_update_async() a_1_v = og.Controller.get(controller.attribute("outputs:sum", a_1)) a_2_v = og.Controller.get(controller.attribute("outputs:sum", a_2)) on_tick_dt = og.Controller.get(controller.attribute("outputs:timeSinceStart", on_tick)) a_1_expected = 0 + on_tick_dt a_2_expected = (a_1_expected + 1.0 + 1.0) + (42.0 + 1.0) self.assertAlmostEqual(a_1_v, a_1_expected, places=3) self.assertAlmostEqual(a_2_v, a_2_expected, places=3) # ---------------------------------------------------------------------- async def test_fan_in(self): """Test that fan-in of execution connections works as expected (from a loaded test .usda file)""" (result, error) = await ogts.load_test_file("TestActionFanIn.usda", use_caller_subdirectory=True) self.assertTrue(result, error) graph_path = "/World/ActionGraph" controller = og.Controller() graph = controller.graph(graph_path) # Trigger the loop. og.Controller.set(controller.attribute(f"{graph_path}/on_impulse_event.state:enableImpulse"), True) await controller.evaluate(graph) graph_state = og.OmniGraphInspector().as_json(controller.graph(graph_path), flags=["evaluation"]) graph_state_obj = json.loads(graph_state) trace = graph_state_obj["Evaluator"]["Instances"][0]["LastNonEmptyEvaluation"]["Trace"] # Verify the first loop iteration. self.assertEqual("for_loop", trace[0]) # These nodes can compute in any order self.assertEqual(["counter", "counter_01"], sorted(trace[1:3])) expected_trace = [ "to_uint64", "sync_gate", "to_uint64", "sync_gate", ] self.assertListEqual(expected_trace, trace[3:7]) trace[0:7] = [] # Verify downstream from sync gate. expected_trace = [ "counter_02", ] self.assertListEqual(expected_trace, trace[0:1]) trace[0 : len(expected_trace)] = [] # Verify second iteration. self.assertEqual("for_loop", trace[0]) # These nodes can compute in any order self.assertEqual(["counter", "counter_01"], sorted(trace[1:3])) expected_trace = [ "to_uint64", "sync_gate", "to_uint64", "sync_gate", ] self.assertListEqual(expected_trace, trace[3:7]) # ---------------------------------------------------------------------- async def test_loading_type_resol(self): """Test that loading a file with weird type resolution pattern works""" (result, error) = await ogts.load_test_file("load_with_type_resol.usda", use_caller_subdirectory=True) self.assertTrue(result, error) graph_path = "/World/ActionGraph" controller = og.Controller() graph = controller.graph(graph_path) # eval await controller.evaluate(graph) # check result var = graph.find_variable("Result") val = var.get_array(graph.get_default_graph_context(), False, 0) self.assertTrue((val == [(-50, -50, -50), (50, 50, 50)]).all()) # ---------------------------------------------------------------------- async def test_fan_in_exec(self): """Test that execution fan-in is handled correctly""" # The evaluator has to consider the case that gate.enter will have contradicting upstream values. # Gate needs to know which input is active, it needs the value of enter to be ENABLED when it # is triggered by OnTick, even though OnTickDisabled has set it's output to the same attrib as DISABLED. # # +--------------+ # |OnImpulseEvent+---+ +-----------+ # +--------------+ | |Gate | # +--->toggle | # +--------------+ | | # |OnTick +------>|enter | # +--------------+ +^-----exit-+ # | # +--------------+ | # |OnTickDisabled+--------+ # +--------------+ controller = og.Controller() keys = og.Controller.Keys (graph, (_, _, gate, on_impulse_event), _, _,) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("OnTickDisabled", "omni.graph.action.OnTick"), ("Gate", "omni.graph.action.Gate"), ("OnImpulseEvent", "omni.graph.action.OnImpulseEvent"), ], keys.CONNECT: [ ("OnTick.outputs:tick", "Gate.inputs:enter"), ("OnTickDisabled.outputs:tick", "Gate.inputs:enter"), ("OnImpulseEvent.outputs:execOut", "Gate.inputs:toggle"), ], keys.SET_VALUES: [ ("Gate.inputs:startClosed", True), ("OnTick.inputs:onlyPlayback", False), ("OnImpulseEvent.inputs:onlyPlayback", False), ], }, ) await controller.evaluate(graph) gate_exit = controller.attribute("outputs:exit", gate) # Verify the Gate has not triggered. self.assertFalse(gate_exit.get()) await controller.evaluate(graph) self.assertFalse(gate_exit.get()) # Toggle the gate and verify that the tick goes through. The first evaluate it isn't known if the Gate will # trigger because the order that entry points are executed is not defined... FIXME. controller.attribute("state:enableImpulse", on_impulse_event).set(True) await controller.evaluate(graph) await controller.evaluate(graph) self.assertTrue(gate_exit.get()) # ---------------------------------------------------------------------- async def test_fan_out_exec(self): """Test that execution fan-out is handled correctly""" # We want to reset the execution attribute states before the node compute() to avoid bugs # that arise when authors forget to fully specify the output states. However we can't # do this in the middle of traversal, because fan-out from a connection requires that the state # be preserved for every downstream node which may read from it (like Gate). controller = og.Controller() keys = og.Controller.Keys (graph, (_, gate, _), _, _,) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("Gate", "omni.graph.action.Gate"), ("Counter", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("OnTick.outputs:tick", "Counter.inputs:execIn"), ("OnTick.outputs:tick", "Gate.inputs:enter"), ], keys.SET_VALUES: [ ("Gate.inputs:startClosed", False), ("OnTick.inputs:onlyPlayback", False), ], }, ) await controller.evaluate(graph) gate_exit = controller.attribute("outputs:exit", gate) # Verify the Gate has triggered. self.assertTrue(gate_exit.get()) await controller.evaluate(graph) self.assertTrue(gate_exit.get()) # ---------------------------------------------------------------------- async def test_latent_and_push(self): """Exercise latent nodes in combination with stateful loop node""" # # +---------+ +-------+ tick +--------+ loopBody +-------+ +------------+ # |OnImpulse+-->|TickA +----------->ForLoop1++--------->|TickB +-+->|TickCounterB| # +---------+ +----+--+ +--------++ +-------+ | +------------+ # | finish | | # | | | # | +--------------+ +v----------------+ +-v------------+ # +----->|FinishCounterA| |FinishLoopCounter| |FinishCounterB| # +--------------+ +-----------------+ +--------------+ controller = og.Controller() keys = og.Controller.Keys (graph, nodes, _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("TickA", "omni.graph.action.Countdown"), ("ForLoop1", "omni.graph.action.ForLoop"), ("TickB", "omni.graph.action.Countdown"), ("FinishLoopCounter", "omni.graph.action.Counter"), ("FinishCounterA", "omni.graph.action.Counter"), ("FinishCounterB", "omni.graph.action.Counter"), ("TickCounterB", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("OnImpulse.outputs:execOut", "TickA.inputs:execIn"), ("TickA.outputs:tick", "ForLoop1.inputs:execIn"), ("TickA.outputs:finished", "FinishCounterA.inputs:execIn"), ("ForLoop1.outputs:loopBody", "TickB.inputs:execIn"), ("ForLoop1.outputs:finished", "FinishLoopCounter.inputs:execIn"), ("TickB.outputs:tick", "TickCounterB.inputs:execIn"), ("TickB.outputs:finished", "FinishCounterB.inputs:execIn"), ], keys.SET_VALUES: [ ("ForLoop1.inputs:start", 0), ("ForLoop1.inputs:stop", 3), ("TickA.inputs:duration", 2), ("TickB.inputs:duration", 2), ("OnImpulse.state:enableImpulse", True), ("OnImpulse.inputs:onlyPlayback", False), ], }, ) (_, _, _, _, finish_loop_counter, finish_counter_a, finish_counter_b, tick_counter_b) = nodes for _ in range(20): await controller.evaluate(graph) self.assertEqual(og.Controller.get(controller.attribute("outputs:count", finish_counter_a)), 1) self.assertEqual(og.Controller.get(controller.attribute("outputs:count", tick_counter_b)), 12) self.assertEqual(og.Controller.get(controller.attribute("outputs:count", finish_counter_b)), 6) self.assertEqual(og.Controller.get(controller.attribute("outputs:count", finish_loop_counter)), 2) # ---------------------------------------------------------------------- async def test_latent_chain(self): """Exercise a chain of latent nodes""" # +---------+ +-------+ tick +-------+ tick +-------+ # |OnImpulse+-->TickA +-------->TickB +-------->|LatentC| # +---------+ +-----+-+ +------++ +-------+-----+ # | finish | finish | # finish | +-------------+ | +-------------+ +-v----------+ # +->TickCounterA | +-->| TickCounterB| |TickCounterC| # +-------------+ +-------------+ +------------+ controller = og.Controller() keys = og.Controller.Keys (graph, nodes, _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("TickA", "omni.graph.action.Countdown"), ("TickB", "omni.graph.action.Countdown"), ("LatentC", "omni.graph.action.Countdown"), ("TickCounterA", "omni.graph.action.Counter"), ("TickCounterB", "omni.graph.action.Counter"), ("TickCounterC", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("OnImpulse.outputs:execOut", "TickA.inputs:execIn"), ("TickA.outputs:tick", "TickB.inputs:execIn"), ("TickA.outputs:finished", "TickCounterA.inputs:execIn"), ("TickB.outputs:tick", "LatentC.inputs:execIn"), ("TickB.outputs:finished", "TickCounterB.inputs:execIn"), ("LatentC.outputs:finished", "TickCounterC.inputs:execIn"), ], keys.SET_VALUES: [ ("TickA.inputs:duration", 2), ("TickB.inputs:duration", 2), ("LatentC.inputs:duration", 2), ("OnImpulse.inputs:onlyPlayback", False), ("OnImpulse.state:enableImpulse", True), ], }, ) (_, _, _, _, tick_counter_a, tick_counter_b, tick_counter_c) = nodes for _ in range(16): await controller.evaluate(graph) self.assertEqual(og.Controller.get(controller.attribute("outputs:count", tick_counter_a)), 1) self.assertEqual(og.Controller.get(controller.attribute("outputs:count", tick_counter_b)), 2) self.assertEqual(og.Controller.get(controller.attribute("outputs:count", tick_counter_c)), 4)
44,222
Python
47.225736
116
0.453349
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/tests/test_on_stage_event_node.py
"""Basic tests of the OnStageEvent node""" import omni.client import omni.graph.core as og import omni.graph.core.tests as ogts import omni.graph.tools.ogn as ogn import omni.kit.app import omni.kit.test import omni.usd from pxr import Sdf # ====================================================================== class TestOnStageEventNode(ogts.OmniGraphTestCase): """Tests OnStageEvent node functionality""" TEST_GRAPH_PATH = "/World/TestGraph" async def setUp(self): """Set up test environment, to be torn down when done""" await super().setUp() og.Controller.edit({"graph_path": self.TEST_GRAPH_PATH, "evaluator_name": "execution"}) async def test_backward_compatibility_v3(self): """Validate backward compatibility for legacy versions of OnStageEvent node.""" # load the test scene which contains a OnStageEvent V2 node (result, error) = await ogts.load_test_file("TestOnStageEventNode_v2.usda", use_caller_subdirectory=True) self.assertTrue(result, error) action_graph_path = "/World/ActionGraph" action_graph = og.get_graph_by_path(action_graph_path) on_stage_event_node = action_graph.get_node(action_graph_path + "/on_stage_event") self.assertTrue(on_stage_event_node.is_valid()) # The "Hierarchy Changed" event has been introduced since V3. Validate that it is # automatically included by the list of allowed tokens after loading V2. attr = on_stage_event_node.get_attribute("inputs:eventName") allowed_tokens = attr.get_metadata(ogn.MetadataKeys.ALLOWED_TOKENS) self.assertTrue(isinstance(allowed_tokens, str)) self.assertTrue("Hierarchy Changed" in allowed_tokens.split(",")) async def test_stage_events(self): """Test OnStageEvent""" controller = og.Controller() keys = og.Controller.Keys (_, (on_stage_node, _, _, counter_sel_node, counter_stop_node, counter_start_node), _, _,) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnStageEvent", "omni.graph.action.OnStageEvent"), ("OnStageEvent2", "omni.graph.action.OnStageEvent"), ("OnStageEvent3", "omni.graph.action.OnStageEvent"), ("Counter", "omni.graph.action.Counter"), ("Counter2", "omni.graph.action.Counter"), ("Counter3", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("OnStageEvent.outputs:execOut", "Counter.inputs:execIn"), ("OnStageEvent2.outputs:execOut", "Counter2.inputs:execIn"), ("OnStageEvent3.outputs:execOut", "Counter3.inputs:execIn"), ], keys.SET_VALUES: [ ("OnStageEvent.inputs:eventName", "Selection Changed"), ("OnStageEvent.inputs:onlyPlayback", False), ("OnStageEvent2.inputs:eventName", "Animation Stop Play"), ("OnStageEvent2.inputs:onlyPlayback", True), ("OnStageEvent3.inputs:eventName", "Animation Start Play"), ("OnStageEvent3.inputs:onlyPlayback", True), ], }, ) async def wait_2(): await omni.kit.app.get_app().next_update_async() await omni.kit.app.get_app().next_update_async() def get_start_count(): return og.Controller.get(controller.attribute("outputs:count", counter_start_node)) def get_stop_count(): return og.Controller.get(controller.attribute("outputs:count", counter_stop_node)) await omni.kit.app.get_app().next_update_async() self.assertEqual(og.Controller.get(controller.attribute("outputs:count", counter_sel_node)), 0) self.assertEqual(get_stop_count(), 0) self.assertEqual(get_start_count(), 0) selection = omni.usd.get_context().get_selection() selection.set_selected_prim_paths([self.TEST_GRAPH_PATH + "/OnStageEvent"], False) # 1 frame delay on the pop, 1 frame delay on the compute await omni.kit.app.get_app().next_update_async() await omni.kit.app.get_app().next_update_async() self.assertEqual(og.Controller.get(controller.attribute("outputs:count", counter_sel_node)), 1) self.assertEqual(get_stop_count(), 0) self.assertEqual(get_start_count(), 0) # change the tracked event, verify selection doesn't fire og.Controller.set(controller.attribute("inputs:eventName", on_stage_node), "Saved") selection.set_selected_prim_paths([self.TEST_GRAPH_PATH + "/OnStageEvent2"], False) await omni.kit.app.get_app().next_update_async() self.assertEqual(og.Controller.get(controller.attribute("outputs:count", counter_sel_node)), 1) await omni.kit.app.get_app().next_update_async() # change it back, verify it does fire when selection changes again og.Controller.set(controller.attribute("inputs:eventName", on_stage_node), "Selection Changed") await omni.kit.app.get_app().next_update_async() self.assertEqual(og.Controller.get(controller.attribute("outputs:count", counter_sel_node)), 1) selection.set_selected_prim_paths([self.TEST_GRAPH_PATH + "/OnStageEvent"], False) await wait_2() self.assertEqual(og.Controller.get(controller.attribute("outputs:count", counter_sel_node)), 2) # Verify that start/stop events work when only-playback is true timeline = omni.timeline.get_timeline_interface() timeline.set_start_time(1.0) timeline.set_end_time(10.0) timeline.set_target_framerate(timeline.get_time_codes_per_seconds()) timeline.play() await wait_2() self.assertEqual(get_stop_count(), 0) self.assertEqual(get_start_count(), 1) await omni.kit.app.get_app().next_update_async() self.assertEqual(get_stop_count(), 0) self.assertEqual(get_start_count(), 1) # Check that pausing / resuming does not trigger timeline.pause() await wait_2() self.assertEqual(get_stop_count(), 0) self.assertEqual(get_start_count(), 1) timeline.play() await wait_2() self.assertEqual(get_stop_count(), 0) self.assertEqual(get_start_count(), 1) timeline.stop() await wait_2() self.assertEqual(get_stop_count(), 1) self.assertEqual(get_start_count(), 1) await controller.evaluate() self.assertEqual(get_stop_count(), 1) self.assertEqual(get_start_count(), 1) # Verify that stopping while paused triggers the event timeline.play() await wait_2() self.assertEqual(get_stop_count(), 1) self.assertEqual(get_start_count(), 2) timeline.pause() await wait_2() self.assertEqual(get_stop_count(), 1) self.assertEqual(get_start_count(), 2) timeline.stop() await wait_2() self.assertEqual(get_stop_count(), 2) self.assertEqual(get_start_count(), 2) # ---------------------------------------------------------------------- async def test_stage_hierarchy_changed_event(self): """Test the Hierarchy Changed event""" app = omni.kit.app.get_app() controller = og.Controller() keys = og.Controller.Keys usd_context = omni.usd.get_context() stage = usd_context.get_stage() root_path = Sdf.Path.absoluteRootPath # Create Xform omni.kit.commands.execute("CreatePrim", prim_type="Xform") xform_path = root_path.AppendChild("Xform") xform = stage.GetPrimAtPath(xform_path) self.assertTrue(xform) # Create Material omni.kit.commands.execute("CreatePrim", prim_type="Material") material_path = root_path.AppendChild("Material") material = stage.GetPrimAtPath(material_path) self.assertTrue(material) # Create action graph (_, (on_stage_node, counter_node), _, _,) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnStageEvent", "omni.graph.action.OnStageEvent"), ("Counter", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("OnStageEvent.outputs:execOut", "Counter.inputs:execIn"), ], keys.SET_VALUES: [ ("OnStageEvent.inputs:eventName", "Hierarchy Changed"), ("OnStageEvent.inputs:onlyPlayback", False), ], }, ) outputs_count_attr = controller.attribute("outputs:count", counter_node) expected_hierarchy_changed_event_count = 0 await app.next_update_async() self.assertEqual(og.Controller.get(outputs_count_attr), expected_hierarchy_changed_event_count) ########################################################### # Create cube omni.kit.commands.execute("CreatePrim", prim_type="Cube") cube_path = root_path.AppendChild("Cube") cube = stage.GetPrimAtPath(cube_path) self.assertTrue(cube) # 1 frame delay on the pop, 1 frame delay on the compute await app.next_update_async() await app.next_update_async() expected_hierarchy_changed_event_count += 1 self.assertEqual(og.Controller.get(outputs_count_attr), expected_hierarchy_changed_event_count) ########################################################### # Reparent cube cube_path_reparented = xform_path.AppendChild("Cube") omni.kit.commands.execute("MovePrim", path_from=cube_path, path_to=cube_path_reparented) await app.next_update_async() await app.next_update_async() expected_hierarchy_changed_event_count += 1 self.assertEqual(og.Controller.get(outputs_count_attr), expected_hierarchy_changed_event_count) ########################################################### # Rename cube to lowercase cube_path_lowercase = xform_path.AppendChild("cube") omni.kit.commands.execute("MovePrim", path_from=cube_path_reparented, path_to=cube_path_lowercase) await app.next_update_async() await app.next_update_async() expected_hierarchy_changed_event_count += 1 self.assertEqual(og.Controller.get(outputs_count_attr), expected_hierarchy_changed_event_count) ########################################################### # Modify size attribute. cube = stage.GetPrimAtPath(cube_path_lowercase) self.assertTrue(cube) cube.GetAttribute("size").Set(1.0) await app.next_update_async() await app.next_update_async() # The "Hierarchy Changed" event is not expected for attribute change. self.assertEqual(og.Controller.get(outputs_count_attr), expected_hierarchy_changed_event_count) ########################################################### # Modify material binding. rel = cube.CreateRelationship("material:binding", False) rel.SetTargets([material_path]) await app.next_update_async() await app.next_update_async() # The "Hierarchy Changed" event is not expected for relationship change. self.assertEqual(og.Controller.get(outputs_count_attr), expected_hierarchy_changed_event_count) ########################################################### # Change the tracked event og.Controller.set(controller.attribute("inputs:eventName", on_stage_node), "Saved") await og.Controller.evaluate() omni.kit.commands.execute("MovePrim", path_from=cube_path_lowercase, path_to=cube_path) await app.next_update_async() await app.next_update_async() # verify hierarchy changed event doesn't fire self.assertEqual(og.Controller.get(outputs_count_attr), expected_hierarchy_changed_event_count) ########################################################### # Change it back, verify it does fire when hierarchy changes again og.Controller.set(controller.attribute("inputs:eventName", on_stage_node), "Hierarchy Changed") await og.Controller.evaluate() # Remove cube omni.kit.commands.execute("DeletePrims", paths=[cube_path]) await app.next_update_async() await app.next_update_async() expected_hierarchy_changed_event_count += 1 self.assertEqual(og.Controller.get(outputs_count_attr), expected_hierarchy_changed_event_count)
12,775
Python
42.016835
117
0.599687
omniverse-code/kit/exts/omni.graph.action/omni/graph/action/tests/test_evaluation.py
"""Action Graph Evaluation Tests""" import asyncio import carb.events import omni.client import omni.graph.core as og import omni.graph.core.tests as ogts import omni.kit.test import omni.usd # ====================================================================== class TestActionGraphEvaluation(ogts.OmniGraphTestCase): """Tests action graph evaluator functionality""" TEST_GRAPH_PATH = "/World/TestGraph" async def setUp(self): """Set up test environment, to be torn down when done""" await super().setUp() og.Controller.edit({"graph_path": self.TEST_GRAPH_PATH, "evaluator_name": "execution"}) # ---------------------------------------------------------------------- async def test_exec_fan_out(self): """Test that fanning out from an exec port works""" controller = og.Controller() keys = og.Controller.Keys (graph, nodes, _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("FF1", "omni.graph.action.FlipFlop"), ("FF2", "omni.graph.action.FlipFlop"), ("FF11", "omni.graph.action.FlipFlop"), ("FF12", "omni.graph.action.FlipFlop"), ], keys.SET_VALUES: [ ("OnTick.inputs:onlyPlayback", False), ], keys.CONNECT: [ ("OnTick.outputs:tick", "FF1.inputs:execIn"), ("OnTick.outputs:tick", "FF2.inputs:execIn"), ("FF1.outputs:a", "FF11.inputs:execIn"), ("FF1.outputs:a", "FF12.inputs:execIn"), ], }, ) # 1. OnTick triggers FF1 which triggers FF11 and FF12, then FF2 # 2. OnTick triggers FF1 and FF2 # 3. OnTick triggers FF1 which triggers FF11 and FF12, then FF2 await controller.evaluate(graph) flip_flops = nodes[1:] ff_state = [og.Controller.get(controller.attribute("outputs:isA", node)) for node in flip_flops] self.assertEqual(ff_state, [True, True, True, True]) await controller.evaluate(graph) ff_state = [og.Controller.get(controller.attribute("outputs:isA", node)) for node in flip_flops] self.assertEqual(ff_state, [False, False, True, True]) await controller.evaluate(graph) ff_state = [og.Controller.get(controller.attribute("outputs:isA", node)) for node in flip_flops] self.assertEqual(ff_state, [True, True, False, False]) # ---------------------------------------------------------------------- async def test_chained_stateful_nodes(self): """Test that chaining loop nodes works""" controller = og.Controller() keys = og.Controller.Keys (graph, (_, _, _, counter_node), _, _,) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("ForLoop1", "omni.graph.action.ForLoop"), ("ForLoop2", "omni.graph.action.ForLoop"), ("Counter", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("OnTick.outputs:tick", "ForLoop1.inputs:execIn"), ("ForLoop1.outputs:loopBody", "ForLoop2.inputs:execIn"), ("ForLoop2.outputs:loopBody", "Counter.inputs:execIn"), ], keys.SET_VALUES: [ ("OnTick.inputs:onlyPlayback", False), ("ForLoop1.inputs:stop", 5), ("ForLoop2.inputs:stop", 5), ], }, ) await controller.evaluate(graph) self.assertEqual(og.Controller.get(controller.attribute("outputs:count", counter_node)), 5 * 5) # ---------------------------------------------------------------------- async def test_async_nodes(self): """Test asynchronous action nodes""" # Check that a nested loop state is maintained when executing a latent delay # # +---------+ +----------+ +----------+ +-------+ +--------+ # | IMPULSE +-->| FOR-LOOP +--->| FOR-LOOP +--->| DELAY +--->| COUNTER| # +---------+ +----------+ +----------+ +-------+ +--------+ # controller = og.Controller() keys = og.Controller.Keys (graph, (_, _, _, _, counter_node, _, _), _, _,) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("ForLoop1", "omni.graph.action.ForLoop"), ("ForLoop2", "omni.graph.action.ForLoop"), ("Delay", "omni.graph.action.Delay"), ("Counter", "omni.graph.action.Counter"), ("OnTick", "omni.graph.action.OnTick"), ("Counter2", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("OnImpulse.outputs:execOut", "ForLoop1.inputs:execIn"), ("ForLoop1.outputs:loopBody", "ForLoop2.inputs:execIn"), ("ForLoop2.outputs:loopBody", "Delay.inputs:execIn"), ("Delay.outputs:finished", "Counter.inputs:execIn"), ("OnTick.outputs:tick", "Counter2.inputs:execIn"), ], keys.SET_VALUES: [ ("OnTick.inputs:onlyPlayback", False), ("OnImpulse.inputs:onlyPlayback", False), ("Delay.inputs:duration", 0.1), ("ForLoop1.inputs:stop", 2), ("ForLoop2.inputs:stop", 5), ], }, ) await controller.evaluate(graph) # trigger graph once controller.edit(self.TEST_GRAPH_PATH, {keys.SET_VALUES: ("OnImpulse.state:enableImpulse", True)}) await controller.evaluate(graph) # in delay now, no count counter_controller = og.Controller(og.Controller.attribute("outputs:count", counter_node)) self.assertEqual(counter_controller.get(), 0) # wait to ensure the first 5 delays compute for _ in range(5): await asyncio.sleep(0.2) await controller.evaluate(graph) count_val = counter_controller.get() self.assertGreater(count_val, 4) # wait and verify the remainder go through for _ in range(5): await asyncio.sleep(0.1) await controller.evaluate(graph) self.assertEqual(counter_controller.get(), 10) # ---------------------------------------------------------------------- async def test_stateful_flowcontrol_evaluation(self): """Test that stateful flow control nodes are fully evaluated""" # b # +----------+ +---------+ # +--->| Sequence +-->|Counter1 | # | +----------+ +---------+ # +-----------+ | # | OnImpulse +-+ # +-----------+ | # | +----------+ +----------+ # +--->| ForLoop1 +-->| Counter2 | # +----------+ +----------+ # finished controller = og.Controller() keys = og.Controller.Keys (graph, (_, _, counter1_node, _, counter2_node), _, _,) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("Sequence", "omni.graph.action.Sequence"), ("Counter1", "omni.graph.action.Counter"), ("ForLoop1", "omni.graph.action.ForLoop"), ("Counter2", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("OnImpulse.outputs:execOut", "Sequence.inputs:execIn"), ("Sequence.outputs:b", "Counter1.inputs:execIn"), ("OnImpulse.outputs:execOut", "ForLoop1.inputs:execIn"), ("ForLoop1.outputs:finished", "Counter2.inputs:execIn"), ], keys.SET_VALUES: [("OnImpulse.inputs:onlyPlayback", False), ("ForLoop1.inputs:stop", 10)], }, ) await controller.evaluate(graph) # trigger graph once controller.edit(self.TEST_GRAPH_PATH, {keys.SET_VALUES: ("OnImpulse.state:enableImpulse", True)}) await controller.evaluate(graph) # verify that counter was called in spite of sequence 'a' being disconnected self.assertEqual(og.Controller.get(controller.attribute("outputs:count", counter1_node)), 1) # verify that counter was called in spite of there being no loopBody - execution evaluator has to still trigger # the loop 11 times despite there being no downstream connection self.assertEqual(og.Controller.get(controller.attribute("outputs:count", counter2_node)), 1) # ---------------------------------------------------------------------- async def test_request_driven_node(self): """Test that RequestDriven nodes are computed as expected""" controller = og.Controller() keys = og.Controller.Keys (graph, (_, counter_node), _, _,) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("Counter", "omni.graph.action.Counter"), ], keys.SET_VALUES: [("OnImpulse.inputs:onlyPlayback", False)], keys.CONNECT: ("OnImpulse.outputs:execOut", "Counter.inputs:execIn"), }, ) # After several updates, there should have been no compute calls await controller.evaluate(graph) await controller.evaluate(graph) await controller.evaluate(graph) counter_controller = og.Controller(og.Controller.attribute("outputs:count", counter_node)) self.assertEqual(counter_controller.get(), 0) # change OnImpulse state attrib. The node should now request compute controller.edit(self.TEST_GRAPH_PATH, {keys.SET_VALUES: ("OnImpulse.state:enableImpulse", True)}) await controller.evaluate(graph) self.assertEqual(counter_controller.get(), 1) # more updates should not result in more computes await controller.evaluate(graph) await controller.evaluate(graph) await controller.evaluate(graph) self.assertEqual(counter_controller.get(), 1) # ---------------------------------------------------------------------- async def test_fan_in_exec(self): """Test that execution fan-in is handled correctly.""" # The evaluator has to consider the case that gate.enter will have contradicting upstream values. # Gate needs to know which input is active, it needs the value of enter to be ENABLED when it # is triggered by OnTick, even though OnTickDisabled has set it's output to the same attrib as DISABLED. # # +--------------+ # |OnImpulseEvent+---+ +-----------+ # +--------------+ | |Gate | # +--->toggle | # +--------------+ | | # |OnTick +------>|enter | # +--------------+ +^-----exit-+ # | # +--------------+ | # |OnTickDisabled+--------+ # +--------------+ controller = og.Controller() keys = og.Controller.Keys (graph, (_, _, gate, on_impulse_event), _, _,) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("OnTickDisabled", "omni.graph.action.OnTick"), ("Gate", "omni.graph.action.Gate"), ("OnImpulseEvent", "omni.graph.action.OnImpulseEvent"), ], keys.CONNECT: [ ("OnTick.outputs:tick", "Gate.inputs:enter"), ("OnTickDisabled.outputs:tick", "Gate.inputs:enter"), ("OnImpulseEvent.outputs:execOut", "Gate.inputs:toggle"), ], keys.SET_VALUES: [ ("Gate.inputs:startClosed", True), ("OnTick.inputs:onlyPlayback", False), ("OnImpulseEvent.inputs:onlyPlayback", False), ], }, ) await controller.evaluate(graph) gate_exit = controller.attribute("outputs:exit", gate) # Verify the Gate has not triggered self.assertFalse(gate_exit.get()) await controller.evaluate(graph) self.assertFalse(gate_exit.get()) # toggle the gate and verify that the tick goes through. The first evaluate it isn't known if the Gate will # trigger because the order that entry points are executed is not defined... FIXME controller.attribute("state:enableImpulse", on_impulse_event).set(True) await controller.evaluate(graph) await controller.evaluate(graph) self.assertTrue(gate_exit.get()) # ---------------------------------------------------------------------- async def test_fan_out_exec(self): """Test that execution fan-out is handled correctly.""" # We want to reset the execution attribute states before the node compute() to avoid bugs # that arise when authors forget to fully specify the output states. However we can't # do this in the middle of traversal, because fan-out from a connection requires that the state # be preserved for every downstream node which may read from it (like Gate). controller = og.Controller() keys = og.Controller.Keys (graph, (_, gate, _), _, _,) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("Gate", "omni.graph.action.Gate"), ("Counter", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("OnTick.outputs:tick", "Counter.inputs:execIn"), ("OnTick.outputs:tick", "Gate.inputs:enter"), ], keys.SET_VALUES: [ ("Gate.inputs:startClosed", False), ("OnTick.inputs:onlyPlayback", False), ], }, ) await controller.evaluate(graph) gate_exit = controller.attribute("outputs:exit", gate) # Verify the Gate has triggered self.assertTrue(gate_exit.get()) await controller.evaluate(graph) self.assertTrue(gate_exit.get()) # ---------------------------------------------------------------------- async def test_onclosing(self): """Test OnClosing node""" # Test OnClosing is tricky because OG is being destroyed when it happens - # so test by sending a custom event when the network is triggered # and then checking if we got that event def registered_event_name(event_name): """Returns the internal name used for the given custom event name""" n = "omni.graph.action." + event_name return carb.events.type_from_string(n) got_event = [False] def on_event(_): got_event[0] = True reg_event_name = registered_event_name("foo") message_bus = omni.kit.app.get_app().get_message_bus_event_stream() sub = message_bus.create_subscription_to_push_by_type(reg_event_name, on_event) self.assertIsNotNone(sub) controller = og.Controller() keys = og.Controller.Keys controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnClosing", "omni.graph.action.OnClosing"), ("Send", "omni.graph.action.SendCustomEvent"), ], keys.CONNECT: [("OnClosing.outputs:execOut", "Send.inputs:execIn")], keys.SET_VALUES: [("Send.inputs:eventName", "foo"), ("Send.inputs:path", "Test Path")], }, ) # evaluate once so that graph is in steady state await controller.evaluate() # close the stage usd_context = omni.usd.get_context() (result, _) = await usd_context.close_stage_async() self.assertTrue(result) # Check our handler was called self.assertTrue(got_event[0]) async def test_onloaded(self): """Test OnLoaded node""" def registered_event_name(event_name): """Returns the internal name used for the given custom event name""" n = "omni.graph.action." + event_name return carb.events.type_from_string(n) events = [] def on_event(e): events.append(e.payload["!path"]) reg_event_name = registered_event_name("foo") message_bus = omni.kit.app.get_app().get_message_bus_event_stream() sub = message_bus.create_subscription_to_push_by_type(reg_event_name, on_event) self.assertIsNotNone(sub) controller = og.Controller() keys = og.Controller.Keys controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("OnLoaded", "omni.graph.action.OnLoaded"), ("Send1", "omni.graph.action.SendCustomEvent"), ("Send2", "omni.graph.action.SendCustomEvent"), ], keys.CONNECT: [ ("OnLoaded.outputs:execOut", "Send1.inputs:execIn"), ("OnTick.outputs:tick", "Send2.inputs:execIn"), ], keys.SET_VALUES: [ ("OnTick.inputs:onlyPlayback", False), ("Send1.inputs:eventName", "foo"), ("Send2.inputs:eventName", "foo"), ("Send1.inputs:path", "Loaded"), ("Send2.inputs:path", "Tick"), ], }, ) # evaluate once so that graph is in steady state await controller.evaluate() # Verify Loaded came before OnTick self.assertListEqual(events, ["Loaded", "Tick"]) # ---------------------------------------------------------------------- async def test_active_latent(self): """exercise a latent node that executes downstream nodes while latent""" # +--------+ +----------+finished+-------------+ # | OnTick+-->| TickN +-------->FinishCounter| # +--------+ | | +-------------+ # | +-+ # +----------+ | +------------+ +------------+ +------------+ # +-----> TickCounter+----->TickCounter2+---->TickCounter3| # tick +------------+ +------------+ +------------+ controller = og.Controller() keys = og.Controller.Keys (graph, nodes, _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnTick", "omni.graph.action.OnTick"), ("TickN", "omni.graph.action.TickN"), ("FinishCounter", "omni.graph.action.Counter"), ("TickCounter", "omni.graph.action.Counter"), ("TickCounter2", "omni.graph.action.Counter"), ("TickCounter3", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("OnTick.outputs:tick", "TickN.inputs:execIn"), ("TickN.outputs:finished", "FinishCounter.inputs:execIn"), ("TickN.outputs:tick", "TickCounter.inputs:execIn"), ("TickCounter.outputs:execOut", "TickCounter2.inputs:execIn"), ("TickCounter2.outputs:execOut", "TickCounter3.inputs:execIn"), ], keys.SET_VALUES: [("TickN.inputs:duration", 3), ("OnTick.inputs:onlyPlayback", False)], }, ) (_, _, finish_counter, tick_counter, _, tick_counter_3) = nodes finish_counter_controller = og.Controller(og.Controller.attribute("outputs:count", finish_counter)) tick_counter_controller = og.Controller(og.Controller.attribute("outputs:count", tick_counter)) tick_counter_3_controller = og.Controller(og.Controller.attribute("outputs:count", tick_counter_3)) await controller.evaluate(graph) self.assertEqual(finish_counter_controller.get(), 0) await controller.evaluate(graph) self.assertEqual(tick_counter_controller.get(), 1) await controller.evaluate(graph) self.assertEqual(finish_counter_controller.get(), 0) self.assertEqual(tick_counter_controller.get(), 2) await controller.evaluate(graph) self.assertEqual(finish_counter_controller.get(), 0) self.assertEqual(tick_counter_3_controller.get(), 3) await controller.evaluate(graph) self.assertEqual(finish_counter_controller.get(), 1) self.assertEqual(tick_counter_3_controller.get(), 3) await controller.evaluate(graph) self.assertEqual(finish_counter_controller.get(), 1) self.assertEqual(tick_counter_3_controller.get(), 3) # ---------------------------------------------------------------------- async def test_latent_chain(self): """exercise a chain of latent nodes""" # +---------+ +-------+ tick +-------+ tick +-------+ # |OnImpulse+-->TickA +-------->TickB +-------->|LatentC| # +---------+ +-----+-+ +------++ +-------+-----+ # | finish | finish | # finish | +-------------+ | +-------------+ +-v----------+ # +->TickCounterA | +-->| TickCounterB| |TickCounterC| # +-------------+ +-------------+ +------------+ controller = og.Controller() keys = og.Controller.Keys (graph, nodes, _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("TickA", "omni.graph.action.TickN"), ("TickB", "omni.graph.action.TickN"), ("LatentC", "omni.graph.action.TickN"), ("TickCounterA", "omni.graph.action.Counter"), ("TickCounterB", "omni.graph.action.Counter"), ("TickCounterC", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("OnImpulse.outputs:execOut", "TickA.inputs:execIn"), ("TickA.outputs:tick", "TickB.inputs:execIn"), ("TickA.outputs:finished", "TickCounterA.inputs:execIn"), ("TickB.outputs:tick", "LatentC.inputs:execIn"), ("TickB.outputs:finished", "TickCounterB.inputs:execIn"), ("LatentC.outputs:finished", "TickCounterC.inputs:execIn"), ], keys.SET_VALUES: [ ("TickA.inputs:duration", 2), ("TickB.inputs:duration", 2), ("LatentC.inputs:duration", 2), ("OnImpulse.inputs:onlyPlayback", False), ("OnImpulse.state:enableImpulse", True), ], }, ) (_, _, _, _, tick_counter_a, tick_counter_b, tick_counter_c) = nodes for _ in range(16): await controller.evaluate(graph) self.assertEqual(og.Controller.get(controller.attribute("outputs:count", tick_counter_a)), 1) self.assertEqual(og.Controller.get(controller.attribute("outputs:count", tick_counter_b)), 2) self.assertEqual(og.Controller.get(controller.attribute("outputs:count", tick_counter_c)), 4) # ---------------------------------------------------------------------- async def test_latent_and_push(self): """exercise latent nodes in combination with stateful loop node""" # # +---------+ +-------+ tick +--------+ loopBody +-------+ +------------+ # |OnImpulse+-->|TickA +----------->ForLoop1++--------->|TickB +-+->|TickCounterB| # +---------+ +----+--+ +--------++ +-------+ | +------------+ # | finish | | # | | | # | +--------------+ +v----------------+ +-v------------+ # +----->|FinishCounterA| |FinishLoopCounter| |FinishCounterB| # +--------------+ +-----------------+ +--------------+ controller = og.Controller() keys = og.Controller.Keys (graph, nodes, _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("TickA", "omni.graph.action.TickN"), ("ForLoop1", "omni.graph.action.ForLoop"), ("TickB", "omni.graph.action.TickN"), ("FinishLoopCounter", "omni.graph.action.Counter"), ("FinishCounterA", "omni.graph.action.Counter"), ("FinishCounterB", "omni.graph.action.Counter"), ("TickCounterB", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("OnImpulse.outputs:execOut", "TickA.inputs:execIn"), ("TickA.outputs:tick", "ForLoop1.inputs:execIn"), ("TickA.outputs:finished", "FinishCounterA.inputs:execIn"), ("ForLoop1.outputs:loopBody", "TickB.inputs:execIn"), ("ForLoop1.outputs:finished", "FinishLoopCounter.inputs:execIn"), ("TickB.outputs:tick", "TickCounterB.inputs:execIn"), ("TickB.outputs:finished", "FinishCounterB.inputs:execIn"), ], keys.SET_VALUES: [ ("ForLoop1.inputs:start", 0), ("ForLoop1.inputs:stop", 3), ("TickA.inputs:duration", 2), ("TickB.inputs:duration", 2), ("OnImpulse.state:enableImpulse", True), ("OnImpulse.inputs:onlyPlayback", False), ], }, ) (_, _, _, _, finish_loop_counter, finish_counter_a, finish_counter_b, tick_counter_b) = nodes for _ in range(20): await controller.evaluate(graph) self.assertEqual(og.Controller.get(controller.attribute("outputs:count", finish_counter_a)), 1) self.assertEqual(og.Controller.get(controller.attribute("outputs:count", tick_counter_b)), 12) self.assertEqual(og.Controller.get(controller.attribute("outputs:count", finish_counter_b)), 6) self.assertEqual(og.Controller.get(controller.attribute("outputs:count", finish_loop_counter)), 2) # ---------------------------------------------------------------------- async def test_latent_fan_out(self): """Test latent nodes when part of parallel evaluation""" # +------------+ # +---->|TickCounterA| # | +------------+ # | # +--------++ +----------+ # +-> TickA +--->|FinishedA | # | +---------+ +----------+ # +---------+ +-----------+ | # |OnImpulse+-->|TickCounter+-+ # +---------+ +-----------+ | # | +---------+ +----------+ # +>| TickB +--->|FinishedB | # +--------++ +----------+ # | # | +------------+ # +---->|TickCounterB| # +------------+ controller = og.Controller() keys = og.Controller.Keys (graph, nodes, _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("TickA", "omni.graph.action.TickN"), ("TickB", "omni.graph.action.TickN"), ("TickCounter", "omni.graph.action.Counter"), ("TickCounterA", "omni.graph.action.Counter"), ("TickCounterB", "omni.graph.action.Counter"), ("FinishCounterA", "omni.graph.action.Counter"), ("FinishCounterB", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("OnImpulse.outputs:execOut", "TickCounter.inputs:execIn"), ("TickCounter.outputs:execOut", "TickA.inputs:execIn"), ("TickCounter.outputs:execOut", "TickB.inputs:execIn"), ("TickA.outputs:tick", "TickCounterA.inputs:execIn"), ("TickB.outputs:tick", "TickCounterB.inputs:execIn"), ("TickA.outputs:finished", "FinishCounterA.inputs:execIn"), ("TickB.outputs:finished", "FinishCounterB.inputs:execIn"), ], keys.SET_VALUES: [ ("TickA.inputs:duration", 2), ("TickB.inputs:duration", 2), ("OnImpulse.inputs:onlyPlayback", False), ("OnImpulse.state:enableImpulse", True), ], }, ) (_, _, _, tick_counter, tick_counter_a, tick_counter_b, finish_counter_a, finish_counter_b) = nodes def check_counts(c, a, b, f_a, f_b): for node, expected in ( (tick_counter, c), (tick_counter_a, a), (tick_counter_b, b), (finish_counter_a, f_a), (finish_counter_b, f_b), ): count = og.Controller.get(controller.attribute("outputs:count", node)) self.assertEqual(count, expected, node.get_prim_path()) await controller.evaluate(graph) check_counts(1, 0, 0, 0, 0) await controller.evaluate(graph) check_counts(1, 1, 1, 0, 0) await controller.evaluate(graph) check_counts(1, 2, 2, 0, 0) await controller.evaluate(graph) check_counts(1, 2, 2, 1, 1) # ---------------------------------------------------------------------- async def test_diamond_fan(self): """Test latent nodes in parallel fan-out which fan-in to a downstream node""" # +--------++ +----------+ # +--> TickA +--->|FinishedA |---+ # | +---------+ +----------+ | # +---------+ +-----------+ | | +------------+ # |OnImpulse+-->|TickCounter+-+ +-->|MergeCounter| # +---------+ +-----------+ | | +------------+ # | +---------+ +----------+ | # +-->| TickB +--->|FinishedB |--+ # +--------++ +----------+ # | +---------+ # +-->| TickC | # +--------++ controller = og.Controller() keys = og.Controller.Keys (graph, nodes, _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("TickCounter", "omni.graph.action.Counter"), ("TickA", "omni.graph.action.TickN"), ("TickB", "omni.graph.action.TickN"), ("TickC", "omni.graph.action.TickN"), ("FinishCounterA", "omni.graph.action.Counter"), ("FinishCounterB", "omni.graph.action.Counter"), ("MergeCounter", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("OnImpulse.outputs:execOut", "TickCounter.inputs:execIn"), ("TickCounter.outputs:execOut", "TickA.inputs:execIn"), ("TickCounter.outputs:execOut", "TickB.inputs:execIn"), ("TickCounter.outputs:execOut", "TickC.inputs:execIn"), ("TickA.outputs:finished", "FinishCounterA.inputs:execIn"), ("TickB.outputs:finished", "FinishCounterB.inputs:execIn"), ("FinishCounterA.outputs:execOut", "MergeCounter.inputs:execIn"), ("FinishCounterB.outputs:execOut", "MergeCounter.inputs:execIn"), ], keys.SET_VALUES: [ ("TickA.inputs:duration", 1), ("TickB.inputs:duration", 1), ("TickC.inputs:duration", 1), ("OnImpulse.inputs:onlyPlayback", False), ("OnImpulse.state:enableImpulse", True), ], }, ) (_, tick_counter, _, _, tick_c, finish_counter_a, finish_counter_b, merge_counter) = nodes def check_counts(tc, f_a, f_b, mc, tick_c_count): for node, expected in ( (tick_counter, tc), (finish_counter_a, f_a), (finish_counter_b, f_b), (merge_counter, mc), ): count = og.Controller.get(controller.attribute("outputs:count", node)) self.assertEqual(count, expected, node.get_prim_path()) self.assertEqual(tick_c.get_compute_count(), tick_c_count) self.assertEqual(tick_c.get_compute_count(), 0) # set up latent tickers await controller.evaluate(graph) check_counts(1, 0, 0, 0, 1) # latent ticks await controller.evaluate(graph) check_counts(1, 0, 0, 0, 2) # both branches complete await controller.evaluate(graph) check_counts(1, 1, 1, 2, 3) # no count changes + no additional computes of tickC await controller.evaluate(graph) check_counts(1, 1, 1, 2, 3) # ---------------------------------------------------------------------- async def test_diamond_latent_fan(self): """Test latent nodes in parallel fan-out which fan-in to a latent downstream node""" # +--------++ # +--> TickA +--+ # | +---------+ | # +---------+ | | +-------+ +-------+ # |OnImpulse+-->+ +-->|TickD +-+--->|CountF | # +---------+ | | +-------+ | +-------+ # | +--------+ | +--->+-------+ # +-->| TickB +--+ |TickE | # | +--------+ +--->+-------+ # | +--------+ | # +-->| TickC +----------------+ # +--------+ # Note that when TickA triggers TickD into latent state, then TickB hits TickD subsequently. This subsequent # evaluation is _transient_. Meaning that TickB will not block on a new copy of TickD. # This is because there is only one TickD so there can be only one state (latent or not). controller = og.Controller() keys = og.Controller.Keys (graph, nodes, _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("OnImpulse", "omni.graph.action.OnImpulseEvent"), ("TickA", "omni.graph.action.TickN"), ("TickB", "omni.graph.action.TickN"), ("TickC", "omni.graph.action.TickN"), ("TickD", "omni.graph.action.TickN"), ("TickE", "omni.graph.action.TickN"), ("CountF", "omni.graph.action.Counter"), ], keys.CONNECT: [ ("OnImpulse.outputs:execOut", "TickA.inputs:execIn"), ("OnImpulse.outputs:execOut", "TickB.inputs:execIn"), ("OnImpulse.outputs:execOut", "TickC.inputs:execIn"), ("TickA.outputs:finished", "TickD.inputs:execIn"), ("TickB.outputs:finished", "TickD.inputs:execIn"), ("TickC.outputs:finished", "TickE.inputs:execIn"), ("TickD.outputs:finished", "TickE.inputs:execIn"), ("TickD.outputs:finished", "CountF.inputs:execIn"), ], keys.SET_VALUES: [ ("TickA.inputs:duration", 1), ("TickB.inputs:duration", 1), ("TickC.inputs:duration", 2), ("TickD.inputs:duration", 1), ("TickE.inputs:duration", 1), ("OnImpulse.inputs:onlyPlayback", False), ("OnImpulse.state:enableImpulse", True), ], }, ) (_, tick_a, tick_b, tick_c, tick_d, tick_e, count_f) = nodes def check_counts(i, ta, tb, tc, td, te): # print(f"{[node.get_compute_count() for node, expected in ((tick_a, ta), (tick_b, tb), (tick_c, tc), (tick_d, td), (tick_e, te))]}") for node, expected in ((tick_a, ta), (tick_b, tb), (tick_c, tc), (tick_d, td), (tick_e, te)): self.assertEqual(node.get_compute_count(), expected, f"Check {i} for {node.get_prim_path()}") # A, B, C, D, E compute_counts = [ (1, 1, 1, 0, 0), # 0. fan out to trigger A, B, C into latent state (2, 2, 2, 0, 0), # 1. A, B, C tick (3, 3, 3, 2, 0), # 2. A, B end latent, D into latent via A or B, D ticks via A or B, C ticks (3, 3, 4, 3, 2), # 3. (3, 3, 4, 3, 3), # 4. (3, 3, 4, 3, 3), # 5. (3, 3, 4, 3, 3), # 6. ] for i, cc in enumerate(compute_counts): await controller.evaluate(graph) check_counts(i, *cc) # Verify that CountF has computed 1x due to the fan-in at TickD NOT acting like separate threads self.assertEqual(count_f.get_compute_count(), 1) async def test_om_63924(self): """Test OM-63924 bug is fixed""" # The problem here was that if there was fan in to a node which was # computed once and then totally unwound before the other history was # processed, there would never be a deferred activation and so the 2nd # compute would never happen. Instead we want to only unwind one history # at a time to ensure each one is fully evaluated. i = 2 class OnForEachEventPy: @staticmethod def compute(context: og.GraphContext, node: og.Node): nonlocal i go = node.get_attribute("inputs:go") go_val = og.Controller.get(go) if not go_val: return True if i > 0: og.Controller.set( node.get_attribute("outputs:execOut"), og.ExecutionAttributeState.ENABLED_AND_PUSH ) og.Controller.set(node.get_attribute("outputs:syncValue"), i) i -= 1 return True @staticmethod def get_node_type() -> str: return "omni.graph.test.OnForEachEventPy" @staticmethod def initialize_type(node_type: og.NodeType): node_type.add_input( "inputs:go", "bool", False, ) node_type.add_output("outputs:execOut", "execution", True) node_type.add_output("outputs:syncValue", "uint64", True) return True og.register_node_type(OnForEachEventPy, 1) class NoOpPy: @staticmethod def compute(context: og.GraphContext, node: og.Node): og.Controller.set(node.get_attribute("outputs:execOut"), og.ExecutionAttributeState.ENABLED) return True @staticmethod def get_node_type() -> str: return "omni.graph.test.NoOpPy" @staticmethod def initialize_type(node_type: og.NodeType): node_type.add_input( "inputs:execIn", "execution", True, ) node_type.add_output("outputs:execOut", "execution", True) return True og.register_node_type(NoOpPy, 1) controller = og.Controller() keys = og.Controller.Keys (graph, (for_each, _, _, _, _, no_op_2), _, _) = controller.edit( self.TEST_GRAPH_PATH, { keys.CREATE_NODES: [ ("PostProcessDispatcher", "omni.graph.test.OnForEachEventPy"), ("TSA1", "omni.graph.action.SyncGate"), ("TSA0", "omni.graph.action.SyncGate"), ("TestSyncAccum", "omni.graph.action.SyncGate"), ("TestPrimBbox", "omni.graph.test.NoOpPy"), ("NoOpPy2", "omni.graph.test.NoOpPy"), ], keys.CONNECT: [ ("PostProcessDispatcher.outputs:execOut", "TSA0.inputs:execIn"), ("PostProcessDispatcher.outputs:execOut", "TSA1.inputs:execIn"), ("TSA1.outputs:execOut", "TestSyncAccum.inputs:execIn"), ("TSA0.outputs:execOut", "TestPrimBbox.inputs:execIn"), ("TestPrimBbox.outputs:execOut", "TestSyncAccum.inputs:execIn"), ("TestSyncAccum.outputs:execOut", "NoOpPy2.inputs:execIn"), ("PostProcessDispatcher.outputs:syncValue", "TSA1.inputs:syncValue"), ("PostProcessDispatcher.outputs:syncValue", "TSA0.inputs:syncValue"), ("PostProcessDispatcher.outputs:syncValue", "TestSyncAccum.inputs:syncValue"), ], }, ) og.Controller.set(controller.attribute("inputs:go", for_each), True) await controller.evaluate(graph) # Verify the final sync gate triggered due to being computed 2x exec_out = og.Controller.get(controller.attribute("outputs:execOut", no_op_2)) self.assertEqual(exec_out, og.ExecutionAttributeState.ENABLED)
44,048
Python
46.364516
145
0.4706
omniverse-code/kit/exts/omni.graph.action/docs/ui_nodes.md
(ogn_ui_nodes)= # UI Nodes You may have seen the `omni.ui` extension that gives you the ability to create user interface elements through Python scripting. OmniGraph provides some nodes that can be used to do the same thing through an action graph. These nodes provide an interface to the equivalent `omni.ui` script elements. The attributes of the nodes match the parameters you would pass to the script. | Node | omni.ui Equivalent | | --------------------------------------------------------------------------------------------------- | -------------------------------------------------------------- | | {ref}`Button<GENERATED - Documentation _ognomni.graph.ui.Button>` | {py:class}`omni.ui.Button` | | {ref}`ComboBox<GENERATED - Documentation _ognomni.graph.ui.ComboBox>` | {py:class}`omni.ui.ComboBox` | | {ref}`OnWidgetClicked<GENERATED - Documentation _ognomni.graph.ui.OnWidgetClicked>` | {py:class}`omni.ui.Widget.call_mouse_pressed_fn` | | {ref}`OnWidgetValueChanged<GENERATED - Documentation _ognomni.graph.ui.OnWidgetValueChanged>` | {py:class}`omni.ui.Widget.add_value_changed_fn` | | {ref}`Slider<GENERATED - Documentation _ognomni.graph.ui.Slider>` | {py:class}`omni.ui.IntSlider` {py:class}`omni.ui.FloatSlider` | | {ref}`VStack<GENERATED - Documentation _ognomni.graph.ui.VStack>` | {py:class}`omni.ui.VStack` | ## How To Use Them The UI nodes are meant to be triggered to exist temporarily, meaning you want to ensure that they are part of a graph that creates them only once and then destroys them once their utility has ended.
2,019
Markdown
86.826083
221
0.520059
omniverse-code/kit/exts/omni.graph.action/docs/CHANGELOG.md
# Changelog All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] ## [1.31.1] - 2023-02-10 ### Fixed - Add tests for AG fan-out with shared dependencies ## [1.31.0] - 2022-09-01 ### Added - SwitchToken, OnLoaded ## [1.30.2] - 2022-08-30 ### Fixed - Linting errors ## [1.30.1] - 2022-08-09 ### Fixed - Applied formatting to all of the Python files ## [1.30.0] - 2022-07-29 ### Changed - Add prim input to OnObjectChanged node ## [1.29.0] - 2022-07-29 ### Added - Support for relative image urls in UI widget nodes ## [1.28.1] - 2022-07-28 ### Fixed - Spurious error messages about 'Node compute request is ignored because XXX is not request-driven' ## [1.28.0] - 2022-07-26 ### Added - Placer node. - Added ReadWidgetProperty node ## [1.27.0] - 2022-07-26 ### Changed - Removed internal Placers from the widget nodes. ## [1.26.1] - 2022-07-25 ### Fixed - Various UI nodes were rejecting valid exec inputs like ENABLED_AND_PUSH ## [1.26.0] - 2022-07-22 ### Added - 'style' input attributes for Button, Spacer and Stack nodes. ### Fixed - WriteWidgetStyle was failing on styles containing hex values (e.g. for colors) ## [1.25.0] - 2022-07-20 ### Added - WriteWidgetStyle node ## [1.24.1] - 2022-07-21 ### Changed - Undo revert ## [1.24.0] - 2022-07-18 ### Added - Spacer node ### Changed - VStack node: - Removed all execution inputs except for create. - Added support for OgnWriteWidgetProperty. - inputs:parentWidgetPath is no longer optional. ## [1.23.0] - 2022-07-18 ### Changed - Reverted changes in 1.21.1 ## [1.22.0] - 2022-07-15 ### Added - WriteWidgetProperty node ### Changed - Removed all of Button node's execution inputs except for Create - Removed Button node's 'iconSize' input. - Modified Button node to work with WriteWidgetProperty ## [1.21.1] - 2022-07-15 ### Changed - Added test node TickN, modified tests ## [1.21.0] - 2022-07-14 ### Changed - Added +/- icons to Multigate and Sequence ## [1.20.0] - 2022-07-07 ### Added - Test for public API consistency ## [1.19.0] - 2022-07-04 ### Changed - OgnButton requires a parentWidgetPath. It no longer defaults to the current viewport. - Each OgnButton instance can create multiples buttons. They no longer destroy the previous ones. - widgetIdentifiers are now unique within GraphContext ## [1.18.1] - 2022-06-20 ### Changed - Optimized MultiSequence ## [1.18.0] - 2022-05-30 ### Added - OnClosing ## [1.17.1] - 2022-05-23 ### Changed - Changed VStack ui name to Stack ## [1.17.0] - 2022-05-24 ### Changed - Converted ForEach node from Python to C++ - Removed OnWidgetDoubleClicked ## [1.16.1] - 2022-05-23 ### Changed - Converted Counter node from Python to C++ ## [1.16.0] - 2022-05-21 ### Added - Removed OnGraphInitialize, added Once to replace ## [1.15.1] - 2022-05-20 ### Changed - Added direction input to VStack node to allow objects to stack in width, height & depth directions. - Button node uses styles to select icons rather than mouse functions. ## [1.15.0] - 2022-05-19 ### Added - OnGraphInitialize - triggers when the graph is created ### Changed - OnStageEvent - removed non-functional events ## [1.14.1] - 2022-05-19 ### Fixed - Fixed OgnOnWidgetValueChanged output type resolution ## [1.14.0] - 2022-05-17 ### Changed - Added '(BETA)' to the ui_names of the new UI nodes. ## [1.13.0] - 2022-05-16 ### Added - Added Sequence node with dynamic outputs named OgnMultisequence ## [1.12.0] - 2022-05-11 ### Added - Node definitions for UI creation and manipulation - Documentation on how to use the new UI nodes - Dependencies on extensions omni.ui_query and omni.kit.window.filepicker(optional) ## [1.11.3] - 2022-04-12 ### Fixed - OnCustomEvent when onlyPlayback=true - Remove state attrib from PrintText ## [1.11.2] - 2022-04-08 ### Added - Added absoluteSimTime output attribute to the OnTick node ## [1.11.1] - 2022-03-16 ### Fixed - OnStageEvent Animation Stop event when only-on-playback is true ## [1.11.0] - 2022-03-10 ### Added - Removed _outputs::shiftOut_, _outputs::ctrlOut_, _outputs::altOut_ from _OnKeyboardInput_ node. - Added _inputs::shiftIn_, _inputs::ctrlIn_, _inputs::altIn_ from _OnKeyboardInput_ node. - Added support for key modifiers to _OnKeyboardInput_ node. ## [1.10.1] - 2022-03-09 ### Changed - Made all input attributes of all event source nodes literalOnly ## [1.10.0] - 2022-02-24 ### Added - added SyncGate node ## [1.9.1] - 2022-02-14 ### Fixed - add additional extension enabled check for omni.graph.ui not enabled error ## [1.9.0] - 2022-02-04 ### Added - added SetPrimRelationship node ## [1.8.0] - 2022-02-04 ### Modified - Several event nodes now have _inputs:onlyPlayback_ attributes to control when they are active. The default is enabled, which means these nodes will only operate what playback is active. ### Fixed - Category for Counter ## [1.7.0] - 2022-01-27 ### Added - Added SetPrimActive node ## [1.6.0] - 2022-01-27 ### Added - Added OnMouseInput node ### Modified - Changed OnGamepadInput to use SubscriptionToInputEvents instead - Changed OnKeyboardInput to use SubscriptionToInputEvents instead ## [1.5.0] - 2022-01-25 ### Added - Added OnGamepadInput node ## [1.4.5] - 2022-01-24 ### Fixed - categories for several nodes ## [1.4.4] - 2022-01-14 ### Added - Added car customizer tutorial ## [1.4.2] - 2022-01-05 ### Modified - Categories added to all nodes ## [1.4.1] - 2021-12-20 ### Modified - _GetLookAtRotation_ moved to _omni.graph.nodes_ ## [1.4.0] - 2021-12-10 ### Modified - _OnStageEvent_ handles new stage events ## [1.3.0] - 2021-11-22 ### Modified - _OnKeyboardInput_ to use allowedTokens for input key - _OnStageEvent_ bugfix to avoid spurious error messages on shutdown ## [1.2.0] - 2021-11-10 ### Modified - _OnKeyboardInput_, _OnCustomEvent_ to use _INode::requestCompute()_ ## [1.1.0] - 2021-11-04 ### Modified - _OnImpulseEvent_ to use _INode::requestCompute()_ ## [1.0.0] - 2021-05-10 ### Initial Version
6,104
Markdown
25.201717
187
0.693971
omniverse-code/kit/exts/omni.graph.action/docs/README.md
# OmniGraph Action Graphs ## Introduction to Action Graphs Provides visual-programming graphs to help designers bring their omniverse creations to life. Action Graphs are triggered by events and can execute nodes which modify the stage. ## The Action Graph Extension Contains nodes which work only with Action Graphs. Compute Nodes from other extensions can be used with Action Graphs; for example omni.graph.nodes.
422
Markdown
34.249997
119
0.808057
omniverse-code/kit/exts/omni.graph.action/docs/index.rst
.. _ogn_omni_graph_action: OmniGraph Action Graph ###################### .. tabularcolumns:: |L|R| .. csv-table:: :width: 100% **Extension**: omni.graph.action,**Documentation Generated**: |today| .. toctree:: :maxdepth: 1 CHANGELOG This extension is a collection of functionality required for OmniGraph Action Graphs. .. toctree:: :maxdepth: 2 :caption: Contents Overview of Action Graphs<Overview> Hands-on Introduction to Action Graphs<https://docs.omniverse.nvidia.com/prod_extensions/prod_extensions/ext_omnigraph/quickstart.html> Action Graph Car Customizer Tutorial<https://docs.omniverse.nvidia.com/prod_extensions/prod_extensions/ext_omnigraph/car_customizer.html> Building UI With Action Graph<ui_nodes> For more comprehensive examples targeted at explaining the use of OmniGraph features in detail see :ref:`ogn_user_guide` .. note:: Action Graphs are in an early development state
937
reStructuredText
25.799999
140
0.731057
omniverse-code/kit/exts/omni.graph.action/docs/Overview.md
(ogn_omni_graph_action_overview)= ```{csv-table} **Extension**: omni.graph.action,**Documentation Generated**: {sub-ref}`today` ``` This extension is a collection of functionality required for OmniGraph Action Graphs. ```{note} Action Graphs are in an early development state ``` # Action Graph Overview For a hands-on introduction to OmniGraph Action Graphs see [Action Graph Quickstart](https://docs.omniverse.nvidia.com/prod_extensions/prod_extensions/ext_omnigraph/quickstart.html) For more comprehensive and thorough documentation on various OmniGraph features see {ref}`ogn_user_guide` Action Graphs are comprised of any number of separate chains of nodes, like deformer graphs. However there are important differences which make Action graphs more suited to particular applications. ## Event Sources Action graphs are *event driven*, which means that each chain of nodes must start with an *Event Source* node. Each event source node can be thought of as an entry point of the graph. *Event Source* nodes are named with an *On* prefix, never have an *execution* input attribute, and always have at least one output *execution* attribute. | Event Source Nodes | | ------------------------------------------------------------------------------------------ | | {ref}`On Keyboard Input <GENERATED - Documentation _ognomni.graph.action.OnKeyboardInput>` | | {ref}`On Tick <GENERATED - Documentation _ognomni.graph.action.OnTick>` | | {ref}`On Playback Tick <GENERATED - Documentation _ognomni.graph.action.OnPlaybackTick>` | | {ref}`On Impulse Event <GENERATED - Documentation _ognomni.graph.action.OnImpulseEvent>` | | {ref}`On Object Change <GENERATED - Documentation _ognomni.graph.action.OnObjectChange>` | | {ref}`On Custom Event <GENERATED - Documentation _ognomni.graph.action.OnCustomEvent>` | ## Execution Attributes Action graphs make use of *execution*-type attributes. The *execution* evaluator works by following *execution* connections downstream and computing nodes it encounters until there are no more downstream connections to follow. The entire chain is executed to completion. When there is no downstream node the execution terminates and the next node is popped off the *execution stack* Note that if there is more than one downstream connection from an *execution* attribute, each path will be followed in an undetermined order. Multiple downstream chains can be executed in a fixed order either by chaining the end of one to the start of the other, or by using the {ref}`Sequence <GENERATED - Documentation _ognomni.graph.action.Sequence>` node. The value of an *execution* attribute tells the evaluator what the next step should be in the chain. It can be one of: | Value | Description | ---------------- | ------------------------------------------------------------------------------------------- | | DISABLED | Do not continue from this attribute. | | ENABLED | Continue downstream from this attribute. | | ENABLED_AND_PUSH | Save the current node on the *execution stack* and continue downstream from this attribute. | | LATENT_PUSH | Save the current node as it performs some asynchronous operation | | LATENT_FINISH | Finish the asynchronous operation and continue downstream from this attribute. | # Flow Control Many Action graphs will need to do different things depending on some state. In a python script you would use an *if* or *while* loop to accomplish this. Similarly in Action graph there are nodes which provide this branching functionality. Flow control nodes have more than one *execution* output attribute, which is used to branch the evaluation flow. | Flow Control Nodes | | --------------------------------------------------------------------------- | | {ref}`Branch <GENERATED - Documentation _ognomni.graph.action.Branch>` | | {ref}`ForEach <GENERATED - Documentation _ognomni.graph.action.ForEach>` | | {ref}`For Loop <GENERATED - Documentation _ognomni.graph.action.ForLoop>` | | {ref}`Flip Flop <GENERATED - Documentation _ognomni.graph.action.FlipFlop>` | | {ref}`Gate <GENERATED - Documentation _ognomni.graph.action.Gate>` | | {ref}`Sequence <GENERATED - Documentation _ognomni.graph.action.Sequence>` | | {ref}`Delay <GENERATED - Documentation _ognomni.graph.action.Delay>` |
4,622
Markdown
66.985293
359
0.665729
omniverse-code/kit/exts/omni.kit.renderer.cuda_interop/omni/kit/renderer/cuda_interop_test/__init__.py
from ._renderer_cuda_interop_test import * # Cached interface instance pointer def get_renderer_cuda_interop_test_interface() -> IRendererCudaInteropTest: if not hasattr(get_renderer_cuda_interop_test_interface, "renderer_cuda_interop_test"): get_renderer_cuda_interop_test_interface.renderer_cuda_interop_test = acquire_renderer_cuda_interop_test_interface() return get_renderer_cuda_interop_test_interface.renderer_cuda_interop_test
453
Python
49.444439
124
0.785872
omniverse-code/kit/exts/omni.kit.renderer.cuda_interop/omni/kit/renderer/cuda_interop/__init__.py
from ._renderer_cuda_interop import * # Cached interface instance pointer def get_renderer_cuda_interop_interface() -> IRendererCudaInterop: """Returns cached :class:`omni.kit.renderer.IRendererCudaInterop` interface""" if not hasattr(get_renderer_cuda_interop_interface, "renderer_cuda_interop"): get_renderer_cuda_interop_interface.renderer = acquire_renderer_cuda_interop_interface() return get_renderer_cuda_interop_interface.renderer_cuda_interop
475
Python
42.272723
96
0.772632
omniverse-code/kit/exts/omni.kit.renderer.cuda_interop/omni/kit/renderer/cuda_interop/_renderer_cuda_interop.pyi
""" This module contains bindings to C++ omni::kit::renderer::IRendererCudaInterop interface, core C++ part of Omniverse Kit. >>> import omni.kit.renderer.cuda_interop >>> e = omni.kit.renderer.cuda_interop.get_renderer_cuda_interop_interface() """ from __future__ import annotations import omni.kit.renderer.cuda_interop._renderer_cuda_interop import typing __all__ = [ "IRendererCudaInterop", "acquire_renderer_cuda_interop_interface", "release_renderer_cuda_interop_interface" ] class IRendererCudaInterop(): pass def acquire_renderer_cuda_interop_interface(plugin_name: str = None, library_path: str = None) -> IRendererCudaInterop: pass def release_renderer_cuda_interop_interface(arg0: IRendererCudaInterop) -> None: pass
788
unknown
31.874999
129
0.717005
omniverse-code/kit/exts/omni.kit.renderer.cuda_interop/omni/kit/renderer/cuda_interop/tests/__init__.py
from .test_renderer_cuda_interop import *
42
Python
20.49999
41
0.785714
omniverse-code/kit/exts/omni.kit.renderer.cuda_interop/omni/kit/renderer/cuda_interop/tests/test_renderer_cuda_interop.py
import inspect import pathlib import carb import carb.settings import carb.tokens import omni.kit.app import omni.kit.test import omni.kit.renderer.cuda_interop_test class RendererCudaInteropTest(omni.kit.test.AsyncTestCase): async def setUp(self): self._settings = carb.settings.acquire_settings_interface() self._app_window_factory = omni.appwindow.acquire_app_window_factory_interface() self._renderer = omni.kit.renderer.bind.acquire_renderer_interface() self._renderer_cuda_interop_test = omni.kit.renderer.cuda_interop_test.acquire_renderer_cuda_interop_test_interface() self._renderer.startup() self._renderer_cuda_interop_test.startup() def __test_name(self) -> str: return f"{self.__module__}.{self.__class__.__name__}.{inspect.stack()[2][3]}" async def tearDown(self): self._renderer_cuda_interop_test.shutdown() self._renderer.shutdown() self._renderer_cuda_interop_test = None self._renderer = None self._app_window_factory = None self._settings = None async def test_1_render_cuda_interop_test(self): app_window = self._app_window_factory.create_window_from_settings() app_window.startup_with_desc( title="Renderer test OS window", width=16, height=16, x=omni.appwindow.POSITION_CENTERED, y=omni.appwindow.POSITION_CENTERED, decorations=True, resize=True, always_on_top=False, scale_to_monitor=False, dpi_scale_override=-1.0 ) self._renderer.attach_app_window(app_window) self._app_window_factory.set_default_window(app_window) TEST_COLOR = (1, 2, 3, 255) test_color_unit = tuple(c / 255.0 for c in TEST_COLOR) self._renderer.set_clear_color(app_window, test_color_unit) self._renderer_cuda_interop_test.startup_resources_for_app_window(app_window) self._renderer_cuda_interop_test.setup_simple_comparison_for_app_window(app_window, TEST_COLOR[0], TEST_COLOR[1], TEST_COLOR[2], TEST_COLOR[3]) test_name = self.__test_name() for _ in range(3): await omni.kit.app.get_app().next_update_async() self._renderer_cuda_interop_test.shutdown_resources_for_app_window(app_window) self._app_window_factory.set_default_window(None) self._renderer.detach_app_window(app_window) app_window.shutdown() app_window = None
2,522
Python
35.042857
151
0.647898
omniverse-code/kit/exts/omni.kit.window.audioplayer/config/extension.toml
[package] title = "Kit Audio Player Window" category = "Audio" version = "1.0.1" description = "A simple audio player window" detailedDescription = """This adds a window for playing audio assets. This also adds an option to the content browser to play audio assets in this audio player. """ preview_image = "data/preview.png" authors = ["NVIDIA"] keywords = ["audio", "playback"] [dependencies] "omni.audioplayer" = {} "omni.ui" = {} "omni.kit.window.content_browser" = { optional=true } "omni.kit.window.filepicker" = {} "omni.kit.menu.utils" = {} [[python.module]] name = "omni.kit.window.audioplayer" [[test]] unreliable = true args = [ # Use the null device backend so we don't scare devs by playing audio. "--/audio/deviceBackend=null", # Needed for UI testing "--/app/menu/legacy_mode=false", ] dependencies = [ "omni.kit.mainwindow", "omni.kit.ui_test", ] stdoutFailPatterns.exclude = [ "*" # I don't want these but OmniUiTest forces me to use them ]
993
TOML
22.116279
76
0.682779
omniverse-code/kit/exts/omni.kit.window.audioplayer/omni/kit/window/audioplayer/__init__.py
from .audio_player_window import *
35
Python
16.999992
34
0.771429
omniverse-code/kit/exts/omni.kit.window.audioplayer/omni/kit/window/audioplayer/audio_player_window.py
# Copyright (c) 2020, 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. # import carb.settings import carb.dictionary import omni.audioplayer import omni.kit.ui import omni.ui import threading import time import re import asyncio import enum from typing import Callable from omni.kit.window.filepicker import FilePickerDialog PERSISTENT_SETTINGS_PREFIX = "/persistent" class EndReason(enum.Enum): # sound finished naturally FINISHED = 0, # sound was explicitly stopped STOPPED = 1, # seeked to a new location in the sound (causes an end callback) SEEK = 2, # the previous sound ended because another one is being played QUEUED_NEW_SOUND = 3, class AudioPlayerWindowExtension(omni.ext.IExt): """Audio Player Window Extension""" class FieldModel(omni.ui.AbstractValueModel): def __init__(self, end_edit_callback): super(AudioPlayerWindowExtension.FieldModel, self).__init__() self._end_edit_callback = end_edit_callback self._value = "" def get_value_as_string(self): return self._value def begin_edit(self): pass def set_value(self, value): self._value = value self._value_changed() def end_edit(self): self._end_edit_callback(self._value) class SliderModel(omni.ui.AbstractValueModel): def __init__(self, update_callback, end_edit_callback): super(AudioPlayerWindowExtension.SliderModel, self).__init__() self._update_callback = update_callback self._end_edit_callback = end_edit_callback self._value = 0 def get_value_as_int(self): # pragma: no cover return int(self._value) def get_value_as_float(self): # pragma: no cover return float(self._value) def begin_edit(self): # pragma: no cover pass def set_value(self, value): # pragma: no cover self._value = value self._value_changed() self._update_callback(self._value) def end_edit(self): # pragma: no cover self._end_edit_callback(self._value) def _on_file_pick(self, dialog: FilePickerDialog, filename: str, dirname: str): # pragma: no cover path = "" if dirname: path = f"{dirname}/{filename}" elif filename: path = filename dialog.hide() self._file_field.model.set_value(path) # this has to be called manually because set_value doesn't do it self._file_field_end_edit(path) def _choose_file_clicked(self): # pragma: no cover dialog = FilePickerDialog( "Select File", apply_button_label="Select", click_apply_handler=lambda filename, dirname: self._on_file_pick(dialog, filename, dirname), ) dialog.show() def _set_pause_button(self): # pragma: no cover self._play_button.set_style({"image_url": "resources/glyphs/timeline_pause.svg"}) def _set_play_button(self): # pragma: no cover self._play_button.set_style({"image_url": "resources/glyphs/timeline_play.svg"}) def _timeline_str(self, time): # pragma: no cover sec = ":{:02.0f}".format(time % 60) if time > 60.0 * 60.0: return "{:1.0f}".format(time // (60 * 60)) + ":{:02.0f}".format((time // 60) % 60) + sec else: return "{:1.0f}".format(time // 60) + sec def _timeline_ticker(self): # pragma: no cover if not self._playing: return time = self._player.get_play_cursor() self._timeline_cursor_label.text = self._timeline_str(time) self._timeline_slider.model.set_value(time * self._timeline_slider_scale) # if the window was closed, stop the player if not self._window.visible: self._end_reason = EndReason.STOPPED self._player.stop_sound() self._ticker = threading.Timer(0.25, self._timeline_ticker).start() def _loading_ticker(self): labels = {0: "Loading", 1: "Loading.", 2: "Loading..", 3: "Loading..."} if not self._loading: self._loading_label.text = "" return self._loading_label.text = labels[self._loading_counter % 4] self._loading_counter += 1 self._loading_timer = threading.Timer(0.25, self._loading_ticker).start() def _play_sound(self, time): self._loading = True self._player.play_sound( self._file_field.model.get_value_as_string(), time ) def _close_error_window(self): # pragma: no cover self._error_window.visible = False def _set_play_cursor(self, time): # pragma: no cover self._end_reason = EndReason.SEEK self._player.set_play_cursor(time) def _file_loaded(self, success): # pragma: no cover self._loading = False if not success: self._playing = False self._set_play_button() error_text = "Loading failed" file_name = self._file_field.model.get_value_as_string() if re.search("^.*.(m4a|aac)$", file_name): error_text = ( f"Failed to load file '{file_name}' codec not supported - only Vorbis, FLAC and WAVE are supported" ) else: error_text = f"Failed to load file '{file_name}' codec not supported (only Vorbis, FLAC, MP3 and WAVE are supported), file does not exist or the file is corrupted" self._error_window = omni.ui.Window( "Audio Player Error", width=400, height=0, flags=omni.ui.WINDOW_FLAGS_NO_DOCKING ) with self._error_window.frame: with omni.ui.VStack(): with omni.ui.HStack(): omni.ui.Spacer() self._error_window_label = omni.ui.Label( error_text, word_wrap=True, width=380, alignment=omni.ui.Alignment.CENTER ) omni.ui.Spacer() with omni.ui.HStack(): omni.ui.Spacer() self._error_window_ok_button = omni.ui.Button( width=64, height=32, clicked_fn=self._close_error_window, text="ok" ) omni.ui.Spacer() self._waveform_image_provider.set_bytes_data([0, 0, 0, 0], [1, 1]) return if self._new_file: width = 2048 height = 64 raw_image = self._player.draw_waveform(width, height, [0.89, 0.54, 0.14, 1.0], [0.0, 0.0, 0.0, 0.0]) self._waveform_image_provider.set_bytes_data(raw_image, [width, height]) self._new_file = False self._timeline_end_label.text = self._timeline_str(self._player.get_sound_length()) self._sound_length = self._player.get_sound_length() self._timeline_slider_scale = 1.0 / self._sound_length # set the timeline ticker going self._timeline_ticker() # set this back to default self._end_reason = EndReason.FINISHED def _play_finished(self): # pragma: no cover if self._end_reason != EndReason.SEEK and self._end_reason != EndReason.QUEUED_NEW_SOUND: self._playing = False # set the slider to finished self._timeline_cursor_label.text = self._timeline_str(0) self._timeline_slider.model.set_value(0.0) if self._end_reason == EndReason.FINISHED or self._end_reason == EndReason.STOPPED: self._set_play_button() if self._end_reason == EndReason.FINISHED and self._settings.get_as_bool(PERSISTENT_SETTINGS_PREFIX + "/audio/context/closeAudioPlayerOnStop"): self._window.visible = False def _play_clicked(self): # pragma: no cover if self._loading: return if self._playing: if self._paused: self._player.unpause_sound() self._set_pause_button() self._paused = False else: self._player.pause_sound() self._set_play_button() self._paused = True return self._playing = True self._paused = False self._load_result_label.text = "" self._loading_ticker() self._set_pause_button() self._play_sound(self._timeline_slider.model.get_value_as_float() * self._sound_length) def _file_field_end_edit(self, value): self._loading = True self._new_file = True self._load_result_label.text = "" self._loading_ticker() self._stop_clicked() self._player.load_sound(self._file_field.model.get_value_as_string()) def _stop_clicked(self): # pragma: no cover if self._loading: return self._end_reason = EndReason.STOPPED self._player.stop_sound() self._playing = False self._paused = False def _slider_end_edit(self, value): # pragma: no cover if self._loading: return if not self._playing: return self._set_play_cursor(value * self._sound_length) def _slider_changed(self, value): # pragma: no cover if not self._playing and not self._loading: self._timeline_cursor_label.text = self._timeline_str(value * self._sound_length) def open_window(self): """ Make the window become visible Args: No arguments Returns: No return value """ self._window.visible = True def open_window_and_play(self, path): # pragma: no cover """ Make the window become visible then begin playing a file Args: path: The file to begin playing Returns: No return value """ self._playing = True self._loading = True; self._paused = False self._new_file = True self._window.visible = True self._load_result_label.text = "" self._loading_ticker() self._set_pause_button() self._end_reason = EndReason.QUEUED_NEW_SOUND self._file_field.model.set_value(path) self._play_sound(0.0) def _menu_callback(self, a, b): self._window.visible = not self._window.visible def _on_menu_click(self, menu, value): # pragma: no cover if self._content_window is None: return protocol = self._content_window.get_selected_icon_protocol() path = self._content_window.get_selected_icon_path() if not path.startswith(protocol): path = protocol + path self.open_window_and_play(path) def _on_menu_check(self, url): return not not re.search("^.*\\.(wav|wave|ogg|oga|flac|fla|mp3|m4a|spx|opus|adpcm)$", url) def _on_browser_click(self, menu, value): # pragma: no cover if self._content_browser is None: return # protocol = self._content_browser.get_selected_icon_protocol() # path = self._content_browser.get_selected_icon_path() # if not path.startswith(protocol): # path = protocol + path self.open_window_and_play(value) def _on_content_browser_load(self): # pragma: no cover import omni.kit.window.content_browser self._content_browser = omni.kit.window.content_browser.get_content_window() if self._content_browser is not None: self._content_browser_entry = self._content_browser.add_context_menu( "Play Audio", "audio_play.svg", self._on_browser_click, self._on_menu_check ) def _on_content_browser_unload(self): # pragma: no cover if self._content_browser is not None: self._content_browser.delete_context_menu("Play Audio") self._content_browser_entry = None self._content_browser = None def _on_player_event(self, event): if event.type == int(omni.audioplayer.CallbackType.LOADED): success = event.payload["success"] self._file_loaded(success) elif event.type == int(omni.audioplayer.CallbackType.ENDED): self._play_finished() else: print("unrecognized type " + str(event.type)) def on_startup(self): self._content_browser = None self._hooks = [] manager = omni.kit.app.get_app().get_extension_manager() # current content window self._hooks.append( manager.subscribe_to_extension_enable( on_enable_fn=lambda _: self._on_content_browser_load(), on_disable_fn=lambda _: self._on_content_browser_unload(), ext_name="omni.kit.window.content_browser", hook_name="omni.kit.window.audioplayer omni.kit.window.content_browser listener", ) ) self._loading_counter = 0 self._ticker = None self._loading = False self._end_reason = EndReason.FINISHED self._new_file = True self._sound_length = 0 self._timeline_slider_scale = 0 self._file = "" self._playing = False self._paused = False self._player = omni.audioplayer.create_audio_player() self._sub = self._player.get_event_stream().create_subscription_to_pop(self._on_player_event) self._window = omni.ui.Window("Audio Player", width=600, height=200) self._settings = carb.settings.get_settings() self._settings.set_default_bool(PERSISTENT_SETTINGS_PREFIX + "/audio/context/closeAudioPlayerOnStop", False) with self._window.frame: with omni.ui.VStack(height=0, spacing=8): # file dialogue with omni.ui.HStack(): omni.ui.Button( width=32, height=32, clicked_fn=self._choose_file_clicked, style={"image_url": "resources/glyphs/folder.svg"}, ) self._file_field_model = AudioPlayerWindowExtension.FieldModel(self._file_field_end_edit) self._file_field = omni.ui.StringField(self._file_field_model, height=32) # timeline slider with omni.ui.HStack(height=64): self._timeline_cursor_label = omni.ui.Label("0:00", width=25) omni.ui.Label(" / ", width=10) self._timeline_end_label = omni.ui.Label("0:00", width=25) self._timeline_slider_model = AudioPlayerWindowExtension.SliderModel( self._slider_changed, self._slider_end_edit ) with omni.ui.ZStack(): self._waveform_image_provider = omni.ui.ByteImageProvider() self._waveform_image = omni.ui.ImageWithProvider( self._waveform_image_provider, width=omni.ui.Percent(100), height=omni.ui.Percent(100), fill_policy=omni.ui.IwpFillPolicy.IWP_STRETCH, ) with omni.ui.VStack(): omni.ui.Spacer() self._timeline_slider = omni.ui.FloatSlider( self._timeline_slider_model, height=0, style={ "color": 0x00FFFFFF, "background_color": 0x00000000, "draw_mode": omni.ui.SliderDrawMode.HANDLE, "font_size": 22, }, ) omni.ui.Spacer() # buttons with omni.ui.HStack(): with omni.ui.ZStack(): omni.ui.Spacer() self._load_result_label = omni.ui.Label( "", alignment=omni.ui.Alignment.CENTER, style={"color": 0xFF0000FF} ) self._play_button = omni.ui.Button( width=32, height=32, clicked_fn=self._play_clicked, style={"image_url": "resources/glyphs/timeline_play.svg"}, ) omni.ui.Button( width=32, height=32, clicked_fn=self._stop_clicked, style={"image_url": "resources/glyphs/timeline_stop.svg"}, ) with omni.ui.ZStack(): omni.ui.Spacer() self._loading_label = omni.ui.Label("", alignment=omni.ui.Alignment.CENTER) with omni.ui.HStack(alignment=omni.ui.Alignment.LEFT, width=100): omni.ui.Label("Close on Stop", alignment=omni.ui.Alignment.LEFT) omni.ui.Spacer() self._auto_close_on_stop = omni.ui.CheckBox(alignment=omni.ui.Alignment.LEFT) omni.ui.Spacer() self._auto_close_on_stop.model.set_value( self._settings.get_as_bool(PERSISTENT_SETTINGS_PREFIX + "/audio/context/closeAudioPlayerOnStop") ) self._dict = carb.dictionary.get_dictionary() self._auto_close_on_stop.model.add_value_changed_fn( lambda a, b=self._settings: b.set_bool( PERSISTENT_SETTINGS_PREFIX + "/audio/context/closeAudioPlayerOnStop", a.get_value_as_bool() ) ) def on_change(item, event_type): # pragma: no cover self._auto_close_on_stop.model.set_value(self._dict.get(item)) self._subscription = self._settings.subscribe_to_node_change_events( PERSISTENT_SETTINGS_PREFIX + "/audio/context/closeAudioPlayerOnStop", on_change ) # add a callback to open the window # FIXME: disabled until the bugs are worked out self._menuEntry = omni.kit.ui.get_editor_menu().add_item("Window/Audio Player", self._menu_callback) self._window.visible = False def on_shutdown(self): # pragma: no cover self._end_reason = EndReason.STOPPED self._player.stop_sound() if self._ticker != None: self._ticker.cancel() self._settings.unsubscribe_to_change_events(self._subscription) self._subscription = None # run the unload function to avoid breaking the extension when it reloads self._on_content_browser_unload() # remove the subscription before the player to avoid events with a dead player self._sub = None self._player = None self._window = None self._menuEntry = None self._content_window_entry = None
19,473
Python
37.259332
179
0.559185
omniverse-code/kit/exts/omni.kit.window.audioplayer/omni/kit/window/audioplayer/tests/test_audio_player.py
## Copyright (c) 2022, 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. ## import omni.kit.app import omni.kit.test import omni.kit.ui_test import omni.ui as ui import omni.usd import omni.timeline import carb.tokens import omni.usd.audio from omni.ui.tests.test_base import OmniUiTest import pathlib import asyncio; class TestAudioPlayerWindow(OmniUiTest): # pragma: no cover async def _dock_window(self): await self.docked_test_window( window=self._win.window, width=600, height=200) def _dump_ui_tree(self, root): print("DUMP UI TREE START") #windows = omni.ui.Workspace.get_windows() #children = [windows[0].frame] children = [root.frame] print(str(dir(root.frame))) def recurse(children, path=""): for c in children: name = path + "/" + type(c).__name__ print(name) if isinstance(c, omni.ui.ComboBox): print(str(dir(c))) recurse(omni.ui.Inspector.get_children(c), name) recurse(children) print("DUMP UI TREE END") async def setUp(self): await super().setUp() extension_path = carb.tokens.get_tokens_interface().resolve("${omni.kit.window.audioplayer}") self._test_path = pathlib.Path(extension_path).joinpath("data").joinpath("tests").absolute() self._golden_img_dir = self._test_path.joinpath("golden") # open the dropdown window_menu = omni.kit.ui_test.get_menubar().find_menu("Window") self.assertIsNotNone(window_menu) await window_menu.click() # click the audioplayer option to open it player_menu = omni.kit.ui_test.get_menubar().find_menu("Audio Player") self.assertIsNotNone(player_menu) await player_menu.click() self._win = omni.kit.ui_test.find("Audio Player") self.assertIsNotNone(self._win) self._file_name_textbox = self._win.find("**/StringField[*]") self.assertIsNotNone(self._file_name_textbox) async def _test_just_opened(self): await self._dock_window() await self.finalize_test(golden_img_dir=self._golden_img_dir, golden_img_name="test_just_opened.png") async def _test_load_file(self): await self._file_name_textbox.click() await self._file_name_textbox.input(str(self._test_path / "1hz.oga")) await asyncio.sleep(1.0) # delete the text in the textbox so we'll have something constant # for the image comparison await self._file_name_textbox.double_click() await omni.kit.ui_test.emulate_keyboard_press(carb.input.KeyboardInput.DEL) await self._dock_window() await self.finalize_test(golden_img_dir=self._golden_img_dir, golden_img_name="test_open_file.png") async def test_all(self): await self._test_just_opened() await self._test_load_file() self._dump_ui_tree(self._win.window)
3,359
Python
34.368421
109
0.649598
omniverse-code/kit/exts/omni.kit.property.geometry/omni/kit/property/geometry/scripts/geometry_properties.py
import os import carb import omni.ext from functools import partial from pathlib import Path from pxr import Sdf, Usd, UsdGeom, UsdUI from typing import Any, Callable from omni.kit.property.usd.prim_selection_payload import PrimSelectionPayload _extension_instance = None TEST_DATA_PATH = "" def get_instance(): global _extension_instance return _extension_instance class GeometryPropertyExtension(omni.ext.IExt): def __init__(self): self._registered = False self._button_menu_entry = [] self._visual_property_widget = None super().__init__() def on_startup(self, ext_id): global _extension_instance _extension_instance = self self._register_widget() manager = omni.kit.app.get_app().get_extension_manager() extension_path = manager.get_extension_path(ext_id) global TEST_DATA_PATH TEST_DATA_PATH = Path(extension_path).joinpath("data").joinpath("tests") # +add menu item(s) from omni.kit.property.usd import PrimPathWidget context_menu = omni.kit.context_menu.get_instance() if context_menu is None: carb.log_error("context_menu is disabled!") # pragma: no cover return None # pragma: no cover self._button_menu_entry.append( PrimPathWidget.add_button_menu_entry( "Instanceable", show_fn=context_menu.is_prim_selected, onclick_fn=self._click_toggle_instanceable, ) ) self._button_menu_entry.append( PrimPathWidget.add_button_menu_entry( "Rendering/Toggle Wireframe Mode", name_fn=partial(self._get_primvar_state, prim_name="wireframe", text_name=" Wireframe Mode"), show_fn=partial(context_menu.prim_is_type, type=UsdGeom.Boundable), onclick_fn=partial(self._click_set_primvar, prim_name="wireframe"), ) ) self._button_menu_entry.append( PrimPathWidget.add_button_menu_entry( "Rendering/Toggle Do Not Cast Shadows", name_fn=partial( self._get_primvar_state, prim_name="doNotCastShadows", text_name=" Do Not Cast Shadows" ), show_fn=partial(context_menu.prim_is_type, type=UsdGeom.Boundable), onclick_fn=partial(self._click_set_primvar, prim_name="doNotCastShadows"), ) ) self._button_menu_entry.append( PrimPathWidget.add_button_menu_entry( "Rendering/Toggle Enable Shadow Terminator Fix", name_fn=partial( self._get_primvar_state, prim_name="enableShadowTerminatorFix", text_name=" Enable Shadow Terminator Fix" ), show_fn=partial(context_menu.prim_is_type, type=UsdGeom.Boundable), onclick_fn=partial(self._click_set_primvar, prim_name="enableShadowTerminatorFix"), ) ) self._button_menu_entry.append( PrimPathWidget.add_button_menu_entry( "Rendering/Toggle Enable Fast Refraction Shadow", name_fn=partial( self._get_primvar_state, prim_name="enableFastRefractionShadow", text_name=" Enable Fast Refraction Shadow" ), show_fn=partial(context_menu.prim_is_type, type=UsdGeom.Boundable), onclick_fn=partial(self._click_set_primvar, prim_name="enableFastRefractionShadow"), ) ) self._button_menu_entry.append( PrimPathWidget.add_button_menu_entry( "Rendering/Toggle Disable RT SSS Transmission", name_fn=partial( self._get_primvar_state, prim_name="disableRtSssTransmission", text_name=" Disable RT SSS Transmission" ), show_fn=partial(context_menu.prim_is_type, type=UsdGeom.Boundable), onclick_fn=partial(self._click_set_primvar, prim_name="disableRtSssTransmission"), ) ) self._button_menu_entry.append( PrimPathWidget.add_button_menu_entry( "Multimatted ID:", name_fn=partial( self._get_primvar_state, prim_name="multimatte_id", text_name=" ID for multimatte" ), show_fn=partial(context_menu.prim_is_type, type=UsdGeom.Boundable), onclick_fn=partial(self._click_set_primvar, prim_name="multimatte_id"), ) ) self._button_menu_entry.append( PrimPathWidget.add_button_menu_entry( "Rendering/Toggle Enable Holdout Object", name_fn=partial( self._get_primvar_state, prim_name="holdoutObject", text_name=" Enable Holdout Object" ), show_fn=partial(context_menu.prim_is_type, type=UsdGeom.Boundable), onclick_fn=partial(self._click_set_primvar, prim_name="holdoutObject"), ) ) self._button_menu_entry.append( PrimPathWidget.add_button_menu_entry( "Rendering/Toggle Invisible To Secondary Rays", name_fn=partial( self._get_primvar_state, prim_name="invisibleToSecondaryRays", text_name=" Invisible To Secondary Rays" ), show_fn=partial(context_menu.prim_is_type, type=UsdGeom.Boundable), onclick_fn=partial(self._click_set_primvar, prim_name="invisibleToSecondaryRays"), ) ) self._button_menu_entry.append( PrimPathWidget.add_button_menu_entry( "Rendering/Toggle Is Procedural Volume", name_fn=partial( self._get_primvar_state, prim_name="isVolume", text_name=" Is Volume" ), show_fn=partial(context_menu.prim_is_type, type=UsdGeom.Boundable), onclick_fn=partial(self._click_set_primvar, prim_name="isVolume"), ) ) self._button_menu_entry.append( PrimPathWidget.add_button_menu_entry( "Rendering/Toggle Matte Object", name_fn=partial(self._get_primvar_state, prim_name="isMatteObject", text_name=" Matte Object"), show_fn=partial(context_menu.prim_is_type, type=UsdGeom.Boundable), onclick_fn=partial(self._click_set_primvar, prim_name="isMatteObject"), ) ) self._button_menu_entry.append( PrimPathWidget.add_button_menu_entry( "Rendering/Toggle Hide From Camera", name_fn=partial( self._get_primvar_state, prim_name="hideForCamera", text_name=" Hide From Camera" ), show_fn=[partial(context_menu.prim_is_type, type=UsdGeom.Boundable)], onclick_fn=partial(self._click_set_primvar, prim_name="hideForCamera"), ) ) self._button_menu_entry.append( PrimPathWidget.add_button_menu_entry( "Rendering/Toggle Is Light", name_fn=partial(self._get_primvar_state, prim_name="isLight", text_name=" Is Light"), show_fn=[partial(context_menu.prim_is_type, type=UsdGeom.Boundable)], onclick_fn=partial(self._click_set_primvar, prim_name="isLight"), ) ) def on_shutdown(self): # pragma: no cover if self._registered: self._unregister_widget() # release menu item(s) from omni.kit.property.usd import PrimPathWidget for item in self._button_menu_entry: PrimPathWidget.remove_button_menu_entry(item) global _extension_instance _extension_instance = None def register_custom_visual_attribute(self, attribute_name: str, display_name: str, type_name: str, default_value: Any, predicate: Callable[[Any], bool] = None): """ Add custom attribute with placeholder. """ if self._visual_property_widget: self._visual_property_widget.add_custom_attribute( attribute_name, display_name, type_name, default_value, predicate ) def deregister_custom_visual_attribute(self, attribute_name: str): if self._visual_property_widget: self._visual_property_widget.remove_custom_attribute(attribute_name) def _register_widget(self): import omni.kit.window.property as p from .prim_kind_widget import PrimKindWidget from .prim_geometry_widget import GeometrySchemaAttributesWidget, ImageableSchemaAttributesWidget w = p.get_window() if w: w.register_widget( "prim", "geometry", GeometrySchemaAttributesWidget( "Geometry", UsdGeom.Xformable, [ UsdGeom.BasisCurves, UsdGeom.Capsule, UsdGeom.Cone, UsdGeom.Cube, UsdGeom.Cylinder, UsdGeom.HermiteCurves, UsdGeom.Mesh, UsdGeom.NurbsCurves, UsdGeom.NurbsPatch, UsdGeom.PointInstancer, UsdGeom.Points, UsdGeom.Subset, UsdGeom.Sphere, UsdGeom.Xform, UsdGeom.Gprim, UsdGeom.PointBased, UsdGeom.Boundable, UsdGeom.Curves, UsdGeom.Imageable, UsdGeom.PointBased, UsdGeom.Subset, UsdGeom.ModelAPI, UsdGeom.MotionAPI, UsdGeom.PrimvarsAPI, UsdGeom.XformCommonAPI, UsdGeom.ModelAPI, UsdUI.Backdrop, UsdUI.NodeGraphNodeAPI, UsdUI.SceneGraphPrimAPI, ], [ "proceduralMesh:parameterCheck", "outputs:parameterCheck", "refinementEnableOverride", "refinementLevel", "primvars:doNotCastShadows", "primvars:enableShadowTerminatorFix", "primvars:enableFastRefractionShadow", "primvars:disableRtSssTransmission", "primvars:holdoutObject", "primvars:invisibleToSecondaryRays", "primvars:isMatteObject", "primvars:isVolume", "primvars:multimatte_id", "primvars:numSplits", "primvars:endcaps", UsdGeom.Tokens.proxyPrim, ], [ "primvars:displayColor", "primvars:displayOpacity", "doubleSided", "purpose", "visibility", "xformOpOrder", ], ), ) self._visual_property_widget = ImageableSchemaAttributesWidget( "Visual", UsdGeom.Imageable, [], ["primvars:displayColor", "primvars:displayOpacity", "doubleSided", "singleSided"], [] ) w.register_widget( "prim", "geometry_imageable", self._visual_property_widget, ) w.register_widget("prim", "kind", PrimKindWidget()) self._registered = True def _unregister_widget(self): # pragma: no cover import omni.kit.window.property as p w = p.get_window() if w: w.unregister_widget("prim", "geometry") w.unregister_widget("prim", "geometry_imageable") w.unregister_widget("prim", "kind") self._registered = False def _click_set_primvar(self, payload: PrimSelectionPayload, prim_name: str): stage = payload.get_stage() if not stage: return omni.kit.commands.execute("TogglePrimVarCommand", prim_path=payload.get_paths(), prim_name=prim_name) def _get_primvar_state(self, objects: dict, prim_name: str, text_prefix: str = "", text_name: str = "") -> str: if not "stage" in objects or not "prim_list" in objects or not objects["stage"]: return None stage = objects["stage"] primvar_state = [] for path in objects["prim_list"]: prim = stage.GetPrimAtPath(path) if isinstance(path, Sdf.Path) else path if prim: primvars_api = UsdGeom.PrimvarsAPI(prim) is_primvar = primvars_api.GetPrimvar(prim_name) if is_primvar: primvar_state.append(is_primvar.Get()) else: primvar_state.append(False) if primvar_state == [False] * len(primvar_state): return f"{text_prefix}Set{text_name}" elif primvar_state == [True] * len(primvar_state): return f"{text_prefix}Clear{text_name}" return f"{text_prefix}Toggle{text_name}" def _click_toggle_instanceable(self, payload: PrimSelectionPayload): omni.kit.commands.execute("ToggleInstanceableCommand", prim_path=payload.get_paths())
14,059
Python
41.477341
127
0.532968
omniverse-code/kit/exts/omni.kit.property.geometry/omni/kit/property/geometry/scripts/prim_geometry_widget.py
# Copyright (c) 2020, 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. # import omni.ui as ui import omni.usd from dataclasses import dataclass, field from typing import Any, Callable, OrderedDict, List from omni.kit.property.usd.usd_property_widget import MultiSchemaPropertiesWidget, UsdPropertyUiEntry from omni.kit.property.usd.usd_property_widget import create_primspec_bool, create_primspec_int from omni.kit.property.usd.custom_layout_helper import CustomLayoutFrame, CustomLayoutGroup, CustomLayoutProperty from pxr import Kind, Sdf, Usd, UsdGeom class GeometrySchemaAttributesWidget(MultiSchemaPropertiesWidget): def __init__(self, title: str, schema, schema_subclasses: list, include_list: list = [], exclude_list: list = []): """ Constructor. Args: title (str): Title of the widgets on the Collapsable Frame. schema: The USD IsA schema or applied API schema to filter attributes. schema_subclasses (list): list of subclasses include_list (list): list of additional schema named to add exclude_list (list): list of additional schema named to remove """ super().__init__(title, schema, schema_subclasses, include_list, exclude_list) # custom attributes self.add_custom_schema_attribute("primvars:enableFastRefractionShadow", lambda p: p.IsA(UsdGeom.Gprim), None, "", create_primspec_bool(False)) self.add_custom_schema_attribute("primvars:doNotCastShadows", lambda p: p.IsA(UsdGeom.Gprim), None, "", create_primspec_bool(False)) self.add_custom_schema_attribute("primvars:enableShadowTerminatorFix", lambda p: p.IsA(UsdGeom.Gprim), None, "", create_primspec_bool(True)) self.add_custom_schema_attribute("primvars:holdoutObject", lambda p: p.IsA(UsdGeom.Gprim), None, "", create_primspec_bool(False)) self.add_custom_schema_attribute("primvars:invisibleToSecondaryRays", lambda p: p.IsA(UsdGeom.Gprim), None, "", create_primspec_bool(False)) self.add_custom_schema_attribute("primvars:isMatteObject", lambda p: p.IsA(UsdGeom.Gprim), None, "", create_primspec_bool(False)) self.add_custom_schema_attribute("primvars:isVolume", lambda p: p.IsA(UsdGeom.Gprim), None, "", create_primspec_bool(False)) self.add_custom_schema_attribute("primvars:multimatte_id", lambda p: p.IsA(UsdGeom.Gprim), None, "", create_primspec_int(-1)) self.add_custom_schema_attribute("primvars:disableRtSssTransmission", lambda p: p.IsA(UsdGeom.Gprim), None, "", create_primspec_bool(False)) self.add_custom_schema_attribute("primvars:numSplitsOverride", lambda p: p.IsA(UsdGeom.BasisCurves), None, "", create_primspec_bool(False)) self.add_custom_schema_attribute("primvars:numSplits", lambda p: p.IsA(UsdGeom.BasisCurves), None, "", create_primspec_int(2)) self.add_custom_schema_attribute("primvars:endcaps", lambda p: p.IsA(UsdGeom.BasisCurves), None, "", create_primspec_int(1)) self.add_custom_schema_attribute("refinementEnableOverride", self._is_prim_refinement_level_supported, None, "", create_primspec_bool(False)) self.add_custom_schema_attribute("refinementLevel", self._is_prim_refinement_level_supported, None, "", create_primspec_int(0)) def on_new_payload(self, payload): """ See PropertyWidget.on_new_payload """ self._add_curves = False self._add_points = False if not super().on_new_payload(payload): return False if not self._payload or len(self._payload) == 0: return False used = [] for prim_path in self._payload: prim = self._get_prim(prim_path) if not prim or not prim.IsA(self._schema): return False used += [attr for attr in prim.GetProperties() if attr.GetName() in self._schema_attr_names and not attr.IsHidden()] if (prim.IsA(UsdGeom.BasisCurves)): self._add_curves = True if (prim.IsA(UsdGeom.Points)): self._add_points = True if self.is_custom_schema_attribute_used(prim): used.append(None) return used def _is_prim_refinement_level_supported(self, prim): return ( prim.IsA(UsdGeom.Mesh) or prim.IsA(UsdGeom.Cylinder) or prim.IsA(UsdGeom.Capsule) or prim.IsA(UsdGeom.Cone) or prim.IsA(UsdGeom.Sphere) or prim.IsA(UsdGeom.Cube) ) def _is_prim_single_sided_supported(self, prim): return ( prim.IsA(UsdGeom.Mesh) or prim.IsA(UsdGeom.Cylinder) or prim.IsA(UsdGeom.Capsule) or prim.IsA(UsdGeom.Cone) or prim.IsA(UsdGeom.Sphere) or prim.IsA(UsdGeom.Cube) ) def _customize_props_layout(self, attrs): self.add_custom_schema_attributes_to_props(attrs) frame = CustomLayoutFrame(hide_extra=False) with frame: def update_bounds(stage, prim_paths): timeline = omni.timeline.get_timeline_interface() current_time = timeline.get_current_time() current_time_code = Usd.TimeCode( omni.usd.get_frame_time_code(current_time, stage.GetTimeCodesPerSecond()) ) for path in prim_paths: prim = stage.GetPrimAtPath(path) attr = prim.GetAttribute("extent") if prim else None if prim and attr: bounds = UsdGeom.Boundable.ComputeExtentFromPlugins(UsdGeom.Boundable(prim), current_time_code) attr.Set(bounds) def build_extent_func( stage, attr_name, metadata, property_type, prim_paths: List[Sdf.Path], additional_label_kwargs={}, additional_widget_kwargs={}, ): from omni.kit.window.property.templates import HORIZONTAL_SPACING from omni.kit.property.usd.usd_attribute_model import UsdAttributeModel from omni.kit.property.usd.usd_property_widget import UsdPropertiesWidgetBuilder from omni.kit.property.usd.widgets import ICON_PATH if not attr_name or not property_type: return def value_changed_func(model, widget): val = model.get_value_as_string() widget.set_tooltip(val) with ui.HStack(spacing=HORIZONTAL_SPACING): model = UsdAttributeModel(stage, [path.AppendProperty(attr_name) for path in prim_paths], False, metadata) UsdPropertiesWidgetBuilder._create_label(attr_name, metadata, additional_label_kwargs) kwargs = { "name": "models_readonly", "model": model, "enabled": False, "tooltip": model.get_value_as_string(), } if additional_widget_kwargs: kwargs.update(additional_widget_kwargs) with ui.ZStack(): value_widget = ui.StringField(**kwargs) mixed_overlay = UsdPropertiesWidgetBuilder._create_mixed_text_overlay() ui.Spacer(width=0) with ui.VStack(width=8): ui.Spacer() ui.Image( f"{ICON_PATH}/Default value.svg", width=5.5, height=5.5, ) ui.Spacer() model.add_value_changed_fn(lambda m, w=value_widget: value_changed_func(m,w)) return model def build_size_func( stage, attr_name, metadata, property_type, prim_paths: List[Sdf.Path], additional_label_kwargs={}, additional_widget_kwargs={}, ): from omni.kit.window.property.templates import HORIZONTAL_SPACING from omni.kit.property.usd.usd_attribute_model import UsdAttributeModel from omni.kit.property.usd.usd_property_widget import UsdPropertiesWidgetBuilder from omni.kit.property.usd.widgets import ICON_PATH if not attr_name or not property_type: return with ui.HStack(spacing=HORIZONTAL_SPACING): model_kwargs = UsdPropertiesWidgetBuilder._get_attr_value_range_kwargs(metadata) model = UsdAttributeModel( stage, [path.AppendProperty(attr_name) for path in prim_paths], False, metadata, **model_kwargs ) UsdPropertiesWidgetBuilder._create_label(attr_name, metadata, additional_label_kwargs) widget_kwargs = {"model": model} widget_kwargs.update(UsdPropertiesWidgetBuilder._get_attr_value_soft_range_kwargs(metadata)) if additional_widget_kwargs: widget_kwargs.update(additional_widget_kwargs) with ui.ZStack(): value_widget = UsdPropertiesWidgetBuilder._create_drag_or_slider(ui.FloatDrag, ui.FloatSlider, **widget_kwargs) mixed_overlay = UsdPropertiesWidgetBuilder._create_mixed_text_overlay() UsdPropertiesWidgetBuilder._create_control_state(value_widget=value_widget, mixed_overlay=mixed_overlay, **widget_kwargs) model.add_value_changed_fn(lambda m, s=stage, p=prim_paths: update_bounds(s, p)) return model def build_axis_func( stage, attr_name, metadata, property_type, prim_paths: List[Sdf.Path], additional_label_kwargs={}, additional_widget_kwargs={}, ): from omni.kit.window.property.templates import HORIZONTAL_SPACING from omni.kit.property.usd.usd_attribute_model import TfTokenAttributeModel from omni.kit.property.usd.usd_property_widget import UsdPropertiesWidgetBuilder from omni.kit.property.usd.widgets import ICON_PATH if not attr_name or not property_type: return with ui.HStack(spacing=HORIZONTAL_SPACING): model = None UsdPropertiesWidgetBuilder._create_label(attr_name, metadata, additional_label_kwargs) tokens = metadata.get("allowedTokens") if tokens is not None and len(tokens) > 0: model = TfTokenAttributeModel( stage, [path.AppendProperty(attr_name) for path in prim_paths], False, metadata ) widget_kwargs = {"name": "choices"} if additional_widget_kwargs: widget_kwargs.update(additional_widget_kwargs) with ui.ZStack(): value_widget = ui.ComboBox(model, **widget_kwargs) mixed_overlay = UsdPropertiesWidgetBuilder._create_mixed_text_overlay() else: model = UsdAttributeModel( stage, [path.AppendProperty(attr_name) for path in prim_paths], False, metadata ) widget_kwargs = {"name": "models"} if additional_widget_kwargs: widget_kwargs.update(additional_widget_kwargs) with ui.ZStack(): value_widget = ui.StringField(model, **widget_kwargs) mixed_overlay = UsdPropertiesWidgetBuilder._create_mixed_text_overlay() UsdPropertiesWidgetBuilder._create_control_state( model=model, value_widget=value_widget, mixed_overlay=mixed_overlay, **widget_kwargs ) model.add_item_changed_fn(lambda m, i, s=stage, p=prim_paths: update_bounds(s, p)) return model def build_endcaps_func( stage, attr_name, metadata, property_type, prim_paths: List[Sdf.Path], additional_label_kwargs={}, additional_widget_kwargs={}, ): from omni.kit.window.property.templates import HORIZONTAL_SPACING from omni.kit.property.usd.usd_attribute_model import TfTokenAttributeModel from omni.kit.property.usd.usd_property_widget import UsdPropertiesWidgetBuilder from omni.kit.property.usd.widgets import ICON_PATH if not attr_name or not property_type: return with ui.HStack(spacing=HORIZONTAL_SPACING): model = None UsdPropertiesWidgetBuilder._create_label(attr_name, metadata, additional_label_kwargs) class MyTfTokenAttributeModel(TfTokenAttributeModel): allowed_tokens = ["open", "flat", "round"] def _get_allowed_tokens(self, attr): return self.allowed_tokens def _get_value_from_index(self, value): return value def _update_value(self, force=False): was_updating_value = self._updating_value self._updating_value = True if super(TfTokenAttributeModel, self)._update_value(force): # TODO don't have to do this every time. Just needed when "allowedTokens" actually changed self._update_allowed_token() index = self._value if self._value < len(self._allowed_tokens) else -1 if index != -1 and self._current_index.as_int != index: self._current_index.set_value(index) self._item_changed(None) self._updating_value = was_updating_value model = MyTfTokenAttributeModel( stage, [path.AppendProperty(attr_name) for path in prim_paths], False, metadata ) widget_kwargs = {"name": "choices"} if additional_widget_kwargs: widget_kwargs.update(additional_widget_kwargs) with ui.ZStack(): value_widget = ui.ComboBox(model, **widget_kwargs) mixed_overlay = UsdPropertiesWidgetBuilder._create_mixed_text_overlay() UsdPropertiesWidgetBuilder._create_control_state( model=model, value_widget=value_widget, mixed_overlay=mixed_overlay, **widget_kwargs ) return model if self._add_curves: with CustomLayoutGroup("Curve"): CustomLayoutProperty("curveVertexCounts", "Per curve points") CustomLayoutProperty("points", "Points") CustomLayoutProperty("normals", "Normals") CustomLayoutProperty("widths", "Widths") CustomLayoutProperty("type", "Type") CustomLayoutProperty("basis", "Basis") CustomLayoutProperty("wrap", "Wrap") CustomLayoutProperty("primvars:numSplitsOverride", "Number of BVH splits Override") CustomLayoutProperty("primvars:numSplits", "Number of BVH splits") CustomLayoutProperty("primvars:endcaps", "Endcaps", build_fn=build_endcaps_func) if self._add_points: with CustomLayoutGroup("Points"): CustomLayoutProperty("points", "Points") CustomLayoutProperty("normals", "Normals") CustomLayoutProperty("widths", "Widths") commonSectionName = "Mesh" if self._add_curves or self._add_points: commonSectionName = "Common" with CustomLayoutGroup(commonSectionName): CustomLayoutProperty("normals", "Normals") CustomLayoutProperty("orientation", "Orientation") CustomLayoutProperty("points", "Points") CustomLayoutProperty("velocities", "Velocities") CustomLayoutProperty("accelerations", "Accelerations") CustomLayoutProperty("extent", "Extent", build_fn=build_extent_func) CustomLayoutProperty("size", "Size", build_fn=build_size_func) CustomLayoutProperty("radius", "Radius", build_fn=build_size_func) CustomLayoutProperty("axis", "Axis", build_fn=build_axis_func) CustomLayoutProperty("height", "Height", build_fn=build_size_func) CustomLayoutProperty("polymesh:parameterCheck", "Parameter Check") CustomLayoutProperty("primvars:doNotCastShadows", "Cast Shadows", build_fn=self._inverse_bool_builder) CustomLayoutProperty("primvars:enableShadowTerminatorFix", "Shadow Terminator Fix") CustomLayoutProperty("primvars:enableFastRefractionShadow", "Fast Refraction Shadow") CustomLayoutProperty("primvars:disableRtSssTransmission", "Enable Rt SSS Transmission", build_fn=self._inverse_bool_builder) CustomLayoutProperty("primvars:holdoutObject", "Holdout Object") CustomLayoutProperty("primvars:invisibleToSecondaryRays", "Invisible To Secondary Rays") CustomLayoutProperty("primvars:isMatteObject", "Matte Object") CustomLayoutProperty("primvars:isVolme", "Is Volume") CustomLayoutProperty("primvars:multimatte_id", "Multimatte ID") with CustomLayoutGroup("Face"): CustomLayoutProperty("faceVertexIndices", "Indices") CustomLayoutProperty("faceVertexCounts", "Counts") CustomLayoutProperty("faceVaryingLinearInterpolation", "Linear Interpolation") CustomLayoutProperty("holeIndices", "Hole Indices") with CustomLayoutGroup("Refinement"): CustomLayoutProperty("refinementEnableOverride", "Refinement Override") CustomLayoutProperty("refinementLevel", "Refinement Level") CustomLayoutProperty("interpolateBoundary", "Interpolate Boundary") CustomLayoutProperty("subdivisionScheme", "Subdivision Scheme") CustomLayoutProperty("triangleSubdivisionRule", "Triangle SubdivisionRule") with CustomLayoutGroup("Corner"): CustomLayoutProperty("cornerIndices", "Indices") CustomLayoutProperty("cornerSharpnesses", "Sharpnesses") with CustomLayoutGroup("Crease"): CustomLayoutProperty("creaseIndices", "Indices") CustomLayoutProperty("creaseLengths", "Lengths") CustomLayoutProperty("creaseSharpnesses", "Sharpnesses") return frame.apply(attrs) def get_additional_kwargs(self, ui_prop: UsdPropertyUiEntry): """ Override this function if you want to supply additional arguments when building the label or ui widget. """ additional_widget_kwargs = None if ui_prop.prop_name == "refinementLevel": additional_widget_kwargs = {"min": 0, "max": 5} return None, additional_widget_kwargs def _inverse_bool_builder(self, stage, attr_name, metadata, property_type, prim_paths: List[Sdf.Path], additional_label_kwargs={}, additional_widget_kwargs={} ): import carb.settings from omni.kit.window.property.templates import HORIZONTAL_SPACING from omni.kit.property.usd.usd_attribute_model import UsdAttributeInvertedModel from omni.kit.property.usd.usd_property_widget import UsdPropertiesWidgetBuilder if not attr_name or not property_type: return with ui.HStack(spacing=HORIZONTAL_SPACING): model = UsdAttributeInvertedModel(stage, [path.AppendProperty(attr_name) for path in prim_paths], False, metadata) settings = carb.settings.get_settings() left_aligned = settings.get("ext/omni.kit.window.property/checkboxAlignment") == "left" if not left_aligned: if not additional_label_kwargs: additional_label_kwargs = {} additional_label_kwargs["width"] = 0 UsdPropertiesWidgetBuilder._create_label(attr_name, metadata, additional_label_kwargs) if not left_aligned: ui.Spacer(width=10) ui.Line(style={"color": 0x338A8777}, width=ui.Fraction(1)) ui.Spacer(width=5) with ui.VStack(width=10): ui.Spacer() widget_kwargs = {"width": 10, "height": 0, "name": "greenCheck", "model": model} if additional_widget_kwargs: widget_kwargs.update(additional_widget_kwargs) with ui.ZStack(): with ui.Placer(offset_x=0, offset_y=-2): value_widget = ui.CheckBox(**widget_kwargs) with ui.Placer(offset_x=1, offset_y=-1): mixed_overlay = ui.Rectangle( height=8, width=8, name="mixed_overlay", alignment=ui.Alignment.CENTER, visible=False ) ui.Spacer() if left_aligned: ui.Spacer(width=5) ui.Line(style={"color": 0x338A8777}, width=ui.Fraction(1)) UsdPropertiesWidgetBuilder._create_control_state(value_widget=value_widget, mixed_overlay=mixed_overlay, **widget_kwargs) return model @dataclass(frozen=True) class CustomAttributeInfo: schema_name: str display_name: str type_name: str default_value: Any predicate: Callable[[Any], bool] = None def is_supported(self, prim): return self.predicate is None or self.predicate(prim) def get_metadata(self): return {Sdf.PrimSpec.TypeNameKey: self.type_name, "customData": {"default": self.default_value}} class ImageableSchemaAttributesWidget(MultiSchemaPropertiesWidget): def __init__(self, title: str, schema, schema_subclasses: list, include_list: list = [], exclude_list: list = []): """ Constructor. Args: title (str): Title of the widgets on the Collapsable Frame. schema: The USD IsA schema or applied API schema to filter attributes. schema_subclasses (list): list of subclasses include_list (list): list of additional schema named to add exclude_list (list): list of additional schema named to remove """ super().__init__(title, schema, schema_subclasses, include_list, exclude_list) self._custom_attributes: OrderedDict[str, CustomAttributeInfo] = OrderedDict() self._custom_placeholders: List[str] = [] # custom attributes self.add_custom_schema_attribute("singleSided", self._is_prim_single_sided_supported, None, "", create_primspec_bool(False)) def on_new_payload(self, payload): """ See PropertyWidget.on_new_payload """ self._custom_placeholders.clear() if not super().on_new_payload(payload): return False if not self._payload or len(self._payload) == 0: return False used = [] for prim_path in self._payload: prim = self._get_prim(prim_path) if not prim or not prim.IsA(self._schema): return False used += [attr for attr in prim.GetProperties() if attr.GetName() in self._schema_attr_names and not attr.IsHidden()] for schema_name, attr_info in self._custom_attributes.items(): if attr_info.is_supported(prim) and not prim.GetAttribute(schema_name): self._custom_placeholders.append(schema_name) used.append(None) if self.is_custom_schema_attribute_used(prim): used.append(None) return used def add_custom_attribute(self, attribute_name, display_name, type_name="bool", default_value=False, predicate: Callable[[Any], bool] = None): """ Add custom attribute with placeholder. """ self._schema_attr_base.add(attribute_name) self._custom_attributes.update( {attribute_name: CustomAttributeInfo(attribute_name, display_name, type_name, default_value, predicate)} ) self.request_rebuild() def remove_custom_attribute(self, attribute_name): self._schema_attr_base.remove(attribute_name) del self._custom_attributes[attribute_name] self.request_rebuild() def _is_prim_single_sided_supported(self, prim): return ( prim.IsA(UsdGeom.Mesh) or prim.IsA(UsdGeom.Cylinder) or prim.IsA(UsdGeom.Capsule) or prim.IsA(UsdGeom.Cone) or prim.IsA(UsdGeom.Sphere) or prim.IsA(UsdGeom.Cube) ) def _customize_props_layout(self, attrs): self.add_custom_schema_attributes_to_props(attrs) for schema_name, attr_info in self._custom_attributes.items(): if schema_name in self._custom_placeholders: attrs.append( UsdPropertyUiEntry( schema_name, "", attr_info.get_metadata(), Usd.Attribute, ) ) frame = CustomLayoutFrame(hide_extra=True) with frame: for schema_name, attr_info in self._custom_attributes.items(): CustomLayoutProperty(schema_name, attr_info.display_name) # OMFP-1917: Most Visual settings under the Property tab don't work # Hiding doubleSided, singleSided, primvars:displayColor, primvars:displayOpacity CustomLayoutProperty("doubleSided", "Double Sided", hide_if_true=True) CustomLayoutProperty("singleSided", "Single Sided", hide_if_true=True) CustomLayoutProperty("purpose", "Purpose") CustomLayoutProperty("visibility", "Visibility") CustomLayoutProperty("primvars:displayColor", "Display Color") CustomLayoutProperty("primvars:displayOpacity", "Display Opacity") return frame.apply(attrs)
28,148
Python
49.627698
160
0.574996
omniverse-code/kit/exts/omni.kit.property.geometry/omni/kit/property/geometry/scripts/__init__.py
from .geometry_properties import * from .geometry_commands import *
68
Python
21.999993
34
0.794118
omniverse-code/kit/exts/omni.kit.property.geometry/omni/kit/property/geometry/scripts/geometry_commands.py
import carb import omni.kit.commands from typing import List, Optional, Any from pxr import Usd, Sdf, UsdGeom class PrimVarCommand(omni.kit.commands.Command): """ Set primvar undoable **Command**. Args: prim_path (list): List of paths of prims. prim_name (str): Primvar name. prim_type (): Primvar variable type (EG. Sdf.ValueTypeNames.Bool) value (any): New primvar value. If primvar doesn't exist, it will be created """ def __init__( self, prim_path: List[str], prim_name: str, prim_type: str, value: Any, usd_context_name: Optional[str] = "", ): self._prim_path = prim_path self._prim_name = prim_name self._prim_type = prim_type self._value = value self._usd_context = omni.usd.get_context(usd_context_name) self._undo_values = {} def do(self): stage = self._usd_context.get_stage() for path in self._prim_path: if path: primvars_api = UsdGeom.PrimvarsAPI(stage.GetPrimAtPath(path)) value = primvars_api.GetPrimvar(self._prim_name) if value: if value.GetTypeName() != self._prim_type: carb.log_error(f"PrimVarCommand: cannot set value as {path}.{self._prim_name} is type {value.GetTypeName()} and expected type is {self._prim_type}") else: self._undo_values[str(path)] = value.Get() value.Set(self._value) else: self._undo_values[str(path)] = None primvars_api.CreatePrimvar(self._prim_name, self._prim_type).Set(self._value) def undo(self): stage = self._usd_context.get_stage() for path in self._undo_values.keys(): primvars_api = UsdGeom.PrimvarsAPI(stage.GetPrimAtPath(path)) value = primvars_api.GetPrimvar(self._prim_name) orig_value = self._undo_values[path] if orig_value: value.Set(orig_value) else: primvars_api.RemovePrimvar(self._prim_name) self._undo_values = {} class TogglePrimVarCommand(omni.kit.commands.Command): """ Toggle primvar undoable **Command**. Args: prim_path (list): List of paths of prims. prim_name (str): Primvar name. """ def __init__( self, prim_path: List[str], prim_name: str, usd_context_name: Optional[str] = "", ): self._prim_path = prim_path self._prim_name = prim_name self._usd_context = omni.usd.get_context(usd_context_name) self._undo_values = {} def do(self): stage = self._usd_context.get_stage() for path in self._prim_path: if path: primvars_api = UsdGeom.PrimvarsAPI(stage.GetPrimAtPath(path)) value = primvars_api.GetPrimvar(self._prim_name) if value: if value.GetTypeName() != Sdf.ValueTypeNames.Bool: carb.log_error(f"TogglePrimVarCommand: cannot set value as {value.GetTypeName()} isn't a {self._prim_type}") else: self._undo_values[str(path)] = value.Get() value.Set(not value.Get()) else: self._undo_values[path] = None primvars_api.CreatePrimvar(self._prim_name, Sdf.ValueTypeNames.Bool).Set(True) def undo(self): stage = self._usd_context.get_stage() for path in self._undo_values.keys(): primvars_api = UsdGeom.PrimvarsAPI(stage.GetPrimAtPath(path)) value = primvars_api.GetPrimvar(self._prim_name) orig_value = self._undo_values[path] if orig_value: value.Set(orig_value) else: primvars_api.RemovePrimvar(self._prim_name) self._undo_values = {} class ToggleInstanceableCommand(omni.kit.commands.Command): """ Toggle instanceable undoable **Command**. Args: prim_path (list): List of paths of prims. """ def __init__( self, prim_path: List[str], usd_context_name: Optional[str] = "", ): self._prim_path = prim_path self._usd_context = omni.usd.get_context(usd_context_name) self._undo_values = {} def do(self): stage = self._usd_context.get_stage() for path in self._prim_path: if path: prim = stage.GetPrimAtPath(path) value = prim.IsInstanceable() self._undo_values[str(path)] = value prim.SetInstanceable(not value) def undo(self): stage = self._usd_context.get_stage() for path in self._undo_values.keys(): prim = stage.GetPrimAtPath(path) value = self._undo_values[path] prim.SetInstanceable(value) self._undo_values = {}
5,046
Python
33.101351
173
0.550139
omniverse-code/kit/exts/omni.kit.property.geometry/omni/kit/property/geometry/scripts/prim_kind_widget.py
# Copyright (c) 2020, 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. # import carb import omni.ui as ui import omni.usd import carb from omni.kit.window.property.templates import SimplePropertyWidget, LABEL_WIDTH, LABEL_HEIGHT, HORIZONTAL_SPACING from omni.kit.property.usd.usd_property_widget import UsdPropertiesWidgetBuilder, UsdPropertiesWidget from omni.kit.property.usd.usd_object_model import MetadataObjectModel from pxr import Kind, Usd, UsdGeom class Constant: def __setattr__(self, name, value): raise Exception(f"Can't change Constant.{name}") # pragma: no cover FONT_SIZE = 14.0 MIXED = "Mixed" MIXED_COLOR = 0xFFCC9E61 class PrimKindWidget(UsdPropertiesWidget): def __init__(self): super().__init__(title="Kind", collapsed=False) self._metadata_model = None def on_new_payload(self, payload): """ See PropertyWidget.on_new_payload """ if not super().on_new_payload(payload): # pragma: no cover return False # pragma: no cover if len(self._payload) == 0: return False for prim_path in self._payload: # pragma: no cover prim = self._get_prim(prim_path) # pragma: no cover if not prim or not prim.IsA(UsdGeom.Imageable): # pragma: no cover return False return True def reset(self): super().reset() if self._metadata_model: self._metadata_model.clean() self._metadata_model = None def build_items(self): super().build_items() # get Kinds all_kinds = Kind.Registry.GetAllKinds() all_kinds.insert(0, "") # http://graphics.pixar.com/usd/docs/USD-Glossary.html#USDGlossary-Kind # "model" is considered an abstract type and should not be assigned as any prim's kind. all_kinds.remove(Kind.Tokens.model) kind = None ambiguous = False stage = self._payload.get_stage() for path in self._payload: prim = stage.GetPrimAtPath(path) if prim: prim_kind = Usd.ModelAPI(prim).GetKind() if kind == None: kind = prim_kind elif kind != prim_kind: kind = "mixed" if prim_kind not in all_kinds: # pragma: no cover all_kinds.append(prim_kind) # pragma: no cover carb.log_verbose(f"{path} has invalid Kind:{prim_kind}") # pragma: no cover if kind == None: # pragma: no cover return # pragma: no cover if self._filter.matches("Kind"): self._any_item_visible = True highlight = self._filter.name with ui.HStack(spacing=HORIZONTAL_SPACING): UsdPropertiesWidgetBuilder._create_label("Kind", {}, {"highlight": highlight}) with ui.ZStack(): self._metadata_model = MetadataObjectModel( stage, [path for path in self._payload], False, {}, key="kind", default="", options=all_kinds ) value_widget = ui.ComboBox(self._metadata_model, name="choices") mixed_overlay = UsdPropertiesWidgetBuilder._create_mixed_text_overlay() UsdPropertiesWidgetBuilder._create_control_state(self._metadata_model, value_widget, mixed_overlay) def _get_shared_properties_from_selected_prims(self, anchor_prim): return None def _get_prim(self, prim_path): if prim_path: stage = self._payload.get_stage() if stage: return stage.GetPrimAtPath(prim_path) return None # pragma: no cover
4,198
Python
37.172727
117
0.596951
omniverse-code/kit/exts/omni.kit.property.geometry/omni/kit/property/geometry/tests/test_path_toggle.py
## Copyright (c) 2022, 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. ## import omni.kit.test import os import omni.kit.app import omni.usd from omni.kit.test.async_unittest import AsyncTestCase from omni.kit import ui_test from pxr import Gf from omni.kit.test_suite.helpers import open_stage, get_test_data_path, select_prims, wait_stage_loading, arrange_windows class PropertyPathAddMenu(AsyncTestCase): # Before running each test async def setUp(self): await arrange_windows("Stage", 64) await open_stage(get_test_data_path(__name__, "geometry_test.usda")) # After running each test async def tearDown(self): await wait_stage_loading() async def test_property_path_rendering(self): await ui_test.find("Property").focus() usd_context = omni.usd.get_context() stage = usd_context.get_stage() # select cube await select_prims(["/World/Cube"]) await ui_test.human_delay() # verify not set prim = stage.GetPrimAtPath("/World/Cube") attr = prim.GetAttribute("primvars:wireframe") self.assertFalse(attr.IsValid()) # click "Add" add_widget = [w for w in ui_test.find_all("Property//Frame/**/Button[*].identifier==''") if w.widget.text.endswith("Add")][0] await add_widget.click() # select wireframe await ui_test.select_context_menu("Rendering/Set Wireframe Mode") # verify set self.assertTrue(attr.IsValid()) self.assertTrue(attr.Get()) # undo omni.kit.undo.undo() # verify not set self.assertFalse(attr.IsValid())
2,006
Python
33.016949
133
0.678465
omniverse-code/kit/exts/omni.kit.property.geometry/omni/kit/property/geometry/tests/__init__.py
from .test_geometry import * from .test_commands import * from .test_path_toggle import *
90
Python
21.749995
31
0.755556
omniverse-code/kit/exts/omni.kit.property.geometry/omni/kit/property/geometry/tests/test_commands.py
## 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. ## import pathlib import omni.kit.app import omni.kit.commands import omni.kit.test import omni.ui as ui from omni.ui.tests.test_base import OmniUiTest from omni.kit.test_suite.helpers import open_stage, get_test_data_path from omni.kit import ui_test from pxr import Sdf class TestCommandWidget(OmniUiTest): # Before running each test async def setUp(self): await super().setUp() await open_stage(get_test_data_path(__name__, "geometry_test.usda")) # After running each test async def tearDown(self): await super().tearDown() async def test_command_prim_var(self): stage = omni.usd.get_context().get_stage() prim = stage.GetPrimAtPath("/World/Cube") attr = prim.GetAttribute('primvars:test_int') self.assertFalse(attr.IsValid()) # create primvar as int omni.kit.commands.execute("PrimVarCommand", prim_path=["/World/Cube"], prim_name="test_int", prim_type=Sdf.ValueTypeNames.Int, value=123456) attr = prim.GetAttribute('primvars:test_int') self.assertTrue(attr.IsValid()) self.assertEqual(attr.Get(), 123456) # try and change using bool omni.kit.commands.execute("PrimVarCommand", prim_path=["/World/Cube"], prim_name="test_int", prim_type=Sdf.ValueTypeNames.Bool, value=True) attr = prim.GetAttribute('primvars:test_int') self.assertTrue(attr.IsValid()) self.assertEqual(attr.Get(), 123456) # change primvar omni.kit.commands.execute("PrimVarCommand", prim_path=["/World/Cube"], prim_name="test_int", prim_type=Sdf.ValueTypeNames.Int, value=654321) attr = prim.GetAttribute('primvars:test_int') self.assertTrue(attr.IsValid()) self.assertEqual(attr.Get(), 654321) # undo omni.kit.undo.undo() omni.kit.undo.undo() omni.kit.undo.undo() # verify undo removed primvar attr = prim.GetAttribute('primvars:test_int') self.assertFalse(attr.IsValid()) # create primvar as bool omni.kit.commands.execute("PrimVarCommand", prim_path=["/World/Cube"], prim_name="test_bool", prim_type=Sdf.ValueTypeNames.Bool, value=True) attr = prim.GetAttribute('primvars:test_bool') self.assertTrue(attr.IsValid()) self.assertEqual(attr.Get(), True) # try and change using int omni.kit.commands.execute("PrimVarCommand", prim_path=["/World/Cube"], prim_name="test_bool", prim_type=Sdf.ValueTypeNames.Int, value=123456) attr = prim.GetAttribute('primvars:test_bool') self.assertTrue(attr.IsValid()) self.assertEqual(attr.Get(), True) # change primvar omni.kit.commands.execute("PrimVarCommand", prim_path=["/World/Cube"], prim_name="test_bool", prim_type=Sdf.ValueTypeNames.Bool, value=False) attr = prim.GetAttribute('primvars:test_bool') self.assertTrue(attr.IsValid()) self.assertEqual(attr.Get(), False) # undo omni.kit.undo.undo() omni.kit.undo.undo() omni.kit.undo.undo() # verify undo removed primvar attr = prim.GetAttribute('primvars:test_bool') self.assertFalse(attr.IsValid()) async def test_command_toggle_prim_var(self): stage = omni.usd.get_context().get_stage() prim = stage.GetPrimAtPath("/World/Cube") attr = prim.GetAttribute('primvars:test_bool') self.assertFalse(attr.IsValid()) # create primvar as bool omni.kit.commands.execute("TogglePrimVarCommand", prim_path=["/World/Cube"], prim_name="test_bool") attr = prim.GetAttribute('primvars:test_bool') self.assertTrue(attr.IsValid()) self.assertEqual(attr.Get(), True) # try and change using int omni.kit.commands.execute("PrimVarCommand", prim_path=["/World/Cube"], prim_name="test_bool", prim_type=Sdf.ValueTypeNames.Int, value=123456) attr = prim.GetAttribute('primvars:test_bool') self.assertTrue(attr.IsValid()) self.assertEqual(attr.Get(), True) # change primvar omni.kit.commands.execute("TogglePrimVarCommand", prim_path=["/World/Cube"], prim_name="test_bool") attr = prim.GetAttribute('primvars:test_bool') self.assertTrue(attr.IsValid()) self.assertEqual(attr.Get(), False) # undo omni.kit.undo.undo() omni.kit.undo.undo() omni.kit.undo.undo() # verify undo removed primvar attr = prim.GetAttribute('primvars:test_bool') self.assertFalse(attr.IsValid()) async def test_command_toggle_instanceable(self): stage = omni.usd.get_context().get_stage() prim = stage.GetPrimAtPath("/World/Cube") self.assertFalse(prim.IsInstanceable()) # toggle instanceable omni.kit.commands.execute("ToggleInstanceableCommand", prim_path=["/World/Cube"]) self.assertTrue(prim.IsInstanceable()) # toggle instanceable omni.kit.commands.execute("ToggleInstanceableCommand", prim_path=["/World/Cube"]) self.assertFalse(prim.IsInstanceable()) # undo omni.kit.undo.undo() omni.kit.undo.undo() omni.kit.undo.undo() # verify undo self.assertFalse(prim.IsInstanceable())
5,718
Python
38.171233
149
0.659671
omniverse-code/kit/exts/omni.kit.property.geometry/docs/CHANGELOG.md
# Changelog The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [1.2.2] - 2022-09-27 ### Changes - Changed primvars:numSplits* text ## [1.2.1] - 2022-05-13 ### Changes - Cleaned up ImageWithProvider vs Image usage ## [1.2.0] - 2021-05-31 ### Added - Added extent regeneration on size/radius/axis changes ## [1.1.0] - 2021-03-19 ### Added - Added soft range [0, 5] for refinementLevel. ## [1.0.7] - 2021-02-19 ### Changes - Added UI test ## [1.0.6] - 2020-12-09 ### Changes - Added extension icon - Added readme - Updated preview image ## [1.0.5] - 2020-11-20 ### Changes - Silenced unknown kind warning ## [1.0.4] - 2020-11-06 ### Changes - Update Kind to use metadata model ## [1.0.3] - 2020-10-27 ### Changes - Fixed spacing on kind widget ## [1.0.2] - 2020-10-22 ### Changes - Improved layout ## [1.0.1] - 2020-10-22 ### Changes - Moved schema into bundle ## [1.0.0] - 2020-10-05 ### Changes - Сreated
950
Markdown
16.611111
80
0.636842
omniverse-code/kit/exts/omni.kit.property.geometry/docs/README.md
# omni.kit.property.geometry ## Introduction Property window extensions are for viewing and editing Usd Prim Attributes ## This extension supports editing of these Usd Types; - UsdGeom.BasisCurves - UsdGeom.Capsule - UsdGeom.Cone - UsdGeom.Cube - UsdGeom.Cylinder - UsdGeom.HermiteCurves - UsdGeom.Mesh - UsdGeom.NurbsCurves - UsdGeom.NurbsPatch - UsdGeom.PointInstancer - UsdGeom.Points - UsdGeom.Subset - UsdGeom.Sphere - UsdGeom.Xform - UsdGeom.Gprim - UsdGeom.PointBased - UsdGeom.Boundable - UsdGeom.Curves - UsdGeom.Imageable - UsdGeom.PointBased - UsdUI.Backdrop ### and supports editing of these Usd APIs; - UsdGeom.ModelAPI - UsdGeom.MotionAPI - UsdGeom.PrimvarsAPI - UsdGeom.XformCommonAPI - UsdGeom.ModelAPI - UsdUI.NodeGraphNodeAPI - UsdUI.SceneGraphPrimAPI
777
Markdown
17.975609
74
0.788932
omniverse-code/kit/exts/omni.kit.property.geometry/docs/index.rst
omni.kit.property.geometry ########################### Property Geometry Values .. toctree:: :maxdepth: 1 CHANGELOG
127
reStructuredText
9.666666
27
0.551181
omniverse-code/kit/exts/omni.kit.viewport.menubar.display/omni/kit/viewport/menubar/display/style.py
from pathlib import Path CURRENT_PATH = Path(__file__).parent ICON_PATH = CURRENT_PATH.parent.parent.parent.parent.parent.joinpath("data").joinpath("icons") UI_STYLE = {"Menu.Item.Icon::Display": {"image_url": f"{ICON_PATH}/viewport_visibility.svg"}}
253
Python
35.285709
94
0.727273
omniverse-code/kit/exts/omni.kit.viewport.menubar.display/omni/kit/viewport/menubar/display/extension.py
# Copyright (c) 2021-2022, 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. # __all__ = ["ViewportDisplayMenuBarExtension", "get_instance"] from typing import Union from omni.kit.viewport.menubar.core import BaseCategoryItem from .display_menu_container import DEFAULT_SECTION, DisplayMenuContainer import omni.ext _extension_instance = None def get_instance(): global _extension_instance return _extension_instance class ViewportDisplayMenuBarExtension(omni.ext.IExt): """The Entry Point for the Display Settings in Viewport Menu Bar""" def on_startup(self, ext_id): self._display_menu = DisplayMenuContainer() global _extension_instance _extension_instance = self def on_shutdown(self): self._display_menu.destroy() self._display_menu = None global _extension_instance _extension_instance = None def register_custom_setting(self, text: str, setting_path: str): """ Register custom display setting. Args: text (str): Text shown in menu item. setting_path (str): Setting path for custom display setting (bool value). """ if self._display_menu: self._display_menu.register_custom_setting(text, setting_path) def deregister_custom_setting(self, text: str): """ Deregister custom display setting. Args: text (str): Text shown in menu item. """ if self._display_menu: self._display_menu.deregister_custom_setting(text) def register_custom_category_item(self, category: str, item: BaseCategoryItem, section: str = DEFAULT_SECTION): """ Register custom display setting in category. Args: category (str): Category to add menu item. Can be an existing category e.g. "Heads Up Display" or a new one. item (item: BaseCategoryItem): Item to append. section (str): Optional section to organise category, default no section. """ if self._display_menu: self._display_menu.register_custom_category_item(category, item, section) def deregister_custom_category_item(self, category: str, item: BaseCategoryItem): """ Deregister custom display setting in category. Args: category (str): Category to remove menu item. Can be an existing category e.g. "Heads Up Display" or a new one. item (item: BaseCategoryItem): Item to remove. """ if self._display_menu: self._display_menu.deregister_custom_category_item(category, item)
2,971
Python
36.15
123
0.672164
omniverse-code/kit/exts/omni.kit.viewport.menubar.display/omni/kit/viewport/menubar/display/model.py
import omni.ui as ui class DisplayLayerModel(ui.SimpleBoolModel): def __init__(self, layer) -> None: self._layer = layer super().__init__() def get_value_as_bool(self) -> bool: return self._layer.visible def set_value(self, visible: bool): if visible != self._layer.visible: self._layer.visible = visible self._value_changed() def begin_edit(self) -> None: pass def end_edit(self) -> None: pass
493
Python
21.454544
44
0.56998
omniverse-code/kit/exts/omni.kit.viewport.menubar.display/omni/kit/viewport/menubar/display/display_menu_container.py
# Copyright (c) 2021-2022, 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. # __all__ = ["DisplayMenuContainer"] from omni.kit.viewport.menubar.core import ( IconMenuDelegate, SettingModel, ViewportMenuContainer, CategoryMenuContainer, SelectableMenuItem, SimpleCategoryModel, CategoryStateItem, BaseCategoryItem, CategoryCustomItem, CategoryCollectionItem ) from .style import UI_STYLE import carb import carb.settings import omni.ui as ui import omni.kit.app import omni.usd from functools import partial from typing import Dict, List, Optional SHOW_BY_TYPE_EXCLUDE_LIST = "/exts/omni.kit.viewport.menubar.display/showByType/exclude_list" HEADS_UP_CATEGORY_NAME = "Heads Up Display" SHOW_BY_TYPE_CATEGORY_NAME = "Show By Type" SHOW_BY_PURPOSE_CATEGORY_NAME = "Show By Purpose" DEFAULT_CATEGORIES = [HEADS_UP_CATEGORY_NAME, SHOW_BY_TYPE_CATEGORY_NAME, SHOW_BY_PURPOSE_CATEGORY_NAME] DEFAULT_SECTION = "default" def _make_viewport_setting(viewport_api_id: str, setting: str): return f"/persistent/app/viewport/{viewport_api_id}/{setting}/visible" class DisplayMenuContainer(ViewportMenuContainer): """The menu with the visibility settings""" def __init__(self): super().__init__( name="Display", delegate=IconMenuDelegate("Display"), visible_setting_path="/exts/omni.kit.viewport.menubar.display/visible", order_setting_path="/exts/omni.kit.viewport.menubar.display/order", style=UI_STYLE ) self._root_menu: Optional[ui.Menu] = None self._category_models: Dict[str, SimpleCategoryModel] = {} self._custom_settings: List[List[str, str]] = [] self._custom_category_items: Dict[str, List[BaseCategoryItem]] = {} self._section_categories: Dict[str, List[str]] = {} self._section_categories[DEFAULT_SECTION] = DEFAULT_CATEGORIES[:] # Copy the default categories list def destroy(self): super().destroy() def register_custom_setting(self, text: str, setting_path: str): self._custom_settings.append((text, setting_path)) if self._root_menu: self._root_menu.invalidate() def deregister_custom_setting(self, text: str): found = [item for item in self._custom_settings if item[0] == text] if found: for item in found: self._custom_settings.remove(item) if self._root_menu: self._root_menu.invalidate() def register_custom_category_item(self, category: str, item: BaseCategoryItem, section: str): is_top_category = False if category not in DEFAULT_CATEGORIES and category not in self._category_models: if item.text == category and isinstance(item, CategoryCollectionItem): self._category_models[category] = SimpleCategoryModel(category, root=item) is_top_category = True else: self._category_models[category] = SimpleCategoryModel(category) if category not in self._custom_category_items: self._custom_category_items[category] = [] if section not in self._section_categories: self._section_categories[section] = [] if not is_top_category: self._custom_category_items[category].append(item) if category not in self._section_categories[section]: self._section_categories[section].append(category) if self._root_menu: self._root_menu.invalidate() def deregister_custom_category_item(self, category: str, item: BaseCategoryItem): if category in self._custom_category_items: if item in self._custom_category_items[category]: self._custom_category_items[category].remove(item) if category not in DEFAULT_CATEGORIES: if (item.text == category and isinstance(item, CategoryCollectionItem)) or len(self._custom_category_items[category]) == 0: del self._category_models[category] # Now clean up section sections = list(self._section_categories.keys()) for section in sections: if category in self._section_categories[section]: self._section_categories[section].remove(category) if len(self._section_categories[section]) == 0: del self._section_categories[section] if self._root_menu: self._root_menu.invalidate() def build_fn(self, viewport_context: dict): self._root_menu = ui.Menu(self.name, delegate=self._delegate, on_build_fn=partial(self._build_menu_items, viewport_context), style=self._style) def _build_menu_items(self, viewport_context: dict, *args, **kwargs): viewport = viewport_context.get("viewport_api") viewport_api_id: str = str(viewport.id) settings = carb.settings.get_settings() show_by_type_items: list[BaseCategoryItem] = [ CategoryStateItem("Cameras", setting_path=_make_viewport_setting(viewport_api_id, "scene/cameras")), CategoryStateItem("Lights", setting_path=_make_viewport_setting(viewport_api_id, "scene/lights")), CategoryStateItem("Skeletons", setting_path=_make_viewport_setting(viewport_api_id, "scene/skeletons")), CategoryStateItem("Audio", setting_path=_make_viewport_setting(viewport_api_id, "scene/audio")), ] if (exclude_list := settings.get(SHOW_BY_TYPE_EXCLUDE_LIST)): show_by_type_items = [item for item in show_by_type_items if item.text not in exclude_list] # 105.1: Support alternate label of memory (i.e. "Host Memory", "Process Memory", "Memory") # Defaults to pre 105.1 label (Host Memory) when not specified mem_label = settings.get("/exts/omni.kit.viewport.window/hud/hostMemory/label") if mem_label is None: mem_label = "Host" default_category_models = { HEADS_UP_CATEGORY_NAME: SimpleCategoryModel( HEADS_UP_CATEGORY_NAME, [ CategoryStateItem("FPS", setting_path=_make_viewport_setting(viewport_api_id, "hud/renderFPS")), CategoryStateItem("Device Memory", setting_path=_make_viewport_setting(viewport_api_id, "hud/deviceMemory")), CategoryStateItem(f"{mem_label} Memory", setting_path=_make_viewport_setting(viewport_api_id, "hud/hostMemory")), CategoryStateItem("Resolution", setting_path=_make_viewport_setting(viewport_api_id, "hud/renderResolution")), CategoryStateItem("Progress", setting_path=_make_viewport_setting(viewport_api_id, "hud/renderProgress")), ] ), SHOW_BY_TYPE_CATEGORY_NAME: SimpleCategoryModel( SHOW_BY_TYPE_CATEGORY_NAME, show_by_type_items ), SHOW_BY_PURPOSE_CATEGORY_NAME: SimpleCategoryModel( SHOW_BY_PURPOSE_CATEGORY_NAME, [ CategoryStateItem("Guide", setting_path="/persistent/app/hydra/displayPurpose/guide"), CategoryStateItem("Proxy", setting_path="/persistent/app/hydra/displayPurpose/proxy"), CategoryStateItem("Render", setting_path="/persistent/app/hydra/displayPurpose/render"), ] ) } self._category_models.update(default_category_models) # XXX: These add_item calls currently must occur to add the separator! self._category_models[SHOW_BY_TYPE_CATEGORY_NAME].add_item(CategoryCustomItem( "Meshes", lambda: SelectableMenuItem("Meshes", SettingModel(setting_path=_make_viewport_setting(viewport_api_id, "scene/meshes"))) )) self._category_models[HEADS_UP_CATEGORY_NAME].add_item(CategoryCustomItem( "Camera Speed", lambda: SelectableMenuItem("Camera Speed", SettingModel(_make_viewport_setting(viewport_api_id, "hud/cameraSpeed"))) )) identifier = "omni.kit.viewport.menubar.display" # Create default section categories first for name in self._section_categories[DEFAULT_SECTION]: model = self._category_models[name] if name in self._custom_category_items: for item in self._custom_category_items[name]: model.add_item(item) # XXX: Workaround nested creation of these items not being able to trigger an action! trigger_fns = None if name == SHOW_BY_TYPE_CATEGORY_NAME: icon_click_id = f"{identifier}.{name}.{name}" # Left-most check/mixed icon was toggled trigger_fns = { "Cameras": partial(self.__trigger_action, "toggle_camera_visibility", viewport_api=viewport), "Lights": partial(self.__trigger_action, "toggle_light_visibility", viewport_api=viewport), "Skeletons": partial(self.__trigger_action, "toggle_skeleton_visibility", viewport_api=viewport), "Audio": partial(self.__trigger_action, "toggle_audio_visibility", viewport_api=viewport), "Meshes": partial(self.__trigger_action, "toggle_mesh_visibility", viewport_api=viewport), icon_click_id: partial(self.__trigger_action, "toggle_show_by_type_visibility", viewport_api=viewport), } CategoryMenuContainer(model, identifier=f"{identifier}.{name}", trigger_fns=trigger_fns) # Now iterate named sections, with a separator for each. for section, categories in self._section_categories.items(): if section is DEFAULT_SECTION: continue ui.Separator(text=section) for name in categories: model = self._category_models[name] if name in self._custom_category_items: for item in self._custom_category_items[name]: model.add_item(item) CategoryMenuContainer(model, identifier=f"{identifier}.{name}") ui.Separator() # This currently is just easier tied to legacy global setting SelectableMenuItem("Selection Outline", SettingModel(_make_viewport_setting(viewport_api_id, "guide/selection")), triggered_fn=partial(self.__trigger_action, "toggle_selection_hilight_visibility", viewport_api=viewport), trigger_will_set_model=True ) SelectableMenuItem("Axis", SettingModel(_make_viewport_setting(viewport_api_id, "guide/axis")), triggered_fn=partial(self.__trigger_action, "toggle_axis_visibility", viewport_api=viewport), trigger_will_set_model=True ) SelectableMenuItem("Grid", SettingModel(_make_viewport_setting(viewport_api_id, "guide/grid")), triggered_fn=partial(self.__trigger_action, "toggle_grid_visibility", viewport_api=viewport), trigger_will_set_model=True ) # Custom display settings if self._custom_settings: ui.Separator() for (text, setting_path) in self._custom_settings: SelectableMenuItem(text, SettingModel(setting_path)) def __trigger_action(self, action: str, *args, **kwargs): import omni.kit.actions.core action_registry = omni.kit.actions.core.get_action_registry() if action_registry: exc_action = action_registry.get_action("omni.kit.viewport.actions", action) if exc_action: exc_action.execute(*args, **kwargs) else: carb.log_error(f"Could not find action to run: '{action}'") else: carb.log_error(f"Could not get action_registry to run '{action}")
12,371
Python
47.140078
135
0.629941
omniverse-code/kit/exts/omni.kit.viewport.menubar.display/omni/kit/viewport/menubar/display/tests/test_ui.py
import omni.kit.test from re import I from omni.ui.tests.test_base import OmniUiTest import omni.kit.ui_test as ui_test from omni.kit.ui_test import Vec2 import omni.usd import omni.kit.app from pathlib import Path import carb.input import asyncio import omni.ui as ui from omni.kit.viewport.menubar.core import CategoryCollectionItem, CategoryStateItem, CategoryCustomItem, ViewportMenuDelegate, SelectableMenuItem CURRENT_PATH = Path(__file__).parent TEST_DATA_PATH = CURRENT_PATH.parent.parent.parent.parent.parent.parent.joinpath("data").joinpath("tests") TEST_WIDTH, TEST_HEIGHT = 600, 400 TEST_SETTING_TRUE = "/exts/test/setting/true" TEST_SETTING_FALSE = "/exts/test/setting/false" class TestSettingMenuWindow(OmniUiTest): async def setUp(self): self._golden_img_dir = TEST_DATA_PATH.absolute().joinpath("golden_img").absolute() await self.create_test_area(width=TEST_WIDTH, height=TEST_HEIGHT) await omni.kit.app.get_app().next_update_async() async def test_general(self): await self._show_display_menu("menubar_display.png", None) async def test_heads_up(self): await self._show_display_menu("menubar_display_headsup.png", 86) async def test_show_by_type(self): await self._show_display_menu("menubar_display_show_type.png", 106) async def test_show_by_purpose(self): await self._show_display_menu("menubar_display_show_purpose.png", 126) async def test_show_custom_menu_item(self): inst = omni.kit.viewport.menubar.display.get_instance() custom_collection_item = CategoryCollectionItem( "Custom catetory", [ CategoryStateItem("Custom Item", ui.SimpleBoolModel(True)), ] ) inst.register_custom_category_item("Show By Type", custom_collection_item) def _build_menu(): with ui.Menu("Physics", delegate=ViewportMenuDelegate()): SelectableMenuItem("Joints", ui.SimpleBoolModel(True)) with ui.Menu("Colliders", delegate=ViewportMenuDelegate()): SelectableMenuItem("None", ui.SimpleBoolModel(True)) SelectableMenuItem("Selected", ui.SimpleBoolModel(False)) SelectableMenuItem("All", ui.SimpleBoolModel(False)) ui.Separator() SelectableMenuItem("Normals", ui.SimpleBoolModel(False)) physics_item = CategoryCustomItem("Physics", _build_menu) inst.register_custom_category_item("Show By Type", physics_item) settings = carb.settings.get_settings() settings.set(TEST_SETTING_FALSE, False) settings.set(TEST_SETTING_TRUE, True) inst.register_custom_setting("test new setting (True)", TEST_SETTING_TRUE) inst.register_custom_setting("test new setting (False)", TEST_SETTING_FALSE) await omni.kit.app.get_app().next_update_async() await self._show_display_menu("menubar_display_custom.png", 106) inst.deregister_custom_category_item("Show By Type", custom_collection_item) inst.deregister_custom_category_item("Show By Type", physics_item) inst.deregister_custom_setting("test new setting (True)") inst.deregister_custom_setting("test new setting (False)") await omni.kit.app.get_app().next_update_async() async def test_show_custom_category_and_section(self): inst = omni.kit.viewport.menubar.display.get_instance() category = "Draw Overlay" section = "Selection Display" did_shown_changed_callback = False def on_shown(s): print("on_shown: {s}") nonlocal did_shown_changed_callback did_shown_changed_callback = True overlay_item = CategoryCollectionItem( category, [ CategoryCustomItem("Points", lambda: SelectableMenuItem("Points", model=ui.SimpleBoolModel())), CategoryCustomItem("Normals", lambda: SelectableMenuItem("Normals", model=ui.SimpleBoolModel())) ], shown_changed_fn=on_shown ) inst.register_custom_category_item(category, overlay_item, section) await omni.kit.app.get_app().next_update_async() await self._show_display_menu("menubar_display_custom_category_and_section.png", 166) self.assertTrue(did_shown_changed_callback) inst.deregister_custom_category_item(category, overlay_item) await omni.kit.app.get_app().next_update_async() async def _show_display_menu(self, golden_img_name: str, y: int = None) -> None: # Enable mouse input app_window = omni.appwindow.get_default_app_window() for device in [carb.input.DeviceType.MOUSE]: app_window.set_input_blocking_state(device, None) try: await ui_test.emulate_mouse_move(Vec2(20, 46), human_delay_speed=4) await ui_test.emulate_mouse_click() if y is not None: await ui_test.emulate_mouse_move(Vec2(20, y)) await self.finalize_test(golden_img_dir=self._golden_img_dir, golden_img_name=golden_img_name) finally: for i in range(3): await omni.kit.app.get_app().next_update_async() await ui_test.emulate_mouse_move(Vec2(300, 26)) await ui_test.emulate_mouse_click() for i in range(3): await omni.kit.app.get_app().next_update_async()
5,475
Python
39.865671
146
0.652603
omniverse-code/kit/exts/omni.kit.usdz_export/config/extension.toml
[package] title = "USDZ Exporter" description = "Packages assets into a USDZ archive." authors = ["NVIDIA"] version = "1.0.1" changelog="docs/CHANGELOG.md" preview_image = "data/preview.png" readme = "docs/README.md" #icon = "data/icon.png" category = "Internal" feature = true [[python.module]] name = "omni.kit.usdz_export" [dependencies] "omni.kit.pip_archive" = {} "omni.ui" = {} "omni.usd" = {} "omni.usd.libs" = {} "omni.kit.tool.collect" = {} "omni.kit.window.file_exporter" = {} # Additional python module with tests, to make them discoverable by test system. [[python.module]] name = "omni.kit.usdz_export.tests" [[test]] args = [ "--/app/asyncRendering=false", "--/rtx/materialDb/syncLoads=true", "--/omni.kit.plugin/syncUsdLoads=true", "--/rtx/hydra/materialSyncLoads=true" ] dependencies = [ "omni.kit.material.library", ]
861
TOML
21.102564
80
0.671312
omniverse-code/kit/exts/omni.kit.usdz_export/omni/kit/usdz_export/extension_usdz.py
# Copyright (c) 2018-2020, 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. # from .layers_menu import layers_available from .layers_menu import LayersMenu import omni.ext import omni.kit.app class UsdzExportExtension(omni.ext.IExt): def on_startup(self, ext_id): # Setup a callback for the event app = omni.kit.app.get_app_interface() ext_manager = app.get_extension_manager() self.__extensions_subscription = ext_manager.get_change_event_stream().create_subscription_to_pop( self._on_event, name="omni.kit.usdz_export" ) self.__layers_menu = None self._on_event(None) def _on_event(self, event): # Create/destroy the menu in the Layers window if self.__layers_menu: if not layers_available(): self.__layers_menu.destroy() self.__layers_menu = None else: if layers_available(): self.__layers_menu = LayersMenu() def on_shutdown(self): self.__extensions_subscription = None if self.__layers_menu: self.__layers_menu.destroy() self.__layers_menu = None
1,530
Python
33.795454
106
0.65817
omniverse-code/kit/exts/omni.kit.usdz_export/omni/kit/usdz_export/layers_menu.py
# Copyright (c) 2018-2020, 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. # from .utils import is_extension_loaded, copy, list_folder_async from pxr import Sdf, Usd from pathlib import Path from zipfile import ZipFile from functools import partial from typing import Callable, List from omni.kit.window.file_exporter import get_file_exporter from omni.kit.widget.prompt import PromptManager import carb import omni.kit.tool.collect as collect import omni.usd import asyncio import tempfile import os import shutil import omni.kit.app import omni.kit.notification_manager as nm def layers_available() -> bool: """Returns True if the extension "omni.kit.widget.layers" is loaded""" return is_extension_loaded("omni.kit.widget.layers") async def usdz_export(identifier, export_path): try: target_out = export_path carb.log_info(f"Starting to export layer '{identifier}' to '{target_out}'") prompt = PromptManager.post_simple_prompt("Please Wait", "Exporting to USDZ...", ok_button_info=None, modal=True) # Waits for prompt to be shown await omni.kit.app.get_app().next_update_async() await omni.kit.app.get_app().next_update_async() layer = Sdf.Layer.FindOrOpen(identifier) if not layer: message = f"Failed to export layer {identifier} as it does not exist." carb.log_error(message) nm.post_notification(message, status=nm.NotificationStatus.WARNING) return with tempfile.TemporaryDirectory() as tmp_path: tmp_path = Path(tmp_path) collect_path = tmp_path.joinpath("collected") split_ext = os.path.splitext(identifier) # Can't collect USDZ files because MDLs can't be resolved if (split_ext[1] == '.usdz'): input_usdz_temp_path = str(tmp_path.joinpath('temp_copy.usdz')) await copy(identifier, str(input_usdz_temp_path)) with ZipFile(input_usdz_temp_path, 'r') as zip_ref: zip_ref.extractall(str(tmp_path)) tmp_file_path = str(tmp_path.joinpath("main.usdc")) layer.Export(tmp_file_path) entry_layer_to_collect = tmp_file_path elif not omni.usd.is_usd_writable_filetype(identifier) or identifier.startswith('anon'): tmp_file_path = str(tmp_path.joinpath("main.usdc")) layer.Export(tmp_file_path) entry_layer_to_collect = tmp_file_path else: entry_layer_to_collect = identifier collector = collect.Collector(entry_layer_to_collect, str(collect_path), flat_collection=True) await collector.collect(None, None) # must create USDZ locally because the UsdUtils package cannot handle omniverse:// URIs absolute_paths, relative_paths = await list_folder_async(str(collect_path)) local_out_path = collect_path.joinpath("local_out.usdz") # Create usdz package manually without using USD API as it cannot handle UDIM textures. zip_writer = Usd.ZipFileWriter.CreateNew(str(local_out_path)) with zip_writer: for absolute_path, relative_path in zip(absolute_paths, relative_paths): url = omni.client.break_url(absolute_path) absolute_path = url.path # FIXME: omni.client will return windows path prefixed with '/' if os.name == "nt" and absolute_path[0] == '/': absolute_path = absolute_path[1:] zip_writer.AddFile(absolute_path, relative_path) await copy(str(local_out_path), target_out) layer = None zip_writer = None finally: prompt.visible = False prompt = None carb.log_info(f"Finished exporting layer '{identifier}' to '{target_out}'") def export(objects): """Export the target layer to USDZ""" def on_export(callback: Callable, flatten: bool, filename: str, dirname: str, extension: str = '', selections: List[str] = []): nonlocal objects path = f"{dirname}/{filename}{extension}" item = objects["item"] identifier = item().identifier asyncio.ensure_future(usdz_export(identifier, path)) file_picker = get_file_exporter() file_picker.show_window( title="Export To USDZ", export_button_label="Export", export_handler=partial(on_export, None, False), file_extension_types=[(".usdz", "Zipped package")] ) class LayersMenu: """ When this object is alive, Layers 2.0 has an additional action for exporting the layer to USDZ. """ def __init__(self): import omni.kit.widget.layers as layers self.__menu_subscription = layers.ContextMenu.add_menu( [ {"name": ""}, { "name": "Export USDZ", "glyph": "menu_rename.svg", "show_fn": [ layers.ContextMenu.is_layer_item, layers.ContextMenu.is_not_missing_layer, layers.ContextMenu.is_layer_not_locked_by_other, layers.ContextMenu.is_layer_and_parent_unmuted ], "onclick_fn": export, } ] ) def destroy(self): """Remove the menu from Layers 2.0""" self.__menu_subscription = None
5,903
Python
38.891892
131
0.614942
omniverse-code/kit/exts/omni.kit.usdz_export/omni/kit/usdz_export/utils.py
# Copyright (c) 2018-2020, 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. # import os import omni.kit.app import traceback import carb import omni.client import omni.client.utils as clientutils def is_extension_loaded(extansion_name: str) -> bool: """ Returns True if the extension with the given name is loaded. """ def is_ext(id: str, extension_name: str) -> bool: id_name = id.split("-")[0] return id_name == extension_name app = omni.kit.app.get_app_interface() ext_manager = app.get_extension_manager() extensions = ext_manager.get_extensions() loaded = next((ext for ext in extensions if is_ext(ext["id"], extansion_name) and ext["enabled"]), None) return not not loaded async def copy(src_path: str, dest_path: str): carb.log_info(f"Copying from {src_path} to {dest_path}...") try: result = await omni.client.copy_async(src_path, dest_path, omni.client.CopyBehavior.OVERWRITE) if result != omni.client.Result.OK: carb.log_error(f"Cannot copy from {src_path} to {dest_path}, error code: {result}.") return False else: return True except Exception as e: traceback.print_exc() carb.log_error(str(e)) return False async def list_folder_async(folder_path): def compute_absolute_path(base_path, is_base_path_folder, path, is_path_folder): if is_base_path_folder and not base_path.endswith("/"): base_path += "/" if is_path_folder and not path.endswith("/"): path += "/" return clientutils.make_absolute_url_if_possible(base_path, path) def remove_prefix(text, prefix): if text.startswith(prefix): return text[len(prefix) :] return text absolute_paths = [] relative_paths = [] result, entry = await omni.client.stat_async(folder_path) if result == omni.client.Result.OK and entry.flags & omni.client.ItemFlags.CAN_HAVE_CHILDREN: is_folder = True else: is_folder = False folder_path = clientutils.make_file_url_if_possible(folder_path) if not is_folder: absolute_paths = [folder_path] relative_paths = [os.path.basename(folder_path)] else: if not folder_path.endswith("/"): folder_path += "/" folder_queue = [folder_path] while len(folder_queue) > 0: folder = folder_queue.pop(0) (result, entries) = await omni.client.list_async(folder) if result != omni.client.Result.OK: break folders = set((e.relative_path for e in entries if e.flags & omni.client.ItemFlags.CAN_HAVE_CHILDREN)) for f in folders: folder_queue.append(compute_absolute_path(folder, True, f, False)) files = set((e.relative_path for e in entries if not e.flags & omni.client.ItemFlags.CAN_HAVE_CHILDREN)) for file in files: absolute_path = compute_absolute_path(folder, True, file, False) absolute_paths.append(absolute_path) relative_path = remove_prefix(absolute_path, folder_path[:-1]) relative_path = relative_path.replace("\\", "/") if relative_path != "/" and relative_path.startswith("/"): relative_path = relative_path[1:] if len(relative_path) > 0: relative_paths.append(relative_path) return absolute_paths, relative_paths
3,859
Python
36.115384
116
0.63177
omniverse-code/kit/exts/omni.kit.usdz_export/omni/kit/usdz_export/tests/usdz_export_test.py
## 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. ## from omni.ui.tests.test_base import OmniUiTest from pathlib import Path from pxr import Usd from pxr import UsdGeom import carb import omni.client import omni.kit import omni.usd import os import time import unittest from omni.kit.usdz_export import usdz_export OMNI_SERVER = "omniverse://ov-test" class TestUsdzExport(OmniUiTest): def get_test_dir(self): token = carb.tokens.get_tokens_interface() data_dir = token.resolve("${data}") return f"{data_dir}" async def test_export_usdz_file(self): usdz_size = 2600000 usdz_size_tc = 2675966 current_path = Path(__file__) test_data_path = current_path.parent.parent.parent.parent.parent.joinpath("data") test_stage_path = str(test_data_path.joinpath("test_stage").joinpath("scene.usd")) test_dir = self.get_test_dir() export_file_path = Path(test_dir).joinpath("out.usdz").resolve() await usdz_export(test_stage_path, export_file_path.__str__()) self.assertTrue(os.path.isfile(export_file_path.__str__()), 'out.usdz does not exist') size = os.stat(export_file_path).st_size self.assertTrue(size >= usdz_size and size <= usdz_size_tc, f'File size mismatch, expected {usdz_size} but got {size}')
1,702
Python
36.844444
127
0.706228
omniverse-code/kit/exts/omni.kit.usdz_export/docs/CHANGELOG.md
# Changelog ## [1.0.1] - 2022-11-08 - Add "omni.kit.window.file_exporter" as dependency. ## [1.0.0] - 2022-08-18 - Initial extension.
137
Markdown
14.333332
52
0.635036
omniverse-code/kit/exts/omni.kit.usdz_export/docs/README.md
# USDZ Exporter [omni.kit.usdz_export] Exports selected layer to a USDZ archive.
83
Markdown
15.799997
41
0.759036
omniverse-code/kit/fabric/include/carb/flatcache/IToken.h
// Copyright (c) 2019-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. // #pragma once #include <carb/Interface.h> #ifndef __CUDACC__ // InterfaceUtils.h provides carb::getCachedInterface and is not CUDA-compatible #include <carb/InterfaceUtils.h> #endif // __CUDACC__ // Set to empty macro when IToken::iToken static member is removed #define FLATCACHE_ITOKEN_INIT \ const carb::flatcache::IToken* carb::flatcache::iToken = nullptr; namespace carb { namespace flatcache { // TokenC are integer keys that identify paths to C-ABI interfaces struct TokenC { uint64_t token; // Note that in the name comparisons below we mask off USD's lifetime bit. // For example, tokens created from the same string are considered equal even // if one was created with finite lifetime and the other infinite lifetime. constexpr bool operator<(const TokenC& other) const { return (token & ~1) < (other.token & ~1); } constexpr bool operator==(const TokenC& other) const { return (token & ~1) == (other.token & ~1); } constexpr bool operator!=(const TokenC& other) const { return (token & ~1) != (other.token & ~1); } }; static_assert(std::is_standard_layout<TokenC>::value, "Struct must be standard layout as it is used in C-ABI interfaces"); // We don't reference count the uninitialized (or empty) token, and we use // this fact to avoid unnecessary dll calls to addRef()/removeRef(), for // example during std::vector resize. To do this we need to check whether a // token is uninitialized without the dll call getEmptyToken(), so we store // its value here in a constant. // We run automated test "IToken::getEmptyToken() dll call can be replaced with // constant, kUninitializedToken" to ensure that this constant never // changes. static constexpr TokenC kUninitializedToken{0}; // C-ABI interface to pxr::TfToken struct IToken { CARB_PLUGIN_INTERFACE("carb::flatcache::IToken", 0, 1); TokenC (*getHandle)(const char* name); const char* (*getText)(TokenC handle); void (*addRef)(TokenC handle); void (*removeRef)(TokenC handle); TokenC (*getEmptyToken)(); uint64_t (*size)(TokenC handle); }; // C++ wrapper for IToken class Token { static carb::flatcache::IToken& sIToken(); public: // DEPRECATED: keeping for binary compatibility // Will be removed in October 2021 - @TODO set FLATCACHE_ITOKEN_INIT to empty macro when removed! // Still safe to use if initialized in a given dll static const carb::flatcache::IToken* iToken; Token() : mHandle(kUninitializedToken) { } Token(const char* string) { mHandle = sIToken().getHandle(string); } // Needs to be noexcept for std::vector::resize() to move instead of copy ~Token() noexcept { #ifndef __CUDACC__ if (mHandle != kUninitializedToken) { if (!carb::isFrameworkValid()) { return; } // IToken can be nullptr durin exit process if (auto iToken = carb::getCachedInterface<carb::flatcache::IToken>()) { iToken->removeRef(mHandle); } } #endif // __CUDACC__ } // Copy constructor Token(const Token& other) : mHandle(other.mHandle) { if (mHandle != kUninitializedToken) { sIToken().addRef(mHandle); } } // Copy construct from integer Token(TokenC token) : mHandle(token) { if (mHandle != kUninitializedToken) { sIToken().addRef(mHandle); } } // Move constructor // Needs to be noexcept for std::vector::resize() to move instead of copy Token(Token&& other) noexcept { // We are moving the src handle so don't need to change its refcount mHandle = other.mHandle; // Make source invalid other.mHandle = kUninitializedToken; } // Copy assignment Token& operator=(const Token& other) { if (this != &other) { if (mHandle != kUninitializedToken) { sIToken().removeRef(mHandle); } mHandle = other.mHandle; if (other.mHandle != kUninitializedToken) { sIToken().addRef(mHandle); } } return *this; } // Move assignment Token& operator=(Token&& other) noexcept { if (&other == this) return *this; // We are about to overwrite the dest handle, so decrease its refcount if (mHandle != kUninitializedToken) { sIToken().removeRef(mHandle); } // We are moving the src handle so don't need to change its refcount mHandle = other.mHandle; other.mHandle = kUninitializedToken; return *this; } const char* getText() const { return sIToken().getText(mHandle); } uint64_t size() const { return sIToken().size(mHandle); } std::string getString() const { return std::string(sIToken().getText(mHandle), sIToken().size(mHandle)); } // Note that in the name comparisons below TokenC masks off USD's lifetime bit. // In other words, tokens created from the same string are considered equal even // if one was created with finite lifetime and the other infinite lifetime. constexpr bool operator<(const Token& other) const { return mHandle < other.mHandle; } constexpr bool operator!=(const Token& other) const { return mHandle != other.mHandle; } constexpr bool operator==(const Token& other) const { return mHandle == other.mHandle; } constexpr operator TokenC() const { return mHandle; } private: TokenC mHandle; }; static_assert(std::is_standard_layout<Token>::value, "Token must be standard layout as it is used in C-ABI interfaces"); #ifndef __CUDACC__ inline carb::flatcache::IToken& Token::sIToken() { // Acquire carbonite interface on first use carb::flatcache::IToken* iToken = carb::getCachedInterface<carb::flatcache::IToken>(); CARB_ASSERT(iToken); return *iToken; } #endif // __CUDACC__ inline uint64_t swapByteOrder(uint64_t val) { #if !CARB_COMPILER_MSC // Compilers other than MSVC tend to turn the following into a single instruction like bswap val = ((val & 0xFF00000000000000u) >> 56u) | ((val & 0x00FF000000000000u) >> 40u) | ((val & 0x0000FF0000000000u) >> 24u) | ((val & 0x000000FF00000000u) >> 8u) | ((val & 0x00000000FF000000u) << 8u) | ((val & 0x0000000000FF0000u) << 24u) | ((val & 0x000000000000FF00u) << 40u) | ((val & 0x00000000000000FFu) << 56u); #else // MSVC does not currently optimize the above code, so we have to use an intrinsic to get bswap val = _byteswap_uint64(val); #endif return val; } inline size_t hash(TokenC token) { size_t tokenWithoutMortalityBit = token.token & ~1; // The following Hash function was chosen to match the one in pxr\base\tf\hash.h // This is based on Knuth's multiplicative hash for integers. The // constant is the closest prime to the binary expansion of the inverse // golden ratio. The best way to produce a hash table bucket index from // the result is to shift the result right, since the higher order bits // have the most entropy. But since we can't know the number of buckets // in a table that's using this, we just reverse the byte order instead, // to get the highest entropy bits into the low-order bytes. return swapByteOrder(tokenWithoutMortalityBit * 11400714819323198549ULL); } inline size_t hash(Token const& token) { return hash(TokenC(token)); } } } namespace std { template <> struct hash<carb::flatcache::Token> { std::size_t operator()(const carb::flatcache::Token& key) const { return carb::flatcache::hash(key); } }; template <> class hash<carb::flatcache::TokenC> { public: size_t operator()(const carb::flatcache::TokenC& key) const { return carb::flatcache::hash(key); } }; }
8,572
C
27.768456
122
0.640457
omniverse-code/kit/fabric/include/carb/flatcache/Defines.h
// Copyright (c) 2019-2022, 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. // #pragma once // Improved #define preprocessor directives that support compile-time checking for mispelled or missing // directives. Basically, the same as #define MY_FEATURE 0/1, but with a bit more compile-time safety, // and ease of use around mixing or combining boolean logic. // // Example usage: // #define MY_FEATURE_A IN_USE // #define MY_FEATURE_B NOT_IN_USE // #define MY_FEATURE_C USE_IF( USING( MY_FEATURE_A ) && USING( MY_FEATURE_B ) ) // ... // void doStuff() // { // #if USING( MY_FEATURE_C ) // doStuff_C(); // #else // #if USING( MY_FEATURE_C ) // doStuff_NotC(); // #endif // #if USING( MY_FEATURE_C ) // } #define IN_USE && #define NOT_IN_USE &&! #define USE_IF(X) &&((X)?1:0)&& #define USING(X) (1 X 1) #ifndef NDEBUG #define DEVELOPMENT_BUILD IN_USE #else // #ifndef NDEBUG #define DEVELOPMENT_BUILD NOT_IN_USE #endif // #ifndef NDEBUG #ifdef _WIN32 #define WINDOWS_BUILD IN_USE #define LINUX_BUILD NOT_IN_USE #elif defined(__linux__) // #ifdef _WIN32 #define WINDOWS_BUILD NOT_IN_USE #define LINUX_BUILD IN_USE #else // #elif defined(__linux__) // #ifdef _WIN32 #error "Unsupported platform" #endif #define ASSERTS USE_IF( USING( DEVELOPMENT_BUILD ) )
1,630
C
29.203703
103
0.707975
omniverse-code/kit/fabric/include/carb/flatcache/WrapperImpl.h
// Copyright (c) 2020-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. // #pragma once // The purpose of this file is to implement the C++ classes StageInProgress, // StageAtTime, StageAtTimeInterval and StageHistoryWindow by calling the // carbonite C-ABI interfaces, IStageInProgress, IStageAtTime, // IStageAtTimeWindow and IStageHistoryWindow. // // #include "StageWithHistory.h" #include <carb/InterfaceUtils.h> #include <carb/logging/Log.h> #include <type_traits> #include <cstdint> namespace carb { namespace flatcache { // StageInProgress implementation starts here // RAII constructor inline StageInProgress::StageInProgress(StageWithHistory& stageWithHistory, size_t simFrameNumber) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); m_stageInProgress = iStageInProgress->create(stageWithHistory.m_usdStageId, simFrameNumber); m_usdStageId = stageWithHistory.m_usdStageId; m_createdFromId = false; } // Non-RAII constructor inline StageInProgress::StageInProgress(StageInProgressId stageInProgressId) { m_stageInProgress = stageInProgressId; m_createdFromId = true; // m_usdStageId is not valid when m_createdFromId==true } inline StageInProgress::~StageInProgress() { if (!m_createdFromId) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); iStageInProgress->destroy(m_usdStageId); } } inline size_t StageInProgress::getFrameNumber() { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); return iStageInProgress->getFrameNumber(m_stageInProgress); } inline ValidMirrors StageInProgress::getAttributeValidBits(const Path& path, const Token& attrName) const { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); return iStageInProgress->getAttributeValidBits(m_stageInProgress, path, attrName); } inline RationalTime StageInProgress::getFrameTime() { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); return iStageInProgress->getFrameTime(m_stageInProgress); } template <typename T> T* StageInProgress::getAttribute(const Path& path, const Token& attrName) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); SpanC ptrAndSize = iStageInProgress->getAttribute(m_stageInProgress, path, attrName); if (sizeof(T) == ptrAndSize.elementSize) { return reinterpret_cast<T*>(ptrAndSize.ptr); } else { CARB_LOG_WARN_ONCE("Trying to access %zu bytes from %s.%s, but flatcache has only %zu bytes", sizeof(T), path.getText(), attrName.getText(), ptrAndSize.elementSize); return nullptr; } } template <typename T> const T* StageInProgress::getAttributeRd(const Path& path, const Token& attrName) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); ConstSpanC ptrAndSize = iStageInProgress->getAttributeRd(m_stageInProgress, path, attrName); if (sizeof(T) == ptrAndSize.elementSize) { return reinterpret_cast<const T*>(ptrAndSize.ptr); } else { CARB_LOG_WARN_ONCE("Trying to access %zu bytes from %s.%s, but flatcache has only %zu bytes", sizeof(T), path.getText(), attrName.getText(), ptrAndSize.elementSize); return nullptr; } } template <typename T> T* StageInProgress::getAttributeWr(const Path& path, const Token& attrName) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); SpanC ptrAndSize = iStageInProgress->getAttributeWr(m_stageInProgress, path, attrName); if (sizeof(T) == ptrAndSize.elementSize) { return reinterpret_cast<T*>(ptrAndSize.ptr); } else { CARB_LOG_WARN_ONCE("Trying to access %zu bytes from %s.%s, but flatcache has only %zu bytes", sizeof(T), path.getText(), attrName.getText(), ptrAndSize.elementSize); return nullptr; } } template <typename T> T* StageInProgress::getAttributeGpu(const Path& path, const Token& attrName) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); SpanC ptrAndSize = iStageInProgress->getAttributeGpu(m_stageInProgress, path, attrName); if (sizeof(T) == ptrAndSize.elementSize) { return reinterpret_cast<T*>(ptrAndSize.ptr); } else { CARB_LOG_WARN_ONCE("Trying to access %zu bytes from %s.%s, but flatcache has only %zu bytes", sizeof(T), path.getText(), attrName.getText(), ptrAndSize.elementSize); return nullptr; } } template <typename T> const T* StageInProgress::getAttributeRdGpu(const Path& path, const Token& attrName) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); ConstSpanC ptrAndSize = iStageInProgress->getAttributeRdGpu(m_stageInProgress, path, attrName); if (sizeof(T) == ptrAndSize.elementSize) { return reinterpret_cast<const T*>(ptrAndSize.ptr); } else { CARB_LOG_WARN_ONCE("Trying to access %zu bytes from %s.%s, but flatcache has only %zu bytes", sizeof(T), path.getText(), attrName.getText(), ptrAndSize.elementSize); return nullptr; } } template <typename T> T* StageInProgress::getAttributeWrGpu(const Path& path, const Token& attrName) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); SpanC ptrAndSize = iStageInProgress->getAttributeWrGpu(m_stageInProgress, path, attrName); if (sizeof(T*) == ptrAndSize.elementSize) { return reinterpret_cast<T*>(ptrAndSize.ptr); } else { CARB_LOG_WARN_ONCE("Trying to access %zu bytes from %s.%s, but flatcache has only %zu bytes", sizeof(T), path.getText(), attrName.getText(), ptrAndSize.elementSize); return nullptr; } } template <typename T> T& StageInProgress::getOrCreateAttributeWr(const Path& path, const Token& attrName, Type type) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); SpanC ptrAndSize = iStageInProgress->getOrCreateAttributeWr(m_stageInProgress, path, attrName, TypeC(type)); if (sizeof(T) != ptrAndSize.elementSize) { CARB_LOG_WARN_ONCE("Trying to access %zu bytes from %s.%s, but flatcache has only %zu bytes", sizeof(T), path.getText(), attrName.getText(), ptrAndSize.elementSize); } return *reinterpret_cast<T*>(ptrAndSize.ptr); } template <typename T> gsl::span<T> StageInProgress::getArrayAttribute(const Path& path, const Token& attrName) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); SpanC arrayData = iStageInProgress->getArrayAttributeWr(m_stageInProgress, path, attrName); if (sizeof(T) != arrayData.elementSize) { CARB_LOG_WARN_ONCE( "Trying to access array with elements of size %zu bytes from %s.%s, but flatcache has only %zu bytes", sizeof(T), path.getText(), attrName.getText(), arrayData.elementSize); return gsl::span<T>(); } gsl::span<T> retval(reinterpret_cast<T*>(arrayData.ptr), arrayData.elementCount); return retval; } template <typename T> gsl::span<const T> StageInProgress::getArrayAttributeRd(const Path& path, const Token& attrName) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); ConstSpanC arrayData = iStageInProgress->getArrayAttributeRd(m_stageInProgress, path, attrName); if (sizeof(T) != arrayData.elementSize) { CARB_LOG_WARN_ONCE( "Trying to access array with elements of size %zu bytes from %s.%s, but flatcache has only %zu bytes", sizeof(T), path.getText(), attrName.getText(), arrayData.elementSize); return gsl::span<const T>(); } gsl::span<const T> retval(reinterpret_cast<const T*>(arrayData.ptr), arrayData.elementCount); return retval; } template <typename T> gsl::span<T> StageInProgress::getArrayAttributeWr(const Path& path, const Token& attrName) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); SpanC arrayData = iStageInProgress->getArrayAttributeWr(m_stageInProgress, path, attrName); if (sizeof(T) != arrayData.elementSize) { CARB_LOG_WARN_ONCE( "Trying to access array with elements of size %zu bytes from %s.%s, but flatcache has only %zu bytes", sizeof(T), path.getText(), attrName.getText(), arrayData.elementSize); return gsl::span<T>(); } gsl::span<T> retval(reinterpret_cast<T*>(arrayData.ptr), arrayData.elementCount); return retval; } inline size_t StageInProgress::getArrayAttributeSize(const Path& path, const Token& attrName) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); return iStageInProgress->getArrayAttributeSize(m_stageInProgress, path, attrName); } inline void StageInProgress::setArrayAttributeSize(const Path& path, const Token& attrName, size_t elemCount) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); iStageInProgress->setArrayAttributeSize(m_stageInProgress, path, attrName, elemCount); } template <typename T> inline gsl::span<T> StageInProgress::setArrayAttributeSizeAndGet(const PrimBucketList& primBucketList, size_t primBucketListIndex, size_t indexInBucket, const Token& attrName, size_t newElemCount) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); SpanC newArrayC = iStageInProgress->setArrayAttributeSizeAndGet( m_stageInProgress, primBucketList.m_primBucketListId, primBucketListIndex, indexInBucket, attrName, newElemCount); T* typedElementsPtr = reinterpret_cast<T*>(newArrayC.ptr); return { typedElementsPtr, newArrayC.elementCount }; } inline void StageInProgress::createPrim(const Path& path) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); iStageInProgress->createPrim(m_stageInProgress, path); } inline void StageInProgress::destroyPrim(const Path& path) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); iStageInProgress->destroyPrim(m_stageInProgress, path); } inline void StageInProgress::createAttribute(const Path& path, const Token& attrName, Type type) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); iStageInProgress->createAttribute(m_stageInProgress, path, attrName, TypeC(type)); } template <int n> inline void StageInProgress::createAttributes(const Path& path, std::array<AttrNameAndType, n> attributes) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); std::array<TokenC, n> names; std::array<TypeC, n> types; for (int c = 0; c < n; ++c) { names[c] = attributes[c].name; types[c] = TypeC(attributes[c].type); } iStageInProgress->createAttributes(m_stageInProgress, path, names.data(), types.data(), n); } inline void StageInProgress::destroyAttribute(const Path& path, const Token& attrName, Type) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); iStageInProgress->destroyAttribute2(m_stageInProgress, path, attrName); } inline void StageInProgress::destroyAttribute(const Path& path, const Token& attrName) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); iStageInProgress->destroyAttribute2(m_stageInProgress, path, attrName); } template <int n> inline void StageInProgress::destroyAttributes(const Path& path, const std::array<Token, n>& attributes) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); std::array<TokenC, n> names; for (int c = 0; c < n; ++c) { names[c] = TokenC(attributes[c]); } iStageInProgress->destroyAttributes(m_stageInProgress, path, names.data(), n); } inline void StageInProgress::destroyAttributes(const Path& path, const std::vector<Token>& attributes) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); const size_t n = attributes.size(); std::vector<TokenC> names(n); for (size_t c = 0; c < n; ++c) { names[c] = TokenC(attributes[c]); } iStageInProgress->destroyAttributes(m_stageInProgress, path, names.data(), (uint32_t)n); } inline PrimBucketList StageInProgress::findPrims(const carb::flatcache::set<AttrNameAndType>& all, const carb::flatcache::set<AttrNameAndType>& any, const carb::flatcache::set<AttrNameAndType>& none) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); PrimBucketListId primBucketListId = iStageInProgress->findPrims(m_stageInProgress, all, any, none); return { primBucketListId }; } inline void StageInProgress::attributeEnableChangeTracking(const Token& attrName, ListenerId listenerId) { auto iChangeTrackerConfig = carb::getCachedInterface<carb::flatcache::IChangeTrackerConfig>(); iChangeTrackerConfig->attributeEnable(m_stageInProgress, attrName, listenerId); } inline void StageInProgress::enablePrimCreateTracking(ListenerId listenerId) { auto iChangeTrackerConfig = carb::getCachedInterface<carb::flatcache::IChangeTrackerConfig>(); iChangeTrackerConfig->enablePrimCreateTracking(m_stageInProgress, listenerId); } inline void StageInProgress::attributeDisableChangeTracking(const Token& attrName, ListenerId listenerId) { auto iChangeTrackerConfig = carb::getCachedInterface<carb::flatcache::IChangeTrackerConfig>(); iChangeTrackerConfig->attributeDisable(m_stageInProgress, attrName, listenerId); } inline void StageInProgress::pauseChangeTracking(ListenerId listenerId) { auto iChangeTrackerConfig = carb::getCachedInterface<carb::flatcache::IChangeTrackerConfig>(); iChangeTrackerConfig->pause(m_stageInProgress, listenerId); } inline void StageInProgress::resumeChangeTracking(ListenerId listenerId) { auto iChangeTrackerConfig = carb::getCachedInterface<carb::flatcache::IChangeTrackerConfig>(); iChangeTrackerConfig->resume(m_stageInProgress, listenerId); } inline bool StageInProgress::isChangeTrackingPaused(ListenerId listenerId) { auto iChangeTrackerConfig = carb::getCachedInterface<carb::flatcache::IChangeTrackerConfig>(); return iChangeTrackerConfig->isChangeTrackingPaused(m_stageInProgress, listenerId); } inline bool StageInProgress::isListenerAttached(ListenerId listenerId) { auto iChangeTrackerConfig = carb::getCachedInterface<carb::flatcache::IChangeTrackerConfig>(); return iChangeTrackerConfig->isListenerAttached(m_stageInProgress, listenerId); } inline void StageInProgress::detachListener(ListenerId listenerId) { auto iChangeTrackerConfig = carb::getCachedInterface<carb::flatcache::IChangeTrackerConfig>(); iChangeTrackerConfig->detachListener(m_stageInProgress, listenerId); } inline size_t StageInProgress::getListenerCount() { auto iChangeTrackerConfig = carb::getCachedInterface<carb::flatcache::IChangeTrackerConfig>(); return iChangeTrackerConfig->getListenerCount(m_stageInProgress); } inline ChangedPrimBucketList StageInProgress::getChanges(ListenerId listenerId) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); PrimBucketListId changeListId = iStageInProgress->getChanges(m_stageInProgress, listenerId); return ChangedPrimBucketList(changeListId); } inline void StageInProgress::popChanges(ListenerId listenerId) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); iStageInProgress->popChanges(m_stageInProgress, listenerId); } template <typename T> gsl::span<T> StageInProgress::getAttributeArray(const PrimBucketList& primBucketList, size_t primBucketListIndex, const Token& attrName) { SpanC array; auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); iStageInProgress->getAttributeArray( &array, m_stageInProgress, primBucketList.m_primBucketListId, primBucketListIndex, attrName); T* typedElementsPtr = reinterpret_cast<T*>(array.ptr); gsl::span<T> retval(typedElementsPtr, array.elementCount); return retval; } template <typename T> gsl::span<const T> StageInProgress::getAttributeArrayRd(const PrimBucketList& primBucketList, size_t primBucketListIndex, const Token& attrName) const { ConstSpanC array; auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); iStageInProgress->getAttributeArrayRd( &array, m_stageInProgress, primBucketList.m_primBucketListId, primBucketListIndex, attrName); const T* typedElementsPtr = reinterpret_cast<const T*>(array.ptr); gsl::span<const T> retval(typedElementsPtr, array.elementCount); return retval; } template <typename T> gsl::span<T> StageInProgress::getAttributeArrayWr(const PrimBucketList& primBucketList, size_t primBucketListIndex, const Token& attrName) { SpanC array; auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); iStageInProgress->getAttributeArrayWr( &array, m_stageInProgress, primBucketList.m_primBucketListId, primBucketListIndex, attrName); T* typedElementsPtr = reinterpret_cast<T*>(array.ptr); gsl::span<T> retval(typedElementsPtr, array.elementCount); return retval; } template <typename T> gsl::span<T> StageInProgress::getAttributeArrayGpu(const PrimBucketList& primBucketList, size_t primBucketListIndex, const Token& attrName) { SpanC array; auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); iStageInProgress->getAttributeArrayGpu( &array, m_stageInProgress, primBucketList.m_primBucketListId, primBucketListIndex, attrName); T* typedElementsPtr = reinterpret_cast<T*>(array.ptr); gsl::span<T> retval(typedElementsPtr, array.elementCount); return retval; } template <typename T> gsl::span<const T> StageInProgress::getAttributeArrayRdGpu(const PrimBucketList& primBucketList, size_t primBucketListIndex, const Token& attrName) const { ConstSpanC array; auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); iStageInProgress->getAttributeArrayRdGpu( &array, m_stageInProgress, primBucketList.m_primBucketListId, primBucketListIndex, attrName); const T* typedElementsPtr = reinterpret_cast<const T*>(array.ptr); gsl::span<const T> retval(typedElementsPtr, array.elementCount); return retval; } template <typename T> gsl::span<T> StageInProgress::getAttributeArrayWrGpu(const PrimBucketList& primBucketList, size_t primBucketListIndex, const Token& attrName) { SpanC array; auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); iStageInProgress->getAttributeArrayWrGpu( &array, m_stageInProgress, primBucketList.m_primBucketListId, primBucketListIndex, attrName); T* typedElementsPtr = reinterpret_cast<T*>(array.ptr); gsl::span<T> retval(typedElementsPtr, array.elementCount); return retval; } template <typename T> gsl::span<T> StageInProgress::getOrCreateAttributeArrayWr(const PrimBucketList& primBucketList, size_t primBucketListIndex, const Token& attrName, Type type) { SpanC array; auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); iStageInProgress->getOrCreateAttributeArrayWr( &array, m_stageInProgress, primBucketList.m_primBucketListId, primBucketListIndex, attrName, TypeC(type)); T* typedElementsPtr = reinterpret_cast<T*>(array.ptr); gsl::span<T> retval(typedElementsPtr, array.elementCount); return retval; } template <typename T> std::vector<gsl::span<T>> StageInProgress::getArrayAttributeArray(const PrimBucketList& primBucketList, size_t primBucketListIndex, const Token& attrName) const { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); ArrayPointersAndSizesC pointersAndSizes = iStageInProgress->getArrayAttributeArrayWithSizes( m_stageInProgress, primBucketList.m_primBucketListId, primBucketListIndex, attrName); size_t primCount = pointersAndSizes.elementCount; std::vector<gsl::span<T>> arrays(primCount); for (size_t i = 0; i != primCount; i++) { T* typedElementsPtr = reinterpret_cast<T*>(pointersAndSizes.arrayPtrs[i]); arrays[i] = { typedElementsPtr, pointersAndSizes.sizes[i] }; } return arrays; } template <typename T> std::vector<gsl::span<const T>> StageInProgress::getArrayAttributeArrayRd(const PrimBucketList& primBucketList, size_t primBucketListIndex, const Token& attrName) const { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); ConstArrayPointersAndSizesC pointersAndSizes = iStageInProgress->getArrayAttributeArrayWithSizesRd( m_stageInProgress, primBucketList.m_primBucketListId, primBucketListIndex, attrName); size_t primCount = pointersAndSizes.elementCount; std::vector<gsl::span<const T>> arrays(primCount); for (size_t i = 0; i != primCount; i++) { const T* typedElementsPtr = reinterpret_cast<const T*>(pointersAndSizes.arrayPtrs[i]); arrays[i] = { typedElementsPtr, pointersAndSizes.sizes[i] }; } return arrays; } template <typename T> std::vector<gsl::span<T>> StageInProgress::getArrayAttributeArrayWr(const PrimBucketList& primBucketList, size_t primBucketListIndex, const Token& attrName) const { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); ArrayPointersAndSizesC pointersAndSizes = iStageInProgress->getArrayAttributeArrayWithSizesWr( m_stageInProgress, primBucketList.m_primBucketListId, primBucketListIndex, attrName); size_t primCount = pointersAndSizes.elementCount; std::vector<gsl::span<T>> arrays(primCount); for (size_t i = 0; i != primCount; i++) { T* typedElementsPtr = reinterpret_cast<T*>(pointersAndSizes.arrayPtrs[i]); arrays[i] = { typedElementsPtr, pointersAndSizes.sizes[i] }; } return arrays; } inline gsl::span<const Path> StageInProgress::getPathArray(const PrimBucketList& primBucketList, size_t primBucketListIndex) const { ConstPathCSpan arrayC; auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); iStageInProgress->getPathArray(&arrayC, m_stageInProgress, primBucketList.m_primBucketListId, primBucketListIndex); const Path* array = reinterpret_cast<const Path*>(arrayC.ptr); gsl::span<const Path> retval(array, arrayC.elementCount); return retval; } inline void StageInProgress::printBucketNames() const { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); iStageInProgress->printBucketNames(m_stageInProgress); } inline void StageInProgress::logAttributeWriteForNotice(const Path& path, const Token& attrName) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); iStageInProgress->logAttributeWriteForNotice(m_stageInProgress, path, attrName); } inline flatcache::set<AttrNameAndType> StageInProgress::getAttributeNamesAndTypes(const PrimBucketList& primBucketList, size_t primBucketListIndex) const { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); size_t attrCount = iStageInProgress->getBucketAttributeCount( m_stageInProgress, primBucketList.m_primBucketListId, primBucketListIndex); flatcache::set<AttrNameAndType> namesAndTypes; namesAndTypes.v.resize(attrCount); // getBucketAttributeNamesAndTypes is guaranteed to return an ordered vector, so we don't have to sort namesAndTypes iStageInProgress->getBucketAttributeNamesAndTypes( namesAndTypes.data(), attrCount, m_stageInProgress, primBucketList.m_primBucketListId, primBucketListIndex); return namesAndTypes; } // Connection API inline void StageInProgress::createConnection(const Path& path, const Token& connectionName, const Connection& connection) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); iStageInProgress->createConnection(m_stageInProgress, path, connectionName, connection); } inline void StageInProgress::createConnections(const Path& path, const gsl::span<Token>& connectionNames, const gsl::span<Connection>& connections ) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); if(connectionNames.size() != connections.size()) return; const TokenC* namesC = reinterpret_cast<const TokenC*>(connectionNames.data()); iStageInProgress->createConnections(m_stageInProgress, path, namesC, connections.data(), connectionNames.size()); } inline void StageInProgress::destroyConnection(const Path& path, const Token& connectionName) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); iStageInProgress->destroyConnection(m_stageInProgress, path, connectionName); } inline void StageInProgress::destroyConnections(const Path& path, const gsl::span<Token>& connectionNames) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); const TokenC* namesC = reinterpret_cast<const TokenC*>(connectionNames.data()); iStageInProgress->destroyConnections(m_stageInProgress, path, namesC, connectionNames.size()); } inline Connection* StageInProgress::getConnection(const Path& path, const Token& connectionName) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); return iStageInProgress->getConnection(m_stageInProgress, path, connectionName); } inline const Connection* StageInProgress::getConnectionRd(const Path& path, const Token& connectionName) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); return iStageInProgress->getConnectionRd(m_stageInProgress, path, connectionName); } inline Connection* StageInProgress::getConnectionWr(const Path& path, const Token& connectionName) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); return iStageInProgress->getConnectionWr(m_stageInProgress, path, connectionName); } inline void StageInProgress::copyAttributes(const Path& srcPath, const Path& dstPath) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); iStageInProgress->copyAllAttributes(m_stageInProgress, srcPath, dstPath); } inline void StageInProgress::copyAttributes(const Path& srcPath, const gsl::span<Token>& srcAttrs, const Path& dstPath) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); size_t n = srcAttrs.size(); const TokenC* srcAttrsC = reinterpret_cast<const TokenC*>(srcAttrs.data()); iStageInProgress->copySpecifiedAttributes(m_stageInProgress, srcPath, srcAttrsC, dstPath, srcAttrsC, n); } inline void StageInProgress::copyAttributes(const Path& srcPath, const gsl::span<Token>& srcAttrs, const Path& dstPath, const gsl::span<Token>& dstAttrs) { auto iStageInProgress = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); if(srcAttrs.size() != dstAttrs.size()) { return; } size_t n = srcAttrs.size(); const TokenC* srcAttrsC = reinterpret_cast<const TokenC*>(srcAttrs.data()); const TokenC* dstAttrsC = reinterpret_cast<const TokenC*>(dstAttrs.data()); iStageInProgress->copySpecifiedAttributes(m_stageInProgress, srcPath, srcAttrsC, dstPath, dstAttrsC, n); } inline bool StageInProgress::primExists(const Path& path) { auto iStageReaderWriter = carb::getCachedInterface<carb::flatcache::IStageInProgress>(); bool retval = iStageReaderWriter->getAttributeCount(m_stageInProgress, path) != 0; return retval; } // PrimBucketList implementation starts here inline carb::flatcache::IPrimBucketList* PrimBucketList::sIPrimBucketList() { // Acquire carbonite interface on first use return carb::getCachedInterface<carb::flatcache::IPrimBucketList>(); } inline size_t PrimBucketList::bucketCount() const { return sIPrimBucketList()->getBucketCount(m_primBucketListId); } inline size_t PrimBucketList::size() const { return sIPrimBucketList()->getBucketCount(m_primBucketListId); } inline void PrimBucketList::print() const { return sIPrimBucketList()->print(m_primBucketListId); } inline PrimBucketList::~PrimBucketList() { sIPrimBucketList()->destroy(m_primBucketListId); } inline BucketChanges ChangedPrimBucketList::getChanges(size_t index) { return BucketChanges(sIPrimBucketList()->getChanges(m_primBucketListId, index)); } inline AddedPrimIndices ChangedPrimBucketList::getAddedPrims(size_t index) { return AddedPrimIndices(sIPrimBucketList()->getAddedPrims(m_primBucketListId, index)); } // StageAtTimeInterval implementation starts here inline carb::flatcache::IStageAtTimeInterval* StageAtTimeInterval::sIStageAtTimeInterval() { return carb::getCachedInterface<carb::flatcache::IStageAtTimeInterval>(); } inline StageAtTimeInterval::StageAtTimeInterval(StageWithHistory& stageWithHistory, RationalTime beginTime, RationalTime endTime, bool includeEndTime) { m_stageAtTimeInterval = sIStageAtTimeInterval()->create(stageWithHistory.m_stageWithHistory, beginTime, endTime, includeEndTime); } inline StageAtTimeInterval::StageAtTimeInterval(StageWithHistoryId stageWithHistoryId, RationalTime beginTime, RationalTime endTime, bool includeEndTime) { m_stageAtTimeInterval = sIStageAtTimeInterval()->create(stageWithHistoryId, beginTime, endTime, includeEndTime); } inline ValidMirrors StageAtTimeInterval::getAttributeValidBits(const PathC& path, const TokenC& attrName) const { return sIStageAtTimeInterval()->getAttributeValidBits(m_stageAtTimeInterval, path, attrName); } template <typename T> std::vector<const T*> StageAtTimeInterval::getAttributeRd(const Path& path, const Token& attrName) const { size_t count = sIStageAtTimeInterval()->getTimesampleCount(m_stageAtTimeInterval); std::vector<const T*> retval(count); const void** retvalData = reinterpret_cast<const void**>(retval.data()); size_t bytesPerAttr = sIStageAtTimeInterval()->getAttributeRd(retvalData, count, m_stageAtTimeInterval, path, attrName); if (sizeof(T) == bytesPerAttr) { return retval; } else { CARB_LOG_WARN_ONCE("Trying to access %zu bytes from %s.%s, but flatcache has only %zu bytes", sizeof(T), path.getText(), attrName.getText(), bytesPerAttr); return std::vector<const T*>(); } } template <typename T> std::vector<const T*> StageAtTimeInterval::getAttributeRdGpu(const Path& path, const Token& attrName) const { size_t count = sIStageAtTimeInterval()->getTimesampleCount(m_stageAtTimeInterval); std::vector<const T*> retval(count); std::vector<ConstSpanC> arrays(count); sIStageAtTimeInterval()->getAttributeRdGpu(arrays.data(), count, m_stageAtTimeInterval, path, attrName); for (size_t i = 0; i != count; i++) { if (arrays[i].elementSize == sizeof(T)) { retval[i] = reinterpret_cast<const T*>(arrays[i].ptr); } else { retval[i] = nullptr; } } return retval; } inline std::vector<size_t> StageAtTimeInterval::getArrayAttributeSize(const Path& path, const Token& attrName) const { size_t count = sIStageAtTimeInterval()->getTimesampleCount(m_stageAtTimeInterval); std::vector<size_t> sizes(count); sIStageAtTimeInterval()->getArrayAttributeSize(sizes.data(), count, m_stageAtTimeInterval, path, attrName); return sizes; } template <typename T> std::vector<gsl::span<const T>> StageAtTimeInterval::getArrayAttributeRd(const Path& path, const Token& attrName) const { size_t count = sIStageAtTimeInterval()->getTimesampleCount(m_stageAtTimeInterval); std::vector<ConstSpanWithTypeC> arrays(count); std::vector<gsl::span<const T>> retval(count); sIStageAtTimeInterval()->getArrayAttributeWithSizeRd(arrays.data(), count, m_stageAtTimeInterval, path, attrName); for (size_t i = 0; i != count; i++) { if (arrays[i].elementSize != sizeof(T)) { retval[i] = gsl::span<T>(); continue; } const T* ptr = reinterpret_cast<const T*>(arrays[i].ptr); retval[i] = gsl::span<const T>(ptr, arrays[i].elementCount); } return retval; } inline std::vector<ConstArrayAsBytes> StageAtTimeInterval::getArrayAttributeRawRd(const Path& path, const Token& attrName) const { size_t count = sIStageAtTimeInterval()->getTimesampleCount(m_stageAtTimeInterval); std::vector<ConstSpanWithTypeC> arrays(count); std::vector<ConstArrayAsBytes> retval(count); sIStageAtTimeInterval()->getArrayAttributeWithSizeRd(arrays.data(), count, m_stageAtTimeInterval, path, attrName); for (size_t i = 0; i != count; i++) { const gsl::byte* ptr = reinterpret_cast<const gsl::byte*>(arrays[i].ptr); retval[i].arrayBytes = gsl::span<const gsl::byte>(ptr, arrays[i].elementCount * arrays[i].elementSize); retval[i].bytesPerElement = arrays[i].elementSize; retval[i].elementType = Type(arrays[i].type); } return retval; } inline std::vector<RationalTime> StageAtTimeInterval::getTimestamps() const { size_t count = sIStageAtTimeInterval()->getTimesampleCount(m_stageAtTimeInterval); std::vector<RationalTime> retval(count); sIStageAtTimeInterval()->getTimestamps(retval.data(), count, m_stageAtTimeInterval); return retval; } inline size_t StageAtTimeInterval::getTimeSampleCount() const { return sIStageAtTimeInterval()->getTimesampleCount(m_stageAtTimeInterval); } inline PrimBucketList StageAtTimeInterval::findPrims(const carb::flatcache::set<AttrNameAndType>& all, const carb::flatcache::set<AttrNameAndType>& any, const carb::flatcache::set<AttrNameAndType>& none) { PrimBucketListId primBucketListId = sIStageAtTimeInterval()->findPrims(m_stageAtTimeInterval, all, any, none); return { primBucketListId }; } template <typename T> std::vector<gsl::span<const T>> StageAtTimeInterval::getAttributeArrayRd(const PrimBucketList& primBucketList, size_t primBucketListIndex, const Token& attrName) const { size_t count = sIStageAtTimeInterval()->getTimesampleCount(m_stageAtTimeInterval); std::vector<ConstSpanC> outC(count); ConstSpanC* outCData = outC.data(); sIStageAtTimeInterval()->getAttributeArrayRd( outCData, count, m_stageAtTimeInterval, primBucketList.m_primBucketListId, primBucketListIndex, attrName); std::vector<gsl::span<const T>> retval(count); size_t i = 0; for (ConstSpanC array : outC) { const T* typedElementsPtr = reinterpret_cast<const T*>(array.ptr); retval[i] = gsl::span<const T>(typedElementsPtr, array.elementCount); i++; } return retval; } template <typename T> std::vector<gsl::span<const T>> StageAtTimeInterval::getAttributeArrayRdGpu(const PrimBucketList& primBucketList, size_t primBucketListIndex, const Token& attrName) const { size_t count = sIStageAtTimeInterval()->getTimesampleCount(m_stageAtTimeInterval); std::vector<ConstSpanC> outC(count); ConstSpanC* outCData = outC.data(); sIStageAtTimeInterval()->getAttributeArrayRdGpu( outCData, count, m_stageAtTimeInterval, primBucketList.m_primBucketListId, primBucketListIndex, attrName); std::vector<gsl::span<const T>> retval(count); size_t i = 0; for (ConstSpanC array : outC) { const T* typedElementsPtr = reinterpret_cast<const T*>(array.ptr); retval[i] = gsl::span<const T>(typedElementsPtr, array.elementCount); i++; } return retval; } template <typename T> std::vector<std::vector<gsl::span<const T>>> StageAtTimeInterval::getArrayAttributeArrayRd( const PrimBucketList& primBucketList, size_t primBucketListIndex, const Token& attrName) const { size_t count = sIStageAtTimeInterval()->getTimesampleCount(m_stageAtTimeInterval); std::vector<ConstArrayPointersAndSizesC> outC(count); ConstArrayPointersAndSizesC* outCData = outC.data(); sIStageAtTimeInterval()->getArrayAttributeArrayWithSizesRd( outCData, count, m_stageAtTimeInterval, primBucketList.m_primBucketListId, primBucketListIndex, attrName); std::vector<std::vector<gsl::span<const T>>> retval(count); size_t i = 0; for (ConstArrayPointersAndSizesC pointersAndSizes : outC) { size_t primCount = pointersAndSizes.elementCount; retval[i].resize(primCount); for (size_t j = 0; j != primCount; j++) { const T* typedElementsPtr = reinterpret_cast<const T*>(pointersAndSizes.arrayPtrs[j]); retval[i][j] = { typedElementsPtr, pointersAndSizes.sizes[j] }; } i++; } return retval; } inline std::vector<gsl::span<const char>> StageAtTimeInterval::getAttributeArrayRawRd( const PrimBucketList& primBucketList, size_t primBucketListIndex, const Token& attrName) const { size_t count = sIStageAtTimeInterval()->getTimesampleCount(m_stageAtTimeInterval); std::vector<ConstSpanC> outC(count); ConstSpanC* outCData = outC.data(); sIStageAtTimeInterval()->getAttributeArrayRd( outCData, count, m_stageAtTimeInterval, primBucketList.m_primBucketListId, primBucketListIndex, attrName); std::vector<gsl::span<const char>> retval(count); size_t i = 0; for (ConstSpanC array : outC) { const char* typedElementsPtr = reinterpret_cast<const char*>(array.ptr); retval[i] = gsl::span<const char>(typedElementsPtr, array.elementCount * array.elementSize); i++; } return retval; } inline std::vector<gsl::span<const Path>> StageAtTimeInterval::getPathArray(const PrimBucketList& primBucketList, size_t primBucketListIndex) const { size_t count = sIStageAtTimeInterval()->getTimesampleCount(m_stageAtTimeInterval); std::vector<ConstPathCSpan> outC(count); ConstPathCSpan* outCData = outC.data(); sIStageAtTimeInterval()->getPathArray( outCData, count, m_stageAtTimeInterval, primBucketList.m_primBucketListId, primBucketListIndex); std::vector<gsl::span<const Path>> retval(count); size_t i = 0; for (ConstPathCSpan arrayC : outC) { const Path* array = reinterpret_cast<const Path*>(arrayC.ptr); retval[i] = gsl::span<const Path>(array, arrayC.elementCount); i++; } return retval; } inline std::vector<const Connection*> StageAtTimeInterval::getConnectionRd(const Path& path, const Token& connectionName) { size_t count = sIStageAtTimeInterval()->getTimesampleCount(m_stageAtTimeInterval); std::vector<const Connection*> retval(count); const void** retvalData = reinterpret_cast<const void**>(retval.data()); sIStageAtTimeInterval()->getConnectionRd(retvalData, count, m_stageAtTimeInterval, path, connectionName); return retval; } inline void StageAtTimeInterval::printBucketNames() const { sIStageAtTimeInterval()->printBucketNames(m_stageAtTimeInterval); } inline std::vector<size_t> StageAtTimeInterval::getAttributeCounts(const PrimBucketList& primBucketList, size_t primBucketListIndex) const { std::vector<size_t> counts; size_t count = sIStageAtTimeInterval()->getTimesampleCount(m_stageAtTimeInterval); counts.resize(count); sIStageAtTimeInterval()->getAttributeCounts( m_stageAtTimeInterval, primBucketList.m_primBucketListId, primBucketListIndex, count, counts.data()); return counts; } inline std::pair<std::vector<std::vector<Token>>, std::vector<std::vector<Type>>> StageAtTimeInterval::getAttributeNamesAndTypes( const PrimBucketList& primBucketList, size_t primBucketListIndex) const { std::vector<std::vector<Token>> outNames; std::vector<std::vector<Type>> outTypes; size_t count = sIStageAtTimeInterval()->getTimesampleCount(m_stageAtTimeInterval); std::vector<size_t> outSizes; outSizes.resize(count); sIStageAtTimeInterval()->getAttributeCounts( m_stageAtTimeInterval, primBucketList.m_primBucketListId, primBucketListIndex, count, outSizes.data()); outNames.resize(count); outTypes.resize(count); // Make array of pointers to inner arrays to allow us to call // getAttributeNamesAndTypes, which takes a C-style 2D array // not a std::vector<std::vector>. // Also set size of inner arrays std::vector<Token*> outNamesPtrs(count); std::vector<Type*> outTypesPtrs(count); for (size_t i = 0; i < count; ++i) { outNames[i].resize(outSizes[i]); outTypes[i].resize(outSizes[i]); outNamesPtrs[i] = outNames[i].data(); outTypesPtrs[i] = outTypes[i].data(); } sIStageAtTimeInterval()->getAttributeNamesAndTypes(m_stageAtTimeInterval, primBucketList.m_primBucketListId, primBucketListIndex, count, outSizes.data(), outNamesPtrs.data(), outTypesPtrs.data()); return { outNames, outTypes }; } inline StageAtTimeInterval::~StageAtTimeInterval() { sIStageAtTimeInterval()->destroy(m_stageAtTimeInterval); } inline void StageAtTimeInterval::exportUsd(UsdStageId usdStageId) const { auto iStageAtTimeInterval = carb::getCachedInterface<carb::flatcache::IStageAtTimeInterval>(); iStageAtTimeInterval->exportUsd(m_stageAtTimeInterval, usdStageId); } /** * @brief Linear interpolation for carb types Double3, Float3, Float4 (color) * See InterpolationUsd.h for extended type support * * @details This is intended to be used internally by StageAtTime read methods in order * to calculate values that were not written by StageInProgress directly. * * Enables the decoupling of the sim and render threads by allowing them access * to ringbuffer values at various frequencies. */ template <typename T> const T interpolate(const T& a, const T& b, float theta) { T result = T(a * (1.0f - theta)) + T(b * theta); return result; // T result = std::lerp(a, b, theta); } template <> inline const carb::Double3 interpolate(const carb::Double3& a, const carb::Double3& b, float theta) { if (theta < 0.0 || theta > 1.0) { CARB_LOG_WARN_ONCE("WrapperImpl interpolate(): theta %f outside range [0.0, 1.0]", theta); } carb::Double3 result; double tmp = 1.0 - theta; result.x = (a.x * tmp) + (b.x * theta); result.y = (a.y * tmp) + (b.y * theta); result.z = (a.z * tmp) + (b.z * theta); return result; } template <> inline const carb::Float3 interpolate(const carb::Float3& a, const carb::Float3& b, float theta) { if (theta < 0.0f || theta > 1.0f) { CARB_LOG_WARN_ONCE("WrapperImpl interpolate(): theta %f outside range [0.0, 1.0]", theta); } carb::Float3 result; float tmp = 1.0f - theta; result.x = (a.x * tmp) + (b.x * theta); result.y = (a.y * tmp) + (b.y * theta); result.z = (a.z * tmp) + (b.z * theta); return result; } template <> inline const carb::Float4 interpolate(const carb::Float4& a, const carb::Float4& b, float theta) { if (theta < 0.0f || theta > 1.0f) { CARB_LOG_WARN_ONCE("WrapperImpl interpolate(): theta %f outside range [0.0, 1.0]", theta); } carb::Float4 result; float tmp = 1.0f - theta; result.x = (a.x * tmp) + (b.x * theta); result.y = (a.y * tmp) + (b.y * theta); result.z = (a.z * tmp) + (b.z * theta); result.w = (a.w * tmp) + (b.w * theta); return result; } template <> inline const carb::flatcache::Token interpolate(const carb::flatcache::Token& a, const carb::flatcache::Token& b, float theta) { if (theta < 0.0f || theta > 1.0f) { CARB_LOG_WARN_ONCE("WrapperImpl interpolate(): theta %f outside range [0.0, 1.0]", theta); } return theta < 0.5f ? a : b; } // Auxiliary function used when handling data that is not going to be interpolated (bool, string, int, uint) // Returns pair of values from first and second sampled frame, or the value found and nullptr if data is only available // in one frame template <typename T> inline optional<std::pair<optional<T>,optional<T>>> StageAtTime::getNonInterpolatableAttributeRd(const Path& path, const Token& attrName) const { auto rawSamples = m_historyWindow.getAttributeRd<T>(path, attrName); std::vector<RationalTime> sampleTimes = m_historyWindow.getTimestamps(); if (rawSamples.size() != sampleTimes.size()) { return carb::cpp17::nullopt; } // checking that if the rawSamples are not empty, we have something valid in rawSamples[0] CARB_ASSERT(rawSamples.empty() || rawSamples[0]); // Communicate zero samples found if ( rawSamples.empty() || !rawSamples[0] ) { return carb::cpp17::nullopt; } if (rawSamples.size() == 1) { std::pair<carb::cpp17::optional<T>, carb::cpp17::optional<T>> result(*rawSamples[0], carb::cpp17::nullopt); return result; } else if ( (rawSamples.size() == 2) && rawSamples[1] ) { std::pair<carb::cpp17::optional<T>, carb::cpp17::optional<T>> result(*rawSamples[0], *rawSamples[1]); return result; } return carb::cpp17::nullopt; } inline uint64_t StageAtTimeInterval::writeCacheToDisk(const char* file, uint8_t* workingBuffer, uint64_t workingBufferSize) const { return sIStageAtTimeInterval()->writeCacheToDisk(m_stageAtTimeInterval, file, workingBuffer, workingBufferSize); } inline void StageAtTimeInterval::addRefCount() { return sIStageAtTimeInterval()->addRefCount(m_stageAtTimeInterval); } inline bool StageAtTimeInterval::removeRefCount() { return sIStageAtTimeInterval()->removeRefCount(m_stageAtTimeInterval); } inline unsigned int StageAtTimeInterval::getRefCount() { return sIStageAtTimeInterval()->getRefCount(m_stageAtTimeInterval); } // StageAtTime implementation starts here // This is defined here rather than in Carbonite plugin to allow use of templates and inlining inline ValidMirrors StageAtTime::getAttributeValidBits(const PathC& path, const TokenC& attrName) const { return m_historyWindow.getAttributeValidBits(path, attrName); } // The method reports interpolatable data types, and is specialized as optional<pair<optional<T>,optional<T> // in order to report non-interpolatable data types as encountered in either or both samples template <typename T> inline optional<T> StageAtTime::getAttributeRd(const Path& path, const Token& attrName) const { auto rawSamples = m_historyWindow.getAttributeRd<T>(path, attrName); // Communicate zero samples found if (rawSamples.size() == 0) { return carb::cpp17::nullopt; } // Linear interpolation supports at most two samples CARB_ASSERT(rawSamples.size() <= 2); if (rawSamples.size() == 1) { CARB_ASSERT(rawSamples[0]); return *rawSamples[0]; } else if (rawSamples.size() == 2) { CARB_ASSERT(rawSamples[0]); CARB_ASSERT(rawSamples[1]); // Calculate linear approximation of f(time) T a_f = *rawSamples[0]; T b_f = *rawSamples[1]; return interpolate(a_f, b_f, (float)m_theta); } return carb::cpp17::nullopt; } // The following functions are marked for deletion since the specified types cannot be interpolated // StageAtTime reports the non-interpolatable types read from Flatcache as a pair<optional<T>, optional<T>> template <> inline optional<bool> StageAtTime::getAttributeRd(const Path& path, const Token& attrName) const = delete; template <> inline optional<int> StageAtTime::getAttributeRd(const Path& path, const Token& attrName) const = delete; template <> inline optional<unsigned int> StageAtTime::getAttributeRd(const Path& path, const Token& attrName) const = delete; template <> inline optional<unsigned char> StageAtTime::getAttributeRd(const Path& path, const Token& attrName) const = delete; template <> inline optional<int64_t> StageAtTime::getAttributeRd(const Path& path, const Token& attrName) const = delete; template <> inline optional<uint64_t> StageAtTime::getAttributeRd(const Path& path, const Token& attrName) const = delete; template <> inline optional<carb::flatcache::Token> StageAtTime::getAttributeRd(const Path& path, const Token& attrName) const = delete; // Specialize StageAtTime::getAttributeRd for non-interpolatable types: bool, int, uint // In these cases the returned type will be a pair of values from the samples found, or nullopt otherwise template <> inline optional<std::pair<optional<bool>, optional<bool>>> StageAtTime::getAttributeRd(const Path& path, const Token& attrName) const { auto result = getNonInterpolatableAttributeRd<bool>(path, attrName); return result; } template <> inline optional<std::pair<optional<int>, optional<int>>> StageAtTime::getAttributeRd(const Path& path, const Token& attrName) const { return getNonInterpolatableAttributeRd<int>(path, attrName); } template <> inline optional<std::pair<optional<unsigned int>, optional<unsigned int>>> StageAtTime::getAttributeRd(const Path& path, const Token& attrName) const { return getNonInterpolatableAttributeRd<unsigned int>(path, attrName); } template <> inline optional<std::pair<optional<unsigned char>, optional<unsigned char>>> StageAtTime::getAttributeRd(const Path& path, const Token& attrName) const { return getNonInterpolatableAttributeRd<unsigned char>(path, attrName); } template <> inline optional<std::pair<optional<int64_t>, optional<int64_t>>> StageAtTime::getAttributeRd(const Path& path, const Token& attrName) const { return getNonInterpolatableAttributeRd<int64_t>(path, attrName); } template <> inline optional<std::pair<optional<uint64_t>, optional<uint64_t>>> StageAtTime::getAttributeRd(const Path& path, const Token& attrName) const { return getNonInterpolatableAttributeRd<uint64_t>(path, attrName); } template <> inline optional<std::pair<optional<carb::flatcache::Token>, optional<carb::flatcache::Token>>> StageAtTime::getAttributeRd( const Path& path, const Token& attrName) const { return getNonInterpolatableAttributeRd<carb::flatcache::Token>(path, attrName); } template <typename T> const T* StageAtTime::getAttributeRdGpu(const Path& path, const Token& attrName) const { auto rawSamples = m_historyWindow.getAttributeRdGpu<T>(path, attrName); std::vector<RationalTime> sampleTimes = m_historyWindow.getTimestamps(); CARB_ASSERT(rawSamples.size() == sampleTimes.size()); // This API doesn't have a way to communicate zero samples found CARB_ASSERT(rawSamples.size() != 0); // Linear interpolation supports at most two samples CARB_ASSERT(rawSamples.size() <= 2); if (rawSamples.size() == 1) { CARB_ASSERT(rawSamples[0]); return rawSamples[0]; } else if (rawSamples.size() == 2) { // For GPU types there is no support for interpolation yet // Return first sample value instead for now CARB_LOG_WARN_ONCE("Support for interpolation of array attributes is not supported yet, returning first time sample instead!"); CARB_ASSERT(rawSamples[0]); return rawSamples[0]; } return nullptr; } inline size_t StageAtTime::getArrayAttributeSize(const Path& path, const Token& attrName) const { auto rawSamples = m_historyWindow.getArrayAttributeSize(path, attrName); std::vector<RationalTime> sampleTimes = m_historyWindow.getTimestamps(); CARB_ASSERT(rawSamples.size() == sampleTimes.size()); // This API doesn't have a way to communicate zero samples found CARB_ASSERT(rawSamples.size() != 0); // Linear interpolation supports at most two samples CARB_ASSERT(rawSamples.size() <= 2); if (rawSamples.size() == 1) { return rawSamples[0]; } else if (rawSamples.size() == 2) { // For GPU types there is no support for interpolation yet // Return first sample value instead for now return rawSamples[0]; } return 0; } template <typename T> inline gsl::span<const T> StageAtTime::getArrayAttributeRd(const Path& path, const Token& attrName) { auto rawSamples = m_historyWindow.getArrayAttributeRd<T>(path, attrName); std::vector<RationalTime> sampleTimes = m_historyWindow.getTimestamps(); CARB_ASSERT(rawSamples.size() == sampleTimes.size()); // This API doesn't have a way to communicate zero samples found CARB_ASSERT(rawSamples.size() != 0); // Linear interpolation supports at most two samples CARB_ASSERT(rawSamples.size() <= 2); if (rawSamples.size() == 1) { return rawSamples[0]; } else if (rawSamples.size() == 2) { // For array types there is no support for interpolation yet // Return first sample value instead for now CARB_LOG_WARN_ONCE("Support for interpolation of array attributes is not supported yet, returning first time sample instead!"); return rawSamples[0]; } return gsl::span<const T>(); } /** * @brief Auxiliary function used by AttributeArrayResult<T> and AttributeArrayResult<std::vector<T>> * * @details Used to assess if a prim is present in both of the sampled frames */ inline bool checkPathCorrespondence(std::vector<gsl::span<const carb::flatcache::Path>> paths, size_t index, size_t& pos_f0, size_t& pos_f1) { if (paths.size() > 1) { // in the common case, the prim exists in both frames if ((index < paths[1].size()) && (paths[0][index] == paths[1][index])) { pos_f0 = pos_f1 = index; return true; } auto pathIt = std::find(paths[1].begin(), paths[1].end(), paths[0][index]); if (pathIt != paths[1].end()) { pos_f0 = index; // TODO: this isn't needed, can infer it pos_f1 = std::distance(paths[1].begin(), pathIt); return true; } } return false; } /** * @brief Returned by StageAtTime.getAttributeArrayRd * * @details Holds at most two samples (one from frame n, and one from frame n+1) * checkPathCorrespondence verifies if the path in frame n exists in frame n+1 * If no corresponding path exists, the value will be returned and not interpolated */ template <typename T> class AttributeArrayResult { public: size_t size() const { return m_samples[0].size(); } bool empty() const { return (size() == 0); } std::vector<gsl::span<const T>> const* data() const { return &m_samples; } std::vector<gsl::span<const T>>* data() { return &m_samples; } T operator[](const size_t valueIndex) const { { if (valueIndex >= m_samples[0].size() || m_samples[0].empty()) { CARB_LOG_WARN_ONCE("AttributeArrayResult[] out of bounds"); return T(); } if (m_samples.size() == 1) { return m_samples[0][valueIndex]; } else if (m_samples.size() == 2) { size_t pos0, pos1; if (checkPathCorrespondence(m_paths, valueIndex, pos0, pos1)) { T a = (m_samples[0][pos0]); T b = (m_samples[1][pos1]); T result = interpolate<T>(a, b, m_theta); return result; } return m_samples[0][valueIndex]; } } return T(); }; std::vector<gsl::span<const carb::flatcache::Path>> m_paths; std::vector<gsl::span<const T>> m_samples; float m_theta; }; /** * @brief Returned by StageAtTime.getArrayAttributeArrayRd * * @details Enables access to a vector of readily interpolated attribute values */ template <typename T> class AttributeArrayResult<std::vector<T>> { public: size_t size() const { return m_samples[0].size(); } bool empty() const { return (size() == 0); } std::vector<std::vector<gsl::span<const T>>> const* data() const { return m_samples; } std::vector<std::vector<gsl::span<const T>>>* data() { return m_samples; } std::vector<T> operator[](const size_t primIndex) { std::vector<T> interpolatedAttributeValues; if (m_samples.size() == 1) { interpolatedAttributeValues.resize(m_samples[0][primIndex].size()); std::copy(m_samples[0][primIndex].begin(), m_samples[0][primIndex].end(), interpolatedAttributeValues.begin()); return interpolatedAttributeValues; } else if (m_samples.size() == 2) { size_t pos0, pos1; if (checkPathCorrespondence(m_paths, primIndex, pos0, pos1)) { auto values_f0 = m_samples[0][primIndex]; auto values_f1 = m_samples[1][primIndex]; interpolatedAttributeValues.reserve(values_f0.size()); // interpolate attrib values for the requested {prim index : attrib val index} for (size_t valueIndex = 0; valueIndex < values_f0.size(); ++valueIndex) { T a = (values_f0[valueIndex]); T b = (values_f1[valueIndex]); T result = interpolate<T>(a, b, m_theta); interpolatedAttributeValues.emplace_back(result); } return interpolatedAttributeValues; } interpolatedAttributeValues.resize(m_samples[0][primIndex].size()); std::copy(m_samples[0][primIndex].begin(), m_samples[0][primIndex].end(), interpolatedAttributeValues.begin()); return interpolatedAttributeValues; } return std::vector<T>(); } std::vector<gsl::span<const carb::flatcache::Path>> m_paths; std::vector<std::vector<gsl::span<const T>>> m_samples; float m_theta; }; template <typename T> AttributeArrayResult<T> StageAtTime::getAttributeArrayRd(const PrimBucketList& primBucketList, size_t primBucketListIndex, const Token& attrName) const { size_t sampleCount = m_historyWindow.getTimeSampleCount(); if (sampleCount > 0) { AttributeArrayResult<T> arrAttRes; arrAttRes.m_samples = m_historyWindow.getAttributeArrayRd<T>(primBucketList, primBucketListIndex, attrName); arrAttRes.m_paths = m_historyWindow.getPathArray(primBucketList, primBucketListIndex); arrAttRes.m_theta = (float)m_theta; return arrAttRes; } else { CARB_LOG_WARN_ONCE( "getAttributeArrayRd %s: Data not available at time, possible dropped frame", attrName.getText()); return AttributeArrayResult<T>(); } } template <typename T> AttributeArrayResult<T> StageAtTime::getAttributeArrayRdGpu(const PrimBucketList& primBucketList, size_t primBucketListIndex, const Token& attrName) const { size_t sampleCount = m_historyWindow.getTimeSampleCount(); if (sampleCount > 0) { AttributeArrayResult<T> arrAttRes; arrAttRes.m_samples = m_historyWindow.getAttributeArrayRdGpu<T>(primBucketList, primBucketListIndex, attrName); arrAttRes.m_paths = m_historyWindow.getPathArray(primBucketList, primBucketListIndex); arrAttRes.m_theta = (float)m_theta; return arrAttRes; } else { CARB_LOG_WARN_ONCE( "getAttributeArrayRdGpu %s: Data not available at time, possible dropped frame", attrName.getText()); return AttributeArrayResult<T>(); } } inline std::vector<gsl::span<const char>> StageAtTime::getAttributeArrayRawRd(const PrimBucketList& primBucketList, size_t primBucketListIndex, const Token& attrName) const { return m_historyWindow.getAttributeArrayRawRd(primBucketList, primBucketListIndex, attrName); } template <typename T> AttributeArrayResult<std::vector<T>> StageAtTime::getArrayAttributeArrayRd( const PrimBucketList& primBucketList, size_t primBucketListIndex, const Token& attrName) const { size_t sampleCount = m_historyWindow.getTimeSampleCount(); AttributeArrayResult<std::vector<T>> result; if (sampleCount > 0) { result.m_samples = m_historyWindow.getArrayAttributeArrayRd<T>(primBucketList, primBucketListIndex, attrName); result.m_paths = m_historyWindow.getPathArray(primBucketList, primBucketListIndex); result.m_theta = (float)m_theta; return result; } else { CARB_LOG_WARN_ONCE( "getAttributeArrayRd %s: Data not available at time, possible dropped frame", attrName.getText()); return AttributeArrayResult<std::vector<T>>(); } } inline gsl::span<const Path> StageAtTime::getPathArray(const PrimBucketList& primBucketList, size_t primBucketListIndex) const { size_t sampleCount = m_historyWindow.getTimeSampleCount(); if (sampleCount == 1) { return m_historyWindow.getPathArray(primBucketList, primBucketListIndex)[0]; } else if (sampleCount == 0) { CARB_LOG_WARN_ONCE("getPathArray: Data not available at time, possible dropped frame"); return gsl::span<const Path>(); } else if (sampleCount == 2) { // TODO: make this correct when prims are being added and deleted // To do this we need to make a new array out: // out[i] = in0[i] , if in0[i] == in1[i] // = kUninitializedPath, otherwise return m_historyWindow.getPathArray(primBucketList, primBucketListIndex)[0]; #if 0 gsl::span<const Path> in0 = m_historyWindow.getPathArray(primBucketList, primBucketListIndex)[0]; gsl::span<const Path> in1 = m_historyWindow.getPathArray(primBucketList, primBucketListIndex)[1]; std::vector<Path> multiframePaths; for (size_t i = 0; i < in0.size(); ++i) in0[i] == in1[i] ? multiframePaths.emplace_back(in0[i]) : multiframePaths.emplace_back(flatcache::kUninitializedPath); return multiframePaths; #endif } return gsl::span<const Path>(); } inline std::vector<const Connection*> StageAtTime::getConnectionRd(const Path& path, const Token& connectionName) { return m_historyWindow.getConnectionRd(path, connectionName); } inline void StageAtTime::printBucketNames() const { m_historyWindow.printBucketNames(); } inline size_t StageAtTime::getAttributeCount(const PrimBucketList& primBucketList, size_t primBucketListIndex) const { std::vector<size_t> counts = m_historyWindow.getAttributeCounts(primBucketList, primBucketListIndex); if (counts.size() == 1) { return counts[0]; } // Perform a set intersection to get a valid count size; if (counts.size() == 2) { // // TODO: The attributes are internally sorted vectors, see flatcache::set. // Ideally we'd make a C-ABI type that makes it clear that these are sorted, // wrap with flatcache::set in the C++ wrapper and then use the standard library set intersection. // auto namesAndTypes = m_historyWindow.getAttributeNamesAndTypes(primBucketList, primBucketListIndex); const std::vector<std::vector<Token>>& names = namesAndTypes.first; const std::vector<std::vector<Type>>& types = namesAndTypes.second; std::vector<Token> intersection; // Perform a set intersection but we need to track the types as we intersect const std::vector<Token>& workingNames = names[0].size() < names[1].size() ? names[0] : names[1]; const std::vector<Type>& workingTypes = names[0].size() < names[1].size() ? types[0] : types[1]; const std::vector<Token>& testingNames = names[0].size() < names[1].size() ? names[1] : names[0]; const std::vector<Type>& testingTypes = names[0].size() < names[1].size() ? types[1] : types[0]; // Since attribute vectors are sorted we can track last spotted locations to be more efficient. size_t last = 0; for (size_t i = 0; i < workingNames.size(); ++i) { for (size_t j = last; j < testingNames.size(); ++j) { if (workingNames[i] == testingNames[j]) { if (workingTypes[i] == testingTypes[j]) { intersection.push_back(workingNames[i]); } // Store hit location to start next search last = j; break; } } } return intersection.size(); } return 0; } inline std::pair<std::vector<Token>, std::vector<Type>> StageAtTime::getAttributeNamesAndTypes( const PrimBucketList& primBucketList, size_t primBucketListIndex) const { std::vector<Token> outNames; std::vector<Type> outTypes; std::vector<std::vector<Token>> names; std::vector<std::vector<Type>> types; std::tie(names, types) = m_historyWindow.getAttributeNamesAndTypes(primBucketList, primBucketListIndex); if (names.size() == 1) { outNames = std::move(names[0]); outTypes = std::move(types[0]); } if (names.size() == 2) { // Assuming that the invariant that names and types of the same slot are the same count holds. outNames.reserve(std::min(names[0].size(), names[1].size())); outTypes.reserve(std::min(types[0].size(), types[1].size())); // Perform a set intersection but we need to track the types as we intersect std::vector<Token>& workingNames = names[0].size() < names[1].size() ? names[0] : names[1]; std::vector<Type>& workingTypes = names[0].size() < names[1].size() ? types[0] : types[1]; std::vector<Token>& testingNames = names[0].size() < names[1].size() ? names[1] : names[0]; std::vector<Type>& testingTypes = names[0].size() < names[1].size() ? types[1] : types[0]; // Since attribute vectors are sorted we can track last spotted locations to be more efficient. size_t last = 0; for (size_t i = 0; i < workingNames.size(); ++i) { for (size_t j = last; j < testingNames.size(); ++j) { if (workingNames[i] == testingNames[j]) { if (workingTypes[i] == testingTypes[j]) { outNames.push_back(workingNames[i]); outTypes.push_back(workingTypes[i]); } // Store hit location to start next search last = j; break; } } } } return { outNames, outTypes }; } inline uint64_t StageAtTime::writeCacheToDisk(const char* file, uint8_t* workingBuffer, uint64_t workingBufferSize) const { size_t sampleCount = m_historyWindow.getTimeSampleCount(); if (sampleCount != 1) { CARB_LOG_ERROR_ONCE("Can't call StageAtTime::WriteCacheToDisk for interpolated values"); return 0; } return m_historyWindow.writeCacheToDisk(file, workingBuffer, workingBufferSize); } inline void StageAtTime::addRefCount() { m_historyWindow.addRefCount(); } inline bool StageAtTime::removeRefCount() { return m_historyWindow.removeRefCount(); } inline unsigned int StageAtTime::getRefCount() { return m_historyWindow.getRefCount(); } // StageWithHistory implementation starts here inline StageWithHistory::StageWithHistory(UsdStageId usdStageId, size_t historyFrameCount, RationalTime simPeriod, bool withCuda) { auto iStageWithHistory = carb::getCachedInterface<carb::flatcache::IStageWithHistory>(); m_stageWithHistory = iStageWithHistory->create2(usdStageId, historyFrameCount, simPeriod, withCuda); m_usdStageId = usdStageId; } inline StageWithHistory::~StageWithHistory() { auto iStageWithHistory = carb::getCachedInterface<carb::flatcache::IStageWithHistory>(); iStageWithHistory->destroy(m_usdStageId); } inline ListenerId StageWithHistory::createListener() { auto iChangeTrackerConfig = carb::getCachedInterface<carb::flatcache::IStageWithHistory>(); ListenerId newId = iChangeTrackerConfig->createListener(); return newId; } // Templated methods do not get compiled unless they are instantiated. // The following code is not intended to be executed, it just instantiates each // templated method once to make sure that they compile. inline void instantiationTest(StageInProgress& stage, StageAtTimeInterval& stageAtInterval, StageAtTime& stageAtTime, const Path& path, const Token& attrName) { int* x0 = stage.getAttribute<int>(path, attrName); CARB_UNUSED(x0); const int* x1 = stage.getAttributeRd<int>(path, attrName); CARB_UNUSED(x1); int* x2 = stage.getAttributeWr<int>(path, attrName); CARB_UNUSED(x2); gsl::span<int> x3 = stage.getArrayAttribute<int>(path, attrName); CARB_UNUSED(x3); gsl::span<const int> x4 = stage.getArrayAttributeRd<int>(path, attrName); CARB_UNUSED(x4); gsl::span<int> x5 = stage.getArrayAttributeWr<int>(path, attrName); CARB_UNUSED(x5); PrimBucketList pbl = stage.findPrims({}, {}, {}); gsl::span<int> x6 = stage.getAttributeArray<int>(pbl, 0, attrName); CARB_UNUSED(x6); std::vector<const int*> x7 = stageAtInterval.getAttributeRd<int>(path, attrName); CARB_UNUSED(x7); std::vector<gsl::span<const int>> x8 = stageAtInterval.getAttributeArrayRd<int>(pbl, 0, attrName); CARB_UNUSED(x8); optional<float> x9 = stageAtTime.getAttributeRd<float>(path, attrName); CARB_UNUSED(x9); optional<std::pair<optional<int>, optional<int>>> x10 = stageAtTime.getAttributeRd <std::pair<optional<int>, optional<int>>>(path, attrName); CARB_UNUSED(x10); carb::flatcache::AttributeArrayResult<int> x11 = stageAtTime.getAttributeArrayRd<int>(pbl, 0, attrName); CARB_UNUSED(x11); carb::flatcache::AttributeArrayResult<std::vector<int>> x12 = stageAtTime.getArrayAttributeArrayRd<int>(pbl, 0, attrName); CARB_UNUSED(x12); } } // namespace flatcache } // namespace carb
74,405
C
37.176501
153
0.672603
omniverse-code/kit/fabric/include/carb/flatcache/IPath.h
// Copyright (c) 2019-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. // #pragma once #include <carb/Framework.h> #include <carb/Interface.h> #include <carb/flatcache/IToken.h> #include <carb/flatcache/Intrinsics.h> #include <functional> // Set to empty macro when IPath::iPath static member is removed #define FLATCACHE_IPATH_INIT \ const carb::flatcache::IPath* carb::flatcache::Path::iPath = nullptr; namespace carb { namespace flatcache { // PathC are integer keys that identify paths to C-ABI interfaces struct PathC { uint64_t path; constexpr bool operator<(const PathC& other) const { return path < other.path; } constexpr bool operator==(const PathC& other) const { return path == other.path; } constexpr bool operator!=(const PathC& other) const { return path != other.path; } }; static_assert(std::is_standard_layout<PathC>::value, "Struct must be standard layout as it is used in C-ABI interfaces"); // We don't reference count the uninitialized (or empty) path, and we use // this fact to avoid unnecessary dll calls to addRef()/removeRef(), for // example during std::vector resize. To do this we need to check whether a // path is uninitialized without the dll call getEmptyPath(), so we store // its value here in a constant. // We run automated test "IPath::getEmptyPath() dll call can be replaced with // constant, Path::kUninitializedPath" to ensure that this constant never // changes. static constexpr PathC kUninitializedPath{0}; // C-ABI interface to pxr::SdfPath struct IPath { CARB_PLUGIN_INTERFACE("carb::flatcache::IPath", 0, 1); PathC (*getHandle)(const char* name); const char* (*getText)(PathC handle); PathC (*getParent)(PathC handle); PathC (*appendChild)(PathC handle, TokenC childName); void (*addRef)(PathC handle); void (*removeRef)(PathC handle); PathC (*getEmptyPath)(); // Creates a path by appending a given relative path to this path. PathC (*appendPath)(PathC handle, PathC path); // Returns the number of path elements in this path. uint32_t (*getPathElementCount)(PathC handle); }; // C++ wrapper for IPath class Path { static carb::flatcache::IPath& sIPath(); public: // DEPRECATED: keeping for binary compatibility // Will be removed in October 2021 - @TODO set FLATCACHE_IPATH_INIT to empty macro when removed! // Still safe to use if initialized in a given dll static const carb::flatcache::IPath* iPath; Path() : mHandle(kUninitializedPath) { } Path(const char* path) { mHandle = sIPath().getHandle(path); } // Needs to be noexcept for std::vector::resize() to move instead of copy ~Path() noexcept { // We see the compiler construct and destruct many uninitialized // temporaries, for example when resizing std::vector. // We don't want to do an IPath dll call for these, so skip if handle // is uninitialized. if (mHandle != kUninitializedPath) { sIPath().removeRef(mHandle); } } // Copy constructor Path(const Path& other) : mHandle(other.mHandle) { if (mHandle != kUninitializedPath) { sIPath().addRef(mHandle); } } // Copy construct from integer Path(PathC handle) : mHandle(handle) { if (mHandle != kUninitializedPath) { sIPath().addRef(mHandle); } } // Move constructor // Needs to be noexcept for std::vector::resize() to move instead of copy Path(Path&& other) noexcept { // We are moving the src handle so don't need to change its refcount mHandle = other.mHandle; // Make source invalid other.mHandle = kUninitializedPath; } // Copy assignment Path& operator=(const Path& other) { if (this != &other) { if (mHandle != kUninitializedPath) { sIPath().removeRef(mHandle); } if (other.mHandle != kUninitializedPath) { sIPath().addRef(other.mHandle); } } mHandle = other.mHandle; return *this; } // Move assignment Path& operator=(Path&& other) noexcept { if (&other == this) return *this; // We are about to overwrite the dest handle, so decrease its refcount if (mHandle != kUninitializedPath) { sIPath().removeRef(mHandle); } // We are moving the src handle so don't need to change its refcount mHandle = other.mHandle; other.mHandle = kUninitializedPath; return *this; } const char* getText() const { return sIPath().getText(mHandle); } constexpr bool operator<(const Path& other) const { return mHandle < other.mHandle; } constexpr bool operator!=(const Path& other) const { return mHandle != other.mHandle; } constexpr bool operator==(const Path& other) const { return mHandle == other.mHandle; } constexpr operator PathC() const { return mHandle; } private: PathC mHandle; }; static_assert(std::is_standard_layout<Path>::value, "Path must be standard layout as it is used in C-ABI interfaces"); #ifndef __CUDACC__ inline carb::flatcache::IPath& Path::sIPath() { // Acquire carbonite interface on first use carb::flatcache::IPath* iPath = carb::getCachedInterface<carb::flatcache::IPath>(); CARB_ASSERT(iPath); return *iPath; } #endif // __CUDACC__ } } namespace std { template <> class hash<carb::flatcache::PathC> { public: inline size_t operator()(const carb::flatcache::PathC& key) const { // lower 8 bits have no entropy, so just remove the useless bits return key.path >> 8; } }; template <> class hash<carb::flatcache::Path> { public: inline size_t operator()(const carb::flatcache::Path& key) const { return std::hash<carb::flatcache::PathC>()(carb::flatcache::PathC(key)); } }; }
6,493
C
26.171548
121
0.639458
omniverse-code/kit/fabric/include/carb/flatcache/FlatCacheUSD.h
// Copyright (c) 2020, 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. // #pragma once #include <carb/flatcache/IPath.h> #include <carb/logging/Log.h> #include <pxr/base/tf/token.h> #include <pxr/usd/sdf/path.h> namespace carb { namespace flatcache { // asInt() is the same as SdfPath::_AsInt() // Flatcache relies on asInt(a)==asInt(b) <=> a is same path as b, // which is how SdfPath::operator== is currently defined. // If USD changes sizeof(pxr::SdfPath), we will need to change PathC to make it // the same size. inline PathC asInt(const pxr::SdfPath& path) { static_assert(sizeof(pxr::SdfPath) == sizeof(PathC), "Change PathC to make the same size as pxr::SdfPath"); PathC ret; std::memcpy(&ret, &path, sizeof(pxr::SdfPath)); return ret; } inline const PathC* asInt(const pxr::SdfPath* path) { static_assert(sizeof(pxr::SdfPath) == sizeof(PathC), "Change PathC to make the same size as pxr::SdfPath"); return reinterpret_cast<const PathC*>(path); } inline TokenC asInt(const pxr::TfToken& token) { static_assert(sizeof(pxr::TfToken) == sizeof(TokenC), "Change TokenC to make the same size as pxr::TfToken"); TokenC ret; std::memcpy(&ret, &token, sizeof(pxr::TfToken)); return ret; } inline const TokenC* asInt(const pxr::TfToken* token) { static_assert(sizeof(pxr::TfToken) == sizeof(TokenC), "Change TokenC to make the same size as pxr::TfToken"); return reinterpret_cast<const TokenC*>(token); } // Return reference to ensure that reference count doesn't change inline const pxr::TfToken& intToToken(const TokenC& token) { static_assert(sizeof(pxr::TfToken) == sizeof(TokenC), "Change TokenC to make the same size as pxr::TfToken"); return reinterpret_cast<const pxr::TfToken&>(token); } inline const pxr::SdfPath& intToPath(const PathC& path) { static_assert(sizeof(pxr::SdfPath) == sizeof(PathC), "Change PathC to make the same size as pxr::SdfPath"); return reinterpret_cast<const pxr::SdfPath&>(path); } inline const pxr::SdfPath* intToPath(const Path* path) { static_assert(sizeof(pxr::SdfPath) == sizeof(Path), "Change Path to make the same size as pxr::SdfPath"); return reinterpret_cast<const pxr::SdfPath*>(path); } } }
2,585
C
31.325
113
0.716828
omniverse-code/kit/fabric/include/carb/flatcache/PrimChanges.h
// Copyright (c) 2020-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. // #pragma once #include <carb/flatcache/AttrNameAndType.h> #include <carb/flatcache/IFlatcache.h> #include <carb/flatcache/IPath.h> #include <gsl/span> #include <cstddef> namespace carb { namespace flatcache { struct AttrAndChangedIndices { AttrNameAndType attr; // For which prims did this attribute change? bool allIndicesChanged; gsl::span<const size_t> changedIndices; }; struct BucketChanges { // For each attribute, which prims changed? std::vector<AttrAndChangedIndices> attrChangedIndices; gsl::span<const Path> pathArray; BucketChanges() = default; BucketChanges(BucketChangesC in) : pathArray({ in.pathArray.ptr,in.pathArray.elementCount }) { size_t count = in.changedIndices.elementCount; attrChangedIndices.resize(count); for (size_t i = 0; i != count; i++) { const ConstChangedIndicesC& inAttrChanges = in.changedIndices.ptr[i]; attrChangedIndices[i].attr = in.changedAttributes.ptr[i]; attrChangedIndices[i].allIndicesChanged = inAttrChanges.allIndicesChanged; attrChangedIndices[i].changedIndices = gsl::span<const size_t>(inAttrChanges.changedIndices.ptr, inAttrChanges.changedIndices.elementCount); } } }; class AddedPrimIndices { // Which prims were added? gsl::span<const size_t> addedIndices; public: AddedPrimIndices(AddedPrimIndicesC in) { addedIndices = gsl::span<const size_t>(in.addedIndices.ptr, in.addedIndices.elementCount); } size_t size() const { return addedIndices.size(); } // This iterator first iterates over the deletedElements that were replaced // by new elements, then the contiguous range of elements added at the end // of the bucket struct iterator { using iterator_category = std::input_iterator_tag; using difference_type = size_t; using value_type = size_t; using reference = size_t; iterator( gsl::span<const size_t>::iterator _addedIndicesIterator, gsl::span<const size_t>::iterator _addedIndicesEnd) : addedIndicesIterator(_addedIndicesIterator), addedIndicesEnd(_addedIndicesEnd) {} reference operator*() const { return *addedIndicesIterator; } iterator& operator++() { addedIndicesIterator++; return *this; } bool operator==(iterator other) const { return addedIndicesIterator == other.addedIndicesIterator; } bool operator!=(iterator other) const { return !(*this == other); } difference_type operator-(iterator other) { return addedIndicesIterator - other.addedIndicesIterator; } private: gsl::span<const size_t>::iterator addedIndicesIterator; gsl::span<const size_t>::iterator addedIndicesEnd; }; iterator begin() { return iterator(addedIndices.begin(), addedIndices.end()); } iterator end() { return iterator(addedIndices.end(), addedIndices.end()); } }; } }
3,624
C
26.462121
117
0.656457
omniverse-code/kit/fabric/include/carb/flatcache/Intrinsics.h
// Copyright (c) 2019-2022, 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. // #pragma once #include <cstdint> #include <cstdlib> #include <cstddef> #include <carb/flatcache/Defines.h> #if USING( WINDOWS_BUILD ) #include <intrin.h> #elif USING( LINUX_BUILD ) // #if USING( WINDOWS_BUILD ) // no linux-specific includes at this time #else // #if USING( WINDOWS_BUILD ) #error "Unsupported platform" #endif // #if USING( WINDOWS_BUILD ) namespace carb { namespace flatcache { inline uint32_t clz32( const uint32_t x ) { #if USING( WINDOWS_BUILD ) DWORD z; return _BitScanReverse( &z, x ) ? 31 - z : 32; #elif USING( LINUX_BUILD ) // #if USING( WINDOWS_BUILD ) return x ? __builtin_clz( x ) : 32; #else // #if USING( WINDOWS_BUILD ) #error "Unsupported platform" #endif // #if USING( WINDOWS_BUILD ) } inline uint32_t clz64( const uint64_t x ) { #if USING( WINDOWS_BUILD ) DWORD z; return _BitScanReverse64( &z, x ) ? 63 - z : 64; #elif USING( LINUX_BUILD ) // #if USING( WINDOWS_BUILD ) return x ? __builtin_clzll( x ) : 64; #else // #if USING( WINDOWS_BUILD ) #error "Unsupported platform" #endif // #if USING( WINDOWS_BUILD ) } inline uint32_t ctz32( const uint32_t x ) { #if USING( WINDOWS_BUILD ) DWORD z; return _BitScanForward( &z, x ) ? z : 32; #elif USING( LINUX_BUILD ) // #if USING( WINDOWS_BUILD ) return x ? __builtin_ctz( x ) : 32; #else // #if USING( WINDOWS_BUILD ) #error "Unsupported platform" #endif // #if USING( WINDOWS_BUILD ) } inline uint32_t ctz64( const uint64_t x ) { #if USING( WINDOWS_BUILD ) DWORD z; return _BitScanForward64( &z, x ) ? z : 64; #elif USING( LINUX_BUILD ) // #if USING( WINDOWS_BUILD ) return x ? __builtin_ctzll( x ) : 64; #else // #if USING( WINDOWS_BUILD ) #error "Unsupported platform" #endif // #if USING( WINDOWS_BUILD ) } inline uint64_t bswap64( const uint64_t x ) { #if USING( WINDOWS_BUILD ) return _byteswap_uint64( x ); #elif USING( LINUX_BUILD ) // #if USING( WINDOWS_BUILD ) return __builtin_bswap64 ( x ); #else // #if USING( WINDOWS_BUILD ) #error "Unsupported platform" #endif // #if USING( WINDOWS_BUILD ) } inline uint64_t rotr64( const uint64_t value, const int shift ) { #if USING( WINDOWS_BUILD ) return _rotr64( value, shift ); #elif USING( LINUX_BUILD ) // #if USING( WINDOWS_BUILD ) return (value >> shift) | (value << (64 - shift)); #else // #if USING( WINDOWS_BUILD ) #error "Unsupported platform" #endif // #if USING( WINDOWS_BUILD ) } inline uint64_t rotl64( const uint64_t value, const int shift ) { #if USING( WINDOWS_BUILD ) return _rotl64( value, shift ); #elif USING( LINUX_BUILD ) // #if USING( WINDOWS_BUILD ) return (value << shift) | (value >> (64 - shift)); #else // #if USING( WINDOWS_BUILD ) #error "Unsupported platform" #endif // #if USING( WINDOWS_BUILD ) } } // namespace flatcache } // namespace carb
3,199
C
27.318584
77
0.680213
omniverse-code/kit/fabric/include/carb/flatcache/FlatCache.h
// Copyright (c) 2019-2022, 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. // #pragma once #include <carb/Interface.h> #include <carb/flatcache/PathToAttributesMap.h> namespace carb { namespace flatcache { // Callers of createCache() and getCache() can store anything they want in // UserId. For example, OmniGraph uses it to store the OmniGraph pointer. struct UserId { uint64_t id; bool operator<(UserId b) const { return id < b.id; } bool operator==(UserId b) const { return id == b.id; } bool operator!=(UserId b) const { return id != b.id; } }; constexpr UserId kDefaultUserId = { 0 }; constexpr UserId kInvalidUserId = { ~uint64_t(0) }; // Flatcache has the option to save a finite number of frames of history, // organized as a ringbuffer. This is typically used to buffer data between // simulation rendering. The simplest case, double buffering, allows simulation // and rendering to run in parallel, each running for the full frame. // Longer buffers can be used to feed one or more renderers running at // different rates to simulation. // To enable this feature, pass CacheType::eWithHistory to createCache(), // otherwise pass CacheType::eWithoutHistory. // Multiple caches can be created for each UsdStageId, but at most one can have // history. enum class CacheType { eWithHistory, eWithoutHistory, eWithoutHistoryAndWithCuda, eWithHistoryAndCuda }; struct FlatCache { CARB_PLUGIN_INTERFACE("carb::flatcache::FlatCache", 0, 4); // Abstractly, a flatcache maps USD paths to USD attributes, just like a // UsdStage does. // Concretely we represent a flatcache by objects of type PathToAttributesMap. // This method creates a PathToAttributesMap for a given stage, but doesn't // populate it with values. This allows the cache to be filled lazily as // values are needed. // Instead, it traverses the given Usd stage making an index of paths to // attributes. // The cache uses the index to organize data into contiguous arrays, // and also allows you to find prims by type and/or attribute without // traversing the stage. // This method also specifies the stage to be used by calls to usdToCache() // and cacheToUsd(). PathToAttributesMap&(CARB_ABI* createCache)(UsdStageId usdStageId, UserId userId, CacheType cacheType); void(CARB_ABI* addPrimToCache)(PathToAttributesMap& cache, const pxr::UsdPrim& prim, const std::set<TokenC>& filter); // Destroy the cache associated with the given stage. void(CARB_ABI* destroyCache)(UsdStageId usdStageId, UserId userId); // Prefetch the whole USD stage to the cache // Typically you only call this at stage load time, because the USD notify // handler updates the cache if the stage changes. void(CARB_ABI* usdToCache)(PathToAttributesMap& cache); // Write back all dirty cached data to the USD stage. // If your renderer doesn't use the cache then you need to do this // before rendering. void(CARB_ABI* cacheToUsd)(PathToAttributesMap& cache); // Write back only one bucket to usd void(CARB_ABI* cacheBucketToUsd)(PathToAttributesMap& cache, BucketId bucketId, bool skipMeshPoints); TypeC(CARB_ABI* usdTypeToTypeC)(pxr::SdfValueTypeName usdType); PathToAttributesMap*(CARB_ABI* getCache)(UsdStageId usdStageId, UserId userId); pxr::SdfValueTypeName(CARB_ABI* typeCtoUsdType)(TypeC typeC); size_t(CARB_ABI* getUsdTypeCount)(); void(CARB_ABI* getAllUsdTypes)(TypeC* outArray, size_t outArraySize); /** @brief Import a prim in cache */ void(CARB_ABI* addPrimToCacheNoOverwrite)(PathToAttributesMap& cache, const pxr::UsdPrim& prim, const std::set<TokenC>& filter); void(CARB_ABI* initStaticVariables)(); void(CARB_ABI* exportUsd)(PathToAttributesMap& cache, pxr::UsdStageRefPtr usdStage, const double* timeCode, const double* prevTimeCode); /** @brief Attempt to serialize the cache into the specified buffer. * * @cache[in] The cache to serialize * @dest[in/out] Pointer to buffer to be written to, will start writing to head * of pointer. dest will be left pointing to the point after the last write * @destSize Size of buffer that was allocated for the data (in bytes) * @pathStringCache - looking up strings is slow, yo * * @return Number of bytes written success is determined by (return <= @destSize) * * * @invariant It is safe to write to any memory within[dest, dest+size] for the * duration of the function call. * * @note If the cache will not fit into the size of memory allocated in * @dest then it will stop writing, but continue to run the serialize * algorithm to calculate the actual amount of data that needs to be * written * * @Todo : make cache const - not possible because serializeMirroredAray is not * const, however, that is because getArraySpanC is used which also doesn't * have a const version, so that needs to be addressed first, this is because * in the call stack we end up with a copy from GPU -> CPU which would need to * be avoided */ uint64_t(CARB_ABI* serializeCache)(PathToAttributesMap& cache, uint8_t* dest, size_t destSize, SerializationCache& pathStringCache); /** @brief Given a buffer that has the serialized version of a cache written * using the serialize function, this function will override all the data * in the cache with the data from the buffer * * @cache[in/out] Reference to the cache to be populated * @pathCache[in/out] Looking up SDFPath via string can be expensive to it * is worthwhile to cache this data across many repeated * calls. * @input[in] Pointer to buffer of data containing serialized cache * @inputSize[in] Size of data in the buffer * @skipStageConfirmation[in] Whether we should skip making sure the destination stage is open. * * @return True if buffer was successfully de-serialized * * @note : this currently has to clear the cache before it is populated which is a possibly * expensive operation * * @TODO: whould we care that it came from the same version of the USD file? */ bool(CARB_ABI* deserializeCache)( PathToAttributesMap& destStage, DeserializationCache& pathCache, const uint8_t* input, const size_t inputSize, bool skipStageConfirmation); /** @brief Write a cache file to disk at a specified location * * @note many parameters to this function are optional * @cache[in] That cache to be written to disk * @file[in The location the file is desired to be written to * @workingBuffer[in] [Optional] In order to avoid costly reallocations * the code will attempt to re-use the memory at the buffer * location if it is large enough. If the buffer isn't larg * enough the cost of allocation, and re-traversal may be paid * @workingBufferSize[in] [Optional] If workingBuffer is non null, then this desrcibes the length * of the buffer * @return The amount of data needed to serialize the cache, a return value of 0 indicates an error * */ uint64_t(CARB_ABI* writeCacheToDisk)(PathToAttributesMap& cache, const char* file, uint8_t* workingBuffer, uint64_t workingBufferSize); /** @brief Read a cache file from the specified location * * @file[in] The location the file is desired to be written to * @cache[in/out] That cache to be populates * @pathCache[in/out] Looking up SDFPath via string can be expensive to it * is worthwhile to cache this data across many repeated * calls. * @buffer[in/out] Buffer to use to read the cache file in to, passed to * allow reuse than allocate per call. Will be resized if not large enough. * @return Whether the read was successful * */ bool(CARB_ABI* readCacheFromDisk)(PathToAttributesMap& cache, const char* fileName, DeserializationCache& pathCache, std::vector<uint8_t>& buffer); /** @brief Enable/Disable change notifications on USD changes. * * @enable[in] True/False enable notifications * */ void(CARB_ABI* setEnableChangeNotifies)(bool enable); /** @brief Return whether change notifications on USD changes is enabled. * * @return True if change notifications on USD changes is enabled, else False. * */ bool(CARB_ABI* getEnableChangeNotifies)(); /** @brief make buckets for all prims on a USD stage, but only if this * hasn't been done before. * * This is used to lazily create an index of all prims on a stage, without * the time or memory cost of fetching all the attribute values. The user * can then use findPrims to, for example, find all the prims of a * particular type. * * If a SimStageWithHistory hasn't been created for this stage then a * warning will be printed and no population will be done. * * @cache[in] The PathToAttributesMap to populate */ void(CARB_ABI* minimalPopulateIfNecessary)(PathToAttributesMap& cache); }; } }
9,956
C
40.144628
140
0.677581
omniverse-code/kit/fabric/include/carb/flatcache/Allocator.h
#pragma once #include <cmath> #include <carb/logging/Log.h> #include <carb/Defines.h> #include <carb/flatcache/Defines.h> #include <carb/flatcache/Intrinsics.h> #define ALLOCATOR_HEADER USE_IF( USING( DEVELOPMENT_BUILD ) ) #define ALLOCATOR_STATS USE_IF( USING( ALLOCATOR_HEADER ) ) // requires Header's byte tracking per-allocation #define ALLOCATOR_LEAK_CHECK USE_IF( USING( ALLOCATOR_HEADER ) ) // requires Header's byte tracking per-allocation namespace carb { namespace flatcache { inline const char* humanReadableSize( const uint64_t bytes ) noexcept { auto va = [](auto ...params) -> const char* { static char tmp[1024]; #ifdef _WIN32 _snprintf_s(tmp, sizeof(tmp), params...); #else snprintf(tmp, sizeof(tmp), params...); #endif return (const char*)&tmp; }; constexpr const char SIZE_UNITS[64][3]{ " B", " B", " B", " B", " B", " B", " B", " B", " B", " B", "KB", "KB", "KB", "KB", "KB", "KB", "KB", "KB", "KB", "KB", "MB", "MB", "MB", "MB", "MB", "MB", "MB", "MB", "MB", "MB", "GB", "GB", "GB", "GB", "GB", "GB", "GB", "GB", "GB", "GB", "TB", "TB", "TB", "TB", "TB", "TB", "TB", "TB", "TB", "TB", "PB", "PB", "PB", "PB", "PB", "PB", "PB", "PB", "PB", "PB", "EB", "EB", "EB", "EB" }; static constexpr size_t B = 1ull; static constexpr size_t KB = 1024ull; static constexpr size_t MB = (1024ull*1024ull); static constexpr size_t GB = (1024ull*1024ull*1024ull); static constexpr size_t TB = (1024ull*1024ull*1024ull*1024ull); static constexpr size_t PB = (1024ull*1024ull*1024ull*1024ull*1024ull); static constexpr size_t EB = (1024ull*1024ull*1024ull*1024ull*1024ull*1024ull); constexpr const size_t SIZE_BASE[64]{ B, B, B, B, B, B, B, B, B, B, KB, KB, KB, KB, KB, KB, KB, KB, KB, KB, MB, MB, MB, MB, MB, MB, MB, MB, MB, MB, GB, GB, GB, GB, GB, GB, GB, GB, GB, GB, TB, TB, TB, TB, TB, TB, TB, TB, TB, TB, PB, PB, PB, PB, PB, PB, PB, PB, PB, PB, EB, EB, EB, EB }; const uint32_t power = bytes ? ( 64u - clz64( bytes ) ) - 1u : 0; const char *const units = SIZE_UNITS[power]; const size_t base = SIZE_BASE[power]; const size_t count = bytes / base; return va("%zu %s", count, units); } // A wrapper around malloc/free that aims to: // // * Cheaply track allocation counts and bytes, and detect leaks automatically at ~Allocator() // // * Cheaply track usage in terms of peak memory usage, and total lifetime usage broken down by size. Sample output: // dumped to console appears like so: // // == Allocator 0x000000E67BEFCEA0 Stats == // allocCount: 0 // allocBytes: 0 B // peakAllocCount: 4002 // peakAllocBytes: 4 GB // minAllocBytes: 312 B // maxAllocBytes: 6 MB // // Lifetime Allocation Histogram: // Normalized over TOTAL allocations: 13956 // < 512 B|***** 29% 4002 // < 1 KB| 0% 0 // < 2 KB| 0% 0 // < 4 KB| 0% 0 // < 8 KB|*** 14% 2000 // < 16 KB| 0% 0 // < 32 KB|*** 14% 1994 // < 64 KB| 0% 0 // < 128 KB|*** 14% 1976 // < 256 KB| 0% 0 // < 512 KB|*** 14% 1904 // < 1 MB| 0% 0 // < 2 MB|** 12% 1616 // < 4 MB| 0% 0 // < 8 MB|* 3% 464 // ======================== struct Allocator { Allocator(); ~Allocator(); void* alloc(const size_t bytes); void free(void *const ptr); template<typename T, typename ...Params> T* new_(Params&& ...params); template<typename T> void delete_(T*const t); void resetStats() noexcept; void reportUsage() noexcept; bool checkLeaks() noexcept; private: #if USING( ALLOCATOR_HEADER ) struct BlockHeader { size_t bytes; }; #endif // #if USING( ALLOCATOR_HEADER ) #if USING( ALLOCATOR_STATS ) || USING( ALLOCATOR_LEAK_CHECK ) size_t allocCount; size_t allocBytes; #endif // #if USING( ALLOCATOR_STATS ) || USING( ALLOCATOR_LEAK_CHECK ) #if USING( ALLOCATOR_STATS ) size_t peakAllocCount; size_t peakAllocBytes; size_t minAllocBytes; size_t maxAllocBytes; static constexpr size_t ALLOC_BUCKET_COUNT = 65; size_t lifetimeAllocCount; size_t lifetimeAllocBuckets[ALLOC_BUCKET_COUNT]; #endif // #if USING( ALLOCATOR_STATS ) }; struct AllocFunctor { Allocator *allocator; void* operator()(const size_t bytes) { CARB_ASSERT(allocator); return allocator->alloc(bytes); } }; struct FreeFunctor { Allocator *allocator; void operator()(void *const ptr) { CARB_ASSERT(allocator); return allocator->free(ptr); } }; inline Allocator::Allocator() { #if USING( ALLOCATOR_STATS ) || USING( ALLOCATOR_LEAK_CHECK ) allocCount = 0; allocBytes = 0; #endif // #if USING( ALLOCATOR_STATS ) || USING( ALLOCATOR_LEAK_CHECK ) resetStats(); } inline Allocator::~Allocator() { checkLeaks(); reportUsage(); } inline void* Allocator::alloc(const size_t bytes) { #if USING( ALLOCATOR_HEADER ) const size_t totalBytes = bytes + sizeof(BlockHeader); #endif // #if USING( ALLOCATOR_HEADER ) #if USING( ALLOCATOR_STATS ) || USING( ALLOCATOR_LEAK_CHECK ) CARB_ASSERT((allocCount + 1) > allocCount); CARB_ASSERT((allocBytes + totalBytes) > allocBytes); ++allocCount; allocBytes += totalBytes; #endif // #if USING( ALLOCATOR_STATS ) || USING( ALLOCATOR_LEAK_CHECK ) #if USING( ALLOCATOR_STATS ) if ( allocBytes > peakAllocBytes ) { peakAllocBytes = allocBytes; peakAllocCount = allocCount; } if ( totalBytes < minAllocBytes ) { minAllocBytes = totalBytes; } if ( totalBytes > maxAllocBytes ) { maxAllocBytes = totalBytes; } const uint32_t bucket = ( 64u - clz64( totalBytes - 1ull ) ); CARB_ASSERT(lifetimeAllocBuckets[bucket] + 1 > lifetimeAllocBuckets[bucket]); ++lifetimeAllocBuckets[bucket]; ++lifetimeAllocCount; #endif // #if USING( ALLOCATOR_STATS ) #if USING( ALLOCATOR_HEADER ) BlockHeader *const header = (BlockHeader*)malloc(totalBytes); CARB_ASSERT(header); header->bytes = totalBytes; return header+1; #else // #if USING( ALLOCATOR_HEADER ) return malloc(bytes); #endif // #if USING( ALLOCATOR_STATS ) } inline void Allocator::free(void *const ptr) { #if USING( ALLOCATOR_HEADER ) CARB_ASSERT(ptr); BlockHeader *header = (BlockHeader*)ptr; --header; #if USING( ALLOCATOR_STATS ) || USING( ALLOCATOR_LEAK_CHECK ) const size_t totalBytes = header->bytes; CARB_ASSERT((allocCount - 1) < allocCount); CARB_ASSERT((allocBytes - totalBytes) < allocBytes); --allocCount; allocBytes -= totalBytes; #endif // #if USING( ALLOCATOR_STATS ) || USING( ALLOCATOR_LEAK_CHECK ) ::free(header); #else // #if USING( ALLOCATOR_STATS ) ::free(ptr); #endif // #if USING( ALLOCATOR_STATS ) } template<typename T, typename ...Params> inline T* Allocator::new_(Params&& ...params) { T *const t = (T*)Allocator::alloc(sizeof(T)); new (t) T(std::forward<Params>(params)...); return t; } template<typename T> inline void Allocator::delete_(T*const t) { CARB_ASSERT(t); t->~T(); #if USING( ALLOCATOR_HEADER ) BlockHeader *header = (BlockHeader*)t; header--; CARB_ASSERT(header->bytes == (sizeof(BlockHeader) + sizeof(T))); #endif // #if USING( ALLOCATOR_HEADER ) Allocator::free(t); } inline void Allocator::resetStats() noexcept { #if USING( ALLOCATOR_STATS ) peakAllocCount = 0; peakAllocBytes = 0; minAllocBytes = SIZE_MAX; maxAllocBytes = 0; lifetimeAllocCount = 0; for ( size_t i = 0; i < ALLOC_BUCKET_COUNT; ++i ) { lifetimeAllocBuckets[i] = 0; } #endif // #if USING( ALLOCATOR_STATS ) } inline void Allocator::reportUsage() noexcept { #if USING( ALLOCATOR_STATS ) CARB_LOG_INFO("== Allocator 0x%p Stats ==", this); if (!lifetimeAllocCount) { CARB_LOG_INFO("<no stats to report; unused allocator>"); CARB_LOG_INFO("========================"); return; } CARB_LOG_INFO("allocCount: %12zu", allocCount); CARB_LOG_INFO("allocBytes: %15s", humanReadableSize(allocBytes)); CARB_LOG_INFO("peakAllocCount: %12zu", peakAllocCount); CARB_LOG_INFO("peakAllocBytes: %15s", humanReadableSize(peakAllocBytes)); CARB_LOG_INFO("minAllocBytes: %15s", humanReadableSize(minAllocBytes)); CARB_LOG_INFO("maxAllocBytes: %15s", humanReadableSize(maxAllocBytes)); CARB_LOG_INFO(""); CARB_LOG_INFO("Lifetime Allocation Histogram:"); size_t begin = 0; for ( ; begin < ALLOC_BUCKET_COUNT; ++begin ) { if ( lifetimeAllocBuckets[begin] ) { break; } } size_t end = 0; for ( ; end < ALLOC_BUCKET_COUNT; ++end ) { if ( lifetimeAllocBuckets[ALLOC_BUCKET_COUNT - end - 1] ) { end = ALLOC_BUCKET_COUNT - end; break; } } CARB_LOG_INFO(" Normalized over TOTAL allocations: %zu", lifetimeAllocCount); size_t i; float normalized[ALLOC_BUCKET_COUNT]; for ( i = begin; i < end; ++i ) { normalized[i] = (float)lifetimeAllocBuckets[i] / (float)lifetimeAllocCount; } constexpr size_t WIDTH = 16; for ( i = begin; i < end; ++i ) { char buf[WIDTH+1] = {}; const size_t w = ( size_t )std::ceil(WIDTH * normalized[i]); for( size_t j = 0; j < w; ++j) { buf[j] = '*'; } static_assert(WIDTH == 16, "Fix CARB_LOG_INFO below"); CARB_LOG_INFO(" <%7s|%-16s %3.0f%% %12zu", humanReadableSize(1ull<<i), buf, (normalized[i] * 100.f), lifetimeAllocBuckets[i]); } CARB_LOG_INFO("========================"); #endif // #if USING( ALLOCATOR_STATS ) } inline bool Allocator::checkLeaks() noexcept { #if USING( ALLOCATOR_LEAK_CHECK ) if (allocCount || allocBytes) { CARB_LOG_ERROR("PathToAttributesMap detected a memory leak of %s!\n", humanReadableSize(allocBytes)); CARB_ASSERT(false, "PathToAttributesMap detected a memory leak of %s!\n", humanReadableSize(allocBytes)); return true; } #endif // #if USING( ALLOCATOR_LEAK_CHECK ) return false; } } // namespace flatcache } // namespace carb
10,798
C
28.425068
135
0.571217
omniverse-code/kit/fabric/include/carb/flatcache/InterpolationUsd.h
// Copyright (c) 2020-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. // #pragma once // This include must come first // clang-format off #include "UsdPCH.h" // clang-format on #include <pxr/base/gf/matrix4d.h> #include <pxr/base/gf/quatf.h> #include "carb/logging/Log.h" /** * @brief Defined in a separate location to the other lerp functions * in order to avoid breaking C-ABI compatibility */ namespace carb { namespace flatcache { /** * @brief Spherical interpolation specialization relying on pxr native * interpolation for quaternions */ template <> inline const pxr::GfQuatf interpolate(const pxr::GfQuatf& q0, const pxr::GfQuatf& q1, float theta) { if (theta < 0.0f || theta > 1.0f) { CARB_LOG_WARN_ONCE("InterpolationUsd interpolate(): theta %f outside range [0.0, 1.0]", theta); } pxr::GfQuatf result = pxr::GfSlerp(theta, q0, q1); return result; } /** * @brief pxr::Matrix4d interpolation specialization Used in Kit by OmniHydraDelegate */ template <> inline const pxr::GfMatrix4d interpolate(const pxr::GfMatrix4d& m0, const pxr::GfMatrix4d& m1, float theta) { if (theta < 0.0f || theta > 1.0f) { CARB_LOG_WARN_ONCE("InterpolationUsd interpolate(): theta %f outside range [0.0, 1.0]", theta); } pxr::GfMatrix4d r0, r1; // rotations, where -r is inverse of r pxr::GfVec3d s0, s1; // scale pxr::GfMatrix4d u0, u1; // rotations, may contain shear info pxr::GfVec3d t0, t1; // translations pxr::GfMatrix4d p0, p1; // p is never modified; can contain projection info // Account for rotation, translation, scale // (order is mat = r * s * -r * u * t), eps=1e-10 used to avoid zero values m0.Factor(&r0, &s0, &u0, &t0, &p0); m1.Factor(&r1, &s1, &u1, &t1, &p1); // Interpolate component-wise pxr::GfVec3d tResult = pxr::GfLerp(theta, t0, t1); pxr::GfVec3d sResult = pxr::GfLerp(theta, s0, s1); pxr::GfQuatd rResult = pxr::GfSlerp(u0.ExtractRotationQuat(), u1.ExtractRotationQuat(), theta); pxr::GfMatrix4d result = pxr::GfMatrix4d(pxr::GfRotation(rResult), pxr::GfCompMult(sResult, tResult)); return result; } } }
2,530
C
29.865853
107
0.686166
omniverse-code/kit/fabric/include/carb/flatcache/RationalTime.h
// Copyright (c) 2020-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. // #pragma once #include <map> #include <stdint.h> namespace carb { namespace flatcache { // Each frame in the history buffer is timestamped with a frame time, stored as // a rational number to minimize rounding issues. See threadgate::TimeRatio. struct RationalTime { int64_t numerator; uint64_t denominator; // Minimize denominator small by dividing by gcd(numerator,denominator) RationalTime reduce() const { RationalTime result{0, 0}; int64_t gcdNumDen = gcd(numerator, denominator); if (gcdNumDen != 0) { result.numerator = numerator / gcdNumDen; result.denominator = denominator / gcdNumDen; } return result; } bool operator==(RationalTime rhs) const { RationalTime thisReduced = reduce(); RationalTime rhsReduced = rhs.reduce(); return (thisReduced.numerator == rhsReduced.numerator) && (thisReduced.denominator == rhsReduced.denominator); } bool operator!=(RationalTime rhs) const { return !(*this == rhs); } static int64_t gcd(int64_t a, int64_t b) { while (b != 0) { int64_t t = b; b = a % b; a = t; } return std::max(a, -a); } RationalTime operator-(RationalTime b) const { RationalTime result; result.numerator = numerator * int64_t(b.denominator) - b.numerator * int64_t(denominator); result.denominator = denominator * b.denominator; return result.reduce(); } RationalTime operator*(int64_t b) const { RationalTime result; result.numerator = numerator * b; result.denominator = denominator; return result.reduce(); } }; static const RationalTime kInvalidTime = { 0, 0 }; } // namespace flatcache } // namespace carb
2,308
C
24.94382
118
0.642548
omniverse-code/kit/fabric/include/carb/flatcache/ApiLogger.h
// 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. // #pragma once #include <carb/flatcache/IPath.h> #include <carb/flatcache/IToken.h> #include <iostream> // To log all FlatCache methods that access a particular path and attribute, // set the following three defines #define ENABLE_FLATCACHE_API_LOG 0 #if ENABLE_FLATCACHE_API_LOG #define attrToTrace "attrToLog" #define pathToTrace "/primToLog" namespace carb { namespace flatcache { struct ApiLogger { bool& enabled; const char* desc; ApiLogger(const char* desc, bool& enabled, const TokenC& attrNameC) : desc(desc), enabled(enabled) { Token attrName(attrNameC); if (attrName == Token(attrToTrace)) { std::cout << "begin " << desc << "\n"; enabled = true; } } ApiLogger(const char* desc, bool& enabled, const PathC& pathC, const TokenC& attrNameC) : desc(desc), enabled(enabled) { Path path(pathC); Token attrName(attrNameC); if (path == Path(pathToTrace) && attrName == Token(attrToTrace)) { std::cout << "begin " << desc << "\n"; enabled = true; } } ~ApiLogger() { if (enabled) { std::cout << "end " << desc << "\n"; } enabled = false; } }; #define APILOGGER(...) ApiLogger logger(__VA_ARGS__) } } #else #define APILOGGER(...) #endif
1,789
C
23.861111
122
0.636669
omniverse-code/kit/fabric/include/carb/flatcache/underlying.h
#pragma once #include <type_traits> namespace carb { namespace flatcache { template <typename EnumT> constexpr inline typename std::underlying_type<EnumT>::type underlying(const EnumT& t) { return static_cast<typename std::underlying_type<EnumT>::type>(t); } } // namespace flatcache } // namespace carb
312
C
18.562499
86
0.740385
omniverse-code/kit/fabric/include/carb/flatcache/Ordered_Set.h
// Copyright (c) 2019-2020, 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. // #pragma once #include <algorithm> #include <functional> #include <initializer_list> #include <vector> namespace carb { namespace flatcache { template <class T, class Compare = std::less<T>> struct set { using value_type = T; std::vector<T> v; Compare cmp; using iterator =typename std::vector<T>::iterator; using const_iterator =typename std::vector<T>::const_iterator; iterator begin() { return v.begin(); } iterator end() { return v.end(); } const_iterator begin() const { return v.begin(); } const_iterator end() const { return v.end(); } set(const Compare& c = Compare()) : v(), cmp(c) { } template <class InputIterator> set(InputIterator first, InputIterator last, const Compare& c = Compare()) : v(first, last), cmp(c) { std::sort(begin(), end(), cmp); } set(std::initializer_list<T> _Ilist) : set(_Ilist.begin(), _Ilist.end()) { } void reserve(size_t newCapacity) { v.reserve(newCapacity); } void clear() { v.clear(); } iterator insert(const T& t) { iterator i = std::lower_bound(begin(), end(), t, cmp); if (i == end() || cmp(t, *i)) i = v.insert(i, t); return i; } iterator insert(T&& t) { iterator i = std::lower_bound(begin(), end(), t, cmp); if (i == end() || cmp(t, *i)) i = v.insert(i, std::move(t)); return i; } template <class _Iter> void insert(_Iter _First, _Iter _Last) { // insert [_First, _Last) one at a time for (; _First != _Last; ++_First) { insert(*_First); } } iterator insert(const_iterator hint, const value_type& value) { // Measurements show it is faster to ignore hint in this application return insert(value); } void insert(std::initializer_list<T> _Ilist) { insert(_Ilist.begin(), _Ilist.end()); } size_t erase(const T& key) { iterator removeElement = find(key); if (removeElement != v.end()) { v.erase(removeElement); return 1; } else { return 0; } } iterator erase(iterator iter) { return v.erase(iter); } const_iterator find(const T& t) const { const_iterator i = std::lower_bound(begin(), end(), t, cmp); return i == end() || cmp(t, *i) ? end() : i; } iterator find(const T& t) { iterator i = std::lower_bound(begin(), end(), t, cmp); return i == end() || cmp(t, *i) ? end() : i; } bool contains(const T& t) const { const_iterator i = std::lower_bound(begin(), end(), t, cmp); return i != end() && !cmp(t, *i); } bool operator==(const set<T>& other) const { return v == other.v; } bool operator!=(const set<T>& other) const { return v != other.v; } size_t size() const { return v.size(); } T* data() { return v.data(); } const T* data() const { return v.data(); } }; template <class T, class Compare = std::less<T>> bool operator<(const set<T, Compare>& left, const set<T, Compare>& right) { return left.v < right.v; } template<typename T> flatcache::set<T> nWayUnion(std::vector<flatcache::set<T>>& srcBuckets) { flatcache::set<T> retval; // Calculate the maximum number of destination attributes // We could instead calculate it exactly by finding union of attribute names size_t maxDestAttrCount = 0; for (flatcache::set<T>& srcBucket : srcBuckets) { maxDestAttrCount += srcBucket.size(); } retval.reserve(maxDestAttrCount); auto currentDest = std::back_inserter(retval.v); size_t bucketCount = srcBuckets.size(); // Initialize invariant that nonEmpty is the vector of buckets that have // non-zero attribute counts struct NonEmptySegment { // Invariant is current!=end typename std::vector<T>::iterator current; typename std::vector<T>::iterator end; }; std::vector<NonEmptySegment> nonEmpty; nonEmpty.reserve(bucketCount); for (size_t i = 0; i != bucketCount; i++) { if (srcBuckets[i].begin() != srcBuckets[i].end()) { nonEmpty.push_back({ srcBuckets[i].begin(), srcBuckets[i].end() }); } } // Keep going until there's only 1 non-empty bucket // At that point we can just copy its attributes to the output while (1 < nonEmpty.size()) { // Find all the buckets that have the minimum element // These are the ones whose iterators will get advanced // By the loop guard and the invariant, we know that nonEmpty[0] exists // and that nonEmpty[0].current!=nonEmpty[0].end. // So *nonEmpty[0].current is a safe dereference T minSoFar = *nonEmpty[0].current; std::vector<size_t> indicesAtMin; indicesAtMin.reserve(nonEmpty.size()); indicesAtMin.push_back(0); for (size_t i = 1; i != nonEmpty.size(); i++) { if (*nonEmpty[i].current < minSoFar) { minSoFar = *nonEmpty[i].current; indicesAtMin = { i }; } else if (*nonEmpty[i].current == minSoFar) { indicesAtMin.push_back(i); } } // Copy minimum element to the output *currentDest = minSoFar; ++(*currentDest); // Advance the iterators that pointed to the min std::vector<NonEmptySegment> tempNonEmpty; tempNonEmpty.reserve(indicesAtMin.size()); for (size_t i = 0; i != indicesAtMin.size(); i++) { nonEmpty[indicesAtMin[i]].current++; } // Maintain the invariant that nonEmpty are the non empty ones // Replace with O(n) copy into a temporary if necessary auto it = nonEmpty.begin(); while (it != nonEmpty.end()) { if (it->current == it->end) { it = nonEmpty.erase(it); } else { ++it; } } } // By the negation of the guard we know that nonEmpty has zero or one elements if (nonEmpty.size() == 1) { // If one bucket is left, copy its elements to the output std::copy(nonEmpty[0].current, nonEmpty[0].end, currentDest); } return retval; } } }
7,055
C
24.381295
103
0.556768