You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Isaac Sim中代码给立方体贴PNG纹理仅显示灰色问题求助

问题分析与修复方案

问题描述

我尝试用代码把.png文件作为纹理贴到立方体上,但立方体始终显示均匀灰色。用UI做相同操作却能正常显示。对比了Isaac Sim UI生成的参数和代码生成的参数,没发现差异,求指点问题出在哪,谢谢!

尝试过的两种方案

方案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))

方案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)

问题根源与修复

方案1的核心问题

方案1只创建了材质、表面Shader和纹理Shader,但缺少两个关键步骤:

  1. 没有将纹理Shader的输出连接到表面Shader的diffuseColor输入
  2. 没有将材质绑定到立方体上

修复代码(添加到方案1末尾):

# 连接纹理的rgb输出到PreviewSurface的diffuseColor输入
shader.CreateInput("diffuseColor", Sdf.ValueTypeNames.Color3f).ConnectToSource(texture, "rgb")

# 将材质绑定到立方体
UsdShade.MaterialBindingAPI(cuboid).Bind(material)

方案2的排查与修复

方案2的流程更完整,但可能存在以下细节问题:

  1. 纹理路径解析问题:确保image_path是绝对路径,或者相对于当前USD stage的正确相对路径。可以添加路径验证:
# 验证纹理路径是否可解析
resolved_path = omni.usd.get_context().resolve_asset_path(image_path)
if not resolved_path:
    omni.log.error(f"无法解析纹理路径: {image_path}")
  1. 材质绑定强度不足:默认绑定可能被几何体的默认材质覆盖,指定更强的绑定强度:
material_api.Bind(preview_surface_texture_material.material, bindingStrength=UsdShade.Tokens.strongerThanDescendants)
  1. UV坐标缺失:虽然Cube默认带UV,但可以显式确认并添加:
usd_cube = UsdGeom.Cube(headsup_prim.prim)
if not usd_cube.GetPrimvar("st"):
    usd_cube.CreatePrimvar("st", Sdf.ValueTypeNames.TexCoord2fArray, interpolation=UsdGeom.Tokens.faceVarying)
  1. 纹理加载延迟:Isaac Sim加载纹理可能有延迟,可以尝试触发资产加载:
omni.usd.get_context().load_asset(texture_asset)

通用排查建议

对照UI生成的USD结构,重点检查以下几点:

  • 纹理Shader与表面Shader的连接关系(UI中会自动创建diffuseColor到纹理rgb的连接)
  • 材质到几何体的绑定是否存在(在USD Inspector中查看几何体的material:binding属性)
  • 纹理文件的assetPath是否正确,是否能被Isaac Sim解析

内容的提问来源于stack exchange,提问作者Lars

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 08:29:53