SofaPython3 icon indicating copy to clipboard operation
SofaPython3 copied to clipboard

isDirty returning always false with writeable access

Open hugtalbot opened this issue 3 years ago • 0 comments

Originally posted by @ScheiklP in https://github.com/sofa-framework/SofaPython3/issues/290#issuecomment-1284069903


When trying to write into MechanicalObject.position, with writeable() or __setitem__, SofaPython3 checks if the data is dirty or not in memcache. For some reason, the d->isDirty() check always evaluates to false. So after calling the function the first time, d is in memcache and the not-updated version of the array is used. This is why len(MechanicalObject.position.writeable() is incorrect.

https://github.com/sofa-framework/SofaPython3/blob/master/Plugin/src/SofaPython3/DataHelper.cpp#L430-L445

py::array getPythonArrayFor(BaseData* d)
{
    auto& memcache = getObjectCache();
    if(d->isDirty() || memcache.find(d) == memcache.end())
    {
        auto capsule = py::capsule(new Base::SPtr(d->getOwner()), [](void*p){ delete static_cast<Base::SPtr*>(p); } );

        py::buffer_info ninfo = toBufferInfo(*d);
        py::array a(pybind11::dtype(ninfo), ninfo.shape,
                    ninfo.strides, ninfo.ptr, capsule);

        memcache[d] = a;
        return a;
    }
    return memcache[d];
}

Since the binding itself is functional, I would like to merge it, and then open another issue to address the actual problem.

  • Long term, by making sure that d->isDirty() correctly evaluates to true.
  • Short term, by checking if the shapes of d and memcache[d] are the same.

@damienmarchal @epernod

hugtalbot avatar Oct 25 '22 16:10 hugtalbot