file_path
stringlengths
21
207
content
stringlengths
5
1.02M
size
int64
5
1.02M
lang
stringclasses
9 values
avg_line_length
float64
1.33
100
max_line_length
int64
4
993
alphanum_fraction
float64
0.27
0.93
omniverse-code/kit/exts/omni.graph.core/docs/directory_structure.rst
.. _omnigraph_directory_structure: OmniGraph Directory Structure ----------------------------- It is advantageous to consider nodes as a separate type of thing and structure your directories to make them easier to find. While it's not required in order to make the build work, it's recommended in order to keep the location of files consistent. The standard Kit extension layout has these directories by default:: omni.my.feature/ bindings/ Files related to Python bindings of your C++ config/ extension.toml configuration file docs/ index.rst explaining your extension plugins/ C++ code used by your extension python/ __init__.py extension.py = Imports of your bindings and commands, and a omni.ext.IExt object for startup/shutdown scripts/ Python code used by your extension The contents of your *__init__.py* file should expose the parts of your Python code that you wish to make public, including some boilerplate to register your extension and its nodes. For example, if you have two scripts for general use in a *utility.py* file then your *__init__.py* file might look like this: .. code-block:: python """Public interface for my.extension""" import .extension import .ogn from .scripts.utility import my_first_useful_script from .scripts.utility import my_second_useful_script The C++ node files (OgnSomeNode.ogn and OgnSomeNode.cpp) will live in a top level *nodes/* directory and the Python ones (OgnSomePythonNode.ogn and OgnSomePythonNode.py) go into a *python/nodes/* subdirectory:: omni.my.feature/ bindings/ config/ docs/ nodes/ OgnSomeNode.ogn OgnSomeNode.cpp plugins/ python/ nodes/ OgnSomePythonNode.ogn OgnSomePythonNode.py If your extension has a large number of nodes you might also consider adding extra subdirectories to keep them together:: omni.my.feature/ ... nodes/ math/ OgnMathSomeNode.ogn OgnMathSomeNode.cpp physics/ OgnPhysicsSomeNode.ogn OgnPhysicsSomeNode.cpp utility/ OgnUtilitySomeNode.ogn OgnUtilitySomeNode.cpp ... .. tip:: Although any directory structure can be used, using this particular structure lets you take advantage of the predefined build project settings for OmniGraph nodes, and makes it easier to find files in both familiar and unfamiliar extensions.
2,677
reStructuredText
33.77922
120
0.649608
omniverse-code/kit/exts/omni.graph.core/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). ## [2.65.4] - 2023-02-10 ### Fixed - Ordering of dependency node compute for AG fan-out with shared dependencies ## [2.65.3] - 2023-02-10 ### Removed - Remove a workaround now useless and affecting perf, as real fix has been merged ## [2.65.2] - 2022-12-20 ### Fixed - Crash fix when resizing a GPU buffer ## [2.65.1] - 2022-12-19 ### Fixed - Write back to Usd. ## [2.65.0] - 2022-12-16 ### Added - Initialize setting to ensure defaults are set properly the first run ## [2.64.8] - 2022-12-15 ### Fixed - Performance for processing of large node fan-in (action graph) ## [2.64.7] - 2022-12-12 ### Fixed - Previous fix is crashing in master. Reseting it to a state where it is actually stable on master ## [2.64.6] - 2022-12-09 ### Fixed - Invalidate any soon-to-be stalled pointers before deleting nodes ## [2.64.5] - 2022-12-08 ### Fixed - Accessing the size of an inexistent array was returning 1 ## [2.64.4] - 2022-12-06 ### Fixed - Don't assign the node's database until *after* its bucket has been invalidated. ## [2.64.3] - 2022-12-03 ### Fixed - Fixed crash when loading resolved string attributes ## [2.64.2] - 2022-11-29 ### Changed - GraphContext::invalidateBucket() now also invalidates the databases of downstream nodes. ### Fixed - OM-75593 Fixed DataModel::copyAttributeInternal for array attribute that used to set array size to 0 when src and dst was the same. ## [2.64.1] - 2022-11-22 ### Added - Documentation link in how-tos for running a minimal Kit with OmniGraph - Documentation for the various types of node implementations and what they are for ## [2.64.0] - 2022-11-17 ### Changed - Improved attach time overhead of OmniGraph for stages that do not contain OmniGraph prims ## [2.63.0] - 2022-11-14 ### Added - IGraphContext 3.3 -> 3.4: getVariableInstanceDataHandle, getVariableInstanceConstDataHandle ## [2.62.7] - 2022-11-11 ### Fixed - Do not reinitialize node's bucket to empty bucket. ## [2.62.6] - 2022-11-07 ### Fixed - potential crash on detach stage in same update that nodes are removed ## [2.62.5] - 2022-09-07 ### Changed - Fixes OM-62261 `og.OmniGraphInspector().as_json` Returns Bad Json ## [2.62.4] - 2022-10-20 ### Fixed - Made the category check more lenient, ignoring empty descriptions ## [2.62.3] - 2022-10-19 ### Changed - Fix for AG dependency logic ## [2.62.2] - 2022-10-08 ### Changed - Fix for evaluation of event inputs to latent nodes ## [2.62.1] - 2022-09-29 ### Fixed - Inconsistent evaluation of nodes with fan-in ## [2.62.0] - 2022-09-23 ### Changed - Extended attribute type values are now persisted to USD as customData - Dynamic extended attribute types are now persisted to scoped customData as well as deprecated unscoped ## [2.61.4] - 2022-09-21 ### Fixed - A bug in action graph logic related to fan-in ## [2.61.3] - 2022-09-08 ### Added - Fixed a bug in action graph traversal logic ## [2.61.2] - 2022-09-06 ### Changed - Gave a better error message for deprecated node type presence ## [2.61.1] - 2022-08-31 ### Fixed - Changed an error into an info message since it is usually harmless ## [2.61.0] - 2022-09-01 ### Added - Added isValid() methods to OG Obj ABI structs ## [2.60.0] - 2022-08-30 ### Removed - Temporary hack for Python node late delayed default value setting ### Added - Extra information to flag when the node type definition is responsible for deletion of default data memory ## [2.59.2] - 2022-08-25 ### Added - Architectural decision for splitting off the extensions into a separate repo - Added isValid() methods to OG Obj ABI structs ### Fixed - Made Fabric dump of connections provide a default when presented with bad data ### Changed - Updated the apply_change_info script to preserve local branch edits of extension.toml ## [2.59.1] - 2022-08-25 ### Fixed - Fixes ReadOnly array attribute sometimes having a dangling pointer ## [2.59.0] - 2022-08-25 ### Added - Support for new setting that turns deprecations into errors - New macro for flagging use of deprecated code path - Deprecation messages for code paths not using schema prims ## [2.58.2] - 2022-08-18 ### Fixed - Added missing call to tell nodes created from a Prim that they already have a default set ## [2.58.1] - 2022-08-15 ### Changed - Allow write variable to operate even if role types differs ## [2.58.0] - 2022-08-12 ### Changed - Node path is now added to console-logged messages - Node path is no longer added to messages captured on nodes ## [2.57.4] - 2022-08-11 ### Changed - Handles auto conversion in data model ## [2.57.3] - 2022-08-08 ### Added - Warning when schema prims are loaded when the setting is disabled. ## [2.57.2] - 2022-08-05 ### Fixed - action evaluator for cycles in connections ## [2.57.1] - 2022-08-04 ### Fixed - Avoided multiple `__metadata__bundle__` tokens in a path ## [2.57.0] - 2022-08-03 ### Changed - OGN Bundle wrapper `size` and `abi_primHandle` are marked as deprecated. ### Added - Two methods to OGN Bundle wrapper: `attributeCount` and `childCount`. ## [2.56.0] - 2022-08-02 ### Added - Update OGN Bundle wrapper to use IBundle2 ## [2.55.0] - 2022-08-02 ### Added - /persistent/omnigraph/enablePathChangedCallback to deprecate INode::registerPathChangedCallback - /persistent/omnigraph/disableInfoNoticeHandlingInPlayback optimization setting ### Changed - pathChangedCallback will now trigger once per OG update with accumulated changes instead of immediately ## [2.54.0] - 2022-07-28 ### Added - Added `IDirtyID` interface - Added `BundleAttrib` class and `BundleAttribSource` enumeration - Added `BundlePrim`, `BundlePrims`, `BundlePrimIterator` and `BundlePrimAttrIterator` - Added `ConstBundlePrim`, `ConstBundlePrims`, `ConstBundlePrimIterator` and `ConstBundlePrimAttrIterator` ## [2.53.1] - 2022-07-28 ### Fixed - Fixed handling of empty strings in operator< and operator> of ogn::base_string ## [2.53.0] - 2022-07-22 ### Added - IBundle2 - remove_attributes_by_name and remove_child_bundles_by_name ## [2.52.1] - 2022-07-27 ### Fixed - Fixed data model for storing attribute metadata in IBundle2 ## [2.52.0] - 2022-07-27 ### Changed - valueChanged callback handling ## [2.51.3] - 2022-07-23 ### Fixed - Fixed dirty_push graphs being re-run during playback when time has not advanced ## [2.51.2] - 2022-07-22 ### Fixed - Registration order of TfNotice handler for OG ## [2.51.1] - 2022-07-21 ### Fixed - OM-56991 Ill-formed sdf paths ## [2.51.0] - 2022-07-21 ### Changed - Undo revert and fix bug in evaluation of nodes with dynamic execution attribs ## [2.50.0] - 2022-07-19 ### Fixed - IBundle2 reviewed ABI, reviewed copy and link for attributes and bundles ## [2.49.0] - 2022-07-18 ### Changed - Reverted the changes in 2.48.0 ## [2.48.1] - 2022-07-18 ### Added - Better ReadPrimAttribute error reporting ## [2.48.0] - 2022-07-15 ### Changed - Action graph fan-out of execution wires are now evaluated in parallel instead of sequence ## [2.47.1] - 2022-07-12 ### Changed - Changed node create to write extended attributes to USD prior to node creation ## [2.47.0] - 2022-07-08 ### Added - 'deprecated' .ogn keyword for attributes - IInternal / omni.graph.core._internal - IInternal::deprecateAttribute() / omni.graph.core._internal.deprecate_attribute() - IAttribute::isDeprecated() / omni.graph.core.Attribute.is_deprecated() - IAttribute::deprecationMessage() / omni.graph.core.Attribute.deprecation_message() - 'internal' attribute metadata keyword ## [2.46.0] - 2022-07-08 ### Fixed - Fixed memory leak coming from NodeType objects ### Changed - INodeType 1.6 -> 1.7 - fileFormatVersion attribute on OmniGraph objects only writes to USD if it has changed ### Added - INodeType::destroyNodeType to deallocate a node type object ## [2.45.2] - 2022-07-07 ### Fixed - IConstBundle2 and IBundle2 broken logging system ## [2.45.1] - 2022-07-06 ### Added - Ability to raise an error/warning for a node through (new) static methods of the database ## [2.45.0] - 2022-06-30 ### Added - Metadata support for IBundle2 interface ## [2.44.3] - 2022-06-30 ### Changed - USD write back is now performed just once per frame per Fabric cache ## [2.44.2] - 2022-06-28 ### Fixed - Checked newly added nodes for file format version upgrade requirements ## [2.44.1] - 2022-06-28 ### Changed - Ensured nodeContextHandle is initialized correctly ## [2.44.0] - 2022-06-29 ### Changed - Deprecated IScheduleNode::getNode, INodeType::getScheduleNodeCount, INodeType::getScheduleNodes ## [2.43.1] - 2022-06-29 ### Added - A compile switch to disable OGN database caching ## [2.43.0] - 2022-06-28 ### Fixed - Array attributes not keeping a single copy of their data/size pointers as intended - Few places using P2AM directly instead of DataModel - Debugging features of DataModel - usdToCache overwritting connection informations - Bad performance of getLocalAttributeHandle ### Added - OGN Database caching at the node level - Tracking of changes that might invalidate this caching - Gives a chance to DataModel to prepare buckets of nodes at load time, in order to prevent useless rebucket later ## [2.42.0] - 2022-06-23 ### Added - clearContents method to IBundle2 interface ## [2.41.0] - 2022-06-22 ### Added - Faster check for deprecated node types ### Removed - Unused core node animation/OgnPointsWeight - Unused core node animation/OgnTextGenerator - Unused core node animation/OgnTimeSampleArray - Unused core node animation/OgnTimeSamplePoints - Unused core node animation/OgnXformDeformer - Unused core node animation/OgnXformSRT - Unused core node animation/OgnXformToFastCache - Unused core node core/OgnFetch - Unused core node core/OgnGather - Unused core node core/OgnScatter - Unused core node core/OgnSlang - Unused core node core/OgnTime - Unused core node core/OgnTimeSample - Unused core node core/OgnXtoY - Unused core node math/OgnVectorElementDecompose - Unused core node math/OgnVectorElementSet ## [2.40.4] - 2022-06-22 ### Changed - Fix for output attribute being resolved by downstream connection ## [2.40.3] - 2022-06-22 ### Added - Architectural decision records support - Added ADR for the decision around how to present the Python API surface ## [2.40.2] - 2022-06-21 ### Changed - Optimized attribute look up ## [2.40.1] - 2022-06-17 ### Changed - Added extra validity checks to bundle data access ### Added - Finished off the documentation for versioning, both internal and external ## [2.40.0] - 2022-06-17 ### Added - IGraph::getEvaluationName ### Changed - When using schemaPrims, OmniGraph now responds to changes made to graph settings in USD. ## [2.39.0] - 2022-06-16 ### Changed - ForceWriteBack mechanism (that was used to write back prim data to USD) is now soft deprecated behind a setting ### Added - A per attribute tracking system that allows to write back to USD at single attribute definition ## [2.38.3] - 2022-06-16 ### Fixed - cpp wrapepr getAttributesR template function, internally it wasn't casting to NameToken*. ### Changed - Outputs for `IBundle2::create*`, `IBundle2::link*` and `IBundle2::copy*` are optional. ## [2.38.2] - 2022-06-15 ### Fixed - Handled the one missing colorama reference ## [2.38.1] - 2022-06-14 ### Fixed - When removing the last attributes on a path, DataModel was failling to move it properly to the empty bucket ## [2.38.0] - 2022-06-13 ### Added - CreateGraphAsNodeOptions structure - GraphEvaluationMode enum - IGraph::getEvaluationMode - IGraph::setEvaluationMode - IGraph::createGraphAsNode(GraphObject&, const CreateGraphAsNodeOption*) ### Deprecated - IGraph::createGraphAsNode(GraphObject&, const char*, const char*, const char*, bool, bool, GraphBackingType, GraphPipelineStage) ## [2.37.1] - 2022-06-13 ### Fixed - Crash fix when trying to convert an invalid "FlatCacheBundle" to a value ## [2.37.0] - 2022-06-13 ### Added - Temporary support for flagging values already set for Python initialize ## [2.36.0] - 2022-06-08 ### Added - Pre-render evaluation functions ## [2.35.0] - 2022-06-07 ### Added - Support for the generator setting for Python optimization ## [2.34.3] - 2022-06-03 ### Fixed - Copy on Write for Child bundles ## [2.34.2] - 2022-06-02 ## Changed - Allow arguments arrayDepthsBuf and tuplesBuf for resolvePartiallyCoupledAttributes() to be null pointers ## [2.34.1] - 2022-06-01 ### Fixed - Input execution attribute are now reset to Disabled state when node enters latent state - Bug making bundle connection to legacy prim ## [2.34.0] - 2022-05-30 ### Changed - Connections between nodes are now not stored in fabric anymore - We now rely only on connections expressed in the Object Oriented Layer in OmniGraph core - DataModel now don't have to do extra logic to interpret FC connections as toppological connection or upstream reference ### Added - Ability to access and set default value on an attribute - Interfaced this new feature with python - Using this new feature in python OGN generator ### Fixed - Bug preventing to clear an attribute array in python ### Removed - Some useless dependencies/visibility to PathToAttributeMap ## [2.33.11] - 2022-05-27 ### Changed - Deprecated the GraphContext::updateToUsd() code path with useSchemaPrims off ### Fixed - Fixed the FlatCache dump in GraphContext, broken when FlatCache was upgraded - Fixed the graph population phase in PluginInterface to walk all graphs, not just the first one ## [2.33.10] - 2022-05-26 ### Fixed - Fixed memory leak with variables - Fixed errors caused by removing variables from Fabric ## [2.33.9] - 2022-05-24 ### Fixed - Attributes on bundle not properly materialized, attempt 2 ## [2.33.8] - 2022-05-18 ### Fixed - Attributes on bundle not properly materialized ## [2.33.7] - 2022-05-17 ### Fixed - Fixed reparenting graph to previous parent which otherwise corrupted fabric state and left nodes without properties ## [2.33.6] - 2022-05-17 ### Fixed - Duplicate bundle connections on rename ## [2.33.5] - 2022-05-16 ### Fixed - Connection-compatibility check for execution attributes ## [2.33.4] - 2022-05-16 ### Changed - Makes dynamic bundle works in python ## [2.33.3] - 2022-05-16 ### Changed - Fix type not properly registered in FC when adding new attributes ## [2.33.2] - 2022-05-13 ### Changed - Makes bundle works on node not backed by USD ## [2.33.1] - 2022-05-12 ### Changed - Array attribute are now using ABI, giving a chance to the framework to perform CoW and DS ## [2.33.0] - 2022-05-11 ### Changed - Refactoring of data model: centralized all FlatCache accesses in one place - MBiB now uses connections instead of bucket tagging ### Added - Lazy pointer retrieval (for read and write) in OGN layer - Array attribute and bundles are now passed by reference from node to node - Copy on Write: Array attributes/Bundle content are copied when mutated - ABI entry for batch attribute copy from one bundle/prim to another ### Fixed - Fixed som API for data retrieval in OGN layer that where missbehaving when used ### Removed - Data stealing prototype ## [2.32.2] - 2022-05-11 ### Fixed - Fixed read time node with pull evaluator when time changes outside of playback (e.g. timeline tool) ## [2.32.1] - 2022-05-06 ### Fixed - Fixed instances loaded from references requiring a stage reload to evaluate correctly ## [2.32.0] - 2022-05-05 ### Changed - Refactored settings to handle interdependencies ### Fixed - Changed std::is_pod to std::is_trivial for C++20 compatibility ### Added - Added architectural diagram of the OmniGraph extension interdependencies - Internal diagram of the schema prim code path dependencies - Adding callbacks in settings to handle interdependencies ## [2.31.3] - 2022-05-04 ### Fixed - Fixed a bit too agressive type unresolution on input attributes ## [2.31.2] - 2022-05-03 ### Fixed - Default variable values not always getting set to instances with no overrides ## [2.31.1] - 2022-05-02 ### Fixed - bug resolving values in certain extended attribute connections ## [2.31.0] - 2022-04-29 ### Changed - Passed graph prim for more targeted FlatCache initialization - Bumped file format to 1.4 - Made file format calls more tolerant of useSchemaPrims settings ## [2.30.3] - 2022-04-25 ### Changed - Only flush USD to flatcache when we are parsing the first root graph, otherwise previous changes to flatcache may be overwritten ## [2.30.2] - 2022-04-28 ### Fixed - Removed the obsolete parts of the unit tests ## [2.30.1] - 2022-04-26 ### Fixed - Fix situation with bad type resolution ## [2.30.0] - 2022-04-22 ### Added - `OmniGraphDatabase.getGraphTarget` - IGraphContext 3.1->3.2 - `IGraphContext.getGraphTarget` ## [2.30.0] - 2022-04-25 ### Removed - Removed the unimplemented IDataReference class - Removed the obsolete TestComputeGraph.cpp unit test ### Changed - Stopped building the obsolete nodes - Moved the generated substitutions.rst file to the build area ## [2.29.4] - 2022-04-21 ### Changed - "ui:"-prefixed attributes present on Node prims are no longer read as OG attributes ## [2.29.3] - 2022-04-19 ### Fixed - Rare crash that occurred when using string-based node attributes ## [2.29.2] - 2022-04-14 ### Fixed - __updateSimStepUsd__ disableUsdUpdates is respected ## [2.29.1] - 2022-04-08 ### Fixed - Auto conversion bug for native tuple ## [2.29.0] - 2022-04-08 ### Changed - Passed graph prim for more targeted FlatCache initialization - Bumped file format to 1.4 - Made file format calls more tolerant of useSchemaPrims settings ## [2.28.0] - 2022-04-08 ### Changed - input attribute types are no longer inferred from output attribute types. - type resolution and unresolution will no longer propagate upstream. ## [2.27.0] - 2022-04-08 ### Added - Added IGraphContext::getAbsoluteSimTime() function - Added ComputeGraph::updateV2() function which updates the absoluteSimTime ## [2.26.3] - 2022-04-07 ### Fixed - Graph::writeTokenSetting() was failing to update the token if it already existed but was empty. ### Changed - Graph::writeTokenSetting() no longer updates the token or issue a warning if the new value is the same as the existing one. ## [2.26.2] - 2022-04-05 ### Fixed - Fixes issues where ForceWriteBack token on newly created Nodes would not be respected unless the Prim of the Node was selected ## [2.26.1] - 2022-03-31 ### Fixed - Fixed issue when using OG in MGPU: forces use of Device 0 in StaticScheduler ## [2.26.0] - 2022-03-31 ### Added - `PrimHandle` and `ConstPrimHandle` renamed to `BundleHandle` and `ConstBundleHandle`. `PrimHandle` and `ConstPrimHandle` remain as aliases to `BundleHandle` and `ConstBundleHandle`. - `IConstBundle2` - ONI const bundle interface, successor of carbonite `IBundle`. - `IBundle2` - ONI bundle interface, successor of carbonite `IBundle`. - `IBundleFactory` - ONI factory to create instances of `IConstBundle2` and `IBundle2` interface. - Initial implementation of `IConstBundle2`, `IBundle2` and `IBundleFactory`. - `ComputeGraph` 2.6 -> 2.7. - `ComputeGraph::getBundleFactoryInterface` method added to instantiate bundle factory. - `IPath` new methods: `appendPath` and `getPathElementCount`. ## [2.25.2] - 2022-03-25 ### Fixed - Fixed issue where type resolution on node with coupled attribute would not work - Fixed auto type conversion not working on coupled attributes ## [2.25.1] - 2022-03-25 ### Fixed - dynamic scheduler will now evaluate execution subgraphs ## [2.25.0] - 2022-03-23 ### Added - IGraph 3.6 -> 3.7 - getEventStream - accessor to retrieve a graphs event stream - IGraphEvent enum type - IGraphEvent.eCreateVariable, raised when a variable is created through the ABI - IGraphEvent.eRemoveVariable, raised when a variable is removed through the ABI ## [2.24.0] - 2022-03-16 ### Added - Setting /persistent/omnigraph/enableLegacyPrimConnections. When enabled, this will restore legacy behavior for OG Prim nodes: - Entire stage is searched for connections between Nodes and Prims, any connected Prims will be brought into FC, and corresponding OG Prim nodes created with all attributes. - Terminal nodes will be automatically synced to FC every frame - This setting is not exposed in the UI. It is a temporary setting until legacy code can be migrated to no longer use Prim connections. ## [2.23.8] - 2022-03-15 ### Fixed - Fixed issue where ComputeGraphs in sublayers may not correctly load ## [2.23.7] - 2022-03-14 ### Fixed - Auto type conversion was not correctly computed on load ## [2.23.6] - 2022-03-14 ### Fixed - Bug in default string attribute initialization ## [2.23.5] - 2022-03-09 ### Added - Added a check to make sure that literalOnly attributes cannot be connected to other attributes ## [2.23.4] - 2022-03-08 ### Fixed - Cleanup of graph deletion ## [2.23.3] - 2022-03-08 ### Changed - Modified the node type registration/deregistration process to avoid static destructor ordering problems and allow multiple initialize/release calls ## [2.23.2] - 2022-03-07 ### Fixed - Removed unnecessary migration of bundle connection members to a copied bundle ## [2.23.1] - 2022-03-05 ### Fixed - __RenderVar__ prim data will be automatically loaded into FC when they are part of an OG network ## [2.23.0] - 2022-03-04 ### Changed - Removed the _updateTerminalNodesOnly_ setting, the default of _updateToUSD_ is now False. OG will now only sync Prims FC->USD with the _ForceWriteBack_ token when _updateToUSD_ is False. When the setting is True, it will sync all Prims. - Added _settingsVersion_ setting ## [2.22.3] - 2022-03-04 ### Changed - OG will no longer automatically load non-OG Prim data into FC during stage attach. ## [2.22.2] - 2022-03-02 ### Fixed - Added support for omni.graph.nodes.ReadTime to lazy (dirty_push) evaluator ## [2.22.1] - 2022-02-18 ### Changed - Bug fix in path attribute monitoring: properly retarget subpath when reparenting happens higher in the hierarchy ## [2.22.0] - 2022-02-18 ### Added - Added tests for schema-based prims - Added test case base class factory method ### Fixed - Fixed handling of the schema-based prims - Removed use of deprecated OmniGraphHelper from some scripts ## [2.21.4] - 2022-02-18 ### Changed - Bug fix accessing a deleted entry in a map ## [2.21.3] - 2022-02-17 ### Changed - Added proper support for path role (that keep track of the target) ## [2.21.2] - 2022-02-16 ### Changed - Changed Fabric Cache creation to new `create2` to allow RingBuffers to have CUDA enabled ## [2.21.1] - 2022-02-15 ### Fixed - Categories on several nodes ## [2.21.0] - 2022-02-15 ### Added - Added support for update_usd flag to the data_view and SetAttr command ### Fixed - Ignored the Expression node for the warning on nodes without namespaces - Avoided a crash when the fileFormatVersion attribute could not be found ## [2.20.0] - 2022-02-13 ### Changed - Prim nodes will no longer have OG attributes automatically added - Prim node authored USD attributes added to FC - Removed support for special attribute OGN_NonUSDConnections ## [2.19.0] - 2022-02-11 ### Added - Added OmniGraphDatabase.getVariable ## [2.18.0] - 2022-02-10 ### Added - Added setting for using schema prims for graphs and nodes. - Added use of the omniGraphSchema library for graph and node prims (protected by the setting). ## [2.17.0] - 2022-02-08 ### Changed - Added support for auto conversion between attributes. ## [2.16.1] - 2022-02-07 ### Changed - Moved carb logging out of Database.h and into Node::logComputeMessage. ## [2.16.0] - 2022-02-05 ### Added - IGraph 3.5 -> 3.6 - Added IGraph::createVariable - Added IGraph::removeVariable - Added IGraph::findVariable - IVariable - Added IVariable::getDisplayName and IVariable::setDisplayName - Added IVariable::getCategory and IVariable::setCategory - Added IVariable::getTooltip and IVariable::setTooltip - Added IVariable::getScope and IVariable::setScope - Added IVariable::isValid ## [2.15.1] - 2022-02-04 ### Fixed - Edge cases in action graph evaluator for fan-in and fan-out of execution attributes ## [2.15.0] - 2022-02-02 ### Added - IGatherPrototype 1.1 -> 2.0 - Changed the signature of _gatherPaths_ - Added _getGatheredRepeatedPaths_ ## [2.14.0] - 2022-02-01 ### Added - ComputeGraph 1.0 -> 1.1 - Added *IOmniGraphTest::setTestFailure* - Added *IOmniGraphTest::testFailureCount* - Python bindings at the module level - Added *set_test_failure* - Added *test_failure_count* - Added deregistration macro for earlier deregistration of node types (done in a backwards compatible way) ## [2.13.0] - 2022-01-31 ### Changed - IVariable.getPath renamed to IVariable.getSourcePath ## [2.12.0] - 2022-01-28 ### Fixed - Error status change callbacks weren't firing during normal evaluations. - Bumping the minor version # since features which use the callbacks will not work without this change. ## [2.11.0] - 2022-01-26 ### Added - INode 4.1 -> 4.2 - Added INode::logComputeMessage - Added INode::getComputeMessageCount - Added INode::getComputeMessage - Added INode::clearOldComputeMessages - IGraph 3.4 -> 3.5 - Added IGraph::registerErrorStatusChangeCallback - Added IGraph::deregisterErrorStatusChangeCallback - Added IGraph::nodeErrorStatusChanged - Added IGraph::getVariableCount - Added IGraph::getVariables - Created the IVariable type ### Changed - OmniGraphDatabase::logWarning and logError now log compute messages whenever they are different from the node's previous evaluation. Previously they would only log a given error once per session and node type. ## [2.10.1] - 2022-01-19 ### Changed - Fixed a bug in attribute resolution propagation logic ## [2.10.0] - 2022-01-18 ### Changed - Extended attributes will now be returned to an unresolved state upon disconnection if the topology of the graph does not prevent it. ## [2.9.0] - 2022-01-17 ### Added - INode 4.0 -> 4.1 - Added INode::getComputeCount - Added INode::incrementComputeCount ## [2.8.0] - 2022-01-13 ### Added - IGraphRegistry 1.1 -> 1.2 - Added _IGraphRegistry::getRegisteredType_ ## [2.7.0] - 2022-01-06 ### Modified - _GraphContextObj.context_ has been replaced with _GraphContextObj.contextHandle_. The old member was a pointer to an internal OG class. The new member is a POD handle which can be used with OG ABI functions. ## [2.6.1] - 2021-12-30 ### Modified - _INode::resolvePartiallyCoupledAttributes_ checks more type resolution error conditions. ## [2.6.0] - 2021-12-21 ### Added - IAttributeData 1.3 -> 1.4 - Added _IAttributeData::getDataRGpuAt_ - Added _IAttributeData::getDataWGpuAt_ - Added _IAttributeData::getDataReferenceRGpuAt_ - Added _IAttributeData::getDataReferenceWGpuAt_ ## [2.5.1] - 2021-12-16 ### Changed - Action Graph evaluator now uses `og.ExecutionAttributeState.LATENT_FINISH` to indicate when a latent node is finished. ## [2.5.0] - 2021-12-10 ### Added - Created the INodeCategories interface - ComputeGraph 2.4 -> 2.5 - Added _ComputeGraph::getNodeCategories_ ## [2.5.1] - 2021-12-08 ### Fixed - Fix RuntimeAttribute getXX templates to avoid an MSVC compiler bug ## [2.4.0] - 2021-12-07 ### Added - IGraph 3.3 -> 3.4 - Added _IGraph::isGlobalGraphPrim_ - Modified RuntimeAttribute to include PtrToPtrKind template, with default for backward compatibility ## [2.3.0] - 2021-12-06 ### Added - Added _INodeEvent::eAttributeTypeResolve_ ## [2.2.1] - 2021-12-02 ### Fixed - Fix bug where renaming a graph doesn't rename the wrapper node, resulting in new graphs overwriting old graphs ## [2.2.0] - 2021-11-26 ### Added - Python Bindings 2.1.0 -> 2.2.0 - Added Python api `Graph.get_parent_graph` - Ignore connections to ComputeGraph prims for the new OmniGraph UI. ## [2.1.0] - 2021-11-26 ### Added - IAttributeType 1.2 -> 1.3 - Added _IAttributeType::isLegalOgnType_ ## [2.0.3] - 2021-11-25 ### Added - INodeType 1.5 -> 1.6 - Added _INodeType::getName_ ## [2.0.2] - 2021-11-24 ### Fixed - Fix for handling of referenced OmniGraph Graphs ## [2.0.1] - 2021-11-18 Exposing attribute optional flag. ### Added - IAttribute 1.8 -> 1.9 - Added _IAttribute::getIsOptionalForCompute_ - Added _IAttribute::setIsOptionalForCompute_ ## [2.0.0] - 2021-11-12 Major change to deprecate functionality that has been on the chopping block for a while now. ### Removed - Removed _PlugFlags_ - Removed _kPlugFlagNone_ - Removed _kPlugFlagFollowUpstream_ - INode 3.1 -> 4.0 - Retired _getPlug_ - Retired _stashPlug_ - Retired _getStashedPlugCount_ - Retired _getStashedPlug_ - IGraphContext 2.1 -> 3.0 - Retired _getAttribute_ - Retired _getAttributeWithAttr_ - Retired _getAttributeGPU_ - Retired _getElementCount_ ## [1.4.1] - 2021-11-10 - Fixed bugs where renaming the global graph breaks connections and where bundles don't work in global graphs ## [1.4.0] - 2021-11-10 ### Added - New Interface ISchedulingHints - Added _ISchedulingHints::getComputeRule_ - Added _ISchedulingHints::setComputeRule_ ## [1.3.1] - 2021-11-08 ### Changed - ISchedulingHints 1.0 -> ONI - Kept same functionality, implemented as ONI interface instead of Carbonite interface ## [1.3.0] - 2021-11-04 ### Added - INode 3.1 -> 3.2 - Added _INode::requestCompute_ ## [1.3.0] - 2021-10-27 ### Added - IGraph 3.2 -> 3.3 - Added _IGraph::evaluate_ ## [1.2.0] - 2021-10-18 ### Changed - Prim nodes now not created unless attribute-connected to OG node ### Added - IGraph 3.1 -> 3.2 - Added _IGraph::getFlatCacheUserId_ - IAttribute 1.8 -> 1.9 - Added _IAttribute::ensurePortTypeInName_ - Added _IAttribute::getPortTypeFromName_ - Added _IAttribute::removePortTypeFromName_ ## [1.1.0] - 2021-10-13 ### Added - IAttribute 1.7 -> 1.8 - Added _IAttribute::isValid_ - INode 3.0 -> 3.1 - Added _INode::isValid_ - IGraph 3.0 -> 3.1 - Added _IGraph::isValid_ - IGraphContext 2.0 -> 2.1 - Added _IGraphContext::isValid_ ## [1.0.6] - 2021-10-08 ### Added - FileFormatVersion 1.1 -> 1.2 - Added support for scoped graphs in the editor ## [1.0.5] - 2021-10-07 ### Added - IBundle 1.3 -> 1.4 - Added _IBundle::addAttributes_ - Added _IBundle::removeAttributes_ ## [1.0.4] - 2021-09-30 ### Added - Created ISchedulingHints 1.0 - Added _ISchedulingHints::getThreadsafety_ - Added _ISchedulingHints::setThreadsafety_ - Added _ISchedulingHints::getDataAccess_ - Added _ISchedulingHints::setDataAccess_ - INodeType 1.4 -> 1.5 - Added _INodeType::getSchedulingHints_ - Added _INodeType::setSchedulingHints_ ## [1.0.3] - 2021-09-29 ### Added - ComputeGraph 2.3 -> 2.4 - Added _ComputeGraph::getGraphCountInPipelineStage_ - Added _ComputeGraph::getGraphsInPipelineStage_ - Added _GraphBackingType::kGraphBackingType_None_ ## [1.0.3] - 2021-09-23 ### Added - IGraph 2.1 -> 3.0 - Changed the signature of _IGraph::createGraphAsNode_ - Added _IGraph::getGraphBackingType_ - Added _IGraph::getPipelineStage_ ## [1.0.3] - 2021-09-21 ### Added - IGatherPrototype 1.0 -> 1.1 - Added _getGatherArray_ - Added _getGatherArrayGPU_ - Added _getGatherPathArray_ - Added _getGatherArrayAttributeSizes_ - Added _getGatherArrayAttributeSizesGPU_ - Added _getElementCount_ ## [1.0.3] - 2021-09-16 ### Added - IAttribute 1.6 --> 1.7 - Added _IAttribute::isDynamic()_ - Added _IAttribute::getUnionTypes()_ ## [1.0.3] - 2021-09-15 ### Added - IAttributeData 1.2 --> 1.3 - Added _IAttributeData::cpuValid()_ - Added _IAttributeData::gpuValid()_ ## [1.0.3] - 2021-08-30 ### Added - IGraph 2.0 --> 2.1 - Added _IGraph::reloadFromStage()_ ## [1.0.2] - 2021-08-18 ### Added - INode 2.0 -> 2.1 - Added _INode::getEventStream_ ## [1.0.2] - 2021-08-09 ### IGatherPrototype 1.0 - Added _IGatherPrototype::gatherPaths_ - Added _IGatherPrototype::getGatheredPaths_ - Added _IGatherPrototype::getGatheredBuckets_ - Added _IGatherPrototype::getGatheredType_ ## [1.0.2] - 2021-07-26 ### Changed - INode 1.6 --> 2.0 - Changed the signature of _INode::createAttribute_ ## [1.0.2] - 2021-07-16 ### Added - INodeType 1.3 -> 1.4 - Added _INodeType::removeSubNodeType()_ ## [1.0.2] - 2021-07-14 ### Changed - IGraph 1.3 -> 2.0 - Changed the signature of _IGraph::createSubgraph_ ## [1.0.2] - 2021-07-12 ### Added - IGraph 1.2 -> 1.3 - Added _IGraph::createGraphAsNode_ ## [1.0.2] - 2021-07-04 ### Added - INode 1.5 -> 1.6 - Added _INode::getWrappedGraph()_ ### Changed - ComputeGraph 2.2 -> 2.3 - Changed the semantics of getGraphCount, getGraphs, getGraphContextCount, getGraphContexts ## [1.0.2] - 2021-07-01 ### Added - INode 1.4 -> 1.5 - Added _INode::registerPathChangedCallback()_ - Added _INode::deregisterPathChangedCallback()_ ## [1.0.2] - 2021-06-21 ### Added - IAttribute 1.5 -> 1.6 - Added _IAttribute::getPortType()_ ## [1.0.2] - 2021-06-11 ### Added - IBundle 1.2 -> 1.3 - Added _IBundle::removeAttribute_ - IGraphContext 1.3 -> 1.4 - Added _IGraphContext::getTimeSinceStart()_ ## [1.0.2] - 2021-06-10 ### Added - INodeType 1.2 -> 1.3 - Added _INodeType::getSubNodeTypeCount_ - Added _INodeType::getAllSubNodeTypes_ - IDataStealinPrototype 1.0 - Added _IDataStealingPrototype::actualReference_ - Added _IDataStealingPrototype::moveReference_ - Added _IDataStealingPrototype::enabled_ - Added _IDataStealingPrototype::setEnabled_ ## [1.0.2] - 2021-06-01 ### Changed - INode 1.3 -> 1.4 - Modified _INode::registerConnectedCallback_ - Modified _INode::registerDisconnectedCallback_ - Modified _INode::deregisterConnectedCallback_ - Modified _INode::deregisterDisconnectedCallback_ ## [1.0.2] - 2021-05-31 ### Added - IGraph 1.1 -> 1.2 - Added _IGraph::registerPreLoadFileFormatUpgradeCallback_ - Added _IGraph::registerPostLoadFileFormatUpgradeCallback_ - Added _IGraph::deregisterPreLoadFileFormatUpgradeCallback_ - Added _IGraph::deregisterPostLoadFileFormatUpgradeCallback_ ## [1.0.2] - 2021-05-05 ### Added - IGraph 1.0 -> 1.1 - Added _IGraph::inspect()_ - IAttributeData 1.1 -> 1.2 - Added _IAttributeData::getDataReferenceR()_ - Added _IAttributeData::getDataReferenceRGpu()_ - Added _IAttributeData::getDataReferenceW()_ - Added _IAttributeData::getDataReferenceWGpu()_ - IAttributeType 1.0 -> 1.1 - Added _IAttributeType::inspect()_ - Added _IAttributeType::baseDataSize()_ - Added _IGraphRegistry::inspect()_ - Added _IGraphRegistry::registerNodeTypeAlias()_ - Added _INodeType::inspect()_ - INode 1.2 -> 1.3 - Added _INode::resolveCoupledAttributes()_ - Added _INode::resolvePartiallyCoupledAttributes()_ - INode 1.1 -> 1.2 - Added _INode::getNodeTypeObj()_ - IGraphContext 1.1 -> 1.2 - Added _IGraphContext::inspect()_ ## [1.0.1] - 2021-04-07 ### Added - Created the interface IAttributeType for working with the Type struct ## [1.0.0] - 2021-03-01 ### Initial Version - Started changelog with initial released version of the OmniGraph core
34,930
Markdown
29.29575
149
0.718752
omniverse-code/kit/exts/omni.graph.core/docs/versioning.rst
.. _omnigraph_versioning: OmniGraph Versioning #################### There are many facets to OmniGraph that can and will change over time as OmniGraph is still very much a work in progress. In order to minimize disruption to our users all of these types of changes have been enumerated and incremental deprecation plans have been created. .. _omnigraph_deprecation_communication: Deprecation Communication ************************* The first thing we will do when some functionality is to be deprecated is to communicate our intent in the #omni-graph Slack channel, and in the Slack channel of any groups we believe will be affected by the change. The intent of this initial communication is to coordinate with stakeholders to come to an agreement on a timeline for the deprecation. Once a date has been determined we will proceed with up to three phases of deprecation. The combination of phases will depend on the exact nature of the change and the timeline that has been decided. .. _omnigraph_soft_deprecation: Soft Deprecation ================ The soft deprecation phase is where a setting will be introduced to toggle between the old and new behaviors. The difference between the two could be as trivial as the availability of a deprecated function or as complex as a complete replacement of a commonly used data structure or algorithm. The setting will be a Carbonite setting under the tree `/persistent/omnigraph/deprecation/...`. At this phase the default value of the setting will be to take the original code paths, which will remain untouched. Communication of the change will take place in two ways: - An **@channel** announcement will be posted in the Slack channel(s) indicating the new behavior is available - A deprecation message is added to the original code paths as a one-time warning .. _omnigraph_default_deprecation: Default Deprecation =================== Once you have had a chance to migrate to the new behavior the default behavior will switch: - An **@channel** announcement will be posted in the Slack channel(s) indicating the new behavior is to be the default - The setting default will change to take the new code path - The settings manager will emit a deprecation warning whenever the setting is changed to specify the old code path .. _omnigraph_hard_deprecation: Hard Deprecation ================ In the rare case when old behavior must be completely removed, after the agreed on time has passed the old code path will be removed: - An **@channel** announcement will be posted in the Slack channel(s) indicating the old behavior is being removed - The setting and all of the old code paths will be physically deleted - The settings manager will emit an error whenever the now-deleted setting is attempted to be accessed .. _omnigraph_semantic_versioning: Semantic Versioning ******************* CHANGELOG.md:and this project adheres to `Semantic Versioning <https://semver.org/spec/v2.0.0.html>`_, mostly applied to changes made via the extension versions. Given a version number MAJOR.MINOR.PATCH, increment the: **MAJOR** version when you make incompatible API changes, **MINOR** version when you add functionality in a backwards compatible manner **PATCH** version when you make backwards compatible bug fixes. What this means to the C++ programmer is: **MAJOR** change: You must recompile and potentially refactor your code for compatibility **MINOR** change: You can use your code as-is in binary form, and optionally recompile for compatibility **PATCH** change: Your code works as-is in binary form, no need for recompiling What this means to the Python programmer is: **MAJOR** change: Your code may not work. You should refactor to restore correct execution. **MINOR** change: Your code will continue to work, however you may receive deprecation warnings and should refactor your code to use the latest interfaces. **PATCH** change: Your code will be mostly unaware of the change, other than corrections to previously incorrect behaviours. (You may need to refactor if your code relied on incorrect behaviour.) .. _omnigraph_public_python_api: The Public Python API ===================== We are using the `PEP8 Standard <https://peps.python.org/pep-0008/#public-and-internal-interfaces>`_ for defining the actual public API we are supporting. .. warning:: Although the nature of Python allows you to inspect and access pretty much anything if it doesn't appear as part of our "published" API then it is subject to change at any time without warning. If there is something that is not part of the interface you might find particular useful then request that it be surfaced. The model of import we use is similar to what is used by popular packages like `numpy`, `pandas`, and `scikit-learn`. You import the top level module as a simple prefix and use the API directly from there. .. code-block:: python # The core OmniGraph functionality import omni.graph.core as og og.get_all_graphs() There are two more modules we provide for interacting with OmniGraph: .. code-block:: python # A subset of the OmniGraph functionality that facilitates integration testing import omni.graph.core.tests as ogt # NOT YET AVAILABLE: A subset of the OmniGraph functionality that implements the AutoNode functionality import omni.graph.core.autonode as oga # NOT YET AVAILABLE: The code generator framework - for utilities, key/value constants, and typing import omni.graph.tools as ogn .. hint:: The Python bindings to our ABI are considered part of the published API and are available in the same way as all of the other Python functions. They are available in the `omni.graph.core` module. Getting Old Versions -------------------- By default you will always get the latest version of the module, however some level of backward compatibility will be provided through importing a version-specific module. This module will be frozen at the state of the API contents as of the specified version so that you can continue to access old functionality without warnings until such time as it is :ref:`hard deprecated<omnigraph_hard_deprecation>`. .. code-block:: python import omni.graph.core._1 as og og.the_deprecated_function() Other Types Of Versions ======================= OmniGraph Extension Versions ---------------------------- As with other parts of Kit the OmniGraph extensions have their own version number, and the interface definitions each have their own version number. These version numbers follow the general guidelines used by Kit. .. only:: internal Internal: link to Kit / exts-main-doc broken until kit_repo_docs migration completed (bidirectional dependency bad) .. _omnigraph_carbonite_versions: OmniGraph Carbonite Interface Versions -------------------------------------- A good portion of the OmniGraph interfaces are Carbonite style for historic reasons, so they will follow the Carbonite upgrade procedures. The primary rules are: - Existing functions in an interface always work as described - Functions are never removed from an interface - Add all new functions to the end of the interface - Increment the minor version of the interface whenever a new function is added .. hint:: Most Carbonite version changes will result in a corresponding minor version bump of the extension in which they live as it involves new functionality being added. .. _omnigraph_oni_versions: OmniGraph ONI Versions ---------------------- Some of the OmniGraph interfaces, and those developed in the future, will be ONI style, so they will follow the ONI upgrade procedures. The primary rules are: - Once an interface is published it can never be changed - New functionality in an interface is created by adding a new version that is a derived class (e.g. *IBundle2* and *IBundle3*) .. hint:: Most ONI version changes will result in a corresponding minor version bump of the extension in which they live as it involves new functionality being added. OmniGraph File Format Versions ------------------------------ The OmniGraph nodes also have a USD backing which itself contains a file format version number. For the most part this version number is strictly used to automatically migrate a USD scene to the latest version as it would be problematic to maintain multiple versions within the same code base. It appears as an attribute on the *OmniGraph* prim type. .. code-block:: USD :caption: OmniGraph prim using version 1.4 def OmniGraph "TestGraph" { token evaluator:type = "push" int2 fileFormatVersion = (1, 4) token flatCacheBacking = "Shared" token pipelineStage = "pipelineStageSimulation" token evaluationMode = "Automatic" } OGN Code Generator Versions --------------------------- There is also a version number for the .ogn node generator. It is equal to the version number of the extension containing it (*omni.graph.tools*) and is embedded into the generated code for version management. You should not need to worry about this version number unless you want to file a bug report, where it would be valuable information. Node Type Versions ------------------ Every node type definition has a version number with it. The number should be incremented every time there is a change in behavior of the node, such as adding or removing attributes or changing the computation algorithm. This applies to nodes you write as well as nodes you consume. A mismatch in version number between a node being created and the current node implementation will trigger a callback to the `updateNodeVersion` function, which your node can override if there is some cleanup you can do that will help maintain expected operation of the node. Protecting Yourself With Tests ****************************** OmniGraph uses something called the Extension Testing Matrix to validate any dangerous changes. The more tests you have written that typify your pattern of OmniGraph use the safer it will be for you when changes are made. We can't run tests we don't have so please help us help you; write more tests!
10,183
reStructuredText
41.789916
199
0.750172
omniverse-code/kit/exts/omni.graph.core/docs/core_concepts.rst
OmniGraph Core Concepts *********************** .. _omnigraph_concept_node: Node ==== The heart of any node graph system is of course, the node. The most important exposed method on the node is one to get an attribute, which houses all of the data the node uses for computing. The most import method you implement is the ``compute`` method, which performs the node's computation algorithm. .. _omnigraph_concept_context: GraphContext ============ The graph context contains things such as the current evaluation time and other information relevant to the current context of evaluation (and hence the name). Thus, we can ask it for the values on a particular attribute (as it will need to take things such as time into consideration when determining the value). .. _omnigraph_concept_attribute: Attribute ========= An attribute has a name and contains some data. This should be no surprise to anyone who has worked with graphs before. Attributes can be connected to other attributes on other nodes to form an evaluation network. In C++ an AttributeObj, which references a specific NodeObj, uniquely identifies a location in the graph. Once we have the attribute, we can ask the graph context for the value of that attribute. .. _omnigraph_concept_nodetype: NodeType ======== In order to register a new type of node with the system, we’ll need to fill the exposed ``INodeType`` interface with our own custom functions in order to register our new node type with the system. To simplify this process a descriptive format (.ogn files) has been created which is described in :ref:`ogn_user_guide`. Each node type has a unique implementation of the ``INodeType`` interface. This can be used for both C++ and Python node type implementations. Type ==== OmniGraph mostly relies on Fabric for data, and Fabric was designed to mostly just mirror the data in USD, but in more compute friendly form. That said, we did not want to literally use the USD data types, as that creates unnecessary dependencies. Instead, we create data types that are binary compatible to the USD data types (so can be cast to it), but can be defined independently. Also, our types capture some usefule metadata, such as the role of the data. For example, a float3 can be used both to describe a position as well as a normal. However, the way the code would want to deal with the data is very different depending on which of the two roles it plays. Our types have a *role* field to capture this sort of meta-data. Currently our data type strucuture captures the following: - Base Data Type: can be bool, uchar, int, uint, int64, uint64, half, float, double, token, relationship, asset, prim, connection, tag - Component Count: 1 for raw base types, 2 for things like vector2f, 3 for point3d, normal3f etc - Array Depth: 0 for single value, 1 for array, 2 for array of array (not yet supported) - Role: can be none, vector, normal, position, color, texcoord, quaternion, frame, time code, text, applied schema, prim type name, execution, matrix, object id, or unknown Bundle Attributes ================= To address the limitations of “regular” attributes, we introduced the notion of the “bundle”. As the name suggests, this is a flexible bundle of data, similar to a prim. One can dynamically create any number of attributes inside the bundle and transport that data down the graph. This serves two important purposes. First, the system becomes more flexible - we are no longer limited to pre-declared data. Second, the system becomes more usable. Instead of many connections in the graph, we have just a single connection, with all the necessary data that needs to be transported. .. _omnigraph_concept_registry: GraphRegistry ============= This is where we register new node types (and unregister, when our plugin is unloaded). The code generated through the descriptive .ogn format automatically handles interaction with the registry. .. _omnigraph_concept_fabric: Fabric DataModel ================ The data model for OmniGraph is based on the Fabric data manager. Fabric is used as a common location for all data within the nodes in OmniGraph. This common data location allows for many efficiencies. Further, the Fabric handles data synchronization between CPU data, GPU data, and USD data, offloading that task from OmniGraph.
4,330
reStructuredText
51.180722
578
0.764434
omniverse-code/kit/exts/omni.graph.core/docs/usd.rst
OmniGraph And USD ################# USD is used as a source of data for OmniGraph. Both can be described as a set of nodes, attributes, and connections, each with their own set of strengths and restrictions. USD is declarative - it has no system for procedurally generating attribute values (such as expression binding, keyframing, etc). Timesamples can give different values when requested at different times, however they too are declarative - they are fixed values at fixed times and are unaffected by any configuration of the USD stage outside of composition. OmniGraph adds a procedural engine to Omniverse on top of USD that does generate computed attribute values. OmniGraph is not constrained by the data values in its definition. It can create entirely new values at runtime based on the computation algorithms in its nodes. A USD stage can contain several layers forming "opinions" on what its declared values should be, which are composed together to form the final result. OmniGraph only operates on the composed USD stage in general, although it is possible for special purpose nodes to access the USD layer data through the USD APIs. .. _omnigraph_fabric_sync: In order to store and manage data efficiently, OmniGraph uses a data storage component called *Fabric*, which is a part of Omniverse. After the USD stage is composed, USD prim data is read into *Fabric*, where it can be efficiently accessed by OmniGraph for its computations. When appropriate, OmniGraph directs *Fabric* to synchronize with USD and the computed data becomes available to the USD stage. .. mermaid:: sequenceDiagram autonumber USD->>Fabric: Populate Node Data loop Computation Fabric->>OmniGraph: Read Data For Computation OmniGraph->>Fabric: Update With Results Of Computation end Fabric-->>USD: Sync Computed Data The nature of the computation being performed by OmniGraph will determine the interval for syncing computed data back to USD. If, for example, the graph is deforming a mesh for passing on to some external stress analysis software then the resulting mesh must be synced to USD on each playback frame so that each version of the deformed mesh is available for analysis. On the other hand if the graph is performing a large set of Machine Learning calculations for the purposes of training a model then it only needs to send the results back to USD when the training is complete and the resulting model needs to be stored. .. important:: While some nodes within OmniGraph access USD data, and most have a direct correspondence with a USD prim, neither of these is required for OmniGraph to work properly. If an OmniGraph node is only required temporarily to perform some intermediate calculation, for example, it might only be known to *Fabric* and never appear in the USD stage. .. note:: To avoid awkward wording the generic term "graph" will be used herein when discussing the component of the OmniGraph subsystem that represents a graph to be evaluated. OmniGraph as a whole comprises the collection of components required to perform runtime evaluation (graphs, evaluators, schedulers) and their interaction. How The Graph Appears In USD **************************** We'll start with a simple example of how a graph looks in the USD Ascii format and the progressively fill in the details of exactly what each part of it represents. The graph will contain two nodes connected together to perform the two-part computation of multiplying a value by four using two nodes whose computation multiplies a value by two. .. code-block:: usd def OmniGraph "Graph" { token evaluator:type = "push" token fabricCacheBacking = "Shared" int2 fileFormatVersion = (1, 4) token pipelineStage = "pipelineStageOnDemand" def OmniGraphNode "times_2_node_1" { token node:type = "omni.graph.nodes.Times2" int node:typeVersion = 1 custom double inputs:a = 2.5 custom double outputs:two_a = 5.0 } def OmniGraphNode "times_2_node_2" { token node:type = "omni.graph.nodes.Times2" int node:typeVersion = 1 custom double inputs:a = 0.0 custom double inputs:a.connect = </Graph/times_2_node_1.outputs:two_a> custom double outputs:two_a = 10.0 } } The Graph ========= The graph itself is the top level USD prim of type **OmniGraph**. It serves to define the parameters of the graph definition in its attribute values, and as a scope for all of the nodes that are contained within the graph. .. important:: Nodes must all be contained within the scope of an **OmniGraph** prim, and no prims that are not of type **OmniGraph** or **OmniGraphNode** are allowed within that scope. .. code-block:: usd :emphasize-lines: 3-6 def OmniGraph "Graph" { token evaluator:type = "push" token fabricCacheBacking = "Shared" int2 fileFormatVersion = (1, 4) token pipelineStage = "pipelineStageOnDemand" def OmniGraphNode "times_2_node_1" { token node:type = "omni.graph.nodes.Times2" int node:typeVersion = 1 custom double inputs:a = 2.5 custom double outputs:two_a = 5.0 } def OmniGraphNode "times_2_node_2" { token node:type = "omni.graph.nodes.Times2" int node:typeVersion = 1 custom double inputs:a = 0.0 custom double inputs:a.connect = </Graph/times_2_node_1.outputs:two_a> custom double outputs:two_a = 10.0 } } .. mermaid:: classDiagram class Graph Graph: token evaluator.type Graph: token fabricCacheBacking Graph: int2 fileFormatVersion Graph: token pipelineStage The predefined attributes in the OmniGraph prim come from the OmniGraph prim schema, generated using the standard `USD Schema Definition API <https://graphics.pixar.com/usd/release/tut_generating_new_schema.html>`_. This is the generated schema definition. .. code-block:: usd class OmniGraph "OmniGraph" ( doc = "Base class for OmniGraph primitives containing the graph settings." ) { token evaluationMode = "Automatic" ( allowedTokens = ["Automatic", "Standalone", "Instanced"] doc = """Token that describes under what circumstances the graph should be evaluated. \r Automatic mode will evaluate the graph in Standalone mode when \r no Prims having a relationship to it exist. Otherwise it will be \r evaluated in Instanced mode.\r Standalone mode evaluates the graph with itself as the graph\r target, and ignores any Prims having relationships to the graph Prim. \r Use this mode with graphs that are expected to run only once per frame\r and use explicit paths to Prims on the stage. \r Instanced graphs will only evaluate the graph when it is part of a \r relationship from an OmniGraphAPI interface. Each Prim with a \r relationship to the graph will cause a unique evaluation with the \r graph target set to the path of the Prim having the relationship. \r Use this mode when the graph represents an asset or template, and \r parameterizes Prim paths through the use of the GraphTarget node,\r variables, or similar mechanisms. """ ) token evaluator:type = "push" ( allowedTokens = ["dirty_push", "push", "pull", "execution", "execution_pull"] doc = "Type name for the evaluator used by the graph." ) token fabricCacheBacking = "Shared" ( allowedTokens = ["Shared", "StageWithHistory", "StageWithoutHistory"] doc = "Token that identifies the type of FabricCache backing used by the graph." ) int2 fileFormatVersion ( doc = """A pair of integers consisting of the major and minor versions of the\r file format under which the graph was saved.""" ) token pipelineStage = "pipelineStageSimulation" ( allowedTokens = ["pipelineStageSimulation", "pipelineStagePreRender", "pipelineStagePostRender", "pipelineStageOnDemand"] doc = """Optional token which is used to indicate the role of a vector\r valued field. This can drive the data type in which fields\r are made available in a renderer or whether the vector values\r are to be transformed.""" ) } .. note:: This schema is still in development and will most likely change in the upcoming releases. The Nodes ========= A graph is more than just a graph prim on its own. Within its scope are an interconnected set of nodes with attribute values that are a visual representation of the computation algorithm of the graph. Each of these nodes are represented in USD as prims with the schema type **OmniGraphNode**. .. code-block:: usd :emphasize-lines: 8-14,16-23 def OmniGraph "Graph" { token evaluator:type = "push" token fabricCacheBacking = "Shared" int2 fileFormatVersion = (1, 4) token pipelineStage = "pipelineStageOnDemand" def OmniGraphNode "times_2_node_1" { token node:type = "omni.graph.nodes.Times2" int node:typeVersion = 1 custom double inputs:a = 2.5 custom double outputs:two_a = 5.0 } def OmniGraphNode "times_2_node_2" { token node:type = "omni.graph.nodes.Times2" int node:typeVersion = 1 custom double inputs:a = 0.0 custom double inputs:a.connect = </Graph/times_2_node_1.outputs:two_a> custom double outputs:two_a = 10.0 } } .. mermaid:: flowchart LR subgraph Graph times_2_node_1 --> times_2_node_2 end The predefined attributes in the **OmniGraphNode** prim come from the **OmniGraphNode** prim schema, also defined through the `USD Schema Definition API <https://graphics.pixar.com/usd/release/tut_generating_new_schema.html>`_. This is the generated schema for the node's prim type. .. code-block:: usd class OmniGraphNode "OmniGraphNode" ( doc = "Base class for OmniGraph nodes." ) { token node:type ( doc = "Unique identifier for the name of the registered type for this node." ) int node:typeVersion ( doc = """Each node type is versioned for enabling backward compatibility. This value indicates which version of\r the node type definition was used to create this node. By convention the version numbers start at 1.""" ) } Connections =========== Although OmniGraph connections between attributes are implemented and handled directly within OmniGraph they are published within USD as *UsdAttribute* connections. Imagine a "Times2" node with one input and one output whose value is computed as the input multiplied by two. Then you can connect two of these in a row to multiple the original input by four. In USD this would look like: .. code-block:: usd :emphasize-lines: 13,20-22 def OmniGraph "Graph" { token evaluator:type = "push" token fabricCacheBacking = "Shared" int2 fileFormatVersion = (1, 4) token pipelineStage = "pipelineStageOnDemand" def OmniGraphNode "times_2_node_1" { token node:type = "omni.graph.nodes.Times2" int node:typeVersion = 1 custom double inputs:a = 2.5 custom double outputs:two_a = 5.0 } def OmniGraphNode "times_2_node_2" { token node:type = "omni.graph.nodes.Times2" int node:typeVersion = 1 custom double inputs:a = 0.0 custom double inputs:a.connect = </Graph/times_2_node_1.outputs:two_a> custom double outputs:two_a = 10.0 } } .. mermaid:: flowchart LR subgraph Graph times_2_node_1 --> times_2_node_2 end One thing worth mentioning here - in the second node */Graph/times_2_node_2* the value at *inputs:a* defines a value of 0.0, which is used in the event that a connection is not authored, or the connection is not evaluated (for example: outside an OmniGraph context), or an invalid connection is authored for the attribute. Where a valid connection is authored, it will take precedence during OmniGraph evaluation. In this example this is seen by the computation using the connected value of *5.0* as the multiplicand, not the authored value of *0.0*. Graph Extras ============ The graph evaluation type is a hint to OmniGraph on how a set of nodes should be evaluated. One of the more common variations is the type *execution*, which is usually referred to as an :ref:`Action Graph<ogn_omni_graph_action_overview>`. .. code-block:: usd :emphasize-lines: 3,9,14-16 def OmniGraph "ActionGraph" { token evaluator:type = "execution" token fabricCacheBacking = "Shared" int2 fileFormatVersion = (1, 4) token pipelineStage = "pipelineStageOnDemand" def OmniGraphNode "DoNothingNode" ( prepend apiSchemas = ["NodeGraphNodeAPI"] ) { token node:type = "omni.graph.nodes.Noop" int node:typeVersion = 1 uint inputs:execIn = 0 uniform token ui:nodegraph:node:expansionState = "open" uniform float2 ui:nodegraph:node:pos = (-196.9863, 332.03223) } } .. mermaid:: flowchart LR subgraph ActionGraph DoNothingNode end The key differentiator of an :ref:`Action Graph<ogn_omni_graph_action_overview>` is its use of :ref:`execution attributes<omnigraph_usd_execution_attributes>` to trigger evaluation, rather than some external event such as the update of a USD stage. The additional two **ui:nodegraph** attributes shown in this example illustrate how extra information can be added to the node and be stored with the prim definition. In this case the Action Graph editor has added the information indicating the node's position and expansion state so that when the file is reloaded and the editor is opened the node will appear in the same location it was last edited. The node graph editor has added the API schema *NodeGraphNodeAPI* to help it in interpreting the extra data it has added. This is a standard Pixar USD schema that you can read more about in the `USD documentation <https://graphics.pixar.com/usd/dev/api/class_usd_shade_node_graph.html>`_. Custom Attributes ================= Every node type can define its own set of custom attributes, specified using the :ref:`.ogn format<ogn_reference_guide>`. These custom attributes are what an OmniGraph node uses for its computation. Although the attributes are represented in USD, the actual values OmniGraph nodes use are stored in *Fabric*. These are the values that need to be synchronized by OmniGraph as mentioned :ref:`above<omnigraph_fabric_sync>`. For the most part, every USD attribute type has a corresponding .ogn attribute type, though some may have some different interpretations. The full :ref:`type correspondence<ogn_attribute_types>` shows how the types relate. Here is how the definition of our "multiply by two" node might look in .ogn format: .. code-block:: json { "Times2": { "version": 1, "description": "Multiplies a double value by 2.0", "inputs": { "a": { "type": "double", "description": "The number to be doubled" } }, "outputs": { "two_a": { "type": "double", "description": "The input multiplied by 2.0" } } } When the graph creates an instance of this node type then it will publish it in the equivalent USD format. The data that can be recreated from the node type definition (i.e. the descriptions in this case) do not get published into USD. .. code-block:: usd :emphasize-lines: 12-13,20-22 def OmniGraph "Graph" { token evaluator:type = "push" token fabricCacheBacking = "Shared" int2 fileFormatVersion = (1, 4) token pipelineStage = "pipelineStageOnDemand" def OmniGraphNode "times_2_node_1" { token node:type = "omni.graph.nodes.Times2" int node:typeVersion = 1 custom double inputs:a = 2.5 custom double outputs:two_a = 5.0 } def OmniGraphNode "times_2_node_2" { token node:type = "omni.graph.nodes.Times2" int node:typeVersion = 1 custom double inputs:a = 0.0 custom double inputs:a.connect = </Graph/times_2_node_1.outputs:two_a> custom double outputs:two_a = 10.0 } } A few points of interest in this output: - OmniGraph attributes are published as *custom* attributes - The value of *node:typeVersion* must match the *"version"* value specified in the .ogn file - The value of *node:type* is an extended version of the name key specified in the .ogn file, with the extension prepended - Input attributes are put into the namespace *inputs:* and output attributes are put into the namespace *outputs:* Special Attribute Types ======================= There are some .ogn attribute types that do not have a direct equivalent in USD. They are implemented using existing USD concepts, relying on OmniGraph to interpret them properly. .. _omnigraph_usd_execution_attributes: Execution Attributes -------------------- Attributes of type *execution* are used by Action Graphs to trigger evaluation based on events. They are stored in USD as unsigned integer values, with some custom data to indicate to OmniGraph that they are an execution type, not a plain old value. Here is an example of the builtin **OnTick** node which has one such input. .. code-block:: usd :emphasize-lines: 11-15 def OmniGraphNode "on_tick" { custom uint inputs:framePeriod = 0 custom bool inputs:onlyPlayback = 1 custom token node:type = "omni.graph.action.OnTick" custom int node:typeVersion = 1 custom double outputs:absoluteSimTime = 0 custom double outputs:deltaSeconds = 0 custom double outputs:frame = 0 custom bool outputs:isPlaying = 0 custom uint outputs:tick = 0 ( customData = { bool isExecution = 1 } ) custom double outputs:time = 0 custom double outputs:timeSinceStart = 0 } .. note:: With execution attributes only the outputs require the **customData** values. .. _omnigraph_usd_bundle_attributes: Bundle Attributes ----------------- A *bundle* attribute in OmniGraph is simply a collection of other attributes. They are published differently in USD depending on whether they are inputs or outputs. Input bundles cannot be specified directly; they take their attribute members from an input connection. For this reason an input bundle attribute is represented as a USD *rel* type. Output bundles are generated or passed along by the node's compute function, so they do not have a permanent representation. Instead, a placeholder child prim is added to the node's prim in USD for it to populate when computing. Here is an example of a graph with two nodes, with an import of a cube prim to use for the extraction. .. code-block:: usd :linenos: :emphasize-lines: 21-22,31-33,38,48,54-56 def Xform "World" { def Mesh "Cube" { double3 xformOp:rotateXYZ = (0, 0, 0) double3 xformOp:scale = (1, 1, 1) double3 xformOp:translate = (0, 0, 0) string comment = "The rest of the cube data omitted for brevity" } def OmniGraph "PushGraph" { token evaluator:type = "push" token fabricCacheBacking = "Shared" int2 fileFormatVersion = (1, 4) token pipelineStage = "pipelineStageSimulation" def OmniGraphNode "read_prim_into_bundle" { custom bool inputs:computeBoundingBox = 0 custom rel inputs:prim prepend rel inputs:prim = </World/Cube> custom token inputs:primPath = "" custom timecode inputs:usdTimecode = -1 custom bool inputs:usePath = 0 token node:type = "omni.graph.nodes.ReadPrimBundle" int node:typeVersion = 1 custom uint64 state:target = 0 custom timecode state:usdTimecode = -1 def Output "outputs_primBundle" { } } def OmniGraphNode "extract_bundle" { custom rel inputs:bundle = </World/PushGraph/read_prim_into_bundle/outputs_primBundle> token node:type = "omni.graph.nodes.ExtractBundle" int node:typeVersion = 1 custom int[] outputs:faceVertexCounts custom int[] outputs:faceVertexIndices custom normal3f[] outputs:normals custom point3f[] outputs:points custom float2[] outputs:primvars:st custom token outputs:sourcePrimPath = "" custom token outputs:subdivisionScheme = "" custom matrix4d outputs:worldMatrix = ( (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0) ) custom double3 outputs:xformOp:rotateXYZ = (0, 0, 0) custom double3 outputs:xformOp:scale = (0, 0, 0) custom double3 outputs:xformOp:translate = (0, 0, 0) custom token[] outputs:xformOpOrder def Output "outputs_passThrough" { } } } } .. mermaid:: flowchart LR /World/Cube subgraph /World/PushGraph read_prim_into_bundle --> extract_bundle end /World/Cube -.-> read_prim_into_bundle The interpretation of the five highlighted sections is: 1. The import node is given a relationship to the prim it will be importing 2. The bundle it is creating is given a placeholder child prim of type "Output" 3. The generated bundle is passed along to the extract_bundle node via a connection 4. The *worldMatrix* value was extracted from the bundle containing the cube's definition 5. The bundle being extracted is also presented as an output via the passThrough placeholder Extended Attributes ------------------- An extended attribute is one that can take on any number of different types when it is part of an OmniGraph. For example it might be a double, float, or int value. The exact type will be determined by OmniGraph, either when a connection is made or a value is set. The actual attributes, however, are represented as token types in USD and it is only OmniGraph's interpretation that provides them with their "real" type. Here is an example of a node having its *inputs:floatOrToken* attribute type resolve to a *float* value at runtime since it has an incoming connection from a float type attribute. .. code-block:: usd :emphasize-lines: 12-15,18 def Xform "World" { def OmniGraph "ActionGraph" { token evaluator:type = "execution" token fabricCacheBacking = "Shared" int2 fileFormatVersion = (1, 4) token pipelineStage = "pipelineStageSimulation" def OmniGraphNode "tutorial_node_extended_attribute_types" { custom token inputs:flexible custom token inputs:floatOrToken custom token inputs:toNegate custom token inputs:tuple token node:type = "omni.graph.tutorials.ExtendedTypes" int node:typeVersion = 1 prepend token inputs:floatOrToken.connect = </World/ActionGraph/constant_float.inputs:value> custom token outputs:doubledResult custom token outputs:flexible custom token outputs:negatedResult custom token outputs:tuple } def OmniGraphNode "constant_float" { custom float inputs:value = 0 token node:type = "omni.graph.nodes.ConstantFloat" int node:typeVersion = 1 } } } .. mermaid:: flowchart LR subgraph /World/PushGraph constant_float --> tutorial_node_extended_attribute_types end .. _omnigraph_usd_path_attributes: Path Attributes --------------- The special OmniGraph attribute type *path* is used to represent USD paths, represented as token-type attributes in USD. To OmniGraph they are equivalent to an *Sdf.Path* that points to a prim or attribute that may or may not exist on the USD stage. When paths are modified, OmniGraph listens to the USD notices and attempts to fix up any path attributes that may have changed locations. To USD though they are simply strings and no updates are attempted when OmniGraph is not enabled and listening to the changes. See the attribute *inputs:primPath* in the example above for the representation of a path attribute in USD. Accessing USD Prims From Nodes ****************************** OmniGraph accesses USD scene data through special import and export nodes. Examples of each of these are the :ref:`ReadPrimBundle<GENERATED - Documentation _ognomni.graph.nodes.ReadPrimBundle>` node for importing a USD prim into an :ref:`OmniGraph bundle attribute<omnigraph_usd_bundle_attributes>` and a corresponding :ref:`WritePrim<GENERATED - Documentation _ognomni.graph.nodes.WritePrim>` node for exporting a computed bundle of attributes back onto a USD prim. Both of these node types use the :ref:`path attribute<omnigraph_usd_path_attributes>` type to specify the location of the prim on the USD stage that they will access. Once the nodes are connected to the prim they are importing or exporting, usually via specifying the *SdfPath* to that prim on the USD stage, they can be used the same as any other OmniGraph node, passing the extracted USD data through the graph for computation. See :ref:`above<omnigraph_usd_bundle_attributes>` for a detailed example. The USD API *********** OmniGraph listens the USD change notices, so it is able to update values when the USD API is used to modify them, or by proxy when UI elements that use the USD API modify them. However this is not an efficient way to set values as the notification process can be slow so it is best to avoid this if possible. .. code-block:: python import omni.usd import omni.graph.core as og from pxr import Usd # Slow Method prim_node = omni.usd.get_context().get_stage().GetPrimAtPath("/World/ActionGraph/constant_float") attribute = prim_node.GetAttribute("inputs:value") attribute.Set(5.0) # Fast Method og.Controller("/World/ActionGraph/constant_float.inputs:value").set(5.0)
27,539
reStructuredText
40.854103
133
0.663931
omniverse-code/kit/exts/omni.graph.core/docs/README.md
# OmniGraph [omni.graph.core] OmniGraph is a graph system that provides a compute framework for Omniverse through the use of nodes and graphs. In time, this computational framework will form the foundation of many other subsystems in Omniverse. This extension contains the core functionality of the OmniGraph.
313
Markdown
51.333325
112
0.814696
omniverse-code/kit/exts/omni.graph.core/docs/naming_conventions.rst
.. _omnigraph_naming_conventions: OmniGraph Naming Conventions ---------------------------- The mandatory naming conventions are noted where applicable. The rest of these conventions have been set up to make it as easy as possible for you to write your own nodes, and to be able to read the nodes that others have written. File And Class Names ++++++++++++++++++++ Class and file naming is ``InterCaps`` with the prefix ``Ogn``. For example, *OgnMyNode.ogn*, *OgnYourNode.py*, *class OgnMyNode*, and *OgnHerNode.cpp*. As with regular writing, active phrasing (verb-noun) is preferred over passive phrasing when a node name refers to its function. +---------------------+--------------------+ | Weak | Preferred | +=====================+====================+ | OgnSumOfTwoValues | OgnAddTwoValues | +---------------------+--------------------+ | OgnAttributeRemoval | OgnRemoveAttribute | +---------------------+--------------------+ | OgnTextGenerator | OgnGenerateText | +---------------------+--------------------+ .. tip:: Some of the automated processes use the ``Ogn`` prefix to more quickly recognize node files. Using it helps them work at peak efficiency. Node Names ++++++++++ Although the only real restriction on node names is that they consist of alphanumeric, underscore, or dot characters there are some conventions established to make it easier to work with nodes, both familiar and unfamiliar. A node will generally have two names - a unique name to identify it to OmniGraph, and a user-friendly name for viewing in the UI. The name of the node should be generally related to the name of the class, to make them easy to find. The name will be made unique within the larger space by prepending the extension as a namespace. The name specified in the file only need be unique within its extension, and by convention is in uppercase ``CamelCase``, also known as ``PascalCase``. It's a good idea to keep your name related to the class name so that you can correlate the two easily. .. warning:: You can override the prepending of the extension name if you want your node name to be something different, but if you do, you must be prepared to ensure its uniqueness. To override the name simply put it in a namespace by including a `.` character, e.g. `omni.deformer.Visualizer`. Note that the `omni.` prefix is reserved for NVIDIA developed extensions. The user-friendly name can be anything, even an entire phrase, in ``Title Case``. Although any name is acceptable always keep in mind that your node name will appear in the user interface and its function should be immediately identifiable from its name. If you do not specify a user-friendly name then the unique name will be used in the user interface instead. Here are a few examples from the OmniGraph extensions: +----------------------+------------------------+-------------------------------------------+----------------------------------+ | Class Name | Name in the .ogn file | Unique Extended Name | User Facing Node Name | +======================+========================+===========================================+==================================+ | OgnTutorialTupleData | TupleData | omni.graph.tutorials.TupleData | Tutorial Node: Tuple Attributes | +----------------------+------------------------+-------------------------------------------+----------------------------------+ | OgnVersionedDeformer | VersionedDeformer | omni.graph.examples.cpp.VersionedDeformer | Example Node: Versioned Deformer | +----------------------+------------------------+-------------------------------------------+----------------------------------+ | OgnAdd | Add | omni.graph.nodes.Add | Add | +----------------------+------------------------+-------------------------------------------+----------------------------------+ .. attention:: The unique node name is restricted to the alphanumeric characters, underscore (``_``), and dot (``.``). Any other characters in the node name will cause the code generation to fail with an error message. Attribute Names +++++++++++++++ As you will learn, every node has a set of attributes which describe the data it requires to perform its operations. The attributes also have naming conventions. The mandatory part is that attributes may only contain alphanumeric characters, underscore, and optional colon-separated namespacing. The preferred naming for attributes is ``camelCase`` and, as with nodes, both a unique name and a user-friendly name may be specified where the user-friendly name has no real restrictions. Attributes have some predefined namespaces depending on their location with the node as well (`inputs:`, `outputs:`, and `state:`) so you can use this to have inputs and outputs with the same name since they will be in different namespaces. Here is an example of attribute names on a node that adds two values together: +-------------+------------------+ | Full Name | User Facing Name | +=============+==================+ | inputs:a | First Addend | +-------------+------------------+ | inputs:b | Second Addend | +-------------+------------------+ | outputs:sum | Sum Of Inputs | +-------------+------------------+ .. attention:: The unique attribute name is restricted to the alphanumeric characters, underscore (``_``), and colon (``:``). Any other characters in the attribute name will cause the code generation to fail with an error message. .. tip:: You will find that use of your node will be easier if you minimize the use of attribute namespaces. It has some implications in generated code due to the special meaning of the colon in C++ and Python.
5,871
reStructuredText
54.923809
128
0.592744
omniverse-code/kit/exts/omni.graph.core/docs/index.rst
.. include:: ../../../../_build/ogn/docs/substitutions.rst .. tabularcolumns:: |L|R| .. csv-table:: :width: 100% **Extension Version**: |omni.graph.core.version|,**Documentation Generated**: |today| What is it, and why are we doing it? ************************************ Omniverse will be made up of many extensions - from animations, to physics, to inferencing algorithms with DNNs, to name just a few. In order for all these systems to interact with each other, we need a graph framework to "glue" it all together - not only those prebuilt components, but also provide a compute framework where clients can integrate their own functionality as well. The framework is to be flexible enough to address all of the above range of use cases, and more, as well as handling a large range of granularity of compute - from sub-millisecond warp level to hours long training of neural networks as some examples. OmniGraph Overview ****************** Current Capabilities ==================== Currently OmniGraph is being used to define deformer pipelines, in particle and fluid simulations, as part of Drivesim for some of its nodes, and to trigger runtime logic based on the receipt of events, though it will grow into more use cases in the future. At present OmniGraph is already capable of a variety of things, although it still is very much a work in progress. We are able to: - Write nodes in C++, Python, and Cuda - Writes scripts in Python using commands and the OmniGraph API that is bound to Python - Employ a code synthesizing framework (OGN) to substantially simplify the development process by generating boilerplate and hiding it away from the developer - Provide an efficient, and flexible data model. Efficient, because the data model is built on top of Fabric, which in short takes USD data and puts into a vectorized, compute friendly form, with the data available on both CPU and GPU. The data model is flexible, because we employ constructs like bundles, which allows an arbitrary amount of data to be passed from node to node, without having to be pre-declared. We also employ constructs like extended and dynamic attributes. Extended attributes allows data of any type to be connected to an attribute, and it resolves at runtime to the correct type. This allows nodes to be created without accounting for an combinatorial explosion of possible types. Dynamic attributes allow new attributes to be tacked onto the node instance at runtime. - Last, but certainly not least, OmniGraph is not a single type of graph. One can create any type of graph, with any semantics with OmniGraph. We do this by clearly separating graph description from graph evaluation. Three graphs are already supported: busy evaluation graph (push), Lazy evaluation graph (dirty_push), and event handling graph (action), with more to come. Ready To Start? *************** .. toctree:: :maxdepth: 1 :caption: Let's Go Through Some Tutorials :glob: Getting Started Tutorials<https://docs.omniverse.nvidia.com/prod_extensions/prod_extensions/ext_omnigraph.html> Node Writing Guides<nodes> Bird's eye view of the OmniGraph Architecture<architecture> Core Concepts<core_concepts> OmniGraph and USD<usd> File, Class, Node, and Attribute Naming Conventions<naming_conventions> Directory Structure<directory_structure> Python Documentation<../../omni.graph/docs/index.rst> How-To Guides<how_to> Versioning<versioning> Categories<categories> Administrative Details ********************** .. toctree:: :maxdepth: 1 :caption: Administrivia :glob: CHANGELOG Roadmap<roadmap> .. ifconfig:: build_name in ('internal') OmniGraph developers can access :ref:`Internal Documentation<omnigraph_internal_development>` for more information. OmniGraph Architectural Decision Log ************************************ OmniGraphs Architectural decisions are tracked and logged. .. toctree:: :maxdepth: 1 OmniGraph Architectural Decision Log<omnigraph_arch_decisions>
4,037
reStructuredText
47.650602
798
0.744365
omniverse-code/kit/exts/omni.graph.core/docs/usd_representations.rst
******************************** OmniGraph Representations in USD ******************************** Each of the components of an OmniGraph are represented using native USD elements for maximum compatibility. Sometimes extra data will be encoded in metadata or ancillary attributes where functionality is needed that isn't provided by standard USD. This page only describes the representation of the elements and values in USD. See the full documentation for more detailed descriptions of what they are and what they do. OmniGraph Graphs In USD ======================= All graphs in OmniGraph are represented as a prim, using the `OmniGraph` schema. The schema adds five standard attributes to all graph prims that describe the settings used for evaluation of that graph. :: def OmniGraph "MyGraph" { token evaluator:type int2 fileFormatVersion token flatCacheBacking token pipelineStage token evaluationMode } **evaluator:type** is the type name for the evaluator the graph uses to run its computation. Legal values for the built-in evaluators are *dirty_push*, *push*, *pull*, *execution*, and *execution_pull*. **fileFormatVersion** is a pair of integers consisting of the major and minor versions of the file format under which the graph was saved. This allows graphs to be processed for backward compatibility. **flatCacheBacking** identifies the type of FlatCache backing used by the graph for its evaluation data. Legal values are *Shared*, *StagedWithHistory*, and *StagedWithoutHistory*. **pipelineStage** is used to indicate the section of the orchestration graph in which this graph lives. Legal values are *pipelineStageSimulation*, *pipelineStagePreRender*, *pipelineStagePostRender*, and *pipelineStageOnDemand*. **evaluationMode** is used to indicate whether the graph should evaluate standalone, or only when instanced. Legal values are *Automatic*, *Standalone* and *Instanced* These graph prims can appear at any level in the prim hierarchy, including the root level. :: def OmniGraph "MyGraph" { token evaluator:type = "push" int2 fileFormatVersion = (1, 2) token flatCacheBacking = "Shared" token pipelineStage = "piplineStageSimulation" } .. note:: The only type of prims that can appear as a child of the **OmniGraph** prim are described below. OmniGraph Nodes in USD ====================== OmniGraph nodes always appear as the child of an **OmniGraph** prim in USD since all OmniGraph nodes must be children of a specific OmniGraph. They are constructed using the **OmniGraphNode** schema. :: def OmniGraphNode "SimpleNode" { token node:type int node:typeVersion } **node:type** is the unique identifier for the name of the registered type of the node. **node:typeVersion** is the node type version number. Each node type is versioned for enabling backward compatibility. This value indicates which version of the node type definition was used to create this node. By convention the version numbers start at 1.
2,986
reStructuredText
37.294871
120
0.754856
omniverse-code/kit/exts/omni.graph.core/docs/Overview.md
# OmniGraph Overview ```{eval-rst} .. include:: ../../../../_build/ogn/docs/substitutions.rst :start-line: 2 ``` ```{csv-table} **Extension**: omni.graph.core,**Documentation Generated**: {sub-ref}`today` ``` ## What is it, and why are we doing it? Omniverse will be made up of many extensions - from animations, to physics, to inferencing algorithms with DNNs, to name just a few. In order for all these systems to interact with each other, we need a graph framework to "glue" it all together - not only those prebuilt components, but also provide a compute framework where clients can integrate their own functionality as well. The framework is to be flexible enough to address all of the above range of use cases, and more, as well as handling a large range of granularity of compute - from sub-millisecond warp level to hours long training of neural networks as some examples. ## Current Capabilities Currently OmniGraph is being used to define deformer pipelines, in particle and fluid simulations, as part of Drivesim for some of its nodes, and to trigger runtime logic based on the receipt of events, though it will grow into more use cases in the future. At present OmniGraph is already capable of a variety of things, although it still is very much a work in progress. We are able to: - Write nodes in C++, Python, and Cuda - Writes scripts in Python using commands and the OmniGraph API that is bound to Python - Employ a code synthesizing framework (OGN) to substantially simplify the development process by generating boilerplate and hiding it away from the developer - Provide an efficient, and flexible data model. Efficient, because the data model is built on top of Fabric, which in short takes USD data and puts into a vectorized, compute friendly form, with the data available on both CPU and GPU. The data model is flexible, because we employ constructs like bundles, which allows an arbitrary amount of data to be passed from node to node, without having to be pre-declared. We also employ constructs like extended and dynamic attributes. Extended attributes allows data of any type to be connected to an attribute, and it resolves at runtime to the correct type. This allows nodes to be created without accounting for an combinatorial explosion of possible types. Dynamic attributes allow new attributes to be tacked onto the node instance at runtime. - Last, but certainly not least, OmniGraph is not a single type of graph. One can create any type of graph, with any semantics with OmniGraph. We do this by clearly separating graph description from graph evaluation. Three graphs are already supported: busy evaluation graph (push), Lazy evaluation graph (dirty_push), and event handling graph (action), with more to come. ## Ready To Start? - [Getting Started Tutorials](https://docs.omniverse.nvidia.com/prod_extensions/prod_extensions/ext_omnigraph.html) - [Node Writing Guides](nodes) - [Bird's eye view of the OmniGraph Architecture](architecture) - [Core Concepts](core_concepts) - [OmniGraph and USD](usd) - [File, Class, Node, and Attribute Naming Conventions](naming_conventions) - [Python Documentation](omnigraph_python_scripting) - [How-To Guides](how_to) - [Versioning](versioning) ## Administrative Details - [](CHANGELOG) - [](roadmap) ```{only} internal OmniGraph developers can access {ref}`Internal Documentation<omnigraph_internal_development>` for more information. ``` ## OmniGraph Architectural Decision Log OmniGraphs Architectural decisions are tracked and logged {doc}`here <omnigraph_arch_decisions>`
3,585
Markdown
59.77966
798
0.775453
omniverse-code/kit/exts/omni.graph.core/docs/decisions/adr-template.rst
:orphan: --- Template for problem with solution --- ########################################## * Status: {proposed | rejected | accepted | deprecated | … | superseded by LINK_TO_OTHER_ADR} (OPTIONAL) * Deciders: {list everyone involved in the decision} (OPTIONAL) * Date: {YYYY-MM-DD when the decision was last updated} (OPTIONAL) Technical Story: {description | ticket/issue URL} (OPTIONAL) Context and Problem Statement ============================== {Describe the context and problem statement, e.g., in free form using two to three sentences. You may want to articulate the problem in form of a question.} Decision Drivers (OPTIONAL) =========================== * {driver 1, e.g., a force, facing concern, …} * {driver 2, e.g., a force, facing concern, …} * {numbers of drivers can vary} Considered Options ================== * {option 1} * {option 2} * {option 3} * {numbers of options can vary} Decision Outcome ================ Chosen option: "{option 1}", because {justification. e.g., only option, which meets k.o. criterion decision driver | which resolves force {force} | … | comes out best (see below)}. Positive Consequences (OPTIONAL) -------------------------------- * {e.g., improvement of quality attribute satisfaction, follow-up decisions required, …} * … Negative Consequences (OPTIONAL) -------------------------------- * {e.g., compromising quality attribute, follow-up decisions required, …} * … Pros and Cons of the Options (OPTIONAL) ======================================= {option 1} ---------- .. {example | description | pointer to more information | …} (OPTIONAL) * Good, because {argument a} * Good, because {argument b} * Bad, because {argument c} * {numbers of pros and cons can vary} {option 2} ---------- .. {example | description | pointer to more information | …} (OPTIONAL) * Good, because {argument a} * Good, because {argument b} * Bad, because {argument c} * {numbers of pros and cons can vary} {option 3} ---------- .. {example | description | pointer to more information | …} (OPTIONAL) * Good, because {argument a} * Good, because {argument b} * Bad, because {argument c} * {numbers of pros and cons can vary} Links (OPTIONAL) ================ * {Link type} {Link to ADR} * {numbers of links can vary} .. <!-- example: Refined by [ADR-0005](0005-example.md) -->
2,350
reStructuredText
24.27957
180
0.611064
omniverse-code/kit/exts/omni.graph.core/docs/decisions/0002-python-api-exposure.rst
Deciding What To Expose As The Python API In OmniGraph ###################################################### * Status: proposed * Deciders: kpicott, OmniGraph devs * Date: 2022-07-07 Technical Story: `OM-46154 <https://nvidia-omniverse.atlassian.net/browse/OM-46154>`_ Context and Problem Statement ============================= Once the Python API definition was decided there was the further decision of exactly what should go into the public API, what should be internal, and what should be completely deprecated. Considered Options ================== 1. **The Wild West** - everything that was already exposed continues to be exposed in exactly the same way 2. **Complete Lockdown** - only what is strictly necessary for current functionality to work is exposed, everything else is made inaccessible to the user 3. **Graceful Deprecation** - we define what we want exposed and make those the *__all__* exposed symbols, but we also include all of the previously available symbols in the module itself. Soft deprecation warnings are put in for anything we want to eventually completely hide from the user. Decision Outcome ================ **Option 3**: the users get full compatibility without any code changes but we have drawn our line in the sand denoting exactly what we will be supporting in the future. The exposure of symbols will be broken into different categories depending on the exposure desired: 1. **Full Exposure** the module imports the symbol and its name is added to the module's *__all__* list 2. **Internal Exposure** the symbol is renamed to have a leading underscore and the module imports the symbol. The name of the symbol is not added to the module's *__all__* list 3. **Hidden Exposure** for symbols that should be internal but for which the work to rename was too involved for now, create a new exposure list called *_HIDDEN* in the same location where *__all__* is defined and but all of the symbols that should be made internal into that list. Eventually this list will be empty as symbols are renamed to reflect the fact that they are only internal to OmniGraph 4. **Soft Deprecation** for symbols we no longer want to support. Their import and/or definition will be moved to a module file with the current version number, e.g. *_1_11.py*. The deprecated symbols will be added to that module's *__all__* list and the module will be imported into the main module but not added to the main module's *__all__* list 5. **Hard Deprecation** for symbols that were deprecated before that can simply be deleted. The file from which they were imported is replaced by a file that is empty except for a single *raise DeprecationError* statement explaining what action the user has to take to replace the deprecated functionality To reduce the size of the largest modules, submodules will be created to segment by grouped functionality. For example, **omni.graph.autonode** will contain all of the AutoNode-specific functionality. Lastly, in order to support this model some reorganization of files will be necessary. In particular, anything that is not part of the public API should be moved to an *_impl* subdirectory with imports from it being adjusted to match. Pros and Cons of the Options ============================ Option 1 -------- * **Good** - work required to define the API is minimal as it is just what we have * **Bad** - with the existing structure it's difficult to ascertain exactly what the API is, and as a consequence it would be harder to maintain in the future * **Ugly** - the current surface is organic anarchy; continuing to support all of it would be a development drag Option 2 -------- * **Good** - with everything well defined we make a strong statement as to what we are claiming to support. * **Bad** - we would have to rely on tests to find any missing exports, and we know we don't have 100% coverage even within Kit * **Ugly** - we are already at the stage where we don't know for sure what users are using, and the testing matrix is not yet populated enough to give us any confidence we will find the problems so there as an almost certainty that we would end up breaking somebody's code Option 3 -------- * **Good** - users's are happy because they have no immediate work to do, and we are happy because we have defined exactly what we are claiming to support. Because it's a smaller subset we can write a compatibility test for future changes. * **Bad** - we will end up exposing some things we don't want to, simply because it's too difficult to remove in one step * **Ugly** - everything is still wide open and users are free to make use of anything in our system. Despite any warnings to the contrary `Hyrum's Law <https://abseil.io/resources/swe-book/html/ch01.html#hyrumapostrophes_law>`_ dictates that it will be used anyway.
4,831
reStructuredText
57.926829
127
0.742703
omniverse-code/kit/exts/omni.graph.core/docs/decisions/0003-extension-locations.rst
OmniGraph Extension Locations In The Repo ######################################### * Status: proposed * Deciders: kpicott, OmniGraph devs * Date: 2022-08-12 Technical Story: `OM-50040 <https://nvidia-omniverse.atlassian.net/browse/OM-50040>`_ Context and Problem Statement ============================= All of the OmniGraph extensions were developed directly within Kit. As they grow, and Kit grows, the development velocity of both the OmniGraph and the larger Kit team are negatively impacted. Kit builds have to wait for all of the OmniGraph extensions and tests to build, and OmniGraph has to wait for the entire Kit SDK to build. In addition the Kit team has expressed a desire to move to a thin model where only code necessary to run what they call "Kit core" should be in the Kit repo, and OmniGraph is not considered part of that. Considered Options ================== 1. **Status Quo** - we just leave all of the extensions we have inside Kit and continue to develop that way 2. **Single Downstream Replacement** - move all of the extensions downstream of `omni.graph.core` to an extensions repo, removing the existing extensions from Kit 3. **Multi Downstream Replacement** - move each of the extensions downstream of `omni.graph.core` to their own individual extensions repo, removing the existing extension from Kit 4. **Downstream Copy** - move all of the extensions downstream from `omni.graph.core` to their own repo, bumping their major version, but leave the existing extensions in Kit at the previous version and have them be dormant Decision Outcome ================ **Option 2**: Single Downstream Replacement. Although none of these solutions is perfect this one affords the best balance of controlling our own destiny and administration overhead. There will be some changes in developer workflow required to support it, especially in the short term when extensions are transitioned out, and in the medium term when we have to backport across repos, but in the long run the workflow gains will outweigh the extra efforts. Moving Extensions ----------------- It's not practical to move every extension out in a single step so we'll have to move them in small subgroups. The extension dependency diagram will guide us on the ordering required to move an extension out. Generally speaking an extension can be moved as soon as nothing in Kit has a direct dependency on it. The :ref:`extension dependency graph<omnigraph_extension_dependency_graph>` that was extracted from the full list of dependencies will guide this. Any extension without an incoming arrow can be moved, starting with `omni.graph.bundle.action`. Pros and Cons of the Options ============================ Option 1 -------- * **Good** - Workflow is simple, we just build and run Kit and there's no overhead in managing our published versions * **Bad** - Kit build continues to be slow for all concerned * **Ugly** - We can't independently version our extensions, Kit becomes increasingly more complex as we add more, our tests continue to be subject to instability due to unrelated Kit problems Option 2 -------- * **Good** - Kit builds and our builds both get way faster, we can version our extension set indpendently of Kit, our tests don't rely as much on how the Kit app behaves, just Kit core * **Bad** - Some developers have to work in multiple repos during the transition, resulting in a clunky workflow for testing changes. There's extra overhead in deciding which apps get which versions of our extensions. * **Ugly** - Backport integration becomes more difficult as previous versions of extensions are not in a related repo. Option 3 -------- * **Good** - We gain full control over extension versioning, builds are even smaller and faster than Option 2, more flexible set of Kit SDK versioning gives us a wider variety of cross-version testing * **Bad** - It becomes more difficult to build tests as typically they will rely on nodes in multiple extensions * **Ugly** - Multiple repos mean multiple copies of the build to handle, multiple packman packages to wrangle, and the overhead reduction we gained by moving out of Kit is lost to administrivia Option 4 -------- * **Good** - Backward compatibility is easier to maintain as there are full "frozen" copies of the extensions available with Kit while we move forward in the separate repo. * **Bad** - Same as Option 2, plus the Kit builds continue to be slow as they still build our frozen extensions * **Ugly** - Random test failures will still occur in our code even though we aren't touching it, bugs that arise from apps using specific versions of the Kit SDK have to be addressed in the "frozen" copies, and it becomes more difficult for apps like Create to choose the correct versions of our extensions.
4,807
reStructuredText
53.022471
120
0.750364
omniverse-code/kit/exts/omni.graph.core/docs/decisions/0001-python-api-definition.rst
Definition of Python API Surface In OmniGraph ############################################# * Status: proposed * Deciders: kpicott, OmniGraph devs * Date: 2022-06-24 Technical Story: `OM-46154 <https://nvidia-omniverse.atlassian.net/browse/OM-46154>`_ Context and Problem Statement ============================= Although the C++ ABI is stable and backwards compatible all of the Python code supplied by OmniGraph is, by the nature of the language, public by default. This is causing problems with compatibility as much of the exposed code is implementation detail that we wish to be able to change without affecting downstream customers. After a few iterations we have run into many cases of unintentional dependencies, which will only grow more frequent as adoption grows. Considered Options ================== 1. **The Wild West** - we leave everything exposed and let people use what they find useful. Behind the scenes we attempt to maintain as much compatibility as we can, with deprecation notices where we cannot. 2. **Complete Lockdown** - we ship our Python modules as pre-interpreted bytecode (*.pyc* files), and tailor our documentation to the specific code we wish to expose. 3. **PEP8 Play Nice** - we follow the `PEP8 Standard <https://peps.python.org/pep-0008/#public-and-internal-interfaces>`_ for defining public interfaces, where nothing is truly hidden but we do have a well-defined way of identifying the interfaces we intend to support through multiple versions. Decision Outcome ================ Option 3: it strikes a good balance of ease of development and ease of use. Pros and Cons of the Options ============================ Option 1 -------- * **Good** - development is simpler as we don't have to worry about how to format or organize most Python code * **Bad** - there is extra work involved in explicitly deprecating things that change, and since everything is visible the need for deprecation will be quite frequent. * **Ugly** - if everything is fair game then users will start to make use of things that we either want to change or get rid of, making evolution of the interfaces much more difficult than they need to be Option 2 -------- * **Good** - with everything well defined we make a strong statement as to what we are claiming to support. * **Bad** - the code is still introspectable at the interface level even if the details are not. This makes it more likely that a mismatch in the code and the description will cause unreconcilable user confusion. * **Ugly** - locking everything is inherently un-Pythonic and puts up artificial barriers to our primary goal in using Python, which is widespread adoption. The fact that the definitive documentation is outside of the code also contributes to those barriers. Option 3 -------- * **Good** - we get a good balance of both worlds. The retained introspection makes it easier for seasoned programmers to understand how things are working and the API conventions make it clear where things are not meant to be relied on in future versions. * **Bad** - there's a bit more work to follow the guidelines and not much automatic help in doing so. Internal developers have to be diligent in what they are exposing. * **Ugly** - everything is still wide open and users are free to make use of anything in our system. Despite any warnings to the contrary `Hyrum's Law <https://abseil.io/resources/swe-book/html/ch01.html#hyrumapostrophes_law>`_ dictates that it will be used anyway.
3,497
reStructuredText
51.208954
121
0.740635
omniverse-code/kit/exts/omni.graph.core/docs/decisions/0000-use-markdown-any-decision-records.rst
Use Markdown Any Decision Records ################################# Context and Problem Statement ============================= We want to record any decisions made in this project independent whether decisions concern the architecture ("architectural decision record"), the code, or other fields. Which format and structure should these records follow? Considered Options ================== * `MADR <https://adr.github.io/madr/>`_ 3.0.0 – The Markdown Any Decision Records * Google Documents * Confluence Decision Outcome ================ Chosen option: "MADR 3.0.0", because * Implicit assumptions should be made explicit. Design documentation is important to enable people understanding the decisions later on. See also `A rational design process: How and why to fake it <https://doi.org/10.1109/TSE.1986.6312940>`_. * MADR allows for structured capturing of any decision. * The MADR format is lean and fits our development style. * The MADR structure is comprehensible and facilitates usage & maintenance. * The MADR project is vivid. * Google docs are not easily discoverable. * We already spend too much time explaining past decisions.
1,153
reStructuredText
36.225805
169
0.717259
omniverse-code/kit/exts/omni.graph.core/docs/internal/versioning_usd.rst
.. _omnigraph_versioning_usd: OmniGraph USD Versioning ######################## This type of versioning involves changes to the structure or content of the USD backing for OmniGraph. It should happen rarely. Two examples of changes of this type are the withdrawal of support for the global implicit graph, and the move from using plain prims to back the OmniGraph elements to using schema-based prims. In both cases the ABI remains unchanged, only the contents of the USD backing changed. This deprecation follows a three-step process to give users ample opportunity to modify any code they need to in order to support the new version that follows the standard :ref:`omnigraph_deprecation_approach`. .. hint:: Using the deprecation process ensures that each step only involves a minor version bump to the extension as existing code will continue to work. In the final phase of removal of the original behavior a major version bump may be required as existing scripts may not function without modification. Step 1 - Introduce A Deprecation Setting **************************************** For anything but the most trivial change there is always the possibility of existing code relying on the old way of doing things so the first step is to introduce a deprecation setting using the instructions in :ref:`omnigraph_versioning_settings`. Step 2 - Add In Automatic File Migration **************************************** Changes to the file format require a bump of the file format version, which can be found in the C++ file `omni.graph.core/plugins/Graph.cpp` in a line that looks like this: .. code-block:: cpp FileFormatVersion s_currentFileFormatVersion = { 1, 4 }; The first number is the major version, the second is the minor version, following the :ref:`omnigraph_semantic_versioning` guidelines. A major version bump is reserved for any changes that are incapable of having an automatic upgrade performed - e.g. adding mandatory metadata that identifies the time the file was created. Next step is to introduce some code that will take the USD structure in the previous file formats, going as far back as is reasonable, and modify it to conform to the desired new structure. Any USD-based modifications can be made at this point so it is quite flexible. One good way of doing this is to register a callback on a file format version update in the extension startup code, remembering to unregister it when the extension shuts down. .. literalinclude:: ../../../../../source/extensions/omni.graph/python/_impl/extension.py :language: python :start-after: begin-file-format-update :end-before: end-file-format-update The callback should be placed somewhere that indicates it is on a deprecation path. For the **useSchemaPrims** deprecation, for example, the deprecation code was added to a special directory `omni.graph/python/_impl/v1_5_0/` that contains code deprecated after version 1.5.0 of that extension. The callback code should check the version of the file being read and check it against the version that requires deprecation. Once you've found that the file format is one that would require conversion then issue a deprecation warning, and if the setting is configured to the new path then perform the conversion. .. code-block:: python :emphasize-lines: 15,21 def cb_for_my_deprecation( old_version: Optional[og.FileFormatVersion], new_version: Optional[og.FileFormatVersion], graph: Optional[og.Graph] ): if old_version is not None and\ (old_version.majorVersion > VERSION_BEFORE_MY_CHANGE.majorVersion or\ (old_version.majorVersion == VERSION_BEFORE_MY_CHANGE.majorVersion and\ old_version.minorVersion > VERSION_BEFORE_MY_CHANGE.minorVersion)): # No conversion needed, the file format is updated return # If the setting to automatically migrate is not on then there is nothing to do here carb.log_warn(f"Old format {old_version} automatically migrating to {new_version}") # If the setting to automatically migrate is not on then there is nothing to do here if not og.Settings().(og.Settings.MY_SETTING_NAME): return update_the_scene() Step 3 - Enable The New Code Path By Default ******************************************** Once the deprecation time has been reached then modify the default value of the setting in `omni.graph.core/plugins/OmniGraphSettings.cpp` and announce that the deprecation is happening. Step 4 - Remove The Old Code Path ********************************* In the rare case where full removal is required then you can remove the support code for the old code path. - Delete the deprecation setting - Delete all code paths that only execute when the setting is configured to use the deprecated code path - Change the file format conversion callback to issue and error and fail - Bump the major version of the file format to indicate older ones are no longer compatible USD Version Effects On Python Scripting *************************************** The automatic file migration provides a flexible upgrade path to making old files use the new way of doing things, however there may still be incompatibilities lurking in the Python files. Simple examples include scripts that assume the existence of deprecated prim types or attributes, or files containing script nodes that do the same. The only way to handle these is to ensure that you have good test coverage so that when the default value of the setting is reversed the test will fail and can be fixed.
5,621
reStructuredText
50.10909
120
0.736702
omniverse-code/kit/exts/omni.graph.core/docs/internal/versioning_extensions.rst
.. _omnigraph_versioning_extensions: Versioning OmniGraph Extensions ############################### The version numbers used by our extensions will follow the :ref:`omnigraph_semantic_versioning` model. Updates to the version numbers will occur in every MR that modifies the extension. This will entail two changes. The first is an update to the *config/extension.toml* file with the appropriate portion of the semantic version updated. The second is an update to the *docs/CHANGELOG.md* file with a description of the changes that appear in that update. For example if a new node was added to the `omni.graph.nodes` extension then it's *extension.toml* file would change this section: .. code-block:: toml [package] title = "OmniGraph Nodes" version = "1.23.8" to this, as an addition is a MINOR version change: .. code-block:: toml :emphasize-lines: 3 [package] title = "OmniGraph Nodes" version = "1.24.0" And these lines would be added at the top of the *CHANGELOG.md*: .. code-block:: md ## [1.24.0] - 2112-03-04 ### Added - Added the node type "Rush" Inter-Extension Versions ************************ For now all of the extensions that live inside of Kit will always be set to use the "latest" version, so in an `extension.toml` file they will appear like this: .. code-block:: toml [dependencies] "omni.graph" = {} "omni.graph.scriptnode" = {} "omni.graph.tools" = {} Extensions that live downstream of the Kit repo will be versioned explicitly. For example in the `kit-extensions/kit-graphs` repo there is the extension *omni.graph.window.action* whose .toml includes this, where the version of dependent extensions are explicit. .. code-block:: toml [dependencies] "omni.graph.window.core" = {version="1.16"} "omni.graph.ui" = {version="1.5"}
1,839
reStructuredText
30.18644
117
0.695487
omniverse-code/kit/exts/omni.graph.core/docs/internal/deprecation.rst
.. _omnigraph_deprecation: OmniGraph Code Deprecation ========================== This is a guide to the deprecation of Python classes, functions, and objects to support backward compatibility. C++ deprecation is left to the ABI functions. Before You Deprecate -------------------- Unlike C++, Python is totally transparent to the user. At least it is in the way in which we are currently using it. That means there is no fixed ABI that you can safely code to, knowing that all incompatible changes will be localized to that set of functions. In order to mitigate this, it is a best practice to provide a published Python API through explicit imports in your main module's `__init__.py` file. Then you can make a statement that only things that appear at the top level of that module are guaranteed to remain compatible through minor and patch version changes. Here's a sample directory structure that allows hiding of the implementation details in a Pythonic way, using a leading underscore to hint that the contents are not meant to be visible. .. code-block:: text omni.my.extension/ python/ __init__.py _impl/ MyClass.py my_function.py Then inside the `__init__.py` file you might see something like this: .. code-block:: python """Interface for the omni.my.extension module. Only components explicitly imported through this file are to be considered part of the API, guaranteed to remain backwardly compatible through all minor and patch version changes. Recommended import usage is as follows: .. code-block:: python import omni.my.extension as ome ome.my_function() """ from ._impl/my_function import my_function from ._impl/MyClass import MyClass A good documentation approach is still being worked out. For now using docstrings in your functions, modules, and classes will suffice. .. warning:: By default Git will ignore anything starting with an underscore, so you might have to add a .gitignore file in the `python/` directory containing something like this: # Although directories starting with _ are globally ignored, this one is intentional, # to follow the Python convention of internal implementation details starting with underscore !_impl/ When To Deprecate ----------------- Now that an API is established the deprecation choice can be limited to times when that API must be changed. Python code is very flexible so very large changes can be made without losing existing functionality. Examples of such changes are: - Adding new methods to classes - Adding new parameters to existing class methods, if they have default values and appear last in the parameter list - Adding new functions - Adding new parameters to existing class methods, if they have default values and appear last in the parameter list - Adding new constant objects Each of these changes would be considered additional functionality, requiring a bump to the minor version of the extension. Each of these changes could also be done by wholesale deprecation of the entity in question, with a replacement that uses a new name using the `Deprecation Process` below. Some changes require full deprecation of the existing functionality due to potential incompatibility. Examples of such changes are: - Removing a parameter from a class method or function - Deleting a class method, function, or object - Renaming an object - Changing an implementation - An ABI function with a Python binding has been deprecated Deprecation Process ------------------- Deprecation In Place ++++++++++++++++++++ The first line of defence for easy deprecation when adding new features to existing code is to provide defaults that replicate existing functionality. For example, let's say you have this function that adds two integers together: .. code-block:: python def add(value_1: int, value_2: int) -> int: return value1 + value_2 New functionality is added that lets you add three numbers. Rather than creating a new function you can use the existing one, adding a default third parameter: .. code-block:: python def add(value_1: int, value_2: int, value_3: int = 0) -> int: return value1 + value_2 + value_3 The key feature here is that all code written against the original API will continue to function without changes. You can be creative about how you do this as well. You can instead use flexible arguments to add an arbitrary number of values: .. code-block:: python def add(value_1: int, value_2: int, *args) -> int: return value1 + value_2 + sum(args) Or you can use the typing system to generalize the function (assuming you are not using static type checkers): .. code-block:: python def add(value_1: Union[int, float], value_2: Union[int, float]) -> Union[int, float]: return value1 + value_2 Or even allow different object types to use the same pattern: .. code-block:: python Add_t = Union[int, Tuple[float, float]] def add(value_1: Add_t, value_2: Add_t) -> Add_t: if isinstance(value_1, tuple): if isinstance(value_2, tuple): return value_1 + value_2 else: return value_1 + tuple([value_2] * len(value_1)) else: if isinstance(value_2, tuple): return value_2 + tuple([value_1] * len(value_2) else: return value_1 + value_2 .. tip:: A good deprecation strategy prevents the remaining code from becoming overly complex. If you start to see the parameter type checking code outweigh the actual functioning code it's time to think about deprecating the original function and introducing a new one. Deprecation By Renaming +++++++++++++++++++++++ If you really do need incompatible features then you can make your new function the primary interface and relegate the old one to the deprecated section. One easy way to do this is to create a new subdirectory that contains all of the deprecated functionality. This makes it both easy to find and easy to eliminate once a few releases have passed and you can no longer support it. For example if you want to create completely new versions of your class and function you can modify the directory structure to look like this: .. code-block:: text omni.my.extension/ python/ __init__.py _impl/ MyNewClass.py my_new_function.py v_1/ MyClass.py my_function.py Then inside the `__init__.py` file you might see something like this: .. code-block:: python """Interface for the omni.my.extension module. Only components explicitly imported through this file are to be considered part of the API, guaranteed to remain backwardly compatible through all minor and patch version changes. Recommended import usage is as follows: .. code-block:: python import omni.my.extension as ome ome.my_new_function() """ from ._impl/my_new_function import my_new_function from ._impl/MyNewClass import MyNewClass r"""Deprecated - everything below here is deprecated as of version 1.1. _____ ______ _____ _____ ______ _____ _______ ______ _____ | __ \ | ____|| __ \ | __ \ | ____|/ ____| /\ |__ __|| ____|| __ \ | | | || |__ | |__) || |__) || |__ | | / \ | | | |__ | | | | | | | || __| | ___/ | _ / | __| | | / /\ \ | | | __| | | | | | |__| || |____ | | | | \ \ | |____| |____ / ____ \ | | | |____ | |__| | |_____/ |______||_| |_| \_\|______|\_____|/_/ \_\|_| |______||_____/ """ from .impl.v_1.my_functon import my_function from .impl.v_1.MyClass import MyClass Now, as before, existing code continues to work as it is still calling the old code which it accesses with the same imported module, however the new versions are clearly marked as the main API. So what happens if the user is using the deprecated versions? With just this change they remain blissfuly unaware that progress has happened. Instead we would prefer if they were notified so that they have a chance to upgrade their code to take advantage of new features and avoid the shortcomings of the old ones. To this end some decorators can be used to provide some messaging to the user when they are using deprecated features. Deprecation Messaging --------------------- Messaging can be added in deprecation situations by using one of the functions and decorators that support it. All deprecation functions can be accessed from the top `omni.graph.tools` module level. The :py:class:`omni.graph.tools.DeprecateMessage` class provides a simple way of logging a message that will only show up once per session. .. literalinclude:: ../../../../../source/extensions/omni.graph.tools/python/_impl/deprecate.py :language: python :start-after: begin-deprecate-message :end-before: end-deprecate-message The :py:class:`omni.graph.tools.DeprecateClass` decorator provides a method to emit a deprecation message when the deprecated class is accessed. .. literalinclude:: ../../../../../source/extensions/omni.graph.tools/python/_impl/deprecate.py :language: python :start-after: begin-deprecated-class :end-before: end-deprecated-class The :py:class:`omni.graph.tools.RenamedClass` decorator is a slightly more sophisticated method of deprecating a class when the deprecation is simply a name change. .. literalinclude:: ../../../../../source/extensions/omni.graph.tools/python/_impl/deprecate.py :language: python :start-after: begin-renamed-class :end-before: end-renamed-class The :py:func:`omni.graph.tools.deprecated_function` decorator provides a method to emit a deprecation message when the old function is called. .. literalinclude:: ../../../../../source/extensions/omni.graph.tools/python/_impl/deprecate.py :language: python :start-after: begin-deprecated-function :end-before: end-deprecated-function The :py:func:`omni.graph.tools.DeprecatedImport` decorator provides a method to emit a deprecation message when an entire deprecated file is imported for use. This should not be used for imports that will be included in the API for backward compatibility, nor should these files be moved as they must continue to exist at the same import location in order to remain compatible. .. literalinclude:: ../../../../../source/extensions/omni.graph.tools/python/_impl/deprecate.py :language: python :start-after: begin-deprecated-import :end-before: end-deprecated-import The :py:func:`omni.graph.core.Attribute.is_deprecated` method provides a way to determine if an attribute has been deprecated, and :py:func:`omni.graph.core.Attribute.deprecation_message` returns the associated text from the .ogn file. For The Future -------------- If deprecations become too difficult to manage a more structured approach can be implemented. This would involve using a namespaced versioning for a module so that you can import a more precise version to maintain compatibility. For example, the directory structure might be arranged as follows to support version 1 and version 2 of a module: .. code-block:: text omni.my.extension/ python/ __init__.py v1.py v2.py _impl/ v1/ MyClass.py my_function.py v1/ MyClass.py my_function.py With this structure the imports can be selectively added to the top level files to make explicit versioning decisions for the available APIs. .. code-block:: python import omni.graph.tools as og # Always use the latest version of the interfaces, potentially breaking on upgrade import omni.graph.tools.v1 as og # Lock yourself to version 1, requiring explicit change to upgrade import omni.graph.tools.v2 as og # Lock yourself to version 2, requiring explicit change to upgrade The main files might contain something like this to support that import structure: .. code-block:: python # __init__.py from .v1 import * # v1.py from ._impl/v1/MyClass import MyClass from ._impl/v1/my_function import my_function # v2.py from ._impl/v2/MyClass import MyClass from ._impl/v2/my_function import my_function You can see how version selection redirects you to the matching versions of the classes and functions without any renaming necessary. The deprecation messaging can then be applied to older versions as before. It might also be desirable to apply versioning information to class implementations so that their version can be checked in a standard way where it is important. .. code-block:: python @version(1) @deprecated("Use omni.graph.tools.v2.MyClass instead") class MyClass: pass More sophisticated mechanisms could provide version conversions as well, so that you can always get a certain version of a class if you require it, even if it was created using the old API by providing a function with a standard name: .. code-block:: python @version(1) @deprecated("Use omni.graph.tools.v2.MyClass instead") class MyClass: @classmethod @version_compatibility(1) def upgrade_version(cls, old_version) -> MyClass: return MyClass(old_version.parameters)
13,499
reStructuredText
39.178571
123
0.691681
omniverse-code/kit/exts/omni.graph.core/docs/internal/versioning_nodes.rst
.. _omnigraph_versioning_nodes: Node Type Versioning #################### Although it will probably not happen too frequently, the implementation of node types will occasionally change in such a way that makes them incompatible with existing uses. In extreme cases the best course of action might be to create an entirely new node type with the new functionality. For less intrusive changes these are the types of modifications for which OmniGraph supports backward compatibility. - :ref:`omnigraph_versioning_nodes_abi` - :ref:`omnigraph_versioning_nodes_renaming` - :ref:`omnigraph_versioning_nodes_removing` - :ref:`omnigraph_versioning_nodes_modifying` - :ref:`omnigraph_versioning_nodes_collisions` .. hint:: Most node type version changes will result in a corresponding minor version bump of the extension in which they live. .. _omnigraph_versioning_nodes_abi: ABI Versioning ************** ABI versioning for node types should follow the basic Carbonite guidelines. `INodeType` is a Carbonite interface, so its backward compatibiliy is assured by following the update guidelines for it described in :ref:`omnigraph_versioning_carbonite`. .. _omnigraph_versioning_nodes_renaming: Renaming A Node Type ******************** From time to time users may wish to change the name of their node without changing the node itself, perhaps because they came across a better name, to fix a misspelling, or to prevent conflicts with an existing name. The functionality to do this is currently hardcoded in the Node.cpp file but should be surfaced to the ABI. Here’s a potential addition to the INodeType interface to make it happen: .. code-block:: cpp /** * Create an alternate name for a node type, most often used to support renaming a node type. * When the node type is deregistered the alternate name will be automatically removed as well. * * @param[in] alternateName Secondary name that can be used interchangeably with nodeTypeName * @param[in] nodeTypeName Name of the node type for which an alternate is to be created * @return True if the alternate name was successfully set up, false if the node type name could * not be found or the alternateName was already in use (both issue a warning) */ bool(CARB_ABI* createNodeTypeAlias)(char const* alternateName, char const* nodeTypeName); The alternate name(s) will be specified in the .ogn file so that the node definition can control them directly. .. code-block:: json { "MySpiffyNode": { "$comment": "...other stuff...", "alias": "MySpifyNode" } } .. warning:: This feature is not yet implemented. Move to user-side documentation once it is. .. _omnigraph_versioning_nodes_removing: Removing A Node Type ******************** From time to time a user may wish to withdraw support for a node, perhaps because they have a more functional replacement, or the function it provided is no longer relevant. Support will be added to support our soft deprecation path whereby the node first starts issuing deprecation warnings when it is instantiated and then later will be removed entirely. The second phase just involves deletion of the node from the extension, which still retains some measure of backward compatibility as the user can always enable an earlier version of the extension to bring it back. The first phase will be supported in a standard way with this ABI function on INodeType: .. code-block:: cpp /** * Mark a node type as being deprecated with a message for the user on action they should take. * * @param[in] nodeObj Node to which this function applies * @param[in] deprecationMsg Message to the user describing what to do before the deprecation happens */ void(CARB_ABI* deprecate)(NodeObj& nodeObj, char const* deprecationMsg); /** * Return the deprecation message for an node. * * @param[in] nodeObj node to which this function applies * @return String containing the node deprecation message (nullptr if the node is not deprecated) */ char const*(CARB_ABI* deprecationMessage)(NodeObj const& nodeObj); The soft deprecation is enabled through a new .ogn keyword: .. code-block:: json { "MySpiffyNode": { "$comment": "...other stuff...", "deprecate": "This node will be removed in the next release" } } .. warning:: This feature is not yet implemented. Move to user-side documentation once it is. .. _omnigraph_versioning_nodes_modifying: Modifying Attributes ******************** Currently the `updateNodeVersion` method can be overridden on the node type implementation to support arbitrary changes to attributes or internal node state, however it happens after the node initialization and therefore cannot support things like moving connections. We've already seen several examples of desired upgrades, including the ability to have a soft deprecation where old attributes remain in place, so this mechanism needs to be extended to be more robust. The changes to a node type that we will have a built-in upgrade path for are: - Renaming an attribute - Soft deprecation of an attribute Things that are not planned to be handled directly include: - Removal of an attribute - Replacement of an attribute with another attribute or attributes .. _omnigraph_renaming_attributes: Renaming Attributes =================== Renaming an attribute can only be done if the attribute keeps all of the defining properties and only the name changes. This includes the port type, data type, default value, and memory type. The metadata, such as UI name, hidden state, etc. can be changed if required without affecting the attribute renaming. This is what the attribute renaming would look like in the .ogn file, where an input named "x" changes its name to "red". .. code-block:: json :caption: Before the renaming { "MyNode": { "version": 1, "description": "This is my node", "inputs": { "x": { "type": "float", "description": "The first value", "uiName": "First Value" } } } } .. code-block:: json :caption: After the renaming :emphasize-lines: 3,7,9,10 { "MyNode": { "version": 2, "description": "This is my node", "inputs": { "red": { "oldName": ["x", 1], "type": "float", "description": "The red value", "uiName": "Red Value" } } } } The **oldName** keyword takes a pair of values consisting of the old attribute name and the last node version before it was renamed. There can be multiple pairs on the off chance that you rename the same attribute more than once. Notice how the node type version number was incremented as well, to tell OmniGraph that this implementation is different from the previous version. In order to support this there must be an addition to the `IAttribute` interface in the ABI that can remember this information for processing. This is what it might look like: .. code-block:: cpp /** * Define an old name by which an attribute was known. * * @param[in] attributeObj Attribute to which this function applies * @param[in] oldName Old name by which the attribute was known * @param[in] lastVersion Last node type version in which the old name was valid */ void(CARB_ABI* renamedFrom)(AttributeObj& attributeObj, char const* oldName, int lastVersion); .. warning:: This feature is not yet implemented. Move to user-side documentation once it is. Soft Deprecation Of Attributes ============================== In order to respect our :ref:`omnigraph_soft_deprecation`, when an attribute is to be removed it must be flagged as deprecated in its .ogn file, along with a message telling users what they must do to prepare for the loss of the attribute, if possible. The node type version does not yet need to be bumped as nothing has really changed for it at this point. .. code-block:: json :emphasize-lines: 3,7,9,10 { "MyNode": { "version": 1, "description": "This is my node", "inputs": { "x": { "type": "float", "description": "The first value", "uiName": "First Value", "deprecated": "Use 'offset' instead." }, "offset": { "type": "float", "description": "This will be added to all values." } } } } This flag is set on the Attribute using `IInternal::deprecateAttribute()` but there should be no need to call this method outside of the node generator tool. `IAttribute::isDeprecated()` can be called to retrieve the flag and `IAttribute::deprecationMessage()` will return the associated message. When actual deprecation occurs then the appropriate action will take the place of the deprecation message - renaming, removing, or replacing the attribute. Modifying Node Behaviour ======================== In addition to changes in attributes, there may also be a change in attribute interpretation or of computational algorithm. A simple example might be the switch to a more numerically stable integration method, or the addition of an extra parameter that can switch computation type. For any change that affects how the node is computed you must bump the node type version number. A mismatch in version number for a node being created and the current node implementation will trigger a callback to the `updateNodeVersion` function, which your node can override if there is some cleanup you can do that will help maintain expected operation of the node. .. _omnigraph_versioning_nodes_collisions: Generated Code Rearrangement **************************** As the number of nodes and variety of use cases has grown some weaknesses in the structure of the generated code directories has been found. This section will describe the problems to be solved without yet any proposed solutions. Node Collisions =============== The generated OgnXXDatabase.h files are all stored in a single directory `_build/ogn/include`, which opens the possibility of name collisions as well as keeping the contents of an extension self-contained. Global Documentation ==================== The generated OgnXX.rst files are all stored in a single directory `_build/ogn/docs`, which while allowing creation of an index that lists all available nodes opens the possibility of collisions as above, and does not tie the documentation of individual nodes to the extensions that implement them. This problem is even more apparent with external/downstream extensions which aren't included in the Kit build and which therefore will not appear in documentation anywhere. There is also a lack of ability for a user to jump from a node type definition or node instantiation to the documentation for that node type, which would be helpful in understanding how to use a node type. Bits and pieces appear throughout the UI (descriptions, attribute types, etc. as tooltips) but this is not the same as a single source of everything related to the node type. Python Builds ============= The Python node types can be built at runtime so while it's helpful to have the prebuilt OgnXXDatabase.py files available it's not strictly necessary. A user should be able to include a Python node type in an extension simply by linking the directory to which it belongs to their build module directory, or even in their extension's directory if they are using the local Python extension approach. Getting in the way of this are first the lack of versioning support in the generated code, especially for local extensions, so that if a user loads their extension using multiple versions of the Kit SDK the generated code will only match one of them. Ideally they would like to support both and the generated code would be a cache, similar to how packman works. The Warp team has implemented something like this already for their own code generator. The Symlinked ogn Directory =========================== Also a problem in the Python generation is how the extension is expecting an `/ogn/` directory in their Python module that has a `nodes/` subdirectory symlinked back to the source for their nodes. The latter is to support hot reload, however the method of doing that breaks the standard method in the build system. If, as above, the generated files appeared elsewhere then there would be no need for the `/ogn/` directory and any `nodes/` subdirectory could be linked directly by the build for hot reload support. The ogn/tests Directory ======================= Similarly to the above the generated tests appear in the `/ogn/tests` subdirectory and the .usda files used for the tests appear in the `/ogn/tests/usd` subdirectory below that. These directories may also vary based on the version of the code generator and the version of the node type. They may also be generated at runtime rather than at build time, making the current mechanism for locating and registering the tests ineffective. Similarly, if outdated versions exist then they should not be registered and run, so the whole test registration mechanism may need rethinking. Supporting Multiple Node Type Versions ====================================== At the moment although there is ABI support for creating multiple versions of the same node type at the same time there is no API support, so that is a necessary addition to make before the code generator can fully support them. Setting that aside, for the code generation there is currently no support for multiple versions of the same node type. The paths and object names are hardcoded to just the node type and/or class name without a version number attached to it. This would also suffer from the same file collisions cited above, exacerbated by the fact that the names for multiple versions would currently be exactly the same. Other Possible Node Type Changes ******************************** This is a catch-all to mention any types of node modifications that won't have intrinsic support. They are listed here to make note of the fact that they have been considered, and if they occur often may merit more direct support. - Node type consolidation. e.g. we had a vertical stack node type and a horizontal node stack type and they were replaced by a single stack node type with an attribute defining the direction. - Node language conversion. A node that was implemented in Python that is changed to be implemented in C++. Ideally both might be available to the user as the Python one is easier to debug but the C++ one is faster. - Device targeting. A node might wish to be implemented on both the CPU and the GPU so that an intelligent scheduler can choose where to execute the node at runtime. Or a node type implementation may just switch from one to the other between versions for other reasons.
15,237
reStructuredText
45.742331
126
0.717792
omniverse-code/kit/exts/omni.graph.core/docs/internal/versioning_behavior.rst
.. _omnigraph_versioning_behavior: Code Behavior Versioning ######################## Modifying code behavior is a little trickier than modifying interfaces as the change is invisible to the user writing code. They can only see it at runtime. One example of this is the deprecation of support for the global implicit graph. The ABI does not change to remove support for it, just the behavior of the graph construction. This follows the standard :ref:`omnigraph_deprecation_approach` to give users ample opportunity to modify their code use. The key here is strategic identification of reliance on the old code behaviour so that the user can be properly notified of the need to handle the deprecation. .. hint:: Using the deprecation process ensures that each step only involves a minor version bump to the extension as existing code will continue to work. In the final phase of removal of the original behavior a major version bump may be required.
967
reStructuredText
47.399998
116
0.77456
omniverse-code/kit/exts/omni.graph.core/docs/internal/versioning_python.rst
.. _omnigraph_versioning_python: Python Support Versioning ######################### Python has the simultaneous strength and weakness of introspection, where users can access anything that appears in the Python scripts, making all of the Python deliverables a potential part of the API. This is not a reasonable amount of code to support so part of the Python versioning involves defining the API "surface" that versioning support will be provided for. Python Bindings Support *********************** Python bindings can be versioned in parallel with the ABI since for the most part they all correspond to ABI functions. There are exceptions where bindings functions are made as variations of ABI functions to make them more Pythonic, or to provide missing functionality like creation of Python class wrappers. These should be versioned in exactly the same way as the ABI functions so in any given extension version the set of bindings will be consistent. Defining The Python API Surface ******************************* To support proper versioning we first have to be able to define what constitutes "a version" of a set of Python scripts. The easiest way to do this is to use the import system to define the specific set of Python objects that will be supported as part of a version in the way that many popular packages such as *numpy* and *pandas* do. .. code-block:: python import omni.graph.core as og The officially supported Python API "surface" is everything that can be imported in this way. There also might be some submodules that have separate import support in the same way you can use both *import os* and *import os.path*. The extension will provide the set of support imports in its help information. Hiding Implementation Definitions ================================= Python programmers have an understanding that elements with a leading underscore or double underscore ("dunder") are not meant to be public so this can be used to signal this fact to anyone looking through the import structure. This is done at a high level using a module structure that looks like this: .. code-block:: text omni.my.extension/ └── python/ ├── __init__.py ├── _impl/ | extension.py | my_script.py └── tests/ └── test_my_extension.py With this structure then anyone trying to import anything that's not officially supported would be faced with something like this: .. code-block:: python import omni.my.extension._impl.my_script my_script.my_script() It's still possible to do this kind of thing but any Python programmer will realize there is something wrong with that. The method for accessing this same function in an officially supported way would be this: .. code-block:: python import omni.my.extension as me me.my_script() Inside the *__init__.py* file the officially supported API surface is all imported from wherever it happens to be defined so that it can be accessed in this way. .. code-block:: python :caption: __init__.py """Define the API surface for omni.my.extension To access the API for this extension use this import: import omni.my.extension as me To access the testing support for this extension use this import: import omni.my.extension.tests as met """ from ._impl.extension import PublicExtension # Necessary for automatic extension initialization from ._impl.my_script import my_script .. code-block:: python :caption: tests/__init__.py """Define the API surface for omni.my.extension.tests To access the testing support for this extension use this import: import omni.my.extension.tests as met """ from ._impl.test_support import load_test_file .. note:: For future examples the comments will be elided for clarity but the API should always be commented. Adding Version Support ********************** After the soft deprecation period is over the deprecated features will move out of the main imports and into a new location that is version-specific. This will allow continue support for deprecated functions without cluttering up the active code. .. warning:: This is one approach to making a version-specific API layer, to be vetted. A deprecated version should be presented to the user as a fully intact entity so that it can be used pretty much as it was before deprecation. For example if I have upgraded my extension from version 1, which contained a function *old_function()* to version 2, which deprecates that function and adds *new_function()* then the module structure will be altered to this: .. code-block:: text omni.my.extension/ └── python/ ├── __init__.py ├── _impl/ | extension.py | my_script.py └── _1/ __init__.py my_script.py Only the new function will be exposed in the main API definition: .. code-block:: python :caption: __init__.py from ._impl.extension import PublicExtension from ._impl.my_script import new_function .. code-block:: python :caption: _1/__init__.py from .my_script import old_function A-La Carte Access ================= The most flexible method of access is to use a mix-and-match approach to pick up the exact versions of everything you want. This is intentionally difficult to do as it's not a usage pattern we want to encourage, more of a back door that allows users to revert to previous behaviour in very specific places. .. code-block:: python :caption: user_code.py import omni.my.extension as me import omni.my.extension._1 as me1 if some_condition: me.new_function() else: me1.old_function() Always-Latest Access ==================== The most desired type of access is to use the latest API only so that when a new version is added your code will already be using it. So long as we keep the top level *__init__.py* updated to define the latest API this is also the easiest to use: .. code-block:: python :caption: user_code.py import omni.my.extension as me me.new_function() Fixed Version Access ==================== If users wish to lock to a specific version so that they can rely on a stable base they can lock to a specific version. This will only affect them at some point in the future when something in that version is hard-deprecated. .. code-block:: python :caption: user_code.py # Note the leading underscore on the version, hinting that it should not be accessed directly import omni.my.extension._1 as me me.old_function() Module Constant Gated Access ============================ We also want to support a hybrid approach where the users can enable the new interface if they want in order to check whether they need to modify any code for compatibility, while not being locked into it if any work required cannot happen all at once. A Pythonic approach to this problem is to modify the interface definition to have a module-level constant that can be set to different values in order to dynamically flip between different versions using the same import statement. .. code-block:: python :caption: __init__.py import os __version = os.getenv("omni.my.extension", 2) if __version == 1: from ._1 import * elif __version == 2: from ._impl.my_script import new_function else: raise VersionError(f"Environment variable omni.my.extension must be in [1, 2] - got {__version}) .. note:: This is intentionally not something that can change at runtime as that would have unpredictable effects. Something more sophisticated like a sequence of **unload module** --> **edit environment variable** --> **reload module** would be required to support that. Deprecation Of Python Objects ***************************** In order to follow the :ref:`omnigraph_deprecation_approach` for Python objects there has to be different treatment for different types of Python objects so as to provide the deprecation information to the user in an informative but non-intrusive manner. The soft deprecation stage is especially import in Python as the things that can be deprecated can be small and change at a relatively high frequency. To make it easier to deprecate Python code a number of decorators have been created which can be accessed through this import: .. code-block:: python import omni.graph.tools.deprecate as ogd help(ogd) You can see the details of how to deprecate everything in the :ref:`Python deprecation documentation<omnigraph_deprecation>`.
8,587
reStructuredText
37
125
0.718412
omniverse-code/kit/exts/omni.graph.core/docs/internal/internal.rst
:orphan: .. _omnigraph_internal_development: Kit Internal Documentation ########################## .. note:: This documentation is used internally and is intended for Kit developers, though it may provide some insights to Kit SDK users as well. .. toctree:: :maxdepth: 1 :glob: Deprecated Code For Schema Prim<SchemaPrimSetting> Versioning and Deprecation <versioning>
395
reStructuredText
19.842104
113
0.686076
omniverse-code/kit/exts/omni.graph.core/docs/internal/versioning.rst
.. _omnigraph_internal_versioning: OmniGraph Versioning And Deprecation #################################### There are many ways in which the OmniGraph code can change from one release to another. This document sets out to enumerate all of them and provide guidelines on how to handle each of them. .. important:: The overall philosophy of versioning in OmniGraph is "First do no harm". Many internal and external customers will be relying on many different versions of OmniGraph so it is up to us to minimize their disruption by providing backward compatibility for as long as possible. Breaking changes should be rare, and vital for future development. .. _omnigraph_deprecation_approach: Deprecation Approach ******************** On those rare occasions where it is necessary to deprecate and eventually remove existing functionality the deprecation should always follow a safe process. This is the generic set of steps to follow for any kind of deprecation. You can see what it should look like to the user in :ref:`omnigraph_deprecation_communication` If there is a significant amount of code to deprecate then you should move it into a separate location for easier removal later. External functions follow the Carbonite ABI versioning :ref:`omnigraph_versioning_carbonite`. If an internal function call is significantly different between the two behaviors then introduce a new version of the function and move the old implementation to a `deprecated/` subdirectory. .. code-block:: c++ :caption: Pre-Deprecation Version struct MyStruct { void mySnazzyFunction(); }; .. code-block:: c++ :caption: Post-Deprecation Version struct MyStruct { void mySnazzyFunction(); private: // Deprecated functions void mySnazzyFunctionV1(); }; void MyStruct::mySnazzyFunction() { if (OmniGraphSettings::myDeprecationFlag()) { mySnazzyFunctionV1(); return; } // do the new stuff }; Code Notices ************ It's a good idea for the deprecation path to clearly mark all of the deprecated code paths. Here is a simple but effective comment that can be inserted before any deprecated code. .. code-block:: cpp // ============================================================================================================== // _____ ______ _____ _____ ______ _____ _______ ______ _____ // | __ | | ____|| __ \ | __ \ | ____|/ ____| /\ |__ __|| ____|| __ | // | | | || |__ | |__) || |__) || |__ | | / \ | | | |__ | | | | // | | | || __| | ___/ | _ / | __| | | / /\ \ | | | __| | | | | // | |__| || |____ | | | | \ \ | |____| |____ / ____ \ | | | |____ | |__| | // |_____/ |______||_| |_| \_\|______|\_____|/_/ \_\|_| |______||_____/ // // Code below here is no longer recommended for use in new code. // // ============================================================================================================== Specific Versioning Information ******************************* These documents illustrate versioning and deprecation information that is specific to the modification of particular facets of OmniGraph. .. toctree:: :maxdepth: 1 :glob: Carbonite ABIs <versioning_carbonite> ONI ABIs <versioning_oni> Existing Node Types <versioning_nodes> Code Behavior <versioning_behavior> Extension Numbering <versioning_extensions> Using Settings For Deprecation<versioning_settings> USD File Format<versioning_usd> Python Support<versioning_python> Python Deprecation<deprecation> Compatibility Testing<versioning_testing>
3,750
reStructuredText
36.51
119
0.5744
omniverse-code/kit/exts/omni.graph.core/docs/internal/versioning_testing.rst
.. _omnigraph_versioning_testing: Testing Version Upgrades ######################## In order to properly support backward compatibility we need tests in our own code that verify that any code that has not been fully deprecated continues to be accessible and, if possible, functions as intended. In an ideal world we would have tests that have full code coverage and when we deprecate some code we would move the tests exercising that old functionality to the deprecated section and new tests would be added to exercise the new code. Practically speaking this isn't possible so there will be a compromise between thoroughness and test time. These are some of the areas to consider when designing tests for versioning: - Use the ETM (Extension Test Matrix) to verify our new extension versions against existing users of our extensions - For Python code, create "button tests" where every import in our API surface is checked for type and existence - For C++ code create "ABI tests" whose only job is to exercise every function in the ABI - For Python bindings create some combination of the above two that verify every function in the ABI Creating these tests will require some test scaffolding that allows us to confirm coverage. Ideally this would take the form of a code-coverage tool so that we don't have to write our own as that is prone to omissions. .. note:: Performance testing is an important part of compatibility however it will be handled in a separate effort.
1,485
reStructuredText
56.153844
120
0.786532
omniverse-code/kit/exts/omni.graph.core/docs/internal/versioning_settings.rst
.. _omnigraph_versioning_settings: OmniGraph Settings For Deprecation ################################## The OmniGraph settings are instantions of `carb::settings` that are specific to OmniGraph. They are used to put in temporary settings for deprecation that follows the standard :ref:`omnigraph_deprecation_approach`. The settings for deprecation will use the namespace `/persistent/omnigraph/deprecation/` to emphasize the fact that these are temporary and meant to control deprecated code. Add The Deprecation Setting *************************** The settings are all controlled through `omni.graph.core/plugins/OmniGraphSettings.{h,cpp}` so the first thing to do is go there and create a new setting with the deprecation name and set up its default value to be the one that will take the original code path. You may need to add some compatibility code if two or more of the settings have illegal combinations. Add an accessor method as well so that your code can read it to decide which path to take. It will also be helpful to add a Python constant containing the path to the new setting in the file `omni.graph/python/_impl/settings.py` Add UI Access Of The Setting **************************** For testing and test migrations it's useful to add access to the setting through the standard settings UI. You can do this by going to `omni.graph.ui/python/scripts/omnigraph_settings_editor.py` and adding a new settings widget that is customized to the path of your new setting. Modify C++ Code Paths Using The Setting *************************************** If you've added an access method for your setting then you can switch code paths by checking that setting. This example is how you do it if you are within the core where the settings object is accessible. .. code-block:: cpp #include "OmniGraphSettings.h" void someCode() { if (OmniGraphSettings::myDeprecationIsActive()) { doTheNewThing(); } else { // It's important to only warn once to avoid spamming the user's console CARB_LOG_WARNING_ONCE("The old thing is deprecated, use the new thing instead"); doTheDeprecatedThing(); } } If you have code that does not live in the core but changes behaviour based on the setting then you have to use the settings ABI to access it. The most common reason for this would be changing behavior within the core Python bindings. .. code-block:: cpp #include <carb/settings/ISettings.h> void someCode() { auto iSettings = carb::getCachedInterface<carb::settings::ISettings>(); if (! iSettings) { CARB_LOG_ERROR_ONCE("Unable to access the iSettings interface"); } // If the settings interface isn't available assume the default. // The hardcoded name is here to avoid exposing the string in the API when we know it will go away soon. if (iSettings && iSettings->getAsBool("/persistent/omnigraph/deprecation/myNewThing")) { doTheNewThing(); } else { // It's important to only warn once to avoid spamming the user's console CARB_LOG_WARNING_ONCE("The old thing is deprecated, use the new thing instead"); doTheDeprecatedThing(); } } Modify Python Code Paths Using The Setting ****************************************** After adding the new setting to the Python *og.Settings* class you can use it to select code paths based on its value. As the Python API is always visible there will be direct access to it and you won't have to drop down to the Carbonite settings bindings. You can see its definition at :py:class:`omni.graph.core.Settings` .. code-block:: python import omni.graph.core as og def some_code(): if og.Settings()(og.Settings.MY_SETTING_NAME): do_the_new_thing() else: do_the_old_thing() You can also use the class as a context manager to temporarily modify the setting, most useful in compatibility tests: .. code-block:: python import omni.graph.core as og while og.Settings.temporary(og.Settings.MY_SETTING_NAME, False): do_the_old_thing()
4,223
reStructuredText
40.009708
118
0.682216
omniverse-code/kit/exts/omni.graph.core/docs/internal/versioning_oni.rst
.. _omnigraph_versioning_oni: OmniGraph Versioning For ONI Interfaces ####################################### See the description of the :ref:`omnigraph_oni_versions`. For this example this sample interface will be used, shown in its initial form. .. code-block:: cpp OMNI_DECLARE_INTERFACE(ITestInterface); class ITestInterface_abi : public omni::core::Inherits<omni::core::IObject, OMNI_TYPE_ID("omni.graph.core.ITestInterface")> { protected: /** * Does the old thing * * @param[in] value The value of the old thing */ virtual void oldThing_abi(int value) noexcept = 0; }; class TestInterface : public omni::Implements<ITestInterface> { public: TestInterface() = default; ~TestInterface() = default; protected: void oldThing_abi(int value) noexcept override { doSomething(value); return; } }; void getInterfaceImplementations(const omni::InterfaceImplementation** out, uint32_t* outCount) { static const char* interfacesImplemented[] = { "ITestInterface" }; static omni::InterfaceImplementation impls[] = { { "omni.graph.core.ITestInterface", []() { return static_cast<omni::IObject*>(new ITestInterface); }, 1, interfacesImplemented, CARB_COUNTOF32(interfacesImplemented) } }; *out = impls; *outCount = CARB_COUNTOF32(impls); } To call the interface code you instantiate one of them and call an interface function: .. code-block:: cpp auto iTestOld = omni::core::createType<ITestInterface>(); iTest.oldThing(1); Adding A Function ***************** A new function is added by creating a new interface that derives from the old interface and implements the new function. .. code-block:: cpp :emphasize-lines: 2,13,30,38-43 class ITestInterface2_abi : public omni::core::Inherits<omni::graph::core::ITestInterface, OMNI_TYPE_ID("omni.graph.core.ITestInterface2")> { protected: /** * Does the new thing * * @param[in] value The value of the new thing */ virtual void newThing_abi(int value) noexcept = 0; }; class TestInterface2 : public omni::Implements<ITestInterface2> { public: TestInterface2() = default; ~TestInterface2() = default; protected: void newThing_abi(int value) noexcept override { doSomethingNew(value); return; } }; void getInterfaceImplementations(const omni::InterfaceImplementation** out, uint32_t* outCount) { static const char* interfacesImplemented[] = { "ITestInterface" }; static const char* interfacesImplemented2[] = { "ITestInterface2" }; static omni::InterfaceImplementation impls[] = { { "omni.graph.core.ITestInterface", []() { return static_cast<omni::IObject*>(new ITestInterface); }, 1, interfacesImplemented, CARB_COUNTOF32(interfacesImplemented) }, { "omni.graph.core.ITestInterface2", []() { return static_cast<omni::IObject*>(new ITestInterface2); }, 1, interfacesImplemented2, CARB_COUNTOF32(interfacesImplemented) } }; *out = impls; *outCount = CARB_COUNTOF32(impls); } The old code is untouched by this and continues to function as usual. Any code wishing to use the new function has to instantiate the new interface. .. code-block:: cpp auto iTestOld = omni::core::createType<ITestInterface>(); iTest.oldThing(1); // iTest.newThing(2); // Would fail - this behaves as the old interface only auto iTestNew = omni::core::createType<ITestInterface2>(); iTestNew.oldThing(1); // Still works iTestNew.newThing(2); Deprecating Functions ********************* In the rare occasion where you want to deprecate a function entirely you must follow this process to ensure maximum compatibility and notification to affected users. Step 1: Add a Deprecation Notice ================================ The team should decide exactly when a deprecation will happen, as it is beneficial to group deprecations together to minimize the work required on the user end to deal with the deprecations. Once that's decided the first thing to do is to communicate the deprecation intent through the decided-upon channels. Once that has happend the implementation of the interface function will add a warning message that the function is deprecated and will be going away. .. code-block:: cpp :emphasize-lines: 8,26 OMNI_DECLARE_INTERFACE(ITestInterface); class ITestInterface_abi : public omni::core::Inherits<omni::core::IObject, OMNI_TYPE_ID("omni.graph.core.ITestInterface")> { protected: /** * This function is deprecated - use newFunction() instead * * Does the old thing * * @param[in] value The value of the old thing */ virtual void oldThing_abi(int value) noexcept = 0; }; class TestInterface : public omni::Implements<ITestInterface> { public: TestInterface() = default; ~TestInterface() = default; protected: void oldThing_abi(int value) noexcept override { CARB_LOG_WARNING("ITestInterface::oldFunction() is deprecated - use ITestInterface::newFunction() instead"); doSomething(value); return; } }; Step 2: Make The Deprecated Path An Error ========================================= To ensure the use of the deprecated path is highly visible in this phase the warning is upgraded to an error and the comments on the function make it more clear that it is not to be used in new code. .. code-block:: cpp :emphasize-lines: 8,22 OMNI_DECLARE_INTERFACE(ITestInterface); class ITestInterface_abi : public omni::core::Inherits<omni::core::IObject, OMNI_TYPE_ID("omni.graph.core.ITestInterface")> { protected: /** * This function is deprecated - use newFunction() instead */ virtual void oldThing_abi(int value) noexcept = 0; }; class TestInterface : public omni::Implements<ITestInterface> { public: TestInterface() = default; ~TestInterface() = default; protected: void oldThing_abi(int value) noexcept override { CARB_LOG_ERROR("ITestInterface::oldFunction() is deprecated - use ITestInterface::newFunction() instead"); doSomething(value); return; } }; .. note:: You can choose to make the deprecation even more emphatic if calling the function could do something bad like create instability in the code. You can do so by using a `throw` instead of just `CARB_LOG_ERROR`. Step 3: Hard Deprecation Of The Function ======================================== When the final step is necessary you must bump the major version number of the extension as this is a breaking change. You must also bump the version of the interface so that in those cases where an extension has access to both the old and new versions of the interface it will prefer the new one. The interface class remains though, even if the last function has been taken from it. .. important:: This type of hard deprecation is extremely disruptive as it will require extensions to be rebuilt and possibly modified to conform to the new interface. It should only be used when there is something in the interface that is intrinsically dangerous or difficult to continue supporting. .. code-block:: cpp :emphasize-lines: 5-6,14-15,25 OMNI_DECLARE_INTERFACE(ITestInterface); class ITestInterface_abi : public omni::core::Inherits<omni::core::IObject, OMNI_TYPE_ID("omni.graph.core.ITestInterface")> { }; class TestInterface : public omni::Implements<ITestInterface> { public: TestInterface() = default; ~TestInterface() = default; protected: }; void getInterfaceImplementations(const omni::InterfaceImplementation** out, uint32_t* outCount) { static const char* interfacesImplemented[] = { "ITestInterface" }; static const char* interfacesImplemented2[] = { "ITestInterface2" }; static omni::InterfaceImplementation impls[] = { { "omni.graph.core.ITestInterface", []() { return static_cast<omni::IObject*>(new ITestInterface); }, 2, interfacesImplemented, CARB_COUNTOF32(interfacesImplemented) }, { "omni.graph.core.ITestInterface2", []() { return static_cast<omni::IObject*>(new ITestInterface2); }, 1, interfacesImplemented2, CARB_COUNTOF32(interfacesImplemented) } }; *out = impls; *outCount = CARB_COUNTOF32(impls); }
9,179
reStructuredText
31.210526
121
0.628718
omniverse-code/kit/exts/omni.graph.core/docs/internal/SchemaPrimSetting.rst
Schema Prim Setting Flow ######################## The **useSchemaPrim** setting controls how the graph is backed by USD. The code was refactored to divert as much of the old code as possible into the *deprecated/* directory so that it is isolated and won't change until such time as the setting is made permanent and it can be deleted. The graph is divided into three sections - code that was converted fully to use the schema approach, hybrid code that uses the setting to switch between schema and non-schema behaviours, and deprecated code that is only used by the non-schema code paths. .. _omnigraph_extension_dependency_graph: .. mermaid:: flowchart LR subgraph Converted ComputeGraphImpl::parseGraph Graph::attachToStage Graph::changePipelineStage Graph::initFromStage Graph::onNodeTypeRegistered Graph::postLoadUpgradeFileFormatVersion Graph::rangeContainsOGNodes Graph::writeSettingsInUSD GraphContext::addPrimToCache GraphContext::attachToStage GraphContext::createNeededNodesForPrim GraphContext::createNewNode GraphContext::createNewSubgraph GraphContext::initializeFabric GraphContext::updateChangeProcessing end subgraph Uses Setting Graph::couldPrimBelongToGraph Graph::createGraphAsNode Graph::findFileFormatVersionInRange Graph::initContextAndFabric Graph::isGlobalGraphPrim Graph::isOmniGraphPrim Graph::isPrimAnOmniGraphMember Graph::reloadFromStage Graph::requiresFileFormatCallback Graph::setNodeInstance Graph::setPathToGraph Graph::writeFileFormatVersion Graph::writeSettingsToUSD Graph::writeTokenSetting GraphContext::_::getPrimsNodeTypeAttribute GraphContext::_addGatherPrimToFlatCacheIndex GraphContext::_addPrimToFlatCacheIndex GraphContext::checkForDynamicAttributes GraphContext::createNewNodeWithUSD GraphContext::createNewSubgraph GraphContext::createNodeFromPrim GraphContext::createNodesAndSubgraphs GraphContext::initializeNode GraphContext::getUpstreamPrims GraphContext::reloadGraphSettings Node::isPrimAnOmniGraphNode Node::getPrimsNodeTypeAttribute Node::Node OmniGraphUsdNoticeListener::processNewAndDeletedGraphs PluginInterface::attach end subgraph Deprecated ComputeGraphImpl::parseGraphNoSchema Graph::attachToStageNoSchema Graph::couldPrimBelongToGraphNoSchema Graph::createGraphAsNodeNoSchema Graph::findFileFormatVersionInRangeNoSchema Graph::initContextAndFlatCache Graph::initContextAndFlatCacheNoSchema Graph::initFromStageNoSchema Graph::isGlobalGraphPrimNoSchema Graph::reloadFromStageNoSchema Graph::writeFileFormatVersionNoSchema Graph::writeSettingsToUSDNoSchema GraphContext::_addPrimToFlatCacheIndexNoSchema GraphContext::attachToStageNoSchema GraphContext::checkForDynamicAttributesNoSchema GraphContext::createNewSubgraphNoSchema GraphContext::createNodesAndSubgraphsNoSchema GraphContext::initializeFlatcacheNoSchema GraphContext::reloadGraphSettingsNoSchema NoSchemaSupport::writeEvaluatorTypeToSettings NoSchemaSupport::writeGraphBackingTypeToSettings NoSchemaSupport::writeGraphPipelineStageToSettings NoSchemaSupport::writeEvaluationModeToSettings NoSchemaSupport::writeTokenSetting PluginInterface::attachNoSchema end ComputeGraphImpl::parseGraph --> ComputeGraphImpl::parseGraphNoSchema ComputeGraphImpl::parseGraph --> Graph::attachToStage ComputeGraphImpl::parseGraph --> Graph::findFileFormatVersionInRange ComputeGraphImpl::parseGraph --> Graph::initFromStage ComputeGraphImpl::parseGraph --> Graph::setNodeInstance ComputeGraphImpl::parseGraph --> GraphContext::initializeFabric ComputeGraphImpl::parseGraph --> Node::Node GraphContext::addPrimToCache --> GraphContext::_addPrimToFlatCacheIndex GraphContext::attachToStage --> Graph::writeSettingsInUSD GraphContext::attachToStage --> GraphContext::createNodesAndSubgraphs GraphContext::createNeededNodesForPrim --> GraphContext::createNodeFromPrim GraphContext::createNewNode --> Graph::setNodeInstance GraphContext::createNewNodeWithUSD --> GraphContext::_addPrimToFlatCacheIndex GraphContext::createNodeFromPrim --> Graph::setNodeInstance GraphContext::createNodeFromPrim --> GraphContext::initializeNode GraphContext::createNodesAndSubgraphs --> Graph::writeSettingsInUSD GraphContext::createNodesAndSubgraphs --> GraphContext::createNeededNodesForPrim GraphContext::createNodesAndSubgraphs --> GraphContext::createNodesAndSubgraphs GraphContext::initializeFabric --> GraphContext::_addGatherPrimToFlatCacheIndex GraphContext::initializeFabric --> GraphContext::_addGatherPrimToFlatCacheIndex GraphContext::initializeFabric --> GraphContext::_addPrimToFlatCacheIndex GraphContext::initializeFabric --> GraphContext::_addPrimToFlatCacheIndex GraphContext::initializeNode --> GraphContext::_::getPrimsNodeTypeAttribute GraphContext::initializeNode --> GraphContext::checkForDynamicAttributes GraphContext::initializeNode --> Node::getPrimsNodeTypeAttribute GraphContext::updateChangeProcessing --> GraphContext::_::getPrimsNodeTypeAttribute GraphContext::updateChangeProcessing --> GraphContext::_addPrimToFlatCacheIndex GraphContext::updateChangeProcessing --> GraphContext::createNodesAndSubgraphs GraphContext::updateChangeProcessing --> Node::getPrimsNodeTypeAttribute GraphContext::_addPrimToFlatCacheIndex --> GraphContext::_addPrimToFlatCacheIndexNoSchema GraphContext::attachToStage --> GraphContext::attachToStageNoSchema GraphContext::checkForDynamicAttributes --> GraphContext::checkForDynamicAttributesNoSchema GraphContext::createNewSubgraph --> GraphContext::createNewSubgraphNoSchema GraphContext::createNodesAndSubgraphs --> GraphContext::createNodesAndSubgraphsNoSchema GraphContext::initializeFabric --> GraphContext::initializeFlatcacheNoSchema GraphContext::reloadGraphSettings --> GraphContext::reloadGraphSettingsNoSchema Graph::attachToStage --> Graph::attachToStageNoSchema Graph::attachToStage --> GraphContext::attachToStage Graph::changePipelineStage --> Graph::setNodeInstance Graph::couldPrimBelongToGraph --> Graph::couldPrimBelongToGraphNoSchema Graph::couldPrimBelongToGraph --> Graph::isGlobalGraphPrim Graph::couldPrimBelongToGraph --> Graph::isPrimAnOmniGraphMember Graph::createGraphAsNode --> Graph::createGraphAsNodeNoSchema Graph::createGraphAsNode --> Graph::setNodeInstance Graph::createGraphAsNode --> GraphContext::initializeFabric Graph::findFileFormatVersionInRange --> Graph::findFileFormatVersionInRangeNoSchema Graph::initContextAndFabric --> GraphContext::initializeFabric Graph::initContextAndFlatCache --> Graph::initContextAndFlatCacheNoSchema Graph::initFromStage --> Graph::attachToStage Graph::initFromStage --> Graph::initContextAndFabric Graph::initFromStage --> Graph::initFromStageNoSchema Graph::isGlobalGraphPrim --> Graph::isGlobalGraphPrimNoSchema Graph::postLoadUpgradeFileFormatVersion --> Graph::writeFileFormatVersion Graph::reloadFromStage --> Graph::attachToStage Graph::reloadFromStage --> Graph::rangeContainsOGNodes Graph::reloadFromStage --> Graph::reloadFromStageNoSchema Graph::reloadFromStage --> GraphContext::initializeFabric Graph::writeFileFormatVersion --> Graph::writeFileFormatVersionNoSchema Graph::writeSettingsToUSD --> Graph::writeFileFormatVersionNoSchema Graph::writeSettingsToUSD --> Graph::writeSettingsToUSDNoSchema Graph::writeSettingsToUSDNoSchema --> NoSchemaSupport::writeEvaluatorTypeToSettings Graph::writeSettingsToUSDNoSchema --> NoSchemaSupport::writeGraphBackingTypeToSettings Graph::writeSettingsToUSDNoSchema --> NoSchemaSupport::writeGraphPipelineStageToSettings NoSchemaSupport::writeEvaluatorTypeToSettings --> NoSchemaSupport::writeTokenSetting NoSchemaSupport::writeGraphBackingTypeToSettings --> NoSchemaSupport::writeTokenSetting NoSchemaSupport::writeGraphPipelineStageToSettings --> NoSchemaSupport::writeTokenSetting NoSchemaSupport::writeEvaluationModeToSettings --> NoSchemaSupport::writeTokenSetting Node::Node --> Node::isPrimAnOmniGraphNode PluginInterface::attach --> ComputeGraphImpl::parseGraph PluginInterface::attach --> Graph::findFileFormatVersionInRange PluginInterface::attach --> Graph::initContextAndFabric PluginInterface::attach --> Graph::rangeContainsOGNodes PluginInterface::attach --> PluginInterface::attachNoSchema OmniGraphUsdNoticeListener::processNewAndDeletedGraphs --> ComputeGraphImpl::parseGraph OmniGraphUsdNoticeListener::processNewAndDeletedGraphs --> Graph::isGlobalGraphPrim
9,752
reStructuredText
54.731428
119
0.728158
omniverse-code/kit/exts/omni.graph.core/docs/internal/versioning_carbonite.rst
.. _omnigraph_versioning_carbonite: OmniGraph Versioning For Carbonite Interfaces ############################################# See the description of the :ref:`omnigraph_carbonite_versions`. For this example this sample interface will be used, shown in its initial form. Note the structure integrity check at the end, designed to catch situations where a new function is unintentionally added to the middle of the interface. .. code-block:: cpp class ITestInterface { CARB_PLUGIN_INTERFACE("omni::graph::core::ITestInterface", 1, 0); /** * Does the old thing * * @param[in] value The value of the old thing */ void (CARB_ABI* oldFunction)(int value); }; // Update this every time a new ABI function is added, to ensure one isn't accidentally added in the middle STRUCT_INTEGRITY_CHECK(IAttribute, oldFunction, 1) void oldFunction_impl(int value) { doSomething(value); return; } Adding A Function ***************** A new function is added. The version number is bumped and the structural integrity check is updated. .. code-block:: cpp :emphasize-lines: 3,12-17,20,28-32 class ITestInterface { CARB_PLUGIN_INTERFACE("omni::graph::core::ITestInterface", 1, 1); /** * Does the old thing * * @param[in] value The value of the old thing */ void (CARB_ABI* oldFunction)(int value); /** * Does the new thing * * @param[in] value The value of the new thing */ void (CARB_ABI* newFunction)(int value); }; // Update this every time a new ABI function is added, to ensure one isn't accidentally added in the middle STRUCT_INTEGRITY_CHECK(IAttribute, newFunction, 2) void oldFunction_impl(int value) { doSomething(value); return; } void newFunction_impl(int value) { doSomethingNew(value); return; } Deprecating Functions ********************* In the rare occasion where you want to deprecate a function entirely you must follow this three-step process to ensure maximum compatibility and notification to affected users. Step 1: Add a Deprecation Notice ================================ The team should decide exactly when a deprecation will happen, as it is beneficial to group deprecations together to minimize the work required on the user end to deal with the deprecations. Once that's decided the first thing to do is to communicate the deprecation intent through the decided-upon channels. Once that has happend the implementation of the interface function will add a warning message that the function is deprecated and will be going away. .. code-block:: cpp :emphasize-lines: 6,26 class ITestInterface { CARB_PLUGIN_INTERFACE("omni::graph::core::ITestInterface", 1, 1); /** * This function is deprecated - use newFunction() instead * * Does the old thing * * @param[in] value The value of the old thing */ void (CARB_ABI* oldFunction)(int value); /** * Does the new thing * * @param[in] value The value of the new thing */ void (CARB_ABI* newFunction)(int value); }; // Update this every time a new ABI function is added, to ensure one isn't accidentally added in the middle STRUCT_INTEGRITY_CHECK(IAttribute, newFunction, 2) void oldFunction_impl(int value) { CARB_LOG_WARNING("ITestInterface::oldFunction() is deprecated - use ITestInterface::newFunction() instead"); doSomething(value); return; } void newFunction_impl(int value) { doSomethingNew(value); return; } Step 2: Make The Deprecated Path An Error ========================================= To ensure the use of the deprecated path is highly visible in this phase the warning is upgraded to an error and the comments on the function make it more clear that it is not to be used in new code. .. code-block:: cpp :emphasize-lines: 6-7,22 class ITestInterface { CARB_PLUGIN_INTERFACE("omni::graph::core::ITestInterface", 1, 1); /** * This function is deprecated - use newFunction() instead */ void (CARB_ABI* oldFunction)(int value); /** * Does the new thing * * @param[in] value The value of the new thing */ void (CARB_ABI* newFunction)(int value); }; // Update this every time a new ABI function is added, to ensure one isn't accidentally added in the middle STRUCT_INTEGRITY_CHECK(IAttribute, newFunction, 2) void oldFunction_impl(int value) { CARB_LOG_ERROR("ITestInterface::oldFunction() is deprecated - use ITestInterface::newFunction() instead"); doSomething(value); return; } void newFunction_impl(int value) { doSomethingNew(value); return; } Step 3: Hard Deprecation Of The Function ======================================== To ensure the ABI integrity functions may not be removed from the interface, however they can be renamed and all information about what they once were can be removed. At this point calls to them will no longer function. They will only issue an error. The function signatures remain the same to avoid type errors. The implementation of the deprecated function can be moved to a common graveyard of deprecated functions. .. code-block:: cpp :emphasize-lines: 5,23-27 class ITestInterface { CARB_PLUGIN_INTERFACE("omni::graph::core::ITestInterface", 1, 1); /* DEPRECATED */ void (CARB_ABI* deprecated_1)(int); /** * Does the new thing * * @param[in] value The value of the new thing */ void (CARB_ABI* newFunction)(int value); }; // Update this every time a new ABI function is added, to ensure one isn't accidentally added in the middle STRUCT_INTEGRITY_CHECK(IAttribute, newFunction, 2) void newFunction_impl(int value) { doSomethingNew(value); return; } void oldFunction_impl(int) { CARB_LOG_ERROR("ITestInterface::oldFunction() is deprecated - use ITestInterface::newFunction() instead"); return; }
6,382
reStructuredText
29.395238
119
0.63256
omniverse-code/kit/exts/omni.kit.widget.text_editor/PACKAGE-LICENSES/omni.kit.widget.text_editor-LICENSE.md
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.
412
Markdown
57.999992
74
0.839806
omniverse-code/kit/exts/omni.kit.widget.text_editor/config/extension.toml
[package] title = "Text Editor" category = "Internal" description = "Bindings to ImGuiColorTextEdit" version = "1.0.2" changelog = "docs/CHANGELOG.md" icon = "data/icon.png" preview_image = "data/preview.png" [dependencies] "omni.ui" = {} [[native.library]] path = "bin/${lib_prefix}omni.kit.widget.text_editor${lib_ext}" [[python.module]] name = "omni.kit.widget.text_editor" [[test]] dependencies = [ "omni.kit.renderer.capture", "omni.kit.ui_test", ] args = [ "--/app/window/dpiScaleOverride=1.0", "--/app/window/scaleToMonitor=false", "--no-window" ] stdoutFailPatterns.exclude = [ "*omniclient: Initialization failed*", ]
657
TOML
18.352941
63
0.669711
omniverse-code/kit/exts/omni.kit.widget.text_editor/omni/kit/widget/text_editor/__init__.py
## Copyright (c) 2018-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. ## """ omni.kit.widget.text_editor --------------------------- """ __all__ = ["TextEditor"] from ._text_editor import *
560
Python
31.999998
77
0.725
omniverse-code/kit/exts/omni.kit.widget.text_editor/omni/kit/widget/text_editor/tests/__init__.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 .test_text_editor import TestTextEditor
473
Python
46.399995
76
0.811839
omniverse-code/kit/exts/omni.kit.widget.text_editor/omni/kit/widget/text_editor/tests/test_text_editor.py
## Copyright (c) 2018-2019, 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__ = ["TestTextEditor"] import omni.kit.test from omni.ui.tests.test_base import OmniUiTest from pathlib import Path import omni.kit.app from .._text_editor import TextEditor EXTENSION_PATH = Path(omni.kit.app.get_app().get_extension_manager().get_extension_path_by_module(__name__)) GOLDEN_PATH = EXTENSION_PATH.joinpath("data/golden") STYLE = {"Field": {"background_color": 0xFF24211F, "border_radius": 2}} class TestTextEditor(OmniUiTest): async def test_general(self): """Testing general look of TextEditor""" window = await self.create_test_window() lines = ["The quick brown fox jumps over the lazy dog."] * 20 with window.frame: TextEditor(text_lines=lines) for i in range(2): await omni.kit.app.get_app().next_update_async() await self.finalize_test(golden_img_dir=GOLDEN_PATH, golden_img_name=f"test_general.png") async def test_syntax(self): """Testing languages of TextEditor""" import inspect window = await self.create_test_window() with window.frame: TextEditor(text_lines=inspect.getsource(self.test_syntax).splitlines(), syntax=TextEditor.Syntax.PYTHON) for i in range(2): await omni.kit.app.get_app().next_update_async() await self.finalize_test(golden_img_dir=GOLDEN_PATH, golden_img_name=f"test_syntax.png") async def test_text_changed_flag(self): """Testing TextEditor text edited callback""" from omni.kit import ui_test window = await self.create_test_window(block_devices=False) window.focus() text = "Lorem ipsum" text_new = "dolor sit amet" with window.frame: text_editor = TextEditor(text=text) await ui_test.wait_n_updates(2) self.text_changed = False def on_text_changed(text_changed): self.text_changed = text_changed text_editor.set_edited_fn(on_text_changed) self.assertFalse(self.text_changed) await ui_test.emulate_mouse_move_and_click(ui_test.Vec2(100, 100)) await ui_test.wait_n_updates(2) await ui_test.emulate_char_press("A") await ui_test.wait_n_updates(2) self.assertTrue(self.text_changed) await ui_test.emulate_key_combo("BACKSPACE") await ui_test.wait_n_updates(2) self.assertFalse(self.text_changed) text_editor.text = text_new await ui_test.wait_n_updates(2) # Only user input will trigger the callback self.assertFalse(self.text_changed) await ui_test.emulate_char_press("A") await ui_test.wait_n_updates(2) self.assertTrue(self.text_changed) text_editor.set_edited_fn(None)
3,199
Python
31.98969
116
0.666458
omniverse-code/kit/exts/omni.kit.widget.text_editor/docs/CHANGELOG.md
# Changelog ## [1.0.2] - 2022-07-28 ### Added - Callback passing a bool that user has edited the text ## [1.0.1] - 2022-06-29 ### Added - Syntax highlighting ## [1.0.0] - 2022-04-30 ### Added - Initial commit
212
Markdown
14.214285
55
0.622642
omniverse-code/kit/exts/omni.kit.widget.text_editor/docs/README.md
# omni.kit.widget.text_editor ## Overview It's binding of ImGuiColorTextEdit in omni.ui, syntax highlighting text editor. It approximates typical code editor look and feel. ![](../data/preview.png) ## Fonts TextEditor widget supports fonts from style ``` import omni.ui as ui from omni.kit.widget.text_editor import TextEditor font = "c:/windows/fonts/consola.ttf" my_window = ui.Window("Example", width=600, height=300) with my_window.frame: TextEditor( text="The quick brown fox", style={"font": font}) ``` ## Syntax Highlighting TextEditor widget supports many languages to highlight the syntax. This is the list of the languages: ``` TextEditor.Syntax.NONE TextEditor.Syntax.PYTHON TextEditor.Syntax.CPLUSPLUS TextEditor.Syntax.HLSL TextEditor.Syntax.GLSL TextEditor.Syntax.C TextEditor.Syntax.SQL TextEditor.Syntax.ANGELSCRIPT TextEditor.Syntax.LUA ``` ``` import omni.ui as ui from omni.kit.widget.text_editor import TextEditor font = "c:/windows/fonts/consola.ttf" my_window = ui.Window("Example", width=600, height=300) with my_window.frame: TextEditor( text=open(__file__).read(), style={"font": font}, syntax=TextEditor.Syntax.PYTHON) ```
1,212
Markdown
19.913793
79
0.728548
omniverse-code/kit/exts/omni.kit.widget.text_editor/docs/index.rst
omni.kit.widget.text_editor ########################### .. toctree:: :maxdepth: 1 CHANGELOG
99
reStructuredText
13.285712
27
0.484848
omniverse-code/kit/exts/omni.timeline/omni/timeline/__init__.py
from ._timeline import * def get_timeline_interface(timeline_name: str='') -> Timeline: """Returns the timeline with the given name via cached :class:`omni.timeline.ITimeline` interface""" if not hasattr(get_timeline_interface, "timeline"): get_timeline_interface.timeline = acquire_timeline_interface() return get_timeline_interface.timeline.get_timeline(timeline_name) def destroy_timeline(timeline_name: str): """Destroys a timeline object with the given name, if it is not the default timeline and it is not in use.""" if not hasattr(get_timeline_interface, "timeline"): get_timeline_interface.timeline = acquire_timeline_interface() return get_timeline_interface.timeline.destroy_timeline(timeline_name)
754
Python
43.411762
113
0.740053
omniverse-code/kit/exts/omni.timeline/omni/timeline/tests/tests.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 carb.events import omni.kit.app import omni.kit.test import omni.timeline import carb.settings import queue import asyncio from time import sleep USE_FIXED_TIMESTEP_PATH = "/app/player/useFixedTimeStepping" RUNLOOP_RATE_LIMIT_PATH = "/app/runLoops/main/rateLimitFrequency" COMPENSATE_PLAY_DELAY_PATH = "/app/player/CompensatePlayDelayInSecs" class TestTimeline(omni.kit.test.AsyncTestCase): async def setUp(self): self._app = omni.kit.app.get_app() self._timeline = omni.timeline.get_timeline_interface() self._timeline_sub = self._timeline.get_timeline_event_stream().create_subscription_to_pop( self._on_timeline_event ) self._buffered_evts = queue.Queue() self._settings = carb.settings.acquire_settings_interface() async def tearDown(self): self._timeline = None self._timeline_sub = None async def test_timeline_api(self): ## Check initial states self.assertFalse(self._timeline.is_playing()) self.assertTrue(self._timeline.is_stopped()) self.assertEqual(0.0, self._timeline.get_start_time()) self.assertEqual(0.0, self._timeline.get_end_time()) self.assertEqual(0.0, self._timeline.get_current_time()) self.assertEqual(0.0, self._timeline.get_time_codes_per_seconds()) self.assertTrue(self._timeline.is_looping()) self.assertTrue(self._timeline.is_auto_updating()) self.assertFalse(self._timeline.is_prerolling()) self.assertFalse(self._timeline.is_zoomed()) ## Change start time start_time = -1.0 self._timeline.set_start_time(start_time) self._assert_no_change_then_commit(self._timeline.get_start_time(), 0) self._verify_evt(omni.timeline.TimelineEventType.START_TIME_CHANGED, "startTime", start_time) self.assertTrue(self._buffered_evts.empty()) self.assertEqual(start_time, self._timeline.get_start_time()) self.assertEqual(0.0, self._timeline.get_current_time()) ## Change end time end_time = 2.0 self._timeline.set_end_time(end_time) self._assert_no_change_then_commit(self._timeline.get_end_time(), 0) self._verify_evt(omni.timeline.TimelineEventType.END_TIME_CHANGED, "endTime", end_time) self.assertTrue(self._buffered_evts.empty()) self.assertEqual(end_time, self._timeline.get_end_time()) self.assertEqual(0.0, self._timeline.get_current_time()) ## Change start time to current_time < start_time < end_time # OM-75796: changing start time does not move current time when the timeline is not playing old_start_time = start_time start_time = 1.0 self._timeline.set_start_time(start_time) self._assert_no_change_then_commit(self._timeline.get_start_time(), old_start_time) self._verify_evt(omni.timeline.TimelineEventType.START_TIME_CHANGED, "startTime", start_time) self.assertTrue(self._buffered_evts.empty()) self.assertEqual(start_time, self._timeline.get_start_time()) self.assertEqual(0.0, self._timeline.get_current_time()) ## Change timecode per second time_code_per_sec = 24.0 self._timeline.set_time_codes_per_second(time_code_per_sec) self._assert_no_change_then_commit(self._timeline.get_time_codes_per_seconds(), 0) self._verify_evt( omni.timeline.TimelineEventType.TIME_CODE_PER_SECOND_CHANGED, "timeCodesPerSecond", time_code_per_sec ) self.assertEqual(time_code_per_sec, self._timeline.get_time_codes_per_seconds()) ## Do not allow endtime <= starttime frame_time = 1.0 / time_code_per_sec new_start_time = end_time self._timeline.set_start_time(new_start_time) self._assert_no_change_then_commit(self._timeline.get_start_time(), start_time) self._verify_evt(omni.timeline.TimelineEventType.START_TIME_CHANGED, "startTime", new_start_time) self._verify_evt( omni.timeline.TimelineEventType.END_TIME_CHANGED, "endTime", new_start_time + frame_time, exact=False ) self.assertTrue(self._buffered_evts.empty()) new_start_time = new_start_time + 10.0 * frame_time self._timeline.set_start_time(new_start_time) self._timeline.commit() self._verify_evt(omni.timeline.TimelineEventType.START_TIME_CHANGED, "startTime", new_start_time) self._verify_evt( omni.timeline.TimelineEventType.END_TIME_CHANGED, "endTime", new_start_time + frame_time, exact=False ) self.assertTrue(self._buffered_evts.empty()) new_end_time = self._timeline.get_start_time() self._timeline.set_end_time(new_end_time) self._timeline.commit() self._verify_evt(omni.timeline.TimelineEventType.END_TIME_CHANGED, "endTime", new_end_time) self._verify_evt( omni.timeline.TimelineEventType.START_TIME_CHANGED, "startTime", new_end_time - frame_time, exact=False ) self.assertTrue(self._buffered_evts.empty()) new_end_time = new_end_time - 10.0 * frame_time self._timeline.set_end_time(new_end_time) self._timeline.commit() self._verify_evt(omni.timeline.TimelineEventType.END_TIME_CHANGED, "endTime", new_end_time) self._verify_evt( omni.timeline.TimelineEventType.START_TIME_CHANGED, "startTime", new_end_time - frame_time, exact=False ) self.assertTrue(self._buffered_evts.empty()) # Revert start_time = 1.0 self._timeline.set_start_time(start_time) self._timeline.set_end_time(end_time) self._timeline.set_current_time(start_time) self._timeline.commit() self._clear_evt_queue() # Don't care ## Time conversion self.assertAlmostEqual(self._timeline.time_to_time_code(-1), -24, places=5) self.assertAlmostEqual(self._timeline.time_to_time_code(0), 0, places=5) self.assertAlmostEqual(self._timeline.time_to_time_code(0.5), 12, places=5) self.assertAlmostEqual(self._timeline.time_to_time_code(2), 48, places=5) self.assertAlmostEqual(self._timeline.time_code_to_time(-24), -1, places=5) self.assertAlmostEqual(self._timeline.time_code_to_time(0), 0, places=5) self.assertAlmostEqual(self._timeline.time_code_to_time(12), 0.5, places=5) self.assertAlmostEqual(self._timeline.time_code_to_time(48), 2, places=5) self.assertTrue(self._buffered_evts.empty()) ## Set current time old_current_time = self._timeline.get_current_time() new_current_time = start_time self._timeline.set_current_time(new_current_time) self._assert_no_change_then_commit(self._timeline.get_current_time(), old_current_time) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED, "currentTime", new_current_time) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED_PERMANENT, "currentTime", new_current_time) self.assertEqual(new_current_time, self._timeline.get_current_time()) # dt is smaller than 1 frame old_current_time = self._timeline.get_current_time() expected_dt = 0.5 * frame_time new_current_time = start_time + expected_dt self._timeline.set_current_time(new_current_time) self._assert_no_change_then_commit(self._timeline.get_current_time(), old_current_time) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED, "dt", expected_dt, False) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED_PERMANENT, "dt", expected_dt, False) self.assertEqual(new_current_time, self._timeline.get_current_time()) # Edge case: dt is exactly one frame old_current_time = self._timeline.get_current_time() expected_dt = 1.0 * frame_time new_current_time = new_current_time + expected_dt self._timeline.set_current_time(new_current_time) self._assert_no_change_then_commit(self._timeline.get_current_time(), old_current_time) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED, "dt", expected_dt, False) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED_PERMANENT, "dt", expected_dt, False) self.assertEqual(new_current_time, self._timeline.get_current_time()) # If dt would be too large to simulate, it is set to zero old_current_time = self._timeline.get_current_time() expected_dt = 0 new_current_time = new_current_time + 1.5 * frame_time self._timeline.set_current_time(new_current_time) self._assert_no_change_then_commit(self._timeline.get_current_time(), old_current_time) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED, "dt", expected_dt, False) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED_PERMANENT, "dt", expected_dt, False) self.assertEqual(new_current_time, self._timeline.get_current_time()) # If dt would be negative, it is set to zero old_current_time = self._timeline.get_current_time() expected_dt = 0 new_current_time = start_time self._timeline.set_current_time(new_current_time) self._assert_no_change_then_commit(self._timeline.get_current_time(), old_current_time) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED, "dt", expected_dt, False) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED_PERMANENT, "dt", expected_dt, False) self.assertEqual(new_current_time, self._timeline.get_current_time()) ## Forward one frame old_current_time = self._timeline.get_current_time() self._timeline.forward_one_frame() self._assert_no_change_then_commit(self._timeline.get_current_time(), old_current_time) # Forward one frame triggers a play and pause if the timeline was not playing self._verify_evt(omni.timeline.TimelineEventType.PLAY) self._verify_evt( omni.timeline.TimelineEventType.CURRENT_TIME_TICKED, "currentTime", start_time + 1.0 / time_code_per_sec ) self._verify_evt( omni.timeline.TimelineEventType.CURRENT_TIME_TICKED_PERMANENT, "currentTime", start_time + 1.0 / time_code_per_sec ) self._verify_evt(omni.timeline.TimelineEventType.PAUSE) self.assertAlmostEqual(start_time + 1.0 / time_code_per_sec, self._timeline.get_current_time(), places=5) ## Rewind one frame old_current_time = self._timeline.get_current_time() self._timeline.rewind_one_frame() self._assert_no_change_then_commit(self._timeline.get_current_time(), old_current_time) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED, "currentTime", start_time) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED_PERMANENT, "currentTime", start_time) self.assertAlmostEqual(start_time, self._timeline.get_current_time(), places=5) new_current_time = self._timeline.get_current_time() ## Set target framerate. DEFAULT_TARGET_FRAMERATE = 60 self.assertEqual(self._timeline.get_target_framerate(), DEFAULT_TARGET_FRAMERATE) target_vs_set_fps = [ [2 * time_code_per_sec - 1, 2 * time_code_per_sec], [3 * time_code_per_sec + 15, 4 * time_code_per_sec], [5 * time_code_per_sec - 7, 5 * time_code_per_sec], [time_code_per_sec, time_code_per_sec] # this comes last to avoid frame skipping in Play tests ] self._timeline.play() self._timeline.commit() self._clear_evt_queue() # don't care for fps in target_vs_set_fps: target_fps = fps[0] desired_runloop_fps = fps[1] old_fps = self._timeline.get_target_framerate() self._timeline.set_target_framerate(target_fps) self._assert_no_change_then_commit(self._timeline.get_target_framerate(), old_fps) self._verify_evt( omni.timeline.TimelineEventType.TARGET_FRAMERATE_CHANGED, "targetFrameRate", target_fps ) self.assertEqual(target_fps, self._timeline.get_target_framerate()) self.assertEqual(self._settings.get(RUNLOOP_RATE_LIMIT_PATH), desired_runloop_fps) self._timeline.stop() self._timeline.commit() self._clear_evt_queue() # don't care ## Play self._timeline.play() self._assert_no_change_then_commit(self._timeline.is_playing(), False) self._verify_evt(omni.timeline.TimelineEventType.PLAY) self.assertTrue(self._settings.get(USE_FIXED_TIMESTEP_PATH)) self.assertTrue(self._timeline.is_playing()) self.assertFalse(self._timeline.is_stopped()) await self._app.next_update_async() dt = 1.0 / time_code_per_sec # timeline uses fixed dt by default self.assertAlmostEqual(new_current_time + dt, self._timeline.get_current_time(), places=5) self._verify_evt( omni.timeline.TimelineEventType.CURRENT_TIME_TICKED, "currentTime", new_current_time + dt, exact=False ) self._verify_evt( omni.timeline.TimelineEventType.CURRENT_TIME_TICKED_PERMANENT, "dt", dt, exact=False ) # varying dt self._timeline.stop() self._settings.set(USE_FIXED_TIMESTEP_PATH, False) self._timeline.play() self._timeline.commit() self._clear_evt_queue() new_current_time = self._timeline.get_current_time() dt = await self._app.next_update_async() self.assertAlmostEqual(new_current_time + dt, self._timeline.get_current_time(), places=5) self._verify_evt( omni.timeline.TimelineEventType.CURRENT_TIME_TICKED, "currentTime", new_current_time + dt, exact=False ) self._verify_evt( omni.timeline.TimelineEventType.CURRENT_TIME_TICKED_PERMANENT, "dt", dt, exact=False ) self._settings.set(USE_FIXED_TIMESTEP_PATH, True) ## Pause self._timeline.pause() self._assert_no_change_then_commit(self._timeline.is_playing(), True) self._verify_evt(omni.timeline.TimelineEventType.PAUSE) self.assertFalse(self._timeline.is_playing()) self.assertFalse(self._timeline.is_stopped()) # current time should not change await self._app.next_update_async() self.assertAlmostEqual(new_current_time + dt, self._timeline.get_current_time(), places=5) # permanent update event is still ticking self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED_PERMANENT) ## Stop self._timeline.stop() self._assert_no_change_then_commit(self._timeline.is_stopped(), False) self._verify_evt(omni.timeline.TimelineEventType.STOP) self.assertFalse(self._timeline.is_playing()) self.assertTrue(self._timeline.is_stopped()) self.assertEqual(start_time, self._timeline.get_current_time()) ## Loop # self._timeline.set_looping(True) # it was already set self._timeline.play() self._timeline.commit() dt = 1.0 / time_code_per_sec # timeline uses fixed dt by default elapsed_time = 0.0 while elapsed_time < end_time * 1.5: await self._app.next_update_async() elapsed_time += dt self._timeline.pause() self._timeline.commit() self._clear_evt_queue() # don't care # time is looped loopped_time = elapsed_time % (end_time - start_time) + start_time self.assertAlmostEqual(loopped_time, self._timeline.get_current_time(), places=5) ## Non-loop self._timeline.set_looping(False) self._assert_no_change_then_commit(self._timeline.is_looping(), True) self._verify_evt(omni.timeline.TimelineEventType.LOOP_MODE_CHANGED, "looping", False) self._timeline.stop() self._timeline.play() self._timeline.commit() elapsed_time = 0.0 while elapsed_time < end_time * 1.5: dt = await self._app.next_update_async() elapsed_time += dt # timeline paused when reached the end self.assertFalse(self._timeline.is_playing()) self.assertFalse(self._timeline.is_stopped()) self.assertAlmostEqual(end_time, self._timeline.get_current_time(), places=5) ## Change end time that should change current time because current time was > end time self._clear_evt_queue() end_time = 1.5 current_time = self._timeline.get_current_time() self._timeline.set_end_time(end_time) self._timeline.commit() self._verify_evt(omni.timeline.TimelineEventType.END_TIME_CHANGED, "endTime", end_time) # OM-75796: changing the end time does not move current time when timeline is not playing self.assertTrue(self._buffered_evts.empty()) self.assertEqual(end_time, self._timeline.get_end_time()) self.assertEqual(current_time, self._timeline.get_current_time()) self._timeline.stop() # OM-75796: changing the end time moves current time to start when timeline is playing self._timeline.play() self._timeline.set_current_time(end_time) # someting after the new end time self._timeline.commit() self._clear_evt_queue() # don't care end_time = 1.25 self._timeline.set_end_time(end_time) self._timeline.commit() self._verify_evt(omni.timeline.TimelineEventType.END_TIME_CHANGED, "endTime", end_time) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED, "currentTime", start_time) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED_PERMANENT, "currentTime", start_time) self.assertTrue(self._buffered_evts.empty()) self.assertEqual(end_time, self._timeline.get_end_time()) self.assertEqual(start_time, self._timeline.get_current_time()) # OM-75796: changing the start time moves current time to start when timeline is playing end_time = 100 self._timeline.set_end_time(end_time) self._timeline.commit() self._clear_evt_queue() # don't care start_time = 2.0 self._timeline.set_start_time(start_time) self._timeline.commit() self._verify_evt(omni.timeline.TimelineEventType.START_TIME_CHANGED, "startTime", start_time) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED, "currentTime", start_time) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED_PERMANENT, "currentTime", start_time) self.assertTrue(self._buffered_evts.empty()) self.assertEqual(start_time, self._timeline.get_start_time()) self.assertEqual(start_time, self._timeline.get_current_time()) self._timeline.stop() self._timeline.commit() ## Auto update self._clear_evt_queue() self._timeline.set_auto_update(False) self._assert_no_change_then_commit(self._timeline.is_auto_updating(), True) self._verify_evt(omni.timeline.TimelineEventType.AUTO_UPDATE_CHANGED, "autoUpdate", False) self.assertFalse(self._timeline.is_auto_updating()) self._timeline.set_looping(True) self._timeline.play() for i in range(5): await self._app.next_update_async() self.assertEqual(start_time, self._timeline.get_current_time()) ## Prerolling self._clear_evt_queue() self._timeline.set_prerolling(True) self._assert_no_change_then_commit(self._timeline.is_prerolling(), False) self.assertTrue(self._timeline.is_prerolling()) self._verify_evt(omni.timeline.TimelineEventType.PREROLLING_CHANGED, "prerolling", True) ## Change TimeCodesPerSeconds and verify if CurrentTime is properly refitted. time_codes_per_second = 24 timecode = 50 self._timeline.set_time_codes_per_second(time_codes_per_second) self._timeline.set_start_time(0) self._timeline.set_end_time(100) self._timeline.set_current_time(timecode) self._timeline.set_time_codes_per_second(100) self._timeline.commit() # we stay the same timecode after change TimeCodesPerSeconds self.assertEqual(timecode, self._timeline.get_current_time()) ## Play in range self._timeline.stop() self._timeline.set_auto_update(True) start_time_seconds = 0 range_start_timecode = 20 range_end_timecode = 30 end_time_seconds = 40 self._timeline.set_start_time(start_time_seconds) self._timeline.set_end_time(end_time_seconds) self._timeline.commit() self._clear_evt_queue() self._timeline.play(range_start_timecode, range_end_timecode, False) self._assert_no_change_then_commit(True, True) self._verify_evt(omni.timeline.TimelineEventType.PLAY) self.assertTrue(self._timeline.is_playing()) self.assertFalse(self._timeline.is_stopped()) await asyncio.sleep(5) self.assertEqual(start_time_seconds, self._timeline.get_start_time()) self.assertEqual(end_time_seconds, self._timeline.get_end_time()) self.assertEqual(range_end_timecode, self._timeline.get_current_time() * self._timeline.get_time_codes_per_seconds()) ## tentative time current_time = 2.0 tentative_time = 2.5 self._timeline.set_current_time(current_time) self._timeline.commit() self._clear_evt_queue() self._timeline.set_tentative_time(tentative_time) self._assert_no_change_then_commit(self._timeline.get_tentative_time(), current_time) self._verify_evt(omni.timeline.TimelineEventType.TENTATIVE_TIME_CHANGED) self.assertEqual(tentative_time, self._timeline.get_tentative_time()) self.assertEqual(current_time, self._timeline.get_current_time()) self._timeline.clear_tentative_time() self._assert_no_change_then_commit(self._timeline.get_tentative_time(), tentative_time) self.assertEqual(self._timeline.get_tentative_time(), self._timeline.get_current_time()) ## tentative time and events tentative_time = 3.5 self._timeline.set_tentative_time(tentative_time) self._timeline.commit() self._clear_evt_queue() self._timeline.forward_one_frame() self._assert_no_change_then_commit(True, True) self._verify_evt(omni.timeline.TimelineEventType.PLAY) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED_PERMANENT) self._verify_evt(omni.timeline.TimelineEventType.PAUSE) self.assertTrue(self._buffered_evts.empty()) # tentative time was used, no other event self._timeline.set_tentative_time(tentative_time) self._timeline.commit() self._clear_evt_queue() self._timeline.rewind_one_frame() self._assert_no_change_then_commit(True, True) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED_PERMANENT) self.assertTrue(self._buffered_evts.empty()) # tentative time was used, no other event ## Forward one frame while the timeline is playing: only time is ticked, no play/pause self._timeline.play() self._timeline.commit() self._clear_evt_queue() # don't care old_current_time = self._timeline.get_current_time() tcps_current = self._timeline.get_time_codes_per_seconds() self._timeline.forward_one_frame() self._assert_no_change_then_commit(self._timeline.get_current_time(), old_current_time) self._verify_evt( omni.timeline.TimelineEventType.CURRENT_TIME_TICKED, "currentTime", old_current_time + 1.0 / tcps_current ) self._verify_evt( omni.timeline.TimelineEventType.CURRENT_TIME_TICKED_PERMANENT, "currentTime", old_current_time + 1.0 / tcps_current ) self.assertTrue(self._buffered_evts.empty()) self.assertAlmostEqual(old_current_time + 1.0 / tcps_current, self._timeline.get_current_time(), places=5) self._timeline.stop() self._timeline.commit() self._clear_evt_queue() # don't care ## substepping subsample_rate = 3 old_subsample_rate = self._timeline.get_ticks_per_frame() self._timeline.set_ticks_per_frame(subsample_rate) self._assert_no_change_then_commit(self._timeline.get_ticks_per_frame(), old_subsample_rate) self._verify_evt( omni.timeline.TimelineEventType.TICKS_PER_FRAME_CHANGED, "ticksPerFrame", subsample_rate, exact=True ) self.assertEqual(self._timeline.get_ticks_per_frame(), subsample_rate) self.assertEqual(self._timeline.get_ticks_per_second(), subsample_rate * self._timeline.get_time_codes_per_seconds()) self._timeline.play() self._timeline.commit() self._verify_evt(omni.timeline.TimelineEventType.PLAY) await self._app.next_update_async() for i in range(subsample_rate): self._verify_evt( omni.timeline.TimelineEventType.CURRENT_TIME_TICKED, "tick", i, exact=True ) self._verify_evt( omni.timeline.TimelineEventType.CURRENT_TIME_TICKED_PERMANENT, "tick", i, exact=True ) tick_dt = 1.0 / (self._timeline.get_time_codes_per_seconds() * subsample_rate) await self._app.next_update_async() for i in range(subsample_rate): self._verify_evt( omni.timeline.TimelineEventType.CURRENT_TIME_TICKED, "dt", tick_dt, exact=False ) self._verify_evt( omni.timeline.TimelineEventType.CURRENT_TIME_TICKED_PERMANENT, "dt", tick_dt, exact=False ) start_time = self._timeline.get_current_time() await self._app.next_update_async() for i in range(subsample_rate): self._verify_evt( omni.timeline.TimelineEventType.CURRENT_TIME_TICKED, "currentTime", start_time + (i + 1) * tick_dt, exact=False ) self._verify_evt( omni.timeline.TimelineEventType.CURRENT_TIME_TICKED_PERMANENT, "currentTime", start_time + (i + 1) * tick_dt, exact=False ) self._timeline.stop() self._timeline.commit() ## substepping with event handling and event firing in the kit runloop self._tick = True def pause(e: carb.events.IEvent): if e.type == int(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED): if self._tick: self._timeline.pause() else: self._timeline.play() self._tick = not self._tick pause_sub = self._timeline.get_timeline_event_stream().create_subscription_to_pop(pause, order=1) start_time = self._timeline.get_current_time() self._timeline.play() self._timeline.commit() self._clear_evt_queue() # callbacks call stop and pause but we still finish the frame await self._app.post_update_async() for i in range(subsample_rate): self._verify_evt( omni.timeline.TimelineEventType.CURRENT_TIME_TICKED, "currentTime", start_time + (i + 1) * tick_dt, exact=False ) self._verify_evt( omni.timeline.TimelineEventType.CURRENT_TIME_TICKED_PERMANENT, "currentTime", start_time + (i + 1) * tick_dt, exact=False ) # No events until the next frame self.assertTrue(self._buffered_evts.empty()) # wait a until the next update, events are then re-played await self._app.next_update_async() self._tick = True for i in range(subsample_rate): if self._tick: self._verify_evt(omni.timeline.TimelineEventType.PAUSE) else: self._verify_evt(omni.timeline.TimelineEventType.PLAY) self._tick = not self._tick self.assertEqual(self._timeline.is_playing(), self._tick) pause_sub = None # stop for the next test self._timeline.stop() self._timeline.commit() self._clear_evt_queue() ## frame skipping self.assertFalse(self._timeline.get_play_every_frame()) # off by default self.assertAlmostEqual(self._settings.get(COMPENSATE_PLAY_DELAY_PATH), 0.0) # No compensation by default self._timeline.set_time_codes_per_second(time_codes_per_second) self._timeline.set_ticks_per_frame(1) self._timeline.set_target_framerate(3.0 * time_code_per_sec) self._timeline.play() self._timeline.commit() self._clear_evt_queue() await self._app.next_update_async() # events are fired on the first call self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED_PERMANENT) # 2 frames are skipped await self._app.next_update_async() self.assertTrue(self._buffered_evts.empty()) await self._app.next_update_async() self.assertTrue(self._buffered_evts.empty()) # firing events again await self._app.next_update_async() self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED_PERMANENT) self._timeline.stop() self._timeline.commit() self._clear_evt_queue() # don't care # fast mode, target frame rate is still 3 * time_code_per_sec, but frames should not be skipped self._timeline.set_play_every_frame(True) self._assert_no_change_then_commit(self._timeline.get_play_every_frame(), False) self._verify_evt( omni.timeline.TimelineEventType.PLAY_EVERY_FRAME_CHANGED, "playEveryFrame", True, exact=True ) self._timeline.play() self._timeline.commit() self._verify_evt(omni.timeline.TimelineEventType.PLAY) await self._app.next_update_async() for i in range(5): await self._app.next_update_async() # events are fired for every call self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED_PERMANENT) self._timeline.set_play_every_frame(False) self._timeline.commit() self._clear_evt_queue() # don't care ## test multiple pipelines self._timeline.stop() self._timeline.commit() default_timeline_current_time = self._timeline.get_current_time() new_timeline_name = "new_timeline" self._new_timeline = omni.timeline.get_timeline_interface(new_timeline_name) self._new_timeline.set_current_time(default_timeline_current_time + 1.0) self._new_timeline.set_end_time(default_timeline_current_time + 10.0) self._new_timeline.commit() new_timeline_current_time = self._new_timeline.get_current_time() self.assertNotEqual(new_timeline_current_time, self._timeline.get_current_time()) self._new_timeline.play() self._new_timeline.commit() self.assertFalse(self._timeline.is_playing()) self.assertTrue(self._new_timeline.is_playing()) for i in range(10): await self._app.next_update_async() self.assertNotEqual(new_timeline_current_time, self._new_timeline.get_current_time()) self.assertEqual(default_timeline_current_time, self._timeline.get_current_time()) # OM-76359: test that cleaning works self._new_timeline = None self._dummy_timeline = omni.timeline.get_timeline_interface('dummy') self._new_timeline = omni.timeline.get_timeline_interface(new_timeline_name) self.assertAlmostEqual(0.0, self._new_timeline.get_current_time(), places=5) omni.timeline.destroy_timeline(new_timeline_name) omni.timeline.destroy_timeline('dummy') ## commit and its silent version self._clear_evt_queue() self._timeline.set_target_framerate(17) self._timeline.set_time_codes_per_second(1) self._timeline.set_ticks_per_frame(19) self._timeline.play() self._timeline.stop() self._timeline.set_current_time(15) self._timeline.rewind_one_frame() self._timeline.commit_silently() self.assertTrue(self._buffered_evts.empty()) self.assertAlmostEqual(self._timeline.get_target_framerate(), 17) self.assertAlmostEqual(self._timeline.get_time_codes_per_seconds(), 1) self.assertAlmostEqual(self._timeline.get_ticks_per_frame(), 19) self.assertFalse(self._timeline.is_playing()) self.assertAlmostEqual(self._timeline.get_current_time(), 14) # Revert self._timeline.set_ticks_per_frame(1) await self._app.next_update_async() self._clear_evt_queue() self._timeline.set_start_time(1) self._timeline.set_end_time(10) self._timeline.set_current_time(5) self._timeline.play() self._timeline.forward_one_frame() self._timeline.rewind_one_frame() self.assertAlmostEqual(self._timeline.get_current_time(), 14) self._timeline.commit() self._verify_evt(omni.timeline.TimelineEventType.START_TIME_CHANGED, 'startTime', 1) self._verify_evt(omni.timeline.TimelineEventType.END_TIME_CHANGED, 'endTime', 10) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED, 'currentTime', 5) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED_PERMANENT, 'currentTime', 5) self._verify_evt(omni.timeline.TimelineEventType.PLAY) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED, 'currentTime', 6) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED_PERMANENT, 'currentTime', 6) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED, 'currentTime', 5) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED_PERMANENT, 'currentTime', 5) self.assertTrue(self._buffered_evts.empty()) self.assertAlmostEqual(self._timeline.get_current_time(), 5) self.assertTrue(self._timeline.is_playing()) self._timeline.stop() self._timeline.commit() self._clear_evt_queue() # calling manually to make sure they come after the API test # independently of the test env await self._test_runloop_integration() await self._test_director() async def _test_runloop_integration(self): """ Test how the run loop behaves when it is controlled by the timeline and test proper behavior when runloop slows down """ # make sure other tests do not interfere TIMELINE_FPS = 25.0 RUNLOOP_FPS = TIMELINE_FPS * 4.0 # 100, 1/4 frame skipping self._timeline.stop() self._timeline.set_current_time(0.0) self._timeline.set_start_time(0.0) self._timeline.set_end_time(1000.0) self._timeline.set_target_framerate(RUNLOOP_FPS) self._timeline.set_time_codes_per_second(TIMELINE_FPS) self._timeline.set_play_every_frame(False) self._timeline.set_ticks_per_frame(1) self._timeline.play() self._timeline.commit() # Run loop is expected to run with 100 FPS, nothing should slow it down. # Timeline relies on this so we test this here. self.assertEqual(self._settings.get(RUNLOOP_RATE_LIMIT_PATH), RUNLOOP_FPS) FPS_TOLERANCE_PERCENT = 5 runloop_dt_min = (1.0 / RUNLOOP_FPS) * (1 - FPS_TOLERANCE_PERCENT * 0.01) runloop_dt_max = (1.0 / RUNLOOP_FPS) * (1 + FPS_TOLERANCE_PERCENT * 0.01) update_sub = self._app.get_update_event_stream().create_subscription_to_pop( self._save_runloop_dt, name="[TimelineTest save dt]" ) for i in range(3): await self._app.next_update_async() # warm up, "consume" old FPS for i in range(5): await self._app.next_update_async() self.assertTrue(self._runloop_dt >= runloop_dt_min, "Run loop dt is too far from expected: {} vs {}" .format(self._runloop_dt, (1.0 / RUNLOOP_FPS))) self.assertTrue(self._runloop_dt <= runloop_dt_max, "Run loop dt is too far from expected: {} vs {}" .format(self._runloop_dt, (1.0 / RUNLOOP_FPS))) # Timeline wants to keep up with real time if run loop gets too slow (no frame skipping) self._settings.set(COMPENSATE_PLAY_DELAY_PATH, 1000.0) # set something high, e.g. 1000s pre_update_sub = self._app.get_pre_update_event_stream().create_subscription_to_pop( self._sleep, name="[TimelineTest sleep]" ) self._timeline.stop() self._timeline.play() self._timeline.commit() self._clear_evt_queue() await self._app.next_update_async() for i in range(5): await self._app.next_update_async() self.assertTrue(self._runloop_dt > 0.99) # sleep # run loop is slow, no frame skipping even though fast mode is off self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED) self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED_PERMANENT) self._timeline.stop() self._timeline.commit() self._clear_evt_queue() async def _test_director(self): self._timeline.stop() self._timeline.set_current_time(0) self._timeline.set_end_time(10) self._timeline.set_start_time(0) self._timeline.set_time_codes_per_second(24) self._timeline.set_looping(False) self._timeline.set_prerolling(True) self._timeline.set_auto_update(False) self._timeline.set_play_every_frame(True) self._timeline.set_ticks_per_frame(2) self._timeline.set_target_framerate(24) self._timeline.commit() self._clear_evt_queue() self.assertIsNone(self._timeline.get_director()) self._director_timeline = omni.timeline.get_timeline_interface('director') # Make sure they have the same parameters self._director_timeline.stop() self._director_timeline.set_current_time(self._timeline.get_current_time()) self._director_timeline.set_end_time(self._timeline.get_end_time()) self._director_timeline.set_start_time(self._timeline.get_start_time()) self._director_timeline.set_time_codes_per_second(self._timeline.get_time_codes_per_seconds()) self._director_timeline.set_looping(self._timeline.is_looping()) self._director_timeline.set_prerolling(self._timeline.is_prerolling()) self._director_timeline.set_auto_update(self._timeline.is_auto_updating()) self._director_timeline.set_play_every_frame(self._timeline.get_play_every_frame()) self._director_timeline.set_ticks_per_frame(self._timeline.get_ticks_per_frame()) self._director_timeline.set_target_framerate(self._timeline.get_target_framerate()) self._director_timeline.commit() self._timeline.set_director(self._director_timeline) self._timeline.commit() self._verify_evt(omni.timeline.TimelineEventType.DIRECTOR_CHANGED, 'directorName', 'director') self._director_timeline.play() self._director_timeline.commit() self._timeline.commit() await self._app.next_update_async() self.assertTrue(self._timeline.is_playing()) self._director_timeline.pause() self._director_timeline.commit() self._timeline.commit() await self._app.next_update_async() self.assertFalse(self._timeline.is_playing()) self._director_timeline.stop() self._director_timeline.commit() self._timeline.commit() await self._app.next_update_async() self.assertTrue(self._timeline.is_stopped()) self._director_timeline.set_current_time(2) self._director_timeline.commit() self._timeline.commit() await self._app.next_update_async() self.assertAlmostEqual(self._timeline.get_current_time(), 2, places=4) self._director_timeline.set_end_time(5) self._director_timeline.commit() self._timeline.commit() await self._app.next_update_async() self.assertAlmostEqual(self._timeline.get_end_time(), 5, places=4) self._director_timeline.set_start_time(1) self._director_timeline.commit() self._timeline.commit() await self._app.next_update_async() self.assertAlmostEqual(self._timeline.get_start_time(), 1, places=4) self._director_timeline.set_time_codes_per_second(30) self._director_timeline.commit() self._timeline.commit() await self._app.next_update_async() self.assertAlmostEqual(self._timeline.get_time_codes_per_seconds(), 30, places=4) self._director_timeline.set_looping(True) self._director_timeline.commit() self._timeline.commit() await self._app.next_update_async() self.assertTrue(self._timeline.is_looping()) self._director_timeline.set_prerolling(False) self._director_timeline.commit() self._timeline.commit() await self._app.next_update_async() self.assertFalse(self._timeline.is_prerolling()) self._director_timeline.set_auto_update(True) self._director_timeline.commit() self._timeline.commit() await self._app.next_update_async() self.assertTrue(self._timeline.is_auto_updating()) self._director_timeline.set_play_every_frame(False) self._director_timeline.commit() self._timeline.commit() await self._app.next_update_async() self.assertFalse(self._timeline.get_play_every_frame()) self._director_timeline.set_ticks_per_frame(1) self._director_timeline.commit() self._timeline.commit() await self._app.next_update_async() self.assertEqual(self._timeline.get_ticks_per_frame(), 1) self._director_timeline.set_target_framerate(30) self._director_timeline.commit() self._timeline.commit() await self._app.next_update_async() self.assertAlmostEqual(self._timeline.get_target_framerate(), 30, places=4) self.assertFalse(self._timeline.is_zoomed()) zoom_start = self._timeline.get_start_time() + 1 zoom_end = self._timeline.get_end_time() - 1 self._director_timeline.set_zoom_range(zoom_start, zoom_end) self._director_timeline.commit() self._timeline.commit() await self._app.next_update_async() self.assertAlmostEqual(self._timeline.get_zoom_start_time(), zoom_start) self.assertAlmostEqual(self._timeline.get_zoom_end_time(), zoom_end) self.assertTrue(self._timeline.is_zoomed()) self._clear_evt_queue() # don't care # Make sure we still get the permanent tick from the timeline # It might be delayed by one frame await self._app.next_update_async() await self._app.next_update_async() self._verify_evt(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED_PERMANENT) self._clear_evt_queue() # don't care self._timeline.set_director(None) self._timeline.commit() await self._app.next_update_async() self._verify_evt_exists(omni.timeline.TimelineEventType.DIRECTOR_CHANGED, 'hasDirector', False) omni.timeline.destroy_timeline('director') async def test_zoom(self): timeline_name = 'zoom_test' # create a new timeline so we don't interfere with other tests. timeline = omni.timeline.get_timeline_interface(timeline_name) timeline.set_time_codes_per_second(30) start_time = 0 end_time = 10 timeline.set_start_time(start_time) timeline.set_end_time(end_time) # initial state: no zoom self.assertAlmostEqual(timeline.get_start_time(), timeline.get_zoom_start_time()) self.assertAlmostEqual(timeline.get_end_time(), timeline.get_zoom_end_time()) self.assertFalse(timeline.is_zoomed()) # setting start and end time keeps the non-zoomed state start_time = 1 # smaller interval than the current end_time = 9 timeline.set_start_time(start_time) self.assertAlmostEqual(timeline.get_start_time(), timeline.get_zoom_start_time()) self.assertFalse(timeline.is_zoomed()) timeline.commit() timeline.set_end_time(end_time) timeline.commit() self.assertAlmostEqual(timeline.get_end_time(), timeline.get_zoom_end_time()) self.assertFalse(timeline.is_zoomed()) start_time = 0 # larger interval end_time = 10 timeline.set_start_time(start_time) self.assertAlmostEqual(timeline.get_start_time(), timeline.get_zoom_start_time()) self.assertFalse(timeline.is_zoomed()) timeline.commit() timeline.set_end_time(end_time) timeline.commit() self.assertAlmostEqual(timeline.get_end_time(), timeline.get_zoom_end_time()) self.assertFalse(timeline.is_zoomed()) # changes are not immediate timeline.set_zoom_range(start_time + 1, end_time - 1) self.assertAlmostEqual(timeline.get_zoom_start_time(), start_time) self.assertAlmostEqual(timeline.get_zoom_end_time(), end_time) self.assertFalse(timeline.is_zoomed()) timeline.commit() self.assertAlmostEqual(timeline.get_zoom_start_time(), start_time + 1) self.assertAlmostEqual(timeline.get_zoom_end_time(), end_time - 1) self.assertTrue(timeline.is_zoomed()) # clear timeline.clear_zoom() self.assertAlmostEqual(timeline.get_zoom_start_time(), start_time + 1) self.assertAlmostEqual(timeline.get_zoom_end_time(), end_time - 1) self.assertTrue(timeline.is_zoomed()) timeline.commit() self.assertAlmostEqual(timeline.get_zoom_start_time(), start_time) self.assertAlmostEqual(timeline.get_zoom_end_time(), end_time) self.assertFalse(timeline.is_zoomed()) # set zoom ranges inside the timeline's range timeline_sub = timeline.get_timeline_event_stream().create_subscription_to_pop( self._on_timeline_event ) self._test_zoom_change(timeline, start_time + 1, end_time - 1, True, start_time + 1, end_time - 1, "startTime", start_time + 1, False) self._test_zoom_change(timeline, start_time + 1, end_time - 2, True, start_time + 1, end_time - 2, "endTime", end_time - 2, False) self._test_zoom_change(timeline, start_time + 2, end_time - 1, True, start_time + 2, end_time - 1, "cleared", False) # invalid input zoom_start, zoom_end = timeline.get_zoom_start_time(), timeline.get_zoom_end_time() timeline.set_zoom_range(start_time + 3, start_time + 1) # end < start timeline.commit() self.assertAlmostEqual(timeline.get_zoom_start_time(), zoom_start) self.assertAlmostEqual(timeline.get_zoom_end_time(), zoom_end) # setting the same values should fire no events timeline.set_zoom_range(timeline.get_zoom_start_time(), timeline.get_zoom_end_time()) timeline.commit() self.assertTrue(self._buffered_evts.empty()) # set zoom ranges fully or partially outside the timeline's range, should be clipped self._test_zoom_change(timeline, start_time + 1, end_time + 1, True, start_time + 1, end_time, "endTime", end_time, False) self._test_zoom_change(timeline, start_time - 1, end_time - 1, True, start_time, end_time - 1, "startTime", start_time, False) self._test_zoom_change(timeline, start_time - 1, end_time + 1, False, start_time, end_time, "cleared", True) # passing an empty interval should set a 1 frame long zoom range self.assertGreater(timeline.get_time_codes_per_seconds(), 0) dt = 1.0 / timeline.get_time_codes_per_seconds() self._test_zoom_change(timeline, end_time, end_time, True, end_time - dt, end_time, "startTime", end_time - dt) self._test_zoom_change(timeline, start_time + 1, start_time + 1, True, start_time + 1, start_time + 1 + dt, "endTime", start_time + 1 + dt) # changing start/end time should not affect the zoom when setting a larger range old_zoom_start = timeline.get_zoom_start_time() old_zoom_end = timeline.get_zoom_end_time() start_time = -1 timeline.set_start_time(start_time) timeline.commit() self.assertAlmostEqual(timeline.get_zoom_start_time(), old_zoom_start) self.assertAlmostEqual(timeline.get_zoom_end_time(), old_zoom_end) self.assertTrue(timeline.is_zoomed()) self._verify_evt(omni.timeline.TimelineEventType.START_TIME_CHANGED) self.assertTrue(self._buffered_evts.empty()) end_time = 9 timeline.set_end_time(end_time) timeline.commit() self.assertAlmostEqual(timeline.get_zoom_start_time(), old_zoom_start) self.assertAlmostEqual(timeline.get_zoom_end_time(), old_zoom_end) self.assertTrue(timeline.is_zoomed()) self._verify_evt(omni.timeline.TimelineEventType.END_TIME_CHANGED) self.assertTrue(self._buffered_evts.empty()) # zoom range should shrink with start and end time timeline.set_zoom_range(start_time + 1, end_time - 1) # preparations for this test timeline.commit() old_zoom_start = timeline.get_zoom_start_time() old_zoom_end = timeline.get_zoom_end_time() self.assertTrue(timeline.is_zoomed()) self._clear_evt_queue() # don't care start_time = timeline.get_zoom_start_time() + 1 timeline.set_start_time(start_time) timeline.commit() self.assertAlmostEqual(timeline.get_zoom_start_time(), start_time) self.assertAlmostEqual(timeline.get_zoom_end_time(), old_zoom_end) self._verify_evt(omni.timeline.TimelineEventType.START_TIME_CHANGED) self._verify_evt(omni.timeline.TimelineEventType.ZOOM_CHANGED, "startTime", start_time, False) end_time = timeline.get_zoom_end_time() - 1 timeline.set_end_time(end_time) timeline.commit() self.assertAlmostEqual(timeline.get_zoom_start_time(), start_time) self.assertAlmostEqual(timeline.get_zoom_end_time(), end_time) self._verify_evt(omni.timeline.TimelineEventType.END_TIME_CHANGED) self._verify_evt(omni.timeline.TimelineEventType.ZOOM_CHANGED, "endTime", end_time, False) # playback is affected by zoom timeline_sub = None # don't care anymore zoom_start = start_time + 1 zoom_end = end_time - 1 timeline.set_zoom_range(zoom_start, zoom_end) timeline.commit() self.assertTrue(timeline.is_zoomed()) timeline.play() timeline.commit() self.assertAlmostEqual(timeline.get_current_time(), zoom_start) timeline.rewind_one_frame() timeline.commit() self.assertAlmostEqual(timeline.get_current_time(), zoom_end) timeline.forward_one_frame() timeline.commit() self.assertAlmostEqual(timeline.get_current_time(), zoom_start) timeline.set_current_time(zoom_start + 1) timeline.stop() timeline.commit() self.assertAlmostEqual(timeline.get_current_time(), zoom_start) omni.timeline.destroy_timeline(timeline_name) def _on_timeline_event(self, e: carb.events.IEvent): self._buffered_evts.put(e) def _verify_evt( self, type: omni.timeline.TimelineEventType, payload_key: str = None, payload_val=None, exact=False ): try: evt = self._buffered_evts.get_nowait() if evt: self.assertEqual(evt.type, int(type)) if payload_key and payload_val: if exact: self.assertEqual(evt.payload[payload_key], payload_val) else: self.assertAlmostEqual(evt.payload[payload_key], payload_val, places=4) except queue.Empty: self.assertTrue(False, "Expect event in queue but queue is empty") # verifies that the an event of the given type exists in the queue, and its payload matches def _verify_evt_exists( self, type: omni.timeline.TimelineEventType, payload_key: str = None, payload_val=None, exact=False ): found = False while not self._buffered_evts.empty(): evt = self._buffered_evts.get_nowait() if evt and evt.type == int(type): found = True if payload_key and payload_val: if exact: self.assertEqual(evt.payload[payload_key], payload_val) else: self.assertAlmostEqual(evt.payload[payload_key], payload_val, places=4) self.assertTrue(found, f"Event {type} was not found in the queue.") def _clear_evt_queue(self): while not self._buffered_evts.empty(): # clear the buffer self._buffered_evts.get_nowait() def _sleep(self, _): sleep(1.0) def _save_runloop_dt(self, e: carb.events.IEvent): self._runloop_dt = e.payload['dt'] def _assert_no_change_then_commit(self, old_value, new_value): self.assertEqual(old_value, new_value) self.assertTrue(self._buffered_evts.empty()) self._timeline.commit() def _test_zoom_change( self, timeline, start_time, end_time, expected_is_zoomed, expected_start_time, expected_end_time, payload_key, payload_value, exact=True ): timeline.set_zoom_range(start_time, end_time) timeline.commit() self.assertAlmostEqual(timeline.get_zoom_start_time(), expected_start_time) self.assertAlmostEqual(timeline.get_zoom_end_time(), expected_end_time) self.assertEqual(expected_is_zoomed, timeline.is_zoomed()) self._verify_evt(omni.timeline.TimelineEventType.ZOOM_CHANGED, payload_key, payload_value, exact) self.assertTrue(self._buffered_evts.empty())
56,075
Python
46.643161
125
0.640892
omniverse-code/kit/exts/omni.timeline/omni/timeline/tests/test_thread_safety.py
# 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. # import carb.events import omni.kit.app import omni.kit.test import omni.timeline import carb.settings import random from threading import get_ident, Lock, Thread from time import sleep from typing import List class TestTimelineThreadSafety(omni.kit.test.AsyncTestCase): async def setUp(self): self._app = omni.kit.app.get_app() self._timeline = omni.timeline.get_timeline_interface() self._timeline.stop() # make sure other tests do not interfere await self._app.next_update_async() self._timeline.set_end_time(100) self._timeline.set_start_time(0) self._timeline.set_current_time(0) self._timeline.set_time_codes_per_second(30) self._timeline.clear_zoom() await self._app.next_update_async() self._buffered_evts = [] self._timeline_sub = self._timeline.get_timeline_event_stream().create_subscription_to_pop( self._on_timeline_event ) self._setter_to_event_map = { 'set_auto_update': omni.timeline.TimelineEventType.AUTO_UPDATE_CHANGED, 'set_prerolling': omni.timeline.TimelineEventType.PREROLLING_CHANGED, 'set_looping': omni.timeline.TimelineEventType.LOOP_MODE_CHANGED, 'set_fast_mode': omni.timeline.TimelineEventType.FAST_MODE_CHANGED, 'set_target_framerate': omni.timeline.TimelineEventType.TARGET_FRAMERATE_CHANGED, 'set_current_time': omni.timeline.TimelineEventType.CURRENT_TIME_TICKED, 'set_start_time': omni.timeline.TimelineEventType.START_TIME_CHANGED, 'set_end_time': omni.timeline.TimelineEventType.END_TIME_CHANGED, 'set_time_codes_per_second': omni.timeline.TimelineEventType.TIME_CODE_PER_SECOND_CHANGED, 'set_ticks_per_frame': omni.timeline.TimelineEventType.TICKS_PER_FRAME_CHANGED, 'set_tentative_time': omni.timeline.TimelineEventType.TENTATIVE_TIME_CHANGED, 'play': omni.timeline.TimelineEventType.PLAY, 'pause': omni.timeline.TimelineEventType.PAUSE, 'stop': omni.timeline.TimelineEventType.STOP, 'rewind_one_frame': omni.timeline.TimelineEventType.CURRENT_TIME_TICKED, 'forward_one_frame': omni.timeline.TimelineEventType.CURRENT_TIME_TICKED, } async def tearDown(self): self._timeline = None self._timeline_sub = None async def test_setters(self): self._main_thread_id = get_ident() all_setters = ['set_auto_update', 'set_prerolling', 'set_looping', 'set_fast_mode', 'set_target_framerate', 'set_current_time', 'set_start_time', 'set_end_time', 'set_time_codes_per_second', 'set_ticks_per_frame', 'set_tentative_time'] all_getters = ['is_auto_updating', 'is_prerolling', 'is_looping', 'get_fast_mode', 'get_target_framerate', 'get_current_time', 'get_start_time', 'get_end_time', 'get_time_codes_per_seconds', 'get_ticks_per_frame', 'get_tentative_time'] all_values_to_set = [[True, False], [True, False], [True, False], [True, False], [24, 30, 60, 100], [0, 10, 12, 20], [0, 10], [20, 100], [24, 30, 60], [1, 2, 4], [0, 10, 12, 20]] self.assertEqual(len(all_getters), len(all_setters)) self.assertEqual(len(all_getters), len(all_values_to_set)) # Run for every attribute individually for i in range(len(all_setters)): print(f'Thread safety test for timeline method {all_setters[i]}') # Trying all values await self.do_multithreaded_test([[all_setters[i]]], [[all_getters[i]]], [[all_values_to_set[i]]], 200, 100) # Setting a single value, no other values should appear. Making sure the initial value is what we'll set. getattr(self._timeline, all_setters[i])(all_values_to_set[i][0]) await self._app.next_update_async() await self.do_multithreaded_test([[all_setters[i]]], [[all_getters[i]]], [[[all_values_to_set[i][0]]]], 50, 50) # Run for all attributes print('Thread safety test for all timeline methods') await self.do_multithreaded_test([all_setters], [all_getters], [all_values_to_set], 100, 100) async def test_time_control(self): self._main_thread_id = get_ident() all_methods = ['play', 'pause', 'stop', 'rewind_one_frame', 'forward_one_frame'] await self.do_multithreaded_test([all_methods], [None], [None], 50, 50) async def do_multithreaded_test( self, setters: List[List[str]], getters: List[List[str]], values_to_set: List[list], thread_count_per_type: int = 50, thread_runs: int = 50): MIN_SLEEP = 0.01 SLEEP_RANGE = 0.05 self.assertEqual(len(setters), len(getters)) for i, setter_list in enumerate(setters): if values_to_set[i] is not None: self.assertEqual(len(setter_list), len(values_to_set[i])) lock = Lock() running_threads = 0 def do(runs: int, thread_id: int, setters: List[str], getters: List[str], values_to_set: list, running_threads: int): with lock: running_threads = running_threads + 1 if getters is not None: self.assertEqual(len(setters), len(getters)) if values_to_set is not None: self.assertEqual(len(setters), len(values_to_set)) timeline = omni.timeline.get_timeline_interface() rnd = random.Random() rnd.seed(thread_id) for run in range(runs): i_attr = rnd.randint(0, len(setters) - 1) if values_to_set is not None: # setter is a setter method that accepts a value values = values_to_set[i_attr] i_value = rnd.randint(0, len(values) - 1) getattr(timeline, setters[i_attr])(values[i_value]) # We might want to see this when running tests, commented out for now # print(f'Thread {thread_id} has called {setters[i_attr]}({values[i_value]})') else: # "setter" is a method with no parameter (e.g. play()) getattr(timeline, setters[i_attr])() sleep(MIN_SLEEP + rnd.random() * SLEEP_RANGE) if getters is not None and values_to_set is not None: current_value = getattr(timeline, getters[i_attr])() self.assertTrue(current_value in values, f'Invalid value in thread {thread_id}: {current_value} is not in {values}') with lock: running_threads = running_threads - 1 thread_id = 0 threads = [] for thread_type_idx, setter in enumerate(setters): for i in range(thread_count_per_type): threads.append( Thread( target = do, args = ( thread_runs, thread_id, setters[thread_type_idx], getters[thread_type_idx], values_to_set[thread_type_idx], running_threads ) ) ) threads[-1].start() thread_id = thread_id + 1 self._buffered_evts = [] threads_running = True while threads_running: await self._app.next_update_async() with lock: threads_running = running_threads > 0 for thread in threads: thread.join() # an extra update to trigger last callbacks await self._app.next_update_async() # validate that we received only the expected events all_setters = [] for setter_list in setters: for setter in setter_list: all_setters.append(setter) allowed_events = [int(omni.timeline.TimelineEventType.CURRENT_TIME_TICKED_PERMANENT)] for setter in all_setters: allowed_events.append(int(self._setter_to_event_map[setter])) for evt in self._buffered_evts: self.assertTrue(evt in allowed_events, f'Error: event {evt} is not in allowed events {allowed_events} for setters {all_setters}') def _on_timeline_event(self, e: carb.events.IEvent): # callbacks are on the main thread self.assertEqual(get_ident(), self._main_thread_id) # save event type self._buffered_evts.append(e.type)
9,135
Python
45.141414
141
0.592556
omniverse-code/kit/exts/omni.timeline/omni/timeline/tests/__init__.py
from .tests import * from .test_thread_safety import *
54
Python
26.499987
33
0.759259
omniverse-code/kit/exts/omni.kit.widget.graph/PACKAGE-LICENSES/omni.kit.widget.graph-LICENSE.md
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.
412
Markdown
57.999992
74
0.839806
omniverse-code/kit/exts/omni.kit.widget.graph/config/extension.toml
[package] title = "Omni::UI Graph Widget" category = "Internal" description = "Omniverse Kit Graph Widget" version = "1.5.6-104_2" authors = ["NVIDIA"] repository = "" keywords = ["graph", "widget"] changelog = "docs/CHANGELOG.md" preview_image = "data/preview.png" icon = "data/icon.png" [dependencies] "omni.ui" = {} "omni.usd.libs" = {} [[python.module]] name = "omni.kit.widget.graph" [[python.module]] name = "omni.kit.widget.graph.tests" [settings] exts."omni.kit.widget.graph".raster_nodes = false [[test]] args = [ "--/app/window/dpiScaleOverride=1.0", "--/app/window/scaleToMonitor=false", ] dependencies = [ "omni.kit.renderer.capture", ] stdoutFailPatterns.exclude = [ "*omniclient: Initialization failed*", ] unreliable = true # OM-48945 [documentation] pages = [ "docs/overview.md", "docs/CHANGELOG.md", ]
852
TOML
17.148936
49
0.666667
omniverse-code/kit/exts/omni.kit.widget.graph/omni/kit/widget/graph/graph_node_delegate.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. # __all__ = ["GraphNodeDelegate"] from .graph_model import GraphModel from .graph_node_delegate_closed import GraphNodeDelegateClosed from .graph_node_delegate_full import GraphNodeDelegateFull from .graph_node_delegate_router import GraphNodeDelegateRouter from pathlib import Path import omni.ui as ui CURRENT_PATH = Path(__file__).parent ICON_PATH = CURRENT_PATH.parent.parent.parent.parent.joinpath("icons") # The main colors LABEL_COLOR = 0xFFB4B4B4 BACKGROUND_COLOR = 0xFF34302A BORDER_DEFAULT = 0xFFDBA656 BORDER_SELECTED = 0xFFFFFFFF CONNECTION = 0xFF80C280 NODE_BACKGROUND = 0xFF675853 NODE_BACKGROUND_SELECTED = 0xFF7F6C66 class GraphNodeDelegate(GraphNodeDelegateRouter): """ The delegate with the Omniverse design that has both full and collapsed states. """ def __init__(self): super().__init__() def is_closed(model, node): expansion_state = model[node].expansion_state return expansion_state == GraphModel.ExpansionState.CLOSED # Initial setup is the delegates for full and closed states. self.add_route(GraphNodeDelegateFull()) self.add_route(GraphNodeDelegateClosed(), expression=is_closed) @staticmethod def get_style(): """Return style that can be used with this delegate""" style = { "Graph": {"background_color": BACKGROUND_COLOR}, "Graph.Connection": {"color": CONNECTION, "background_color": CONNECTION, "border_width": 2.0}, # Node "Graph.Node.Background": {"background_color": NODE_BACKGROUND}, "Graph.Node.Background:selected": {"background_color": NODE_BACKGROUND_SELECTED}, "Graph.Node.Border": {"background_color": BORDER_DEFAULT}, "Graph.Node.Border:selected": {"background_color": BORDER_SELECTED}, "Graph.Node.Resize": {"background_color": BORDER_DEFAULT}, "Graph.Node.Resize:selected": {"background_color": BORDER_SELECTED}, "Graph.Node.Description": {"color": LABEL_COLOR}, "Graph.Node.Description.Edit": {"color": LABEL_COLOR, "background_color": NODE_BACKGROUND}, # Header Input "Graph.Node.Input": { "background_color": BORDER_DEFAULT, "border_color": BORDER_DEFAULT, "border_width": 3.0, }, # Header "Graph.Node.Header.Label": {"color": 0xFFB4B4B4, "margin_height": 5.0, "font_size": 14.0}, "Graph.Node.Header.Label::Degenerated": {"font_size": 22.0}, "Graph.Node.Header.Collapse": { "background_color": 0x0, "padding": 0, "image_url": f"{ICON_PATH}/hamburger-open.svg", }, "Graph.Node.Header.Collapse::Minimized": {"image_url": f"{ICON_PATH}/hamburger-minimized.svg"}, "Graph.Node.Header.Collapse::Closed": {"image_url": f"{ICON_PATH}/hamburger-closed.svg"}, # Header Output "Graph.Node.Output": { "background_color": BACKGROUND_COLOR, "border_color": BORDER_DEFAULT, "border_width": 3.0, }, # Port Group "Graph.Node.Port.Group": {"color": 0xFFB4B4B4}, "Graph.Node.Port.Group::Plus": {"image_url": f"{ICON_PATH}/Plus.svg"}, "Graph.Node.Port.Group::Minus": {"image_url": f"{ICON_PATH}/Minus.svg"}, # Port Input "Graph.Node.Port.Input": { "background_color": BORDER_DEFAULT, "border_color": BORDER_DEFAULT, "border_width": 3.0, }, "Graph.Node.Port.Input:selected": {"border_color": BORDER_SELECTED}, "Graph.Node.Port.Input.CustomColor": {"background_color": 0x0}, # Port "Graph.Node.Port.Branch": { "color": 0xFFB4B4B4, "border_width": 0.75, }, "Graph.Node.Port.Label": { "color": 0xFFB4B4B4, "margin_width": 5.0, "margin_height": 3.0, "font_size": 14.0, }, "Graph.Node.Port.Label::output": {"alignment": ui.Alignment.RIGHT}, # Port Output "Graph.Node.Port.Output": { "background_color": BACKGROUND_COLOR, "border_color": BORDER_DEFAULT, "border_width": 3.0, }, "Graph.Node.Port.Output.CustomColor": {"background_color": 0x0, "border_color": 0x0, "border_width": 3.0}, # Footer "Graph.Node.Footer": { "background_color": BACKGROUND_COLOR, "border_color": BORDER_DEFAULT, "border_width": 3.0, "border_radius": 8.0, }, "Graph.Node.Footer:selected": {"border_color": BORDER_SELECTED}, "Graph.Node.Footer.Image": {"image_url": f"{ICON_PATH}/0101.svg", "color": BORDER_DEFAULT}, "Graph.Selecion.Rect": { "background_color": ui.color(1.0, 1.0, 1.0, 0.1), "border_color": ui.color(1.0, 1.0, 1.0, 0.5), "border_width": 1, }, } return style @staticmethod def specialized_color_style(name, color, icon, icon_tint_color=None): """ Return part of the style that has everything to color special node type. Args: name: Node type color: Node color icon: Filename to the icon the node type should display icon_tint_color: Icon tint color """ style = { # Node f"Graph.Node.Border::{name}": {"background_color": color}, # Header Input f"Graph.Node.Input::{name}": {"background_color": color, "border_color": color}, # Header Output f"Graph.Node.Output::{name}": {"border_color": color}, # Port Input f"Graph.Node.Port.Input::{name}": {"background_color": color, "border_color": color}, f"Graph.Node.Port.Input::{name}:selected": {"background_color": color, "BORDER_SELECTED": color}, # Port Output f"Graph.Node.Port.Output::{name}": {"border_color": color}, # Footer f"Graph.Node.Footer::{name}": {"border_color": color}, f"Graph.Node.Footer.Image::{name}": {"image_url": icon, "color": icon_tint_color or color}, } return style @staticmethod def specialized_port_style(name, color): """ Return part of the style that has everything to color the customizable part of the port. Args: name: Port type type color: Port color """ style = { f"Graph.Node.Port.Input.CustomColor::{name}": {"background_color": color}, f"Graph.Node.Port.Output.CustomColor::{name}": {"background_color": color, "border_color": color}, f"Graph.Connection::{name}": {"color": color, "background_color": color}, } return style
7,536
Python
41.342696
118
0.574973
omniverse-code/kit/exts/omni.kit.widget.graph/omni/kit/widget/graph/isolation_graph_model.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. # __all__ = ["IsolationGraphModel"] from .graph_model import GraphModel from typing import Any from typing import Dict from typing import List from typing import Optional from typing import Tuple from typing import Union from .graph_model_batch_position_helper import GraphModelBatchPositionHelper def _get_ports_recursive(model, root): """Recursively get all the ports""" ports = model[root].ports if ports: recursive = [] for port in ports: recursive.append(port) subports = _get_ports_recursive(model, port) if subports: recursive += subports return recursive return ports class IsolationGraphModel: class MagicWrapperMeta(type): """ Python always looks in the class (and parent classes) __dict__ for magic methods and __getattr__ doesn't work, but since we want to override them, we need to use this trick with proxy property. It makes the class looking like the source object. See https://stackoverflow.com/questions/9057669 for details. """ def __init__(cls, name, bases, dct): ignore = "class mro new init setattr getattribute dict" def make_proxy(name): def proxy(self, *args): return getattr(self.source, name) return proxy type.__init__(cls, name, bases, dct) ignore = set("__%s__" % n for n in ignore.split()) for name in dir(cls): if name.startswith("__"): if name not in ignore and name not in dct: setattr(cls, name, property(make_proxy(name))) class EmptyPort: """Is used by the model for an empty port""" def __init__(self, parent: Union["IsolationGraphModel.InputNode", "IsolationGraphModel.OutputNode"]): self.parent = parent @staticmethod def get_type_name() -> str: """The string type that goes the source model abd view""" return "EmptyPort" class InputNode(metaclass=MagicWrapperMeta): """ Is used by the model for the input node. This node represents input ports of the compound node and it's placed to the subnetwork of the compound. """ def __init__(self, model: GraphModel, source): self._model = model self._inputs = [] self.source = source # TODO: circular reference self.empty_port = IsolationGraphModel.EmptyPort(self) def __getattr__(self, attr): return getattr(self.source, attr) def __hash__(self): # Hash should be different from source and from OutputNode return hash((self.source, "InputNode")) @staticmethod def get_type_name() -> str: """The string type that goes the source model and view""" return "InputNode" @property def ports(self) -> Optional[List[Any]]: """The list of ports of this node. It only has input ports from the compound node.""" ports = _get_ports_recursive(self._model, self.source) if ports is not None: inputs = [(port, self._model[port].inputs) for port in ports] self._inputs = [(p, i) for p, i in inputs if i is not None] return [p for p, _ in self._inputs] + [self.empty_port] class OutputNode(metaclass=MagicWrapperMeta): """ Is used by the model for the ouput node. This node represents output ports of the comound node and it's placed to the subnetwork of the compound. """ def __init__(self, model: GraphModel, source): self.source = source self._model = model self._outputs = [] # TODO: circular reference self.empty_port = IsolationGraphModel.EmptyPort(self) def __getattr__(self, attr): return getattr(self.source, attr) def __hash__(self): # Hash should be different from source and from InputNode return hash((self.source, "OutputNode")) @staticmethod def get_type_name() -> str: """The string type that goes the source model and view""" return "OutputNode" @property def ports(self) -> Optional[List[Any]]: """The list of ports of this node. It only has output ports from the compound node.""" ports = self._model[self.source].ports if ports is not None: outputs = [(port, self._model[port].outputs) for port in ports] self._outputs = [(p, o) for p, o in outputs if o is not None] return [p for p, _ in self._outputs] + [self.empty_port] class _IsolationItemProxy(GraphModel._ItemProxy): """ The proxy class that redirects the model calls from view to the source model or to the isolation model. """ def __init__(self, model: GraphModel, item: Any, isolation_model: "IsolationGraphModel"): super().__init__(model, item) object.__setattr__(self, "_isolation_model", isolation_model) def __setattr__(self, attr, value): """ Called when an attribute assignment is attempted. This is called instead of the normal mechanism (i.e. store the value in the instance dictionary). """ if hasattr(type(self), attr): proxy_property = getattr(type(self), attr) proxy_property.fset(self, value) else: super().__setattr__(attr, value) def is_input_node( self, item: Union["IsolationGraphModel.InputNode", "IsolationGraphModel.OutputNode"] = None ) -> bool: """True if the current redirection is related to the input node""" if item is None: item = self._item return isinstance(item, IsolationGraphModel.InputNode) def is_output_node( self, item: Union["IsolationGraphModel.InputNode", "IsolationGraphModel.OutputNode"] = None ) -> bool: """True if the current redirection is related to the output node""" if item is None: item = self._item return isinstance(item, IsolationGraphModel.OutputNode) def is_empty_port(self, item: "IsolationGraphModel.EmptyPort" = None) -> bool: """True if the current redirection is related to the empty port""" if item is None: item = self._item return isinstance(item, IsolationGraphModel.EmptyPort) # The methods of the model @property def name(self) -> str: """Redirects call to the source model if it's the model of the source model""" if self.is_input_node(): return self._model[self._item.source].name + " (input)" elif self.is_output_node(): return self._model[self._item.source].name + " (output)" elif self.is_empty_port(): return type(self._item).get_type_name() else: return self._model[self._item].name @name.setter def name(self, value: str): # Usually it's redirected automatically, but since we overrided property getter, we need to override setter if self.is_input_node() or self.is_output_node(): self._model[self._item.source].name = value else: self._model[self._item].name = value @property def type(self) -> str: """Redirects call to the source model if it's the model of the source model""" if self.is_input_node() or self.is_output_node() or self.is_empty_port(): return type(self._item).get_type_name() else: return self._model[self._item].type @property def ports(self) -> Optional[List[Any]]: """Redirects call to the source model if it's the model of the source model""" if self.is_input_node() or self.is_output_node(): ports = self._item.ports elif self.is_empty_port(): # TODO: Sub-ports return else: ports = self._model[self._item].ports # Save it for the future, so we know which ports belong to the current sub-network. # TODO: read from cache when second call for port in ports or []: self._isolation_model._ports[port] = self._item self._isolation_model._nodes[self._item] = ports return ports @ports.setter def ports(self, value: List[Any]): if self.is_input_node(): # Pretend like we set the ports of the compound node of the source model. filtered = list(self._isolation_model._root_outputs.keys()) for port in value: if self.is_empty_port(port): continue filtered.append(port) self._model[self._item.source].ports = filtered elif self.is_output_node(): # Pretend like we set the ports of the compound node of the source model. filtered = [] for port in value: if self.is_empty_port(port): continue filtered.append(port) filtered += list(self._isolation_model._root_inputs.keys()) self._model[self._item.source].ports = filtered else: # Just redirect the call self._model[self._item].ports = value @property def inputs(self) -> Optional[List[Any]]: if self.is_empty_port(): if self.is_output_node(self._item.parent): # Show dot on the left side return [] else: return elif self._item in self._isolation_model._root_ports: # This is the port of the root compound node inputs = self._isolation_model._root_outputs.get(self._item, None) if inputs is None: return else: inputs = self._model[self._item].inputs if not inputs: return inputs # Filter out the connections that go outside of this isolated model return [ i for i in inputs if i in self._isolation_model._ports or i in self._isolation_model._root_inputs or i in self._isolation_model._root_outputs ] @inputs.setter def inputs(self, value: List[Any]): if self.is_empty_port(): # The request to connect something to the Empty port of InputNode or OutputNode target = self._item.parent.source elif self.is_input_node() or self.is_output_node(): # The request to connect something to the InputNode or OutputNode itself target = self._item.source else: target = self._item if value is not None: filtered = [] for p in value: if isinstance(p, IsolationGraphModel.EmptyPort): # If it's an EmptyPort, we need to connect it to the # source directly. When the model receives the request # to connect to the node itself, it will create a port # for it. source = p.parent.source filtered.append(source) else: filtered.append(p) value = filtered self._model[target].inputs = value @property def outputs(self) -> Optional[List[Any]]: if self.is_empty_port(): if self.is_input_node(self._item.parent): # Show dot on the right side return [] else: return elif self._item in self._isolation_model._root_ports: outputs = self._isolation_model._root_inputs.get(self._item) if outputs is None: return else: outputs = self._model[self._item].outputs if not outputs: return outputs # Filter out the connections that go outside of this isolated model return [ o for o in outputs if o in self._isolation_model._ports or o in self._isolation_model._root_inputs or o in self._isolation_model._root_outputs ] @property def position(self) -> Optional[Tuple[float]]: if self.is_input_node(): if self._isolation_model._root_inputs: # TODO: It's not good to keep the position on the first available port. We need to group them. port_to_keep_position = list(self._isolation_model._root_inputs.keys())[0] position = self._model[port_to_keep_position].position if position is not None: return position elif self._isolation_model._input_position: return self._isolation_model._input_position else: return self._isolation_model._input_position elif self.is_output_node(): if self._isolation_model._root_outputs: # TODO: It's not good to keep the position on the first available port. We need to group them. port_to_keep_position = list(self._isolation_model._root_outputs.keys())[0] position = self._model[port_to_keep_position].position if position is not None: return position elif self._isolation_model._output_position: return self._isolation_model._output_position else: return self._isolation_model._output_position else: return self._model[self._item].position @position.setter def position(self, value: Optional[Tuple[float]]): if self.is_input_node(): if self._isolation_model._root_inputs: # TODO: get the proper port_to_keep_position port_to_keep_position = list(self._isolation_model._root_inputs.keys())[0] self._model[port_to_keep_position].position = value else: self._isolation_model._input_position = value elif self.is_output_node(): if self._isolation_model._root_outputs: # TODO: get the proper port_to_keep_position port_to_keep_position = list(self._isolation_model._root_outputs.keys())[0] self._model[port_to_keep_position].position = value else: self._isolation_model._output_position = value else: self._model[self._item].position = value @property def size(self) -> Optional[Tuple[float]]: if self.is_input_node() or self.is_output_node(): # Can't set/get size of input/output node return else: return self._model[self._item].size @size.setter def size(self, value: Optional[Tuple[float]]): if self.is_input_node() or self.is_output_node(): # Can't set/get size of input/output node pass else: self._model[self._item].size = value @property def display_color(self) -> Optional[Tuple[float]]: if self.is_input_node() or self.is_output_node(): # Can't set/get display_color of input/output node return else: return self._model[self._item].display_color @display_color.setter def display_color(self, value: Optional[Tuple[float]]): if self.is_input_node() or self.is_output_node(): # Can't set display color of input/output pass else: self._model[self._item].display_color = value @property def stacking_order(self) -> int: if self.is_input_node() or self.is_output_node(): return 1 else: return self._model[self._item].stacking_order @property def preview(self) -> Any: """Redirects call to the source model if it's the model of the source model""" if self.is_input_node() or self.is_output_node(): return self._model[self._item.source].preview else: return self._model[self._item].preview @property def icon(self) -> Any: """Redirects call to the source model if it's the model of the source model""" if self.is_input_node() or self.is_output_node(): return self._model[self._item.source].icon else: return self._model[self._item].icon @property def preview_state(self) -> GraphModel.PreviewState: """Redirects call to the source model if it's the model of the source model""" if self.is_input_node() or self.is_output_node(): return self._model[self._item.source].preview_state else: return self._model[self._item].preview_state @preview_state.setter def preview_state(self, value: GraphModel.PreviewState): if self.is_input_node() or self.is_output_node(): self._model[self._item.source].preview_state = value else: self._model[self._item].preview_state = value ############################################################################ def __getattr__(self, attr): """Pretend it's self._model""" return getattr(self._model, attr) def __getitem__(self, item): """Called to implement evaluation of self[key]""" # Return a proxy that redirects its properties back to the model. # return self._model[item] return self._IsolationItemProxy(self._model, item, self) def __init__(self, model: GraphModel, root): self._model: Optional[GraphModel] = model # It's important to set the proxy to set the position through this model if self._model and isinstance(self._model, GraphModelBatchPositionHelper): self._model.batch_proxy = self self._root = root self.clear_caches() # Redirect events from the original model to here self.__on_item_changed = GraphModel._Event() self.__on_selection_changed = GraphModel._Event() self.__on_node_changed = GraphModel._Event() self.__item_changed_subscription = self._model.subscribe_item_changed(self._item_changed) self.__selection_changed_subscription = self._model.subscribe_selection_changed(self._selection_changed) self.__node_changed_subscription = self._model.subscribe_node_changed(self._rebuild_node) # We create input and output nodes for the attributes of the parent compound # node. When the parent doesn't have attributes, we need to keep the # position locally. self._input_position = None self._output_position = None # Virtual nodes if self._root is not None and self._root_inputs: self._input_nodes = [IsolationGraphModel.InputNode(self._model, self._root)] else: self._input_nodes = [] if self._root is not None and self._root_outputs: self._output_nodes = [IsolationGraphModel.OutputNode(self._model, self._root)] else: self._output_nodes = [] def destroy(self): self._model = None self._root = None self._ports = {} self._nodes = {} self._root_inputs = {} self._root_outputs = {} self.__on_item_changed = GraphModel._Event() self.__on_selection_changed = GraphModel._Event() self.__on_node_changed = GraphModel._Event() self.__item_changed_subscription = None self.__selection_changed_subscription = None self._input_nodes = [] self._output_nodes = [] def clear_caches(self): # Port to node # TODO: WeakKeyDictionary self._ports: Dict[Any, Any] = {} # Nodes to ports # TODO: WeakKeyDictionary self._nodes: Dict[Any, Any] = {} # Cache ports and inputs self._root_ports: List[Any] = _get_ports_recursive(self._model, self._root) if (self._root is not None) else [] # Port to input/output self._root_inputs: Dict[Any, Any] = {} self._root_outputs: Dict[Any, Any] = {} for p in self._root_ports or []: inputs = self._model[p].inputs if inputs is not None: self._root_inputs[p] = inputs outputs = self._model[p].outputs if outputs is not None: self._root_outputs[p] = outputs def add_input_or_output(self, position: Tuple[float], is_input: bool = True): if is_input: if self._input_nodes: # TODO: Remove when we can handle many nodes if self._root_inputs: # Set the position of the input node if the position is not set port_position = list(self._root_inputs.keys())[0] if not self[port_position].position: self[port_position].position = position return node = IsolationGraphModel.InputNode(self._model, self._root) self._input_nodes.append(node) else: if self._output_nodes: # TODO: Remove when we can handle many nodes if self._root_outputs: # Set the position of the output node if the position is not set port_position = list(self._root_outputs.keys())[0] if not self[port_position].position: self[port_position].position = position return node = IsolationGraphModel.OutputNode(self._model, self._root) self._output_nodes.append(node) self[node].position = position # TODO: Root is changed self._item_changed(None) def _item_changed(self, item=None): """Call the event object that has the list of functions""" if item is None: self.clear_caches() if item == self._root: # Root item is changed. Rebuild all. self.clear_caches() item = None if item in self._root_inputs.keys(): self.__on_item_changed(self._input_nodes[0]) elif item in self._root_outputs.keys(): self.__on_item_changed(self._output_nodes[0]) else: # TODO: Filter unnecessary calls self.__on_item_changed(item) def subscribe_item_changed(self, fn): """ Return the object that will automatically unsubscribe when destroyed. """ return self._EventSubscription(self.__on_item_changed, fn) def _selection_changed(self): """Call the event object that has the list of functions""" # TODO: Filter unnecessary calls self.__on_selection_changed() def _rebuild_node(self, item=None, full=False): """Call the event object that has the list of functions""" self.__on_node_changed(item, full=full) def subscribe_selection_changed(self, fn): """ Return the object that will automatically unsubscribe when destroyed. """ return self._EventSubscription(self.__on_selection_changed, fn) def subscribe_node_changed(self, fn): """ Return the object that will automatically unsubscribe when destroyed. """ return self._EventSubscription(self.__on_node_changed, fn) @property def nodes(self, item: Any = None): """It's only called to get the nodes from the top level""" nodes = self._model[self._root].nodes if nodes is not None: # Inject the input and the output nodes nodes += self._input_nodes + self._output_nodes return nodes def can_connect(self, source: Any, target: Any): """Return if it's possible to connect source to target""" if isinstance(source, IsolationGraphModel.EmptyPort) or isinstance(target, IsolationGraphModel.EmptyPort): return True return self._model.can_connect(source, target) def position_begin_edit(self, item: Any): if isinstance(item, IsolationGraphModel.InputNode): if self._root_inputs: # The position of the input node port_to_keep_position = list(self._root_inputs.keys())[0] self._model.position_begin_edit(port_to_keep_position) elif isinstance(item, IsolationGraphModel.OutputNode): if self._root_outputs: # The position of the output node port_to_keep_position = list(self._root_outputs.keys())[0] self._model.position_begin_edit(port_to_keep_position) else: # Position of the regular node self._model.position_begin_edit(item) def position_end_edit(self, item: Any): if isinstance(item, IsolationGraphModel.InputNode): if self._root_inputs: # The position of the input node port_to_keep_position = list(self._root_inputs.keys())[0] self._model.position_end_edit(port_to_keep_position) elif isinstance(item, IsolationGraphModel.OutputNode): if self._root_outputs: # The position of the output node port_to_keep_position = list(self._root_outputs.keys())[0] self._model.position_end_edit(port_to_keep_position) else: # Position of the regular node self._model.position_end_edit(item) @property def selection(self) -> Optional[List[Any]]: # Redirect to the model. We need it to override the setter if self._model: return self._model.selection @selection.setter def selection(self, value: Optional[List[Any]]): if not self._model: return # Remove InputNode and OutputNode from selection filtered = [] for v in value: if not isinstance(v, IsolationGraphModel.EmptyPort): filtered.append(v) self._model.selection = filtered
27,756
Python
40
119
0.558834
omniverse-code/kit/exts/omni.kit.widget.graph/omni/kit/widget/graph/backdrop_delegate.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. # __all__ = ["BackdropDelegate"] from .abstract_graph_node_delegate import GraphNodeDescription from .graph_node_delegate_full import GraphNodeDelegateFull from .graph_node_delegate_full import LINE_VISIBLE_MIN from .graph_node_delegate_full import TEXT_VISIBLE_MIN import omni.ui as ui class BackdropDelegate(GraphNodeDelegateFull): """ The delegate with the Omniverse design for the nodes of the closed state. """ def node_header(self, model, node_desc: GraphNodeDescription): """Called to create widgets of the top of the node""" node = node_desc.node def set_color(model, node, item_model): sub_models = item_model.get_item_children() rgb = ( item_model.get_item_value_model(sub_models[0]).as_float, item_model.get_item_value_model(sub_models[1]).as_float, item_model.get_item_value_model(sub_models[2]).as_float, ) model[node].display_color = rgb with ui.ZStack(skip_draw_when_clipped=True): with ui.VStack(): self._common_node_header_top(model, node) with ui.VStack(): ui.Spacer(height=23) color_widget = ui.ColorWidget(width=20, height=20, style={"margin": 1}) sub_models = color_widget.model.get_item_children() # This constant is BORDER_DEFAULT = 0xFFDBA656 border_color = model[node].display_color or (0.337, 0.651, 0.859) color_widget.model.get_item_value_model(sub_models[0]).as_float = border_color[0] color_widget.model.get_item_value_model(sub_models[1]).as_float = border_color[1] color_widget.model.get_item_value_model(sub_models[2]).as_float = border_color[2] color_widget.model.add_end_edit_fn(lambda m, i: set_color(model, node, m)) def node_background(self, model, node_desc: GraphNodeDescription): """Called to create widgets of the node background""" node = node_desc.node # Constants from GraphNodeDelegateFull.node_background MARGIN_WIDTH = 7.5 MARGIN_TOP = 20.0 MARGIN_BOTTOM = 25.0 BORDER_THICKNESS = 3.0 BORDER_RADIUS = 3.5 HEADER_HEIGHT = 25.0 MIN_WIDTH = 180.0 TRIANGLE = 20.0 MIN_HEIGHT = 50.0 def on_size_changed(placer, model, node): offset_x = placer.offset_x offset_y = placer.offset_y model[node].size = (offset_x.value, offset_y.value) if offset_x.value < MIN_WIDTH: placer.offset_x = MIN_WIDTH if offset_y.value < MIN_HEIGHT: placer.offset_y = MIN_HEIGHT def set_description(field: ui.StringField, model: "GraphModel", node: any, description: str): """Called to set the description on the model and remove the field with the description""" model[node].description = description field.visible = False def on_description(model: "GraphModel", node: any, frame: ui.Frame, description: str): """Called to create a field on the node to edit description""" with frame: field = ui.StringField(multiline=True, style_type_name_override="Graph.Node.Description.Edit") if description: field.model.as_string = description field.model.add_end_edit_fn(lambda m: set_description(field, model, node, m.as_string)) field.focus_keyboard() width, height = model[node].size or (MIN_WIDTH, MIN_HEIGHT) with ui.ZStack(width=0, height=0): super().node_background(model, node_desc) # Properly alighned description with ui.VStack(): ui.Spacer(height=HEADER_HEIGHT + MARGIN_TOP) with ui.HStack(): ui.Spacer(width=MARGIN_WIDTH + BORDER_THICKNESS) with ui.ZStack(): description = model[node].description if description: ui.Label( description, alignment=ui.Alignment.LEFT_TOP, style_type_name_override="Graph.Node.Description", ) # Frame with description field frame = ui.Frame() frame.set_mouse_double_clicked_fn( lambda x, y, b, m, f=frame, d=description: on_description(model, node, f, d) ) ui.Spacer(width=MARGIN_WIDTH + BORDER_THICKNESS) ui.Spacer(height=MARGIN_BOTTOM + BORDER_THICKNESS) # The triangle that resizes the node with ui.VStack(): with ui.HStack(): placer = ui.Placer(draggable=True) placer.offset_x = width placer.offset_y = height placer.set_offset_x_changed_fn(lambda _, p=placer, m=model, n=node: on_size_changed(p, m, n)) placer.set_offset_y_changed_fn(lambda _, p=placer, m=model, n=node: on_size_changed(p, m, n)) with placer: triangle = ui.Triangle( width=TRIANGLE, height=TRIANGLE, alignment=ui.Alignment.RIGHT_TOP, style_type_name_override="Graph.Node.Resize", ) triangle.set_mouse_pressed_fn(lambda x, y, b, m: b == 0 and model.size_begin_edit(node)) triangle.set_mouse_released_fn(lambda x, y, b, m: b == 0 and model.size_end_edit(node)) ui.Spacer(width=MARGIN_WIDTH + 0.5 * BORDER_THICKNESS) ui.Spacer(height=MARGIN_BOTTOM + BORDER_THICKNESS) def node_footer(self, model, node_desc: GraphNodeDescription): pass
6,494
Python
44.41958
113
0.569911
omniverse-code/kit/exts/omni.kit.widget.graph/omni/kit/widget/graph/graph_node_delegate_full.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. # __all__ = ["color_to_hex", "GraphNodeDelegateFull"] from .abstract_graph_node_delegate import AbstractGraphNodeDelegate from .abstract_graph_node_delegate import GraphConnectionDescription from .abstract_graph_node_delegate import GraphNodeDescription from .abstract_graph_node_delegate import GraphNodeLayout from .abstract_graph_node_delegate import GraphPortDescription from .graph_model import GraphModel from functools import partial import colorsys import math import omni.ui as ui # Zoom level when the text disappears and the replacement line appears TEXT_VISIBLE_MIN = 0.6 # Zoom level when the line disappears LINE_VISIBLE_MIN = 0.15 CONNECTION_CURVE = 60 def color_to_hex(color: tuple) -> int: """Convert float rgb to int""" def to_int(f: float) -> int: return int(255 * max(0.0, min(1.0, f))) red = to_int(color[0]) green = to_int(color[1]) blue = to_int(color[2]) alpha = to_int(color[3]) if len(color) > 3 else 255 return (alpha << 8 * 3) + (blue << 8 * 2) + (green << 8 * 1) + red class GraphNodeDelegateFull(AbstractGraphNodeDelegate): """ The delegate with the Omniverse design. """ def __init__(self, scale_factor=1.0): self._scale_factor = scale_factor def __scale(self, value): """Return the value multiplied by global scale multiplier""" return value * self._scale_factor def __build_rectangle(self, radius, width, height, draw_top, style_name, name, style_override=None): """ Build rectangle of the specific design. Args: radius: The radius of round corers. The corners are top-left and bottom-right. width: The width of triangle to cut. The top-right and bottom-left trialgles are cut. height: The height of triangle to cut. The top-right and bottom-left trialgles are cut. draw_top: When false the top corners are straight. style_name: style_type_name_override of each widget name: name of each widget style_override: the style to apply to the top level node """ stack = ui.VStack() if style_override: stack.set_style(style_override) with stack: # Top of the rectangle if draw_top: with ui.HStack(height=0): with ui.VStack(width=0): ui.Circle( radius=radius, width=0, height=0, size_policy=ui.CircleSizePolicy.FIXED, alignment=ui.Alignment.RIGHT_BOTTOM, style_type_name_override=style_name, name=name, ) ui.Rectangle(style_type_name_override=style_name, name=name) ui.Rectangle(style_type_name_override=style_name, name=name) ui.Triangle( width=width, height=height, alignment=ui.Alignment.LEFT_TOP, style_type_name_override=style_name, name=name, ) # Middle of the rectangle ui.Rectangle(style_type_name_override=style_name, name=name) # Bottom of the rectangle with ui.HStack(height=0): ui.Triangle( width=width, height=height, alignment=ui.Alignment.RIGHT_BOTTOM, style_type_name_override=style_name, name=name, ) ui.Rectangle(style_type_name_override=style_name, name=name) with ui.VStack(width=0): ui.Rectangle(style_type_name_override=style_name, name=name) ui.Circle( radius=radius, width=0, height=0, size_policy=ui.CircleSizePolicy.FIXED, alignment=ui.Alignment.LEFT_TOP, style_type_name_override=style_name, name=name, ) # TODO: build_node_footer_input/build_node_footer_output def node_background(self, model, node_desc: GraphNodeDescription): """Called to create widgets of the node background""" node = node_desc.node # Constants MARGIN_WIDTH = 7.5 MARGIN_TOP = 20.0 MARGIN_BOTTOM = 25.0 BORDER_THICKNESS = 3.0 BORDER_RADIUS = 3.5 HEADER_HEIGHT = 25.0 MIN_WIDTH = 180.0 TRIANGLE = 20.0 # Computed values left_right_offset = MARGIN_WIDTH - BORDER_THICKNESS * 0.5 outer_radius = BORDER_RADIUS + BORDER_THICKNESS * 0.5 inner_radius = BORDER_RADIUS - BORDER_THICKNESS * 0.5 # The size of the triangle outer_triangle = TRIANGLE # The size of the triangle to make the thickness of the diagonal line # the same as horizontal and vertial thickness. Works only with 45 # degrees diagonals inner_triangle = outer_triangle - BORDER_THICKNESS / ( math.sqrt(2) * math.tan(math.radians((180.0 - 45.0) / 2.0)) ) style_name = str(model[node].type) # Draw a rectangle and a top line with ui.HStack(): # Left offset ui.Spacer(width=self.__scale(left_right_offset)) with ui.VStack(): ui.Spacer(height=self.__scale(MARGIN_TOP)) # The node body with ui.ZStack(): # This trick makes min width ui.Spacer(width=self.__scale(MIN_WIDTH)) # The color override for the border border_color = model[node].display_color if border_color: style = {"Graph.Node.Border": {"background_color": color_to_hex(border_color)}} else: style = None # Build outer rectangle self.__build_rectangle( self.__scale(outer_radius), self.__scale(outer_triangle), self.__scale(outer_triangle), True, "Graph.Node.Border", style_name, style, ) # Build inner rectangle with ui.VStack(): ui.Spacer(height=self.__scale(HEADER_HEIGHT)) with ui.HStack(): ui.Spacer(width=self.__scale(BORDER_THICKNESS)) if border_color: # 140% lightness from the border color # 50% saturation from the border color L_MULT = 1.4 S_MULT = 0.5 hls = colorsys.rgb_to_hls(border_color[0], border_color[1], border_color[2]) rgb = colorsys.hls_to_rgb(hls[0], min(1.0, (hls[1] * L_MULT)), hls[2] * S_MULT) if len(border_color) > 3: # alpha rgb = rgb + (border_color[3],) style = {"background_color": color_to_hex(rgb)} else: style = None self.__build_rectangle( self.__scale(inner_radius), self.__scale(inner_triangle), self.__scale(inner_triangle), False, "Graph.Node.Background", style_name, style, ) ui.Spacer(width=self.__scale(BORDER_THICKNESS)) ui.Spacer(height=self.__scale(BORDER_THICKNESS)) ui.Spacer(height=self.__scale(MARGIN_BOTTOM)) # Right offset ui.Spacer(width=self.__scale(left_right_offset)) def node_header_input(self, model, node_desc: GraphNodeDescription): """Called to create the left part of the header that will be used as input when the node is collapsed""" ui.Spacer(width=self.__scale(8)) def node_header_output(self, model, node_desc: GraphNodeDescription): """Called to create the right part of the header that will be used as output when the node is collapsed""" ui.Spacer(width=self.__scale(8)) def _common_node_header_top(self, model, node): """Node header part that is used in both full and closed states""" def switch_expansion(model, node): current = model[node].expansion_state model[node].expansion_state = GraphModel.ExpansionState((current.value + 1) % 3) # Draw the node name and a bit of space with ui.ZStack(width=0): ui.Label(model[node].name, style_type_name_override="Graph.Node.Header.Label", visible_min=TEXT_VISIBLE_MIN) with ui.Placer(stable_size=True, visible_min=LINE_VISIBLE_MIN, visible_max=TEXT_VISIBLE_MIN, offset_y=-8): ui.Label(model[node].name, name="Degenerated", style_type_name_override="Graph.Node.Header.Label") with ui.HStack(): # Collapse button collapse = ui.ImageWithProvider( width=self.__scale(18), height=self.__scale(18), style_type_name_override="Graph.Node.Header.Collapse" ) expansion_state = model[node].expansion_state if expansion_state == GraphModel.ExpansionState.CLOSED: collapse.name = "Closed" elif expansion_state == GraphModel.ExpansionState.MINIMIZED: collapse.name = "Minimized" else: collapse.name = "Open" collapse.set_mouse_pressed_fn(lambda x, y, b, m, model=model, node=node: switch_expansion(model, node)) def node_header(self, model, node_desc: GraphNodeDescription): """Called to create widgets of the top of the node""" with ui.VStack(skip_draw_when_clipped=True): self._common_node_header_top(model, node_desc.node) ui.Spacer(height=self.__scale(8)) def node_footer(self, model, node_desc: GraphNodeDescription): """Called to create widgets of the bottom of the node""" node = node_desc.node style_name = str(model[node].type) # Draw the circle and the image on the top of it with ui.VStack(visible_min=LINE_VISIBLE_MIN, skip_draw_when_clipped=True): ui.Spacer(height=self.__scale(10)) with ui.HStack(height=0): ui.Spacer() with ui.ZStack(width=self.__scale(50), height=self.__scale(50)): ui.Rectangle( style_type_name_override="Graph.Node.Footer", name=style_name, visible_min=TEXT_VISIBLE_MIN ) ui.ImageWithProvider( style_type_name_override="Graph.Node.Footer.Image", name=style_name, visible_min=TEXT_VISIBLE_MIN, ) # Scale up the icon when zooming out with ui.Placer( stable_size=True, visible_min=LINE_VISIBLE_MIN, visible_max=TEXT_VISIBLE_MIN, offset_x=self.__scale(-15), offset_y=self.__scale(-30), ): with ui.ZStack(width=0, height=0): ui.Rectangle(style_type_name_override="Graph.Node.Footer", name=style_name) ui.ImageWithProvider( style_type_name_override="Graph.Node.Footer.Image", name=style_name, width=self.__scale(80), height=self.__scale(80), ) ui.Spacer() ui.Spacer(height=self.__scale(2)) def port_input(self, model, node_desc: GraphNodeDescription, port_desc: GraphPortDescription): """Called to create the left part of the port that will be used as input""" node = node_desc.node port = port_desc.port connected_source = port_desc.connected_source connected_target = port_desc.connected_target if port is None: ui.Spacer(width=self.__scale(7.5)) return outputs = model[port].outputs is_output = outputs is not None if is_output: ui.Spacer(width=self.__scale(7.5)) return style_type_name = "Graph.Node.Port.Input" customcolor_style_type_name = "Graph.Node.Port.Input.CustomColor" inputs = model[port].inputs node_type = str(model[node].type) port_type = str(model[port].type) with ui.HStack(skip_draw_when_clipped=True): ui.Spacer(width=self.__scale(6)) with ui.ZStack(width=self.__scale(8)): if inputs is not None: # Half-circle that shows the port is able to have input connection # Background of the node color ui.Circle( radius=self.__scale(6), name=node_type, style_type_name_override=style_type_name, size_policy=ui.CircleSizePolicy.FIXED, style={"border_color": 0x0, "border_width": 0}, alignment=ui.Alignment.LEFT_CENTER, arc=ui.Alignment.RIGHT, visible_min=TEXT_VISIBLE_MIN, ) # Port has unique color ui.Circle( radius=self.__scale(6), name=port_type, style_type_name_override=customcolor_style_type_name, size_policy=ui.CircleSizePolicy.FIXED, alignment=ui.Alignment.LEFT_CENTER, arc=ui.Alignment.RIGHT, visible_min=TEXT_VISIBLE_MIN, ) # Border of the node color ui.Circle( radius=self.__scale(6), name=node_type, style_type_name_override=style_type_name, size_policy=ui.CircleSizePolicy.FIXED, style={"background_color": 0x0}, alignment=ui.Alignment.LEFT_CENTER, arc=ui.Alignment.RIGHT, visible_min=TEXT_VISIBLE_MIN, ) if connected_source: # Circle that shows that the port is a source for the connection ui.Circle( radius=self.__scale(7), name=port_type, size_policy=ui.CircleSizePolicy.FIXED, style_type_name_override="Graph.Connection", alignment=ui.Alignment.LEFT_CENTER, visible_min=TEXT_VISIBLE_MIN, ) if connected_target: # Circle that shows that the port is a target for the connection ui.Circle( radius=self.__scale(5), name=port_type, size_policy=ui.CircleSizePolicy.FIXED, style_type_name_override="Graph.Connection", alignment=ui.Alignment.LEFT_CENTER, visible_min=TEXT_VISIBLE_MIN, ) def port_output(self, model, node_desc: GraphNodeDescription, port_desc: GraphPortDescription): """Called to create the right part of the port that will be used as output""" node = node_desc.node port = port_desc.port connected_source = port_desc.connected_source connected_target = port_desc.connected_target if port is None: ui.Spacer(width=self.__scale(7.5)) return style_type_name = "Graph.Node.Port.Output" customcolor_style_type_name = "Graph.Node.Port.Output.CustomColor" outputs = model[port].outputs inputs = model[port].inputs node_type = str(model[node].type) port_type = str(model[port].type) with ui.ZStack(width=self.__scale(16), skip_draw_when_clipped=True): if outputs is not None: # Circle that shows the port is able to be an output connection # Background of the node color ui.Circle( name=node_type, style_type_name_override=style_type_name, alignment=ui.Alignment.CENTER, radius=self.__scale(6), size_policy=ui.CircleSizePolicy.FIXED, visible_min=TEXT_VISIBLE_MIN, ) # Port has unique color ui.Circle( name=port_type, style_type_name_override=customcolor_style_type_name, style={"background_color": 0x0}, alignment=ui.Alignment.CENTER, radius=self.__scale(6), size_policy=ui.CircleSizePolicy.FIXED, visible_min=TEXT_VISIBLE_MIN, ) if connected_source: # Circle that shows that the port is a source for the connection ui.Circle( radius=self.__scale(7), name=port_type, size_policy=ui.CircleSizePolicy.FIXED, style_type_name_override="Graph.Connection", alignment=ui.Alignment.CENTER, visible_min=TEXT_VISIBLE_MIN, ) if connected_target or (outputs == [] and inputs): # Circle that shows that the port is a target for the connection ui.Circle( radius=self.__scale(5), name=port_type, size_policy=ui.CircleSizePolicy.FIXED, style_type_name_override="Graph.Connection", alignment=ui.Alignment.CENTER, visible_min=TEXT_VISIBLE_MIN, ) def port(self, model, node_desc: GraphNodeDescription, port_desc: GraphPortDescription): """Called to create the middle part of the port""" def set_expansion_state(model, port, state: GraphModel.ExpansionState, *args): model[port].expansion_state = state port = port_desc.port level = port_desc.level if port is None: return sub_ports = model[port].ports is_group = sub_ports is not None inputs = model[port].inputs outputs = model[port].outputs is_input = inputs is not None is_output = outputs is not None style_name = "input" if is_input and not is_output else "output" with ui.HStack(skip_draw_when_clipped=True): if level > 0: style_type_name_override = "Graph.Node.Port.Branch" if port_desc.relative_position == port_desc.parent_child_count - 1: ui.Line( width=4, height=ui.Percent(50), style_type_name_override=style_type_name_override, alignment=ui.Alignment.RIGHT, visible_min=TEXT_VISIBLE_MIN, ) else: ui.Line( width=4, style_type_name_override=style_type_name_override, alignment=ui.Alignment.RIGHT, visible_min=TEXT_VISIBLE_MIN, ) ui.Line( width=8, style_type_name_override=style_type_name_override, visible_min=TEXT_VISIBLE_MIN, ) if is_group: # +/- button state = model[port].expansion_state if state == GraphModel.ExpansionState.CLOSED: image_name = "Plus" next_state = GraphModel.ExpansionState.OPEN else: image_name = "Minus" next_state = GraphModel.ExpansionState.CLOSED ui.ImageWithProvider( width=10, style_type_name_override="Graph.Node.Port.Group", name=image_name, visible_min=TEXT_VISIBLE_MIN, mouse_pressed_fn=partial(set_expansion_state, model, port, next_state), ) with ui.ZStack(): ui.Label( model[port].name, style_type_name_override="Graph.Node.Port.Label", name=style_name, visible_min=TEXT_VISIBLE_MIN, ) ui.Line( style_type_name_override="Graph.Node.Port.Label", visible_min=LINE_VISIBLE_MIN, visible_max=TEXT_VISIBLE_MIN, ) def connection(self, model, source: GraphConnectionDescription, target: GraphConnectionDescription): """Called to create the connection between ports""" port_type = str(model[source.port].type) if target.is_tangent_reversed != source.is_tangent_reversed: # It's the same node connection. Set tangent in pixels. start_tangent_width=ui.Pixel(20) end_tangent_width=ui.Pixel(20) else: # If the connection is reversed, we need to mirror tangents source_reverced_tangent = -1.0 if target.is_tangent_reversed else 1.0 target_reverced_gangent = -1.0 if source.is_tangent_reversed else 1.0 start_tangent_width=ui.Percent(-CONNECTION_CURVE * source_reverced_tangent) end_tangent_width=ui.Percent(CONNECTION_CURVE * target_reverced_gangent) ui.FreeBezierCurve( target.widget, source.widget, start_tangent_width=start_tangent_width, end_tangent_width=end_tangent_width, name=port_type, style_type_name_override="Graph.Connection", )
23,474
Python
41.994505
120
0.516827
omniverse-code/kit/exts/omni.kit.widget.graph/omni/kit/widget/graph/graph_layout.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. # __all__ = ["SugiyamaLayout"] from collections import defaultdict from bisect import bisect class SugiyamaLayout: """ Compute the coordinates for drawing directed graphs following the method developed by Sugiyama. This method consists of four phases: 1. Cycle Removal 2. Layer Assignment 3. Crossing Reduction 4. Coordinate Assignment As input it takes the list of edges in the following format: [(vertex1, vertex2), (vertex3, vertex4), ... ] Once the object is created, it's possible to get the node layer number immediately. To get the node positions, it's necessary to set each node's size and call `update_positions`. Follows closely to the following papers: [1] "An Efficient Implementation of Sugiyama's Algorithm for Layered Graph Drawing" Eiglsperger Siebenhaller Kaufmann [2] "Sugiyama Algorithm" Nikolov [3] "Graph Drawing Algorithms in Information Visualization" Frishman """ class Node: """Temporary node that caches all the intermediate compute data""" def __init__(self, id): self.id = id # Upstream and downstream nodes self.upstream = [] self.downstream = [] # For iteration self.is_currently_iterating = False self.highest_iteration_index = 0 self.lowest_iteration_index = 0 # Layer and position self.layer = None self.is_dummy = False # Barycenter is the position in the layer in [0..1] interval self.barycenter = None self.index_in_layer = None # The final geometry self.width = None self.height = None self.final_position = None self.root = None self.horizontal_aligned_to = None self.vertical_aligned_to = None self.offset = None self.max_y = None self.bound = [0.0, 0.0, 0.0, 0.0] def add_upstream(self, node): """Add the upstream node. It will add current node to downstream as well.""" if node.id not in self.upstream: self.upstream.append(node.id) if self.id not in node.downstream: node.downstream.append(self.id) def __repr__(self): result = f"<Node {self.id}: up" for n in self.upstream: result += f" {n}" result += "; down" for n in self.downstream: result += f" {n}" result += f"; layer_id{self.layer}>" return result def __init__(self, edges=[], vertical_distance=10.0, horizontal_distance=10.0): # Minimal space between items self.vertical_distance = vertical_distance self.horizontal_distance = horizontal_distance # Current alignment direction. It will call property setter. self._alignment_direction = 0 # Counter for creating dummy nodes. Dummy nodes are negative ID. self._dummy_counter = -1 self._edges = set(edges) # All the nodes self._nodes = {} # Connected graphs self._graphs = [] # self._dummies = {} # All the layers it's a dict with list of vertices id self._layers = defaultdict(list) # The action self._split_to_graphs() self._layout() @property def _alignment_direction(self): return self.__alignment_direction @property def _alignment_direction_horizontal(self): return self.__alignment_direction_horizontal @property def _alignment_direction_vertical(self): return self.__alignment_direction_vertical @_alignment_direction.setter def _alignment_direction(self, alignment_direction): """ Alignment policy: _alignment_direction=0 -> vertical=1, horizontal=-1 _alignment_direction=1 -> vertical=-1, horizontal=-1 _alignment_direction=2 -> vertical=1, horizontal=1 _alignment_direction=3 -> vertical=-1, horizontal=1 """ self.__alignment_direction = alignment_direction self.__alignment_direction_vertical, self.__alignment_direction_horizontal = { 0: (1, -1), 1: (-1, -1), 2: (1, 1), 3: (-1, 1), }[alignment_direction] @_alignment_direction_horizontal.setter def _alignment_direction_horizontal(self, _alignment_direction_horizontal): _alignment_direction = (_alignment_direction_horizontal + 1) + (1 - self.__alignment_direction_vertical) // 2 self._alignment_direction = _alignment_direction @_alignment_direction_vertical.setter def _alignment_direction_vertical(self, _alignment_direction_vertical): _alignment_direction = (self.__alignment_direction_horizontal + 1) + (1 - _alignment_direction_vertical) // 2 self._alignment_direction = _alignment_direction def _get_roots(self, graph): # Nodes that doesn't have anything downstream current_roots = [] for edge in graph: vertex = edge[0] if not self._nodes[vertex].downstream: current_roots.append(self._nodes[vertex]) return current_roots def __get_connected_dummy(self, node): dummy_id = node.dummy_nodes.get(node.layer - 1, None) return [dummy_id] if dummy_id is not None else [] def __is_between_dummies(self, node): return any([x.is_dummy for x in self.__get_connected_dummy(node)]) def __iterate_node_edges(self, node, counter, visited, reversed_edges): counter[0] += 1 node.highest_iteration_index = counter[0] node.lowest_iteration_index = counter[0] visited.append(node) node.is_currently_iterating = True for vertex in node.upstream: upstream = self._nodes[vertex] if upstream.highest_iteration_index == 0: self.__iterate_node_edges(upstream, counter, visited, reversed_edges) node.lowest_iteration_index = min(node.lowest_iteration_index, upstream.lowest_iteration_index) elif upstream.is_currently_iterating: # It's a loop. We need to invert this connection. reversed_edges.append((node.id, upstream.id)) if upstream in visited: node.lowest_iteration_index = min(node.lowest_iteration_index, upstream.highest_iteration_index) if node.lowest_iteration_index == node.highest_iteration_index: backtracing = [visited.pop()] while backtracing[-1] != node: backtracing.append(visited.pop()) node.is_currently_iterating = False def _get_reversed_edges(self, graph, roots): counter = [0] visited = [] reversed_edges = [] for vertex, node in self._nodes.items(): node.highest_iteration_index = 0 # Start from roots for root in roots: self.__iterate_node_edges(root, counter, visited, reversed_edges) # Iterate rest for edge in graph: for vertex in edge: node = self._nodes[vertex] if node.highest_iteration_index == 0: self.__iterate_node_edges(node, counter, visited, reversed_edges) return reversed_edges def _invert_edge(self, edge): """Invert the flow direction of the given edge""" v1 = edge[0] v2 = edge[1] node1 = self._nodes[v1] node1.upstream.remove(v2) node2 = self._nodes[v2] node2.downstream.remove(v1) node2.add_upstream(node1) def _set_layer(self, node): """Set the layer id of the node""" current_layer = node.layer # Layers start from 1 new_layer = max([self._nodes[x].layer for x in node.downstream] + [0]) + 1 if current_layer == new_layer: # Nothing changed return if current_layer is not None: self._layers[current_layer].remove(node.id) node.layer = new_layer self._layers[node.layer].append(node.id) def _iterate_layer(self, roots): scaned_edges = {} # Roots are in the first layer current_layer = roots while len(current_layer) > 0: next_layer = [] for node in current_layer: self._set_layer(node) # Mark out-edges has scanned. for vertex in node.upstream: edge = (node.id, vertex) scaned_edges[edge] = True # Check if out-vertices are rank-able. for x in node.upstream: upstream = self._nodes[x] if not ( False in [scaned_edges.get((vertex, upstream.id), False) for vertex in upstream.downstream] ): if x not in next_layer: next_layer.append(self._nodes[x]) current_layer = next_layer def _create_dummy(self, layer, dummy_nodes): """Create a dummy node and put it to the layer.""" # Setup a new node dummy_node = self._nodes[self._dummy_counter] = self.Node(self._dummy_counter) self._dummy_counter -= 1 dummy_node.is_dummy = True dummy_node.layer = layer dummy_node.width = 0.0 dummy_node.height = 0.0 self._layers[layer].append(dummy_node.id) dummy_node.dummy_nodes = dummy_nodes dummy_nodes[layer] = dummy_node return dummy_node def _create_dummies(self, edge): """Create and all dummy nodes for the given edge.""" v1, v2 = edge layer1, layer2 = self._nodes[v1].layer, self._nodes[v2].layer if layer1 > layer2: v1, v2 = v2, v1 layer1, layer2 = layer2, layer1 if (layer2 - layer1) > 1: # "dummy vertices" are stored in the dict, keyed by their layer. dummy_nodes = self._dummies[edge] = {} node1 = self._nodes[v1] node2 = self._nodes[v2] dummy_nodes[layer1] = node1 dummy_nodes[layer2] = node2 # Disconnect the nodes node1.upstream.remove(v2) node2.downstream.remove(v1) # Insert dummies between nodes prev_dummy = node1 for layer_id in range(layer1 + 1, layer2): dummy = self._create_dummy(layer_id, dummy_nodes) prev_dummy.add_upstream(dummy) prev_dummy = dummy prev_dummy.add_upstream(node2) def _get_cross_count(self, layer_id): """Number of crosses in the layer""" neighbor_indices = [] layer = self._layers[layer_id] for vertex in layer: node = self._nodes[vertex] neighbor_indices.extend( sorted([self._nodes[neighbor].index_in_layer for neighbor in self._get_neighbors(node)]) ) s = [] count = 0 for i, neighbor_index in enumerate(neighbor_indices): j = bisect(s, neighbor_index) if j < i: count += i - j s.insert(j, neighbor_index) return count def _get_next_layer_id(self, layer_id): """The layer that is the next according to the current slignment direction""" layer_id += 1 if self._alignment_direction_horizontal == -1 else -1 return layer_id def _get_prev_layer_id(self, layer_id): """The layer that is the previous according to the current slignment direction""" layer_id += 1 if self._alignment_direction_horizontal == 1 else -1 return layer_id def _get_mean_value_position(self, node): """ Compute the position of the node according to the position of neighbors in the previous layer. It's the mean value of adjacent positions of neighbors. """ layer_id = node.layer prev_layer = self._get_prev_layer_id(layer_id) if prev_layer not in self._layers: return node.barycenter bars = [self._nodes[vertex].barycenter for vertex in self._get_neighbors(node)] return node.barycenter if len(bars) == 0 else float(sum(bars)) / len(bars) def _reduce_crossings(self, layer_id): """ Reorder the nodes in the layer to reduce the number of crossings in the layer. Return the new number of crossing. """ layer = self._layers[layer_id] num_nodes = len(layer) total_crossings = 0 for i, j in zip(range(num_nodes - 1), range(1, num_nodes)): vertex_i = layer[i] vertex_j = layer[j] barycenters_neighbors_i = [ self._nodes[vertex].barycenter for vertex in self._get_neighbors(self._nodes[vertex_i]) ] barycenters_neighbors_j = [ self._nodes[vertex].barycenter for vertex in self._get_neighbors(self._nodes[vertex_j]) ] crossings_ij = crossings_ji = 0 for neightbor_j in barycenters_neighbors_j: crossings = len([neighbor_i for neighbor_i in barycenters_neighbors_i if neighbor_i > neightbor_j]) # Crossings we have now crossings_ij += crossings # Crossings we would have if swap verices crossings_ji += len(barycenters_neighbors_i) - crossings if crossings_ji < crossings_ij: # Swap vertices layer[i] = vertex_j layer[j] = vertex_i total_crossings += crossings_ji else: total_crossings += crossings_ij return total_crossings def _reorder(self, layer_id): """ Reorder the nodes within the layer to reduce the number of crossings in the layer. Return the number of crossing. """ # TODO: Use code from _reduce_crossings to find the initial number of # crossings. c = self._get_cross_count(layer_id) layer = self._layers[layer_id] barycenter_height = 1.0 / (len(layer) - 1) if len(layer) > 1 else 1.0 if c > 0: for vertex in layer: node = self._nodes[vertex] node.barycenter = self._get_mean_value_position(node) # Reorder layers according to barycenter. layer.sort(key=lambda x: self._nodes[x].barycenter) c = self._reduce_crossings(layer_id) # Re assign new position since it was reordered for i, vertex in enumerate(layer): self._nodes[vertex].index_in_layer = i self._nodes[vertex].barycenter = i * barycenter_height return c def _ordering_pass(self, direction=-1): """ Ordering of the vertices such that the number of edge crossings is reduced. """ crossings = 0 self._alignment_direction_horizontal = direction for layer_id in sorted(self._layers.keys())[::-direction]: crossings += self._reorder(layer_id) return crossings def _get_neighbors(self, node): """ Neighbors are to left/right adjacent nodes. Node.upstream/downstream have connections from all the layers. This returns from neighbour layers according to the current alignment direction. """ alignment_direction_horizontal = self._alignment_direction_horizontal # TODO: It's called very often. We need to cache it. node.neighbors_at_direction = {-1: list(node.downstream), 1: list(node.upstream)} if node.is_dummy: return node.neighbors_at_direction[alignment_direction_horizontal] for direction in (-1, 1): tr = node.layer + direction for i, x in enumerate(node.neighbors_at_direction[direction]): if self._nodes[x].layer == tr: continue # TODO: check if we need this code. upstream/downstream has dummies. edge = (node.id, x) if edge not in self._dummies: edge = (x, node.id) dum = self._dummies[edge][tr] node.neighbors_at_direction[direction][i] = dum.id return node.neighbors_at_direction[alignment_direction_horizontal] def _get_median_index(self, node): """ Find the position of node according to neigbor positions in neigbor layer. """ neighbors = self._get_neighbors(node) index_in_layer = [self._nodes[x].index_in_layer for x in neighbors] neighbors_size = len(index_in_layer) if neighbors_size == 0: return [] index_in_layer.sort() index_in_layer = index_in_layer[:: self._alignment_direction_vertical] i, j = divmod(neighbors_size - 1, 2) return [index_in_layer[i]] if j == 0 else [index_in_layer[i], index_in_layer[i + j]] def _horizontal_alignment(self): """ Horizontal alignment according to current alignment direction. """ alignment_direction_vertical, alignment_direction_horizontal = ( self._alignment_direction_vertical, self._alignment_direction_horizontal, ) for layer_id in sorted(self._layers.keys())[::-alignment_direction_horizontal]: prev_layer_id = self._get_prev_layer_id(layer_id) if prev_layer_id not in self._layers: continue current_vertex_index = None layer = self._layers[layer_id] prev_layer = self._layers[prev_layer_id] for vertex in layer[::alignment_direction_vertical]: node = self._nodes[vertex] for median_vertex_index in self._get_median_index(node): median_vertex = prev_layer[median_vertex_index] if self._nodes[vertex].horizontal_aligned_to is vertex: if alignment_direction_horizontal == 1: edge = (vertex, median_vertex) else: edge = (median_vertex, vertex) if edge in self._dummy_intersections: continue if (current_vertex_index is None) or ( alignment_direction_vertical * current_vertex_index < alignment_direction_vertical * median_vertex_index ): # Align median self._nodes[median_vertex].horizontal_aligned_to = vertex # Align the given one median_root = self._nodes[median_vertex].root self._nodes[vertex].horizontal_aligned_to = median_root self._nodes[vertex].root = median_root current_vertex_index = median_vertex_index def _align_subnetwork(self, vertex): """ Vertical alignment according to current alignment direction. """ if self._nodes[vertex].max_y is not None: return self._nodes[vertex].max_y = 0.0 vertex_to_align = vertex while True: prev_index_in_layer = self._nodes[vertex_to_align].index_in_layer - self._alignment_direction_vertical layer_id = self._nodes[vertex_to_align].layer if 0 <= prev_index_in_layer < len(self._layers[layer_id]): prev_vertex_id = self._layers[layer_id][prev_index_in_layer] vertical_distance = ( self.vertical_distance + self._nodes[prev_vertex_id].height * 0.5 + self._nodes[vertex_to_align].height * 0.5 ) # Recursively place subnetwork root_vertex_id = self._nodes[prev_vertex_id].root self._align_subnetwork(root_vertex_id) # Adjust node position if self._nodes[vertex].vertical_aligned_to is vertex: self._nodes[vertex].vertical_aligned_to = self._nodes[root_vertex_id].vertical_aligned_to if self._nodes[vertex].vertical_aligned_to == self._nodes[root_vertex_id].vertical_aligned_to: self._nodes[vertex].max_y = max( self._nodes[vertex].max_y, self._nodes[root_vertex_id].max_y + vertical_distance ) else: aligned_vertex = self._nodes[root_vertex_id].vertical_aligned_to offset = self._nodes[vertex].max_y - self._nodes[root_vertex_id].max_y + vertical_distance if self._nodes[aligned_vertex].offset is None: self._nodes[aligned_vertex].offset = offset else: self._nodes[aligned_vertex].offset = min(self._nodes[aligned_vertex].offset, offset) vertex_to_align = self._nodes[vertex_to_align].horizontal_aligned_to if vertex_to_align is vertex: # Already aligned break def _vertical_alignment(self): """ Vertical alignment according to current alignment direction. """ _alignment_direction_vertical, _alignment_direction_horizontal = ( self._alignment_direction_vertical, self._alignment_direction_horizontal, ) layer_ids = sorted(self._layers.keys())[::-_alignment_direction_horizontal] for layer_id in layer_ids: for vertex in self._layers[layer_id][::_alignment_direction_vertical]: if self._nodes[vertex].root is vertex: self._align_subnetwork(vertex) # Mirror nodes when the alignment is bottom. if _alignment_direction_vertical == -1: for layer_id in layer_ids: for vertex in self._layers[layer_id]: y = self._nodes[vertex].max_y if y: self._nodes[vertex].max_y = -y # Assign bound bound = None for layer_id in layer_ids: for vertex in self._layers[layer_id][::_alignment_direction_vertical]: self._nodes[vertex].bound[self._alignment_direction] = self._nodes[self._nodes[vertex].root].max_y aligned_vertex = self._nodes[self._nodes[vertex].root].vertical_aligned_to offset = self._nodes[aligned_vertex].offset if offset is not None: self._nodes[vertex].bound[self._alignment_direction] += _alignment_direction_vertical * offset if bound is None: bound = self._nodes[vertex].bound[self._alignment_direction] else: bound = min(bound, self._nodes[vertex].bound[self._alignment_direction]) # Initialize for layer_id, layer in self._layers.items(): for vertex in layer: self._nodes[vertex].root = vertex self._nodes[vertex].horizontal_aligned_to = vertex self._nodes[vertex].vertical_aligned_to = vertex self._nodes[vertex].offset = None self._nodes[vertex].max_y = None def _split_to_graphs(self): """Split edges to multiple connected graphs. Result is self._graphs.""" # All vertices vertices = set() # Dict where key is vertex, value is all the connected vertices dependencies = defaultdict(set) for v1, v2 in self._edges: vertices.add(v1) vertices.add(v2) dependencies[v1].add(v2) dependencies[v2].add(v1) if v1 in self._nodes: n1 = self._nodes[v1] else: n1 = self.Node(v1) self._nodes[v1] = n1 if v2 in self._nodes: n2 = self._nodes[v2] else: n2 = self.Node(v2) self._nodes[v2] = n2 n1.add_upstream(n2) while vertices: # Start with any vertex current_vertices = [vertices.pop()] # For each vertex, remove all connected from `vertices` and add it to current graph for vertex in current_vertices: for dependent in dependencies.get(vertex, []): if dependent in vertices: vertices.remove(dependent) current_vertices.append(dependent) # Here `current_vertices` has all the vertices that are conneted to a single graph current_edges = set() for vertex in current_vertices: for edge in self._edges: if edge[0] in current_vertices or edge[1] in current_vertices: current_edges.add(edge) self._graphs.append(current_edges) def _find_dummy_intersections(self): """ Detect crossings in dummy nodes. """ self._dummy_intersections = [] for layer_id, layer_vertices in self._layers.items(): prev_layer_id = layer_id - 1 if prev_layer_id not in self._layers: continue first_vertex = 0 vertices_count = len(layer_vertices) - 1 prev_layer_vertices_count = len(self._layers[prev_layer_id]) - 1 vertex_range_from = 0 for i, current_vertex_id in enumerate(layer_vertices): node = self._nodes[current_vertex_id] if not node.is_dummy: continue if i == vertices_count or self.__is_between_dummies(node): connected_dummy_index = prev_layer_vertices_count if self.__is_between_dummies(node): connected_dummy_index = self.__get_connected_dummy(node)[-1].index_in_layer for vertex_in_this_layer in layer_vertices[vertex_range_from : i + 1]: for neighbor in self._get_neighbors(self._nodes[vertex_in_this_layer]): neighbor_index = self._nodes[neighbor].index_in_layer if neighbor_index < first_vertex or neighbor_index > connected_dummy_index: # Intersection found self._dummy_intersections.append((neighbor, vertex_in_this_layer)) vertex_range_from = i + 1 first_vertex = connected_dummy_index def _layout(self): """Perform first three steps of Sugiyama layouting""" for graph in self._graphs: # 1. Cycle Removal roots = self._get_roots(graph) reversed_edges = self._get_reversed_edges(graph, roots) # Make the graph acyclic by reversing appropriate edges. for edge in reversed_edges: self._invert_edge(edge) # Get roots one more time roots += [node for node in self._get_roots(graph) if node not in roots] # 2. Layer Assignment self._iterate_layer(roots) # TODO: Optimize layers. Root nodes could potentially go to other # layers if it could minimize crossings # Add dummies for edge in graph: self._create_dummies(edge) # Pre-setup of some indices for _, layer in self._layers.items(): # Barycenter is the position in the layer in [0..1] interval barycenter_height = 1.0 / (len(layer) - 1) if len(layer) > 1 else 1.0 for i, vertex in enumerate(layer): node = self._nodes[vertex] node.index_in_layer = i node.barycenter = i * barycenter_height # 3. Crossing Reduction, 2 passes in both direction for optimal ordering for i in range(2): # Ordering pass for layers from 0 to end crossings = self._ordering_pass() if crossings > 0: # Second pass in reverse direction self._ordering_pass(direction=1) self._ordering_pass() # 3a. Crossing Reduction in dummy nodes self._find_dummy_intersections() def update_positions(self): """4. Coordinate Assignment""" # Initialize node attributes. # TODO: Put it to init for _, layer in self._layers.items(): for vertex in layer: node = self._nodes[vertex] node.root = vertex node.horizontal_aligned_to = vertex node.vertical_aligned_to = vertex node.offset = None node.max_y = None node.bound = [0.0, 0.0, 0.0, 0.0] for _alignment_direction in range(4): self._alignment_direction = _alignment_direction self._horizontal_alignment() self._vertical_alignment() # Final coordinate assigment of all nodes: x_counter = 0 for _, layer in self._layers.items(): if not layer: continue max_width = max([self._nodes[vertex].width / 2.0 for vertex in layer]) y_counter = None for vertex in layer: y = sorted(self._nodes[vertex].bound) # Average of computed bound average_y = (y[1] + y[2]) / 2.0 # Prevent node intersections if y_counter is None: y_counter = average_y y_counter = max(y_counter, average_y) # Final xy-coordinates. Negative X because we go from right to left. self._nodes[vertex].final_position = (-x_counter - max_width, y_counter) y_counter += self._nodes[vertex].height + self.vertical_distance x_counter += 2 * max_width + self.horizontal_distance def set_size(self, vertex, width, height): """Set the size of the node""" node = self._nodes.get(vertex, None) if not node: # The node not in the list because the node is not connected to anythig. # Since the graph layers start from 1, we put those non-graph # vertices to the layer 0 and they will stay in a separate column. layer_id = 0 node = self.Node(vertex) node.layer = layer_id node.index_in_layer = len(self._layers[layer_id]) self._nodes[node.id] = node self._layers[layer_id].append(node.id) node.width = width node.height = height def get_position(self, vertex): """The position of the node""" node = self._nodes.get(vertex, None) if node: return node.final_position def get_layer(self, vertex): """The layer id of the node""" node = self._nodes.get(vertex, None) if node: return node.layer
31,702
Python
38.480697
117
0.562867
omniverse-code/kit/exts/omni.kit.widget.graph/omni/kit/widget/graph/graph_node_index.py
# Copyright (c) 2018-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__ = ["GraphNodeIndex"] from .graph_model import GraphModel from collections import defaultdict from typing import Any, Set, Tuple from typing import Dict from typing import List from typing import Optional from typing import Union import itertools import weakref class CachePort: """ The structure to keep in the ports and their properties and don't access the model many times. """ def __init__( self, port: Any, state: Optional[GraphModel.ExpansionState], child_count: int, level: int, relative_position: int, parent_child_count: int, parent_cached_port: Optional[weakref.ProxyType] = None, ): # Port from the model self.port: Any = port # Expansion state if it's a groop self.state: Optional[GraphModel.ExpansionState] = state # Number of children self.child_count: int = child_count # The level in the tree structure self.level: int = level # Position number in the parent list self.relative_position = relative_position # Number of children of the parent self.parent_child_count = parent_child_count # The visibility after collapsing self.visibile: bool = True # Inputs from the model self.inputs: Optional[List[Any]] = None # Outputs from the model self.outputs: Optional[List[Any]] = None self.parent_cached_node: Optional[weakref.ProxyType] = None self.parent_cached_port: Optional[weakref.ProxyType] = parent_cached_port def __repr__(self): return f"<CachePort {self.port}>" def __hash__(self): if self.inputs: inputs_hash = hash(tuple(hash(i) for i in self.inputs)) else: inputs_hash = 0 if self.outputs: outputs_hash = hash(tuple(hash(i) for i in self.outputs)) else: outputs_hash = 0 return hash( ( CachePort, self.port, self.state and self.state.value, self.visibile, inputs_hash, outputs_hash, ) ) def __eq__(self, other): return hash(self) == hash(other) class CacheNode: """The structure to keep in the cache and don't access the model many times""" def __init__( self, node: Any, state: Optional[GraphModel.ExpansionState], cached_ports: List[CachePort], stacking_order: int, additional_hash=0, ): # Node from the model self.node: Any = node # Expansion state if it's a groop self.state: Optional[GraphModel.ExpansionState] = state # Ports from the model self.cached_ports: List[CachePort] = cached_ports # Level represents the distance of the current node from the root self.level = None self.stacking_order: int = stacking_order # The nodes dependent from the current node. If the nodes connected like this A.out -> B.in, # then A.dependent = [B] self.dependent = [] # Hash self._additional_hash = additional_hash self.inputs = [] self.outputs = [] # Add connection to the parent node selfproxy = weakref.proxy(self) for port in self.cached_ports: port.parent_cached_node = selfproxy def __repr__(self): return f"<CacheNode {self.node}>" def __hash__(self): cached_ports_hash = hash(tuple(hash(p) for p in self.cached_ports)) return hash((CacheNode, self.node, self.state and self.state.value, cached_ports_hash, self._additional_hash)) def __eq__(self, other): return hash(self) == hash(other) class CacheConnection: """The structure to keep connections in the cache and don't access the model many times""" def __init__( self, source_cached_port: CachePort, target_cached_port: CachePort, ): self.source_port: Any = source_cached_port.port self.target_port: Any = target_cached_port.port self.__hash = hash( ( CacheConnection, source_cached_port, target_cached_port, source_cached_port.parent_cached_node.__hash__(), target_cached_port.parent_cached_node.__hash__(), ) ) @property def pair(self): return (self.source_port, self.target_port) def __repr__(self): return f"<CacheConnection {self.pair}>" def __hash__(self): return self.__hash def __eq__(self, other): return hash(self) == hash(other) class GraphNodeDiff: """ The object that keeps the difference that is the list of nodes and connections to add and delete. """ def __init__( self, nodes_to_add: List[CacheNode] = None, nodes_to_del: List[CacheNode] = None, connections_to_add: List[CacheConnection] = None, connections_to_del: List[CacheConnection] = None, ): self.nodes_to_add = nodes_to_add self.nodes_to_del = nodes_to_del self.connections_to_add = connections_to_add self.connections_to_del = connections_to_del @property def valid(self): return ( self.nodes_to_add is not None and self.nodes_to_del is not None and self.connections_to_add is not None and self.connections_to_del is not None ) def __repr__(self): return ( "<GraphNodeDiff\n" f" Add: {self.nodes_to_add} {self.connections_to_add}\n" f" Del: {self.nodes_to_del} {self.connections_to_del}\n" ">" ) class GraphNodeIndex: """ Hirarchy index of the model. Provides fast access to the nodes and connections.""" def __init__(self, model: Optional[GraphModel], port_grouping: bool): # List of all the nodes from the model self.cached_nodes: List[Optional[CacheNode]] = [] # List of all the connections from the model self.cached_connections: List[Optional[CacheConnection]] = [] # Dictionary that has a node from the model as a key and the # index of this node in all_cached_nodes. self.node_to_id: Dict[Any, int] = {} # Dictionary that has a port from the model as a key and the # index of the parent node in self.cached_nodes. self.port_to_id: Dict[Any, int] = {} # Dictionary that has a port from the model as a key and the # index of the port in cached_node.cached_ports. port_to_port_id: Dict[Any, int] = {} # Connection hash to ID in self.cached_connections. self.connection_to_id: Dict[Tuple[Any, Any], int] = {} # Set with all the ports from the model if this port is output. We need # it to detect the flow direction. self.ports_used_as_output = set() # All the connections. self.source_to_target = defaultdict(set) # Dict[child port: parent port] self.port_child_to_parent: Dict[Any, Any] = {} if not model: return # Preparing the cache and indices. for node in model.nodes or []: # Caching ports # TODO: Nested group support root_ports = model[node].ports or [] root_port_count = len(root_ports) cached_ports: List[CachePort] = [] for id, port in enumerate(root_ports): if port_grouping: sub_ports = model[port].ports is_group = sub_ports is not None else: sub_ports = None is_group = False state = model[port].expansion_state cached_port = CachePort(port, state, len(sub_ports) if is_group else 0, 0, id, root_port_count) cached_ports.append(cached_port) if is_group: # TODO: Nested group support cached_port_proxy = weakref.proxy(cached_port) sub_port_count = len(sub_ports) cached_sub_ports = [ CachePort(p, None, 0, 1, id, sub_port_count, cached_port_proxy) for id, p in enumerate(sub_ports) ] cached_ports += cached_sub_ports # The ID of the current cached node in the cache cached_node_id = len(self.cached_nodes) # The global index self.node_to_id[node] = cached_node_id state = model[node].expansion_state # Allows to draw backdrops in backgroud stacking_order = model[node].stacking_order # Hash name, description and color, so the node is autogenerated when it's changed. additional_hash = hash((model[node].name, model[node].description, model[node].display_color)) # The global cache self.cached_nodes.append(CacheNode(node, state, cached_ports, stacking_order, additional_hash)) # Cache connections for cached_node_id, cached_node in enumerate(self.cached_nodes): cached_ports = cached_node.cached_ports # True if node has output node node_has_outputs = False # Parent port to put the connections to cached_port_parent: Optional[CachePort] = None # How many ports already hidden hidden_port_counter = 0 # How many ports we need to hide hidden_port_number = 0 # for port, port_state, port_child_count in zip(ports, port_states, port_child_counts): for cached_port_id, cached_port in enumerate(cached_ports): port = cached_port.port # print("Cached ports", port, cached_port_id) port_inputs = model[port].inputs port_outputs = model[port].outputs # Copy to detach from the model port_inputs = port_inputs[:] if port_inputs is not None else None port_outputs = port_outputs[:] if port_outputs is not None else None cached_port.visibile = not cached_port_parent if cached_port_parent: # Put inputs/outputs to the parent if port_inputs is not None: cached_port_parent.inputs = cached_port_parent.inputs or [] cached_port_parent.inputs += port_inputs if port_outputs is not None: cached_port_parent.outputs = cached_port_parent.outputs or [] cached_port_parent.outputs += port_outputs else: # Put inputs/outputs to the port cached_port.inputs = port_inputs cached_port.outputs = port_outputs if cached_port_parent: connection_port = cached_port_parent.port # Dict[child: parent] self.port_child_to_parent[cached_port.port] = cached_port_parent.port else: connection_port = cached_port.port if port_inputs: for source in port_inputs: # |--------| |-----------------| # | source | ---> | connection_port | # |--------| |-----------------| # Ex: # source is Usd.Prim(</World/Looks/OmniGlass/Shader>).GetAttribute('outputs:out') # connection_port is Usd.Prim(</World/Looks/OmniGlass>).GetAttribute('outputs:mdl:displacement') self.source_to_target[source].add(connection_port) if port_outputs: for source in port_outputs: self.source_to_target[source].add(connection_port) if port_outputs is not None: node_has_outputs = True self.port_to_id[port] = cached_node_id port_to_port_id[port] = cached_port_id # Check if the port is hidden and hide it. It happens # when the port is collapsed. if cached_port_parent: hidden_port_counter += 1 hidden_port_number += cached_port.child_count if hidden_port_counter == hidden_port_number: # We hide enough cached_port_parent = None continue if cached_port.child_count > 0 and cached_port.state == GraphModel.ExpansionState.CLOSED: # The next one should be hidden cached_port_parent = cached_port hidden_port_counter = 0 hidden_port_number = cached_port.child_count for cached_port in cached_ports: # If the node don't have any output (in OmniGraph inputs and outputs are equal) we consider that all # the input attributes are used as outputs. if not node_has_outputs or cached_port.outputs is not None: self.ports_used_as_output.add(cached_port.port) # chain.from_iterable(['ABC', 'DEF']) --> A B C D E F cached_node.inputs = list(itertools.chain.from_iterable([p.inputs for p in cached_ports if p.inputs])) cached_node.outputs = list(itertools.chain.from_iterable([p.outputs for p in cached_ports if p.outputs])) # Replace input/output of cached ports to point to the parents of the collapsed ports for cached_node in self.cached_nodes: for cached_port in cached_node.cached_ports: if cached_port.inputs: inputs = [] for i in cached_port.inputs: parent = self.port_child_to_parent.get(i, None) if parent: inputs.append(parent) else: inputs.append(i) cached_port.inputs = inputs if cached_port.outputs: outputs = [] for i in cached_port.outputs: parent = self.port_child_to_parent.get(i, None) if parent: outputs.append(parent) else: outputs.append(i) cached_port.outputs = outputs # Remove the collapsed ports from source_to_target and move the removed content to the port parents. source_to_target_filtered = defaultdict(set) for source, target in self.source_to_target.items(): if source in self.port_child_to_parent: source_to_target_filtered[self.port_child_to_parent[source]].update(target) else: source_to_target_filtered[source].update(target) self.source_to_target = source_to_target_filtered # Save connections for source, targets in self.source_to_target.items(): source_node_id = self.port_to_id[source] source_port_id = port_to_port_id[source] source_cached_port = self.cached_nodes[source_node_id].cached_ports[source_port_id] for target in targets: target_node_id = self.port_to_id[target] target_port_id = port_to_port_id[target] target_cached_port = self.cached_nodes[target_node_id].cached_ports[target_port_id] connection = CacheConnection(source_cached_port, target_cached_port) self.connection_to_id[connection.pair] = len(self.cached_connections) self.cached_connections.append(connection) def get_diff(self, other: "GraphNodeIndex"): """ Generate the difference object: the list of nodes and connections to add and delete. """ # Nodes diff self_nodes = set(self.cached_nodes[i] for _, i in self.node_to_id.items()) other_nodes = set(other.cached_nodes[i] for _, i in other.node_to_id.items()) nodes_to_add = list(other_nodes - self_nodes) nodes_to_del = list(self_nodes - other_nodes) if len(self.node_to_id) == len(nodes_to_del): # If we need to remove all nodes, it's better to create a new graph node index return GraphNodeDiff() max_level = max(self.cached_nodes[i].stacking_order for _, i in self.node_to_id.items()) for node in nodes_to_add: if node.stacking_order is not None and node.stacking_order < max_level: # We can't put the node under others. Only to top. return GraphNodeDiff() # Connections diff self_connections = set(self.cached_connections[i] for _, i in self.connection_to_id.items()) other_connections = set(other.cached_connections[i] for _, i in other.connection_to_id.items()) connections_to_add = list(other_connections - self_connections) connections_to_del = list(self_connections - other_connections) return GraphNodeDiff(nodes_to_add, nodes_to_del, connections_to_add, connections_to_del) def mutate(self, diff: Union["GraphNodeIndex", GraphNodeDiff]) -> Tuple[Set]: """Apply the difference to the cache index.""" if isinstance(diff, GraphNodeIndex): diff = self.get_diff(diff) node_add = set() node_del = set() connection_add = set() connection_del = set() # Remove nodes from index for cached_node in diff.nodes_to_del: node = cached_node.node node_id = self.node_to_id[node] self.cached_nodes[node_id] = None for cached_port in cached_node.cached_ports: port = cached_port.port self.port_to_id.pop(port) if port in self.ports_used_as_output: self.ports_used_as_output.remove(port) self.port_child_to_parent.pop(port, None) node_del.add(node) self.node_to_id.pop(node) # Remove connections from index for cached_connection in diff.connections_to_del: connection_id = self.connection_to_id[cached_connection.pair] self.cached_connections[connection_id] = None source_port = cached_connection.source_port target_port = cached_connection.target_port targets = self.source_to_target[source_port] targets.remove(target_port) if len(targets) == 0: self.source_to_target.pop(source_port) connection_del.add(cached_connection) self.connection_to_id.pop(cached_connection.pair) # Add nodes to index for cached_node in diff.nodes_to_add: node = cached_node.node node_id = len(self.cached_nodes) self.cached_nodes.append(cached_node) self.node_to_id[node] = node_id node_add.add(node) cached_ports = cached_node.cached_ports # True if node has output node node_has_outputs = False for cached_port in cached_ports: port = cached_port.port self.port_to_id[port] = node_id self.ports_used_as_output if cached_port.outputs is not None: node_has_outputs = True cached_port_parent = cached_port.parent_cached_port if cached_port_parent: self.port_child_to_parent[cached_port.port] = cached_port_parent.port for cached_port in cached_ports: # If the node don't have any output (in OmniGraph inputs and # outputs are equal) we consider that all the input attributes # are used as outputs. if not node_has_outputs or cached_port.outputs is not None: self.ports_used_as_output.add(cached_port.port) # Add connections to index for cached_connection in diff.connections_to_add: connection_id = len(self.cached_connections) self.cached_connections.append(cached_connection) self.connection_to_id[cached_connection.pair] = connection_id connection_add.add(cached_connection) source_port = cached_connection.source_port target_port = cached_connection.target_port self.source_to_target[source_port].add(target_port) return node_add, node_del, connection_add, connection_del
21,336
Python
38.078755
120
0.569226
omniverse-code/kit/exts/omni.kit.widget.graph/omni/kit/widget/graph/graph_model.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. # __all__ = ["GraphModel"] from enum import Enum from enum import IntFlag from enum import auto from typing import Any from typing import Optional from typing import Union class GraphModel: """ The base class for the Graph model. The model is the central component of the graph widget. It is the application's dynamic data structure, independent of the user interface, and it directly manages the data. It follows closely model–view pattern. It defines the standard interface to be able to interoperate with the components of the model-view architecture. It is not supposed to be instantiated directly. Instead, the user should subclass it to create a new model. The model manages two kinds of data elements. Node and port are the atomic data elements of the model. Both node and port can have any number of sub-ports and any number of input and output connections. There is no specific Python type for the elements of the model. Since Python has dynamic types, the model can return any object as a node or a port. When the widget needs to get a property of the node, it provides the given node back to the model. Example: .. code:: python class UsdShadeModel(GraphModel): @property def nodes(self, prim=None): # Return Usd.Prim in a list return [stage.GetPrimAtPath(selection)] @property def name(self, item=None): # item here is Usd.Prim because UsdShadeModel.nodes returns # Usd.Prim return item.GetPath().name # Accessing nodes and properties example model = UsdShadeModel() # UsdShadeModel decides the type of nodes. It's a list with Usd.Prim nodes = model.nodes for node in nodes: # The node is accessed through evaluation of self[key]. It will # return the proxy object that redirects its properties back to # model. So the following line will call UsdShadeModel.name(node). name = model[node].name print(f"The model has node {name}") """ class _ItemProxy: """ The proxy that allows accessing the nodes and ports of the model through evaluation of self[key]. This proxy object redirects its properties to the model that has properties like this: class Model: @property def name(self, item): pass @name.setter def name(self, value, item): pass """ def __init__(self, model, item): """Save model and item to be able to redirect the properties""" # Since we already have __setattr__ reimplemented, the following # code is the way to bypass it super().__setattr__("_model", model) super().__setattr__("_item", item) def __getattr__(self, attr): """ Called when the default attribute access fails with an AttributeError. """ model_property = getattr(type(self._model), attr) return model_property.fget(self._model, self._item) def __setattr__(self, attr, value): """ Called when an attribute assignment is attempted. This is called instead of the normal mechanism (i.e. store the value in the instance dictionary). """ model_property = getattr(type(self._model), attr) model_property.fset(self._model, value, self._item) # TODO: Maybe it's better to automatically call model._item_changed here? # No, it's not better because in some cases node is changing and # the widget doesn't change. For example when the user changes the # position. class _Event(set): """ A list of callable objects. Calling an instance of this will cause a call to each item in the list in ascending order by index. """ def __call__(self, *args, **kwargs): """Called when the instance is “called” as a function""" # Call all the saved functions for f in list(self): f(*args, **kwargs) def __repr__(self): """ Called by the repr() built-in function to compute the “official” string representation of an object. """ return f"Event({set.__repr__(self)})" class ExpansionState(Enum): OPEN = 0 MINIMIZED = 1 CLOSED = 2 class PreviewState(IntFlag): NONE = 0 OPEN = auto() CACHED = auto() LARGE = auto() class _EventSubscription: """ Event subscription. _Event has callback while this object exists. """ def __init__(self, event, fn): """ Save the function, the event, and add the function to the event. """ self._fn = fn self._event = event event.add(self._fn) def __del__(self): """Called by GC.""" self._event.remove(self._fn) DISPLAY_NAME = None def __init__(self): super().__init__() # TODO: begin_edit/end_edit self.__on_item_changed = self._Event() self.__on_selection_changed = self._Event() self.__on_node_changed = self._Event() def __getitem__(self, item): """Called to implement evaluation of self[key]""" # Return a proxy that redirects its properties back to the model. return self._ItemProxy(self, item) def _item_changed(self, item=None): """Call the event object that has the list of functions""" self.__on_item_changed(item) def _rebuild_node(self, item=None, full=False): """Call the event object that has the list of functions""" self.__on_node_changed(item, full=full) def subscribe_item_changed(self, fn): """ Return the object that will automatically unsubscribe when destroyed. """ return self._EventSubscription(self.__on_item_changed, fn) def _selection_changed(self): """Call the event object that has the list of functions""" self.__on_selection_changed() def subscribe_selection_changed(self, fn): """ Return the object that will automatically unsubscribe when destroyed. """ return self._EventSubscription(self.__on_selection_changed, fn) def subscribe_node_changed(self, fn): """ Return the object that will automatically unsubscribe when destroyed. """ return self._EventSubscription(self.__on_node_changed, fn) @staticmethod def has_nodes(obj): """Returns true if the model can currently build the graph network using the provided object""" pass # The list of properties of node and port. @property def name(self, item) -> str: """The name of the item how it should be displayed in the view""" pass @name.setter def name(self, value: str, item=None): """Request to rename item""" pass @property def type(self, item): pass @property def inputs(self, item): pass @inputs.setter def inputs(self, value, item=None): pass @property def outputs(self, item): pass @outputs.setter def outputs(self, value, item=None): pass @property def nodes(self, item=None): pass @property def ports(self, item=None): pass @ports.setter def ports(self, value, item=None): pass @property def expansion_state(self, item=None) -> ExpansionState: return self.ExpansionState.OPEN @expansion_state.setter def expansion_state(self, value: ExpansionState, item=None): pass def can_connect(self, source, target): """Return if it's possible to connect source to target""" return True @property def position(self, item=None): """Returns the position of the node""" pass @position.setter def position(self, value, item=None): """The node position setter""" pass def position_begin_edit(self, item): """Called when the user started dragging the node""" pass def position_end_edit(self, item): """Called when the user finished dragging the node and released the mouse""" pass @property def size(self, item): """The node size. Is used for nodes like Backdrop.""" pass @size.setter def size(self, value, item=None): """The node position setter""" pass def size_begin_edit(self, item): """Called when the user started resizing the node""" pass def size_end_edit(self, item): """Called when the user finished resizing the node and released the mouse""" pass @property def description(self, item): """The text label that is displayed on the backdrop in the node graph.""" pass @description.setter def description(self, value, item=None): """The node description setter""" pass @property def display_color(self, item): """The node color.""" pass @display_color.setter def display_color(self, value, item=None): """The node color setter""" pass @property def stacking_order(self, item): """ This value is a hint when an application cares about the visibility of a node and whether each node overlaps another. """ return 0 @property def selection(self): pass @selection.setter def selection(self, value: list): pass def special_select_widget(self, node, node_widget): # Returning None means node_widget will get used return None def destroy(self): pass @property def icon(self, item) -> Optional[Union[str, Any]]: """Return icon of the image""" pass @icon.setter def icon(self, value: Optional[Union[str, Any]], item=None): """Set the icon of the image""" pass @property def preview(self, item) -> Optional[Union[str, Any]]: """Return the preview of the image""" pass @preview.setter def preview(self, value: Optional[Union[str, Any]], item=None): """Set the preview of the image""" pass @property def preview_state(self, item) -> PreviewState: """Return the state of the preview of the node""" return GraphModel.PreviewState.NONE @preview_state.setter def preview_state(self, value: PreviewState, item=None): """Set the state of the preview of the node""" pass
11,338
Python
28.997354
103
0.602928
omniverse-code/kit/exts/omni.kit.widget.graph/omni/kit/widget/graph/__init__.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. # # This extension both provides generic Omni UI Graph interface and implements couple of graph models, including one for # omni.graph. we need to split it eventually in 2 exts. So that one can use ui graph without omni.graph. For now # just make it optional by handling import failure as non-error. from .abstract_batch_position_getter import AbstractBatchPositionGetter from .abstract_graph_node_delegate import AbstractGraphNodeDelegate from .abstract_graph_node_delegate import GraphConnectionDescription from .abstract_graph_node_delegate import GraphNodeDescription from .abstract_graph_node_delegate import GraphNodeLayout from .abstract_graph_node_delegate import GraphPortDescription from .backdrop_delegate import BackdropDelegate from .backdrop_getter import BackdropGetter from .compound_node_delegate import CompoundInputOutputNodeDelegate from .compound_node_delegate import CompoundNodeDelegate from .graph_model import GraphModel from .graph_model_batch_position_helper import GraphModelBatchPositionHelper from .graph_node_delegate import GraphNodeDelegate from .graph_node_delegate_router import GraphNodeDelegateRouter from .graph_view import GraphView from .isolation_graph_model import IsolationGraphModel from .selection_getter import SelectionGetter
1,706
Python
54.064514
119
0.841149
omniverse-code/kit/exts/omni.kit.widget.graph/omni/kit/widget/graph/compound_node_delegate.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. # __all__ = ["CompoundNodeDelegate", "CompoundInputOutputNodeDelegate"] from .abstract_graph_node_delegate import GraphNodeDescription from .abstract_graph_node_delegate import GraphPortDescription from .graph_node_delegate_full import GraphNodeDelegateFull from typing import Any from typing import List import omni.ui as ui class CompoundNodeDelegate(GraphNodeDelegateFull): """ The delegate for the compound nodes. """ pass class CompoundInputOutputNodeDelegate(GraphNodeDelegateFull): """ The delegate for the input/output nodes of the compound. """ def __init__(self): super().__init__() self.__port_context_menu = None def destroy(self): self.__port_context_menu = None def port_input(self, model, node_desc: GraphNodeDescription, port_desc: GraphPortDescription): node = node_desc.node port = port_desc.port frame = ui.Frame() with frame: super().port_input(model, node_desc, port_desc) frame.set_mouse_pressed_fn(lambda x, y, b, _, m=model, n=node, p=port: b == 1 and self.__on_menu(m, n, p)) def port_output(self, model, node_desc: GraphNodeDescription, port_desc: GraphPortDescription): node = node_desc.node port = port_desc.port frame = ui.Frame() with frame: super().port_output(model, node_desc, port_desc) frame.set_mouse_pressed_fn(lambda x, y, b, _, m=model, n=node, p=port: b == 1 and self.__on_menu(m, n, p)) def port(self, model, node_desc: GraphNodeDescription, port_desc: GraphPortDescription): port = port_desc.port stack = ui.ZStack() with stack: super().port(model, node_desc, port_desc) # Draw input on top of the original port field = ui.StringField(visible=False) value_model = field.model value_model.as_string = model[port].name def begin_edit(): field.visible = True def end_edit(value_model): # Hide field.visible = False # Rename model[port].name = value_model.as_string # Activate input with double click stack.set_mouse_double_clicked_fn(lambda x, y, b, m: begin_edit()) value_model.add_end_edit_fn(end_edit) def __on_menu(self, model: "GraphModel", node: Any, port: Any): def disconnect(model: "GraphModel", port: Any): """Disconnect everything from the given port""" model[port].inputs = [] def remove(model: "GraphModel", node: Any, port: Any): """Remove the given port from the node""" ports = model[node].ports ports.remove(port) model[node].ports = ports def move_up(model: "GraphModel", node: Any, port: Any): """Move the given port one position up""" ports: List = model[node].ports index = ports.index(port) if index > 0: ports.insert(index - 1, ports.pop(index)) model[node].ports = ports def move_down(model: "GraphModel", node: Any, port: Any): """Move the given port one position down""" ports: List = model[node].ports index = ports.index(port) if index < len(ports) - 1: ports.insert(index + 1, ports.pop(index)) model[node].ports = ports def move_top(model: "GraphModel", node: Any, port: Any): """Move the given port to the top of the node""" ports: List = model[node].ports index = ports.index(port) if index > 0: ports.insert(0, ports.pop(index)) model[node].ports = ports def move_bottom(model: "GraphModel", node: Any, port: Any): """Move the given port to the bottom of the node""" ports: List = model[node].ports index = ports.index(port) if index < len(ports) - 1: ports.insert(len(ports) - 1, ports.pop(index)) model[node].ports = ports self.__port_context_menu = ui.Menu("CompoundInputOutputNodeDelegate Port Menu") with self.__port_context_menu: if model[port].inputs: ui.MenuItem("Disconnect", triggered_fn=lambda m=model, p=port: disconnect(m, p)) ui.MenuItem("Remove", triggered_fn=lambda m=model, n=node, p=port: remove(m, n, p)) ui.MenuItem("Move Up", triggered_fn=lambda m=model, n=node, p=port: move_up(m, n, p)) ui.MenuItem("Move Down", triggered_fn=lambda m=model, n=node, p=port: move_down(m, n, p)) ui.MenuItem("Move Top", triggered_fn=lambda m=model, n=node, p=port: move_top(m, n, p)) ui.MenuItem("Move Bottom", triggered_fn=lambda m=model, n=node, p=port: move_bottom(m, n, p)) self.__port_context_menu.show()
5,359
Python
38.411764
114
0.605523
omniverse-code/kit/exts/omni.kit.widget.graph/omni/kit/widget/graph/graph_node_delegate_closed.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. # __all__ = ["GraphNodeDelegateClosed"] from .abstract_graph_node_delegate import GraphNodeDescription from .graph_node_delegate_full import GraphNodeDelegateFull from .graph_node_delegate_full import LINE_VISIBLE_MIN from .graph_node_delegate_full import TEXT_VISIBLE_MIN import omni.ui as ui class GraphNodeDelegateClosed(GraphNodeDelegateFull): """ The delegate with the Omniverse design for the nodes of the closed state. """ def __init__(self, scale_factor=1.0): super().__init__(scale_factor) def node_header_input(self, model, node_desc: GraphNodeDescription): """Called to create the left part of the header that will be used as input when the node is collapsed""" node = node_desc.node connected_source = node_desc.connected_source connected_target = node_desc.connected_target with ui.ZStack(width=8, skip_draw_when_clipped=True): ui.Circle( radius=6, name=str(model[node].type), style_type_name_override="Graph.Node.Input", size_policy=ui.CircleSizePolicy.FIXED, alignment=ui.Alignment.RIGHT_CENTER, arc=ui.Alignment.RIGHT, visible_min=TEXT_VISIBLE_MIN, ) if connected_source: # Circle that shows that the port is a source for the connection ui.Circle( radius=7, size_policy=ui.CircleSizePolicy.FIXED, style_type_name_override="Graph.Connection", alignment=ui.Alignment.RIGHT_CENTER, visible_min=TEXT_VISIBLE_MIN, ) if connected_target: # Circle that shows that the port is a target for the connection ui.Circle( radius=5, size_policy=ui.CircleSizePolicy.FIXED, style_type_name_override="Graph.Connection", alignment=ui.Alignment.RIGHT_CENTER, visible_min=TEXT_VISIBLE_MIN, ) def node_header_output(self, model, node_desc: GraphNodeDescription): """Called to create the right part of the header that will be used as output when the node is collapsed""" node = node_desc.node connected_source = node_desc.connected_source connected_target = node_desc.connected_target with ui.ZStack(width=8, skip_draw_when_clipped=True): ui.Circle( radius=6, name=str(model[node].type), style_type_name_override="Graph.Node.Output", size_policy=ui.CircleSizePolicy.FIXED, alignment=ui.Alignment.LEFT_CENTER, visible_min=TEXT_VISIBLE_MIN, ) if connected_source: # Circle that shows that the port is a source for the connection ui.Circle( radius=7, size_policy=ui.CircleSizePolicy.FIXED, style_type_name_override="Graph.Connection", alignment=ui.Alignment.LEFT_CENTER, visible_min=TEXT_VISIBLE_MIN, ) if connected_target: # Circle that shows that the port is a target for the connection ui.Circle( radius=5, size_policy=ui.CircleSizePolicy.FIXED, style_type_name_override="Graph.Connection", alignment=ui.Alignment.LEFT_CENTER, visible_min=TEXT_VISIBLE_MIN, ) def node_header(self, model, node_desc: GraphNodeDescription): """Called to create widgets of the top of the node""" node = node_desc.node with ui.VStack(skip_draw_when_clipped=True): self._common_node_header_top(model, node) style_name = str(model[node].type) # Draw the circle and the image on the top of it with ui.HStack(height=0): with ui.VStack(): ui.Label( "Inputs", alignment=ui.Alignment.CENTER, style_type_name_override="Graph.Node.Port.Label", visible_min=TEXT_VISIBLE_MIN, ) ui.Spacer(height=15) with ui.ZStack(width=50, height=50): ui.Rectangle( style_type_name_override="Graph.Node.Footer", name=style_name, visible_min=TEXT_VISIBLE_MIN ) ui.ImageWithProvider( style_type_name_override="Graph.Node.Footer.Image", name=style_name, visible_min=TEXT_VISIBLE_MIN, ) # Scale up the icon when zooming out with ui.Placer( stable_size=True, visible_min=LINE_VISIBLE_MIN, visible_max=TEXT_VISIBLE_MIN, offset_x=-15, offset_y=-20, ): with ui.ZStack(width=0, height=0): ui.Rectangle( style_type_name_override="Graph.Node.Footer", name=style_name, width=80, height=80 ) ui.ImageWithProvider( style_type_name_override="Graph.Node.Footer.Image", name=style_name, width=80, height=80 ) with ui.VStack(): ui.Label( "Outputs", alignment=ui.Alignment.CENTER, style_type_name_override="Graph.Node.Port.Label", visible_min=TEXT_VISIBLE_MIN, ) ui.Spacer(height=15) ui.Spacer(height=18) def node_footer(self, model, node_desc: GraphNodeDescription): """Called to create widgets of the bottom of the node""" # Don't draw footer pass
6,666
Python
41.196202
120
0.539004
omniverse-code/kit/exts/omni.kit.widget.graph/omni/kit/widget/graph/graph_view.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. # __all__ = [ "GraphView", ] from typing import Any from typing import Dict from typing import List from typing import Optional import asyncio import carb import carb.settings import functools import traceback import omni.kit.app import omni.ui as ui from .abstract_graph_node_delegate import AbstractGraphNodeDelegate from .abstract_graph_node_delegate import GraphConnectionDescription from .graph_layout import SugiyamaLayout from .graph_model import GraphModel from .graph_node import GraphNode from .graph_node_index import GraphNodeDiff, GraphNodeIndex CONNECTION_CURVE = 60 FLOATING_DELTA = 0.00001 def handle_exception(func): """ Decorator to print exception in async functions TODO: The alternative way would be better, but we want to use traceback.format_exc for better error message. result = await asyncio.gather(*[func(*args)], return_exceptions=True) """ @functools.wraps(func) async def wrapper(*args, **kwargs): try: return await func(*args, **kwargs) except asyncio.CancelledError: # We always cancel the task. It's not a problem. pass except Exception as e: carb.log_error(f"Exception when async '{func}'") carb.log_error(f"{e}") carb.log_error(f"{traceback.format_exc()}") return wrapper class GraphView: """ The visualisation layer of omni.kit.widget.graph. It behaves like a regular widget and displays nodes and their connections. """ DEFAULT_DISTANCE_BETWEEN_NODES = 50.0 class _Proxy: """ A service proxy object to keep the given object in a central location. It's used to keep the delegate and this object is passed to the nodes, so when the delegate is changed, it's automatically updated in all the nodes. TODO: Add `set_delegate`. Otherwise it's useless. """ def __init__(self, reference=None): self.set_object(reference) def __getattr__(self, attr): """ Called when the default attribute access fails with an AttributeError. """ return getattr(self._ref, attr) def __setattr__(self, attr, value): """ Called when an attribute assignment is attempted. This is called instead of the normal mechanism (i.e. store the value in the instance dictionary). """ setattr(self._ref, attr, value) def set_object(self, reference): """Replace the object this proxy holds with the new one""" super().__setattr__("_ref", reference) class _Event(set): """ A list of callable objects. Calling an instance of this will cause a call to each item in the list in ascending order by index. """ def __call__(self, *args, **kwargs): """Called when the instance is “called” as a function""" # Call all the saved functions for f in self: f(*args, **kwargs) def __repr__(self): """ Called by the repr() built-in function to compute the “official” string representation of an object. """ return f"Event({set.__repr__(self)})" class _EventSubscription: """ Event subscription. _Event has callback while this object exists. """ def __init__(self, event, fn): """ Save the function, the event, and add the function to the event. """ self._fn = fn self._event = event event.add(self._fn) def __del__(self): """Called by GC.""" self._event.remove(self._fn) def __init__(self, **kwargs): """ ### Keyword Arguments: `model : GraphModel` Model to display the node graph `delegate : AbstractGraphNodeDelegate` Delegate to draw the node `virtual_ports : bool` True when the model should use reversed output for better look of the graph. `port_grouping : bool` True when the widget should use sub-ports for port grouping. All other kwargs are passed to CanvasFrame which is the root widget. """ # Selected nodes self.__selection = [] self.__build_task = None self._node_widgets = {} self._connection_widgets = {} self._node_placers = {} self._delegate = self._Proxy() # Regenerate the cache index when true self._force_regenerate = True self.__reference_style = kwargs.get("style", None) self._model = None self.set_model(kwargs.pop("model", None)) self.set_delegate(kwargs.pop("delegate", None)) self.__virtual_ports = kwargs.pop("virtual_ports", False) self.__port_grouping = kwargs.pop("port_grouping", False) self.__rectangle_selection = kwargs.pop("rectangle_selection", False) self.__connections_on_top = kwargs.pop("connections_on_top", False) self.__allow_same_side_connections = kwargs.pop("allow_same_side_connections", False) self.__always_force_regenerate = kwargs.pop("always_force_regenerate", True) # The variable raster_nodes is an experimental feature that can improve # performance by rasterizing all the nodes when it is set to true. # However, it is important to note that this feature may not always # behave as expected and may cause issues with the editor. It is # recommended to use this feature with caution and thoroughly test any # changes before deploying them. settings = carb.settings.get_settings() self.__raster_nodes = kwargs.pop("raster_nodes", None) if self.__raster_nodes is None: self.__raster_nodes = settings.get("/exts/omni.kit.widget.graph/raster_nodes") if self.__raster_nodes: kwargs["compatibility"] = False self._graph_node_index = GraphNodeIndex(None, self.__port_grouping) if "style_type_name_override" not in kwargs: kwargs["style_type_name_override"] = "Graph" # The nodes for filtering upstrem. self._filtering_nodes = None # Nodes & Connections to force draw self._force_draw_nodes = [] with ui.ZStack(): # Capturing selection if self.__rectangle_selection: ui.Spacer( mouse_pressed_fn=self.__rectangle_selection_begin, mouse_released_fn=self.__rectangle_selection_end, mouse_moved_fn=self.__rectangle_selection_moved, ) # The graph canvas self.__root_frame = ui.CanvasFrame(**kwargs) self.__root_stack = None # Drawing selection self.__selection_layer = ui.Frame(separate_window=True, visible=False) with self.__selection_layer: with ui.ZStack(): self.__selection_placer_start = ui.Placer(draggable=True) self.__selection_placer_end = ui.Placer(draggable=True) with self.__selection_placer_start: rect_corner_start = ui.Spacer(width=1, height=1) with self.__selection_placer_end: rect_corner_end = ui.Spacer(width=1, height=1) # The rectangle that is drawn when selection with the rectangle ui.FreeRectangle(rect_corner_start, rect_corner_end, style_type_name_override="Graph.Selecion.Rect") # Build the network self.__on_item_changed(None) # ZStack with connections to be able to add connections to the layout self.__connections_stack = None # The frame that follows mouse cursor when the user creates a new connection self.__user_drag_connection_frame = None # The frame with the temporary connection that is sticky to the port. # We need it to demonstrate that the connection is valid. self.__user_drag_connection_accepted_frame = None # The source port when the user creates a new connection self.__making_connection_source_node = None self.__making_connection_source = None self.__making_connection_source_widget = None # The target port when the user creates a new connection self.__making_connection_target_node = None self.__making_connection_target = None # Dict that has a port as a key and two frames with port widgets self.__port_to_frames = {} # Nodes that will be dragged once the mouse is moved self.__nodes_to_drag = [] # Nodes that are currently dragged self.__nodes_dragging = [] self.__can_connect = False self.__position_changed = False self.__on_post_delayed_build_layout = self._Event() self.__on_pre_delayed_build_layout = self._Event() # Selection caches # Position when the user started selection self.__rectangle_selection_start_position = None # Positions of the nodes for selection caches self.__positions_cache = None # Selection when the user started rectangle self.__selection_cache = [] # Flag for async method to deselect all. See # `_clear_selection_next_frame_async` for details. self.__need_clear_selection = False def __getattr__(self, attr): """Pretend it's self.__root_frame""" return getattr(self.__root_frame, attr) def _post_delayed_build_layout(self): """Call the event object that has the list of functions""" self.__on_post_delayed_build_layout() def subscribe_post_delayed_build_layout(self, fn): """ Return the object that will automatically unsubscribe when destroyed. """ return self._EventSubscription(self.__on_post_delayed_build_layout, fn) def _pre_delayed_build_layout(self): """Call the event object that has the list of functions""" self.__on_pre_delayed_build_layout() def subscribe_pre_delayed_build_layout(self, fn): """ Return the object that will automatically unsubscribe when destroyed. """ return self._EventSubscription(self.__on_pre_delayed_build_layout, fn) def layout_all(self): """Reset positions of all the nodes in the model""" # Turn force_regenerate on whenever calling layout_all, so it doesn't always # need to be called right before the layout_all call. self._force_regenerate = True for node in self._model.nodes: self._model[node].position = None self.__on_item_changed(None) def set_expansion(self, state: GraphModel.ExpansionState): """ Open, close or minimize all the nodes in the model. """ for node in self._model.nodes: self._model[node].expansion_state = state @property def raster_nodes(self): # Read only return self.__raster_nodes @property def model(self): return self._model @model.setter def model(self, model): self._force_regenerate = True self.set_model(model) @property def virtual_ports(self): """ Typically source connections go from the left side of the node to the right side of the node. But sometimes it looks messy when circular connections. When `virtual_ports` is true, and the connection is circular, the view draws it from the right side to the left side. Example: A.out -------------> B.surface A.color [REVERSED] <- B.color """ return self.__virtual_ports @virtual_ports.setter def virtual_ports(self, value): self.__virtual_ports = not not value self.__on_item_changed(None) def set_model(self, model: GraphModel): """Replace the model of the widget. It will refresh all the content.""" self._filtering_nodes = None if self._model: self._model.destroy() self._model = model # Re-subscribe if self._model: self._model_subscription = self._model.subscribe_item_changed(self.__on_item_changed) self._selection_subscription = self._model.subscribe_selection_changed(self.__on_selection_changed) self._node_subscription = self._model.subscribe_node_changed(self.__rebuild_node) else: self._model_subscription = None self._selection_subscription = None self._node_subscription = None self.__on_item_changed(None) self.__on_selection_changed() def set_delegate(self, delegate: AbstractGraphNodeDelegate): """Replace the delegate of the widget""" self._force_regenerate = True self._delegate.set_object(delegate) self.__on_item_changed(None) def filter_upstream(self, nodes: list): """Remove nodes that are not upstream of the given nodes""" self._filtering_nodes = nodes self.__on_item_changed(None) def get_bbox_of_nodes(self, nodes: list): """Get the bounding box of nodes""" if not nodes: nodes = self._model.nodes if not nodes: return min_pos_x = None min_pos_x_node = None min_pos_y = None min_pos_y_node = None max_pos_x = None max_pos_x_node = None max_pos_y = None max_pos_y_node = None for node in nodes: if node not in self._node_widgets: continue pos = self._model[node].position if pos is None: continue if min_pos_x is None: min_pos_x = pos[0] min_pos_x_node = node min_pos_y = pos[1] min_pos_y_node = node max_pos_x = pos[0] max_pos_x_node = node max_pos_y = pos[1] max_pos_y_node = node continue if pos[0] < min_pos_x: min_pos_x = pos[0] min_pos_x_node = node previous_max_max_x = max_pos_x + self._node_widgets[max_pos_x_node].computed_width if pos[0] + self._node_widgets[node].computed_width < previous_max_max_x: pass else: max_pos_x = pos[0] max_pos_x_node = node if pos[1] < min_pos_y: min_pos_y = pos[1] min_pos_y_node = node previous_max_max_y = max_pos_y + self._node_widgets[max_pos_y_node].computed_height if pos[1] + self._node_widgets[node].computed_height < previous_max_max_y: pass else: max_pos_y = pos[1] max_pos_y_node = node if max_pos_x_node in self._node_widgets: computed_width = (max_pos_x + self._node_widgets[max_pos_x_node].computed_width) - min_pos_x computed_height = (max_pos_y + self._node_widgets[max_pos_y_node].computed_height) - min_pos_y else: min_pos_x = 0 min_pos_y = 0 computed_height = 500 computed_width = 500 return computed_width, computed_height, min_pos_x, min_pos_y def focus_on_nodes(self, nodes: Optional[List[Any]] = None): """Focus the view on nodes""" if not nodes: nodes = self._model.nodes if not nodes: return computed_width, computed_height, min_pos_x, min_pos_y = self.get_bbox_of_nodes(nodes) # TODO: Looks like it's a bug of ui.CanvasFrame.computed_width. But it works for now. if self.__raster_nodes: canvas_computed_width = self.__root_frame.computed_width canvas_computed_height = self.__root_frame.computed_height else: canvas_computed_width = self.__root_frame.computed_width * self.zoom canvas_computed_height = self.__root_frame.computed_height * self.zoom zoom = min([canvas_computed_width / computed_width, canvas_computed_height / computed_height]) # We need to clamp the zoom with zoom_min and zoom_max here since we need to update the zoom of graphView and # use the clamped zoom to compute self.pan_x and self.pan_y # this FLOATING_DELTA is needed to avoid floating issue zoom = max(min(zoom, self.zoom_max - FLOATING_DELTA), self.zoom_min + FLOATING_DELTA) self.zoom = zoom self.pan_x = -min_pos_x * zoom + canvas_computed_width / 2 - computed_width * zoom / 2 self.pan_y = -min_pos_y * zoom + canvas_computed_height / 2 - computed_height * zoom / 2 async def restore_smooth_async(): """Set smooth_zoom to True""" await omni.kit.app.get_app().next_update_async() self.__root_frame.smooth_zoom = True if self.smooth_zoom: # Temporarly disable smooth_zoom self.__root_frame.smooth_zoom = False asyncio.ensure_future(restore_smooth_async()) @property def selection(self): """Return selected nodes""" return self.__selection @selection.setter def selection(self, value): """Set selection""" if self._model: self._model.selection = value def __rebuild_node(self, item, full=False): """Rebuild the delegate of the node, this could be used to just update the look of one node""" if item: if full: self._force_draw_nodes.append(item) # Still need to call __delayed_build_layout where this is called. return node = self._node_widgets[item] if node: # Doesn't rebuild ports or connections node.rebuild_layout() def __on_item_changed(self, item): """Called by the model when something is changed""" if item is not None: # Only one item is changed. Not necessary to rebuild the whole network. placer = self._node_placers.get(item, None) if placer: position = self._model[item].position if position: placer.offset_x = position[0] placer.offset_y = position[1] placer.invalidate_raster() # TODO: Rebuild the node return # The whole graph is changed. Rebuild if self.__build_task: self.__build_task.cancel() # Build the layout in the next frame because it's possible that # multiple items is changed and in this case we still need to execute # __build_layouts once. self.__build_task = asyncio.ensure_future(self.__delayed_build_layout()) def __on_selection_changed(self): """Called by the model when selection is changed""" if not self._model: return # Deselect existing selection for node in self.__selection: widget = self._node_widgets.get(node, None) if not widget: continue widget.selected = False # Keep only nodes if we have a widget for them. We need it to skip # nodes that are selected in model but they are filtered out here self.__selection = [] for model_selection_node in self._model.selection or []: for widget_node in self._node_widgets: if model_selection_node == widget_node: self.__selection.append(widget_node) # Select new selection for node in self.__selection: widget = self._node_widgets.get(node, None) if not widget: continue widget.selected = True def __cache_positions(self): """Creates cache for all the positions of all the nodes""" if self.__positions_cache is not None: return class PositionCache: def __init__(self, node: Any, pos: List[float], size: List[float]): self.node = node self.__x0 = pos[0] self.__y0 = pos[1] self.__x1 = pos[0] + size[0] self.__y1 = pos[1] + size[1] def __repr__(self): return f"<PositionCache {self.node} {self.__x0} {self.__y0} {self.__x1} {self.__y1}" def is_in_rect(self, x_min: float, y_min: float, x_max: float, y_max: float): return not (x_max < self.__x0 or x_min > self.__x1 or y_max < self.__y0 or y_min > self.__y1) self.__positions_cache = [] for node in self._model.nodes or []: # NOTE: If we ever have selection objects that aren't positioned in the top left corner of the # node (like the header), we will need to get the select_widget's position instead, here. pos = self._model[node].position if pos: widget = self._node_widgets.get(node, None) if not widget: continue select_widget = self._model.special_select_widget(node, widget) or widget size = [select_widget.computed_width, select_widget.computed_height] self.__positions_cache.append(PositionCache(node, pos, size)) self.__selection_cache = self.model.selection or [] async def _clear_selection_next_frame_async(self, model): """ Called by background layer to clear selection. The idea is that if no node cancels it, then the yser clicked in the background and we need to deselect all. """ await omni.kit.app.get_app().next_update_async() if self.__need_clear_selection: model.selection = [] def _on_rectangle_select(self, model, start, end, modifier=0): """Called when the user is performing the rectangle selection""" # this function could be triggered when we multi-selected nodes and move one of them too quickly. # in this case, we don't want to update the selection. if start == end: return # Don't clear selection frame after self.__need_clear_selection = False self.__cache_positions() x_min = min(start[0], end[0]) x_max = max(start[0], end[0]) y_min = min(start[1], end[1]) y_max = max(start[1], end[1]) selection = [] for position_cache in self.__positions_cache: if position_cache.is_in_rect(x_min, y_min, x_max, y_max): selection.append(position_cache.node) if modifier & carb.input.KEYBOARD_MODIFIER_FLAG_SHIFT: # Add to the current selection selection = self.__selection_cache + [s for s in selection if s not in self.__selection_cache] elif modifier & carb.input.KEYBOARD_MODIFIER_FLAG_CONTROL: # Subtract from the current selection selection = [s for s in self.__selection_cache if s not in selection] model.selection = selection def _on_node_selected(self, model, node, modifier=0, pressed=True): """Called when the user is picking to select nodes""" # Stop rectangle selection self.__rectangle_selection_start_position = None # Don't clear selection frame after self.__need_clear_selection = False selection = model.selection if selection is None: return # Copy selection = selection[:] if modifier & carb.input.KEYBOARD_MODIFIER_FLAG_SHIFT: if not pressed: return # Add if node not in selection: selection.append(node) elif modifier & carb.input.KEYBOARD_MODIFIER_FLAG_CONTROL: if not pressed: return # Add / Remove if node in selection: selection.remove(node) else: selection.append(node) else: if pressed and node in selection: return # Set selection = [node] model.selection = selection @handle_exception async def __delayed_build_layout(self): """ Rebuild all the nodes and connections in the next update cycle. It's delayed because it's possible that it's called multiple times a frame. And we need to create all the widgets only one. The challenge here is the ability of USD making input-to-input and output-to-output connections. In USD it's perfectly fine to have the following network: A.out --> B.out A.in <-- B.in So it doesn't matter if the port is input or output. To draw such connections properly and to know which side of the node it should connect, we need to find out the direction of the flow in this node network. This is the overview of the algorithm to trace the nodes and compute the direction of the flow. STEP 1. Create the cache of the model and indices to be able to access the cache fast. STEP 2. Scan all the nodes and find roots. A root is a node that doesn't have an output connected to another node. STEP 3. Trace connections of roots and assign them level. Level is the distance of the node from the root. The root has level 0. The nodes connected to root has level one. The next connected nodes have level 2 etc. We assume that level is the flow direction. The flow goes from nodes to root. STEP 4. It's easy to check if the connection goes to the direction opposite to flow. Such connections have virtual ports. """ self._pre_delayed_build_layout() await omni.kit.app.get_app().next_update_async() # STEP 1 # Small explanation on input/output, source/target. Input and output is # just a USD tag. We inherit this terminology to be USD compliant. # Input/output is not the direction of the data flow. To express the # data flow direction we use source/target. In USD it's possible to # have input-to-input and output-to-output connections. graph_node_index = GraphNodeIndex(self._model, self.__port_grouping) if self.__always_force_regenerate or self._force_regenerate: self._force_regenerate = False regenerate_all = True else: # Diff diff = self._graph_node_index.get_diff(graph_node_index) regenerate_all = not diff.valid if regenerate_all: self._graph_node_index = graph_node_index # Clean up the previous nodes for _, node_widget in self._node_widgets.items(): node_widget.destroy() for _, connection_widget in self._connection_widgets.items(): connection_widget.destroy() self._node_widgets = {} self._connection_widgets = {} self.__port_to_frames = {} else: if self._force_draw_nodes: self.__add_force_redraws_to_diff(diff, graph_node_index) nodes_add, nodes_del, connections_add, connections_del = self._graph_node_index.mutate(diff) # Utility to access the index def get_node_level_from_port(port, port_to_id, all_cached_nodes): if port is None: return 0 node_id = port_to_id.get(port, None) if node_id is None: return 0 return all_cached_nodes[node_id].level # List of all the nodes from the model. Members are CacheNode objects all_cached_nodes = self._graph_node_index.cached_nodes all_cached_connections = self._graph_node_index.cached_connections # Dictionary that has a port from the model as a key and the index of the parent node in all_cached_nodes. port_to_id = self._graph_node_index.port_to_id # Dictionary that has a port from the model as a key and a flag if this port is output. We need it to detect # the flow direction. ports_used_as_output = self._graph_node_index.ports_used_as_output # All the connections. Usded to determine if we need to draw output circle. source_to_target = self._graph_node_index.source_to_target # Dict[child port: parent port] port_child_to_parent: Dict[Any, Any] = self._graph_node_index.port_child_to_parent if not self._model: return # STEP 2 # True if the node in all_cached_nodes is a root node. The node A is a root node if other nodes are not # connected to outputs of A. is_root_node = [True] * len(all_cached_nodes) for cache_node in all_cached_nodes: if not cache_node: continue if not regenerate_all and cache_node.node not in nodes_add: continue for source in cache_node.inputs + cache_node.outputs: if source not in ports_used_as_output: continue # The source node is not root because it has output connected to this node source_node_id = port_to_id.get(source, None) if source_node_id is None: continue is_root_node[source_node_id] = False if self._filtering_nodes: root_nodes = [cached_node for cached_node in all_cached_nodes if cached_node.node in self._filtering_nodes] else: root_nodes = [cached_node for cached_node, is_root in zip(all_cached_nodes, is_root_node) if is_root] if not root_nodes and all_cached_nodes: # We have a circular connected network. In this way it doesn't matter which node to pick, but we assume that # the first node of the model will be the root node. root_nodes = [all_cached_nodes[0]] # STEP 3 # Create the list of edges. We need to put them into layout. edges = [] edges_sorted = [] for i, cache_node in enumerate(all_cached_nodes): if not cache_node: continue if not regenerate_all and cache_node.node not in nodes_add: continue for source in cache_node.inputs + cache_node.outputs: source_node_id = port_to_id.get(source, None) if source_node_id is None: # try to resolve id of parent instead parent_source = port_child_to_parent.get(source, None) if parent_source is not None: source_node_id = port_to_id.get(parent_source, None) if source_node_id is None: continue v1 = source_node_id v2 = i edge_sorted = (v1, v2) if v1 < v2 else (v2, v1) if v1 != v2 and edge_sorted not in edges_sorted: edges.append((v2, v1)) edges_sorted.append(edge_sorted) if regenerate_all: self.layout = SugiyamaLayout(edges=edges, vertical_distance=20.0, horizontal_distance=200.0) for i, cache_node in enumerate(all_cached_nodes): if not cache_node: continue if not regenerate_all and cache_node.node not in nodes_add: continue level = self.layout.get_layer(i) # If level is None, it means the node is not connected to anything. We assume its level is 0. cache_node.level = level or 0 # STEP 4. Generate widgets. node_and_level = [] if self.__root_stack is None or regenerate_all: with self.__root_frame: self.__root_stack = ui.ZStack() if regenerate_all: self.__connections_stack = None if not regenerate_all: # Delete nodes for node in nodes_del: self._node_widgets[node].visible = False self._node_widgets[node].destroy() # Delete connections for connection in connections_del: connection_widget = self._connection_widgets[connection] connection_widget.visible = False connection_widget.clear() connection_widget.destroy() # If 1 to keep indentation. Otherwise indentation is changed, it's bad # for code review. if 1: with self.__root_stack: # Save the connections. It's the list of tuples (target_port, source_port) for node_id, cached_node in sorted( enumerate(n for n in all_cached_nodes if n), key=lambda a: a[1].stacking_order ): if not regenerate_all and cached_node.node not in nodes_add: continue stacking_order = cached_node.stacking_order # Create ZStack for connections between -1 and 0 stacking # order. It allows to put the connections under nodes and # over the backdrops. if not self.__connections_on_top and not self.__connections_stack and stacking_order >= 0: with ui.Frame(separate_window=True): self.__connections_stack = ui.ZStack() expansion_state = self._model[cached_node.node].expansion_state # Filtered ports ports = [] # Two bools per port: # (True if it's target, True if it's source) # So the connection can have four states: input/output and # source/target. They can be mixed every possible way. port_input = [] # Two bools per port # (True if it's target, True if it's source) port_output = [] port_levels: List[int] = [] port_position: List[int] = [] parent_child_count: List[int] = [] node_input = (False, False) node_output = (False, False) # Save connections and check if the node has virtual input/output for cached_port in cached_node.cached_ports: if not cached_port.visibile: continue target_port = cached_port.port # Or it happens when the node is minimized # TODO: compute it when computing port_visibility if ( expansion_state == GraphModel.ExpansionState.MINIMIZED or expansion_state == GraphModel.ExpansionState.CLOSED ): if not cached_port.inputs and not cached_port.outputs: if target_port not in source_to_target: # Filter out ports with no connections continue # True if the port has input (left side) connection # from this port to another input_is_source_connection = False # True if the port has input (left side) connection # from another port to this port input_is_target_connection = False # True if the port has output (right side) connection # from this port to another output_is_source_connection = False # True if the port has output (right side) connection # from another port to this port output_is_target_connection = False if cached_port.inputs: for source_port in cached_port.inputs: if source_port not in port_to_id: carb.log_warn( f"[Graph UI] The port {target_port} can't be connected to the port " f"{source_port} because it doesn't exist in the model" ) continue if ( self.virtual_ports and get_node_level_from_port(source_port, port_to_id, all_cached_nodes) < cached_node.level ): # The input port is connected from the downflow node # # Example: # A.out -------------------------> B.surface # A.color [checking this option] <- B.color output_is_target_connection = True else: # Example: # A.out -> [checking this option] B.in input_is_target_connection = True if cached_port.outputs: for source_port in cached_port.outputs: if source_port not in port_to_id: carb.log_warn( f"[Graph UI] The port {target_port} can't be connected to the port " f"{source_port} because it doesn't exist in the model" ) continue if ( self.virtual_ports and get_node_level_from_port(source_port, port_to_id, all_cached_nodes) < cached_node.level ): output_is_target_connection = True else: input_is_target_connection = True # The output port has input connection. A.out -> [checking this option] B.out if target_port in source_to_target: for t in source_to_target[target_port]: compare_level = get_node_level_from_port(t, port_to_id, all_cached_nodes) if ( self.virtual_ports and compare_level is not None and cached_node.level < compare_level ): # Example: # A.out -------------------------> B.surface # A.color <- [checking this option] B.color input_is_source_connection = True else: # A.out [checking this option] -> B.in output_is_source_connection = True node_input = ( node_input[0] or input_is_source_connection, node_input[1] or input_is_target_connection, ) node_output = ( node_output[0] or output_is_source_connection, node_output[1] or output_is_target_connection, ) if expansion_state == GraphModel.ExpansionState.CLOSED: continue # Ports ports.append(target_port) port_input.append((input_is_source_connection, input_is_target_connection)) port_output.append((output_is_source_connection, output_is_target_connection)) port_levels.append(cached_port.level) port_position.append(cached_port.relative_position) parent_child_count.append(cached_port.parent_child_count) # Rasterize the nodes. This is an experimental feature that # can improve performance. raster_policy = ui.RasterPolicy.AUTO if self.__raster_nodes else ui.RasterPolicy.NEVER # The side effect of having level, is the possibility to put the node to the correct position. placer = ui.Placer( draggable=True, frames_to_start_drag=2, name="node_graph", raster_policy=raster_policy ) # Set position if possible position = self._model[cached_node.node].position if position: placer.offset_x = position[0] placer.offset_y = position[1] def save_position(placer, model, node): if self.__nodes_to_drag: for drag_node in self.__nodes_to_drag: model.position_begin_edit(drag_node) self.__nodes_dragging[:] = self.__nodes_to_drag[:] self.__nodes_to_drag[:] = [] # Without this, unselected backdrops can still be dragged by their placer if not self.__nodes_dragging: if model[node].position: placer.offset_x.value, placer.offset_y.value = model[node].position return node_position = (placer.offset_x.value, placer.offset_y.value) model[node].position = node_position self.__position_changed = True placer.set_offset_x_changed_fn( lambda _, p=placer, m=self._model, n=cached_node.node: save_position(p, m, n) ) placer.set_offset_y_changed_fn( lambda _, p=placer, m=self._model, n=cached_node.node: save_position(p, m, n) ) with placer: # The node widget node_widget = GraphNode( self._model, cached_node.node, node_input, node_output, list(zip(ports, port_input, port_output, port_levels, port_position, parent_child_count)), self._delegate, ) def position_begin_edit(model, node, modifier): self.__nodes_to_drag[:] = [node] self.__nodes_dragging[:] = [] self.__position_changed = False def position_end_edit(model, node, modifier): self.__nodes_to_drag[:] = [] for drag_node in self.__nodes_dragging: model.position_end_edit(drag_node) self.__nodes_dragging[:] = [] return self.__position_changed select_widget = self._model.special_select_widget(cached_node.node, node_widget) or node_widget # Select node select_widget.set_mouse_pressed_fn( lambda x, y, b, modifier, model=self._model, node=cached_node.node: b == 0 and ( position_begin_edit(model, node, modifier) or self._on_node_selected(model, node, modifier, True) ) ) select_widget.set_mouse_released_fn( lambda x, y, b, modifier, model=self._model, node=cached_node.node: b == 0 and ( position_end_edit(model, node, modifier) or self._on_node_selected(model, node, modifier, False) ) ) # Save the node so it's not removed self._node_widgets[cached_node.node] = node_widget self._node_placers[cached_node.node] = placer node_and_level.append((cached_node.node, node_widget, placer, node_id)) # Save the ports for fast access if expansion_state == GraphModel.ExpansionState.CLOSED: for cached_port in cached_node.cached_ports: self.__port_to_frames[cached_port.port] = ( node_widget.header_input_frame, node_widget.header_output_frame, ) else: self.__port_to_frames.update(node_widget.ports) for port, input_output in node_widget.ports.items(): input_port_widget, output_port_widget = input_output if input_port_widget: # Callback when mouse enters/leaves the port widget input_port_widget.set_mouse_hovered_fn( lambda h, n=cached_node.node, p=port, w=input_port_widget: self.__on_port_hovered( n, p, w, h ) ) input_port_widget.set_mouse_pressed_fn( lambda x, y, b, m, n=cached_node.node, p=port: self._on_port_context_menu(n, p) if b == 1 else None ) if self.__allow_same_side_connections and output_port_widget: # Callback when mouse enters/leaves the port widget output_port_widget.set_mouse_hovered_fn( lambda h, n=cached_node.node, p=port, w=output_port_widget: self.__on_port_hovered( n, p, w, h ) ) for port, input_output in node_widget.user_drag.items(): _, drag_widget = input_output port_widget = self.__port_to_frames.get(port, (None, None))[1] # Callback when user starts doing a new connection drag_widget.set_mouse_pressed_fn( lambda *_, f=port_widget, t=drag_widget, n=cached_node.node, p=port: self.__on_start_connection( n, p, f, t ) ) # Callback when user finishes doing a new connection drag_widget.set_mouse_released_fn(lambda *_: self._on_new_connection()) # The connections ZStack is not created if all the nodes are under connections if not self.__connections_stack: with ui.Frame(separate_window=True): self.__connections_stack = ui.ZStack() # Buffer to keep the lines already built. We use it for filtering. built_lines = set() # Build all the connections for connection in all_cached_connections: if not connection: continue if not regenerate_all and connection not in connections_add: continue target = connection.target_port source = connection.source_port target_node_id = port_to_id.get(target, None) target_cached_node = all_cached_nodes[target_node_id] target_level = target_cached_node.level source_node_id = port_to_id.get(source, None) source_cached_node = all_cached_nodes[source_node_id] source_level = source_cached_node.level # Check if the direction of this connection is the same as flow if self.virtual_ports: is_reversed_connection = source_level < target_level else: is_reversed_connection = False target_node = target_cached_node.node source_node = source_cached_node.node # 0 means the frame from input (left side). 1 is output (right side). target_frames = self.__port_to_frames.get(target, None) source_frames = self.__port_to_frames.get(source, None) if self.__allow_same_side_connections and target_node == source_node: is_reversed_target = True is_reversed_source = False f1 = target_frames[1] f2 = source_frames[1] else: is_reversed_target = is_reversed_connection is_reversed_source = is_reversed_connection f1 = target_frames[1 if is_reversed_connection else 0] f2 = source_frames[0 if is_reversed_connection else 1] # Filter out lines already built. line1 = (f1, f2) line2 = (f2, f1) if line1 in built_lines or line2 in built_lines: continue built_lines.add(line1) # Objects passed to the delegate source_connection_description = GraphConnectionDescription( source_node, source, f2, source_level, is_reversed_source ) target_connection_description = GraphConnectionDescription( target_node, target, f1, target_level, is_reversed_target ) with self.__connections_stack: connection_frame = ui.Frame() self._connection_widgets[connection] = connection_frame with connection_frame: self._delegate.connection( self._model, source_connection_description, target_connection_description ) with self.__connections_stack: self.__user_drag_connection_frame = ui.Frame() self.__user_drag_connection_accepted_frame = ui.Frame() self.__making_connection_source_node = None self.__making_connection_source = None self.__making_connection_source_widget = None # Init selection state self.__on_selection_changed() # Arrange node position in the next frame because now the node is not # created and it's not possible to get its size. if not node_and_level: self._post_delayed_build_layout() return await self.__arrange_nodes(node_and_level) self._post_delayed_build_layout() def __add_force_redraws_to_diff(self, diff: GraphNodeDiff, graph_node_index: GraphNodeIndex): """ Add the force_redraw_nodes, if any, by adding them to both the nodes_to_add and nodes_to_del sides. Also do the same with any connections to the node(s). By deleting and then adding, it forces a redraw. """ cached_add_connections = set() cached_del_connections = set() for force_node in self._force_draw_nodes: # Add node to nodes_to_add if force_node in graph_node_index.node_to_id: force_add_cache_node = graph_node_index.cached_nodes[ graph_node_index.node_to_id[force_node] ] if force_add_cache_node not in diff.nodes_to_add: diff.nodes_to_add.append(force_add_cache_node) # Add any inputs or outputs for c_port in force_add_cache_node.cached_ports: # Check for outputs source = c_port.port targets = graph_node_index.source_to_target.get(c_port.port, []) for t in targets: if (source, t) in graph_node_index.connection_to_id: cached_add_connection = graph_node_index.cached_connections[ graph_node_index.connection_to_id[(source, t)] ] cached_add_connections.add(cached_add_connection) if (source, t) in self._graph_node_index.connection_to_id: cached_del_connection = self._graph_node_index.cached_connections[ self._graph_node_index.connection_to_id[(source, t)] ] cached_del_connections.add(cached_del_connection) # Check for inputs if not targets: target = source if c_port.inputs: for inp in c_port.inputs: source = inp if (source, target) in graph_node_index.connection_to_id: cached_add_connection = graph_node_index.cached_connections[ graph_node_index.connection_to_id[(source, target)] ] cached_add_connections.add(cached_add_connection) if (source, target) in self._graph_node_index.connection_to_id: cached_del_connection = self._graph_node_index.cached_connections[ self._graph_node_index.connection_to_id[(source, target)] ] cached_del_connections.add(cached_del_connection) # Add node to nodes_to_del if force_node in self._graph_node_index.node_to_id: force_del_cache_node = self._graph_node_index.cached_nodes[ self._graph_node_index.node_to_id[force_node] ] if force_del_cache_node not in diff.nodes_to_del: diff.nodes_to_del.append(force_del_cache_node) # Add all connections to the diff for c_connection in cached_add_connections: if c_connection not in diff.connections_to_add: diff.connections_to_add.append(c_connection) for c_connection in cached_del_connections: if c_connection not in diff.connections_to_del: diff.connections_to_del.append(c_connection) self._force_draw_nodes = [] async def __arrange_nodes(self, node_and_level): """ Set the the position of the nodes. If the node doesn't have a predefined position (TODO), the position is assigned automatically. It's async because it waits until the node is created to get its size. """ # Wait untill the node appears on the screen. while node_and_level[0][1].computed_width == 0.0: await omni.kit.app.get_app().next_update_async() # Set size for _, node_widget, _, layout_node_id in node_and_level: self.layout.set_size(layout_node_id, node_widget.computed_width, node_widget.computed_height) # Recompute positions self.layout.update_positions() # Set positions in graph view for node, node_widget, placer, layout_node_id in node_and_level: model_position = self._model[node].position self._model[node].size = (node_widget.computed_width, node_widget.computed_height) if not model_position: # The model doesn't have the position. Set it. model_position = self.layout.get_position(layout_node_id) if model_position: self._model[node].position = model_position placer.offset_x = model_position[0] placer.offset_y = model_position[1] def __on_start_connection(self, node, port, from_widget, to_widget): """Called when the user starts creating connection""" # Keep the source port self.__making_connection_source_node = node self.__making_connection_source = port self.__making_connection_source_widget = from_widget self.__user_drag_connection_frame.visible = True self.__user_drag_connection_accepted_frame.visible = False with self.__user_drag_connection_frame: if from_widget and to_widget: # Temporary connection line follows the mouse # Objects passed to the delegate source_connection_description = GraphConnectionDescription(node, port, from_widget, 0, False) target_connection_description = GraphConnectionDescription(None, None, to_widget, 0, False) self._delegate.connection(self._model, source_connection_description, target_connection_description) else: # Dummy widget will destroy the temporary connection line if it exists ui.Spacer() def __on_port_hovered(self, node, port, widget, hovered): """Called when the mouse pointer enters the area of the port""" if not self.__making_connection_source: # We are not in connection mode, return if self.__making_connection_target: self.__making_connection_target = None if self.__making_connection_target_node: self.__making_connection_target_node = None return # We are here because the user is trying to connect ports if hovered: # The user entered the port widget self.__making_connection_target_node = node self.__making_connection_target = port self.__can_connect = self._model.can_connect(self.__making_connection_source, port) if self.__allow_same_side_connections and node == self.__making_connection_source_node: is_reversed_source = False is_reversed_target = True else: is_reversed_source = False is_reversed_target = False # Replace connection with the sticky one if self.__can_connect: # Show sticky connection and hide the one that follows the mouse self.__user_drag_connection_frame.visible = False self.__user_drag_connection_accepted_frame.visible = True with self.__user_drag_connection_accepted_frame: # Temporary connection line sticked to the port # Objects passed to the delegate source_connection_description = GraphConnectionDescription( self.__making_connection_source_node, self.__making_connection_source, self.__making_connection_source_widget, 0, is_reversed_source, ) target_connection_description = GraphConnectionDescription( self.__making_connection_target_node, self.__making_connection_target, widget, 0, is_reversed_target, ) self._delegate.connection(self._model, source_connection_description, target_connection_description) elif self.__making_connection_target == port: # Check that mouse left the currently hovered port because # sometimes __on_port_hovered gets called like this: # __on_port_hovered("A", 1) # __on_port_hovered("B", 1) # __on_port_hovered("A", 0) self.__making_connection_target_node = None self.__making_connection_target = None # Hide sticky connection and show the one that follows the mouse self.__user_drag_connection_frame.visible = True self.__user_drag_connection_accepted_frame.visible = False def __disconnect_inputs(self, port): """Remove connections from port""" self._model[port].inputs = [] def __canvas_space(self, x: float, y: float): """Convert mouse to canvas space""" if self.__raster_nodes: return self.__root_frame.screen_to_canvas_x(x), self.__root_frame.screen_to_canvas_y(y) offset_x = x - self.__root_frame.screen_position_x * self.zoom offset_y = y - self.__root_frame.screen_position_y * self.zoom offset_x = (offset_x - self.pan_x) / self.zoom offset_y = (offset_y - self.pan_y) / self.zoom return offset_x, offset_y def __rectangle_selection_begin(self, x: float, y: float, button: int, modifier: int): """Mouse pressed callback""" if not self._model or button != 0: return if ( not modifier & carb.input.KEYBOARD_MODIFIER_FLAG_SHIFT and not modifier & carb.input.KEYBOARD_MODIFIER_FLAG_CONTROL ): self.__need_clear_selection = True asyncio.ensure_future(self._clear_selection_next_frame_async(self._model)) # if there is modifier, but the modifier is not shift or control, we skip drawing the selection # so that users are free use other modifier for other actions if modifier: return self.__rectangle_selection_start_position = self.__canvas_space(x, y) # Mouse position in widget space x_w = x - self.__selection_layer.screen_position_x y_w = y - self.__selection_layer.screen_position_y self.__selection_placer_start.offset_x = x_w self.__selection_placer_start.offset_y = y_w self.__selection_placer_end.offset_x = x_w self.__selection_placer_end.offset_y = y_w def __rectangle_selection_end(self, x: float, y: float, button: int, modifier: int): """Mouse released callback""" if self.__rectangle_selection_start_position is None: return # Clean up selection caches self.__selection_placer_start.offset_x = 0 self.__selection_placer_start.offset_y = 0 self.__selection_placer_end.offset_x = 0 self.__selection_placer_end.offset_y = 0 self.__selection_layer.visible = False self.__rectangle_selection_start_position = None self.__positions_cache = None self.__selection_cache = [] def __rectangle_selection_moved(self, x: float, y: float, modifier: int, pressed: bool): """Mouse moved callback""" if self.__rectangle_selection_start_position is None: # No rectangle selection return self._on_rectangle_select( self._model, self.__rectangle_selection_start_position, self.__canvas_space(x, y), modifier ) # Mouse position in widget space x_w = x - self.__selection_layer.screen_position_x y_w = y - self.__selection_layer.screen_position_y self.__selection_layer.visible = True self.__selection_placer_end.offset_x = x_w self.__selection_placer_end.offset_y = y_w def _on_new_connection(self): """Called when the user finished creating connection. This method sends the connection to the model.""" if ( not self.__making_connection_source or not self.__making_connection_target or not self.__making_connection_target_node ): self.__making_connection_source = None self.__making_connection_target_node = None self.__making_connection_target = None if ( self.__making_connection_source_node or self.__making_connection_source or self.__making_connection_source_widget ): # We are here because the user cancelled the connection (most # likely RMB is pressed). We need to clear the current connection. self.__on_start_connection(None, None, None, None) return if self.__can_connect: # Set the connection in the model. self._model[self.__making_connection_target].inputs = [self.__making_connection_source] self.__on_start_connection(None, None, None, None) def _on_port_context_menu(self, node, port): """Open context menu for specific port""" self.__port_context_menu = ui.Menu("Port Context Menu", visible=False) with self.__port_context_menu: if self._model[port].inputs: self.__port_context_menu.visible = True ui.MenuItem("Disconnect", triggered_fn=lambda p=port: self.__disconnect_inputs(p)) self.__port_context_menu.show() def _on_set_zoom_key_shortcut(self, mouse_button, key): """allow user to set the key shortcut for the graphView zoom""" self.__root_frame.set_zoom_key_shortcut(mouse_button, key) def destroy(self): """ Called by extension before destroying this object. It doesn't happen automatically. Without this hot reloading doesn't work. """ self.set_model(None) self.__root_frame = None self._delegate = None # Destroy each node for _, node_widget in self._node_widgets.items(): node_widget.destroy() for _, connection_widget in self._connection_widgets.items(): connection_widget.destroy() self._node_widgets = {} self._connection_widgets = {} self._node_placers = {} self.__port_context_menu = None self.__connections_stack = None self.__port_to_frames = {} self.__user_drag_connection_frame = None self.__user_drag_connection_accepted_frame = None self.__making_connection_source_node = None self.__making_connection_source = None self.__making_connection_source_widget = None # Destroy the graph index self._graph_node_index = GraphNodeIndex(None, self.__port_grouping) @property def zoom(self): """The zoom level of the scene""" return self.__root_frame.zoom @zoom.setter def zoom(self, value): """The zoom level of the scene""" self.__root_frame.zoom = value @property def zoom_min(self): """The minminum zoom level of the scene""" return self.__root_frame.zoom_min @zoom_min.setter def zoom_min(self, value): """The minminum zoom level of the scene""" self.__root_frame.zoom_min = value @property def zoom_max(self): """The maximum zoom level of the scene""" return self.__root_frame.zoom_max @zoom_max.setter def zoom_max(self, value): """The maximum zoom level of the scene""" self.__root_frame.zoom_max = value @property def pan_x(self): """The horizontal offset of the scene""" return self.__root_frame.pan_x @pan_x.setter def pan_x(self, value): """The horizontal offset of the scene""" self.__root_frame.pan_x = value @property def pan_y(self): """The vertical offset of the scene""" return self.__root_frame.pan_y @pan_y.setter def pan_y(self, value): """The vertical offset of the scene""" self.__root_frame.pan_y = value
69,581
Python
41.925355
128
0.540679
omniverse-code/kit/exts/omni.kit.widget.graph/omni/kit/widget/graph/graph_node.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. # __all__ = ["GraphNode"] from .abstract_graph_node_delegate import GraphNodeDescription from .abstract_graph_node_delegate import GraphNodeLayout from .abstract_graph_node_delegate import GraphPortDescription from .graph_model import GraphModel import omni.ui as ui class GraphNode: """ Represents the Widget for the single node. Uses the model and the delegate to fill up its layout. """ def __init__(self, model: GraphModel, item, has_input_connection, has_output_connection, ports: list, delegate): """ Save the model, item and delegate to reuse when drawing the widget """ # Port to the tuple of two widgets that represent the port self._port_to_frames = {} # Port to the tuple of two widgets that are used to draw line to show the connection is in process self._port_to_user_drag = {} self._port_to_user_drag_placer = {} # center port widget (i.e. name labels and string edit fields) self._port_center_widgets = {} self._header_input_widget = None self._header_output_widget = None self._has_input_connection = has_input_connection self._has_output_connection = has_output_connection self._header_widget = None self._footer_widget = None self._node_bg_widget = None self.__root_frame = ui.Frame(width=0, height=0, skip_draw_when_clipped=True) # TODO: WeakRef for the model? self.__model = model self.__delegate = delegate self.__item = item self.__ports = ports # Build everything self.__build_layout() def __getattr__(self, attr): """Pretend it's self.__root_frame""" return getattr(self.__root_frame, attr) def rebuild_layout(self): node_desc = GraphNodeDescription(self.__item, self._has_input_connection[0], self._has_input_connection[1]) def __build_node_bg(): self.__delegate.node_background(self.__model, node_desc) def __build_footer(): self.__delegate.node_footer(self.__model, node_desc) def __build_header(): self.__delegate.node_header(self.__model, node_desc) if not self._header_widget.has_build_fn(): self._header_widget.set_build_fn(__build_header) self._header_widget.rebuild() if not self._node_bg_widget.has_build_fn(): self._node_bg_widget.set_build_fn(__build_node_bg) self._node_bg_widget.rebuild() if not self._footer_widget.has_build_fn(): self._footer_widget.set_build_fn(__build_footer) self._footer_widget.rebuild() def __build_layout(self): """ Build the Node layout with the delegate. See GraphNodeDelegate for info. When it's called, it will replace all the sub-widgets. """ self._port_to_frames.clear() self._port_to_user_drag.clear() self._port_to_user_drag_placer.clear() self._port_center_widgets.clear() # Sorting ports for column layout. If the port has inputs only, it goes # to input_ports. IF the port has both inputs and outputs, it goes to # mixed_ports. mixed_ports = [] input_ports = [] output_ports = [] node_desc = GraphNodeDescription(self.__item, self._has_input_connection[0], self._has_input_connection[1]) node_layout = self.__delegate.get_node_layout(self.__model, node_desc) for port_entry in self.__ports: if node_layout == GraphNodeLayout.LIST or node_layout == GraphNodeLayout.HEAP: mixed_ports.append(port_entry) elif node_layout == GraphNodeLayout.COLUMNS: port, port_input_flags, port_output_flags, level, position, parent_child_count = port_entry inputs = self.__model[port].inputs outputs = self.__model[port].outputs if inputs is None and outputs is None: input_ports.append(port_entry) elif inputs is not None and outputs is None: input_ports.append(port_entry) elif inputs is None and outputs is not None: output_ports.append(port_entry) else: mixed_ports.append(port_entry) with self.__root_frame: # See GraphNodeDelegate for the detailed information on the layout with ui.ZStack(height=0): self._node_bg_widget = ui.Frame() with self._node_bg_widget: self.__delegate.node_background(self.__model, node_desc) if node_layout == GraphNodeLayout.HEAP: stack = ui.ZStack(height=0) else: stack = ui.VStack(height=0) with stack: # Header with ui.HStack(): self._header_input_widget = ui.Frame(width=0) with self._header_input_widget: port_widget = self.__delegate.node_header_input(self.__model, node_desc) # The way to set the center of connection. If the # port function doesn't return anything, the # connection is centered to the frame. If it # returns a widget, the connection is centered to # the widget returned. if port_widget: self._header_input_widget = port_widget self._header_widget = ui.Frame() with self._header_widget: self.__delegate.node_header(self.__model, node_desc) self._header_output_widget = ui.Frame(width=0) with self._header_output_widget: port_widget = self.__delegate.node_header_output(self.__model, node_desc) # Set the center of connection. if port_widget: self._header_output_widget = port_widget # One port per line for port, port_input_flags, port_output_flags, port_level, pos, siblings in mixed_ports: self.__build_port( node_desc, port, port_input_flags, port_output_flags, port_level, pos, siblings, node_layout == GraphNodeLayout.HEAP, ) # Two ports per line with ui.HStack(): # width=0 lets each column only take up the amount of space it needs with ui.VStack(height=0, width=0): for port, port_input_flags, port_output_flags, port_level, pos, siblings in input_ports: self.__build_port( node_desc, port, port_input_flags, None, port_level, pos, siblings, False ) ui.Spacer() # spreads the 2 sides apart from each other with ui.VStack(height=0, width=0): for port, port_input_flags, port_output_flags, port_level, pos, siblings in output_ports: self.__build_port( node_desc, port, None, port_output_flags, port_level, pos, siblings, False ) # Footer self._footer_widget = ui.Frame() with self._footer_widget: self.__delegate.node_footer(self.__model, node_desc) def __build_port( self, node_desc: GraphNodeDescription, port, port_input_flags, port_output_flags, level: int, relative_position: int, parent_child_count: int, is_heap: bool, ): """The layout for one single port line.""" # port_input_flags is a tuple of 2 bools. The first item is the flag # that is True if the port is a source in connection to the input (left # side). The second item is the flag that is True when this port is a # target in the connection to the input. port_output_flags is the same # for output (right side). If port_input_flags or port_output_flags is # None, it doesn't produce left or right part. if is_heap: width = ui.Fraction(1) stack = ui.ZStack() else: width = ui.Pixel(0) stack = ui.HStack() with stack: if port_input_flags is None: input_port_widget = None else: input_port_widget = ui.Frame(width=width) with input_port_widget: port_desc = GraphPortDescription( port, level, relative_position, parent_child_count, port_input_flags[0], port_input_flags[1] ) port_widget = self.__delegate.port_input(self.__model, node_desc, port_desc) # Set the center of connection. if port_widget: input_port_widget = port_widget port_desc = GraphPortDescription(port, level, relative_position, parent_child_count) port_center_widget = self.__delegate.port(self.__model, node_desc, port_desc) self._port_center_widgets[port_desc.port] = port_center_widget def fit_to_mose(placer: ui.Placer, x: float, y: float): """ If the port is big we need to make the placer small enough and move it to the mouse cursor to make the connection follow the mouse """ # The new size SIZE = 3.0 placer.width = ui.Pixel(SIZE) placer.height = ui.Pixel(SIZE) # Move to the mouse position placer.offset_x = x - placer.screen_position_x - SIZE / 2 placer.offset_y = y - placer.screen_position_y - SIZE / 2 def reset_offset(placer: ui.Placer): # Put it back to the port placer.offset_x = 0 placer.offset_y = 0 # Restore the size placer.width = ui.Fraction(1) placer.height = ui.Fraction(1) def set_draggable(placer: ui.Placer, frame: ui.Frame, draggable: bool): placer.draggable = draggable if port_output_flags is None: output_port_widget = None else: with ui.ZStack(width=width): # The placer and the widget that follows the # mouse cursor when the user creates a # connection output_userconnection_placer = ui.Placer(stable_size=True, draggable=True) output_userconnection_placer.set_mouse_pressed_fn( lambda x, y, *_, p=output_userconnection_placer: fit_to_mose(p, x, y) ) output_userconnection_placer.set_mouse_released_fn( lambda *_, p=output_userconnection_placer: reset_offset(p) ) if port_center_widget: port_center_widget.set_mouse_pressed_fn( lambda *_, p=output_userconnection_placer, w=port_center_widget: set_draggable(p, w, False) ) port_center_widget.set_mouse_released_fn( lambda *_, p=output_userconnection_placer, w=port_center_widget: set_draggable(p, w, True) ) self._port_to_user_drag_placer[port] = output_userconnection_placer with output_userconnection_placer: # This widget follows the cursor when the # user creates a connection. Line is # stuck to this widget. rect = ui.Spacer() self._port_to_user_drag[port] = (None, rect) # The port widget output_port_widget = ui.Frame() with output_port_widget: port_desc = GraphPortDescription( port, level, relative_position, parent_child_count, port_output_flags[0], port_output_flags[1], ) port_widget = self.__delegate.port_output(self.__model, node_desc, port_desc) # Set the center of connection. if port_widget: output_port_widget = port_widget # Save input and output frame for each port self._port_to_frames[port] = (input_port_widget, output_port_widget) @property def ports(self): return self._port_to_frames @property def port_center_widgets(self): """ Return the dict for port center widgets (which contains the port name label and edit fields). Dictionary key is the port, value is ui.Widget or None if the delegate does not return a widget """ return self._port_center_widgets @property def user_drag(self): """ Return the dict with the port as the key and widget that follows the mouse cursor when the user creates a connection. """ return self._port_to_user_drag @property def user_drag_placer(self): """ Return the dict with the port as the key and placer that follows the mouse cursor when the user creates a connection. """ return self._port_to_user_drag_placer @property def header_input_frame(self): """ Return the Frame that holds the inputs on the left side of the header bar. """ return self._header_input_widget @property def header_output_frame(self): """ Return the Frame that holds the outputs on the right side of the header bar. """ return self._header_output_widget @property def header_frame(self): """ Return the Frame that holds the entire header bar. """ return self._header_widget def destroy(self): """ Called by extension before destroying this object. It doesn't happen automatically. Without this hot reloading doesn't work. """ self._port_to_frames = None self._port_to_user_drag = None self._port_to_user_drag_placer = None self._port_center_widgets = None if self.__root_frame: self.__root_frame.destroy() self.__root_frame = None self.__model = None self.__delegate = None self._header_widget = None self._footer_widget = None self._node_bg_widget = None @property def skip_draw_clipped(self): """Get skip_draw_when_clipped property of the root frame""" return self.__root_frame.skip_draw_when_clipped @skip_draw_clipped.setter def skip_draw_clipped(self, value): """Set skip_draw_when_clipped property of the root frame""" self.__root_frame.skip_draw_when_clipped = value @property def selected(self): """Return the widget selected style state""" if self.__root_frame: return self.__root_frame.selected @selected.setter def selected(self, value): """Set the widget selected style state""" if self.__root_frame: self.__root_frame.selected = value @property def visible(self) -> bool: return self.__root_frame.visible @visible.setter def visible(self, value: bool): self.__root_frame.visible = value
16,808
Python
39.997561
119
0.539624
omniverse-code/kit/exts/omni.kit.widget.graph/omni/kit/widget/graph/abstract_batch_position_getter.py
# Copyright (c) 2018-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. # __all__ = ["AbstractBatchPositionGetter"] from typing import Any, List, Optional import weakref import abc from .graph_model import GraphModel class AbstractBatchPositionGetter(abc.ABC): """Helper to get the nodes to move at the same time""" def __init__(self, model: GraphModel): self.model = model @property def model(self): return self.__model() @model.setter def model(self, value): self.__model = weakref.ref(value) @abc.abstractmethod def __call__(self, drive_item: Any) -> Optional[List[Any]]: pass
1,009
Python
29.60606
76
0.718533
omniverse-code/kit/exts/omni.kit.widget.graph/omni/kit/widget/graph/backdrop_getter.py
# Copyright (c) 2018-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. # __all__ = ["BackdropGetter"] from functools import lru_cache from typing import Any, Callable, List, Optional import carb.input from .abstract_batch_position_getter import AbstractBatchPositionGetter from .graph_model import GraphModel @lru_cache() def __get_input() -> carb.input.IInput: return carb.input.acquire_input_interface() def _is_alt_down() -> bool: input = __get_input() return ( input.get_keyboard_value(None, carb.input.KeyboardInput.LEFT_ALT) + input.get_keyboard_value(None, carb.input.KeyboardInput.RIGHT_ALT) > 0 ) class BackdropGetter(AbstractBatchPositionGetter): """Helper to get the nodes that placed in the given backdrop""" def __init__(self, model: GraphModel, is_backdrop_fn: Callable[[Any], bool], graph_widget = None): super().__init__(model) # Callback to determine if the node is a backdrop self.__is_backdrop_fn = is_backdrop_fn self.__graph_widget = graph_widget def __call__(self, drive_item: Any) -> Optional[List[Any]]: # Get siblings and return MIN_OFFSET = 25 # Same value used in _create_backdrop_for_selected_nodes() # If user presses ALT while moving the backdrop, don't carry nodes with it. if _is_alt_down(): return [] model = self.model if model and self.__is_backdrop_fn(drive_item): position = model[drive_item].position size = model[drive_item].size if position and size: result = [] for node in model.nodes: node_position = model[node].position if not node_position: continue # Add the node size offset to the extent if self.__graph_widget: widget = self.__graph_widget._graph_view._node_widgets[node] s = (widget.computed_width, widget.computed_height) else: s = (0, 0) # For backwards compatibility # Check if the node is inside the backdrop if ( node_position[0] < position[0] or node_position[1] < position[1] or node_position[0] + s[0] > position[0] + size[0] + MIN_OFFSET or node_position[1] + s[1] > position[1] + size[1] + MIN_OFFSET ): continue result.append(node) return result
3,013
Python
35.313253
102
0.58845
omniverse-code/kit/exts/omni.kit.widget.graph/omni/kit/widget/graph/graph_model_batch_position_helper.py
# Copyright (c) 2018-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. # __all__ = ["GraphModelBatchPositionHelper"] from .abstract_batch_position_getter import AbstractBatchPositionGetter from typing import Any, Callable, Dict, List, Tuple, Union import weakref class GraphModelBatchPositionHelper: """ The model that manages batch position of the items. It can be used to manage the position of multi selection, backdrops and upstreams. """ def __init__(self): super().__init__() # Indicates that the method multiselection_set_position is called and # not finished. We need it because multiselection_set_position can be # called recursively self.__position_set_in_process = False # The same for multiselection_position_begin_edit self.__position_begin_edit_in_process = False # The same for multiselection_position_end_edit self.__position_end_edit_in_process = False # The node that drives position of many nodes. It's the node the user # actually drags. self.__current_drive_node = None # Other nodes in the selection self.__driven_nodes: List = [] self.__position_difference: Dict[Any, Union[List, Tuple]] = {} # Functions to get the nodes that are moving self.__get_moving_items_fns = {} # Proxy is the isolation model. We need it to set the position of # input/output nodes. self.__batch_proxy = None self.batch_proxy = self @property def batch_proxy(self): """ Proxy is the isolation model. We need it because it converts calls to input/output nodes. """ return self.__batch_proxy() @batch_proxy.setter def batch_proxy(self, value): # Weak ref so we don't have circular references self.__batch_proxy = weakref.ref(value) for _, fn in self.__get_moving_items_fns.items(): if isinstance(fn, AbstractBatchPositionGetter): fn.model = value def add_get_moving_items_fn(self, fn: Callable[[Any], List[Any]]): """ Add the function that is called in batch_position_begin_edit to determine which items to move """ def get_new_id(batch_position_fns: Dict[int, Callable]): if not batch_position_fns: return 0 return max(batch_position_fns.keys()) + 1 class Subscription: """The object that will remove the callback when destroyed""" def __init__(self, parent, id): self.__parent = weakref.ref(parent) self.__id = id def __del__(self): parent = self.__parent() if parent: parent._remove_batch_position_fn(self.__id) id = get_new_id(self.__get_moving_items_fns) self.__get_moving_items_fns[id] = fn def _remove_get_moving_items_fn(self, id: int): self.__get_moving_items_fns.pop(id, None) def batch_set_position(self, position: List[float], item: Any = None): """ Should be called in the position setter to make sure the position setter is called for all the selected nodes """ if self.__position_set_in_process: return if self.__current_drive_node != item: # Somehow it's possible that batch_set_position is called for the # wrong node. We need to filter such cases. It happens rarely, but # when it happens, it leads to a crash because the node applies the # diff to itself infinitely. # TODO: investigate OM-58441 more return self.__position_set_in_process = True try: current_position = position if not current_position: # Can't do anything without knowing position return for node in self.__driven_nodes: diff = self.__position_difference.get(node, None) if diff: self.batch_proxy[node].position = ( diff[0] + current_position[0], diff[1] + current_position[1], ) # TODO: except finally: self.__position_set_in_process = False def batch_position_begin_edit(self, item: Any): """ Should be called from position_begin_edit to make sure position_begin_edit is called for all the selected nodes """ if self.__position_begin_edit_in_process: return self.__position_begin_edit_in_process = True try: # The current node is the drive node. self.__current_drive_node = item # Get driven nodes from the callbacks driven_nodes = [] for _, fn in self.__get_moving_items_fns.items(): driven_nodes += fn(self.__current_drive_node) or [] # Just in case filter out drive node self.__driven_nodes = [i for i in driven_nodes if i != self.__current_drive_node] current_position = self.batch_proxy[self.__current_drive_node].position if not current_position: # Can't do anything without knowing position return for node in self.__driven_nodes: node_position = self.batch_proxy[node].position if node_position: self.__position_difference[node] = ( node_position[0] - current_position[0], node_position[1] - current_position[1], ) self.batch_proxy.position_begin_edit(node) # TODO: except finally: self.__position_begin_edit_in_process = False def batch_position_end_edit(self, item: Any): """ Should be called from position_begin_edit to make sure position_begin_edit is called for all the selected nodes """ if self.__position_end_edit_in_process: return self.__position_end_edit_in_process = True try: for node in self.__driven_nodes: self.batch_proxy.position_end_edit(node) # Clean up self.__driven_nodes = [] self.__position_difference = {} # TODO: except finally: self.__position_end_edit_in_process = False
6,864
Python
34.386598
93
0.584062
omniverse-code/kit/exts/omni.kit.widget.graph/omni/kit/widget/graph/graph_node_delegate_router.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. # __all__ = ["GraphNodeDelegateRoutingError", "GraphNodeDelegateRouter", "RoutingCondition"] from .abstract_graph_node_delegate import AbstractGraphNodeDelegate from .abstract_graph_node_delegate import GraphConnectionDescription from .abstract_graph_node_delegate import GraphNodeDescription from .abstract_graph_node_delegate import GraphPortDescription from collections import namedtuple RoutingCondition = namedtuple("RoutingCondition", ["type", "expression", "delegate"]) class GraphNodeDelegateRoutingError(Exception): """ This exception is used when it's not possible to do routing. Can only happen if there is no default route in the table. """ pass class GraphNodeDelegateRouter(AbstractGraphNodeDelegate): """ The delegate that keeps multiple delegates and pick them depending on the routing conditions. It's possible to add the routing conditions with `add_route`, and conditions could be a type or a lambda expression. The latest added routing is stronger than previously added. Routing added without conditions is the default. We use type routing to make the specific kind of nodes unique, and also we can use the lambda function to make the particular state of nodes unique (ex. full/collapsed). It's possible to use type and lambda routing at the same time. Usage examples: delegate.add_route(TextureDelegate(), type="Texture2d") delegate.add_route(CollapsedDelegate(), expressipon=is_collapsed) """ def __init__(self): super().__init__() self.__routing_table = [] def add_route(self, delegate: AbstractGraphNodeDelegate, type=None, expression=None): """Add delegate to the routing tablle""" if not delegate: return if type is None and expression is None: # It's a default delegate, we don't need what we had before self.__routing_table.clear() self.__routing_table.append(RoutingCondition(type, expression, delegate)) def __route(self, model, node): """Return the delegate for the given node""" for c in reversed(self.__routing_table): if (c.type is None or model[node].type == c.type) and (c.expression is None or c.expression(model, node)): return c.delegate raise GraphNodeDelegateRoutingError("Can't route.") def get_node_layout(self, model, node_desc: GraphNodeDescription): """Called to determine the node layout""" return self.__route(model, node_desc.node).get_node_layout(model, node_desc) def node_background(self, model, node_desc: GraphNodeDescription): """Called to create widgets of the node background""" return self.__route(model, node_desc.node).node_background(model, node_desc) def node_header_input(self, model, node_desc: GraphNodeDescription): """Called to create the left part of the header that will be used as input when the node is collapsed""" return self.__route(model, node_desc.node).node_header_input(model, node_desc) def node_header_output(self, model, node_desc: GraphNodeDescription): """Called to create the right part of the header that will be used as output when the node is collapsed""" return self.__route(model, node_desc.node).node_header_output(model, node_desc) def node_header(self, model, node_desc: GraphNodeDescription): """Called to create widgets of the top of the node""" return self.__route(model, node_desc.node).node_header(model, node_desc) def node_footer(self, model, node_desc: GraphNodeDescription): """Called to create widgets of the bottom of the node""" return self.__route(model, node_desc.node).node_footer(model, node_desc) def port_input(self, model, node_desc: GraphNodeDescription, port_desc: GraphPortDescription): """Called to create the left part of the port that will be used as input""" return self.__route(model, node_desc.node).port_input(model, node_desc, port_desc) def port_output(self, model, node_desc: GraphNodeDescription, port_desc: GraphPortDescription): """Called to create the right part of the port that will be used as output""" return self.__route(model, node_desc.node).port_output(model, node_desc, port_desc) def port(self, model, node_desc: GraphNodeDescription, port_desc: GraphPortDescription): """Called to create the middle part of the port""" return self.__route(model, node_desc.node).port(model, node_desc, port_desc) def connection(self, model, source: GraphConnectionDescription, target: GraphConnectionDescription): """Called to create the connection between ports""" return self.__route(model, source.node).connection(model, source, target) def destroy(self): while self.__routing_table: routing_condition = self.__routing_table.pop() routing_condition.delegate.destroy()
5,426
Python
43.851239
118
0.707519
omniverse-code/kit/exts/omni.kit.widget.graph/omni/kit/widget/graph/selection_getter.py
# Copyright (c) 2018-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. # __all__ = ["SelectionGetter"] from .abstract_batch_position_getter import AbstractBatchPositionGetter from typing import Any import weakref from .graph_model import GraphModel class SelectionGetter(AbstractBatchPositionGetter): """ Helper to get the selection of the given model. It's supposed to be used with GraphModelBatchPosition. """ def __init__(self, model: GraphModel): super().__init__(model) def __call__(self, drive_item: Any): model = self.model if model: return model.selection
988
Python
33.103447
76
0.737854
omniverse-code/kit/exts/omni.kit.widget.graph/omni/kit/widget/graph/abstract_graph_node_delegate.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. # __all__ = ["GraphNodeLayout", "GraphNodeDescription", "GraphPortDescription", "GraphConnectionDescription", "AbstractGraphNodeDelegate"] from enum import Enum, auto class GraphNodeLayout(Enum): LIST = auto() COLUMNS = auto() HEAP = auto() class GraphNodeDescription: """The object that holds the main attributes of the node""" def __init__(self, node, connected_source=None, connected_target=None): self.node = node self.connected_source = connected_source self.connected_target = connected_target class GraphPortDescription: """The object that holds the main attributes of the port""" def __init__( self, port, level, relative_position, parent_child_count, connected_source=None, connected_target=None ): self.port = port self.level = level self.relative_position = relative_position self.parent_child_count = parent_child_count self.connected_source = connected_source self.connected_target = connected_target class GraphConnectionDescription: """The object that holds the main attributes of the connection""" def __init__(self, node, port, widget, level, is_tangent_reversed=False): self.node = node self.port = port self.widget = widget self.level = level self.is_tangent_reversed = is_tangent_reversed class AbstractGraphNodeDelegate: """ The delegate generates widgets that together form the node using the model. The following figure shows the LIST layout of the node. For every zone, there is a method that is called to build this zone. :: +-------------------------+ | node_background | | +---+-------------+---+ | | |[A]| node_header |[B]| | | +---+-------------+---+ | | |[C]| port |[D]| | | +---+-------------+---+ | | |[D]| port |[D]| | | +---+-------------+---+ | | |[E]| node_footer |[F]| | | +---+-------------+---+ | +-------------------------+ COLUMN layout allows to put input and output ports at the same line: :: +-------------------------+ | node_background | | +---+-------------+---+ | | |[A]| node_header |[B]| | | +---+------+------+---+ | | |[C]| port | port |[D]| | | | | |------+---| | | |---+------| port |[D]| | | |[D]| port | | | | | +---+------+------+---+ | | |[E]| node_footer |[F]| | | +---+-------------+---+ | +-------------------------+ :: [A] node_header_input [B] node_header_output [C] port_input [D] port_output [E] node_footer_input (TODO) [F] node_footer_output (TODO) """ def destroy(self): pass def get_node_layout(self, model, node_desc: GraphNodeDescription): """Called to determine the node layout""" return GraphNodeLayout.LIST def node_background(self, model, node_desc: GraphNodeDescription): """Called to create widgets of the node background""" pass def node_header_input(self, model, node_desc: GraphNodeDescription): """Called to create the left part of the header that will be used as input when the node is collapsed""" pass def node_header_output(self, model, node_desc: GraphNodeDescription): """Called to create the right part of the header that will be used as output when the node is collapsed""" pass def node_header(self, model, node_desc: GraphNodeDescription): """Called to create widgets of the top of the node""" pass def node_footer(self, model, node_desc: GraphNodeDescription): """Called to create widgets of the bottom of the node""" pass def port_input(self, model, node_desc: GraphNodeDescription, port_desc: GraphPortDescription): """Called to create the left part of the port that will be used as input""" pass def port_output(self, model, node_desc: GraphNodeDescription, port_desc: GraphPortDescription): """Called to create the right part of the port that will be used as output""" pass def port(self, model, node_desc: GraphNodeDescription, port_desc: GraphPortDescription): """Called to create the middle part of the port""" pass def connection(self, model, source_desc: GraphConnectionDescription, target_desc: GraphConnectionDescription): """Called to create the connection between ports""" pass
5,051
Python
33.602739
136
0.583449
omniverse-code/kit/exts/omni.kit.widget.graph/omni/kit/widget/graph/tests/test_delegate.py
## Copyright (c) 2018-2019, 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 from omni.kit.widget.graph.graph_node_delegate import GraphNodeDelegate from omni.kit.widget.graph.graph_view import GraphView from omni.ui.tests.test_base import OmniUiTest from .test_graph import Model import omni.kit.app class TestDelegate(OmniUiTest): async def test_general(self): """Testing general properties of the GraphView default delegate""" window = await self.create_test_window(768, 512) style = GraphNodeDelegate.get_style() # Don't use the image because the image scaling looks different in editor and kit-mini style["Graph.Node.Footer.Image"]["image_url"] = "" style["Graph.Node.Header.Collapse"]["image_url"] = "" style["Graph.Node.Header.Collapse::Minimized"]["image_url"] = "" style["Graph.Node.Header.Collapse::Closed"]["image_url"] = "" with window.frame: delegate = GraphNodeDelegate() model = Model() graph_view = GraphView(model=model, delegate=delegate, style=style, pan_x=600, pan_y=170) # Two frames because the first frame is to draw, the second frame is to auto-layout. for i in range(3): await omni.kit.app.get_app().next_update_async() await self.finalize_test() async def test_zoom(self): """Testing zooming of the GraphView default delegate""" window = await self.create_test_window(384, 256) style = GraphNodeDelegate.get_style() # Don't use the image because the image scaling looks different in editor and kit-mini style["Graph.Node.Footer.Image"]["image_url"] = "" style["Graph.Node.Header.Collapse"]["image_url"] = "" style["Graph.Node.Header.Collapse::Minimized"]["image_url"] = "" style["Graph.Node.Header.Collapse::Closed"]["image_url"] = "" with window.frame: delegate = GraphNodeDelegate() model = Model() graph_view = GraphView(model=model, delegate=delegate, style=style, zoom=0.5, pan_x=300, pan_y=85) # Two frames because the first frame is to draw, the second frame is to auto-layout. for i in range(3): await omni.kit.app.get_app().next_update_async() await self.finalize_test() async def test_focus(self): """Testing focusing of the GraphView default delegate""" window = await self.create_test_window(768, 512) style = GraphNodeDelegate.get_style() # Don't use the image because the image scaling looks different in editor and kit-mini style["Graph.Node.Footer.Image"]["image_url"] = "" style["Graph.Node.Header.Collapse"]["image_url"] = "" style["Graph.Node.Header.Collapse::Minimized"]["image_url"] = "" style["Graph.Node.Header.Collapse::Closed"]["image_url"] = "" with window.frame: delegate = GraphNodeDelegate() model = Model() graph_view = GraphView(model=model, delegate=delegate, style=style) # Two frames because the first frame is to draw, the second frame is to auto-layout. for i in range(3): await omni.kit.app.get_app().next_update_async() graph_view.focus_on_nodes() for i in range(2): await omni.kit.app.get_app().next_update_async() await self.finalize_test()
3,782
Python
41.988636
110
0.654944
omniverse-code/kit/exts/omni.kit.widget.graph/omni/kit/widget/graph/tests/test_graph_with_subports.py
## Copyright (c) 2018-2019, 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__ = ["TestGraphWithSubports"] import omni.kit.test from omni.kit.widget.graph import GraphNodeDescription from omni.kit.widget.graph import GraphPortDescription from omni.kit.widget.graph.abstract_graph_node_delegate import AbstractGraphNodeDelegate from omni.kit.widget.graph.graph_model import GraphModel from omni.kit.widget.graph.graph_view import GraphView from omni.ui.tests.test_base import OmniUiTest import omni.kit.app import omni.ui as ui from functools import partial inputs = ["First.color1", "First.color1.tex", "First.color1.shader", "First.color2", "Third.size", "Fourth.color1", "Fourth.color1.tex", "Fourth.color1.shader",] outputs = ["First.result", "Second.out", "Third.outcome", "Third.outcome.out1", "Third.outcome.out2"] connections = {"First.color1.tex": ["Second.out"], "First.color2": ["Third.outcome.out2"], "Fourth.color1.tex": ["Third.outcome.out1"]} style = { "Graph": {"background_color": 0xFF000000}, "Graph.Connection": {"color": 0xFFDBA656, "background_color": 0xFFDBA656, "border_width": 2.0}, } BORDER_WIDTH = 16 class Model(GraphModel): def __init__(self): super().__init__() @property def nodes(self, item=None): if item: return return sorted(set([n.split(".")[0] for n in inputs + outputs])) @property def name(self, item=None): return item.split(".")[-1] @property def ports(self, item=None): item_len = len(item.split(".")) if item_len == 1 or item_len == 2: result = [n for n in inputs + outputs if n.startswith(item) and len(n.split(".")) == item_len + 1] return result return [] @property def expansion_state(self, item=None): """the expansion state of a node or a port""" item_len = len(item.split(".")) if item_len == 2: return GraphModel.ExpansionState.CLOSED return GraphModel.ExpansionState.OPEN @property def inputs(self, item): if item in inputs: return connections.get(item, []) return None @property def outputs(self, item): if item in outputs: return [] return None class ExpandModel(Model): def __init__(self): super().__init__() @property def expansion_state(self, item=None): """the expansion state of a node or a port""" return GraphModel.ExpansionState.OPEN class Delegate(AbstractGraphNodeDelegate): def node_background(self, model, node_desc: GraphNodeDescription): ui.Rectangle(style={"background_color": 0xFF015C3A}) def node_header(self, model, node_desc: GraphNodeDescription): ui.Label(model[node_desc.node].name, style={"font_size": 12, "margin": 6, "color": 0xFFDBA656}) def port_input(self, model, node_desc: GraphNodeDescription, port_desc: GraphPortDescription): # this is output port if model[port_desc.port].inputs is None: color = 0x0 elif port_desc.connected_target: color = 0xFFDBA6FF else: color = 0xFFDBA656 ui.Circle(width=10, style={"background_color": color}) def port_output(self, model, node_desc: GraphNodeDescription, port_desc: GraphPortDescription): # this is output port if model[port_desc.port].outputs is None: color = 0x0 elif port_desc.connected_source: color = 0xFFDBA6FF else: color = 0xFFDBA656 ui.Circle(width=10, style={"background_color": color}) def port(self, model, node_desc: GraphNodeDescription, port_desc: GraphPortDescription): def set_expansion_state(model, port, state: GraphModel.ExpansionState, *args): model[port].expansion_state = state port = port_desc.port level = port_desc.level sub_ports = model[port].ports is_group = len(sub_ports) > 0 def draw_collapse_button(): if is_group: state = model[port].expansion_state if state == GraphModel.ExpansionState.CLOSED: button_name = "+" next_state = GraphModel.ExpansionState.OPEN else: button_name = "-" next_state = GraphModel.ExpansionState.CLOSED ui.Label(button_name, width=10, style={"font_size": 10, "margin": 2}, mouse_pressed_fn=partial(set_expansion_state, model, port, next_state)) else: ui.Spacer(width=10) def draw_branch(): if level > 0: ui.Line(width=8, style={"color": 0xFFFFFFFF}) alignment = ui.Alignment.LEFT if model[port].inputs is not None else ui.Alignment.RIGHT with ui.HStack(): if alignment == ui.Alignment.RIGHT: ui.Spacer(width=40) else: draw_collapse_button() draw_branch() ui.Label(model[port].name, style={"font_size": 10, "margin": 2}, alignment=alignment) if alignment == ui.Alignment.RIGHT: draw_branch() draw_collapse_button() else: ui.Spacer(width=40) def connection(self, model, source, target): """Called to create the connection between ports""" # If the connection is reversed, we need to mirror tangents ui.FreeBezierCurve(target.widget, source.widget, style={"color": 0xFFFFFFFF}) class TestGraphWithSubports(OmniUiTest): """Testing GraphView""" async def test_general(self): """Testing general properties of GraphView with port_grouping is True""" window = await self.create_test_window(512, 256) with window.frame: delegate = Delegate() model = Model() graph_view = GraphView(model=model, delegate=delegate, style=style, pan_x=400, pan_y=100, port_grouping=True) # Two frames because the first frame is to draw, the second frame is to auto-layout. for i in range(3): await omni.kit.app.get_app().next_update_async() await self.finalize_test() async def test_expansion(self): """Testing subport expansion""" window = await self.create_test_window(512, 256) with window.frame: delegate = Delegate() model = ExpandModel() graph_view = GraphView(model=model, delegate=delegate, style=style, pan_x=400, pan_y=100, port_grouping=True) # Two frames because the first frame is to draw, the second frame is to auto-layout. for i in range(3): await omni.kit.app.get_app().next_update_async() await self.finalize_test()
7,229
Python
36.268041
161
0.618481
omniverse-code/kit/exts/omni.kit.widget.graph/omni/kit/widget/graph/tests/test_graph.py
## Copyright (c) 2018-2019, 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__ = ["TestGraph"] import omni.kit.test from omni.kit.widget.graph import GraphNodeDescription from omni.kit.widget.graph import GraphNodeLayout from omni.kit.widget.graph import GraphPortDescription from omni.kit.widget.graph.abstract_graph_node_delegate import AbstractGraphNodeDelegate from omni.kit.widget.graph.graph_model import GraphModel from omni.kit.widget.graph.graph_view import GraphView from omni.ui import color as cl from omni.ui.tests.test_base import OmniUiTest import omni.kit.app import omni.ui as ui inputs = ["First.color1", "First.color2", "Third.size"] outputs = ["First.result", "Second.out", "Third.outcome"] connections = {"First.color1": ["Second.out"], "First.color2": ["Third.outcome"]} style = { "Graph": {"background_color": 0xFF000000}, "Graph.Connection": {"color": 0xFFDBA656, "background_color": 0xFFDBA656, "border_width": 2.0}, } BORDER_WIDTH = 16 class Model(GraphModel): def __init__(self): super().__init__() self.__positions = {} @property def nodes(self, item=None): if item: return return sorted(set([n.split(".")[0] for n in inputs + outputs])) @property def name(self, item=None): return item.split(".")[-1] @property def ports(self, item=None): if item in inputs: return if item in outputs: return return [n for n in inputs + outputs if n.startswith(item)] @property def inputs(self, item): if item in inputs: return connections.get(item, []) @property def outputs(self, item): if item in outputs: return [] @property def position(self, item=None): """Returns the position of the node""" return self.__positions.get(item, None) @position.setter def position(self, value, item=None): """The node position setter""" self.__positions[item] = value class Delegate(AbstractGraphNodeDelegate): def node_background(self, model, node_desc: GraphNodeDescription): ui.Rectangle(style={"background_color": 0xFF015C3A}) def node_header(self, model, node_desc: GraphNodeDescription): ui.Label(model[node_desc.node].name, style={"font_size": 12, "margin": 6, "color": 0xFFDBA656}) def port_input(self, model, node_desc: GraphNodeDescription, port_desc: GraphPortDescription): color = 0xFFDBA656 if port_desc.connected_target else 0x0 ui.Circle(width=10, style={"background_color": color}) def port_output(self, model, node_desc: GraphNodeDescription, port_desc: GraphPortDescription): color = 0xFFDBA656 if port_desc.connected_source else 0x0 ui.Circle(width=10, style={"background_color": color}) def port(self, model, node_desc: GraphNodeDescription, port_desc: GraphPortDescription): ui.Label(model[port_desc.port].name, style={"font_size": 10, "margin": 2}) def connection(self, model, source, target): """Called to create the connection between ports""" # If the connection is reversed, we need to mirror tangents ui.FreeBezierCurve(target.widget, source.widget, style={"color": 0xFFFFFFFF}) class DelegateHeap(AbstractGraphNodeDelegate): def get_node_layout(self, model, node_desc: GraphNodeDescription): return GraphNodeLayout.HEAP def port_input(self, model, node_desc: GraphNodeDescription, port_desc: GraphPortDescription): return ui.Rectangle(style={"background_color": cl(1.0, 0.0, 0.0, 1.0)}) def port_output(self, model, node_desc: GraphNodeDescription, port_desc: GraphPortDescription): return ui.Rectangle(style={"background_color": cl(0.0, 1.0, 1.0, 0.5)}) def port(self, model, node_desc: GraphNodeDescription, port_desc: GraphPortDescription): with ui.HStack(): ui.Spacer(width=BORDER_WIDTH) with ui.VStack(): ui.Spacer(height=BORDER_WIDTH) frame = ui.Frame(separate_window=True) with frame: with ui.ZStack(): ui.Rectangle(width=32, height=32, style={"background_color": cl(0.75)}) ui.Spacer(height=BORDER_WIDTH) ui.Spacer(width=BORDER_WIDTH) return frame def connection(self, model, source, target): """Called to create the connection between ports""" ui.OffsetLine( target.widget, source.widget, alignment=ui.Alignment.UNDEFINED, begin_arrow_type=ui.ArrowType.ARROW, bound_offset=20, style_type_name_override="Graph.Connection", style={"color": cl.white, "border_width": 1.0}, ) class TestGraph(OmniUiTest): """Testing GraphView""" async def test_general(self): """Testing general properties of GraphView""" window = await self.create_test_window(512, 256) with window.frame: delegate = Delegate() model = Model() graph_view = GraphView(model=model, delegate=delegate, style=style, pan_x=400, pan_y=100) # Two frames because the first frame is to draw, the second frame is to auto-layout. for i in range(10): await omni.kit.app.get_app().next_update_async() await self.finalize_test() async def test_zoom(self): """Testing zooming of GraphView""" window = await self.create_test_window(256, 128) with window.frame: delegate = Delegate() model = Model() graph_view = GraphView(model=model, delegate=delegate, style=style, zoom=0.5, pan_x=200, pan_y=50) # Two frames because the first frame is to draw, the second frame is to auto-layout. for i in range(10): await omni.kit.app.get_app().next_update_async() await self.finalize_test() async def test_focus(self): """Testing focusing of GraphView""" window = await self.create_test_window(512, 256) with window.frame: delegate = Delegate() model = Model() graph_view = GraphView(model=model, delegate=delegate, style=style) # Wait several frames to draw and auto-layout. for i in range(10): await omni.kit.app.get_app().next_update_async() graph_view.focus_on_nodes() for i in range(10): await omni.kit.app.get_app().next_update_async() self.assertAlmostEqual(graph_view.zoom, 1.6606260538101196) await self.finalize_test() async def test_focus_with_zoom_limits(self): """Testing focusing of GraphView""" window = ui.Window("test", width=512, height=256) with window.frame: delegate = Delegate() model = Model() graph_view = GraphView(model=model, delegate=delegate, style=style, zoom_max=1.5) # Wait several frames to draw and auto-layout. for i in range(10): await omni.kit.app.get_app().next_update_async() graph_view.focus_on_nodes() for i in range(10): await omni.kit.app.get_app().next_update_async() self.assertAlmostEqual(graph_view.zoom, 1.5, places=4) async def test_heap(self): """Testing general properties of GraphView""" window = await self.create_test_window(512, 256) with window.frame: delegate = DelegateHeap() model = Model() graph_view = GraphView(model=model, delegate=delegate, style=style, pan_x=400, pan_y=100) # Two frames because the first frame is to draw, the second frame is to auto-layout. for i in range(10): await omni.kit.app.get_app().next_update_async() await self.finalize_test()
8,241
Python
34.991266
110
0.638393
omniverse-code/kit/exts/omni.kit.widget.graph/omni/kit/widget/graph/tests/test_graph_cache.py
## Copyright (c) 2018-2019, 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 from .test_graph import Model from omni.ui.tests.test_base import OmniUiTest from ..graph_node_index import GraphNodeIndex class ModelPlus(Model): def __init__(self): super().__init__() self._inputs = ["First.color1", "First.color2", "Third.size"] self._outputs = ["First.result", "Second.out", "Third.outcome"] self._connections = {"First.color1": ["Second.out"], "First.color2": ["Third.outcome"]} def change(self): self._inputs = ["First.color1", "First.color2", "Fourth.size"] self._outputs = ["First.result", "Second.out", "Fourth.outcome"] self._connections = {"First.color1": ["Second.out"], "First.color2": ["Fourth.outcome"]} @property def nodes(self, item=None): if item: return return sorted(set([n.split(".")[0] for n in self._inputs + self._outputs])) @property def name(self, item=None): return item.split(".")[-1] @property def ports(self, item=None): if item in self._inputs: return if item in self._outputs: return return [n for n in self._inputs + self._outputs if n.startswith(item)] @property def inputs(self, item): if item in self._inputs: return self._connections.get(item, []) @property def outputs(self, item): if item in self._outputs: return [] class TestGraphCache(OmniUiTest): async def test_general(self): model = ModelPlus() cache1 = GraphNodeIndex(model, True) cache2 = GraphNodeIndex(model, True) diff = cache1.get_diff(cache2) self.assertTrue(diff.valid) self.assertTrue(not diff.nodes_to_add) self.assertTrue(not diff.connections_to_add) self.assertTrue(not diff.nodes_to_del) self.assertTrue(not diff.connections_to_del) model.change() cache2 = GraphNodeIndex(model, True) diff = cache1.get_diff(cache2) self.assertTrue(diff.valid) # Check the difference to_add = list(sorted([n.node for n in diff.nodes_to_add])) self.assertEqual(to_add, ["First", "Fourth"]) to_add = list(sorted([(c.source_port, c.target_port) for c in diff.connections_to_add])) self.assertEqual(to_add, [("Fourth.outcome", "First.color2"), ("Second.out", "First.color1")]) to_del = list(sorted([n.node for n in diff.nodes_to_del])) self.assertEqual(to_del, ["First", "Third"]) to_del = list(sorted([(c.source_port, c.target_port) for c in diff.connections_to_del])) self.assertEqual(to_del, [("Second.out", "First.color1"), ("Third.outcome", "First.color2")])
3,154
Python
35.264367
102
0.631896
omniverse-code/kit/exts/omni.kit.widget.graph/omni/kit/widget/graph/tests/__init__.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 .test_delegate import TestDelegate from .test_graph import TestGraph from .test_graph_cache import TestGraphCache from .test_graph_with_subports import TestGraphWithSubports
620
Python
46.769227
77
0.809677
omniverse-code/kit/exts/omni.kit.widget.graph/docs/CHANGELOG.md
# Changelog Omniverse Kit Graph Widget ## [1.5.6-104_2] - 2023-01-11 ### Fixed - Connection or Node is not updated when model changes ## [1.5.4] - 2022-12-19 ### Added - Setting `/exts/omni.kit.widget.graph/raster_nodes` to rasterize the nodes ## [1.5.3] - 2022-10-28 ### Fixed - Crash when moving several nodes ## [1.5.2] - 2022-10-24 ### Added - Overview for extension ## [1.5.1] - 2022-08-31 ### Changed - make sure rebuild_node only rebuilds the node look, but not rebuilds the ports so that it will not affect connections ## [1.5.0] - 2022-08-30 ### Added - a hook of rebuild_node (rebuild node delegate) to GraphModel, so that user can call it from the model ## [1.4.7] - 2022-07-29 ### Changed - revert the previous change since it crashes the graph during the multiple selection ## [1.4.6] - 2022-07-28 ### Fixed - incorrect node moving in graph (OM-55727) ## [1.4.5] - 2022-05-26 ### Added - The change of node name, description and display_color will trigger the node delegate rebuild ## [1.4.4] - 2022-05-26 ### Fixed - multi selection quick drag resets the selection in graph view(OM-48395) ## [1.4.3] - 2022-04-20 ### Changed - A flag to automatically detect which node is added/removed and edit the widget hierarchy instead of rebuilding everything. `always_force_regenerate=False` ## [1.4.2] - 2022-04-22 ### Fixed - fix nodes go wild when the zoom level is beyond the zoom limits(OM-48407) ## [1.4.1] - 2022-04-19 ### Fixed - fix focus_on_nodes to consider zoom_min and zoom_max limits - fix unreliable tests (OM-48945) ## [1.4.0] - 2022-03-31 ### Added - add _on_set_zoom_key_shortcut API for GraphView to allow user set key shortcut to zoom the graph view ## [1.3.8] - 2022-03-17 ### Fixed - avoid modifier except ctrl and shift to trigger the selection ## [1.3.7] - 2022-03-16 ### Added - Fix the source_node_id is None loop - Add tests for graph supporting subport ## [1.3.6] - 2022-03-15 ### Fixed - Fix OM-46181 to consider edges from folded ports in the layout algorithm ## [1.3.5] - 2022-03-14 ### Fixed - The connection issue with RMB click. Clear the connection while mouse release. ## [1.3.4] - 2022-02-18 ### Changed - Fix OM-42502 about drag selection node so that as long as the selection rectangle touches the node, the node is selected ## [1.3.3] - 2022-02-18 ### Changed - Fix OM-45166 about the empty menu box in graph ## [1.3.2] - 2021-12-02 ### Added - The ability to use same side connections ## [1.3.1] - 2021-12-01 ### Added - Update graph when delegate is changed ## [1.3.0] - 2021-11-29 ### Added - GraphModelBatchPosition to simplify multiselection and backdrops ## [1.2.1] - 2021-11-12 ### Fixed - Graph model set size changed during iterating ## [1.2.0] - 2021-07-22 ### Added - GraphNodeLayout.HEAP node layout "put everything to ZStack" ## [1.1.3] - 2021-06-01 ### Added - New properties `preview_state` for the model ## [1.1.2] - 2021-05-12 ### Fixed - Port grouping in compound nodes ## [1.1.1] - 2021-05-05 ### Added - Multisilection (flag `rectangle_selection=True`) - Ability to draw connections on top (flag `connections_on_top`) - New properties `icon` and `preview` on the model ### Fixed - Ability to set `display_color` on the model ## [1.1.0] - 2021-01-25 ### Changed - The signatures of the delegate API. There is no compatibility with the previous version. ### Added - Port grouping - Smooth scrolling ## [1.0.2] - 2020-10-30 ### Added - GraphView has the new property virtual_ports that disables virtual ports. - New column node layout. - Ability to make the ports colored depending on the type. - Ability to check the port type before connection and stick the connection to the port if it can be accepted. - Display color. - IsolationGraphModel isolates any model to show the children of specific item only. ## [1.0.1] - 2020-10-14 - The ability to disable virtual ports - Two-column node layout ## [1.0.0] - 2020-10-13 ### Added - CHANGELOG - Node routing ### Changed - The signature of `AbstractGraphNodeDelegate.connection`
4,030
Markdown
25.175325
122
0.696278
omniverse-code/kit/exts/omni.kit.widget.graph/docs/overview.md
# Overview Omni.kit.widget.graph provides the base for the delegate of graph nodes. It also defines the standard interface for graph models. With the layout algorithm, GraphView gives a visualization layer of how graph nodes display and how they connect/disconnect with each to form a graph. ## Delegate The graph node's delegate defines how the graph node looks like. It has two types of layout: List and Column. It defines the appearance of the ports, header, footer and connection. The delegate keeps multiple delegates and pick them depending on the routing conditions. The conditions could be a type or a lambda expression. We use type routing to make the specific kind of nodes unique (e.g. backdrop delegate or compound delegate), and also we can use the lambda function to make the particular state of nodes unique (e.g. full expanded or collapsed delegate). ## Model GraphModel defines the base class for the Graph model. It defines the standard interface to be able to interoperate with the components of the model-view architecture. The model manages two kinds of data elements. Node and port are the atomic data elements of the model. ## Widget GraphNode Represents the Widget for the single node. Uses the model and the delegate to fill up its layout. The overall graph layout follows the method developed by Sugiyama which computes the coordinates for drawing the entire directed graphs. GraphView plays as the visualization layer of omni.kit.widget.graph. It behaves like a regular widget and displays nodes and their connections. ## Batch Position Helper This extension also adds support for batch position updates for graphs. It makes moving a collection of nodes together super easy by inheriting from GraphModelBatchPositionHelper, such as multi-selection, backdrops etc. ## Relationship with omni.kit.graph.editor.core For users to easily set up a graph framework, we provide another extension `omni.kit.graph.editor.core` which is based on this extension. omni.kit.graph.editor.core is more on the application level which defines the graph to have a catalog view to show all the available graph nodes and the graph editor view to construct the actual graph by dragging nodes from the catalog view, while `omni.kit.widget.graph` is the core definition of how graphs work. There is the documentation extension of `omni.kit.graph.docs` which explains how `omni.kit.graph.editor.core` is built up. Also we provide a real graph example extension called `omni.kit.graph.editor.example` which is based on `omni.kit.graph.editor.core`. It showcases how you can easily build a graph extension by feeding in your customized graph model and how to control the graph look by switching among different graph delegates. Here is an example graph built from `omni.kit.graph.editor.example`: ![](delegate_switch.png)
2,831
Markdown
96.655169
434
0.804309
omniverse-code/kit/exts/omni.kit.viewport.bundle/PACKAGE-LICENSES/omni.kit.viewport.bundle-LICENSE.md
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.
412
Markdown
57.999992
74
0.839806
omniverse-code/kit/exts/omni.kit.viewport.bundle/config/extension.toml
[package] # Semantic Versioning is used: https://semver.org/ version = "104.0.2" # Lists people or organizations that are considered the "authors" of the package. authors = ["NVIDIA"] # The title and description fields are primarly for displaying extension info in UI title = "Viewport Bundle" description="A bundle of Viewport extensions that creates a baseline interactive Viewport." # URL of the extension source repository. repository = "" # Keywords for the extension keywords = ["kit", "ui", "viewport", "hydra", "render"] # Location of change log file in target (final) folder of extension, relative to the root. # More info on writing changelog: https://keepachangelog.com/en/1.0.0/ changelog="docs/CHANGELOG.md" # Path (relative to the root) or content of readme markdown file for UI. readme = "docs/README.md" # Icon is shown in Extensions window, it is recommended to be square, of size 256x256. icon = "data/icon.png" preview_image = "data/preview.png" category = "Viewport" [dependencies] # Load the actual ViewportWindow extension "omni.kit.viewport.window" = {} # Load the camera-manipulator (navigation) "omni.kit.manipulator.camera" = {} # Load the prim-manipulator (translate, rotate, scale) "omni.kit.manipulator.prim" = {} # Load the selection-manipulator (selecteable prims) "omni.kit.manipulator.selection" = {} # Load drag-drop support of external files "omni.kit.window.drop_support" = {} # Load the stats HUD (resoltuin, fsp, etc..) "omni.hydra.engine.stats" = {} # Load the viewport settings menu "omni.kit.viewport.menubar.settings" = {} # Load the renderer selection menu "omni.kit.viewport.menubar.render" = {} # Load the view-from-camera menu "omni.kit.viewport.menubar.camera" = {} # Load the display type menu "omni.kit.viewport.menubar.display" = {} # Load the legacy grid and gizmo drawing for now "omni.kit.viewport.legacy_gizmos" = {} # Main python module this extension provides, it will be publicly available as "import omni.kit.viewport.bundle". # [[python.module]] # name = "omni.kit.viewport.bundle" [settings] # Setup Kit to create 'omni.kit.viewport.window' Windows named Viewport exts."omni.kit.viewport.window".startup.windowName = "Viewport" # Setting to disable opening a Window instance when loaded exts."omni.kit.viewport.window".startup.disableWindowOnLoad = false # Collapse the additional camera control area persistent.exts."omni.kit.viewport.menubar.camera".expand = false # Set the default perspective camera focalLength persistent.app.primCreation.typedDefaults.camera.focalLength = 18.147562 # Default legacy display options for any consumers (all visible but Skeletons) persistent.app.viewport.displayOptions = 32255 [[test]] # This is just a collection of extensions, they should be tested indivdiually for now waiver = ""
2,796
TOML
36.293333
113
0.757511
omniverse-code/kit/exts/omni.kit.viewport.bundle/docs/CHANGELOG.md
# CHANGELOG This document records all notable changes to ``omni.kit.viewport.bundle`` extension. This project adheres to `Semantic Versioning <https://semver.org/>`_. ## [104.0.2] - 2022-09-28 ### Added - New Lighting menu item. ## [104.0.1] - 2022-08-26 ### Added - Default legacy Viewport displayOptions to all visible except Skeletons. ## [104.0.0] - 2022-05-04 ### Added - Initial version
397
Markdown
22.411763
84
0.700252
omniverse-code/kit/exts/omni.kit.viewport.bundle/docs/README.md
# Viewport Bundle Extension [omni.kit.viewport.bundle] A bundle of Viewport extensions that creates a baseline interactive Viewport.
133
Markdown
43.666652
77
0.827068
omniverse-code/kit/exts/omni.kit.viewport.bundle/docs/index.rst
omni.kit.viewport.bundle ########################### Viewport Bundle .. toctree:: :maxdepth: 1 README CHANGELOG .. automodule:: omni.kit.viewport.bundle :platform: Windows-x86_64, Linux-x86_64 :members: :undoc-members: :show-inheritance: :imported-members:
292
reStructuredText
13.649999
43
0.60274
omniverse-code/kit/exts/omni.kit.livestream.native/PACKAGE-LICENSES/omni.kit.livestream.native-LICENSE.md
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.
412
Markdown
57.999992
74
0.839806
omniverse-code/kit/exts/omni.kit.livestream.native/config/extension.toml
[package] version = "0.1.5" title = "Livestream Native Backend" description = "Enable streaming capabilities, making the application useable remotely." readme = "docs/README.md" repository = "" category = "Rendering" keywords = ["streaming", "server"] changelog = "docs/CHANGELOG.md" icon = "data/icon.png" preview_image = "data/preview.png" [[python.module]] name = "omni.kit.livestream.native" [dependencies] "omni.kit.livestream.core" = {} "omni.kit.streamsdk.plugins" = {} # Declare an optional dependency on the Streaming Manager, as the extension may # not be available in the standalone Kit SDK, but be available in Omniverse # applications consuming other streaming extensions: "omni.services.streaming.manager" = { optional = true } [[native.plugin]] path = "${omni.kit.streamsdk.plugins}/bin/carb.livestream.plugin" [settings] app.livestream.port = 48010 app.livestream.proto = "websocket" app.livestream.ipversion = "auto" app.livestream.allowResize = true # Prevent the viewport from being throttled down when using native streaming, # as it may assume that the application is out of the User's focus during # streaming sessions: app.renderer.skipWhileMinimized = false app.renderer.sleepMsOnFocus = 0 app.renderer.sleepMsOutOfFocus = 0 [[test]] waiver = "Bundle extension" # OM-48114
1,304
TOML
29.348837
87
0.757669
omniverse-code/kit/exts/omni.kit.livestream.native/omni/kit/livestream/native/__init__.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 .scripts.extension import *
460
Python
45.099995
76
0.808696
omniverse-code/kit/exts/omni.kit.livestream.native/omni/kit/livestream/native/scripts/extension.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. """Native streaming extension.""" import carb import omni.ext import omni.kit.livestream.bind class NativeStreamingExtension(omni.ext.IExt): """Native streaming extension.""" def __init__(self) -> None: super().__init__() self._stream_interface = None def on_startup(self) -> None: self._kit_livestream = omni.kit.livestream.bind.acquire_livestream_interface() self._kit_livestream.startup() self._register_streaming_interface() def on_shutdown(self) -> None: self._unregister_streaming_interface() self._kit_livestream.shutdown() self._kit_livestream = None def _register_streaming_interface(self) -> None: """Register the streaming interface for the extension.""" try: from omni.services.streaming.manager import get_stream_manager, StreamManager from .streaming_interface import NativeStreamInterface self._stream_interface = NativeStreamInterface() stream_manager: StreamManager = get_stream_manager() stream_manager.register_stream_interface(stream_interface=self._stream_interface) stream_manager.enable_stream_interface(stream_interface_id=self._stream_interface.id) except ImportError: carb.log_info("The Streaming Manager extension was not available when enabling native streaming features.") except Exception as exc: carb.log_error(f"An error occurred while attempting to register the native streaming interface: {str(exc)}.") def _unregister_streaming_interface(self) -> None: """Unregister the streaming interface for the extension.""" if self._stream_interface is None: # No stream interface was register when enabling the extension, so there is no need to attempt to unregister # it and the function can exit early: return try: from omni.services.streaming.manager import get_stream_manager, StreamManager stream_manager: StreamManager = get_stream_manager() stream_manager.disable_stream_interface(stream_interface_id=self._stream_interface.id) stream_manager.unregister_stream_interface(stream_interface_id=self._stream_interface.id) except ImportError: carb.log_info("The Streaming Manager extension was not available when disabling native streaming features.") except Exception as exc: carb.log_error(f"An error occurred while attempting to unregister the native streaming interface: {str(exc)}.")
3,019
Python
44.757575
123
0.700232
omniverse-code/kit/exts/omni.kit.livestream.native/omni/kit/livestream/native/scripts/__init__.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.
426
Python
52.374993
76
0.812207
omniverse-code/kit/exts/omni.kit.livestream.native/omni/kit/livestream/native/scripts/streaming_interface.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. """Native streaming interface.""" import os from typing import List import psutil import carb.settings from omni.services.streaming.manager import StreamInterface class NativeStreamInterface(StreamInterface): """Native streaming interface.""" @property def id(self) -> str: return "Native" @property def menu_label(self) -> str: return "Native" @property def module_name(self) -> str: return __name__ @property def stream_urls(self) -> List[str]: return self.local_ips async def is_healthy(self) -> bool: """ Check if the streaming server is in a state that is considered healthy (i.e. that the streaming server listens for connection requests on the configured port). Args: None Returns: bool: A flag indicating whether the streaming server is in a healthy state. """ # Confirm if the superclass may have already flagged the stream as being in an unhealthy state, in order to # potentially return early: is_healthy = await super().is_healthy() if not is_healthy: return is_healthy # Check that the host process has the expected native streaming server port in a "LISTENING" state by querying # its process, rather than issuing an actual request against the server: kit_process = psutil.Process(pid=os.getpid()) expected_server_port = self._get_native_streaming_port_number() is_listening_on_expected_port = False for connection in kit_process.connections(): if connection.laddr.port == expected_server_port: if connection.status is psutil.CONN_LISTEN: is_listening_on_expected_port = True break return is_listening_on_expected_port def _get_native_streaming_port_number(self) -> int: """ Return the port number of on which the streaming server is expected to receive connection requests. Args: None Returns: int: The port number of on which the streaming server is expected to receive connection requests. """ settings = carb.settings.get_settings() native_server_port = settings.get_as_int("app/livestream/port") return native_server_port
2,789
Python
31.44186
118
0.665113
omniverse-code/kit/exts/omni.kit.livestream.native/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). ## [0.1.5] - 2022-09-06 ### Added - Added streaming manager interface to the extension, in order to handle the lifecycle of other streaming extensions also potentially enabled at the time native streaming is enabled. - Added ability to perform health checks on the stream based on whether the signaling port of the native extension is set to listen for connections on the Omniverse application process. ## [0.1.4] - 2022-05-30 ### Added - Updated icon and preview of the extension. ## [0.1.3] - 2022-04-25 ### Added - Added test waiver ## [0.1.2] - 2022-03-19 ### Added - Updated metadata information about the extension. ## [0.1.1] - 2022-02-23 ### Added - Added metadata information about the extension.
963
Markdown
33.42857
185
0.737279
omniverse-code/kit/exts/omni.kit.livestream.native/docs/README.md
# Livestream Native Backend [omni.kit.livestream.native] Server-side native streaming feature, allowing Users to interact with Omniverse applications over the network or the Internet. For details about using streaming clients on end-User machines, download and install "Kit Remote" from the Omniverse Launcher. Visit https://docs.omniverse.nvidia.com for additional deployment and configuration options.
406
Markdown
66.833322
219
0.825123
omniverse-code/kit/exts/omni.kit.livestream.native/docs/index.rst
omni.kit.livestream.native ########################## This section presents the server-side native streaming feature, allowing Users to interact with Omniverse applications from clients over the network or the Internet. Python API Reference ********************* .. automodule:: omni.kit.livestream.native :platform: Windows-x86_64, Linux-x86_64 :members: :undoc-members: :imported-members:
410
reStructuredText
28.357141
165
0.678049
omniverse-code/kit/exts/omni.kit.stage_templates/PACKAGE-LICENSES/omni.kit.stage_templates-LICENSE.md
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.
412
Markdown
57.999992
74
0.839806
omniverse-code/kit/exts/omni.kit.stage_templates/config/extension.toml
[package] # Semantic Versioning is used: https://semver.org/ version = "1.1.13" category = "Internal" # Lists people or organizations that are considered the "authors" of the package. authors = ["NVIDIA"] # The title and description fields are primarly for displaying extension info in UI title = "Stage Templates" description="Allows custom stage templates to be loaded on omni.usd.get_context().new_stage_async()" [dependencies] "omni.kit.commands" = {} "omni.usd" = {} "omni.kit.actions.core" = {} "omni.kit.primitive.mesh" = {} [settings] persistent.app.newStage.defaultTemplate = "sunlight" persistent.app.newStage.templatePath = ["${app_documents}/scripts/new_stage"] persistent.app.stage.timeCodeRange = [0, 100] [[python.module]] name = "omni.kit.stage_templates" [[test]] args = [ "--/renderer/enabled=pxr", "--/renderer/active=pxr", "--/renderer/multiGpu/enabled=false", "--/renderer/multiGpu/autoEnable=false", # Disable mGPU with PXR due to OM-51026, OM-53611 "--/renderer/multiGpu/maxGpuCount=1", "--/app/asyncRendering=false", "--/app/file/ignoreUnsavedStage=true", "--/app/window/dpiScaleOverride=1.0", "--/app/window/scaleToMonitor=false", "--no-window", ] dependencies = [ "omni.hydra.pxr", "omni.kit.renderer.capture", "omni.kit.mainwindow", "omni.kit.window.file", "omni.kit.menu.utils", "omni.kit.window.preferences", "omni.kit.property.usd", "omni.kit.ui_test", "omni.kit.test_suite.helpers", "omni.kit.material.library" ] stdoutFailPatterns.exclude = [ "*HydraRenderer failed to render this frame*", # Can drop a frame or two rendering with OpenGL interop "*Cannot use omni.hydra.pxr without OpenGL interop*" # Linux TC configs with multi-GPU might not have OpenGL available ] pyCoverageFilter = ["omni.kit.stage_templates"] # Restrict coverage to this extension
1,884
TOML
30.416666
122
0.699045
omniverse-code/kit/exts/omni.kit.stage_templates/omni/kit/stage_templates/__init__.py
# NOTE: all imported classes must have different class names from .new_stage import * from .templates import *
111
Python
26.999993
60
0.774775
omniverse-code/kit/exts/omni.kit.stage_templates/omni/kit/stage_templates/stage_templates_menu.py
import locale import carb import functools import carb.settings import omni.kit.app from functools import partial def get_action_name(name): return name.lower().replace('-', '_').replace(' ', '_') class StageTemplateMenu: def __init__(self): pass def on_startup(self): self._build_sub_menu() omni.kit.menu.utils.add_menu_items(self._menu_list, "File") # update submenu if defaultTemplate changes self._update_setting = omni.kit.app.SettingChangeSubscription( "/persistent/app/newStage/defaultTemplate", lambda *_: omni.kit.menu.utils.refresh_menu_items("File") ) def on_shutdown(self): self._update_setting = None omni.kit.menu.utils.remove_menu_items(self._menu_list, "File") self._menu_list = None def _build_sub_menu(self): from omni.kit.menu.utils import MenuItemDescription def sort_cmp(template1, template2): return locale.strcoll(template1[0], template2[0]) sub_menu = [] default_template = omni.kit.stage_templates.get_default_template() stage_templates = omni.kit.stage_templates.get_stage_template_list() def template_ticked(sn: str) -> bool: return omni.kit.stage_templates.get_default_template() == sn for template_list in stage_templates: for template in sorted(template_list.items(), key=functools.cmp_to_key(sort_cmp)): stage_name = template[0] sub_menu.append( MenuItemDescription( name=stage_name.replace("_", " ").title(), ticked=True, ticked_fn=lambda sn=template[0]: template_ticked(sn), onclick_action=("omni.kit.stage.templates", f"create_new_stage_{get_action_name(stage_name)}") ) ) self._menu_list = [ MenuItemDescription( name="New From Stage Template", glyph="file.svg", appear_after=["Open Recent", "New"], sub_menu=sub_menu ) ] def _rebuild_sub_menu(self): if self._menu_list: omni.kit.menu.utils.remove_menu_items(self._menu_list, "File") self._build_sub_menu() omni.kit.menu.utils.add_menu_items(self._menu_list, "File")
2,350
Python
34.089552
120
0.589362
omniverse-code/kit/exts/omni.kit.stage_templates/omni/kit/stage_templates/stage_templates_page.py
import re import os import carb.settings import omni.kit.app import omni.ui as ui from functools import partial from omni.kit.window.preferences import PreferenceBuilder, show_file_importer, SettingType, PERSISTENT_SETTINGS_PREFIX class StageTemplatesPreferences(PreferenceBuilder): def __init__(self): super().__init__("Template Startup") # update on setting change def on_change(item, event_type): if event_type == carb.settings.ChangeEventType.CHANGED: omni.kit.window.preferences.rebuild_pages() self._update_setting = omni.kit.app.SettingChangeSubscription(PERSISTENT_SETTINGS_PREFIX + "/app/newStage/templatePath", on_change) def build(self): template_paths = carb.settings.get_settings().get("/persistent/app/newStage/templatePath") default_template = omni.kit.stage_templates.get_default_template() script_names = [] new_templates = omni.kit.stage_templates.get_stage_template_list() for templates in new_templates: for template in templates.items(): script_names.append(template[0]) if len(script_names) == 0: script_names = ["None##None"] with ui.VStack(height=0): with self.add_frame("New Stage Template"): with ui.VStack(): for index, path in enumerate(template_paths): with ui.HStack(height=24): self.label("Path to user templates") widget = ui.StringField(height=20) widget.model.set_value(path) ui.Button(style={"image_url": "resources/icons/folder.png"}, clicked_fn=lambda p=self.cleanup_slashes(carb.tokens.get_tokens_interface().resolve(path)), i=index, w=widget: self._on_browse_button_fn(p, i, w), width=24) self.create_setting_widget_combo("Default Template", PERSISTENT_SETTINGS_PREFIX + "/app/newStage/defaultTemplate", script_names) def _on_browse_button_fn(self, path, index, widget): """ Called when the user picks the Browse button. """ show_file_importer( "Select Template Directory", click_apply_fn=lambda p=self.cleanup_slashes(path), i=index, w=widget: self._on_file_pick( p, index=i, widget=w), filename_url=path) def _on_file_pick(self, full_path, index, widget): """ Called when the user accepts directory in the Select Directory dialog. """ directory = self.cleanup_slashes(full_path, True) settings = carb.settings.get_settings() template_paths = settings.get(PERSISTENT_SETTINGS_PREFIX + "/app/newStage/templatePath") template_paths[index] = directory settings.set(PERSISTENT_SETTINGS_PREFIX + "/app/newStage/templatePath", template_paths) widget.model.set_value(directory)
2,921
Python
46.901639
245
0.636768
omniverse-code/kit/exts/omni.kit.stage_templates/omni/kit/stage_templates/new_stage.py
import os import carb import carb.settings import omni.ext import omni.kit.app import omni.usd import asyncio import glob import omni.kit.actions.core from inspect import signature from functools import partial from pxr import Sdf, UsdGeom, Usd, Gf from contextlib import suppress from .stage_templates_menu import get_action_name _extension_instance = None _extension_path = None _template_list = [] _clear_dirty_task = None def _try_unregister_page(page: str): with suppress(Exception): import omni.kit.window.preferences omni.kit.window.preferences.unregister_page(page) # must be initalized before templates class NewStageExtension(omni.ext.IExt): def on_startup(self, ext_id): global _extension_path global clear_dirty_task _extension_path = omni.kit.app.get_app().get_extension_manager().get_extension_path(ext_id) _clear_dirty_task = None register_template("empty", self.new_stage_empty, 0) omni.kit.stage_templates.templates.load_templates() self.load_user_templates() # as omni.kit.stage_templates loads before the UI, it cannot depend on omni.kit.window.preferences or other extensions. self._preferences_page = None self._menu_button1 = None self._menu_button2 = None self._hooks = [] manager = omni.kit.app.get_app().get_extension_manager() self._hooks.append( manager.subscribe_to_extension_enable( on_enable_fn=lambda _: self._register_page(), on_disable_fn=lambda _: self._unregister_page(), ext_name="omni.kit.window.preferences", hook_name="omni.kit.stage_templates omni.kit.window.preferences listener", ) ) self._stage_template_menu = None self._hooks.append( manager.subscribe_to_extension_enable( on_enable_fn=lambda _: self._register_menu(), on_disable_fn=lambda _: self._unregister_menu(), ext_name="omni.kit.menu.utils", hook_name="omni.kit.stage_templates omni.kit.menu.utils listener", ) ) self._hooks.append( manager.subscribe_to_extension_enable( on_enable_fn=lambda _: self._register_property_menu(), on_disable_fn=lambda _: self._unregister_property_menu(), ext_name="omni.kit.property.usd", hook_name="omni.kit.stage_templates omni.kit.property.usd listener", ) ) global _extension_instance _extension_instance = self def on_shutdown(self): global _extension_instance global _template_list global _clear_dirty_task if _clear_dirty_task: _clear_dirty_task.cancel() _clear_dirty_task = None unregister_template("empty") self._unregister_page() self._unregister_menu() self._unregister_property_menu() self._hooks = None _extension_instance = None _template_list = None for user_template in self._user_scripts: del user_template omni.kit.stage_templates.templates.unload_templates() self._user_scripts = None def _register_page(self): try: from omni.kit.window.preferences import register_page from .stage_templates_page import StageTemplatesPreferences self._preferences_page = register_page(StageTemplatesPreferences()) except ModuleNotFoundError: pass def _unregister_page(self): if self._preferences_page: try: import omni.kit.window.preferences omni.kit.window.preferences.unregister_page(self._preferences_page) self._preferences_page = None except ModuleNotFoundError: pass def _register_menu(self): try: from .stage_templates_menu import StageTemplateMenu self._stage_template_menu = StageTemplateMenu() self._stage_template_menu.on_startup() except ModuleNotFoundError: pass def _unregister_menu(self): if self._stage_template_menu: try: self._stage_template_menu.on_shutdown() self._stage_template_menu = None except ModuleNotFoundError: pass def _has_axis(self, objects, axis): if not "stage" in objects: return False stage = objects["stage"] if stage: return UsdGeom.GetStageUpAxis(stage) != axis else: carb.log_error("_click_set_up_axis stage not found") return False def _click_set_up_axis(self, payload, axis): stage = payload.get_stage() if stage: rootLayer = stage.GetRootLayer() if rootLayer: rootLayer.SetPermissionToEdit(True) with Usd.EditContext(stage, rootLayer): UsdGeom.SetStageUpAxis(stage, axis) from omni.kit.property.usd import PrimPathWidget PrimPathWidget.rebuild() else: carb.log_error("_click_set_up_axis rootLayer not found") else: carb.log_error("_click_set_up_axis stage not found") def _register_property_menu(self): # +add menu item(s) from omni.kit.property.usd import PrimPathWidget context_menu = omni.kit.context_menu.get_instance() if context_menu is None: self._menu_button1 = None self._menu_button2 = None carb.log_error("context_menu is disabled!") return None self._menu_button1 = PrimPathWidget.add_button_menu_entry( "Stage/Set up axis +Y", show_fn=partial(self._has_axis, axis=UsdGeom.Tokens.y), onclick_fn=partial(self._click_set_up_axis, axis=UsdGeom.Tokens.y), add_to_context_menu=False, ) self._menu_button2 = PrimPathWidget.add_button_menu_entry( "Stage/Set up axis +Z", show_fn=partial(self._has_axis, axis=UsdGeom.Tokens.z), onclick_fn=partial(self._click_set_up_axis, axis=UsdGeom.Tokens.z), add_to_context_menu=False, ) def _unregister_property_menu(self): if self._menu_button1 or self._menu_button2: try: from omni.kit.property.usd import PrimPathWidget if self._menu_button1: PrimPathWidget.remove_button_menu_entry(self._menu_button1) self._menu_button1 = None if self._menu_button2: PrimPathWidget.remove_button_menu_entry(self._menu_button2) self._menu_button2 = None except ModuleNotFoundError: pass def new_stage_empty(self, rootname): pass def load_user_templates(self): settings = carb.settings.get_settings() template_paths = settings.get("/persistent/app/newStage/templatePath") # Create template directories original_umask = os.umask(0) for path in template_paths: path = carb.tokens.get_tokens_interface().resolve(path) if not os.path.isdir(path): try: os.makedirs(path) except Exception: carb.log_error(f"Failed to create directory {path}") os.umask(original_umask) # Load template scripts self._user_scripts = [] for path in template_paths: for full_path in glob.glob(f"{path}/*.py"): try: with open(os.path.normpath(full_path)) as f: user_script = f.read() carb.log_verbose(f"loaded new_stage template {full_path}") exec(user_script) self._user_scripts.append(user_script) except Exception as e: carb.log_error(f"error loading {full_path}: {e}") def register_template(name, new_stage_fn, group=0, rebuild=True): """Register new_stage Template Args: param1 (str): template name param2 (callable): function to create template param3 (int): group number. User by menu to split into groups with seperators Returns: bool: True for success, False when template already exists. """ # check if element exists global _template_list exists = get_stage_template(name) if exists: carb.log_warn(f"template {name} already exists") return False try: exists = _template_list[group] except IndexError: _template_list.insert(group, {}) _template_list[group][name] = (name, new_stage_fn) omni.kit.actions.core.get_action_registry().register_action( "omni.kit.stage.templates", f"create_new_stage_{get_action_name(name)}", lambda t=name: omni.kit.window.file.new(t), display_name=f"Create New Stage {name}", description=f"Create New Stage {name}", tag="Create Stage Template", ) if rebuild: rebuild_stage_template_menu() return True def unregister_template(name, rebuild: bool=True): """Remove registered new_stage Template Args: param1 (str): template name Returns: nothing """ global _template_list if _template_list is not None: for templates in _template_list: if name in templates: del templates[name] if rebuild: rebuild_stage_template_menu() omni.kit.actions.core.get_action_registry().deregister_action("omni.kit.stage.templates", f"create_new_stage_{get_action_name(name)}") def rebuild_stage_template_menu() -> None: if _extension_instance and _extension_instance._stage_template_menu: _extension_instance._stage_template_menu._rebuild_sub_menu() def get_stage_template_list(): """Get list of loaded new_stage templates Args: none Returns: list: list of groups of new_stage template names & create function pointers """ global _template_list if not _template_list or len(_template_list) == 0: return None return _template_list def get_stage_template(name): """Get named new_stage template & create function pointer Args: param1 (str): template name Returns: tuple: new_stage template name & create function pointer """ global _template_list if not _template_list: return None for templates in _template_list: if name in templates: return templates[name] return None def get_default_template(): """Get name of default new_stage template. Used when new_stage is called without template name specified Args: param1 (str): template name Returns: str: new_stage template name """ settings = carb.settings.get_settings() return settings.get("/persistent/app/newStage/defaultTemplate") def new_stage_finalize(create_result, error_message, usd_context, template=None, on_new_stage_fn=None): global _clear_dirty_task if _clear_dirty_task: _clear_dirty_task.cancel() _clear_dirty_task = None if create_result: stage = usd_context.get_stage() # already finilized if stage.HasDefaultPrim(): return with Usd.EditContext(stage, stage.GetRootLayer()): settings = carb.settings.get_settings() default_prim_name = settings.get("/persistent/app/stage/defaultPrimName") up_axis = settings.get("/persistent/app/stage/upAxis") time_codes_per_second = settings.get_as_float("/persistent/app/stage/timeCodesPerSecond") time_code_range = settings.get("/persistent/app/stage/timeCodeRange") if time_code_range is None: time_code_range = [0, 100] rootname = f"/{default_prim_name}" # Set up axis if up_axis == "Y" or up_axis == "y": UsdGeom.SetStageUpAxis(stage, UsdGeom.Tokens.y) else: UsdGeom.SetStageUpAxis(stage, UsdGeom.Tokens.z) # Set timecodes per second stage.SetTimeCodesPerSecond(time_codes_per_second) # Start and end time code if time_code_range: stage.SetStartTimeCode(time_code_range[0]) stage.SetEndTimeCode(time_code_range[1]) # Create defaultPrim default_prim = UsdGeom.Xform.Define(stage, Sdf.Path(rootname)).GetPrim() if not default_prim: carb.log_error("Failed to create defaultPrim at {rootname}") return stage.SetDefaultPrim(default_prim) if template is None: template = get_default_template() # Run script item = get_stage_template(template) if item and item[1]: try: create_fn = item[1] sig = signature(create_fn) if len(sig.parameters) == 1: create_fn(rootname) elif len(sig.parameters) == 2: create_fn(rootname, usd_context.get_name()) else: carb.log_error(f"template {template} has incorrect parameter count") except Exception as error: carb.log_error(f"exception in {template} {error}") omni.kit.undo.clear_stack() usd_context.set_pending_edit(False) # Clear the stage dirty state again, as bound materials can set it async def clear_dirty(usd_context): import omni.kit.app await omni.kit.app.get_app().next_update_async() await omni.kit.app.get_app().next_update_async() usd_context.set_pending_edit(False) _clear_dirty_task = None if on_new_stage_fn: on_new_stage_fn(create_result, error_message) _clear_dirty_task = asyncio.ensure_future(clear_dirty(usd_context)) def new_stage(on_new_stage_fn=None, template="empty", usd_context=None): """Execute new_stage Args: param2 (str): template name, if not specified default name is used param3 (omni.usd.UsdContext): usd_context, the usd_context to create new stage Returns: nothing """ if usd_context is None: usd_context = omni.usd.get_context() if on_new_stage_fn is not None: carb.log_warn( "omni.kit.stage_templates.new_stage(callback, ...) is deprecated. \ Use `omni.kit.stage_templates.new_stage_with_callback` instead." ) new_stage_with_callback(on_new_stage_fn, template, usd_context) else: new_stage_finalize(usd_context.new_stage(), "", usd_context, template=template, on_new_stage_fn=None) def new_stage_with_callback(on_new_stage_fn=None, template="empty", usd_context=None): """Execute new_stage Args: param1 (callable): callback when new_stage is created param2 (str): template name, if not specified default name is used param3 (omni.usd.UsdContext): usd_context, the usd_context to create new stage Returns: nothing """ if usd_context is None: usd_context = omni.usd.get_context() usd_context.new_stage_with_callback( partial(new_stage_finalize, usd_context=usd_context, template=template, on_new_stage_fn=on_new_stage_fn) ) async def new_stage_async(template="empty", usd_context=None): """Execute new_stage asynchronously Args: param1 (str): template name, if not specified default name is used param2 (omni.usd.UsdContext): usd_context, the usd_context to create new stage Returns: awaitable object until stage is created """ if usd_context is None: usd_context = omni.usd.get_context() f = asyncio.Future() def on_new_stage(result, err_msg): if not f.done(): f.set_result((result, err_msg)) new_stage_with_callback(on_new_stage, template, usd_context) return await f def get_extension_path(sub_directory): global _extension_path path = _extension_path if sub_directory: path = os.path.normpath(os.path.join(path, sub_directory)) return path def set_transform_helper( prim_path, translate=Gf.Vec3d(0, 0, 0), euler=Gf.Vec3d(0, 0, 0), scale=Gf.Vec3d(1, 1, 1), ): rotation = ( Gf.Rotation(Gf.Vec3d.ZAxis(), euler[2]) * Gf.Rotation(Gf.Vec3d.YAxis(), euler[1]) * Gf.Rotation(Gf.Vec3d.XAxis(), euler[0]) ) xform = Gf.Matrix4d().SetScale(scale) * Gf.Matrix4d().SetRotate(rotation) * Gf.Matrix4d().SetTranslate(translate) omni.kit.commands.execute( "TransformPrim", path=prim_path, new_transform_matrix=xform, )
17,039
Python
31.706334
138
0.601326
omniverse-code/kit/exts/omni.kit.stage_templates/omni/kit/stage_templates/templates/default_stage.py
import carb import omni.ext import omni.kit.commands from pxr import UsdLux, UsdShade, Kind, Vt, Gf, UsdGeom, Sdf from ..new_stage import * class DefaultStage: def __init__(self): register_template("default stage", self.new_stage) def __del__(self): unregister_template("default stage") def new_stage(self, rootname, usd_context_name): # S3 URL's carlight_hdr = Sdf.AssetPath("https://omniverse-content-production.s3.us-west-2.amazonaws.com/Assets/Scenes/Templates/Default/SubUSDs/textures/CarLight_512x256.hdr") grid_basecolor = Sdf.AssetPath("https://omniverse-content-production.s3.us-west-2.amazonaws.com/Assets/Scenes/Templates/Default/SubUSDs/textures/ov_uv_grids_basecolor_1024.png") # change ambientLightColor carb.settings.get_settings().set("/rtx/sceneDb/ambientLightColor", (0, 0, 0)) carb.settings.get_settings().set("/rtx/indirectDiffuse/enabled", True) carb.settings.get_settings().set("/rtx/domeLight/upperLowerStrategy", 0) carb.settings.get_settings().set("/rtx/post/lensFlares/flareScale", 0.075) carb.settings.get_settings().set("/rtx/sceneDb/ambientLightIntensity", 0) # get up axis usd_context = omni.usd.get_context(usd_context_name) stage = usd_context.get_stage() up_axis = UsdGeom.GetStageUpAxis(stage) with Usd.EditContext(stage, stage.GetRootLayer()): # create Environment omni.kit.commands.execute( "CreatePrim", prim_path="/Environment", prim_type="Xform", select_new_prim=False, create_default_xform=True, context_name=usd_context_name ) prim = stage.GetPrimAtPath("/Environment") prim.CreateAttribute("ground:size", Sdf.ValueTypeNames.Int, False).Set(1400) prim.CreateAttribute("ground:type", Sdf.ValueTypeNames.String, False).Set("On") # create Sky omni.kit.commands.execute( "CreatePrim", prim_path="/Environment/Sky", prim_type="DomeLight", select_new_prim=False, attributes={ UsdLux.Tokens.inputsIntensity: 1, UsdLux.Tokens.inputsColorTemperature: 6250, UsdLux.Tokens.inputsEnableColorTemperature: True, UsdLux.Tokens.inputsExposure: 9, UsdLux.Tokens.inputsTextureFile: carlight_hdr, UsdLux.Tokens.inputsTextureFormat: UsdLux.Tokens.latlong, UsdGeom.Tokens.visibility: "inherited", } if hasattr(UsdLux.Tokens, 'inputsIntensity') else \ { UsdLux.Tokens.intensity: 1, UsdLux.Tokens.colorTemperature: 6250, UsdLux.Tokens.enableColorTemperature: True, UsdLux.Tokens.exposure: 9, UsdLux.Tokens.textureFile: carlight_hdr, UsdLux.Tokens.textureFormat: UsdLux.Tokens.latlong, UsdGeom.Tokens.visibility: "inherited", }, create_default_xform=True, context_name=usd_context_name ) prim = stage.GetPrimAtPath("/Environment/Sky") prim.CreateAttribute("xformOp:scale", Sdf.ValueTypeNames.Double3, False).Set(Gf.Vec3d(1, 1, 1)) if up_axis == "Y": prim.CreateAttribute("xformOp:translate", Sdf.ValueTypeNames.Double3, False).Set(Gf.Vec3d(0, 305, 0)) prim.CreateAttribute("xformOp:rotateXYZ", Sdf.ValueTypeNames.Double3, False).Set(Gf.Vec3d(0, -90, -90)) else: prim.CreateAttribute("xformOp:translate", Sdf.ValueTypeNames.Double3, False).Set(Gf.Vec3d(0, 0, 305)) prim.CreateAttribute("xformOp:rotateXYZ", Sdf.ValueTypeNames.Double3, False).Set(Gf.Vec3d(0, 0, 0)) prim.CreateAttribute("xformOpOrder", Sdf.ValueTypeNames.String, False).Set(["xformOp:translate", "xformOp:rotateXYZ", "xformOp:scale"]) # create DistantLight omni.kit.commands.execute( "CreatePrim", prim_path="/Environment/DistantLight", prim_type="DistantLight", select_new_prim=False, attributes={UsdLux.Tokens.inputsAngle: 2.5, UsdLux.Tokens.inputsIntensity: 1, UsdLux.Tokens.inputsColorTemperature: 7250, UsdLux.Tokens.inputsEnableColorTemperature: True, UsdLux.Tokens.inputsExposure: 10, UsdGeom.Tokens.visibility: "inherited", } if hasattr(UsdLux.Tokens, 'inputsIntensity') else \ { UsdLux.Tokens.angle: 2.5, UsdLux.Tokens.intensity: 1, UsdLux.Tokens.colorTemperature: 7250, UsdLux.Tokens.enableColorTemperature: True, UsdLux.Tokens.exposure: 10, UsdGeom.Tokens.visibility: "inherited", }, create_default_xform=True, context_name=usd_context_name ) prim = stage.GetPrimAtPath("/Environment/DistantLight") if up_axis == "Y": prim.CreateAttribute("xformOp:translate", Sdf.ValueTypeNames.Double3, False).Set(Gf.Vec3d(0, 305, 0)) prim.CreateAttribute("xformOp:rotateXYZ", Sdf.ValueTypeNames.Double3, False).Set(Gf.Vec3d(-105, 0, 0)) else: prim.CreateAttribute("xformOp:translate", Sdf.ValueTypeNames.Double3, False).Set(Gf.Vec3d(0, 0, 305)) prim.CreateAttribute("xformOp:rotateXYZ", Sdf.ValueTypeNames.Double3, False).Set(Gf.Vec3d(-15, 0, 0)) prim.CreateAttribute("xformOpOrder", Sdf.ValueTypeNames.String, False).Set(["xformOp:translate", "xformOp:rotateXYZ", "xformOp:scale"]) # Material "Grid" mtl_path = omni.usd.get_stage_next_free_path(stage, "/Environment/Looks/Grid", False) omni.kit.commands.execute("CreateMdlMaterialPrim", mtl_url="OmniPBR.mdl", mtl_name="Grid", mtl_path=mtl_path, context_name=usd_context_name) mat_prim = stage.GetPrimAtPath(mtl_path) shader = UsdShade.Material(mat_prim).ComputeSurfaceSource("mdl")[0] shader.SetSourceAssetSubIdentifier("OmniPBR", "mdl") # set inputs omni.usd.create_material_input(mat_prim, "albedo_add", 0, Sdf.ValueTypeNames.Float) omni.usd.create_material_input(mat_prim, "albedo_brightness", 0.52, Sdf.ValueTypeNames.Float) omni.usd.create_material_input(mat_prim, "albedo_desaturation", 1, Sdf.ValueTypeNames.Float) omni.usd.create_material_input(mat_prim, "project_uvw", False, Sdf.ValueTypeNames.Bool) omni.usd.create_material_input(mat_prim, "reflection_roughness_constant", 0.333, Sdf.ValueTypeNames.Float) omni.usd.create_material_input(mat_prim, "diffuse_texture", grid_basecolor, Sdf.ValueTypeNames.Asset, def_value=Sdf.AssetPath(""), color_space="sRGB") omni.usd.create_material_input(mat_prim, "texture_rotate", 0, Sdf.ValueTypeNames.Float, def_value=Vt.Float(0.0)) omni.usd.create_material_input(mat_prim, "texture_scale", Gf.Vec2f(0.5, 0.5), Sdf.ValueTypeNames.Float2, def_value=Gf.Vec2f(1, 1)) omni.usd.create_material_input(mat_prim, "texture_translate", Gf.Vec2f(0, 0), Sdf.ValueTypeNames.Float2, def_value=Gf.Vec2f(0, 0)) omni.usd.create_material_input(mat_prim, "world_or_object", False, Sdf.ValueTypeNames.Bool, def_value=Vt.Bool(False)) # Ground ground_path = "/Environment/ground" omni.kit.commands.execute( "CreateMeshPrimWithDefaultXform", prim_path=ground_path, prim_type="Plane", select_new_prim=False, prepend_default_prim=False, context_name=usd_context_name ) prim = stage.GetPrimAtPath(ground_path) prim.CreateAttribute("xformOp:translate", Sdf.ValueTypeNames.Double3, False).Set(Gf.Vec3d(0, 0, 0)) prim.CreateAttribute("xformOp:scale", Sdf.ValueTypeNames.Double3, False).Set(Gf.Vec3d(1, 1, 1)) prim.CreateAttribute("xformOpOrder", Sdf.ValueTypeNames.String, False).Set(["xformOp:translate", "xformOp:rotateXYZ", "xformOp:scale"]) if up_axis == "Y": prim.CreateAttribute("xformOp:rotateXYZ", Sdf.ValueTypeNames.Double3, False).Set(Gf.Vec3d(0, -90, -90)) else: prim.CreateAttribute("xformOp:rotateXYZ", Sdf.ValueTypeNames.Double3, False).Set(Gf.Vec3d(0, 0, 0)) mesh = UsdGeom.Mesh(prim) mesh.CreateSubdivisionSchemeAttr("none") mesh.GetFaceVertexCountsAttr().Set([4]) mesh.GetFaceVertexIndicesAttr().Set([0, 1, 3, 2]) mesh.GetPointsAttr().Set(Vt.Vec3fArray([(-50, -50, 0), (50, -50, 0), (-50, 50, 0), (50, 50, 0)])) mesh.GetNormalsAttr().Set(Vt.Vec3fArray([(0, 0, 1), (0, 0, 1), (0, 0, 1), (0, 0, 1)])) mesh.SetNormalsInterpolation("faceVarying") # https://github.com/PixarAnimationStudios/USD/commit/592b4d39edf5daf0534d467e970c95462a65d44b # UsdGeom.Imageable.CreatePrimvar deprecated in v19.03 and removed in v22.08 primvar = UsdGeom.PrimvarsAPI(prim).CreatePrimvar("st", Sdf.ValueTypeNames.TexCoord2fArray, UsdGeom.Tokens.faceVarying) primvar.Set(Vt.Vec2fArray([(0, 0), (1, 0), (1, 1), (0, 1)])) # Create a ground plane collider # NOTE: replace with USD plane prim after the next USD update try: from pxr import UsdPhysics colPlanePath = "/Environment/groundCollider" planeGeom = UsdGeom.Plane.Define(stage, colPlanePath) planeGeom.CreatePurposeAttr().Set("guide") planeGeom.CreateAxisAttr().Set(up_axis) colPlanePrim = stage.GetPrimAtPath(colPlanePath) UsdPhysics.CollisionAPI.Apply(colPlanePrim) except ImportError: carb.log_warn("Failed to create a ground plane collider. Please load the omni.physx.bundle extension and create a new stage from this template if you need it.") # bind "Grid" to "Ground" omni.kit.commands.execute("BindMaterialCommand", prim_path=ground_path, material_path=mtl_path, strength=None, context_name=usd_context_name) # update extent attr = prim.GetAttribute(UsdGeom.Tokens.extent) if attr: bounds = UsdGeom.Boundable.ComputeExtentFromPlugins(UsdGeom.Boundable(prim), Usd.TimeCode.Default()) if bounds: attr.Set(bounds)
11,151
Python
57.694737
185
0.59896
omniverse-code/kit/exts/omni.kit.stage_templates/omni/kit/stage_templates/templates/sunlight.py
import carb import omni.ext import omni.kit.commands from pxr import UsdLux from ..new_stage import * class SunlightStage: def __init__(self): register_template("sunlight", self.new_stage) def __del__(self): unregister_template("sunlight") def new_stage(self, rootname, usd_context_name): # Create basic DistantLight usd_context = omni.usd.get_context(usd_context_name) stage = usd_context.get_stage() with Usd.EditContext(stage, stage.GetRootLayer()): # create Environment omni.kit.commands.execute( "CreatePrim", prim_path="/Environment", prim_type="Xform", select_new_prim=False, create_default_xform=True, context_name=usd_context_name ) omni.kit.commands.execute( "CreatePrim", prim_path="/Environment/defaultLight", prim_type="DistantLight", select_new_prim=False, # https://github.com/PixarAnimationStudios/USD/commit/b5d3809c943950cd3ff6be0467858a3297df0bb7 attributes={UsdLux.Tokens.inputsAngle: 1.0, UsdLux.Tokens.inputsIntensity: 3000} if hasattr(UsdLux.Tokens, 'inputsIntensity') else \ {UsdLux.Tokens.angle: 1.0, UsdLux.Tokens.intensity: 3000}, create_default_xform=True, context_name=usd_context_name )
1,486
Python
35.268292
148
0.588156
omniverse-code/kit/exts/omni.kit.stage_templates/omni/kit/stage_templates/templates/__init__.py
from .sunlight import SunlightStage from .default_stage import DefaultStage new_stage_template_list = None def load_templates(): global new_stage_template_list new_stage_template_list = [SunlightStage(), DefaultStage()] def unload_templates(): global new_stage_template_list new_stage_template_list = None
327
Python
20.866665
63
0.746177
omniverse-code/kit/exts/omni.kit.stage_templates/omni/kit/stage_templates/tests/test_new_stage_menu.py
import omni.kit.test import asyncio import carb import omni.usd import omni.ui as ui from omni.kit import ui_test from omni.kit.menu.utils import MenuItemDescription, MenuLayout class TestMenuFile(omni.kit.test.AsyncTestCase): async def setUp(self): # wait for material to be preloaded so create menu is complete & menus don't rebuild during tests await omni.kit.material.library.get_mdl_list_async() await ui_test.human_delay() self._future_test = None self._required_stage_event = -1 self._stage_event_sub = omni.usd.get_context().get_stage_event_stream().create_subscription_to_pop(self._on_stage_event, name="omni.usd.menu.file") async def tearDown(self): pass def _on_stage_event(self, event): if self._future_test and int(self._required_stage_event) == int(event.type): self._future_test.set_result(event.type) async def reset_stage_event(self, stage_event): self._required_stage_event = stage_event self._future_test = asyncio.Future() async def wait_for_stage_event(self): async def wait_for_event(): await self._future_test try: await asyncio.wait_for(wait_for_event(), timeout=30.0) except asyncio.TimeoutError: carb.log_error(f"wait_for_stage_event timeout waiting for {self._required_stage_event}") self._future_test = None self._required_stage_event = -1 async def test_file_new_from_stage_template_empty(self): stage = omni.usd.get_context().get_stage() layer_name = stage.GetRootLayer().identifier if stage else "None" await self.reset_stage_event(omni.usd.StageEventType.OPENED) menu_widget = ui_test.get_menubar() await menu_widget.find_menu("File").click() await menu_widget.find_menu("New From Stage Template").click() # select empty and wait for stage open await menu_widget.find_menu("Empty").click() await self.wait_for_stage_event() # verify Empty stage stage = omni.usd.get_context().get_stage() self.assertFalse(stage.GetRootLayer().identifier == layer_name) prim_list = [prim.GetPath().pathString for prim in stage.TraverseAll()] self.assertTrue(prim_list == ['/World']) async def test_file_new_from_stage_template_sunlight(self): stage = omni.usd.get_context().get_stage() layer_name = stage.GetRootLayer().identifier if stage else "None" await self.reset_stage_event(omni.usd.StageEventType.OPENED) menu_widget = ui_test.get_menubar() await menu_widget.find_menu("File").click() await menu_widget.find_menu("New From Stage Template").click() # select Sunlight and wait for stage open await menu_widget.find_menu("Sunlight").click() await self.wait_for_stage_event() # verify Sunlight stage stage = omni.usd.get_context().get_stage() self.assertFalse(stage.GetRootLayer().identifier == layer_name) prim_list = [prim.GetPath().pathString for prim in stage.TraverseAll()] self.assertTrue(prim_list == ['/World', '/Environment', '/Environment/defaultLight'])
3,227
Python
38.851851
155
0.657577
omniverse-code/kit/exts/omni.kit.stage_templates/omni/kit/stage_templates/tests/__init__.py
from .test_commands import * from .test_new_stage import * from .test_new_stage_menu import * from .test_templates import * from .test_preferences import *
156
Python
25.166662
34
0.75641
omniverse-code/kit/exts/omni.kit.stage_templates/omni/kit/stage_templates/tests/test_new_stage.py
import unittest import carb.settings import omni.kit.app import omni.kit.test import omni.kit.stage_templates from pxr import UsdGeom class TestNewStage(omni.kit.test.AsyncTestCase): async def setUp(self): self._staged_called = 0 self._stage_name = "$$_test_stage_$$" async def tearDown(self): pass def new_stage_test(self, rootname): self._staged_called += 1 async def test_dirty_status_after_new_stage(self): await omni.kit.stage_templates.new_stage_async(template="sunflowers") self.assertFalse(omni.usd.get_context().has_pending_edit()) # Puts some delay to make sure no pending events to handle. await omni.kit.app.get_app().next_update_async() await omni.kit.app.get_app().next_update_async() self.assertFalse(omni.usd.get_context().has_pending_edit()) async def test_command_new_stage(self): # check template already exists item = omni.kit.stage_templates.get_stage_template(self._stage_name) self.assertTrue(item == None) # add new template omni.kit.stage_templates.register_template(self._stage_name, self.new_stage_test, 0) # check new template exists item = omni.kit.stage_templates.get_stage_template(self._stage_name) self.assertTrue(item != None) self.assertTrue(item[0] == self._stage_name) self.assertTrue(item[1] == self.new_stage_test) # run template & check was called once await omni.kit.stage_templates.new_stage_async(self._stage_name) self.assertTrue(self._staged_called == 1) stage = omni.usd.get_context().get_stage() settings = carb.settings.get_settings() default_prim_name = settings.get("/persistent/app/stage/defaultPrimName") rootname = f"/{default_prim_name}" # create cube cube_path = omni.usd.get_stage_next_free_path(stage, "{}/Cube".format(rootname), False) omni.kit.commands.execute( "CreatePrim", prim_path=cube_path, prim_type="Cube", select_new_prim=False, attributes={UsdGeom.Tokens.size: 100, UsdGeom.Tokens.extent: [(-50, -50, -50), (50, 50, 50)]}, ) prim = stage.GetPrimAtPath(cube_path) self.assertTrue(prim, "Cube Prim exists") # create sphere sphere_path = omni.usd.get_stage_next_free_path(stage, "{}/Sphere".format(rootname), False) omni.kit.commands.execute("CreatePrim", prim_path=sphere_path, prim_type="Sphere", select_new_prim=False) prim = stage.GetPrimAtPath(sphere_path) self.assertTrue(prim, "Sphere Prim exists") # delete template omni.kit.stage_templates.unregister_template(self._stage_name) item = omni.kit.stage_templates.get_stage_template(self._stage_name) self.assertTrue(item == None)
2,881
Python
38.479452
113
0.646303
omniverse-code/kit/exts/omni.kit.stage_templates/omni/kit/stage_templates/tests/test_preferences.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 carb import omni.usd import omni.kit.app from omni.kit.test.async_unittest import AsyncTestCase from omni.kit import ui_test # duplicate of kit\source\extensions\omni.kit.window.preferences\python\omni\kit\window\preferences\tests\test_pages.py # added here for coverage class PreferencesTestPages(AsyncTestCase): # Before running each test async def setUp(self): omni.kit.window.preferences.show_preferences_window() async def test_show_pages(self): pages = omni.kit.window.preferences.get_page_list() page_names = [page._title for page in pages] # is list alpha sorted. Don't compare with fixed list as members can change self.assertEqual(page_names, sorted(page_names)) for page in pages: omni.kit.window.preferences.select_page(page) await ui_test.human_delay(50)
1,319
Python
40.249999
119
0.745262
omniverse-code/kit/exts/omni.kit.stage_templates/omni/kit/stage_templates/tests/test_templates.py
import asyncio import unittest import carb.settings import omni.kit.app import omni.kit.test import omni.kit.stage_templates # also see kit\source\extensions\omni.kit.menu.file\python\omni\kit\menu\file\tests\test_func_templates.py as simular test # added here for coverage class TestNewStageTemplates(omni.kit.test.AsyncTestCase): async def setUp(self): pass async def tearDown(self): pass async def test_stage_template_empty(self): await omni.kit.stage_templates.new_stage_async(template="empty") # verify Empty stage stage = omni.usd.get_context().get_stage() prim_list = [prim.GetPath().pathString for prim in stage.TraverseAll()] self.assertTrue(prim_list == ['/World']) async def test_stage_template_sunlight(self): await omni.kit.stage_templates.new_stage_async(template="sunlight") # verify Sunlight stage stage = omni.usd.get_context().get_stage() prim_list = [prim.GetPath().pathString for prim in stage.TraverseAll()] self.assertTrue(prim_list == ['/World', '/Environment', '/Environment/defaultLight']) async def test_stage_template_default_stage(self): await omni.kit.stage_templates.new_stage_async(template="default stage") # verify Default stage stage = omni.usd.get_context().get_stage() prim_list = [prim.GetPath().pathString for prim in stage.TraverseAll()] self.assertTrue(set(prim_list) == set([ '/World', '/Environment', '/Environment/Sky', '/Environment/DistantLight', '/Environment/Looks', '/Environment/Looks/Grid', '/Environment/Looks/Grid/Shader', '/Environment/ground', '/Environment/groundCollider']), prim_list) async def test_stage_template_noname(self): await omni.kit.stage_templates.new_stage_async() # verify Empty stage stage = omni.usd.get_context().get_stage() prim_list = [prim.GetPath().pathString for prim in stage.TraverseAll()] self.assertTrue(prim_list == ['/World'])
2,077
Python
36.781818
122
0.666346
omniverse-code/kit/exts/omni.kit.stage_templates/omni/kit/stage_templates/tests/test_commands.py
import unittest import carb.settings import omni.kit.test import omni.kit.stage_templates from pxr import UsdGeom class TestCommands(omni.kit.test.AsyncTestCase): async def setUp(self): self._staged_called = 0 self._stage_name = "$$_test_stage_$$" async def tearDown(self): pass def new_stage_test(self, rootname): self._staged_called += 1 async def test_command_new_stage(self): # check template already exists item = omni.kit.stage_templates.get_stage_template(self._stage_name) self.assertTrue(item == None) # add new template omni.kit.stage_templates.register_template(self._stage_name, self.new_stage_test, 0) # check new template exists item = omni.kit.stage_templates.get_stage_template(self._stage_name) self.assertTrue(item != None) self.assertTrue(item[0] == self._stage_name) self.assertTrue(item[1] == self.new_stage_test) # run template & check was called once await omni.kit.stage_templates.new_stage_async(self._stage_name) self.assertTrue(self._staged_called == 1) stage = omni.usd.get_context().get_stage() settings = carb.settings.get_settings() default_prim_name = settings.get("/persistent/app/stage/defaultPrimName") rootname = f"/{default_prim_name}" # create cube cube_path = omni.usd.get_stage_next_free_path(stage, "{}/Cube".format(rootname), False) omni.kit.commands.execute( "CreatePrim", prim_path=cube_path, prim_type="Cube", select_new_prim=False, attributes={UsdGeom.Tokens.size: 100, UsdGeom.Tokens.extent: [(-50, -50, -50), (50, 50, 50)]}, ) prim = stage.GetPrimAtPath(cube_path) self.assertTrue(prim, "Cube Prim exists") # create sphere sphere_path = omni.usd.get_stage_next_free_path(stage, "{}/Sphere".format(rootname), False) omni.kit.commands.execute("CreatePrim", prim_path=sphere_path, prim_type="Sphere", select_new_prim=False) prim = stage.GetPrimAtPath(sphere_path) self.assertTrue(prim, "Sphere Prim exists") # delete template omni.kit.stage_templates.unregister_template(self._stage_name) item = omni.kit.stage_templates.get_stage_template(self._stage_name) self.assertTrue(item == None)
2,409
Python
36.656249
113
0.638854
omniverse-code/kit/exts/omni.kit.widget.inspector/config/extension.toml
[package] title = "UI Inspector (Preview)" description = "Inspect your UI Elements" version = "1.0.3" category = "Developer" authors = ["NVIDIA"] repository = "" keywords = ["stage", "outliner", "scene"] changelog = "docs/CHANGELOG.md" readme = "docs/README.md" preview_image = "data/inspector_full.png" icon = "data/clouseau.png" [package.writeTarget] kit = true [dependencies] "omni.ui" = {} "omni.kit.pip_archive" = {} [[native.library]] path = "bin/${lib_prefix}omni.kit.widget.inspector${lib_ext}" [[python.module]] name = "omni.kit.widget.inspector" [[test]] args = [] stdoutFailPatterns.exclude = [ ]
616
TOML
18.281249
61
0.680195
omniverse-code/kit/exts/omni.kit.widget.inspector/omni/kit/widget/inspector/__init__.py
## Copyright (c) 2018-2019, 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. ## """ InspectorWidget ------ InspectorWidget provides a way to display Widget Internals :class:`.InspectorWidget` """ # Import the ICef bindings right away from ._inspector_widget import * from .scripts.extension import *
664
Python
30.666665
77
0.778614
omniverse-code/kit/exts/omni.kit.widget.inspector/omni/kit/widget/inspector/scripts/extension.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 carb import omni.ext import omni.ui import omni.kit.app import omni.kit.widget.inspector class InspectorWidgetExtension(omni.ext.IExt): def __init__(self): super().__init__() pass def on_startup(self, ext_id): pass # extension_path = omni.kit.app.get_app().get_extension_manager().get_extension_path(ext_id) # omni.kit.widget.inspector.startup(extension_path) def on_shutdown(self): pass # omni.kit.widget.inspector.shutdown()
939
Python
31.413792
100
0.724175
omniverse-code/kit/exts/omni.kit.widget.inspector/omni/kit/widget/inspector/tests/test_widget.py
import omni.kit.test from omni.kit.widget.inspector import PreviewMode, InspectorWidget import functools class TestInspectorWidget(omni.kit.test.AsyncTestCase): async def setUp(self): self._widget = InspectorWidget() def test_widget_init(self): self.assertTrue(self._widget) def test_widget_get_set_widget(self): widget = self._widget.widget self.assertEqual(widget, None) # Check default label = omni.ui.Label("Blah") self._widget.widget = label self.assertEqual(self._widget.widget, label) def test_widget_get_set_preview(self): preview_mode = self._widget.preview_mode self.assertEqual(preview_mode, PreviewMode.WIREFRAME_AND_COLOR) # Check default self._widget.preview_mode = PreviewMode.WIRE_ONLY self.assertEqual(self._widget.preview_mode, PreviewMode.WIRE_ONLY) def test_widget_get_set_selected_widget(self): self.assertEqual(self._widget.selected_widget, None) # Check default label = omni.ui.Label("Blah") self._widget.selected_widget = label self.assertEqual(self._widget.selected_widget, label) def test_widget_get_set_start_depth(self): start_depth = self._widget.start_depth self.assertEqual(start_depth, 0) # Check default self._widget.start_depth = 25 self.assertEqual(self._widget.start_depth, 25) def test_widget_get_set_end_depth(self): end_depth = self._widget.end_depth self.assertEqual(end_depth, 65535) # Check default self._widget.end_depth = 25 self.assertEqual(self._widget.end_depth, 25) def test_widget_set_widget_changed_fn(self): def selection_changed(the_list, widget): the_list.append(widget) the_list = [] self._widget.set_selected_widget_changed_fn(functools.partial(selection_changed, the_list)) label = omni.ui.Label("Blah") self.assertTrue(len(the_list) == 0) self._widget.selected_widget = label self.assertTrue(len(the_list) == 1) self.assertEqual(the_list[0], label) def test_widget_set_preview_mode_changed_fn(self): def preview_mode_value_changed(the_list, value): the_list.append(value) the_list = [] self._widget.set_preview_mode_change_fn(functools.partial(preview_mode_value_changed, the_list)) self.assertTrue(len(the_list) == 0) self._widget.preview_mode = PreviewMode.WIRE_ONLY self.assertTrue(len(the_list) == 1) self.assertTrue(the_list[0] == PreviewMode.WIRE_ONLY)
2,603
Python
34.671232
104
0.659624