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()}")