如何在UnstructuredGrid外调用PyVista Plotter类的各类方法?
解决PyVista中UnstructuredGrid调用Plotter类方法的问题
当然可以使用enable_point_picking这类Plotter类的方法,只是不能直接通过UnstructuredGrid实例调用。grid.plot()是PyVista提供的快捷绘图方式,内部会自动创建一个临时的Plotter实例,但这个实例不会暴露给你操作。要使用Plotter的交互方法,你需要手动创建Plotter对象,步骤如下:
具体实现步骤
- 手动初始化
Plotter实例 - 将你的
UnstructuredGrid添加到Plotter中 - 调用
enable_point_picking开启点选择功能 - 启动绘图窗口
示例代码:
import pyvista as pv # 假设你的UnstructuredGrid已经创建好,命名为grid grid = pv.UnstructuredGrid(...) # 你的网格数据 # 1. 创建Plotter实例 plotter = pv.Plotter() # 2. 添加网格到Plotter plotter.add_mesh(grid) # 3. 开启点选择功能,可自定义回调函数处理选中的点 def pick_callback(point_id): print(f"选中的点ID: {point_id}") plotter.enable_point_picking(callback=pick_callback, show_message=True) # 4. 显示绘图窗口 plotter.show()
额外说明
enable_point_picking支持多个参数自定义交互行为,比如设置pickable指定可选择元素类型、tolerance调整选择精度等,你可以根据需求调整。- 如果需要选择单元格而非点,可以使用
enable_cell_picking方法,用法类似。
内容的提问来源于stack exchange,提问作者man-teiv
相关产品推荐
相关产品推荐

