Skip to content

Commit

Permalink
update USD
Browse files Browse the repository at this point in the history
  • Loading branch information
qbp758 committed Oct 2, 2023
1 parent 753ebdc commit 9ed9063
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
24 changes: 24 additions & 0 deletions python/rainbow/util/USD.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,30 @@ def set_mesh_positions(self, name: str, V: ArrayLike, time: float) -> None:
raise ValueError(f'Mesh {name} does not exist')
vertex_positions = Vt.Vec3fArray(V.tolist())
self.meshes[name].GetPointsAttr().Set(vertex_positions, time)

def get_mesh_positions(self, name: str, time: float) -> ArrayLike:
""" Retrieve the positions of a mesh at a given timestamp.
Args:
name (str): The name of the mesh.
time (float): The timestamp at which the positions should be retrieved.
Returns:
ArrayLike: An array containing the vertex positions of the mesh.
Raises:
ValueError: If the mesh does not exist in the scene, or if the mesh does not have positions set at the given timestamp.
"""
if name not in self.meshes:
raise ValueError(f'Mesh {name} does not exist')

vertex_positions_attr = self.meshes[name].GetPointsAttr()
vertex_positions = vertex_positions_attr.Get(time)

if vertex_positions:
return np.array(vertex_positions, dtype=np.float64)
else:
raise ValueError(f"No positions set for mesh {name} at time {time}")

def set_animation_time(self, duration: float) -> None:
""" Set the total animation time of the scene
Expand Down
4 changes: 3 additions & 1 deletion python/unit_tests/test_utils_USD.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ def test_set_mesh_positions(self):
self.usd_instance.add_mesh(
self.sample_mesh_name, self.sample_vertex_positions, self.sample_triangle_faces)
new_positions = np.array(
[[0.1, 0.1, 0.1], [1.1, 0.1, 0.1], [0.1, 1.1, 0.1]])
[[0.1, 0.1, 0.1], [1.1, 0.1, 0.1], [0.1, 1.1, 0.1]], dtype=np.float64)
time_stamp = 1.0
self.usd_instance.set_mesh_positions(
self.sample_mesh_name, new_positions, time_stamp)
updated_positions = self.usd_instance.get_mesh_positions(self.sample_mesh_name, time_stamp)
self.assertTrue(np.allclose(new_positions, updated_positions, rtol=1e-5, atol=1e-10))

def test_set_mesh_positions_with_invalid_name(self):
""" Test if the mesh does not exist, an error will be raised.
Expand Down

0 comments on commit 9ed9063

Please sign in to comment.