Cuboid Prim with custom points

Hello, I wanted to create a prim and set new point values (8 points to make it a cuboid) to this prim, and I have being searching through the documentation but all I could find was the command omni.usd.commands.CreatePrimWithDefaultXformCommand . I guess I could pass the data on the attributes argument but there is no more info or examples on how to do it, and I have tried different formatting styles to create it but it doesn’t work.
Do you have any example available of what I’m trying to do? Or maybe I need other class to do it?
Im new to usd and Omniverse, so maybe the problem is that I don’t have clear what I’m working with. Any imput is appreciated.
Thanks!

Hi inessorzano,

Not sure if your point values exist inside a mesh, but if so the following may help you along:

from pxr import UsdGeom, Sdf, UsdGeom, Gf
import omni.usd


class UsdGeomMeshUtility:

    @staticmethod
    def CreatePrimWithCubeMesh(
        primPath: str = "/World/newPrim",
        size: Gf.Vec3f = (2.0, 2.0, 2.0),
        position: Gf.Vec3f = (2.0, 2.0, 2.0),
        rotationXYZ: Gf.Vec3f = (45.0, 45.0, 45.0)) -> UsdGeom.Xform:
        
        """Generate a Cube with size at postion and rotation"""

        # get a reference to the active stage
        stage = omni.usd.get_context().get_stage() 
        x = size[0]
        y = size[1]
        z = size[2]
        # create or get a prim at path /World/newPrim
        targetPrim: UsdGeom.Xform = UsdGeom.Xform.Define(stage, Sdf.Path(primPath))

        # position and rotate the prim
        targetPrim.AddTranslateOp().Set(position)
        targetPrim.AddRotateXYZOp().Set(rotationXYZ)

        # create or get a mesh component on the prim
        targetMesh: UsdGeom.Mesh = UsdGeom.Mesh.Define(stage, primPath)

        # create an array with identity cube coordinates (position verts at size), and assign to points
        verts: list[Gf.Vec3f] = [
            (-x/2, -y/2, z/2),
            (x/2, -y/2, z/2),
            (-x/2, y/2, z/2),
            (x/2, y/2, z/2),
            (-x/2, y/2, -z/2),
            (x/2, y/2, -z/2),
            (-x/2, -y/2, -z/2),
            (x/2, -y/2, -z/2)]
        targetMesh.CreatePointsAttr(verts)

        # autocalculate the extend based on a pointmesh of the verts
        targetMesh.CreateExtentAttr(
            UsdGeom.PointBased(targetMesh).ComputeExtent(verts))

        # set the mesh to use no subdivision (i.e. default Polygonal mesh)
        targetMesh.CreateSubdivisionSchemeAttr().Set(UsdGeom.Tokens.none)

        # create 6 faces, each with 4 corner verts, and assign to vertex
        faceVertCnts: list[int] = [4, 4, 4, 4, 4, 4]
        targetMesh.CreateFaceVertexCountsAttr(faceVertCnts)

        # assign the 4 defined verts to the 6 faces (righthanded)
        indices: list[int] = [
            0, 1, 3, 2,
            2, 3, 5, 4,
            4, 5, 7, 6,
            6, 7, 1, 0,
            1, 7, 5, 3,
            6, 0, 2, 4]
        targetMesh.CreateFaceVertexIndicesAttr(indices)

        # assign the face normals
        normals: list[Gf.Vec3f] = [(0, 0, 1), (0, 1, 0), (0, 0, -1), (0, -1, 0), (1, 0, 0), (-1, 0, 0)]
        targetMesh.CreateNormalsAttr(normals)
        targetMesh.SetNormalsInterpolation(UsdGeom.Tokens.uniform)

        return targetPrim