错误试图在C+&#x2B中运行Python函数通过pybind11

发布于 2025-02-08 22:30:27 字数 13146 浏览 0 评论 0原文

我正在尝试从C ++程序导出网格到VTU格式。为此,我决定使用meshio Python库,该库已经使用PYBIND11实现了所需的功能。

我正在运行的代码如下:

    py::scoped_interpreter guard{};

    std::vector<std::array<double, 3>> pointsConverted;
    pointsConverted.reserve(points.size());
    for (auto p : points) {
        pointsConverted.push_back(std::array{p[0], p[1], p[2]});
    }

    std::vector<std::array<int, 3>> trianglesConverted;
    trianglesConverted.reserve(triangles.size());
    for (auto triangle : triangles) {
        trianglesConverted.push_back(std::array{triangle[0], triangle[1], triangle[2]});
    }

    const auto pyPoints = py::cast(pointsConverted);
    const auto pyTriangles = py::cast(trianglesConverted);

    py::module::import("sys").attr("path").attr("append")("blablabla");
    const auto pyWrapper = py::module::import("VTKExporter");
    pyWrapper.attr("writeVTU")(pyPoints, pyTriangles);

如您所见,我将数据转换为STL类型,然后将此数据发送到Python中的一个函数,

import meshio


def writeVTU(points, triangles):
    print(points)
    print(triangles)
    meshio.Mesh(points, {"triangle": triangles}).write("test.vtu")

但是,由于以下错误,

[[-0.0, -0.0, -0.0], [-0.0, -0.0, 0.0], [-0.0, 0.0, -0.0], [-0.0, 0.0, 0.0], [0.0, -0.0, -0.0], [0.0, -0.0, 0.0], [0.0, 0.0, -0.0], [0.0, 0.0, 0.0]]                                                                               [48/2685]
[[4, 2, 0], [2, 7, 3], [6, 5, 7], [1, 7, 5], [0, 3, 1], [4, 1, 5], [4, 6, 2], [2, 6, 7], [6, 4, 5], [1, 3, 7], [0, 2, 3], [4, 0, 1]]                                                                                                                                  
CAPTURED EXCEPTION 'SystemError: Objects/structseq.c:481: bad argument to internal function                                                                     
                                                                                                                                                                                                                                            
At:                                                                                                                                                                                                                                         
  /usr/lib/python3.10/site-packages/numpy/core/overrides.py(6): <module>                                                                                                                                                                    
  <frozen importlib._bootstrap>(241): _call_with_frames_removed                                                                                                                                                                             
  <frozen importlib._bootstrap_external>(883): exec_module                                                                                                                                                                                  
  <frozen importlib._bootstrap>(703): _load_unlocked                                                                                                                                                                                        
  <frozen importlib._bootstrap>(1006): _find_and_load_unlocked                                                                                                                                                                              
  <frozen importlib._bootstrap>(1027): _find_and_load                                                                                                                                                                                       
  <frozen importlib._bootstrap>(241): _call_with_frames_removed                                                                                                                                                                             
  <frozen importlib._bootstrap>(1079): _handle_fromlist                                                                                                                                                                                     
  /usr/lib/python3.10/site-packages/numpy/core/multiarray.py(10): <module>                                                                                                                                                                  
  <frozen importlib._bootstrap>(241): _call_with_frames_removed                                                                                                                                                                             
  <frozen importlib._bootstrap_external>(883): exec_module                                                                                                                                                                                  
  <frozen importlib._bootstrap>(703): _load_unlocked                                                                                                                                                                                        
  <frozen importlib._bootstrap>(1006): _find_and_load_unlocked                                                                                                                                                                              
  <frozen importlib._bootstrap>(1027): _find_and_load                                                                                                                                                                                       
  <frozen importlib._bootstrap>(241): _call_with_frames_removed                                                                                                                                                                             
  <frozen importlib._bootstrap>(1079): _handle_fromlist                                                                                                                                                                                     
  /usr/lib/python3.10/site-packages/numpy/core/__init__.py(51): <module>                                                                                                                                                                    
  <frozen importlib._bootstrap>(241): _call_with_frames_removed                                                                                                                                                                             
  <frozen importlib._bootstrap_external>(883): exec_module                                                                                                                                                                                  
  <frozen importlib._bootstrap>(703): _load_unlocked                                                                                                                                                                                        
  <frozen importlib._bootstrap>(1006): _find_and_load_unlocked                                                                                                                                                                              
  <frozen importlib._bootstrap>(1027): _find_and_load                                                                                                                                                                                       
  <frozen importlib._bootstrap>(241): _call_with_frames_removed                                                                                                                                                                             
  <frozen importlib._bootstrap>(1079): _handle_fromlist                                                                                                                                                                                     
  /usr/lib/python3.10/site-packages/numpy/__init__.py(144): <module>                                                                                                                                                                        
  <frozen importlib._bootstrap>(241): _call_with_frames_removed                                                                                                                                                                             
  <frozen importlib._bootstrap_external>(883): exec_module                                                                                                                                                                                  
  <frozen importlib._bootstrap>(703): _load_unlocked                                                                                                                                                                                        
  <frozen importlib._bootstrap>(1006): _find_and_load_unlocked                                                                                                                                                                              
  <frozen importlib._bootstrap>(1027): _find_and_load                                                                                                                                                                                       
  /usr/lib/python3.10/site-packages/meshio/ansys/_ansys.py(8): <module>                                                                                                                                                                     
  <frozen importlib._bootstrap>(241): _call_with_frames_removed                                                                                                                                                                             
  <frozen importlib._bootstrap_external>(883): exec_module                                                                                                                                                                                  
  <frozen importlib._bootstrap>(703): _load_unlocked                                                                                                                                                                                        
  <frozen importlib._bootstrap>(1006): _find_and_load_unlocked                                                                                                                                                                              
  <frozen importlib._bootstrap>(1027): _find_and_load                                                                                                                                                                                       
  /usr/lib/python3.10/site-packages/meshio/ansys/__init__.py(1): <module>                                                                                                                                                                   
  <frozen importlib._bootstrap>(241): _call_with_frames_removed                                                                                                                                                                             
  <frozen importlib._bootstrap_external>(883): exec_module                                                                                                                                                                                  
  <frozen importlib._bootstrap>(703): _load_unlocked                                                                                                                                                                                        
  <frozen importlib._bootstrap>(1006): _find_and_load_unlocked                                                                                                                                                                              
  <frozen importlib._bootstrap>(1027): _find_and_load                                                                                                                                                                                       
  <frozen importlib._bootstrap>(241): _call_with_frames_removed                                                                                                                                                                             
  <frozen importlib._bootstrap>(1079): _handle_fromlist
  /usr/lib/python3.10/site-packages/meshio/_cli/_ascii.py(4): <module>
  <frozen importlib._bootstrap>(241): _call_with_frames_removed
...

我也尝试直接从C ++运行此代码导入meshio带有py :: module :: import(“ meshio”)具有相同错误的模块。

我不明白这个错误或如何解决。任何想法都将受到欢迎。

I am trying to export a mesh to VTU format from a C++ program. To do so, I decided to use the meshio python library which already implements the required functionality using pybind11.

The code I am running is the following:

    py::scoped_interpreter guard{};

    std::vector<std::array<double, 3>> pointsConverted;
    pointsConverted.reserve(points.size());
    for (auto p : points) {
        pointsConverted.push_back(std::array{p[0], p[1], p[2]});
    }

    std::vector<std::array<int, 3>> trianglesConverted;
    trianglesConverted.reserve(triangles.size());
    for (auto triangle : triangles) {
        trianglesConverted.push_back(std::array{triangle[0], triangle[1], triangle[2]});
    }

    const auto pyPoints = py::cast(pointsConverted);
    const auto pyTriangles = py::cast(trianglesConverted);

    py::module::import("sys").attr("path").attr("append")("blablabla");
    const auto pyWrapper = py::module::import("VTKExporter");
    pyWrapper.attr("writeVTU")(pyPoints, pyTriangles);

As you can see, I convert my data to stl types and I send this data to a function in python

import meshio


def writeVTU(points, triangles):
    print(points)
    print(triangles)
    meshio.Mesh(points, {"triangle": triangles}).write("test.vtu")

However, this fails with the following error

[[-0.0, -0.0, -0.0], [-0.0, -0.0, 0.0], [-0.0, 0.0, -0.0], [-0.0, 0.0, 0.0], [0.0, -0.0, -0.0], [0.0, -0.0, 0.0], [0.0, 0.0, -0.0], [0.0, 0.0, 0.0]]                                                                               [48/2685]
[[4, 2, 0], [2, 7, 3], [6, 5, 7], [1, 7, 5], [0, 3, 1], [4, 1, 5], [4, 6, 2], [2, 6, 7], [6, 4, 5], [1, 3, 7], [0, 2, 3], [4, 0, 1]]                                                                                                                                  
CAPTURED EXCEPTION 'SystemError: Objects/structseq.c:481: bad argument to internal function                                                                     
                                                                                                                                                                                                                                            
At:                                                                                                                                                                                                                                         
  /usr/lib/python3.10/site-packages/numpy/core/overrides.py(6): <module>                                                                                                                                                                    
  <frozen importlib._bootstrap>(241): _call_with_frames_removed                                                                                                                                                                             
  <frozen importlib._bootstrap_external>(883): exec_module                                                                                                                                                                                  
  <frozen importlib._bootstrap>(703): _load_unlocked                                                                                                                                                                                        
  <frozen importlib._bootstrap>(1006): _find_and_load_unlocked                                                                                                                                                                              
  <frozen importlib._bootstrap>(1027): _find_and_load                                                                                                                                                                                       
  <frozen importlib._bootstrap>(241): _call_with_frames_removed                                                                                                                                                                             
  <frozen importlib._bootstrap>(1079): _handle_fromlist                                                                                                                                                                                     
  /usr/lib/python3.10/site-packages/numpy/core/multiarray.py(10): <module>                                                                                                                                                                  
  <frozen importlib._bootstrap>(241): _call_with_frames_removed                                                                                                                                                                             
  <frozen importlib._bootstrap_external>(883): exec_module                                                                                                                                                                                  
  <frozen importlib._bootstrap>(703): _load_unlocked                                                                                                                                                                                        
  <frozen importlib._bootstrap>(1006): _find_and_load_unlocked                                                                                                                                                                              
  <frozen importlib._bootstrap>(1027): _find_and_load                                                                                                                                                                                       
  <frozen importlib._bootstrap>(241): _call_with_frames_removed                                                                                                                                                                             
  <frozen importlib._bootstrap>(1079): _handle_fromlist                                                                                                                                                                                     
  /usr/lib/python3.10/site-packages/numpy/core/__init__.py(51): <module>                                                                                                                                                                    
  <frozen importlib._bootstrap>(241): _call_with_frames_removed                                                                                                                                                                             
  <frozen importlib._bootstrap_external>(883): exec_module                                                                                                                                                                                  
  <frozen importlib._bootstrap>(703): _load_unlocked                                                                                                                                                                                        
  <frozen importlib._bootstrap>(1006): _find_and_load_unlocked                                                                                                                                                                              
  <frozen importlib._bootstrap>(1027): _find_and_load                                                                                                                                                                                       
  <frozen importlib._bootstrap>(241): _call_with_frames_removed                                                                                                                                                                             
  <frozen importlib._bootstrap>(1079): _handle_fromlist                                                                                                                                                                                     
  /usr/lib/python3.10/site-packages/numpy/__init__.py(144): <module>                                                                                                                                                                        
  <frozen importlib._bootstrap>(241): _call_with_frames_removed                                                                                                                                                                             
  <frozen importlib._bootstrap_external>(883): exec_module                                                                                                                                                                                  
  <frozen importlib._bootstrap>(703): _load_unlocked                                                                                                                                                                                        
  <frozen importlib._bootstrap>(1006): _find_and_load_unlocked                                                                                                                                                                              
  <frozen importlib._bootstrap>(1027): _find_and_load                                                                                                                                                                                       
  /usr/lib/python3.10/site-packages/meshio/ansys/_ansys.py(8): <module>                                                                                                                                                                     
  <frozen importlib._bootstrap>(241): _call_with_frames_removed                                                                                                                                                                             
  <frozen importlib._bootstrap_external>(883): exec_module                                                                                                                                                                                  
  <frozen importlib._bootstrap>(703): _load_unlocked                                                                                                                                                                                        
  <frozen importlib._bootstrap>(1006): _find_and_load_unlocked                                                                                                                                                                              
  <frozen importlib._bootstrap>(1027): _find_and_load                                                                                                                                                                                       
  /usr/lib/python3.10/site-packages/meshio/ansys/__init__.py(1): <module>                                                                                                                                                                   
  <frozen importlib._bootstrap>(241): _call_with_frames_removed                                                                                                                                                                             
  <frozen importlib._bootstrap_external>(883): exec_module                                                                                                                                                                                  
  <frozen importlib._bootstrap>(703): _load_unlocked                                                                                                                                                                                        
  <frozen importlib._bootstrap>(1006): _find_and_load_unlocked                                                                                                                                                                              
  <frozen importlib._bootstrap>(1027): _find_and_load                                                                                                                                                                                       
  <frozen importlib._bootstrap>(241): _call_with_frames_removed                                                                                                                                                                             
  <frozen importlib._bootstrap>(1079): _handle_fromlist
  /usr/lib/python3.10/site-packages/meshio/_cli/_ascii.py(4): <module>
  <frozen importlib._bootstrap>(241): _call_with_frames_removed
...

I have also tried to run this code directly from C++ import meshio module with py::module::import("meshio") with the same error.

I dont understand this error or how to solve it. Any ideas will be welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文