.. _run_omnigraph_python_script:

Running Minimal Kit With OmniGraph
==================================

The Kit application at its core is a basic framework from plugging in extensions with a common communication method.
We can take advantage of this to run a script that works with OmniGraph without pulling in the entire Kit overhead.

Running With Python Support
---------------------------
The most user-friendly approach to running a minimal version of Kit with OmniGraph is to make use of the `omni.graph`
extension, which adds Python bindings and scripts to the OmniGraph core.

Your extension must have a dependency on `omni.graph` using these lines in your `extension.toml` file:

.. code-block:: toml

    [dependencies]
    "omni.graph" = {}

Let's say your extension, `omni.my.extension`, has a single node type `MyNodeType` that when executed will take a
directory path name as input and will print out the number of files and total file size of every file in that
directory. This is a script that will set up and execute an OmniGraph that creates a node that will display that
information for the Omniverse Cache directory.

.. code-block:: Python

    import carb
    import omni.graph.core as og
    import omni.usd

    # This is needed to initialize the OmniGraph backing
    omni.usd.get_context().new_stage()

    # Get the cache directory to be examined
    cache_directory = carb.tokens.get_tokens_interface().resolve("${omni_cache}")

    # Create a graph with the node that will print out the cache directory contents
    _ = og.Controller.edit("/CacheGraph", {
        og.Controller.Keys.CREATE_NODES: ("MyNode", "omni.my.extension.MyNodeType"),
        og.Controller.Keys.SET_VALUES: ("MyNode.inputs:directory", cache_directory)
    })

    # Evaluate the node to complete the operation
    og.Controller.evaluate_sync()

If this script is saved in the file `showCache.py` then you run this from your Kit executable directory:

.. code-block:: sh

    $ ./kit.exe --enable omni.my.extension --exec showCache.py
    C:/Kit/cache contained 123 files with a total size of 456,789 bytes

.. note::

    Running with only `omni.graph` enabled will work, but it is just a framework and has no nodes of its own to
    execute. That is why you must enable your own extension. You might also want to enable other extensions such as
    `omni.graph.nodes` or `omni.graph.action` if you want to access the standard set of OmniGraph nodes.


Running With Just The Core
--------------------------
If you have an extension that uses the C++ ABI to create and manipulate an OmniGraph you can run Kit with only
your extension enabled, executing a script that will trigger the code you wish to execute.

Your extension must have a dependency on `omni.graph.core` using these lines in your `extension.toml` file:

.. code-block:: toml

    [dependencies]
    "omni.graph.core" = {}

You can then run your own script `setUpOmniGraphAndEvaluate.py` that executes your C++ code to create and evaluate the
graph in a way similar to the above, but using the C++ ABI, with the same command line:

.. code-block:: sh

    $ ./kit.exe --enable omni.my.extension --exec setUpOmniGraphAndEvaluate.py
