from pxr import Usd, UsdGeom, Gf, Vt, Sdf, Tf
# 1) Create the stage and our Mesh prim
stage = Usd.Stage.CreateNew("sample.usda")
mesh = UsdGeom.Mesh.Define(stage, "/Tetrahedron")
# (Optional) make it the defaultPrim
stage.SetDefaultPrim(mesh.GetPrim())
# 2) Set the point positions
mesh.GetPointsAttr().Set(Vt.Vec3fArray([
Gf.Vec3f(-1, -1, -1),
Gf.Vec3f( 1, -1, -1),
Gf.Vec3f(-1, 1, -1),
Gf.Vec3f( 1, 1, 1),
]))
# 3) Set the face‐vertex counts & indices (topology)
mesh.GetFaceVertexCountsAttr().Set(Vt.IntArray([3,3,3,3]))
mesh.GetFaceVertexIndicesAttr().Set(Vt.IntArray([
1,2,0,
3,2,1,
3,0,2,
3,1,0
]))
# 4) Turn off subdivision
mesh.GetSubdivisionSchemeAttr().Set(UsdGeom.Tokens.none)
# 5) Set per‐face (uniform) normals
mesh.GetNormalsAttr().Set(Vt.Vec3fArray([
Gf.Vec3f(-0.58, -0.58, -0.58),
Gf.Vec3f( 0.58, 0.58, -0.58),
Gf.Vec3f( 0.58, -0.58, 0.58),
Gf.Vec3f(-0.58, 0.58, 0.58),
]))
mesh.SetNormalsInterpolation(UsdGeom.Tokens.uniform)
# 6) Manually create the face‐varying "st" primvar and its indices
prim = mesh.GetPrim()
# Create primvars:st by passing ["primvars","st"] as the name
st_attr = prim.CreateAttribute(
["primvars","st"], # this yields an attribute named "primvars:st"
Sdf.ValueTypeNames.Float2Array # typeName
)
st_attr.Set(Vt.Vec2fArray([
Gf.Vec2f(0.75, 0.43),
Gf.Vec2f(0.5, 0.87),
Gf.Vec2f(0.25, 0.43),
Gf.Vec2f(1.0, 0.87),
Gf.Vec2f(0.0, 0.87),
Gf.Vec2f(0.5, 0.5),
]))
# Attach the interpolation metadata
st_attr.SetMetadata("interpolation", UsdGeom.Tokens.faceVarying)
# st:indices
idx_attr = prim.CreateAttribute(
["primvars","st","indices"],
Sdf.ValueTypeNames.IntArray
)
idx_attr.Set(Vt.IntArray([
0,1,2,
3,1,0,
4,2,1,
5,0,2
]))
# 7) Save out the USD‐ASCII file
stage.GetRootLayer().Save()