Skip to content

Commit 233aebc

Browse files
committed
Updated Converter CLI. Added unit test(testCli.py).
1 parent 189dde4 commit 233aebc

File tree

5 files changed

+70
-6
lines changed

5 files changed

+70
-6
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Repository = "https://github.com/newton-physics/urdf-usd-converter"
1818
Changelog = "https://github.com/newton-physics/urdf-usd-converter/blob/main/CHANGELOG.md"
1919

2020
[project.scripts]
21-
urdf_usd_converter = "urdf_usd_converter.__main__:cli_main"
21+
urdf_usd_converter = "urdf_usd_converter.__main__:run"
2222

2323
[dependency-groups]
2424
dev = [

tests/testCli.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,55 @@
11
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0
3+
import pathlib
4+
import shutil
5+
from unittest.mock import patch
36

7+
import usdex.core
8+
import usdex.test
9+
from pxr import Sdf, Tf, Usd
10+
11+
from urdf_usd_converter._impl.cli import run
412
from tests.util.ConverterTestCase import ConverterTestCase
513

614

715
class TestCli(ConverterTestCase):
816

917
def test_run(self):
10-
pass # dummy test for now
18+
for robot in pathlib.Path("tests/data").glob("*.urdf"):
19+
robot_name = robot.stem
20+
with patch("sys.argv", ["urdf_usd_converter", str(robot), self.tmpDir()]):
21+
self.assertEqual(run(), 0, f"Failed to convert {robot}")
22+
self.assertTrue((pathlib.Path(self.tmpDir()) / f"{robot_name}.usda").exists())
23+
24+
def test_invalid_input(self):
25+
with (
26+
patch("sys.argv", ["urdf_usd_converter", "tests/data/invalid.xml", self.tmpDir()]),
27+
usdex.test.ScopedDiagnosticChecker(self, [(Tf.TF_DIAGNOSTIC_WARNING_TYPE, "Input file does not exist.*")]),
28+
):
29+
self.assertEqual(run(), 1, "Expected non-zero exit code for invalid input")
30+
31+
def test_input_path_is_directory(self):
32+
# Create a directory as input_file (should fail)
33+
input_dir = pathlib.Path("tests/data/input_dir")
34+
input_dir.mkdir(parents=True, exist_ok=True)
35+
try:
36+
with (
37+
patch("sys.argv", ["urdf_usd_converter", str(input_dir), self.tmpDir()]),
38+
usdex.test.ScopedDiagnosticChecker(self, [(Tf.TF_DIAGNOSTIC_WARNING_TYPE, "Input path is not a file.*")]),
39+
):
40+
self.assertEqual(run(), 1, "Expected non-zero exit code for input path as directory")
41+
finally:
42+
shutil.rmtree(input_dir)
43+
44+
def test_input_file_not_xml(self):
45+
# Create a non-xml file as input_file (should fail)
46+
not_xml = pathlib.Path("tests/data/not_xml.txt")
47+
not_xml.write_text("dummy content")
48+
try:
49+
with (
50+
patch("sys.argv", ["urdf_usd_converter", str(not_xml), self.tmpDir()]),
51+
usdex.test.ScopedDiagnosticChecker(self, [(Tf.TF_DIAGNOSTIC_WARNING_TYPE, "Only URDF.*are supported as input.*")]),
52+
):
53+
self.assertEqual(run(), 1, "Expected non-zero exit code for non-xml input file")
54+
finally:
55+
not_xml.unlink()

urdf_usd_converter/_impl/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def __create_parser() -> argparse.ArgumentParser:
7878
"output_dir",
7979
type=Path,
8080
help="""
81-
Path to the output USD directory. The primary USD file will be <output_dir>/<modelname>.usda
81+
Path to the output USD directory. The primary USD file will be <output_dir>/<robotname>.usda
8282
and it will be an Atomic Component with Asset Interface layer and payloaded contents
8383
(unless --no-layer-structure is used)
8484
""",

urdf_usd_converter/_impl/convert.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import pathlib
44
from dataclasses import dataclass
55

6-
from pxr import Sdf, Tf
6+
import usdex.core
7+
from pxr import Sdf, Tf, UsdGeom
8+
9+
from .utils import get_authoring_metadata
710

811
__all__ = ["Converter"]
912

@@ -45,9 +48,16 @@ def convert(self, input_file: str, output_dir: str) -> Sdf.AssetPath:
4548
if not output_path.exists():
4649
output_path.mkdir(parents=True)
4750

48-
# TODO: Still a dummy.
4951
file_name = f"{input_file.stem}.usda"
5052
asset_identifier = str(output_dir / file_name)
51-
5253
Tf.Status(f"Converting {input_path} into {output_path}")
54+
asset_stage = usdex.core.createStage(
55+
asset_identifier,
56+
defaultPrimName="Robot", # TODO: use parsed name
57+
upAxis=UsdGeom.Tokens.z,
58+
linearUnits=UsdGeom.LinearUnits.meters,
59+
authoringMetadata=get_authoring_metadata(),
60+
)
61+
# TODO: implement core logic
62+
usdex.core.saveStage(asset_stage, comment=self.params.comment)
5363
return Sdf.AssetPath(asset_identifier)

urdf_usd_converter/_impl/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
from .._version import __version__
4+
5+
__all__ = ["get_authoring_metadata"]
6+
7+
8+
def get_authoring_metadata() -> str:
9+
return f"URDF USD Converter v{__version__}"

0 commit comments

Comments
 (0)