I’ve been having a difficult time getting a .png file to appear as a texture on a cube using code. The cube appears as uniform grey. 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 one and I don’t see any differences. I’d greatly appreciate any pointers to what I’m doing wrong. Thank you!
Here are a couple of the approaches I’ve tried:
(1)
# 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))
And (2)
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)