Skip to content

geometry

geometry

Transforms, visibility, basic shapes and materials.

Attributes

PURPOSES = ('default', 'render', 'proxy', 'guide') module-attribute

Functions:

bind_material(stage, prim_path, material_path)

Bind a material to a prim and what is below it.

Parameters:

Name Type Description Default
stage str

stage handle.

required
prim_path str

path of the prim.

required
material_path str

path of the material prim.

required

Raises:

Type Description
ValueError

no prim at a path, or material_path is not a material.

Returns:

Name Type Description
str str

handle of the stage.

Source code in src/nyx_usd/ops/geometry.py
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
def bind_material(stage: str, prim_path: str, material_path: str) -> str:
    """Bind a material to a prim and what is below it.

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

    Raises:
        ValueError: no prim at a path, or `material_path` is not a material.

    Returns:
        str: handle of the stage.
    """
    prim = get_prim(stage, prim_path)
    material = UsdShade.Material(get_prim(stage, material_path))
    if not material:
        raise ValueError(f"{material_path} is not a Material")
    UsdShade.MaterialBindingAPI.Apply(prim).Bind(material)
    return stage

create_cube(stage, prim_path, size=1.0)

Define a cube prim.

Parameters:

Name Type Description Default
stage str

stage handle.

required
prim_path str

path of the prim.

required
size float

length of an edge.

1.0

Returns:

Name Type Description
str str

handle of the stage.

Source code in src/nyx_usd/ops/geometry.py
186
187
188
189
190
191
192
193
194
195
196
197
198
def create_cube(stage: str, prim_path: str, size: float = 1.0) -> str:
    """Define a cube prim.

    Args:
        stage (str): stage handle.
        prim_path (str): path of the prim.
        size (float): length of an edge.

    Returns:
        str: handle of the stage.
    """
    UsdGeom.Cube.Define(get_stage(stage), prim_path).GetSizeAttr().Set(float(size))
    return stage

create_material(stage, prim_path, diffuse_color=None)

Define a material with a UsdPreviewSurface shader.

Parameters:

Name Type Description Default
stage str

stage handle.

required
prim_path str

path of the material. The shader is PreviewSurface below it.

required
diffuse_color list[float] | None

red, green and blue, [0.8, 0.8, 0.8] if omitted.

None

Raises:

Type Description
ValueError

the color doesn't have three numbers.

Returns:

Name Type Description
str str

handle of the stage.

Source code in src/nyx_usd/ops/geometry.py
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
def create_material(
    stage: str, prim_path: str, diffuse_color: list[float] | None = None
) -> str:
    """Define a material with a UsdPreviewSurface shader.

    Args:
        stage (str): stage handle.
        prim_path (str): path of the material. The shader is `PreviewSurface` below it.
        diffuse_color (list[float] | None): red, green and blue, `[0.8, 0.8, 0.8]` if
            omitted.

    Raises:
        ValueError: the color doesn't have three numbers.

    Returns:
        str: handle of the stage.
    """
    color = _vector([0.8, 0.8, 0.8] if diffuse_color is None else diffuse_color, "color")
    usd_stage = get_stage(stage)
    material = UsdShade.Material.Define(usd_stage, prim_path)
    shader = UsdShade.Shader.Define(usd_stage, f"{prim_path}/PreviewSurface")
    shader.CreateIdAttr("UsdPreviewSurface")
    shader.CreateInput("diffuseColor", Sdf.ValueTypeNames.Color3f).Set(Gf.Vec3f(*color))
    surface = shader.CreateOutput("surface", Sdf.ValueTypeNames.Token)
    material.CreateSurfaceOutput().ConnectToSource(surface)
    return stage

create_mesh(stage, prim_path, points, face_vertex_counts, face_vertex_indices)

Define a polygon mesh prim.

Parameters:

Name Type Description Default
stage str

stage handle.

required
prim_path str

path of the prim.

required
points list[list[float]]

position of every vertex, as x, y and z.

required
face_vertex_counts list[int]

number of vertices of every face.

required
face_vertex_indices list[int]

vertex of every corner of every face.

required

Raises:

Type Description
ValueError

the counts and the indices don't fit, or an index is out of range.

Returns:

Name Type Description
str str

handle of the stage.

Source code in src/nyx_usd/ops/geometry.py
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
def create_mesh(
    stage: str,
    prim_path: str,
    points: list[list[float]],
    face_vertex_counts: list[int],
    face_vertex_indices: list[int],
) -> str:
    """Define a polygon mesh prim.

    Args:
        stage (str): stage handle.
        prim_path (str): path of the prim.
        points (list[list[float]]): position of every vertex, as x, y and z.
        face_vertex_counts (list[int]): number of vertices of every face.
        face_vertex_indices (list[int]): vertex of every corner of every face.

    Raises:
        ValueError: the counts and the indices don't fit, or an index is out of range.

    Returns:
        str: handle of the stage.
    """
    if sum(face_vertex_counts) != len(face_vertex_indices):
        raise ValueError(
            f"The faces have {sum(face_vertex_counts)} corners, "
            f"there are {len(face_vertex_indices)} indices"
        )
    if face_vertex_indices and not 0 <= min(face_vertex_indices) <= max(
        face_vertex_indices
    ) < len(points):
        raise ValueError(f"An index is outside of the {len(points)} points")
    mesh = UsdGeom.Mesh.Define(get_stage(stage), prim_path)
    mesh.GetPointsAttr().Set(
        Vt.Vec3fArray([Gf.Vec3f(*_vector(p, "point")) for p in points])
    )
    mesh.GetFaceVertexCountsAttr().Set(Vt.IntArray([int(c) for c in face_vertex_counts]))
    mesh.GetFaceVertexIndicesAttr().Set(
        Vt.IntArray([int(i) for i in face_vertex_indices])
    )
    return stage

create_sphere(stage, prim_path, radius=1.0)

Define a sphere prim.

Parameters:

Name Type Description Default
stage str

stage handle.

required
prim_path str

path of the prim.

required
radius float

radius of the sphere.

1.0

Returns:

Name Type Description
str str

handle of the stage.

Source code in src/nyx_usd/ops/geometry.py
201
202
203
204
205
206
207
208
209
210
211
212
213
def create_sphere(stage: str, prim_path: str, radius: float = 1.0) -> str:
    """Define a sphere prim.

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

    Returns:
        str: handle of the stage.
    """
    UsdGeom.Sphere.Define(get_stage(stage), prim_path).GetRadiusAttr().Set(float(radius))
    return stage

get_bounding_box(stage, prim_path)

Get the bounds of a prim and what is below it, in the world.

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[list[float]]

list[list[float]]: the minimum and maximum corner. Empty bounds have a minimum larger than the maximum.

Source code in src/nyx_usd/ops/geometry.py
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
def get_bounding_box(stage: str, prim_path: str) -> list[list[float]]:
    """Get the bounds of a prim and what is below it, in the world.

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

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

    Returns:
        list[list[float]]: the minimum and maximum corner. Empty bounds have a
            minimum larger than the maximum.
    """
    cache = UsdGeom.BBoxCache(Usd.TimeCode.Default(), [UsdGeom.Tokens.default_])
    box = cache.ComputeWorldBound(get_prim(stage, prim_path)).ComputeAlignedRange()
    return [list(box.GetMin()), list(box.GetMax())]

get_transform(stage, prim_path)

Get the local transform of a prim as a 4x4 matrix.

Parameters:

Name Type Description Default
stage str

stage handle.

required
prim_path str

path of the prim.

required

Raises:

Type Description
ValueError

no prim at the path, or it can't be transformed.

Returns:

Type Description
list[list[float]]

list[list[float]]: the matrix, as rows.

Source code in src/nyx_usd/ops/geometry.py
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
def get_transform(stage: str, prim_path: str) -> list[list[float]]:
    """Get the local transform of a prim as a 4x4 matrix.

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

    Raises:
        ValueError: no prim at the path, or it can't be transformed.

    Returns:
        list[list[float]]: the matrix, as rows.
    """
    matrix = _xformable(stage, prim_path).GetLocalTransformation(Usd.TimeCode.Default())
    return to_json(matrix)

get_world_transform(stage, prim_path)

Get the transform of a prim in the world as a 4x4 matrix.

Parameters:

Name Type Description Default
stage str

stage handle.

required
prim_path str

path of the prim.

required

Raises:

Type Description
ValueError

no prim at the path, or it can't be transformed.

Returns:

Type Description
list[list[float]]

list[list[float]]: the matrix, as rows.

Source code in src/nyx_usd/ops/geometry.py
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
def get_world_transform(stage: str, prim_path: str) -> list[list[float]]:
    """Get the transform of a prim in the world as a 4x4 matrix.

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

    Raises:
        ValueError: no prim at the path, or it can't be transformed.

    Returns:
        list[list[float]]: the matrix, as rows.
    """
    matrix = _xformable(stage, prim_path).ComputeLocalToWorldTransform(
        Usd.TimeCode.Default()
    )
    return to_json(matrix)

set_purpose(stage, prim_path, purpose='default')

Set the purpose of a prim: what it is drawn for.

Parameters:

Name Type Description Default
stage str

stage handle.

required
prim_path str

path of the prim.

required
purpose str

default, render, proxy or guide.

'default'

Raises:

Type Description
ValueError

no prim at the path, it has no purpose, or the purpose is unknown.

Returns:

Name Type Description
str str

handle of the stage.

Source code in src/nyx_usd/ops/geometry.py
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
def set_purpose(stage: str, prim_path: str, purpose: str = "default") -> str:
    """Set the purpose of a prim: what it is drawn for.

    Args:
        stage (str): stage handle.
        prim_path (str): path of the prim.
        purpose (str): `default`, `render`, `proxy` or `guide`.

    Raises:
        ValueError: no prim at the path, it has no purpose, or the purpose is unknown.

    Returns:
        str: handle of the stage.
    """
    if purpose not in PURPOSES:
        raise ValueError(f"The purpose has to be one of {PURPOSES}, not {purpose!r}")
    imageable = UsdGeom.Imageable(get_prim(stage, prim_path))
    if not imageable:
        raise ValueError(f"{prim_path} has no purpose, it is not Imageable")
    imageable.GetPurposeAttr().Set(purpose)
    return stage

set_transform(stage, prim_path, translate=None, rotate=None, scale=None)

Set the local transform of a prim, replacing the transform operations it has.

Parameters:

Name Type Description Default
stage str

stage handle.

required
prim_path str

path of the prim.

required
translate list[float] | None

x, y and z, [0, 0, 0] if omitted.

None
rotate list[float] | None

x, y and z rotation in degrees, [0, 0, 0] if omitted.

None
scale list[float] | None

x, y and z, [1, 1, 1] if omitted.

None

Raises:

Type Description
ValueError

no prim at the path, it can't be transformed, or a list doesn't have three numbers.

Returns:

Name Type Description
str str

handle of the stage.

Source code in src/nyx_usd/ops/geometry.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def set_transform(
    stage: str,
    prim_path: str,
    translate: list[float] | None = None,
    rotate: list[float] | None = None,
    scale: list[float] | None = None,
) -> str:
    """Set the local transform of a prim, replacing the transform operations it has.

    Args:
        stage (str): stage handle.
        prim_path (str): path of the prim.
        translate (list[float] | None): x, y and z, `[0, 0, 0]` if omitted.
        rotate (list[float] | None): x, y and z rotation in degrees, `[0, 0, 0]` if
            omitted.
        scale (list[float] | None): x, y and z, `[1, 1, 1]` if omitted.

    Raises:
        ValueError: no prim at the path, it can't be transformed, or a list doesn't
            have three numbers.

    Returns:
        str: handle of the stage.
    """
    translation = _vector([0, 0, 0] if translate is None else translate, "translate")
    rotation = _vector([0, 0, 0] if rotate is None else rotate, "rotate")
    scaling = _vector([1, 1, 1] if scale is None else scale, "scale")
    xformable = _xformable(stage, prim_path)
    xformable.ClearXformOpOrder()
    xformable.AddTranslateOp().Set(Gf.Vec3d(*translation))
    xformable.AddRotateXYZOp().Set(Gf.Vec3f(*rotation))
    xformable.AddScaleOp().Set(Gf.Vec3f(*scaling))
    return stage

set_visibility(stage, prim_path, visible=True)

Show or hide a prim and what is below it.

Parameters:

Name Type Description Default
stage str

stage handle.

required
prim_path str

path of the prim.

required
visible bool

whether the prim is visible.

True

Raises:

Type Description
ValueError

no prim at the path, or it has no visibility.

Returns:

Name Type Description
str str

handle of the stage.

Source code in src/nyx_usd/ops/geometry.py
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
def set_visibility(stage: str, prim_path: str, visible: bool = True) -> str:
    """Show or hide a prim and what is below it.

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

    Raises:
        ValueError: no prim at the path, or it has no visibility.

    Returns:
        str: handle of the stage.
    """
    imageable = UsdGeom.Imageable(get_prim(stage, prim_path))
    if not imageable:
        raise ValueError(f"{prim_path} has no visibility, it is not Imageable")
    if visible:
        imageable.MakeVisible()
    else:
        imageable.MakeInvisible()
    return stage