44import sys
55import trimesh
66
7+
78@dataclass
89class 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"\n ERROR: Failed to load file: { input_filepath } . Check if the file is valid." )
101+ print (f"\n ERROR: 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
0 commit comments