1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
| from typing import Dict import bpy import bmesh import mathutils import numpy as np import os import shutil import sys import time
class SimplifyObj: def __init__(self, input_dir: str, output_dir: str): self.input_dir = input_dir self.output_dir = output_dir
def clear_scene(self): bpy.ops.wm.read_factory_settings(use_empty=True)
def ensure_dir(self, path: str): if not os.path.exists(path): os.makedirs(path)
def simplify_and_bake( self, obj_file: str, decimate_ratio: float, tile_num=5, is_single_obj=True, rotation_matrix=None, ): if rotation_matrix is not None and ( not isinstance(rotation_matrix, list) or len(rotation_matrix) != 4 or not all( isinstance(row, list) and len(row) == 4 for row in rotation_matrix ) ): raise ValueError("旋转矩阵必须是4x4的嵌套列表") start_time = time.perf_counter() folder_path = os.path.dirname(obj_file) rel_path = os.path.relpath(folder_path, self.input_dir) base_name = os.path.splitext(os.path.basename(obj_file))[0] output_subdir = os.path.join(self.output_dir, rel_path)
self.ensure_dir(output_subdir) self.clear_scene()
bpy.ops.wm.obj_import(filepath=obj_file) high_poly = bpy.context.selected_objects[0] high_poly.name = "HighPoly"
if rotation_matrix is not None: matrix = mathutils.Matrix(rotation_matrix) high_poly.matrix_world = matrix @ high_poly.matrix_world if is_single_obj and decimate_ratio >= 1.0: for item in os.listdir(folder_path): s = os.path.join(folder_path, item) d = os.path.join(output_subdir, item) if os.path.isfile(s): shutil.copy2(s, d) print(f"文件复制完成: {folder_path} -> {output_subdir}") return
print( f"简化前({high_poly.name}): 顶点数: {len(high_poly.data.vertices)}, 面数: {len(high_poly.data.polygons)}" )
low_poly = high_poly.copy() low_poly.data = high_poly.data.copy() low_poly.name = "LowPoly" bpy.context.collection.objects.link(low_poly)
low_poly.data.materials.clear() if low_poly.data.uv_layers: for uv_layer in list(low_poly.data.uv_layers): low_poly.data.uv_layers.remove(uv_layer)
bpy.context.view_layer.objects.active = low_poly decimate_mod = low_poly.modifiers.new(name="Decimate", type="DECIMATE") decimate_mod.ratio = decimate_ratio bpy.ops.object.modifier_apply(modifier=decimate_mod.name) print( f"简化后({low_poly.name}): 顶点数: {len(low_poly.data.vertices)}, 面数: {len(low_poly.data.polygons)}" )
bm = bmesh.new() bm.from_mesh(low_poly.data) face_centroids = [face.calc_center_median()[:] for face in bm.faces] bm.free()
centroids_np = np.array(face_centroids)
np.random.seed(0) indices = np.random.choice(len(centroids_np), tile_num, replace=False) centers = centroids_np[indices]
while True: distances = np.linalg.norm( centroids_np[:, None, :] - centers[None, :, :], axis=2 ) labels = np.argmin(distances, axis=1) new_centers = np.array( [ centroids_np[labels == i].mean(axis=0) if np.any(labels == i) else centers[i] for i in range(tile_num) ] ) if np.allclose(centers, new_centers, atol=1e-6): break centers = new_centers
tiles = [] for i in range(tile_num): bpy.ops.object.select_all(action="DESELECT") low_poly.select_set(True) bpy.context.view_layer.objects.active = low_poly bpy.ops.object.duplicate() tile_obj = bpy.context.active_object tile_obj.name = f"{base_name}_{i + 1}"
bpy.ops.object.mode_set(mode="EDIT") bm_tile = bmesh.from_edit_mesh(tile_obj.data)
for idx, face in enumerate(bm_tile.faces): if labels[idx] != i: face.select = True else: face.select = False
bmesh.update_edit_mesh(tile_obj.data) bpy.ops.mesh.delete(type="FACE") bpy.ops.object.mode_set(mode="OBJECT") tiles.append(tile_obj)
for tile in tiles: bpy.ops.object.select_all(action="DESELECT") bpy.context.view_layer.objects.active = tile tile.select_set(True) bpy.ops.object.mode_set(mode="EDIT") bpy.ops.mesh.select_all(action="SELECT") bpy.ops.uv.smart_project() bpy.ops.uv.select_all(action="SELECT") bpy.ops.uv.pack_islands() bpy.ops.uv.select_all(action="DESELECT") bpy.ops.object.mode_set(mode="OBJECT") tile.select_set(False)
for tile in tiles: image = bpy.data.images.new(name=f"{tile.name}", width=4096, height=4096)
mat = bpy.data.materials.new(name=f"{tile.name}") mat.use_nodes = True nodes = mat.node_tree.nodes links = mat.node_tree.links
for node in list(nodes): if node.type != "OUTPUT_MATERIAL": nodes.remove(node)
bsdf = nodes.new(type="ShaderNodeBsdfPrincipled") tex_image = nodes.new(type="ShaderNodeTexImage") tex_image.image = image output = nodes.get("Material Output")
links.new(tex_image.outputs["Color"], bsdf.inputs["Base Color"]) links.new(bsdf.outputs["BSDF"], output.inputs["Surface"])
if len(tile.data.materials) > 0: tile.data.materials[0] = mat else: tile.data.materials.append(mat)
bpy.context.scene.render.engine = "CYCLES" bpy.context.scene.cycles.device = "CPU" bpy.context.scene.cycles.use_denoising = False bpy.context.scene.cycles.bake_type = "DIFFUSE" bpy.context.scene.render.bake.use_pass_color = True bpy.context.scene.render.bake.use_pass_direct = False bpy.context.scene.render.bake.use_pass_indirect = False bpy.context.scene.render.bake.use_selected_to_active = True bpy.context.scene.render.bake.cage_extrusion = 0.1 bpy.context.scene.render.bake.use_clear = True
for tile in tiles: for obj in bpy.data.objects: if obj != tile and obj != high_poly: obj.hide_viewport = True obj.hide_render = True
bpy.ops.object.select_all(action="DESELECT") tile.select_set(True) high_poly.select_set(True) bpy.context.view_layer.objects.active = tile
mat = tile.data.materials[0] nodes = mat.node_tree.nodes tex_nodes = [n for n in nodes if n.type == "TEX_IMAGE"] if tex_nodes: mat.node_tree.nodes.active = tex_nodes[0]
bpy.ops.object.bake(type="DIFFUSE")
baked_image = tex_nodes[0].image baked_image.filepath_raw = f"{output_dir}/{baked_image.name}.jpg" baked_image.file_format = "JPEG" baked_image.save(quality=30)
for obj in bpy.data.objects: obj.hide_viewport = False obj.hide_render = False
print("3D reconstruction baking complete.")
bpy.ops.object.select_all(action="DESELECT")
for tile in tiles: tile.select_set(True)
bpy.context.view_layer.objects.active = tiles[0]
bpy.ops.object.join()
combined_obj = bpy.context.active_object combined_obj.name = base_name
export_path = os.path.join(output_subdir, f"{base_name}.obj") bpy.ops.wm.obj_export( filepath=export_path, export_selected_objects=True, export_materials=True, path_mode="COPY", export_uv=True, export_normals=False, ) elapsed = (time.perf_counter() - start_time) / 60 print(f"简化和烘焙完成: {export_path}, 耗时: {elapsed:.4f}分钟")
def batch_process(self): obj_files: Dict[str, int] = {} for root, _, files in os.walk(self.input_dir): for file in files: if file.endswith(".obj"): obj_file = os.path.join(root, file) obj_dir = os.path.dirname(obj_file) rel_path = os.path.relpath(obj_dir, self.input_dir) bpy.ops.wm.read_factory_settings(use_empty=True) bpy.ops.wm.obj_import(filepath=obj_file) obj = bpy.context.selected_objects[0] obj_files[obj_file] = len(obj.data.polygons) bpy.ops.wm.read_factory_settings(use_empty=True) break
total_files = len(obj_files) total_face_count = sum(obj_files.values())
individual_max_face_count = int( os.getenv("MULTI_OBJ_INDIVIDUAL_MAX_FACE_COUNT", 500000) ) all_obj_max_face_count = int( os.getenv("MULTI_OBJ_ALL_OBJ_MAX_FACE_COUNT", 2500000) ) single_obj_skip_face_num = int( os.getenv("SINGLE_OBJ_SKIP_SIMPLIFY_FACE_NUM", 1000000) ) obj_face_simplify_target: Dict[str, int] = {} if total_files == 1: single_obj_skip_face_num = int( os.getenv("SINGLE_OBJ_SKIP_SIMPLIFY_FACE_NUM", 1000000) ) print( f"总面数 {total_face_count},按照阈值 {single_obj_skip_face_num} 裁剪" ) obj_face_simplify_target[obj_file] = min( total_face_count, single_obj_skip_face_num, ) elif total_face_count > all_obj_max_face_count: print( f"总面数 {total_face_count} 超过限制 {all_obj_max_face_count},按比例裁剪" ) for obj_file, face_count in obj_files.items(): if face_count > 0: obj_face_simplify_target[obj_file] = min( int(face_count / total_face_count * all_obj_max_face_count), individual_max_face_count, ) else: print( f"总面数 {total_face_count} 在限制 {all_obj_max_face_count} 内,仅限制单个文件面数" ) for obj_file, face_count in obj_files.items(): obj_face_simplify_target[obj_file] = min( face_count, individual_max_face_count )
for obj_file, face_count in obj_files.items(): rotation_matrix = [ [0, 0, 1, 0], [0, 1, 0, 0], [-1, 0, 0, 0], [0, 0, 0, 1], ] rotation_matrix = None self.simplify_and_bake( obj_file=obj_file, decimate_ratio=obj_face_simplify_target[obj_file]/total_face_count, is_single_obj=total_files == 1, rotation_matrix=rotation_matrix, )
print("✅ 所有模型处理完毕。")
if __name__ == "__main__": args = sys.argv if "--" in args: idx = args.index("--") script_args = args[idx + 1 :] else: script_args = []
if len(script_args) != 2: print("❌ 用法错误:请提供 input_dir 和 output_dir 参数") print( "例如: blender --background --python script.py -- /path/to/input /path/to/output" ) sys.exit(1)
input_dir, output_dir = script_args task = SimplifyObj(input_dir, output_dir) task.batch_process()
|