Skip to content

Commit 78bf8d2

Browse files
Fix lint
Signed-off-by: Shameek Ganguly <shameek@intrinsic.ai>
1 parent 0e43abd commit 78bf8d2

File tree

3 files changed

+49
-32
lines changed

3 files changed

+49
-32
lines changed

sdformat_mjcf/src/sdformat_mjcf/sdformat_to_mjcf/converters/geometry.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616

1717
import os
1818

19-
from sdformat_mjcf.sdformat_to_mjcf.mesh_io import convert_mesh_to_obj_multimesh
19+
from sdformat_mjcf.sdformat_to_mjcf.mesh_io import (
20+
convert_mesh_to_obj_multimesh,
21+
)
2022
from sdformat_mjcf.sdformat_to_mjcf.converters.material import add_material
2123
import sdformat_mjcf.utils.sdf_utils as su
2224

2325
COLLISION_GEOM_GROUP = 3
2426
VISUAL_GEOM_GROUP = 0
2527

28+
2629
def _set_mesh_inertia(mjcf_mesh, is_visual):
2730
if is_visual:
2831
# Some visual meshes are too thin and can cause inertia
@@ -38,9 +41,9 @@ def _validate_uri(uri):
3841
if 'http://' in uri or 'https://' in uri:
3942
raise RuntimeError("Fuel meshes are not yet supported")
4043
if 'model://' in uri:
41-
prefix = 'model://'
42-
# TODO: Support sdf::ParserConfig::AddURIPath to resolve URIs.
43-
return uri.replace(prefix, '', 1)
44+
prefix = 'model://'
45+
# TODO: Support sdf::ParserConfig::AddURIPath to resolve URIs.
46+
return uri.replace(prefix, '', 1)
4447
return uri
4548

4649

@@ -190,7 +193,8 @@ def convert_and_add_mesh(body, name, pose, sdf_mesh, is_visual=False):
190193
if sub_mesh_loaded:
191194
geom_list.append(
192195
_add_mesh_geom_with_assets(body, name, pose, sub_mesh_loaded,
193-
mjcf_material_asset=material_loaded))
196+
mjcf_material_asset=material_loaded)
197+
)
194198
continue
195199

196200
# Add mesh and material assets
@@ -199,14 +203,13 @@ def convert_and_add_mesh(body, name, pose, sdf_mesh, is_visual=False):
199203
mesh.scale = su.vec3d_to_list(sdf_mesh.scale())
200204
material_asset = None
201205
if is_visual:
202-
material_asset = body.root.asset.add("material",
203-
name=sub_mesh_material_asset_name,
204-
specular=info.mat.specular,
205-
shininess=info.mat.shininess,
206-
rgba=info.mat.rgba)
206+
material_asset = body.root.asset.add(
207+
"material", name=sub_mesh_material_asset_name,
208+
specular=info.mat.specular, shininess=info.mat.shininess,
209+
rgba=info.mat.rgba)
207210
geom_list.append(
208211
_add_mesh_geom_with_assets(body, name, pose, mesh,
209-
mjcf_material_asset=material_asset))
212+
mjcf_material_asset=material_asset))
210213

211214
return geom_list
212215

@@ -241,10 +244,10 @@ def add_collision(body, col):
241244
if _is_unsupported_mesh_geo(col.geometry()):
242245
sdf_mesh = col.geometry().mesh_shape()
243246
geoms = convert_and_add_mesh(body, col.name(), pose, sdf_mesh,
244-
is_visual=False)
247+
is_visual=False)
245248
else:
246249
geom = add_geometry(body, col.name(), pose, col.geometry(),
247-
is_visual=False)
250+
is_visual=False)
248251
geoms = [geom]
249252
for geom in geoms:
250253
geom.group = COLLISION_GEOM_GROUP
@@ -270,10 +273,10 @@ def add_visual(body, vis):
270273
if _is_unsupported_mesh_geo(vis.geometry()):
271274
sdf_mesh = vis.geometry().mesh_shape()
272275
geoms = convert_and_add_mesh(body, vis.name(), pose, sdf_mesh,
273-
is_visual=True)
276+
is_visual=True)
274277
else:
275278
geom = add_geometry(body, vis.name(), pose, vis.geometry(),
276-
is_visual=True)
279+
is_visual=True)
277280
geoms = [geom]
278281

279282
for geom in geoms:

sdformat_mjcf/src/sdformat_mjcf/sdformat_to_mjcf/mesh_io.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55
import trimesh
66

7+
78
@dataclass
89
class Material:
910
specular: float
@@ -30,7 +31,7 @@ def _extract_material_info(mesh_material) -> Material:
3031
NOTE: Removed explicit type hint for mesh_material to avoid the
3132
'BaseMaterial' error, relying on runtime checks instead.
3233
"""
33-
default_rgba = [0.5, 0.5, 0.5, 1.0] # Default grey
34+
default_rgba = [0.5, 0.5, 0.5, 1.0] # Default grey
3435

3536
# Simple defaults for common material properties
3637
# TODO: Make this configurable
@@ -72,27 +73,33 @@ def _extract_material_info(mesh_material) -> Material:
7273
rgba=rgba
7374
)
7475

75-
def convert_mesh_to_obj_multimesh(input_filepath: str, output_filepath: str) -> ConversionOutput:
76+
77+
def convert_mesh_to_obj_multimesh(input_filepath: str,
78+
output_filepath: str) -> ConversionOutput:
7679
"""
7780
Converts an input mesh file (e.g., glb, glTF) into multiple OBJ files,
7881
one for each mesh in the input scene.
7982
8083
Args:
8184
input_filepath (str): Path to the input mesh file.
82-
output_filepath (str): Base path for output files. Each mesh will be saved as
83-
<base_path>_<mesh_name>.obj
85+
output_filepath (str): Base path for output files. Each mesh will be
86+
saved as <base_path>_<mesh_name>.obj
8487
8588
Returns:
86-
ConversionOutput: An object containing a dictionary mapping the generated
87-
OBJ filenames to their extracted material properties.
89+
ConversionOutput: An object containing a dictionary mapping the
90+
generated OBJ filenames to their extracted material
91+
properties.
8892
"""
8993
if not os.path.exists(input_filepath):
90-
raise RuntimeError(f"Unable to find the input mesh file {input_filepath}")
94+
raise RuntimeError(
95+
f"Unable to find the input mesh file {input_filepath}"
96+
)
9197

9298
try:
9399
model = trimesh.load(input_filepath, force='scene')
94100
except Exception as e:
95-
print(f"\nERROR: Failed to load file: {input_filepath}. Check if the file is valid.")
101+
print(f"\nERROR: Failed to load file: {input_filepath}. Check if the "
102+
"file is valid.")
96103
print(f"Detail: {e}")
97104
sys.exit(1)
98105

@@ -114,23 +121,28 @@ def convert_mesh_to_obj_multimesh(input_filepath: str, output_filepath: str) ->
114121

115122
# Check if the geometry name is in the loaded geometry map
116123
if G_name not in model.geometry:
117-
print(f"WARNING: Geometry '{G_name}' referenced in graph but not in geometry map. Skipping.")
124+
print(f"WARNING: Geometry '{G_name}' referenced in graph but not "
125+
"in geometry map. Skipping.")
118126
continue
119127

120128
mesh = model.geometry[G_name]
121129

122130
if not isinstance(mesh, trimesh.Trimesh):
123-
print(f"Skipping geometry '{G_name}' as it's not a Trimesh object.")
131+
print(f"Skipping geometry '{G_name}' as it's not a Trimesh "
132+
"object.")
124133
continue
125134

126-
# Apply the transform to the mesh's vertices *before* exporting the OBJ.
127-
# This writes the mesh at its correctly scaled/positioned size to the OBJ.
135+
# Apply the transform to the mesh's vertices *before* exporting the
136+
# OBJ. This writes the mesh at its correctly scaled/positioned size
137+
# to the OBJ.
128138
transformed_mesh = mesh.copy()
129139
transformed_mesh.apply_transform(transform_matrix)
130140

131141
# Use the geometry name and path for a unique filename
132142
if len(model.geometry) > 1:
133-
safe_name = G_name.replace(' ', '_').replace('/', '_').replace('\\', '_')
143+
safe_name = (
144+
G_name.replace(' ', '_').replace('/', '_').replace('\\', '_')
145+
)
134146
obj_filename = f"{base_path}_{safe_name}_{count}.obj"
135147
count += 1
136148
else:
@@ -145,10 +157,12 @@ def convert_mesh_to_obj_multimesh(input_filepath: str, output_filepath: str) ->
145157
extracted_material = None
146158
if mesh.visual.material is not None:
147159
# Use the material from the original mesh
148-
extracted_material = _extract_material_info(mesh.visual.material)
160+
extracted_material = _extract_material_info(
161+
mesh.visual.material)
149162

150163
if extracted_material is None:
151-
extracted_material = Material(specular=0.5, shininess=30.0, rgba=[0.5, 0.5, 0.5, 1.0])
164+
extracted_material = Material(specular=0.5, shininess=30.0,
165+
rgba=[0.5, 0.5, 0.5, 1.0])
152166

153167
mesh_info = MeshInfo(mat=extracted_material)
154168

sdformat_mjcf/tests/sdformat_to_mjcf/test_add_geometry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def test_mesh(self):
128128
mujoco = mjcf.RootElement(model="test")
129129
body = mujoco.worldbody.add('body')
130130
mj_geoms = geometry_conv.convert_and_add_mesh(body, "mesh_shape",
131-
self.test_pose, mesh)
131+
self.test_pose, mesh)
132132
self.assertEqual(1, len(mj_geoms))
133133
mj_geom = mj_geoms[0]
134134
self.assertEqual("mesh_shape", mj_geom.name)
@@ -146,7 +146,7 @@ def test_glb_mesh(self):
146146
mujoco = mjcf.RootElement(model="test")
147147
body = mujoco.worldbody.add('body')
148148
mj_geoms = geometry_conv.convert_and_add_mesh(body, "mesh_shape",
149-
self.test_pose, mesh)
149+
self.test_pose, mesh)
150150
self.assertEqual(4, len(mj_geoms))
151151

152152
actual_types = [geom.type for geom in mj_geoms]

0 commit comments

Comments
 (0)