diff --git a/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_ForceField.cpp b/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_ForceField.cpp index 85f2461a..e58068f0 100644 --- a/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_ForceField.cpp +++ b/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_ForceField.cpp @@ -147,36 +147,39 @@ namespace sofapython3 py::object ret = _addKToMatrix(mparams, nNodes, nDofs); + if(!py::isinstance(ret)) + { + throw py::type_error("Can't read return value of AddKToMatrix. A numpy array is expected"); + } + // if ret is numpy array - if(py::isinstance(ret)) + auto r = py::cast(ret); + if (r.ndim() == 3 && r.shape(2) == 1) { - auto r = py::cast(ret); - if (r.ndim() == 3 && r.shape(2) == 1) + // read K as a plain 2D matrix + auto kMatrix = r.unchecked(); + for (size_t x = 0 ; x < size_t(kMatrix.shape(0)) ; ++x) { - // read K as a plain 2D matrix - auto kMatrix = r.unchecked(); - for (size_t x = 0 ; x < size_t(kMatrix.shape(0)) ; ++x) + for (size_t y = 0 ; y < size_t(kMatrix.shape(1)) ; ++y) { - for (size_t y = 0 ; y < size_t(kMatrix.shape(1)) ; ++y) - { - mat->add(int(offset + x), int(offset + y), kMatrix(x,y, 0)); - } + mat->add(int(offset + x), int(offset + y), kMatrix(x,y, 0)); } } - else if (r.ndim() == 2 && r.shape(1) == 3) - { - // consider ret to be a list of tuples [(i,j,[val])] - auto kMatrix = r.unchecked(); - for (auto x = 0 ; x < kMatrix.shape(0) ; ++x) - { - mat->add(int(offset + size_t(kMatrix(x,0))), int(offset + size_t(kMatrix(x,1))), kMatrix(x,2)); - } - } - else + } + else if (r.ndim() == 2 && r.shape(1) == 3) + { + // consider ret to be a list of tuples [(i,j,[val])] + auto kMatrix = r.unchecked(); + for (auto x = 0 ; x < kMatrix.shape(0) ; ++x) { - throw py::type_error("Can't read return value of AddKToMatrix. The method should return either a plain 2D matrix or a vector of tuples (i, j, val)"); + mat->add(int(offset + size_t(kMatrix(x,0))), int(offset + size_t(kMatrix(x,1))), kMatrix(x,2)); } } + else + { + throw py::type_error("Can't read return value of AddKToMatrix. The method should return either a plain 2D matrix or a vector of tuples (i, j, val)"); + } + }