Skip to content

Commit dbee333

Browse files
committed
Fix material interface texture paths when there's no layer structure
- the texture paths used to be in the shader prims, now they're in the material prims
1 parent a85baab commit dbee333

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

mujoco_usd_converter/_impl/_flatten.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ def export_flattened(asset_stage: Usd.Stage, output_dir: str, asset_dir: str, as
1717
asset_identifier = f"{output_path.absolute().as_posix()}/{asset_stem}.{asset_format}"
1818
usdex.core.exportLayer(layer, asset_identifier, get_authoring_metadata(), comment)
1919

20-
# fix all PreviewMaterial inputs:file to ./Textures/xxx
20+
# fix all PreviewMaterial material interface asset inputs from abs to rel paths (./Textures/xxx)
2121
stage = Usd.Stage.Open(asset_identifier)
2222
for prim in stage.Traverse():
23-
if prim.IsA(UsdShade.Shader):
24-
shader = UsdShade.Shader(prim)
25-
file_input = shader.GetInput("file")
26-
if file_input and file_input.Get() is not None:
27-
file_path = pathlib.Path(file_input.Get().path if hasattr(file_input.Get(), "path") else file_input.Get())
28-
tmpdir = pathlib.Path(tempfile.gettempdir())
29-
if file_path.is_relative_to(tmpdir):
30-
new_path = f"./{Tokens.Textures}/{file_path.name}"
31-
file_input.Set(Sdf.AssetPath(new_path))
23+
if prim.IsA(UsdShade.Material):
24+
material = UsdShade.Material(prim)
25+
for input in material.GetInputs(onlyAuthored=True):
26+
if input.GetTypeName() == Sdf.ValueTypeNames.Asset:
27+
file_path = pathlib.Path(input.Get().path if hasattr(input.Get(), "path") else input.Get())
28+
tmpdir = pathlib.Path(tempfile.gettempdir())
29+
if file_path.is_relative_to(tmpdir):
30+
new_path = f"./{Tokens.Textures}/{file_path.name}"
31+
input.Set(Sdf.AssetPath(new_path))
3232
stage.Save()
3333
# copy texture to output dir
3434
temp_textures_dir = pathlib.Path(asset_dir) / Tokens.Payload / Tokens.Textures

tests/testAssetStructure.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,10 @@ def test_no_layer_structure_material_texture(self):
380380

381381
texture_input: UsdShade.Input = shader.GetInput("diffuseColor")
382382
connected_source = texture_input.GetConnectedSource()
383-
texture_prim = connected_source[0].GetPrim()
384-
texture_file_attr = texture_prim.GetAttribute("inputs:file")
383+
texture_shader_prim = UsdShade.Shader(connected_source[0].GetPrim())
384+
385+
# The values are defined in the material interface, not in the shader
386+
value_attrs = UsdShade.Utils.GetValueProducingAttributes(texture_shader_prim.GetInput("file"))
387+
self.assertEqual(value_attrs[0].GetPrim(), material_prim)
388+
texture_file_attr = value_attrs[0]
385389
self.assertEqual(texture_file_attr.Get().path, "./Textures/grid.png")

0 commit comments

Comments
 (0)