【wrl2stl】WRL文件转STL文件-Python

之前有一篇博客写了Avizo自动化批量导出wrl文件:【Avizo&Python】离散颗粒的分割、网格化与单颗粒批量自动保存wrl文件_avizo python-CSDN博客

还有一篇写了wrl转为xyz格式文件:

Wrl文件转XYZ文件-Python_python 打开wrl三维模型-CSDN博客

在这篇博客中,程序仅读取了顶点信息,面片信息直接被丢弃了,这样会丢失一手的网格信息,并不利于后续的数据分析和结构表征。

前几天在用Meshlab的时候,发现可以读取Wrl文件后直接导出stl/obj格式文件:

那就想有没有像Avizo之类的内置python,结果找到了PyMeshLab的官方库:cnr-isti-vclab/PyMeshLab: The open source mesh processing python library

官方手册:PyMeshLab --- PyMeshLab documentation

安装方式(Python >= 3.7 (64 bit)):

bash 复制代码
pip3 install pymeshlab

然后就直接:

python 复制代码
# wrl2stl_0913.py

import pymeshlab
import os
from tqdm import tqdm

wrls = []
gel = r'C:\DataSet_DOOR\dataset_3Dbubble\1008-gel-reexport'


for root, dirs, files in os.walk(gel):
    for file in tqdm(files):
        if file.endswith(".wrl"):
            wrls = os.path.join(root, file)

            ms = pymeshlab.MeshSet()
            ms.load_new_mesh(wrls)
            wrls = wrls.replace('0802gel', 'mesh')
            wrls = wrls.replace('wrl', 'stl')
            # stl_path = os.path.join(gel, '0802gel1-stl', file.replace('wrl', 'stl'))
            ms.save_current_mesh(wrls)

STL还不可以直接用,存在问题如下:

  1. STL的坐标还是Avizo中的坐标,分布在世界各地,需要进行中心化,将中心坐标移动到原点;
  2. 在一个非常不起眼的角落,存在着一个圆球,每个文件中都存在,尺寸固定。这导致对点坐标计算平均值求的点云中心位置的时候出现很大的偏差,导致中心化失败。这是wrl文件中存在的问题,不一定会遇到,但一定要检查。

罪魁祸首(长得像个病毒😡):

参考以下代码,其中还包括了点云超采样,如果不需要可以删掉。

python 复制代码
import os
import cv2
import numpy as np
import pyvista as pv
import matplotlib.pyplot as plt
from tqdm import tqdm

def upsample_point_cloud(points, num_clusters):
    # 点云上采样,返回mesh的点云数量大于num_clusters
    cloud = pv.PolyData(points)
    sample_spacing = 5
    while(1):
        mesh = cloud.reconstruct_surface(nbr_sz = 10, sample_spacing = sample_spacing)
        # 提取顶点作为新的点云
        new_points = np.asarray(mesh.points)
        if new_points.shape[0] < num_clusters * 1.1:
            sample_spacing *= 0.9
        else:
            break
    return mesh

stl_folder = r"C:\DataSet_DOOR\dataset_3Dbubble\1008-gel-reexport\mesh1-filter"
# stl_folder = r"C:\DataSet_DOOR\dataset_3Dbubble\1008-gel-reexport\mesh2-filter"

# 保存点到文件
progress1 = tqdm(os.listdir(stl_folder), dynamic_ncols=True, desc="Read STL", leave=True)
for stl_file in progress1:
    if stl_file.endswith(".stl"):
        stl_path = os.path.join(stl_folder, stl_file)

        mesh = pv.read(stl_path)
        points = np.array(mesh.points, dtype=float)

        distance_to_origin = np.linalg.norm(points, axis=1)
        # 筛出异常球
        points = points[distance_to_origin >= 100] / 1000000
        points -= np.mean(points, axis=0)
        
        # plotter = pv.Plotter()
        # actor = plotter.add_points(points, color = '#c8c8ff')
        # actor = plotter.add_points(np.array([0, 0, 0]), color = '#f14c36')
        # actor = plotter.show_bounds(grid='back',
        #             location='outer',
        #             ticks='both',
        #             n_xlabels=2,
        #             n_ylabels=2,
        #             n_zlabels=2,
        #             xtitle='X',
        #             ytitle='Y',
        #             ztitle='Z',
        #         )
        # plotter.show()
        
        # mesh = upsample_point_cloud(points, 5000)
        
        # 仅进行一次点云上采样
        cloud = pv.PolyData(points)
        mesh = cloud.reconstruct_surface(nbr_sz = 10, sample_spacing = 0.8)
        mesh.smooth_taubin(n_iter=10, pass_band=5, inplace = True)
        mesh = mesh.fill_holes(100)
        mesh.save(stl_path.replace('filter', 'center'))
相关推荐
BUG研究员_3 小时前
LangGraph持久化之失败后恢复运行
python·agent
gb42152873 小时前
数字人面试和RAG区别?
python
宁渡AI大模型3 小时前
AI 全栈面试新趋势:Vibe Coding、前端、Java 后端高频面试题深度解析|河南宁渡科技有限公司编程教程
java·javascript·人工智能·python·ai大模型
Groundwork Explorer3 小时前
ESP32-C3 SuperMini 排查WIFI收发故障
python·单片机·嵌入式硬件·mcu
智购科技自动售货机工厂4 小时前
2026自动售货机端侧AI降本逻辑:从云端API到本地推理的成本重构~YH
人工智能·python·ui·面试·交互
Data_Journal4 小时前
如何将网页抓取用于机器学习
大数据·开发语言·数据库·python·scrapy
郝学胜-神的一滴4 小时前
Effective Python 条款 13:善用星号解包,告别下标切片拆分的坑
服务器·开发语言·数据结构·python·程序人生·pycharm
数据智研4 小时前
【数据分享】300个城市-5G试点城市DID数据(2014-2025)
大数据·运维·人工智能·信息可视化·数据分析
IvanCodes4 小时前
Python 基础语法(五):列表与元组
python