Skip to content

isDirty returning always false with writeable access #313

@hugtalbot

Description

@hugtalbot

Originally posted by @ScheiklP in #290 (comment)


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

Metadata

Metadata

Assignees

No one assigned

    Labels

    issue: bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions