def test_meshcat_animation(self): animation = mut.MeshcatAnimation(frames_per_second=64) self.assertEqual(animation.frames_per_second(), 64) self.assertEqual(animation.frame(1.0), 64) animation.set_autoplay(play=False) self.assertEqual(animation.autoplay(), False) animation.set_loop_mode(mode=mut.MeshcatAnimation.LoopMode.kLoopOnce) animation.set_loop_mode(mode=mut.MeshcatAnimation.LoopMode.kLoopRepeat) animation.set_loop_mode( mode=mut.MeshcatAnimation.LoopMode.kLoopPingPong) self.assertEqual(animation.loop_mode(), mut.MeshcatAnimation.LoopMode.kLoopPingPong) animation.set_repetitions(repetitions=20) self.assertEqual(animation.repetitions(), 20) animation.set_clamp_when_finished(clamp=False) self.assertEqual(animation.clamp_when_finished(), False) animation.SetTransform(frame=0, path="test", X_ParentPath=RigidTransform()) animation.SetProperty(frame=0, path="test", property="bool", value=True) animation.SetProperty(frame=0, path="test", property="double", value=32.0) animation.SetProperty(frame=0, path="test", property="vector_double", value=[1., 2., 3.]) meshcat = mut.Meshcat() meshcat.SetAnimation(animation)
def test_meshcat_visualizer_scalar_conversion_to_be_deprecated(self): # This checks a legacy API spelling that will be deprecated eventually. meshcat = mut.Meshcat() vis = mut.MeshcatVisualizerCpp(meshcat) vis_autodiff = vis.ToAutoDiffXd() self.assertIsInstance(vis_autodiff, mut.MeshcatVisualizerCpp_[AutoDiffXd])
def test_meshcat_visualizer(self, T): meshcat = mut.Meshcat() params = mut.MeshcatVisualizerParams() params.publish_period = 0.123 params.role = mut.Role.kIllustration params.default_color = mut.Rgba(0.5, 0.5, 0.5) params.prefix = "py_visualizer" params.delete_on_initialization_event = False self.assertNotIn("object at 0x", repr(params)) vis = mut.MeshcatVisualizer_[T](meshcat=meshcat, params=params) vis.Delete() self.assertIsInstance(vis.query_object_input_port(), InputPort_[T]) animation = vis.StartRecording(set_transforms_while_recording=True) self.assertIsInstance(animation, mut.MeshcatAnimation) self.assertEqual(animation, vis.get_mutable_recording()) vis.StopRecording() vis.PublishRecording() vis.DeleteRecording() builder = DiagramBuilder_[T]() scene_graph = builder.AddSystem(mut.SceneGraph_[T]()) mut.MeshcatVisualizer_[T].AddToBuilder(builder=builder, scene_graph=scene_graph, meshcat=meshcat, params=params) mut.MeshcatVisualizer_[T].AddToBuilder( builder=builder, query_object_port=scene_graph.get_query_output_port(), meshcat=meshcat, params=params)
def test_deprecated_meshcat_visualizer_cpp_scalar_conversion(self): """This checks a deprecated API spelling; remove this on 2022-11-01.""" meshcat = mut.Meshcat() with catch_drake_warnings(expected_count=1): vis = mut.MeshcatVisualizerCpp(meshcat) vis_autodiff = vis.ToAutoDiffXd() self.assertIsInstance(vis_autodiff, mut.MeshcatVisualizerCpp_[AutoDiffXd])
def test_deprecated_meshcat_point_cloud_visualizer_cpp(self): """This checks a deprecated API spelling; remove this on 2022-11-01.""" meshcat = mut.Meshcat() with catch_drake_warnings(expected_count=1): visualizer = mut.MeshcatPointCloudVisualizerCpp( meshcat=meshcat, path="cloud", publish_period=1/12.0) visualizer.set_point_size(0.1) ad_visualizer = visualizer.ToAutoDiffXd() self.assertIsInstance( ad_visualizer, mut.MeshcatPointCloudVisualizerCpp_[AutoDiffXd])
def test_deprecated_meshcat_visualizer_cpp_add(self): """This checks a deprecated API spelling; remove this on 2022-11-01.""" T = float builder = DiagramBuilder_[T]() scene_graph = builder.AddSystem(mut.SceneGraph_[T]()) meshcat = mut.Meshcat() with catch_drake_warnings(expected_count=1): added = mut.MeshcatVisualizerCpp.AddToBuilder( builder=builder, scene_graph=scene_graph, meshcat=meshcat) self.assertIsInstance(added, mut.MeshcatVisualizer)
def test_meshcat_point_cloud_visualizer_to_be_deprecated(self): # This checks a legacy API spelling that will be deprecated eventually. meshcat = mut.Meshcat() visualizer = mut.MeshcatPointCloudVisualizerCpp(meshcat=meshcat, path="cloud", publish_period=1 / 12.0) visualizer.set_point_size(0.1) ad_visualizer = visualizer.ToAutoDiffXd() self.assertIsInstance(ad_visualizer, mut.MeshcatPointCloudVisualizerCpp_[AutoDiffXd])
def test_meshcat_point_cloud_visualizer(self, T): meshcat = mut.Meshcat() visualizer = mut.MeshcatPointCloudVisualizer_[T]( meshcat=meshcat, path="cloud", publish_period=1/12.0) visualizer.set_point_size(0.1) visualizer.set_default_rgba(mut.Rgba(0, 0, 1, 1)) context = visualizer.CreateDefaultContext() cloud = PointCloud(4) cloud.mutable_xyzs()[:] = np.zeros((3, 4)) visualizer.cloud_input_port().FixValue( context, AbstractValue.Make(cloud)) self.assertIsInstance(visualizer.pose_input_port(), InputPort_[T]) visualizer.Publish(context) visualizer.Delete() if T == float: ad_visualizer = visualizer.ToAutoDiffXd() self.assertIsInstance( ad_visualizer, mut.MeshcatPointCloudVisualizer_[AutoDiffXd])
def test_meshcat(self): port = 7051 params = mut.MeshcatParams(host="*", port=port, web_url_pattern="http://host:{port}") meshcat = mut.Meshcat(params=params) self.assertEqual(meshcat.port(), port) with self.assertRaises(RuntimeError): meshcat2 = mut.Meshcat(port=port) self.assertIn("http", meshcat.web_url()) self.assertIn("ws", meshcat.ws_url()) meshcat.SetObject(path="/test/box", shape=mut.Box(1, 1, 1), rgba=mut.Rgba(.5, .5, .5)) meshcat.SetTransform(path="/test/box", X_ParentPath=RigidTransform()) meshcat.SetTransform(path="/test/box", matrix=np.eye(4)) self.assertTrue(meshcat.HasPath("/test/box")) cloud = PointCloud(4) cloud.mutable_xyzs()[:] = np.zeros((3, 4)) meshcat.SetObject(path="/test/cloud", cloud=cloud, point_size=0.01, rgba=mut.Rgba(.5, .5, .5)) mesh = mut.TriangleSurfaceMesh(triangles=[ mut.SurfaceTriangle(0, 1, 2), mut.SurfaceTriangle(3, 0, 2) ], vertices=[[0, 0, 0], [1, 0, 0], [1, 0, 1], [0, 0, 1]]) meshcat.SetObject(path="/test/triangle_surface_mesh", mesh=mesh, rgba=mut.Rgba(0.3, 0.3, 0.3), wireframe=True, wireframe_line_width=2.0) meshcat.SetLine(path="/test/line", vertices=np.eye(3), line_width=2.0, rgba=mut.Rgba(.3, .3, .3)) meshcat.SetLineSegments(path="/test/line_segments", start=np.eye(3), end=2 * np.eye(3), line_width=2.0, rgba=mut.Rgba(.3, .3, .3)) meshcat.SetTriangleMesh(path="/test/triangle_mesh", vertices=np.array([[0, 0, 0], [1, 0, 0], [1, 0, 1], [0, 0, 1]]).T, faces=np.array([[0, 1, 2], [3, 0, 2]]).T, rgba=mut.Rgba(0.3, 0.3, 0.3), wireframe=True, wireframe_line_width=2.0) meshcat.SetProperty(path="/Background", property="visible", value=True) meshcat.SetProperty(path="/Lights/DirectionalLight/<object>", property="intensity", value=1.0) meshcat.SetProperty(path="/Background", property="top_color", value=[0, 0, 0]) meshcat.Set2dRenderMode(X_WC=RigidTransform(), xmin=-1, xmax=1, ymin=-1, ymax=1) meshcat.ResetRenderMode() meshcat.AddButton(name="button") self.assertEqual(meshcat.GetButtonClicks(name="button"), 0) meshcat.DeleteButton(name="button") meshcat.AddSlider(name="slider", min=0, max=1, step=0.01, value=0.5) meshcat.SetSliderValue(name="slider", value=0.7) self.assertAlmostEqual(meshcat.GetSliderValue(name="slider"), 0.7, delta=1e-14) meshcat.DeleteSlider(name="slider") meshcat.DeleteAddedControls() self.assertIn("data:application/octet-binary;base64", meshcat.StaticHtml()) meshcat.Flush() # PerspectiveCamera camera = mut.Meshcat.PerspectiveCamera(fov=80, aspect=1.2, near=0.2, far=200, zoom=1.3) self.assertEqual(camera.fov, 80) self.assertEqual(camera.aspect, 1.2) self.assertEqual(camera.near, 0.2) self.assertEqual(camera.far, 200) self.assertEqual(camera.zoom, 1.3) self.assertEqual( repr(camera), "".join([ r"PerspectiveCamera(", r"fov=80.0, " r"aspect=1.2, ", r"near=0.2, ", r"far=200.0, ", r"zoom=1.3)" ])) meshcat.SetCamera(camera=camera, path="mypath") # OrthographicCamera camera = mut.Meshcat.OrthographicCamera(left=0.1, right=1.3, top=0.3, bottom=1.4, near=0.2, far=200, zoom=1.3) self.assertEqual(camera.left, 0.1) self.assertEqual(camera.right, 1.3) self.assertEqual(camera.top, 0.3) self.assertEqual(camera.bottom, 1.4) self.assertEqual(camera.near, 0.2) self.assertEqual(camera.far, 200) self.assertEqual(camera.zoom, 1.3) self.assertEqual( repr(camera), "".join([ r"OrthographicCamera(", r"left=0.1, " r"right=1.3, ", r"top=0.3, " r"bottom=1.4, ", r"near=0.2, ", r"far=200.0, ", r"zoom=1.3)" ])) meshcat.SetCamera(camera=camera, path="mypath")
def test_meshcat_visualizer_scalar_conversion(self): meshcat = mut.Meshcat() vis = mut.MeshcatVisualizer(meshcat) vis_autodiff = vis.ToAutoDiffXd() self.assertIsInstance(vis_autodiff, mut.MeshcatVisualizer_[AutoDiffXd])
def test_meshcat(self): meshcat = mut.Meshcat(port=7051) self.assertEqual(meshcat.port(), 7051) with self.assertRaises(RuntimeError): meshcat2 = mut.Meshcat(port=7051) self.assertIn("http", meshcat.web_url()) self.assertIn("ws", meshcat.ws_url()) meshcat.SetObject(path="/test/box", shape=mut.Box(1, 1, 1), rgba=mut.Rgba(.5, .5, .5)) meshcat.SetTransform(path="/test/box", X_ParentPath=RigidTransform()) meshcat.SetTransform(path="/test/box", matrix=np.eye(4)) self.assertTrue(meshcat.HasPath("/test/box")) cloud = PointCloud(4) cloud.mutable_xyzs()[:] = np.zeros((3, 4)) meshcat.SetObject(path="/test/cloud", cloud=cloud, point_size=0.01, rgba=mut.Rgba(.5, .5, .5)) mesh = mut.TriangleSurfaceMesh(triangles=[ mut.SurfaceTriangle(0, 1, 2), mut.SurfaceTriangle(3, 0, 2) ], vertices=[[0, 0, 0], [1, 0, 0], [1, 0, 1], [0, 0, 1]]) meshcat.SetObject(path="/test/triangle_surface_mesh", mesh=mesh, rgba=mut.Rgba(0.3, 0.3, 0.3), wireframe=True, wireframe_line_width=2.0) meshcat.SetLine(path="/test/line", vertices=np.eye(3), line_width=2.0, rgba=mut.Rgba(.3, .3, .3)) meshcat.SetLineSegments(path="/test/line_segments", start=np.eye(3), end=2 * np.eye(3), line_width=2.0, rgba=mut.Rgba(.3, .3, .3)) meshcat.SetTriangleMesh(path="/test/triangle_mesh", vertices=np.array([[0, 0, 0], [1, 0, 0], [1, 0, 1], [0, 0, 1]]).T, faces=np.array([[0, 1, 2], [3, 0, 2]]).T, rgba=mut.Rgba(0.3, 0.3, 0.3), wireframe=True, wireframe_line_width=2.0) meshcat.SetProperty(path="/Background", property="visible", value=True) meshcat.SetProperty(path="/Lights/DirectionalLight/<object>", property="intensity", value=1.0) meshcat.SetProperty(path="/Background", property="top_color", value=[0, 0, 0]) meshcat.Set2dRenderMode(X_WC=RigidTransform(), xmin=-1, xmax=1, ymin=-1, ymax=1) meshcat.ResetRenderMode() meshcat.AddButton(name="button") self.assertEqual(meshcat.GetButtonClicks(name="button"), 0) meshcat.DeleteButton(name="button") meshcat.AddSlider(name="slider", min=0, max=1, step=0.01, value=0.5) meshcat.SetSliderValue(name="slider", value=0.7) self.assertAlmostEqual(meshcat.GetSliderValue(name="slider"), 0.7, delta=1e-14) meshcat.DeleteSlider(name="slider") meshcat.DeleteAddedControls() self.assertIn("data:application/octet-binary;base64", meshcat.StaticHtml()) meshcat.Flush()