Adding an operation
A new operation is a function, a node spec and tests. Say it is set_foo in the area
prims.
1. The function
In the module of its area (src/nyx_usd/ops/prims.py):
def set_foo(stage: str, prim_path: str, foo: float = 1.0) -> str:
"""Set the foo of a prim.
Args:
stage (str): stage handle.
prim_path (str): path of the prim.
foo (float): the foo.
Raises:
ValueError: there is no prim at the path.
Returns:
str: handle of the stage.
"""
prim = get_prim(stage, prim_path)
...
return stage
Rules:
stagecomes first, a handle. Useget_stageandget_primfrom_common, they raise a clearValueError.- JSON in, JSON out. Arguments are numbers, strings, booleans, lists and
dictionaries. Return the same, with
to_jsonfor USD values. Convert lists withto_usdwhen an attribute needs a vector or an array. - A function that edits returns the handle (
Returns: str: handle of the stage.), so nodes chain. A function that reads returns the value. - Fail with
ValueErrorand a message that names the path or value. Catch the exceptions ofpxrthat you can explain, aTferror says little. Check a resultFalsefromSet...andApply: USD often reports failure that way. - Only
pxrand the standard library, and only long-lived USD APIs (Usd,Sdf,UsdGeom,UsdShade): the same function has to work with the older USD of Maya and Houdini. - No newer syntax than Python 3.11. Use
from __future__ import annotations. - Docstrings in Google style. They are the API reference.
Then import it in src/nyx_usd/ops/__init__.py, in the group of its module and in
__all__. The nodes call nyx_usd.ops.set_foo, so a function that is not there is not
found.
2. The node
In the module of the area in plugins/usd_plugin/nodes/specs/, add an OperationSpec to
SPECS:
OperationSpec(
"set_foo", # the function name, also the node type id usd:set_foo
"Set Foo", # shown in the palette
"Sets the foo of a prim.", # the description
(
STAGE_PARAM,
PRIM_PARAM,
Param("foo", FLOAT, 1.0, "The foo."),
),
STRING, # datatype of the response
("prim",), # tags
),
- The params match the function: the same names, in the same order. A node passes them as keyword arguments, a mismatch fails at run time.
- Use
STAGE_PARAM,PRIM_PARAM,ATTR_PARAMand the datatypes ofspecs/common.pywhere they fit. The datatype of a stage handle isSTRING. - Give a
defaultfor every input the user would not want to be empty. Lists as defaults are copied for each node. - Tags are what the operation is about (
prim,attribute,composition,transform,geometry,shading, ...). Reuse the existing ones, they are searchable astags:prim. - A new area is a new module in
specs/that definesSPECS, and a line innodes/operations.py.
3. Tests
In tests/test_ops/test_<area>.py, against real USD. Cover the normal result and each
ValueError. The stage fixture is an empty stage in memory and stages are closed after
each test. See Testing.
4. Check it
make check
Then check the node side with Nyx, see Testing: the spec matches the signature, the node can be created, and a graph runs it.
5. Document it
- Add a row for the node to the table in
docs/user/nodes/<area>.md, with the inputs, output and description of the spec. The tables are written by hand: keep them in step with the specs. - If the node has a behaviour users trip over, add a line to the notes of that page.
- A user-visible addition goes in
CHANGELOG.rstwhen releasing, see Releasing.