Create Cube with texture loaded from a file

You’d think this should be easy, but I’ve been trying this for a while with a number of different methods and I haven’t been able to get it to work. The cube just shows up as uniform grey. Any suggestions would be greatly appreciated!

    # Create a new stage
    stage = omni.usd.get_context().get_stage()

    # Headsup display path
    display_path = Sdf.Path("/World/HeadsupDisplay")
    # Define the path for the cuboid
    cuboid_path = display_path.AppendPath("VisualCuboid")

    # Create a cuboid with unequal sides
    cuboid = UsdGeom.Cube.Define(stage, cuboid_path)
    cuboid.GetSizeAttr().Set(1)  # This is a placeholder, we'll scale it to make it a cuboid

    # Scale the cuboid to make it a VisualCuboid
    xform = UsdGeom.XformCommonAPI(cuboid)
    xform.SetScale((0.2, 0.1, 0.3))  # Set the scale to make it a cuboid with unequal sides

    # Create a material
    material_path = display_path.AppendPath("Material")
    material = UsdShade.Material.Define(stage, material_path)

    # Create a shader
    shader = UsdShade.Shader.Define(stage, material_path.AppendChild("Shader"))
    shader.CreateIdAttr("UsdPreviewSurface")

    # Set the texture file
    texture = UsdShade.Shader.Define(stage, material_path.AppendChild("Texture"))
    texture.CreateIdAttr("UsdUVTexture")
    texture.CreateInput("file", Sdf.ValueTypeNames.Asset).Set(Sdf.AssetPath(image_path))
    texture.CreateOutput("rgb", Sdf.ValueTypeNames.Float3)

    # Connect the texture to the shader
    shader.CreateInput("diffuseColor", Sdf.ValueTypeNames.Float3).ConnectToSource(texture.ConnectableAPI(), "rgb")

    # Bind the material to the cuboid
    UsdShade.MaterialBindingAPI(cuboid).Bind(material)

I’ve also tried doing it this way. If do the same thing via the UI, it works just fine. I’ve walked through all the parameters between the code generated one and the UI generated one and I don’t see any differences. Hmmm…

        headsup_prim_path = f"{prim_path}/headsup_display"

        omni.kit.commands.execute(
            "CreateMeshPrimWithDefaultXform",
            prim_type="Cube",
            prim_path=headsup_prim_path,
            select_new_prim=True,
            prepend_default_prim=True,
            above_ground=True,
        )

        material_prim_path = f"{headsup_prim_path}/Looks/PreviewSurfaceTexture"

        omni.kit.commands.execute(
            "CreatePreviewSurfaceTextureMaterialPrim",
            mtl_path=material_prim_path,
            select_new_prim=True,
        )

        preview_surface_texture_material = PreviewSurface(
            prim_path=material_prim_path,
        )

        diffuse_color_texture_shader_prim = get_current_stage().GetPrimAtPath(
            f"{material_prim_path}/diffuseColorTex"
        )
        if not diffuse_color_texture_shader_prim:
            omni.log.warn(f"Shader not found! {material_prim_path}/diffuseColorTex")
            return

        # Ensure the diffuse_color_texture is a UsdShade.Shader
        diffuse_color_texture_shader = UsdShade.Shader(diffuse_color_texture_shader_prim)
        if not diffuse_color_texture_shader:
            raise ValueError(
                f"Prim at path {diffuse_color_texture_shader_prim.GetPrimPath()} is not a UsdShade.Shader"
            )

        file_input = diffuse_color_texture_shader.GetInput("file")
        if file_input is not None:
            texture_asset = Sdf.AssetPath(image_path)
            file_input.Set(texture_asset)

        # Scale Mesh Cube
        headsup_prim = RigidPrim(
            prim_path=headsup_prim_path,
            translation=[0.0, 0.0, 0.02458],
            scale=[0.5, 0.01, 0.5],
            orientation=euler_angles_to_quat([0, 0, 0], degrees=True, extrinsic=False),
        )
        headsup_prim.disable_rigid_body_physics()
        geometry_prim = GeometryPrim(prim_path=headsup_prim_path)
        geometry_prim.set_collision_enabled(True)

        # Bind the material to the prim
        material_api = UsdShade.MaterialBindingAPI(headsup_prim.prim)
        material_api.Bind(preview_surface_texture_material.material)