Mapping texture file to a surface in code

My team is struggling to get a texture loaded from a file to display on a cube. We’ve spent quite a few days working at this to no avail and haven’t been able to get any support on the forums or elsewhere. I’m wondering if perhaps this is a bug?

If I do the same thing via the UI, it works just fine. I’ve also walked through all the parameters in the Isaac Sim UI and compared the code generated ones to the UI generated ones and I don’t see any differences. However, in the code version the surface is uniform gray.

Here are couple (of many) methods that I’ve tried. Would love to get a pointer in the right direction.

    # 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))

VERSION 1

    # Ensure the cube has UV mapping
    uv_reader = UsdShade.Shader.Define(stage, material_path.AppendChild("PrimvarReader_float2"))
    uv_reader.CreateIdAttr("UsdPrimvarReader_float2")
    uv_reader.CreateInput("varname", Sdf.ValueTypeNames.Token).Set("st")

    # Connect the UV reader to the shader
    shader.CreateInput("st", Sdf.ValueTypeNames.Float2).ConnectToSource(uv_reader, "result")

    # Bind the material to the cube
    material_api = UsdShade.MaterialBindingAPI(cuboid.GetPrim())
    material_api.Bind(material)"""

VERSION 2

    # Use NVIDIA's built-in command to bind the material
    omni.kit.commands.execute(
        "BindMaterial",
        prim_path=cuboid_path.pathString,
        material_path=material_path.pathString,
        binding_strength=UsdShade.Tokens.strongerThanDescendants,
    )

    # Verify the binding
    material_api = UsdShade.MaterialBindingAPI(cuboid.GetPrim())
    bound_material = material_api.GetBoundMaterial()
    if bound_material.GetPath() != material_path:
        raise ValueError(f"Material binding failed. Expected: {material_path}, Got: {bound_material.GetPath()}")

Doing everything with omni.kit.commands works:

cls.headsup_prim_path = f"{CELL_PRIM_PATH}/headsup_display"

# Create the headsup display surface
result, cube_prim_path = omni.kit.commands.execute(
    "CreateMeshPrim",
    prim_type="Cube",
    prim_path=f"{cls.headsup_prim_path}/Cube",
    object_orgin=Gf.Vec3f(0.0, 0.0, 0.0),
)
if not result:
    raise ValueError(f"Failed to create Cube prim at path: {cls.headsup_prim_path}/Cube")

# Create the material
result, _ = omni.kit.commands.execute(
    "CreatePreviewSurfaceTextureMaterialPrim",
    mtl_path=f"{cls.headsup_prim_path}/looks/default_visual_material",
    select_new_prim=True,
)
if not result:
    raise ValueError(
        f"Failed to create material at path: {cls.headsup_prim_path}/looks/default_visual_material"
    )

# Bind the material to the cube
result, _ = omni.kit.commands.execute(
    "BindMaterial",
    prim_path=cls.headsup_prim_path,
    material_path=f"{cls.headsup_prim_path}/looks/default_visual_material",
)
if not result:
    raise ValueError(f"Failed to bind material at path: {cls.headsup_prim_path}")

# Set the file attribute on the diffuseColorTex shader
stage = get_current_stage()
texture_shader_path = f"{cls.headsup_prim_path}/looks/default_visual_material/diffuseColorTex"
texture_shader = UsdShade.Shader(stage.GetPrimAtPath(texture_shader_path))
if texture_shader:
    texture_shader.CreateInput("file", Sdf.ValueTypeNames.Asset).Set(Sdf.AssetPath(image_path))
else:
    raise ValueError(f"Failed to bind material at path: {texture_shader_path}")

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.