Skip to content

Commit c2a8c2f

Browse files
committed
Add UUID-based caching of geometry and materials
1 parent 0f25add commit c2a8c2f

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

src/meshcat/geometry.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ def lower(self, object_data):
268268

269269

270270
class Object(SceneElement):
271+
sent_geom_uuids = set()
272+
sent_material_uuids = set()
273+
271274
def __init__(self, geometry, material=MeshPhongMaterial()):
272275
super(Object, self).__init__()
273276
self.geometry = geometry
@@ -289,8 +292,15 @@ def lower(self):
289292
u"matrix": list(self.geometry.intrinsic_transform().flatten())
290293
}
291294
}
292-
self.geometry.lower_in_object(data)
293-
self.material.lower_in_object(data)
295+
# If this geometry or material has been previously sent,
296+
# then we don't need to populate these fields; the server
297+
# will load the matching geometry or material from its cache.
298+
if self.geometry.uuid not in self.sent_geom_uuids:
299+
self.sent_geom_uuids.add(self.geometry.uuid)
300+
self.geometry.lower_in_object(data)
301+
if self.material.uuid not in self.sent_material_uuids:
302+
self.sent_material_uuids.add(self.material.uuid)
303+
self.material.lower_in_object(data)
294304
return data
295305

296306

src/meshcat/tests/test_drawing.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ def setUp(self):
2323
port = self.vis.url().split(":")[-1].split("/")[0]
2424
self.dummy_proc = subprocess.Popen([sys.executable, "-m", "meshcat.tests.dummy_websocket_client", str(port)])
2525
else:
26-
self.vis.open()
26+
#self.vis.open()
2727
self.dummy_proc = None
2828

29-
self.vis.wait()
29+
#self.vis.wait()
3030

3131
def tearDown(self):
3232
if self.dummy_proc is not None:
@@ -271,6 +271,43 @@ def runTest(self):
271271
v.set_object(g.TriangularMeshGeometry(vertices, faces, colors), g.MeshLambertMaterial(vertexColors=True, wireframe=True))
272272

273273

274+
class TestUUIDCloning(VisualizerTest):
275+
def runTest(self):
276+
"""
277+
Test that geometry with identical UUIDs are handled correctly.
278+
"""
279+
v = self.vis["triangular_mesh"]
280+
v.set_transform(tf.rotation_matrix(np.pi/2, [0., 0, 1]))
281+
vertices = np.array([
282+
[0, 0, 0],
283+
[1, 0, 0],
284+
[1, 0, 1],
285+
[0, 0, 1]
286+
])
287+
faces = np.array([
288+
[0, 1, 2],
289+
[3, 0, 2]
290+
])
291+
geom = g.TriangularMeshGeometry(vertices, faces)
292+
mat = g.MeshLambertMaterial(color=0xeedd22, wireframe=True)
293+
geom.uuid = 1234
294+
mat.uuid = 5678
295+
v.set_object(geom, mat)
296+
297+
# This should be drawn as a duplicate of the first geometry
298+
# at and offset; if nothing appears, or the material is different,
299+
# the UUID lookup did not succeed.
300+
geom_2 = g.TriangularMeshGeometry(np.empty((0, 3)), np.empty((0, 3)))
301+
mat_2 = g.MeshLambertMaterial(color=0x000000, wireframe=False)
302+
geom_2.uuid = 1234
303+
mat_2.uuid = 5678
304+
v2 = v["duplicate"]
305+
v2.set_transform(tf.translation_matrix([2., 0., 0.]))
306+
v2.set_object(geom_2, mat_2)
307+
308+
import time
309+
time.sleep(1000)
310+
274311
class TestOrthographicCamera(VisualizerTest):
275312
def runTest(self):
276313
"""

src/meshcat/viewer

0 commit comments

Comments
 (0)