|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2025 The Newton Developers |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +import pathlib |
| 4 | +import shutil |
| 5 | +from unittest.mock import patch |
| 6 | + |
| 7 | +from pxr import Usd, UsdGeom |
| 8 | + |
| 9 | +from tests.util.ConverterTestCase import ConverterTestCase |
| 10 | +from urdf_usd_converter._impl.cli import run |
| 11 | + |
| 12 | + |
| 13 | +class TestROSPackagesCli(ConverterTestCase): |
| 14 | + def test_do_not_specify_ros_package_name(self): |
| 15 | + # If the `package` argument is not specified in `converter.convert`. |
| 16 | + # In this case, if the mesh or texture URI specifies "package://PackageName/foo/test.png", |
| 17 | + # this will be removed and treated as a relative path "foo/test.png". |
| 18 | + input_path = "tests/data/ros_packages.urdf" |
| 19 | + output_dir = self.tmpDir() |
| 20 | + |
| 21 | + with patch("sys.argv", ["urdf_usd_converter", input_path, output_dir]): |
| 22 | + self.assertEqual(run(), 0, f"Failed to convert {input_path}") |
| 23 | + |
| 24 | + output_path = pathlib.Path(output_dir) / "ros_packages.usda" |
| 25 | + self.assertTrue(output_path.exists()) |
| 26 | + |
| 27 | + self.stage: Usd.Stage = Usd.Stage.Open(str(output_path)) |
| 28 | + self.assertIsValidUsd(self.stage) |
| 29 | + |
| 30 | + # Check geometry. |
| 31 | + default_prim = self.stage.GetDefaultPrim() |
| 32 | + geometry_scope_prim = self.stage.GetPrimAtPath(default_prim.GetPath().AppendChild("Geometry")) |
| 33 | + self.assertTrue(geometry_scope_prim.IsValid()) |
| 34 | + |
| 35 | + link_mesh_stl_path = geometry_scope_prim.GetPath().AppendChild("BaseLink").AppendChild("link_mesh_stl") |
| 36 | + link_stl_prim = self.stage.GetPrimAtPath(link_mesh_stl_path) |
| 37 | + self.assertTrue(link_stl_prim.IsValid()) |
| 38 | + self.assertTrue(link_stl_prim.IsA(UsdGeom.Xform)) |
| 39 | + |
| 40 | + stl_mesh_prim = self.stage.GetPrimAtPath(link_mesh_stl_path.AppendChild("box")) |
| 41 | + self.assertTrue(stl_mesh_prim.IsValid()) |
| 42 | + self.assertTrue(stl_mesh_prim.IsA(UsdGeom.Mesh)) |
| 43 | + self.assertTrue(stl_mesh_prim.HasAuthoredReferences()) |
| 44 | + |
| 45 | + # Check material texture. |
| 46 | + # TODO: Here we need to make sure that the reference to the usd file is correct after the texture is loaded. |
| 47 | + |
| 48 | + def test_specify_ros_package_names(self): |
| 49 | + """ |
| 50 | + Specify ROS package arguments as CLI |
| 51 | + """ |
| 52 | + input_path = "tests/data/ros_packages.urdf" |
| 53 | + output_dir = self.tmpDir() |
| 54 | + |
| 55 | + test_package_dir = output_dir + "/temp" |
| 56 | + test_texture_package_dir = output_dir + "/temp/textures" |
| 57 | + pathlib.Path(test_package_dir + "/assets").mkdir(parents=True, exist_ok=True) |
| 58 | + pathlib.Path(test_texture_package_dir + "/assets").mkdir(parents=True, exist_ok=True) |
| 59 | + |
| 60 | + # Copy "tests/data/assets/box.stl" to test_package_dir |
| 61 | + shutil.copy("tests/data/assets/box.stl", test_package_dir + "/assets") |
| 62 | + |
| 63 | + # Copy "tests/data/assets/grid.png" to test_texture_package_dir |
| 64 | + shutil.copy("tests/data/assets/grid.png", test_texture_package_dir + "/assets") |
| 65 | + |
| 66 | + temp_stl_file_path = test_package_dir + "/assets/box.stl" |
| 67 | + temp_texture_file_path = test_texture_package_dir + "/assets/grid.png" |
| 68 | + self.assertTrue(pathlib.Path(temp_stl_file_path).exists()) |
| 69 | + self.assertTrue(pathlib.Path(temp_texture_file_path).exists()) |
| 70 | + |
| 71 | + with patch( |
| 72 | + "sys.argv", |
| 73 | + [ |
| 74 | + "urdf_usd_converter", |
| 75 | + input_path, |
| 76 | + output_dir, |
| 77 | + "--package", |
| 78 | + "test_package=" + test_package_dir, |
| 79 | + "--package", |
| 80 | + "test_texture_package=" + test_texture_package_dir, |
| 81 | + ], |
| 82 | + ): |
| 83 | + self.assertEqual(run(), 0, f"Failed to convert {input_path}") |
| 84 | + |
| 85 | + output_path = pathlib.Path(output_dir) / "ros_packages.usda" |
| 86 | + self.assertTrue(output_path.exists()) |
| 87 | + |
| 88 | + self.stage: Usd.Stage = Usd.Stage.Open(str(output_path)) |
| 89 | + self.assertIsValidUsd(self.stage) |
| 90 | + |
| 91 | + # Check geometry. |
| 92 | + default_prim = self.stage.GetDefaultPrim() |
| 93 | + geometry_scope_prim = self.stage.GetPrimAtPath(default_prim.GetPath().AppendChild("Geometry")) |
| 94 | + self.assertTrue(geometry_scope_prim.IsValid()) |
| 95 | + |
| 96 | + link_mesh_stl_path = geometry_scope_prim.GetPath().AppendChild("BaseLink").AppendChild("link_mesh_stl") |
| 97 | + link_stl_prim = self.stage.GetPrimAtPath(link_mesh_stl_path) |
| 98 | + self.assertTrue(link_stl_prim.IsValid()) |
| 99 | + self.assertTrue(link_stl_prim.IsA(UsdGeom.Xform)) |
| 100 | + |
| 101 | + stl_mesh_prim = self.stage.GetPrimAtPath(link_mesh_stl_path.AppendChild("box")) |
| 102 | + self.assertTrue(stl_mesh_prim.IsValid()) |
| 103 | + self.assertTrue(stl_mesh_prim.IsA(UsdGeom.Mesh)) |
| 104 | + self.assertTrue(stl_mesh_prim.HasAuthoredReferences()) |
| 105 | + |
| 106 | + # Check material texture. |
| 107 | + # TODO: Here we need to make sure that the reference to the usd file is correct after the texture is loaded. |
0 commit comments