Skip to content

Commit

Permalink
[meshcat] More robust parsing of .mtl texture map filenames (#17550)
Browse files Browse the repository at this point in the history
In RobotLocomotion/models#18
(homecart_basecart.mtl) I ran into a texture map specification that
specified a scale option in the map_Kd line. This defeated the simple
regex expression that I had implemented in meschat.cc.

meshcat.cc doesn't need to parse these options properly; it only needs
to successfully extract the filename so that the file can be shipped
over the websocket.  I've updated the parsing line according to the
mtl specification to ignore options.

To reproduce the problem, run meshcat_manual_test with the change to
box.obj.mtl but without the fix to meshcat.cc.  You will see that the
box appears in meshcat without a texture and a warning from meshcat.cc
is printed to the console.  The change to meshcat.cc resolves the
problem.
RussTedrake authored Jul 13, 2022
1 parent 88630a8 commit afd32b5
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 10 additions & 2 deletions geometry/meshcat.cc
Original file line number Diff line number Diff line change
@@ -406,8 +406,16 @@ class MeshcatShapeReifier : public ShapeReifier {

// Scan .mtl file for map_ lines. For each, load the file and add
// the contents to geometry.resources.
// TODO(russt): Make this parsing more robust.
std::regex map_regex("map_[^\\s]+\\s+([^\\s]+)");
// The syntax (http://paulbourke.net/dataformats/mtl/) is e.g.
// map_Ka -options args filename
// Here we ignore the options and only extract the filename (by
// extracting the last word before the end of line/string).
// - "map_.+" matches the map_ plus any options,
// - "\s" matches one whitespace (before the filename),
// - "[^\s]+" matches the filename, and
// - "[$\r\n]" matches the end of string or end of line.
// TODO(russt): This parsing could still be more robust.
std::regex map_regex(R"""(map_.+\s([^\s]+)[$\r\n])""");
for (std::sregex_iterator iter(meshfile_object.mtl_library.begin(),
meshfile_object.mtl_library.end(),
map_regex);
2 changes: 1 addition & 1 deletion geometry/render/test/meshes/box.obj.mtl
Original file line number Diff line number Diff line change
@@ -5,4 +5,4 @@ Ks 1.000000 1.000000 1.000000
d 1.000000
illum 2
Ns 0.000000
map_Kd box.png
map_Kd -s 1 1 1 box.png

0 comments on commit afd32b5

Please sign in to comment.