Skip to content

composition

composition

Layers, references, payloads, inherits and variants, and exporting the result.

Functions:

add_inherit(stage, prim_path, class_path)

Make a prim inherit from a class prim.

Parameters:

Name Type Description Default
stage str

stage handle.

required
prim_path str

path of the prim.

required
class_path str

path of the class prim, like /_class_asset.

required

Raises:

Type Description
ValueError

there is no prim at the path.

Returns:

Name Type Description
str str

handle of the stage.

Source code in src/nyx_usd/ops/composition.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def add_inherit(stage: str, prim_path: str, class_path: str) -> str:
    """Make a prim inherit from a class prim.

    Args:
        stage (str): stage handle.
        prim_path (str): path of the prim.
        class_path (str): path of the class prim, like `/_class_asset`.

    Raises:
        ValueError: there is no prim at the path.

    Returns:
        str: handle of the stage.
    """
    get_prim(stage, prim_path).GetInherits().AddInherit(Sdf.Path(class_path))
    return stage

add_payload(stage, prim_path, asset_path, target_prim='')

Add a payload of a USD file to a prim, defining the prim if needed.

A payload is a reference that can be left unloaded.

Parameters:

Name Type Description Default
stage str

stage handle.

required
prim_path str

path of the prim.

required
asset_path str

file to load.

required
target_prim str

prim of the file to load, its default prim if empty.

''

Returns:

Name Type Description
str str

handle of the stage.

Source code in src/nyx_usd/ops/composition.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def add_payload(
    stage: str, prim_path: str, asset_path: str, target_prim: str = ""
) -> str:
    """Add a payload of a USD file to a prim, defining the prim if needed.

    A payload is a reference that can be left unloaded.

    Args:
        stage (str): stage handle.
        prim_path (str): path of the prim.
        asset_path (str): file to load.
        target_prim (str): prim of the file to load, its default prim if empty.

    Returns:
        str: handle of the stage.
    """
    prim = get_stage(stage).DefinePrim(Sdf.Path(prim_path))
    if target_prim:
        prim.GetPayloads().AddPayload(asset_path, Sdf.Path(target_prim))
    else:
        prim.GetPayloads().AddPayload(asset_path)
    return stage

add_reference(stage, prim_path, asset_path, target_prim='')

Reference a USD file on a prim, defining the prim if needed.

Parameters:

Name Type Description Default
stage str

stage handle.

required
prim_path str

path of the prim.

required
asset_path str

file to reference.

required
target_prim str

prim of the file to reference, like /Thing. Its default prim if empty.

''

Returns:

Name Type Description
str str

handle of the stage.

Source code in src/nyx_usd/ops/composition.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def add_reference(
    stage: str, prim_path: str, asset_path: str, target_prim: str = ""
) -> str:
    """Reference a USD file on a prim, defining the prim if needed.

    Args:
        stage (str): stage handle.
        prim_path (str): path of the prim.
        asset_path (str): file to reference.
        target_prim (str): prim of the file to reference, like `/Thing`. Its default
            prim if empty.

    Returns:
        str: handle of the stage.
    """
    prim = get_stage(stage).DefinePrim(Sdf.Path(prim_path))
    if target_prim:
        prim.GetReferences().AddReference(asset_path, Sdf.Path(target_prim))
    else:
        prim.GetReferences().AddReference(asset_path)
    return stage

add_sublayer(stage, layer_path)

Add a layer file to the sublayers of the root layer, as the weakest one.

Parameters:

Name Type Description Default
stage str

stage handle.

required
layer_path str

layer file to add.

required

Returns:

Name Type Description
str str

handle of the stage.

Source code in src/nyx_usd/ops/composition.py
75
76
77
78
79
80
81
82
83
84
85
86
def add_sublayer(stage: str, layer_path: str) -> str:
    """Add a layer file to the sublayers of the root layer, as the weakest one.

    Args:
        stage (str): stage handle.
        layer_path (str): layer file to add.

    Returns:
        str: handle of the stage.
    """
    get_stage(stage).GetRootLayer().subLayerPaths.append(layer_path)
    return stage

add_variant_set(stage, prim_path, set_name, variants)

Add a variant set with variants to a prim. Existing variants are kept.

Parameters:

Name Type Description Default
stage str

stage handle.

required
prim_path str

path of the prim.

required
set_name str

name of the variant set.

required
variants list[str]

names of the variants.

required

Raises:

Type Description
ValueError

there is no prim at the path.

Returns:

Name Type Description
str str

handle of the stage.

Source code in src/nyx_usd/ops/composition.py
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
def add_variant_set(
    stage: str, prim_path: str, set_name: str, variants: list[str]
) -> str:
    """Add a variant set with variants to a prim. Existing variants are kept.

    Args:
        stage (str): stage handle.
        prim_path (str): path of the prim.
        set_name (str): name of the variant set.
        variants (list[str]): names of the variants.

    Raises:
        ValueError: there is no prim at the path.

    Returns:
        str: handle of the stage.
    """
    variant_set = get_prim(stage, prim_path).GetVariantSets().AddVariantSet(set_name)
    for variant in variants:
        variant_set.AddVariant(variant)
    return stage

export_to_string(stage, flatten=False)

Get the USD text of a stage.

Parameters:

Name Type Description Default
stage str

stage handle.

required
flatten bool

compose everything into one layer first, instead of giving the root layer as it is.

False

Returns:

Name Type Description
str str

USDA text.

Source code in src/nyx_usd/ops/composition.py
198
199
200
201
202
203
204
205
206
207
208
209
210
211
def export_to_string(stage: str, flatten: bool = False) -> str:
    """Get the USD text of a stage.

    Args:
        stage (str): stage handle.
        flatten (bool): compose everything into one layer first, instead of giving the
            root layer as it is.

    Returns:
        str: USDA text.
    """
    usd_stage = get_stage(stage)
    layer = usd_stage.Flatten() if flatten else usd_stage.GetRootLayer()
    return layer.ExportToString()

flatten_stage(stage, path)

Compose a stage into one layer and write it to a file.

Parameters:

Name Type Description Default
stage str

stage handle.

required
path str

file to write.

required

Raises:

Type Description
ValueError

the file can't be written.

Returns:

Name Type Description
str str

the path of the file.

Source code in src/nyx_usd/ops/composition.py
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
def flatten_stage(stage: str, path: str) -> str:
    """Compose a stage into one layer and write it to a file.

    Args:
        stage (str): stage handle.
        path (str): file to write.

    Raises:
        ValueError: the file can't be written.

    Returns:
        str: the path of the file.
    """
    if not get_stage(stage).Flatten().Export(path):
        raise ValueError(f"Can't write {path!r}")
    return path

get_variants(stage, prim_path)

Describe the variant sets of a prim.

Parameters:

Name Type Description Default
stage str

stage handle.

required
prim_path str

path of the prim.

required

Raises:

Type Description
ValueError

there is no prim at the path.

Returns:

Type Description
dict[str, dict]

dict[str, dict]: by set name, its variants and the selection (empty if none).

Source code in src/nyx_usd/ops/composition.py
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
def get_variants(stage: str, prim_path: str) -> dict[str, dict]:
    """Describe the variant sets of a prim.

    Args:
        stage (str): stage handle.
        prim_path (str): path of the prim.

    Raises:
        ValueError: there is no prim at the path.

    Returns:
        dict[str, dict]: by set name, its `variants` and the `selection` (empty if
            none).
    """
    variant_sets = get_prim(stage, prim_path).GetVariantSets()
    return {
        name: {
            "variants": list(variant_sets.GetVariantSet(name).GetVariantNames()),
            "selection": variant_sets.GetVariantSet(name).GetVariantSelection(),
        }
        for name in variant_sets.GetNames()
    }

list_sublayers(stage)

List the sublayers of the root layer, the strongest first.

Parameters:

Name Type Description Default
stage str

stage handle.

required

Returns:

Type Description
list[str]

list[str]: layer paths.

Source code in src/nyx_usd/ops/composition.py
109
110
111
112
113
114
115
116
117
118
def list_sublayers(stage: str) -> list[str]:
    """List the sublayers of the root layer, the strongest first.

    Args:
        stage (str): stage handle.

    Returns:
        list[str]: layer paths.
    """
    return list(get_stage(stage).GetRootLayer().subLayerPaths)

remove_sublayer(stage, layer_path)

Remove a layer from the sublayers of the root layer.

Parameters:

Name Type Description Default
stage str

stage handle.

required
layer_path str

layer file to remove, as it was added.

required

Raises:

Type Description
ValueError

the layer is not a sublayer.

Returns:

Name Type Description
str str

handle of the stage.

Source code in src/nyx_usd/ops/composition.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
def remove_sublayer(stage: str, layer_path: str) -> str:
    """Remove a layer from the sublayers of the root layer.

    Args:
        stage (str): stage handle.
        layer_path (str): layer file to remove, as it was added.

    Raises:
        ValueError: the layer is not a sublayer.

    Returns:
        str: handle of the stage.
    """
    paths = get_stage(stage).GetRootLayer().subLayerPaths
    if layer_path not in paths:
        raise ValueError(f"{layer_path!r} is not a sublayer of {stage!r}")
    paths.remove(layer_path)
    return stage

set_variant_selection(stage, prim_path, set_name, variant)

Select a variant of a variant set.

Parameters:

Name Type Description Default
stage str

stage handle.

required
prim_path str

path of the prim.

required
set_name str

name of the variant set.

required
variant str

variant to select, empty to clear the selection.

required

Raises:

Type Description
ValueError

no prim at the path, or no such variant set or variant.

Returns:

Name Type Description
str str

handle of the stage.

Source code in src/nyx_usd/ops/composition.py
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
def set_variant_selection(
    stage: str, prim_path: str, set_name: str, variant: str
) -> str:
    """Select a variant of a variant set.

    Args:
        stage (str): stage handle.
        prim_path (str): path of the prim.
        set_name (str): name of the variant set.
        variant (str): variant to select, empty to clear the selection.

    Raises:
        ValueError: no prim at the path, or no such variant set or variant.

    Returns:
        str: handle of the stage.
    """
    prim = get_prim(stage, prim_path)
    if not prim.HasVariantSets() or set_name not in prim.GetVariantSets().GetNames():
        raise ValueError(f"{prim_path} has no variant set {set_name!r}")
    variant_set = prim.GetVariantSet(set_name)
    if variant and variant not in variant_set.GetVariantNames():
        raise ValueError(f"Variant set {set_name!r} has no variant {variant!r}")
    if variant:
        variant_set.SetVariantSelection(variant)
    else:
        variant_set.ClearVariantSelection()
    return stage