
缩小1000倍单位从m换成mmimport bpy import os import glob from mathutils import Vector def clear_scene(): 清空当前场景的所有物体 # 选择所有物体 bpy.ops.object.select_all(actionSELECT) # 删除所有选中的物体 bpy.ops.object.delete(use_globalFalse) def import_glb(filepath): 导入 GLB 文件 # 导入 GLB bpy.ops.import_scene.gltf(filepathfilepath) # 获取导入的所有物体 imported_objects [obj for obj in bpy.context.selected_objects] if not imported_objects: # 如果没有选中的物体尝试获取场景中的所有物体 imported_objects [obj for obj in bpy.data.objects if obj.type in {MESH, ARMATURE, EMPTY}] return imported_objects def scale_objects(objects, scale_factor): 缩放物体列表中的所有物体 if not objects: print(警告没有找到需要缩放的物体) return False # 记录当前选中的物体 previous_selection bpy.context.selected_objects.copy() previous_active bpy.context.active_object # 全不选 bpy.ops.object.select_all(actionDESELECT) # 选择要缩放的物体 for obj in objects: if obj and obj.name in bpy.data.objects: obj.select_set(True) # 设置活动物体 if objects: bpy.context.view_layer.objects.active objects[0] # 执行缩放 bpy.ops.transform.resize(value(scale_factor, scale_factor, scale_factor)) # 应用缩放变换 bpy.ops.object.transform_apply(scaleTrue, locationFalse, rotationFalse) # 恢复之前的选中状态 bpy.ops.object.select_all(actionDESELECT) for obj in previous_selection: if obj and obj.name in bpy.data.objects: obj.select_set(True) if previous_active and previous_active.name in bpy.data.objects: bpy.context.view_layer.objects.active previous_active return True def export_to_glb(filepath): 导出为 GLB 文件 # 确保导出时使用正确的单位设置 bpy.context.scene.unit_settings.system METRIC bpy.context.scene.unit_settings.scale_length 0.001 # 毫米单位 # 导出 GLB bpy.ops.export_scene.gltf(filepathfilepath, export_formatGLB, export_applyTrue, export_yupTrue, export_texcoordsTrue, export_normalsTrue, export_draco_mesh_compression_enableFalse, ) return True def process_glb_files(input_dir, output_dirNone, scale_factor0.001): 批量处理 GLB 文件 参数: input_dir: 输入目录路径 output_dir: 输出目录路径如果为 None则覆盖原文件 scale_factor: 缩放因子默认 0.001米→毫米 # 查找所有 GLB 文件 glb_files glob.glob(os.path.join(input_dir, *.glb)) glb_files.extend(glob.glob(os.path.join(input_dir, *.GLB))) if not glb_files: print(f错误在目录 {input_dir} 中没有找到 GLB 文件) return False print(f找到 {len(glb_files)} 个 GLB 文件) print( * 60) # 创建输出目录如果需要 if output_dir and not os.path.exists(output_dir): os.makedirs(output_dir) print(f创建输出目录{output_dir}) success_count 0 fail_count 0 for i, input_file in enumerate(glb_files, 1): print(f\n[{i}/{len(glb_files)}] 处理: {os.path.basename(input_file)}) try: # 清空场景 clear_scene() # 导入 GLB print( → 导入文件...) imported_objects import_glb(input_file) if not imported_objects: print( ✗ 导入失败没有找到物体) fail_count 1 continue print(f → 导入完成找到 {len(imported_objects)} 个物体) # 缩放物体 print(f → 缩放物体 (因子: {scale_factor})...) if not scale_objects(imported_objects, scale_factor): print( ✗ 缩放失败) fail_count 1 continue print( ✓ 缩放完成) # 确定输出路径 if output_dir: output_file os.path.join(output_dir, os.path.basename(input_file)) else: # 覆盖原文件先保存到临时文件再替换 temp_file input_file .tmp output_file input_file # 导出 GLB print(f → 导出文件...) if export_to_glb(output_file): print(f ✓ 导出成功: {os.path.basename(output_file)}) success_count 1 # 如果使用临时文件替换原文件 if not output_dir and output_file input_file: pass # 直接覆盖 else: print(f ✗ 导出失败) fail_count 1 except Exception as e: print(f ✗ 处理失败: {str(e)}) fail_count 1 # 清理临时文件 if not output_dir: temp_files glob.glob(os.path.join(input_dir, *.tmp)) for temp_file in temp_files: try: os.remove(temp_file) except: pass print(\n * 60) print(f处理完成) print(f成功: {success_count} 个) print(f失败: {fail_count} 个) print( * 60) return success_count 0 def main(): 主函数 - 配置在这里修改 # 输入目录包含需要转换的 GLB 文件 INPUT_DIR rE:\project\3d_label\three-bvh-csg-main\examples\kakou # 输出目录None 表示覆盖原文件否则指定新目录 OUTPUT_DIR rE:\project\3d_label\three-bvh-csg-main\examples\kakou_s # OUTPUT_DIR None # 取消注释此行可覆盖原文件 # 缩放因子0.001 米转毫米1 不变1000 毫米转米 SCALE_FACTOR 0.001 # 检查输入目录 if not os.path.exists(INPUT_DIR): print(f错误输入目录不存在 - {INPUT_DIR}) return # 询问确认 print(f输入目录: {INPUT_DIR}) if OUTPUT_DIR: print(f输出目录: {OUTPUT_DIR}) else: print(f输出模式: 覆盖原文件不可恢复) print(f缩放因子: {SCALE_FACTOR} (米→毫米)) print() # 处理文件 process_glb_files(INPUT_DIR, OUTPUT_DIR, SCALE_FACTOR) # 运行主函数 if __name__ __main__: main()