Examples Using Custom Decoration
This file contains example usage of AutoNode decoration that use the extra decorator parameters. For access to the other types of examples see AutoNode Examples.
The @og.create_node_type decorator takes a number of optional arguments that helps provide the extra information to the node type that is normally part of the .ogn definition.
def create_node_type(
func: callable = None,
*,
unique_name: str = None,
ui_name: str = None,
add_execution_pins: bool = False,
metadata: dict[str, str] = None,
) -> callable:
"""Decorator to transform a Python function into an OmniGraph node type definition.
The decorator is configured to allow use with and without parameters. When used without parameters all of the
default values for the parameters are assumed.
If the function is called from the __main__ context, as it would if it were executed from the script editor or
from a file, then the decorator is assumed to be creating a short-lived node type definition and the default
module name "__autonode__" is applied to indicate this. Any attempts to save a scene containing these short-term
node types will be flagged as a warning.
Examples:
>>> import omni.graph.core as og
>>> @og.create_node_type
>>> def double_float(a: ogdt.Float) -> ogdt.Float:
>>> return a * 2.0
>>>
>>> @og.create_node_type(add_execution_pins=True)
>>> def double_float(a: ogdt.Float) -> ogdt.Float:
>>> return a * 2.0
Args:
func: the function object being wrapped. Should be a pure python function object or any other callable which
has an `__annotations__` property. If "None" then the decorator was called using the parameterized form
"@create_node_type(...)" instead of "@create_node_type" and the function will be inferred in other ways.
unique_name: Override the default unique name, which is the function name in the module namespace
ui_name: Name that appears in the node type's menu and node display.
add_execution_pins: Include both input and output execution pins so that this can be used as a trigger node
type in an action graph.
metadata: Dictionary of extra metadata to apply to the node type
Returns:
Decorated version of the function that will create the node type definition
"""
These examples show how the definition of the node type is affected by each of them.
@og.create_node_type(ui_name=str)
import omni.graph.core as og
import omni.graph.core.types as ot
@og.create_node_type(ui_name="Fluffy Bunny")
def autonode_decoration_ui_name() -> ot.string:
"""This node type has no inputs and returns the UI name of its node type as output. It demonstrates how the
optional ui_name argument can be used on the decorator to modify the name of the node type as it
will appear to the user.
"""
# We know the name of the node type by construction
node_type = og.get_node_type("omni.graph.autonode_decoration_ui_name")
# Get the metadata containing the UI name - will always return "Fluffy Bunny"
return node_type.get_metadata(og.MetadataKeys.UI_NAME)
@og.create_node_type(unique_name=str)
import omni.graph.core as og
import omni.graph.core.types as ot
@og.create_node_type(unique_name="omni.graph.autonode_unique_name")
def autonode_decoration_unique_name() -> ot.string:
"""This node type has no inputs and returns the unique name of its node type as output. It demonstrates how the
optional unique_name argument can be used on the decorator to modify the name of the node type as it
is used for registration and identification.
"""
# Look up the node type name using the supplied unique name rather than the one that would have been
# automatically generated (omni.graph.autonode_decoration_unique_name)
node_type = og.get_node_type("omni.graph.autonode_unique_name")
return node_type.get_node_type() if node_type.is_valid() else ""
@og.create_node_type(add_execution_pins)
import inspect
import omni.graph.core as og
import omni.graph.core.types as ot
@og.create_node_type(add_execution_pins=True)
def autonode_decoration_add_execution_pins() -> ot.int:
"""This node type has no inputs and returns the number of attributes it has of type "execution". It
demonstrates how the optional add_execution_pins argument can be used on the decorator to automatically
include both an input and an output execution pin so that the node type can be easily included in the
Action Graph.
"""
frame = inspect.currentframe().f_back
node = frame.f_locals.get("node")
# This will return 2, counting the automatically added input and output execution attributes
return sum(1 for attr in node.get_attributes() if attr.get_resolved_type().role == og.AttributeRole.EXECUTION)
@og.create_node_type(metadata=dict(str,any))
import omni.graph.core as og
import omni.graph.core.types as ot
@og.create_node_type(metadata={"Emperor": "Palpatine"})
def autonode_decoration_metadata() -> ot.string:
"""This node type has no inputs and returns a string consisting of the value of the metadata
whose name was specified in the decorator "metadata" argument. It demonstrates how the optional metadata
argument can be used on the decorator to automatically add metadata to the node type definition.
"""
# We know the name of the node type by construction
node_type = og.get_node_type("omni.graph.autonode_decoration_metadata")
# Return the metadata with the custom name we specified - will always return "Palpatine"
return node_type.get_metadata("Emperor")