(input_path: str, iterations: int, tmp_dir: str)
| 229 | |
| 230 | |
| 231 | def _smooth(input_path: str, iterations: int, tmp_dir: str) -> trimesh.Trimesh: |
| 232 | loaded = trimesh.load(input_path) |
| 233 | if isinstance(loaded, trimesh.Scene): |
| 234 | geoms = list(loaded.geometry.values()) |
| 235 | geom = trimesh.util.concatenate(geoms) if len(geoms) > 1 else geoms[0] |
| 236 | else: |
| 237 | geom = loaded |
| 238 | |
| 239 | ms = _pymeshlab.MeshSet() |
| 240 | |
| 241 | if _has_texture(geom): |
| 242 | obj_in = os.path.join(tmp_dir, "input.obj") |
| 243 | mtl_in = os.path.join(tmp_dir, "input.mtl") |
| 244 | tex_in = os.path.join(tmp_dir, "texture.png") |
| 245 | obj_out = os.path.join(tmp_dir, "output.obj") |
| 246 | |
| 247 | _get_texture_image(geom).save(tex_in) |
| 248 | geom.export(obj_in) |
| 249 | |
| 250 | if os.path.exists(mtl_in): |
| 251 | mtl = open(mtl_in).read() |
| 252 | mtl = re.sub(r"map_Kd\s+\S+", "map_Kd texture.png", mtl) |
| 253 | open(mtl_in, "w").write(mtl) |
| 254 | |
| 255 | ms.load_new_mesh(obj_in) |
| 256 | ms.apply_coord_laplacian_smoothing(stepsmoothnum=iterations) |
| 257 | ms.save_current_mesh(obj_out) |
| 258 | |
| 259 | mtl_out = obj_out.replace(".obj", ".mtl") |
| 260 | if os.path.exists(mtl_out): |
| 261 | mtl = open(mtl_out).read() |
| 262 | mtl = re.sub(r"map_Kd\s+\S+", "map_Kd texture.png", mtl) |
| 263 | open(mtl_out, "w").write(mtl) |
| 264 | |
| 265 | return trimesh.load(obj_out) |
| 266 | |
| 267 | else: |
| 268 | ply_in = os.path.join(tmp_dir, "input.ply") |
| 269 | ply_out = os.path.join(tmp_dir, "output.ply") |
| 270 | |
| 271 | geom.export(ply_in) |
| 272 | ms.load_new_mesh(ply_in) |
| 273 | ms.apply_coord_laplacian_smoothing(stepsmoothnum=iterations) |
| 274 | ms.save_current_mesh(ply_out) |
| 275 | return trimesh.load(ply_out, force="mesh") |
| 276 | |
| 277 | |
| 278 | class ImportByPathRequest(BaseModel): |
no test coverage detected