Skip to content

Commit 3fba171

Browse files
authored
[py visualization] Fix meldis crash on gltf embedded png (#23013)
When an image lacked a uri (e.g., when using a bufferView), the checksum logic would crash.
1 parent 7a5820e commit 3fba171

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

bindings/pydrake/visualization/_meldis.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -256,15 +256,12 @@ def on_gltf_from_disk(self, path: Path, content: bytes):
256256
_logger.warning(f"glTF file is not valid JSON: {path}")
257257
return
258258

259-
# Handle the images
260-
for image in document.get("images", []):
261-
if not image.get("uri", "").startswith("data:"):
262-
self.on_texture_from_disk(path.parent / image["uri"])
263-
264-
# Handle the .bin files.
265-
for buffer in document.get("buffers", []):
266-
if not buffer.get("uri", "").startswith("data:"):
267-
self._read_file(path.parent / buffer["uri"])
259+
# Handle images and .bin files cited via URIs.
260+
for array_property in ("images", "buffers"):
261+
for item in document.get(array_property, []):
262+
uri = item.get("uri", None)
263+
if uri and not uri.startswith("data:"):
264+
self._read_file(path.parent / uri)
268265

269266

270267
class _ViewerApplet:

bindings/pydrake/visualization/test/meldis_test.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,13 +413,32 @@ def dut(data):
413413
self.assertNotEqual(gltf_hash_2, empty_hash)
414414
self.assertNotEqual(gltf_hash_2, gltf_hash_1)
415415

416-
# Valid glTF file reference an external image.
416+
# Valid glTF file references an external image.
417417
with open(gltf_filename, "w") as f:
418418
f.write(json.dumps({"images": [{"uri": str(png_filename)}]}))
419419
gltf_hash_3 = dut(message)
420420
self.assertNotEqual(gltf_hash_3, empty_hash)
421421
self.assertNotEqual(gltf_hash_3, gltf_hash_2)
422422

423+
# Valid glTF file references an inline image.
424+
with open(gltf_filename, "w") as f:
425+
data_uri = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAgAAZABkAAD"
426+
f.write(json.dumps({"images": [{"uri": data_uri}]}))
427+
gltf_hash_4 = dut(message)
428+
self.assertNotEqual(gltf_hash_4, empty_hash)
429+
self.assertNotEqual(gltf_hash_4, gltf_hash_3)
430+
431+
# Valid(ish) glTF file references a buffer image.
432+
with open(gltf_filename, "w") as f:
433+
f.write(json.dumps({
434+
"buffers": [{"byteLength": 0}],
435+
"bufferViews": [{"buffer": 0, "byteLength": 0}],
436+
"images": [{"bufferView": 0, "mimeType": "image/png"}],
437+
}))
438+
gltf_hash_5 = dut(message)
439+
self.assertNotEqual(gltf_hash_5, empty_hash)
440+
self.assertNotEqual(gltf_hash_5, gltf_hash_4)
441+
423442
# Now finally, the glTF file has a .bin. This time, as a cross-check,
424443
# inspect the filenames that were hashed instead of the hash itself.
425444
bin_filename = test_tmpdir / "mesh_checksum_test.bin"

0 commit comments

Comments
 (0)