Skip to content

prims

prims

Querying and editing prims.

Functions:

add_prim(stage, prim_path, type_name='Xform')

Define a prim, creating the prims above it as needed, without a type.

Define a parent first to give it a type, like Xform: an untyped prim can't be transformed.

Parameters:

Name Type Description Default
stage str

stage handle.

required
prim_path str

path of the prim, like /World/cube.

required
type_name str

schema type of the prim, like Xform or Mesh.

'Xform'

Returns:

Name Type Description
str str

handle of the stage.

Source code in src/nyx_usd/ops/prims.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def add_prim(stage: str, prim_path: str, type_name: str = "Xform") -> str:
    """Define a prim, creating the prims above it as needed, without a type.

    Define a parent first to give it a type, like `Xform`: an untyped prim can't be
    transformed.

    Args:
        stage (str): stage handle.
        prim_path (str): path of the prim, like `/World/cube`.
        type_name (str): schema type of the prim, like `Xform` or `Mesh`.

    Returns:
        str: handle of the stage.
    """
    get_stage(stage).DefinePrim(Sdf.Path(prim_path), type_name)
    return stage

copy_prim(stage, prim_path, new_path)

Copy a prim with everything below it, in the layer the stage edits.

Parameters:

Name Type Description Default
stage str

stage handle.

required
prim_path str

path of the prim.

required
new_path str

path of the copy. The prims above it are created if missing.

required

Raises:

Type Description
ValueError

no prim at the path, or the layer has no spec to copy.

Returns:

Name Type Description
str str

handle of the stage.

Source code in src/nyx_usd/ops/prims.py
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
def copy_prim(stage: str, prim_path: str, new_path: str) -> str:
    """Copy a prim with everything below it, in the layer the stage edits.

    Args:
        stage (str): stage handle.
        prim_path (str): path of the prim.
        new_path (str): path of the copy. The prims above it are created if missing.

    Raises:
        ValueError: no prim at the path, or the layer has no spec to copy.

    Returns:
        str: handle of the stage.
    """
    get_prim(stage, prim_path)
    source, target = Sdf.Path(prim_path), Sdf.Path(new_path)
    layer = get_stage(stage).GetEditTarget().GetLayer()
    if not layer.GetPrimAtPath(source):
        raise ValueError(f"{prim_path} has no spec in {layer.identifier}")
    Sdf.CreatePrimInLayer(layer, target.GetParentPath())
    if not Sdf.CopySpec(layer, source, layer, target):
        raise ValueError(f"Can't copy {prim_path} to {new_path}")
    return stage

find_prims(stage, name_pattern, type_name='')

Find prims by name.

Parameters:

Name Type Description Default
stage str

stage handle.

required
name_pattern str

regular expression searched in the name of the prims, not their whole path.

required
type_name str

only the prims of this schema type, all if empty.

''

Returns:

Type Description
list[str]

list[str]: paths of the prims that match.

Source code in src/nyx_usd/ops/prims.py
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
def find_prims(stage: str, name_pattern: str, type_name: str = "") -> list[str]:
    """Find prims by name.

    Args:
        stage (str): stage handle.
        name_pattern (str): regular expression searched in the name of the prims, not
            their whole path.
        type_name (str): only the prims of this schema type, all if empty.

    Returns:
        list[str]: paths of the prims that match.
    """
    pattern = re.compile(name_pattern)
    return [
        str(prim.GetPath())
        for prim in get_stage(stage).Traverse()
        if pattern.search(prim.GetName())
        and (not type_name or prim.GetTypeName() == type_name)
    ]

get_children(stage, prim_path)

List the paths of the children 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
list[str]

list[str]: paths of the children.

Source code in src/nyx_usd/ops/prims.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
def get_children(stage: str, prim_path: str) -> list[str]:
    """List the paths of the children 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:
        list[str]: paths of the children.
    """
    return [str(child.GetPath()) for child in get_prim(stage, prim_path).GetChildren()]

get_parent(stage, prim_path)

Get the path of the parent 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:

Name Type Description
str str

path of the parent, / for a prim at the top, empty for the root.

Source code in src/nyx_usd/ops/prims.py
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
def get_parent(stage: str, prim_path: str) -> str:
    """Get the path of the parent 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:
        str: path of the parent, `/` for a prim at the top, empty for the root.
    """
    parent = get_prim(stage, prim_path).GetParent()
    return str(parent.GetPath()) if parent else ""

get_prim_metadata(stage, prim_path, key)

Get a metadata field of a prim.

Parameters:

Name Type Description Default
stage str

stage handle.

required
prim_path str

path of the prim.

required
key str

metadata key.

required

Raises:

Type Description
ValueError

there is no prim at the path.

Returns:

Name Type Description
Any Any

JSON value, None if the field is not set.

Source code in src/nyx_usd/ops/prims.py
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
def get_prim_metadata(stage: str, prim_path: str, key: str) -> typing.Any:
    """Get a metadata field of a prim.

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

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

    Returns:
        Any: JSON value, None if the field is not set.
    """
    return to_json(get_prim(stage, prim_path).GetMetadata(key))

get_prim_type(stage, prim_path)

Get the schema type 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:

Name Type Description
str str

type name like Xform, empty for a prim without a type.

Source code in src/nyx_usd/ops/prims.py
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def get_prim_type(stage: str, prim_path: str) -> str:
    """Get the schema type 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:
        str: type name like `Xform`, empty for a prim without a type.
    """
    return str(get_prim(stage, prim_path).GetTypeName())

has_prim(stage, prim_path)

Check whether a prim exists.

Parameters:

Name Type Description Default
stage str

stage handle.

required
prim_path str

path of the prim.

required

Returns:

Name Type Description
bool bool

there is a prim at the path.

Source code in src/nyx_usd/ops/prims.py
66
67
68
69
70
71
72
73
74
75
76
def has_prim(stage: str, prim_path: str) -> bool:
    """Check whether a prim exists.

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

    Returns:
        bool: there is a prim at the path.
    """
    return bool(get_stage(stage).GetPrimAtPath(prim_path))

list_prims(stage, type_name='')

List the paths of the prims of a stage.

Parameters:

Name Type Description Default
stage str

stage handle.

required
type_name str

only the prims of this schema type, all if empty.

''

Returns:

Type Description
list[str]

list[str]: prim paths.

Source code in src/nyx_usd/ops/prims.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def list_prims(stage: str, type_name: str = "") -> list[str]:
    """List the paths of the prims of a stage.

    Args:
        stage (str): stage handle.
        type_name (str): only the prims of this schema type, all if empty.

    Returns:
        list[str]: prim paths.
    """
    return [
        str(prim.GetPath())
        for prim in get_stage(stage).Traverse()
        if not type_name or prim.GetTypeName() == type_name
    ]

move_prim(stage, prim_path, new_parent_path)

Move a prim under another parent, in the layer the stage edits.

Parameters:

Name Type Description Default
stage str

stage handle.

required
prim_path str

path of the prim.

required
new_parent_path str

path of the new parent, / for the top.

required

Raises:

Type Description
ValueError

no prim at the path, or the layer can't move it.

Returns:

Name Type Description
str str

handle of the stage.

Source code in src/nyx_usd/ops/prims.py
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
def move_prim(stage: str, prim_path: str, new_parent_path: str) -> str:
    """Move a prim under another parent, in the layer the stage edits.

    Args:
        stage (str): stage handle.
        prim_path (str): path of the prim.
        new_parent_path (str): path of the new parent, `/` for the top.

    Raises:
        ValueError: no prim at the path, or the layer can't move it.

    Returns:
        str: handle of the stage.
    """
    prim = get_prim(stage, prim_path)
    _apply_edit(
        stage,
        Sdf.NamespaceEdit.ReparentAndRename(
            Sdf.Path(prim_path),
            Sdf.Path(new_parent_path),
            prim.GetName(),
            Sdf.NamespaceEdit.atEnd,
        ),
    )
    return stage

remove_prim(stage, prim_path)

Remove 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:

Name Type Description
str str

handle of the stage.

Source code in src/nyx_usd/ops/prims.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def remove_prim(stage: str, prim_path: str) -> str:
    """Remove a prim.

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

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

    Returns:
        str: handle of the stage.
    """
    get_prim(stage, prim_path)
    get_stage(stage).RemovePrim(Sdf.Path(prim_path))
    return stage

rename_prim(stage, prim_path, new_name)

Rename a prim, in the layer the stage edits.

Parameters:

Name Type Description Default
stage str

stage handle.

required
prim_path str

path of the prim.

required
new_name str

new name, without the path.

required

Raises:

Type Description
ValueError

no prim at the path, or the layer can't rename it.

Returns:

Name Type Description
str str

handle of the stage.

Source code in src/nyx_usd/ops/prims.py
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
def rename_prim(stage: str, prim_path: str, new_name: str) -> str:
    """Rename a prim, in the layer the stage edits.

    Args:
        stage (str): stage handle.
        prim_path (str): path of the prim.
        new_name (str): new name, without the path.

    Raises:
        ValueError: no prim at the path, or the layer can't rename it.

    Returns:
        str: handle of the stage.
    """
    get_prim(stage, prim_path)
    _apply_edit(stage, Sdf.NamespaceEdit.Rename(Sdf.Path(prim_path), new_name))
    return stage

set_active(stage, prim_path, active=True)

Activate or deactivate a prim. A deactivated prim is left out of traversals.

Parameters:

Name Type Description Default
stage str

stage handle.

required
prim_path str

path of the prim.

required
active bool

whether the prim is active.

True

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/prims.py
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
def set_active(stage: str, prim_path: str, active: bool = True) -> str:
    """Activate or deactivate a prim. A deactivated prim is left out of traversals.

    Args:
        stage (str): stage handle.
        prim_path (str): path of the prim.
        active (bool): whether the prim is active.

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

    Returns:
        str: handle of the stage.
    """
    get_prim(stage, prim_path).SetActive(bool(active))
    return stage

set_kind(stage, prim_path, kind)

Set the kind of a prim, like component or assembly.

Parameters:

Name Type Description Default
stage str

stage handle.

required
prim_path str

path of the prim.

required
kind str

kind to set.

required

Raises:

Type Description
ValueError

no prim at the path, or the kind can't be set.

Returns:

Name Type Description
str str

handle of the stage.

Source code in src/nyx_usd/ops/prims.py
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
def set_kind(stage: str, prim_path: str, kind: str) -> str:
    """Set the kind of a prim, like `component` or `assembly`.

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

    Raises:
        ValueError: no prim at the path, or the kind can't be set.

    Returns:
        str: handle of the stage.
    """
    if not Usd.ModelAPI(get_prim(stage, prim_path)).SetKind(kind):
        raise ValueError(f"Can't set kind {kind!r} on {prim_path}")
    return stage

set_prim_metadata(stage, prim_path, key, value=None)

Set a metadata field of a prim, like documentation or customData.

Parameters:

Name Type Description Default
stage str

stage handle.

required
prim_path str

path of the prim.

required
key str

metadata key.

required
value Any

JSON value.

None

Raises:

Type Description
ValueError

no prim at the path, or the value doesn't fit the key.

Returns:

Name Type Description
str str

handle of the stage.

Source code in src/nyx_usd/ops/prims.py
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
def set_prim_metadata(
    stage: str, prim_path: str, key: str, value: typing.Any = None
) -> str:
    """Set a metadata field of a prim, like `documentation` or `customData`.

    Args:
        stage (str): stage handle.
        prim_path (str): path of the prim.
        key (str): metadata key.
        value (Any): JSON value.

    Raises:
        ValueError: no prim at the path, or the value doesn't fit the key.

    Returns:
        str: handle of the stage.
    """
    prim = get_prim(stage, prim_path)
    try:
        authored = prim.SetMetadata(key, value)
    except Exception as err:
        raise ValueError(f"Can't set metadata {key!r} of {prim_path}: {err}") from err
    if not authored:
        raise ValueError(f"Can't set metadata {key!r} of {prim_path} to {value!r}")
    return stage